-unsigned char FLASH_SectorErase(unsigned int secadr)
-{
- unsigned char flag = 0;
- volatile unsigned int value = 0;
-
- /* Set FLASH access mode to 16Bit Write Mode */
- FLASH_PrepareWriteHalfWordMode();
-
- secadr |= 0x0003;
-
- /* Start FLASH Sector Erase Sequence */
- *hseq_1 = 0x00AA;
- *hseq_2 = 0x0055;
- *hseq_1 = 0x0080;
- *hseq_1 = 0x00AA;
- *hseq_2 = 0x0055;
- *(unsigned short int *)secadr = 0x0030;
-
- /* Wait for the Auto Algorithm to start */
- while( !( *(unsigned short int *)secadr & SETIMR ) )
- {
- /* Feed the Hardware Watchdog */
- HWWD_CL = 0;
-
- /* Check for Pending Interrupts */
- if( FLASH_CheckPendingInterrupt() )
- {
- /* Wait for Sector Erase Suspend */
- FLASH_SuspendSectorErase(secadr);
-
- /* Set FLASH access mode to 32Bit Read Mode */
- FLASH_PrepareReadMode();
-
- /* Keep on checking for pending Interrupts */
- while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
-
- /* Set FLASH access mode to 16Bit Write Mode */
- FLASH_PrepareWriteHalfWordMode();
-
- /* Sector Erase Resume */
- FLASH_ResumeSectorErase(secadr);
- }
- }
-
- /* Wait for the Auto Algorithm to finish */
- while( flag == 0 )
- {
- /* Feed Hardware Watchdog */
- HWWD_CL = 0;
-
- /* Check for Pending Interrupts */
- if( FLASH_CheckPendingInterrupt() )
- {
- /* Sector Erase Suspend */
- FLASH_SuspendSectorErase(secadr);
-
- /* Set FLASH access mode to 32Bit Read Mode */
- FLASH_PrepareReadMode();
-
- /* Keep on checking for pending Interrupts */
- while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
-
- /* Set FLASH access mode to 16Bit Write Mode */
- FLASH_PrepareWriteHalfWordMode();
-
- /* Sector Erase Resume */
- FLASH_ResumeSectorErase(secadr);
- }
-
-
- /* Check the Hardware Sequence Flags */
- if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
- {
- flag = 1;
- }
- if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
- {
- if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
- {
- flag = 1;
- }
- else
- {
- /* Reset FLASH */
- FLASH_ReadReset();
-
- flag = 2;
- }
- }
- }
- /* Set FLASH access mode to 32Bit Read Mode */
- FLASH_PrepareReadMode();
-
- return flag;
-}
-