PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
work_flash.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 
42 /******************************************************************************/
52 /*---------------------------------------------------------------------------*/
53 /* include files */
54 /*---------------------------------------------------------------------------*/
55 #include "work_flash.h"
56 
57 #if defined(PDL_PERIPHERAL_WORK_FLASH_ACTIVE)
58 
64 
71 #define Flash_Read(addr) *(volatile uint16_t*)((uint32_t)(addr))
72 
80 #define Flash_Write(addr, data) *(volatile uint16_t*)((uint32_t)(addr)) = ( uint16_t)(data)
81 
82 /*---------------------------------------------------------------------------*/
83 /* Local functions */
84 /*---------------------------------------------------------------------------*/
85 static uint8_t WFlash_CheckToggle( uint16_t* pAddr );
86 static void WFlash_ReadResetCmd(uint16_t* pu16ResetSecAddr);
87 
98 static void WFlash_ReadResetCmd(uint16_t* pu16ResetSecAddr)
99 {
100  uint8_t u8Dummy;
101 
102  /* issue read/reset command */
103  Flash_Write(0x0000, 0xF0U) ;
104  u8Dummy = Flash_Read(pu16ResetSecAddr) ;
105  if(u8Dummy) /* avoid warning */
106  ;
107  return ;
108 }
109 
121 uint8_t WFlash_ChipErase(void)
122 {
123  uint8_t u8Dummy;
124  uint8_t u8RetValue = WFLASH_RET_OK;
125 
126  FM4_WORKFLASH_IF->WFASZR = 0x00;
127  u8Dummy = FM4_WORKFLASH_IF->WFASZR;
128  if(u8Dummy) ;
129 
136 
137  /* if execution result of the automatic algorithm of flash memory is abnormally completed */
139  {
140  /* sending the read/reset command to the reset sector */
142 
143  /* return flash operation abnormally */
144  u8RetValue = WFLASH_RET_ABNORMAL ;
145  }
146 
147  /* CPU ROM mode setting */
148  FM4_WORKFLASH_IF->WFASZR = 0x01;
149  u8Dummy = FM4_WORKFLASH_IF->WFASZR;
150 
151  return u8RetValue;
152 }
153 
167 uint8_t WFlash_SectorErase(uint16_t* pu16SecAddr)
168 {
169  uint8_t Dummy;
170  uint8_t u8Ret = WFLASH_RET_OK;
171  if(pu16SecAddr == (uint16_t*)0 )
172  u8Ret = WFLASH_RET_INVALID_PARA;
173  else
174  {
175  FM4_WORKFLASH_IF->WFASZR = 0x00;
176  Dummy = FM4_WORKFLASH_IF->WFASZR;
177  if(Dummy) ;
178 
184  Flash_Write(pu16SecAddr, 0x0030);
185 
186  /* if execution result of the automatic algorithm of flash memory is abnormally completed */
187  if( WFlash_CheckToggle(pu16SecAddr) == WFLASH_CHK_TOGG_ABNORMAL)
188  {
189  /* sending the read/reset command to the reset sector */
190  WFlash_ReadResetCmd(pu16SecAddr) ;
191 
192  /* return flash operation abnormally */
193  u8Ret = WFLASH_RET_ABNORMAL ;
194  }
195 
196  /* CPU ROM mode setting */
197  FM4_WORKFLASH_IF->WFASZR = 0x01;
198  Dummy = FM4_WORKFLASH_IF->WFASZR;
199 
200  }
201  return u8Ret;
202 }
203 
219 uint8_t WFlash_Write(uint16_t* pu16WriteAddr,
220  uint16_t* pu16WriteData,
221  uint32_t u32Size)
222 {
223  uint8_t u8RetValue = WFLASH_RET_OK ;
224  uint32_t u32Cnt;
225  uint8_t u8Dummy;
226 
227  if((pu16WriteAddr == (uint16_t*)0)
228  || ((u32Size%2) != 0))
229  {
230  /* return parameter invalid */
231  u8RetValue = WFLASH_RET_INVALID_PARA ;
232  }
233  else
234  {
235  /* CPU programming mode setting */
236  FM4_WORKFLASH_IF->WFASZR = 0x00;
237  u8Dummy = FM4_WORKFLASH_IF->WFASZR;
238  if(u8Dummy) ;
239 
240  for(u32Cnt=u32Size;u32Cnt;u32Cnt--)
241  {
242  /* issue write command */
246  Flash_Write(pu16WriteAddr, (uint16_t)*pu16WriteData);
247 
248  /* execution result of the automatic algorithm of flash memory is abnormally complete or verify error */
249  if(( WFlash_CheckToggle(pu16WriteAddr) == WFLASH_CHK_TOGG_ABNORMAL) ||
250  ( Flash_Read(pu16WriteAddr) != *pu16WriteData))
251  {
252  /* issue read/reset command to reset sector */
253  WFlash_ReadResetCmd(pu16WriteAddr) ;
254 
255  /* return flash operation abnormally */
256  u8RetValue = WFLASH_RET_ABNORMAL ;
257  }
258  /* Prepare next h-word write */
259  pu16WriteAddr++;
260  pu16WriteData++;
261  }
262 
263  /* CPU ROM mode setting */
264  FM4_WORKFLASH_IF->WFASZR = 0x01;
265  u8Dummy = FM4_WORKFLASH_IF->WFASZR;
266  }
267 
268  return u8RetValue ;
269 }
270 
284 static uint8_t WFlash_CheckToggle( uint16_t* pu16Addr )
285 {
286  uint16_t u16SequenceFlag1, u16SequenceFlag2 ; /* hardware sequence flag */
287  uint8_t u8RetValue = WFLASH_CHK_TOGG_NORMAL ;
288 
289  /* set hardware sequence flag */
290  u16SequenceFlag1 = Flash_Read(pu16Addr) ;
291  u16SequenceFlag2 = Flash_Read(pu16Addr) ;
292  /* if automatic algorithm is executing */
293  while((( u16SequenceFlag1 ^ u16SequenceFlag2) & WFLASH_CHK_TOGG_MASK) == WFLASH_CHK_TOGG_MASK)
294  {
295  /* if exceeds the timing limit */
296  if(( u16SequenceFlag1 & WFLASH_CHK_TLOV_MASK) == WFLASH_CHK_TLOV_MASK)
297  {
298  /* set hardware sequence flag */
299  u16SequenceFlag1 = Flash_Read(pu16Addr) ;
300  u16SequenceFlag2 = Flash_Read(pu16Addr) ;
301 
302  /* if automatic algorithm is executing */
303  if((( u16SequenceFlag1 ^ u16SequenceFlag2) & WFLASH_CHK_TOGG_MASK) == WFLASH_CHK_TOGG_MASK)
304  {
305  /* abnormally complete */
306  u8RetValue = WFLASH_CHK_TOGG_ABNORMAL ;
307 
308  break;
309  }
310  }
311 
312  /* set hardware sequence flag */
313  u16SequenceFlag1 = Flash_Read(pu16Addr) ;
314  u16SequenceFlag2 = Flash_Read(pu16Addr) ;
315  }
316 
317  return u8RetValue ;
318 }
319 
321 
322 #endif
323 
324 /******************************************************************************/
325 /* EOF (not truncated) */
326 /******************************************************************************/