1 /*****************************************************************************/
2 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
3 /* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
4 /* ELIGIBILITY FOR ANY PURPOSES. */
5 /* (C) Fujitsu Microelectronics Europe GmbH */
6 /*****************************************************************************/
11 void FLASH_PrepareWriteHalfWordMode()
13 /* Set FLASH Access Mode via BootROM Routine */
14 /* For details refer to the Hardware Manual or Data Sheet */
19 LDI #0x01,R4 ; Set FLASH to 16Bit read/write Mode
20 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
28 /* Set the FLASH Interface to Write Timing */
29 /* For details refer to the Hardware Manual or Data Sheet */
30 /* Setting shown here is for CLKB = 64MHz */
36 void FLASH_PrepareReadMode()
38 /* Set FLASH Access Mode via BootROM Routine */
39 /* For details refer to the Hardware Manual or Data Sheet */
44 LDI #0x00,R4 ; Set FLASH to 32Bit read/write Mode
45 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
53 /* Set the FLASH Interface to Read Timing */
54 /* For details refer to the Hardware Manual or Data Sheet */
55 /* Setting shown here is for CLKB = 64MHz */
61 unsigned char FLASH_ChipErase(void)
63 unsigned char flag = 0;
65 /*Set FLASH access mode to 16Bit Write Mode*/
66 FLASH_PrepareWriteHalfWordMode();
68 /*Start FLASH Sector Erase Sequence*/
76 /*Wait for the Auto Algorithm to finish*/
78 /* Feed Hardware Watchdog */
84 if(*hseq_1 & TLOVER) {
89 /*Reset FLASH (keep in mind 16Bit access to FLASH)*/
90 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
97 /*Set FLASH access mode to 32Bit Read Mode*/
98 FLASH_PrepareReadMode();
103 unsigned char FLASH_SectorErase(unsigned int secadr)
105 unsigned char flag = 0;
106 volatile unsigned int value = 0;
108 /* Set FLASH access mode to 16Bit Write Mode */
109 FLASH_PrepareWriteHalfWordMode();
113 /* Start FLASH Sector Erase Sequence */
119 *(unsigned short int *)secadr = 0x0030;
121 /* Wait for the Auto Algorithm to start */
122 while( !( *(unsigned short int *)secadr & SETIMR ) )
124 /* Feed the Hardware Watchdog */
127 /* Check for Pending Interrupts */
128 if( FLASH_CheckPendingInterrupt() )
130 /* Wait for Sector Erase Suspend */
131 FLASH_SuspendSectorErase(secadr);
133 /* Set FLASH access mode to 32Bit Read Mode */
134 FLASH_PrepareReadMode();
136 /* Keep on checking for pending Interrupts */
137 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
139 /* Set FLASH access mode to 16Bit Write Mode */
140 FLASH_PrepareWriteHalfWordMode();
142 /* Sector Erase Resume */
143 FLASH_ResumeSectorErase(secadr);
147 /* Wait for the Auto Algorithm to finish */
150 /* Feed Hardware Watchdog */
153 /* Check for Pending Interrupts */
154 if( FLASH_CheckPendingInterrupt() )
156 /* Sector Erase Suspend */
157 FLASH_SuspendSectorErase(secadr);
159 /* Set FLASH access mode to 32Bit Read Mode */
160 FLASH_PrepareReadMode();
162 /* Keep on checking for pending Interrupts */
163 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
165 /* Set FLASH access mode to 16Bit Write Mode */
166 FLASH_PrepareWriteHalfWordMode();
168 /* Sector Erase Resume */
169 FLASH_ResumeSectorErase(secadr);
173 /* Check the Hardware Sequence Flags */
174 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
178 if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
180 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
193 /* Set FLASH access mode to 32Bit Read Mode */
194 FLASH_PrepareReadMode();
199 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
202 unsigned char empty_flag = 0;
203 unsigned int addr = secaddr;
205 /* Clear FIXE bit to see FLASH memory content instead of fixed reset vector */
208 for(count = 0; count < size; count ++)
210 /* Clear Hardware Watchdog */
212 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
216 /* Set FIXE bit to see fixed reset vector */
219 if( empty_flag != 0 )
227 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
229 unsigned char flag = 0;
231 /* Set FLASH access mode to 16Bit Write Mode */
232 FLASH_PrepareWriteHalfWordMode();
234 /* Start Write FLASH Sequence */
238 *((volatile unsigned short int *)adr) = data;
240 /* Wait for the Auto Algorithm to finish */
243 /* Feed Hardware Watchdog */
246 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
250 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
252 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
258 /* Reset FLASH (keep in mind 16Bit access to FLASH) */
259 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
266 /* Set FLASH access mode to 32Bit Read Mode */
267 FLASH_PrepareReadMode();
273 unsigned char FLASH_ReadReset()
281 unsigned char FLASH_SuspendSectorErase(unsigned int secaddr)
283 /* Write Sector Erase Suspend Command */
284 *(volatile unsigned short int *)secaddr = 0x00B0;
286 /* Wait for the FLASH macro to suspend sector erase */
287 while(!(*(unsigned short int *)secaddr /* value */ & DPOLL) && (*(unsigned short int *)secaddr /* value */ & SETIMR))
295 unsigned char FLASH_ResumeSectorErase(unsigned int secaddr)
297 /* Write the Sector Erase Resume Command */
298 *(volatile unsigned short int *)secaddr = 0x0030;
300 /* Wait for the FLASH Macro to resume sector erase */
301 while((*(unsigned short int *)secaddr /*value */ & DPOLL) && !(*(unsigned short int *)secaddr /*value */ & SETIMR))
309 unsigned char FLASH_CheckPendingInterrupt()
311 /* Poll for Pending Interrupts which are needed here */
312 if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0 */
314 /* and return 1 when an Interrupt is pending */