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 #pragma section CODE=IRAM,attr=CODE
16 void FLASH_PrepareWriteHalfWordMode()
18 /* Set FLASH Access Mode via BootROM Routine */
19 /* For details refer to the Hardware Manual or Data Sheet */
24 LDI #0x01,R4 ; Set FLASH to 16Bit read/write Mode
25 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
33 /* Set the FLASH Interface to Write Timing */
34 /* For details refer to the Hardware Manual or Data Sheet */
35 /* Setting shown here is for CLKB = 64MHz */
41 void FLASH_PrepareReadMode()
43 /* Set FLASH Access Mode via BootROM Routine */
44 /* For details refer to the Hardware Manual or Data Sheet */
49 LDI #0x00,R4 ; Set FLASH to 32Bit read/write Mode
50 LDI #0x04,R5 ; Go 4 times through delay loop (64MHz CLKB)
58 /* Set the FLASH Interface to Read Timing */
59 /* For details refer to the Hardware Manual or Data Sheet */
60 /* Setting shown here is for CLKB = 64MHz */
66 unsigned char FLASH_ChipErase(void)
68 unsigned char flag = 0;
70 /*Disable Interrupts if necessary*/
71 IFlag = FLASH_SaveDisableInterruptFlag();
73 /*Set FLASH access mode to 16Bit Write Mode*/
74 FLASH_PrepareWriteHalfWordMode();
76 /*Start FLASH Sector Erase Sequence*/
84 /*Wait for the Auto Algorithm to finish*/
86 /* Feed Hardware Watchdog */
92 if(*hseq_1 & TLOVER) {
97 /*Reset FLASH (keep in mind 16Bit access to FLASH)*/
98 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
105 /*Set FLASH access mode to 32Bit Read Mode*/
106 FLASH_PrepareReadMode();
108 /*Restore the original Interrupt Flag*/
109 FLASH_RestoreInterruptFlag(IFlag);
114 unsigned char FLASH_SectorErase(unsigned int secadr)
116 unsigned char flag = 0;
117 volatile unsigned int value = 0;
119 /* Disable Interrupts if necessary */
120 IFlag = FLASH_SaveDisableInterruptFlag();
122 /* Set FLASH access mode to 16Bit Write Mode */
123 FLASH_PrepareWriteHalfWordMode();
127 /* Start FLASH Sector Erase Sequence */
133 *(unsigned short int *)secadr = 0x0030;
135 /* Wait for the Auto Algorithm to start */
136 while( !( *(unsigned short int *)secadr & SETIMR ) )
138 /* Feed the Hardware Watchdog */
141 /* Check for Pending Interrupts */
142 if( FLASH_CheckPendingInterrupt() )
144 /* Wait for Sector Erase Suspend */
145 FLASH_SuspendSectorErase(secadr);
147 /* Set FLASH access mode to 32Bit Read Mode */
148 FLASH_PrepareReadMode();
150 /* Restore the original Interrupt Flag */
151 FLASH_RestoreInterruptFlag(IFlag);
153 /* Keep on checking for pending Interrupts */
154 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
156 /* Disable Interrupts if necessary */
157 IFlag = FLASH_SaveDisableInterruptFlag();
159 /* Set FLASH access mode to 16Bit Write Mode */
160 FLASH_PrepareWriteHalfWordMode();
162 /* Sector Erase Resume */
163 FLASH_ResumeSectorErase(secadr);
167 /* Wait for the Auto Algorithm to finish */
170 /* Feed Hardware Watchdog */
173 /* Check for Pending Interrupts */
174 if( FLASH_CheckPendingInterrupt() )
176 /* Sector Erase Suspend */
177 FLASH_SuspendSectorErase(secadr);
179 /* Set FLASH access mode to 32Bit Read Mode */
180 FLASH_PrepareReadMode();
182 /* Restore the original Interrupt Flag */
183 FLASH_RestoreInterruptFlag(IFlag);
185 /* Keep on checking for pending Interrupts */
186 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
188 /* Disable Interrupts if necessary */
189 IFlag = FLASH_SaveDisableInterruptFlag();
191 /* Set FLASH access mode to 16Bit Write Mode */
192 FLASH_PrepareWriteHalfWordMode();
194 /* Sector Erase Resume */
195 FLASH_ResumeSectorErase(secadr);
199 /* Check the Hardware Sequence Flags */
200 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
204 if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
206 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
221 /* Restore the original Interrupt Flag */
222 FLASH_RestoreInterruptFlag(IFlag);
224 /* Set FLASH access mode to 32Bit Read Mode */
225 FLASH_PrepareReadMode();
230 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
233 unsigned char empty_flag = 0;
234 unsigned int addr = secaddr;
236 /* Clear FIXE bit to see FLASH memory content instead of fixed reset vector */
239 for(count = 0; count < size; count ++)
241 /* Clear Hardware Watchdog */
243 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
247 /* Set FIXE bit to see fixed reset vector */
250 if( empty_flag != 0 )
258 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
260 unsigned char flag = 0;
262 /* Disable Interrupts if necessary */
263 IFlag = FLASH_SaveDisableInterruptFlag();
265 /* Set FLASH access mode to 16Bit Write Mode */
266 FLASH_PrepareWriteHalfWordMode();
268 /* Start Write FLASH Sequence */
272 *((volatile unsigned short int *)adr) = data;
274 /* Wait for the Auto Algorithm to finish */
277 /* Feed Hardware Watchdog */
280 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
284 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
286 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
292 /* Reset FLASH (keep in mind 16Bit access to FLASH) */
293 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
300 /* Set FLASH access mode to 32Bit Read Mode */
301 FLASH_PrepareReadMode();
303 /* Restore the original Interrupt Flag */
304 FLASH_RestoreInterruptFlag(IFlag);
310 unsigned char FLASH_ReadReset()
318 _FLASH_SaveDisableInterruptFlag:
322 AND R0,R4 ; Store Original Flag
323 ANDCCR #0xFFFFFFEF ; Clear Interrupt Flag
330 _FLASH_RestoreInterruptFlag:
332 MOV PS,R0 ; Get current PS
333 OR R4,R0 ; Set Flag as saved
334 MOV R0,PS ; Write back PS
340 unsigned char FLASH_SuspendSectorErase(unsigned int secaddr)
342 /* Write Sector Erase Suspend Command */
343 *(volatile unsigned short int *)secaddr = 0x00B0;
345 /* Wait for the FLASH macro to suspend sector erase */
346 while(!(*(unsigned short int *)secaddr /* value */ & DPOLL) && (*(unsigned short int *)secaddr /* value */ & SETIMR))
354 unsigned char FLASH_ResumeSectorErase(unsigned int secaddr)
356 /* Write the Sector Erase Resume Command */
357 *(volatile unsigned short int *)secaddr = 0x0030;
359 /* Wait for the FLASH Macro to resume sector erase */
360 while((*(unsigned short int *)secaddr /*value */ & DPOLL) && !(*(unsigned short int *)secaddr /*value */ & SETIMR))
368 unsigned char FLASH_CheckPendingInterrupt()
370 /* Poll for Pending Interrupts which are needed here */
371 if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0 */
373 /* and return 1 when an Interrupt is pending */