4 void FLASH_PrepareWriteHalfWordMode()
6 /* Set FLASH Access Mode via BootROM Routine */
7 /* For details refer to the Hardware Manual or Data Sheet */
12 LDI #0x01,R4 ; Set FLASH to 16Bit read/write Mode
13 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
21 /* Set the FLASH Interface to Write Timing */
22 /* For details refer to the Hardware Manual or Data Sheet */
23 /* Setting shown here is for CLKB = 64MHz */
29 void FLASH_PrepareReadMode()
31 /* Set FLASH Access Mode via BootROM Routine */
32 /* For details refer to the Hardware Manual or Data Sheet */
37 LDI #0x00,R4 ; Set FLASH to 32Bit read/write Mode
38 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
46 /* Set the FLASH Interface to Read Timing */
47 /* For details refer to the Hardware Manual or Data Sheet */
48 /* Setting shown here is for CLKB = 64MHz */
54 unsigned char FLASH_ChipErase(void)
56 unsigned char flag = 0;
58 /*Set FLASH access mode to 16Bit Write Mode*/
59 FLASH_PrepareWriteHalfWordMode();
61 /*Start FLASH Sector Erase Sequence*/
69 /*Wait for the Auto Algorithm to finish*/
71 /* Feed Hardware Watchdog */
77 if(*hseq_1 & TLOVER) {
82 /*Reset FLASH (keep in mind 16Bit access to FLASH)*/
83 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
90 /*Set FLASH access mode to 32Bit Read Mode*/
91 FLASH_PrepareReadMode();
96 unsigned char FLASH_SectorErase(unsigned int secadr)
98 unsigned char flag = 0;
99 volatile unsigned int value = 0;
101 /* Set FLASH access mode to 16Bit Write Mode */
102 FLASH_PrepareWriteHalfWordMode();
106 /* Start FLASH Sector Erase Sequence */
112 *(unsigned short int *)secadr = 0x0030;
114 /* Wait for the Auto Algorithm to start */
115 while( !( *(unsigned short int *)secadr & SETIMR ) )
117 /* Feed the Hardware Watchdog */
120 /* Check for Pending Interrupts */
121 if( FLASH_CheckPendingInterrupt() )
123 /* Wait for Sector Erase Suspend */
124 FLASH_SuspendSectorErase(secadr);
126 /* Set FLASH access mode to 32Bit Read Mode */
127 FLASH_PrepareReadMode();
129 /* Keep on checking for pending Interrupts */
130 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
132 /* Set FLASH access mode to 16Bit Write Mode */
133 FLASH_PrepareWriteHalfWordMode();
135 /* Sector Erase Resume */
136 FLASH_ResumeSectorErase(secadr);
140 /* Wait for the Auto Algorithm to finish */
143 /* Feed Hardware Watchdog */
146 /* Check for Pending Interrupts */
147 if( FLASH_CheckPendingInterrupt() )
149 /* Sector Erase Suspend */
150 FLASH_SuspendSectorErase(secadr);
152 /* Set FLASH access mode to 32Bit Read Mode */
153 FLASH_PrepareReadMode();
155 /* Keep on checking for pending Interrupts */
156 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
158 /* Set FLASH access mode to 16Bit Write Mode */
159 FLASH_PrepareWriteHalfWordMode();
161 /* Sector Erase Resume */
162 FLASH_ResumeSectorErase(secadr);
166 /* Check the Hardware Sequence Flags */
167 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
171 if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
173 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
186 /* Set FLASH access mode to 32Bit Read Mode */
187 FLASH_PrepareReadMode();
192 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
195 unsigned char empty_flag = 0;
196 unsigned int addr = secaddr;
198 /* Clear FIXE bit to see FLASH memory content instead of fixed reset vector */
201 for(count = 0; count < size; count ++)
203 /* Clear Hardware Watchdog */
205 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
209 /* Set FIXE bit to see fixed reset vector */
212 if( empty_flag != 0 )
220 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
222 unsigned char flag = 0;
224 /* Set FLASH access mode to 16Bit Write Mode */
225 FLASH_PrepareWriteHalfWordMode();
227 /* Start Write FLASH Sequence */
231 *((volatile unsigned short int *)adr) = data;
233 /* Wait for the Auto Algorithm to finish */
236 /* Feed Hardware Watchdog */
239 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
243 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
245 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
251 /* Reset FLASH (keep in mind 16Bit access to FLASH) */
252 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
259 /* Set FLASH access mode to 32Bit Read Mode */
260 FLASH_PrepareReadMode();
266 unsigned char FLASH_ReadReset()
274 unsigned char FLASH_SuspendSectorErase(unsigned int secaddr)
276 /* Write Sector Erase Suspend Command */
277 *(volatile unsigned short int *)secaddr = 0x00B0;
279 /* Wait for the FLASH macro to suspend sector erase */
280 while(!(*(unsigned short int *)secaddr /* value */ & DPOLL) && (*(unsigned short int *)secaddr /* value */ & SETIMR))
288 unsigned char FLASH_ResumeSectorErase(unsigned int secaddr)
290 /* Write the Sector Erase Resume Command */
291 *(volatile unsigned short int *)secaddr = 0x0030;
293 /* Wait for the FLASH Macro to resume sector erase */
294 while((*(unsigned short int *)secaddr /*value */ & DPOLL) && !(*(unsigned short int *)secaddr /*value */ & SETIMR))
302 unsigned char FLASH_CheckPendingInterrupt()
304 /* Poll for Pending Interrupts which are needed here */
305 if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0 */
307 /* and return 1 when an Interrupt is pending */