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 */
67 unsigned char FLASH_SectorErase(unsigned int secadr)
69 unsigned char flag = 0;
70 volatile unsigned int value = 0;
72 /* Disable Interrupts if necessary */
73 IFlag = FLASH_SaveDisableInterruptFlag();
75 /* Set FLASH access mode to 16Bit Write Mode */
76 FLASH_PrepareWriteHalfWordMode();
80 /* Start FLASH Sector Erase Sequence */
86 *(unsigned short int *)secadr = 0x0030;
88 /* Wait for the Auto Algorithm to start */
89 while( !( *(unsigned short int *)secadr & SETIMR ) )
91 /* Feed the Hardware Watchdog */
94 /* Check for Pending Interrupts */
95 if( FLASH_CheckPendingInterrupt() )
97 /* Wait for Sector Erase Suspend */
98 FLASH_SuspendSectorErase(secadr);
100 /* Set FLASH access mode to 32Bit Read Mode */
101 FLASH_PrepareReadMode();
103 /* Restore the original Interrupt Flag */
104 FLASH_RestoreInterruptFlag(IFlag);
106 /* Keep on checking for pending Interrupts */
107 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
109 /* Disable Interrupts if necessary */
110 IFlag = FLASH_SaveDisableInterruptFlag();
112 /* Set FLASH access mode to 16Bit Write Mode */
113 FLASH_PrepareWriteHalfWordMode();
115 /* Sector Erase Resume */
116 FLASH_ResumeSectorErase(secadr);
120 /* Wait for the Auto Algorithm to finish */
123 /* Feed Hardware Watchdog */
126 /* Check for Pending Interrupts */
127 if( FLASH_CheckPendingInterrupt() )
129 /* Sector Erase Suspend */
130 FLASH_SuspendSectorErase(secadr);
132 /* Set FLASH access mode to 32Bit Read Mode */
133 FLASH_PrepareReadMode();
135 /* Restore the original Interrupt Flag */
136 FLASH_RestoreInterruptFlag(IFlag);
138 /* Keep on checking for pending Interrupts */
139 while( FLASH_CheckPendingInterrupt() ) HWWD_CL = 0;
141 /* Disable Interrupts if necessary */
142 IFlag = FLASH_SaveDisableInterruptFlag();
144 /* Set FLASH access mode to 16Bit Write Mode */
145 FLASH_PrepareWriteHalfWordMode();
147 /* Sector Erase Resume */
148 FLASH_ResumeSectorErase(secadr);
152 /* Check the Hardware Sequence Flags */
153 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
157 if( ( *(unsigned short int *)secadr /* value */ & TLOVER ) )
159 if( ( *(unsigned short int *)secadr /* value */ & DPOLL ) )
174 /* Restore the original Interrupt Flag */
175 FLASH_RestoreInterruptFlag(IFlag);
177 /* Set FLASH access mode to 32Bit Read Mode */
178 FLASH_PrepareReadMode();
183 unsigned char FLASH_SectorBlankCheck(unsigned int secaddr, unsigned int size)
186 unsigned char empty_flag = 0;
187 unsigned int addr = secaddr;
189 /* Clear FIXE bit to see FLASH memory content instead of fixed reset vector */
192 for(count = 0; count < size; count ++)
194 /* Clear Hardware Watchdog */
196 if( *(unsigned int *)addr != 0xFFFFFFFF ) empty_flag = 1;
200 /* Set FIXE bit to see fixed reset vector */
203 if( empty_flag != 0 )
211 unsigned char FLASH_WriteHalfWord(unsigned int adr, unsigned short int data)
213 unsigned char flag = 0;
215 /* Disable Interrupts if necessary */
216 IFlag = FLASH_SaveDisableInterruptFlag();
218 /* Set FLASH access mode to 16Bit Write Mode */
219 FLASH_PrepareWriteHalfWordMode();
221 /* Start Write FLASH Sequence */
225 *((volatile unsigned short int *)adr) = data;
227 /* Wait for the Auto Algorithm to finish */
230 /* Feed Hardware Watchdog */
233 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
237 if( ( *(volatile unsigned short int *)adr & TLOVER ) == TLOVER )
239 if( ( *(volatile unsigned short int *)adr & DPOLL ) == (data & DPOLL) )
245 /* Reset FLASH (keep in mind 16Bit access to FLASH) */
246 *hseq_1 = 0x00F0; // Keep in Mind (16Bit access)
253 /* Set FLASH access mode to 32Bit Read Mode */
254 FLASH_PrepareReadMode();
256 /* Restore the original Interrupt Flag */
257 FLASH_RestoreInterruptFlag(IFlag);
263 unsigned char FLASH_ReadReset()
271 _FLASH_SaveDisableInterruptFlag:
275 AND R0,R4 ; Store Original Flag
276 ANDCCR #0xFFFFFFEF ; Clear Interrupt Flag
283 _FLASH_RestoreInterruptFlag:
285 MOV PS,R0 ; Get current PS
286 OR R4,R0 ; Set Flag as saved
287 MOV R0,PS ; Write back PS
293 unsigned char FLASH_SuspendSectorErase(unsigned int secaddr)
295 /* Write Sector Erase Suspend Command */
296 *(volatile unsigned short int *)secaddr = 0x00B0;
298 /* Wait for the FLASH macro to suspend sector erase */
299 while(!(*(unsigned short int *)secaddr /* value */ & DPOLL) && (*(unsigned short int *)secaddr /* value */ & SETIMR))
307 unsigned char FLASH_ResumeSectorErase(unsigned int secaddr)
309 /* Write the Sector Erase Resume Command */
310 *(volatile unsigned short int *)secaddr = 0x0030;
312 /* Wait for the FLASH Macro to resume sector erase */
313 while((*(unsigned short int *)secaddr /*value */ & DPOLL) && !(*(unsigned short int *)secaddr /*value */ & SETIMR))
321 unsigned char FLASH_CheckPendingInterrupt()
323 /* Poll for Pending Interrupts which are needed here */
324 if(TMCSR0_UF) return 1; /* in this sample the only interrupt source supervised is Reload Timer 0 */
326 /* and return 1 when an Interrupt is pending */