aefaf96c432b062fe2c72a9cabfa8609cbfea7c3
[fw/stlink] / example / libs_stm / src / stm32l1xx / stm32l1xx_flash_ramfunc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_flash_ramfunc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides all the Flash firmware functions which should be\r
8   *          executed from the internal SRAM. This file should be placed in \r
9   *          internal SRAM. \r
10   *          Other FLASH memory functions that can be used from the FLASH are \r
11   *          defined in the "stm32l1xx_flash.c" file. \r
12   *  @verbatim   \r
13   *    \r
14   *          ARM Compiler\r
15   *          ------------\r
16   *          RAM functions are defined using the toolchain options. \r
17   *          Functions that are be executed in RAM should reside in a separate\r
18   *          source module. Using the 'Options for File' dialog you can simply change\r
19   *          the 'Code / Const' area of a module to a memory space in physical RAM.\r
20   *          Available memory areas are declared in the 'Target' tab of the \r
21   *          'Options for Target' dialog.\r
22   *          \r
23   *          ICCARM Compiler\r
24   *          ---------------\r
25   *          RAM functions are defined using a specific toolchain keyword "__ramfunc".  \r
26   *          \r
27   *          GNU Compiler\r
28   *          ------------\r
29   *          RAM functions are defined using a specific toolchain attribute\r
30   *          "__attribute__((section(".data")))".\r
31   *          \r
32   *          TASKING Compiler\r
33   *          ----------------\r
34   *          RAM functions are defined using a specific toolchain pragma. This \r
35   *          pragma is defined inside this file.  \r
36   *          \r
37   *  @endverbatim     \r
38   *\r
39   ******************************************************************************\r
40   * @attention\r
41   *\r
42   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
43   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
44   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
45   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
46   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
47   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
48   *\r
49   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
50   ******************************************************************************  \r
51   */ \r
52 \r
53 /* Includes ------------------------------------------------------------------*/\r
54 #include "stm32l1xx_flash.h"\r
55 \r
56 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
57   * @{\r
58   */\r
59 \r
60 /** @defgroup FLASH \r
61   * @brief FLASH driver modules\r
62   * @{\r
63   */ \r
64 \r
65 /* Private typedef -----------------------------------------------------------*/\r
66 /* Private define ------------------------------------------------------------*/\r
67 /* Private macro -------------------------------------------------------------*/\r
68 /* Private variables ---------------------------------------------------------*/\r
69 /* Private function prototypes -----------------------------------------------*/\r
70 static __RAM_FUNC GetStatus(void);\r
71 static __RAM_FUNC WaitForLastOperation(uint32_t Timeout);\r
72 \r
73 /* Private functions ---------------------------------------------------------*/\r
74  \r
75 /** @defgroup FLASH_Private_Functions\r
76   * @{\r
77   */ \r
78 \r
79 /** @addtogroup FLASH_Group1\r
80  *\r
81 @verbatim  \r
82 @endverbatim\r
83   * @{\r
84   */  \r
85 #if defined (  __TASKING__  )\r
86 #pragma section_code_init on\r
87 #endif\r
88 \r
89 /**\r
90   * @brief  Enable or disable the power down mode during RUN mode.\r
91   * @note: This function can be used only when the user code is running from Internal SRAM\r
92   * @param  NewState: new state of the power down mode during RUN mode.\r
93   *   This parameter can be: ENABLE or DISABLE.\r
94   * @retval None\r
95   */\r
96 __RAM_FUNC FLASH_RUNPowerDownCmd(FunctionalState NewState)\r
97 {\r
98   FLASH_Status status = FLASH_COMPLETE;\r
99  \r
100   if (NewState != DISABLE)\r
101   {\r
102      /* Unlock the RUN_PD bit */\r
103      FLASH->PDKEYR = FLASH_PDKEY1;\r
104      FLASH->PDKEYR = FLASH_PDKEY2;\r
105      \r
106      /* Set the RUN_PD bit in  FLASH_ACR register to put Flash in power down mode */\r
107      FLASH->ACR |= (uint32_t)FLASH_ACR_RUN_PD;\r
108 \r
109      if((FLASH->ACR & FLASH_ACR_RUN_PD) != FLASH_ACR_RUN_PD)\r
110      {\r
111        status = FLASH_ERROR_PROGRAM;\r
112      }\r
113   }\r
114   else\r
115   {\r
116     /* Clear the RUN_PD bit in  FLASH_ACR register to put Flash in idle  mode */\r
117     FLASH->ACR &= (uint32_t)(~(uint32_t)FLASH_ACR_RUN_PD);\r
118   }\r
119 \r
120   /* Return the Write Status */\r
121   return status;  \r
122 }\r
123 \r
124 /**\r
125   * @}\r
126   */\r
127 \r
128 /** @addtogroup FLASH_Group2\r
129  *\r
130 @verbatim  \r
131 @endverbatim\r
132   * @{\r
133   */\r
134 \r
135 /**\r
136   * @brief  Programs a half page in program memory.\r
137   * @param  Address: specifies the address to be written.\r
138   * @param  pBuffer: pointer to the buffer  containing the data to be  written to \r
139   *         the half page.\r
140   * @note   - To correctly run this function, the FLASH_Unlock() function\r
141   *           must be called before.\r
142   *         - Call the FLASH_Lock() to disable the flash memory access  \r
143   *          (recommended to protect the FLASH memory against possible unwanted operation)\r
144   * @note   Half page write is possible only from SRAM.\r
145   * @note   If there are more than 32 words to write, after 32 words another \r
146   *         Half Page programming operation starts and has to be finished.\r
147   * @note   A half page is written to the program memory only if the first \r
148   *         address to load is the start address of a half page (multiple of 128 \r
149   *         bytes) and the 31 remaining words to load are in the same half page.\r
150   * @note   During the Program memory half page write all read operations are \r
151   *         forbidden (this includes DMA read operations and debugger read \r
152   *         operations such as breakpoints, periodic updates, etc.)\r
153   * @note   If a PGAERR is set during a Program memory half page write, the \r
154   *         complete write operation is aborted. Software should then reset the \r
155   *         FPRG and PROG/DATA bits and restart the write operation from the \r
156   *         beginning.                             \r
157   * @retval FLASH Status: The returned value can be:  \r
158   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
159   */\r
160 __RAM_FUNC FLASH_ProgramHalfPage(uint32_t Address, uint32_t* pBuffer)\r
161 {\r
162   uint32_t count = 0; \r
163    \r
164   FLASH_Status status = FLASH_COMPLETE;\r
165 \r
166   /* Wait for last operation to be completed */\r
167   status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
168   \r
169   if(status == FLASH_COMPLETE)\r
170   {\r
171     /* if the previous operation is completed, proceed to program the new  \r
172     half page */\r
173     FLASH->PECR |= FLASH_PECR_FPRG;\r
174     FLASH->PECR |= FLASH_PECR_PROG;\r
175     \r
176     /* Write one half page directly with 32 different words */\r
177     while(count < 32)\r
178     {\r
179       *(__IO uint32_t*) (Address + (4 * count)) = *(pBuffer++);\r
180       count ++;  \r
181     }\r
182     /* Wait for last operation to be completed */\r
183     status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
184  \r
185     /* if the write operation is completed, disable the PROG and FPRG bits */\r
186     FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);\r
187     FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);\r
188   }\r
189   /* Return the Write Status */\r
190   return status;\r
191 }\r
192 \r
193 /**\r
194   * @}\r
195   */\r
196 \r
197 /** @addtogroup FLASH_Group3\r
198  *\r
199 @verbatim  \r
200 @endverbatim\r
201   * @{\r
202   */\r
203 \r
204 /**\r
205   * @brief  Erase a double word in data memory.\r
206   * @param  Address: specifies the address to be erased\r
207   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
208   *           must be called before.\r
209   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
210   *           and Flash program erase control register access(recommended to protect \r
211   *           the DATA_EEPROM against possible unwanted operation)   \r
212   * @note   Data memory double word erase is possible only from SRAM.\r
213   * @note   A double word is erased to the data memory only if the first address \r
214   *         to load is the start address of a double word (multiple of 8 bytes)  \r
215   * @note   During the Data memory double word erase, all read operations are \r
216   *         forbidden (this includes DMA read operations and debugger read \r
217   *         operations such as breakpoints, periodic updates, etc.)  \r
218   * @retval FLASH Status: The returned value can be: \r
219   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
220   */\r
221 \r
222 __RAM_FUNC DATA_EEPROM_EraseDoubleWord(uint32_t Address)\r
223 {\r
224   FLASH_Status status = FLASH_COMPLETE;\r
225     \r
226   /* Wait for last operation to be completed */\r
227   status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
228   \r
229   if(status == FLASH_COMPLETE)\r
230   {\r
231     /* If the previous operation is completed, proceed to erase the next double word */\r
232     /* Set the ERASE bit */\r
233     FLASH->PECR |= FLASH_PECR_ERASE;\r
234 \r
235     /* Set DATA bit */\r
236     FLASH->PECR |= FLASH_PECR_DATA;\r
237    \r
238     /* Write 00000000h to the 2 words to erase */\r
239     *(__IO uint64_t *)Address = 0x00000000;\r
240  \r
241     /* Wait for last operation to be completed */\r
242     status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
243     \r
244     /* If the erase operation is completed, disable the ERASE and DATA bits */\r
245     FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);\r
246     FLASH->PECR &= (uint32_t)(~FLASH_PECR_DATA);\r
247   }  \r
248   /* Return the erase status */\r
249   return status;\r
250 }\r
251 \r
252 /**\r
253   * @brief  Write a double word in data memory without erase.\r
254   * @param  Address: specifies the address to be written.\r
255   * @param  Data: specifies the data to be written.\r
256   * @note   - To correctly run this function, the DATA_EEPROM_Unlock() function\r
257   *           must be called before.\r
258   *         - Call the DATA_EEPROM_Lock() to he data EEPROM access\r
259   *           and Flash program erase control register access(recommended to protect \r
260   *           the DATA_EEPROM against possible unwanted operation)   \r
261   * @note   Data memory double word write is possible only from SRAM.\r
262   * @note   A data memory double word is written to the data memory only if the \r
263   *         first address to load is the start address of a double word (multiple \r
264   *         of double word).  \r
265   * @note   During the Data memory double word write, all read operations are \r
266   *         forbidden (this includes DMA read operations and debugger read \r
267   *         operations such as breakpoints, periodic updates, etc.)    \r
268   * @retval FLASH Status: The returned value can be: \r
269   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
270   */ \r
271 __RAM_FUNC DATA_EEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)\r
272 {\r
273   FLASH_Status status = FLASH_COMPLETE;\r
274     \r
275   /* Wait for last operation to be completed */\r
276   status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
277   \r
278   if(status == FLASH_COMPLETE)\r
279   {\r
280     /* If the previous operation is completed, proceed to program the new data*/\r
281     FLASH->PECR |= FLASH_PECR_FPRG;\r
282     FLASH->PECR |= FLASH_PECR_DATA;\r
283     \r
284     /* Write the 2 words */  \r
285      *(__IO uint32_t *)Address = (uint32_t) Data;\r
286      Address += 4;\r
287      *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);\r
288     \r
289     /* Wait for last operation to be completed */\r
290     status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);\r
291     \r
292     /* If the write operation is completed, disable the FPRG and DATA bits */\r
293     FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);\r
294     FLASH->PECR &= (uint32_t)(~FLASH_PECR_DATA);     \r
295   }\r
296   /* Return the Write Status */\r
297   return status;\r
298 }\r
299 \r
300 /**\r
301   * @}\r
302   */\r
303 \r
304 /**\r
305   * @brief  Returns the FLASH Status.\r
306   * @param  None\r
307   * @retval FLASH Status: The returned value can be: FLASH_BUSY, \r
308   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE\r
309   */\r
310 static __RAM_FUNC GetStatus(void)\r
311 {\r
312   FLASH_Status FLASHstatus = FLASH_COMPLETE;\r
313   \r
314   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) \r
315   {\r
316     FLASHstatus = FLASH_BUSY;\r
317   }\r
318   else \r
319   {  \r
320     if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)\r
321     { \r
322       FLASHstatus = FLASH_ERROR_WRP;\r
323     }\r
324     else \r
325     {\r
326       if((FLASH->SR & (uint32_t)0xFEF0) != (uint32_t)0x00)\r
327       {\r
328         FLASHstatus = FLASH_ERROR_PROGRAM; \r
329       }\r
330       else\r
331       {\r
332         FLASHstatus = FLASH_COMPLETE;\r
333       }\r
334     }\r
335   }\r
336   /* Return the FLASH Status */\r
337   return FLASHstatus;\r
338 }\r
339 \r
340 /**\r
341   * @brief  Waits for a FLASH operation to complete or a TIMEOUT to occur.\r
342   * @param  Timeout: FLASH programming Timeout\r
343   * @retval FLASH Status: The returned value can be: FLASH_BUSY, \r
344   *   FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or \r
345   *   FLASH_TIMEOUT.\r
346   */\r
347 static __RAM_FUNC  WaitForLastOperation(uint32_t Timeout)\r
348\r
349   FLASH_Status status = FLASH_COMPLETE;\r
350    \r
351   /* Check for the FLASH Status */\r
352   status = GetStatus();\r
353   \r
354   /* Wait for a FLASH operation to complete or a TIMEOUT to occur */\r
355   while((status == FLASH_BUSY) && (Timeout != 0x00))\r
356   {\r
357     status = GetStatus();\r
358     Timeout--;\r
359   }\r
360   \r
361   if(Timeout == 0x00 )\r
362   {\r
363     status = FLASH_TIMEOUT;\r
364   }\r
365   /* Return the operation status */\r
366   return status;\r
367 }\r
368 \r
369 #if defined (  __TASKING__  )\r
370 #pragma section_code_init restore\r
371 #endif\r
372 \r
373 /**\r
374   * @}\r
375   */\r
376    \r
377   /**\r
378   * @}\r
379   */ \r
380 \r
381 /**\r
382   * @}\r
383   */ \r
384 \r
385 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r