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 static unsigned int IFlag;
13 void FLASH_PrepareWriteHalfWordMode()
15 /* Set FLASH Access Mode via BootROM Routine */
16 /* For details refer to the Hardware Manual or Data Sheet */
21 LDI #0x01,R4 ; Set FLASH to 16Bit read/write Mode
22 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
30 /* Set the FLASH Interface to Write Timing */
31 /* For details refer to the Hardware Manual or Data Sheet */
32 /* Setting shown here is for CLKB = 64MHz */
38 void FLASH_PrepareReadMode()
40 /* Set FLASH Access Mode via BootROM Routine */
41 /* For details refer to the Hardware Manual or Data Sheet */
46 LDI #0x00,R4 ; Set FLASH to 32Bit read/write Mode
47 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
55 /* Set the FLASH Interface to Read Timing */
56 /* For details refer to the Hardware Manual or Data Sheet */
57 /* Setting shown here is for CLKB = 64MHz */
63 unsigned char FLASH_ChipErase(void)
65 unsigned char flag = 0;
67 /*Disable Interrupts if necessary*/
68 IFlag = FLASH_SaveDisableInterruptFlag();
70 /*Set FLASH access mode to 16Bit Write Mode*/
71 FLASH_PrepareWriteHalfWordMode();
73 /*Start FLASH Sector Erase Sequence*/
81 /*Wait for the Auto Algorithm to finish*/
83 /* Feed Hardware Watchdog */
89 if(*hseq_1 & TLOVER) {
94 /*Reset FLASH (keep in mind 16Bit access to FLASH)*/
95 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
102 /*Set FLASH access mode to 32Bit Read Mode*/
103 FLASH_PrepareReadMode();
105 /*Restore the original Interrupt Flag*/
106 FLASH_RestoreInterruptFlag(IFlag);
111 unsigned char FLASH_SectorErase(unsigned int secadr)
113 unsigned char flag = 0;
114 volatile unsigned int value = 0;
116 /* Disable Interrupts if necessary */
117 IFlag = FLASH_SaveDisableInterruptFlag();
119 /* Set FLASH access mode to 16Bit Write Mode */
120 FLASH_PrepareWriteHalfWordMode();
124 /* Start FLASH Sector Erase Sequence */
130 *(unsigned short int *)secadr = 0x0030;
132 /* Wait for the Auto Algorithm to start */
133 while( !( *(unsigned short int *)secadr & SETIMR ) )
135 /* Feed the Hardware Watchdog */
138 /* Check for Pending Interrupts */
139 if( FLASH_CheckPendingInterrupt() )
141 /* Wait for Sector Erase Suspend */
142 FLASH_SuspendSectorErase(secadr);
144 /* Set FLASH access mode to 32Bit Read Mode */
145 FLASH_PrepareReadMode();
147 /* Restore the original Interrupt Flag */
148 FLASH_RestoreInterruptFlag(IFlag);
150 /* Keep on checking for pending Interrupts */
151 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
153 /* Disable Interrupts if necessary */
154 IFlag = FLASH_SaveDisableInterruptFlag();
156 /* Set FLASH access mode to 16Bit Write Mode */
157 FLASH_PrepareWriteHalfWordMode();
159 /* Sector Erase Resume */
160 FLASH_ResumeSectorErase(secadr);
164 /* Wait for the Auto Algorithm to finish */
167 /* Feed Hardware Watchdog */
170 /* Check for Pending Interrupts */
171 if( FLASH_CheckPendingInterrupt() )
173 /* Sector Erase Suspend */
174 FLASH_SuspendSectorErase(secadr);
176 /* Set FLASH access mode to 32Bit Read Mode */
177 FLASH_PrepareReadMode();
179 /* Restore the original Interrupt Flag */
180 FLASH_RestoreInterruptFlag(IFlag);
182 /* Keep on checking for pending Interrupts */
183 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
185 /* Disable Interrupts if necessary */
186 IFlag = FLASH_SaveDisableInterruptFlag();
188 /* Set FLASH access mode to 16Bit Write Mode */
189 FLASH_PrepareWriteHalfWordMode();
191 /* Sector Erase Resume */
192 FLASH_ResumeSectorErase(secadr);
196 /* Check the Hardware Sequence Flags */
197 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
201 if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
203 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
218 /* Restore the original Interrupt Flag */
219 FLASH_RestoreInterruptFlag(IFlag);
221 /* Set FLASH access mode to 32Bit Read Mode */
222 FLASH_PrepareReadMode();
227 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
230 unsigned char empty_flag = 0;
231 unsigned int addr = secaddr;
233 /* Clear FIXE bit to see FLASH memory content instead of fixed reset vector */
236 for(count = 0; count < size; count ++)
238 /* Clear Hardware Watchdog */
240 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
244 /* Set FIXE bit to see fixed reset vector */
247 if( empty_flag != 0 )
255 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
257 unsigned char flag = 0;
259 /* Disable Interrupts if necessary */
260 IFlag = FLASH_SaveDisableInterruptFlag();
262 /* Set FLASH access mode to 16Bit Write Mode */
263 FLASH_PrepareWriteHalfWordMode();
265 /* Start Write FLASH Sequence */
269 *((volatile unsigned short int *)adr) = data;
271 /* Wait for the Auto Algorithm to finish */
274 /* Feed Hardware Watchdog */
277 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
281 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
283 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
289 /* Reset FLASH (keep in mind 16Bit access to FLASH) */
290 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
297 /* Set FLASH access mode to 32Bit Read Mode */
298 FLASH_PrepareReadMode();
300 /* Restore the original Interrupt Flag */
301 FLASH_RestoreInterruptFlag(IFlag);
307 unsigned char FLASH_ReadReset()
315 _FLASH_SaveDisableInterruptFlag:
319 AND R0,R4 ; Store Original Flag
320 ANDCCR #0xFFFFFFEF ; Clear Interrupt Flag
327 _FLASH_RestoreInterruptFlag:
329 MOV PS,R0 ; Get current PS
330 OR R4,R0 ; Set Flag as saved
331 MOV R0,PS ; Write back PS
337 unsigned char FLASH_SuspendSectorErase(unsigned int secaddr)
339 /* Write Sector Erase Suspend Command */
340 *(volatile unsigned short int *)secaddr = 0x00B0;
342 /* Wait for the FLASH macro to suspend sector erase */
343 while(!(*(unsigned short int *)secaddr /* value */ & DPOLL) && (*(unsigned short int *)secaddr /* value */ & SETIMR))
351 unsigned char FLASH_ResumeSectorErase(unsigned int secaddr)
353 /* Write the Sector Erase Resume Command */
354 *(volatile unsigned short int *)secaddr = 0x0030;
356 /* Wait for the FLASH Macro to resume sector erase */
357 while((*(unsigned short int *)secaddr /*value */ & DPOLL) && !(*(unsigned short int *)secaddr /*value */ & SETIMR))
365 unsigned char FLASH_CheckPendingInterrupt()
367 /* Poll for Pending Interrupts which are needed here */
368 if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0 */
370 /* and return 1 when an Interrupt is pending */