2 ******************************************************************************
\r
3 * @file stm32l1xx_flash_ramfunc.c
\r
4 * @author MCD Application Team
\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
10 * Other FLASH memory functions that can be used from the FLASH are
\r
11 * defined in the "stm32l1xx_flash.c" file.
\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
25 * RAM functions are defined using a specific toolchain keyword "__ramfunc".
\r
29 * RAM functions are defined using a specific toolchain attribute
\r
30 * "__attribute__((section(".data")))".
\r
34 * RAM functions are defined using a specific toolchain pragma. This
\r
35 * pragma is defined inside this file.
\r
39 ******************************************************************************
\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
49 * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
\r
50 ******************************************************************************
\r
53 /* Includes ------------------------------------------------------------------*/
\r
54 #include "stm32l1xx_flash.h"
\r
56 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
60 /** @defgroup FLASH
\r
61 * @brief FLASH driver modules
\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
73 /* Private functions ---------------------------------------------------------*/
\r
75 /** @defgroup FLASH_Private_Functions
\r
79 /** @addtogroup FLASH_Group1
\r
85 #if defined ( __TASKING__ )
\r
86 #pragma section_code_init on
\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
96 __RAM_FUNC FLASH_RUNPowerDownCmd(FunctionalState NewState)
\r
98 FLASH_Status status = FLASH_COMPLETE;
\r
100 if (NewState != DISABLE)
\r
102 /* Unlock the RUN_PD bit */
\r
103 FLASH->PDKEYR = FLASH_PDKEY1;
\r
104 FLASH->PDKEYR = FLASH_PDKEY2;
\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
109 if((FLASH->ACR & FLASH_ACR_RUN_PD) != FLASH_ACR_RUN_PD)
\r
111 status = FLASH_ERROR_PROGRAM;
\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
120 /* Return the Write Status */
\r
128 /** @addtogroup FLASH_Group2
\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
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
157 * @retval FLASH Status: The returned value can be:
\r
158 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
160 __RAM_FUNC FLASH_ProgramHalfPage(uint32_t Address, uint32_t* pBuffer)
\r
162 uint32_t count = 0;
\r
164 FLASH_Status status = FLASH_COMPLETE;
\r
166 /* Wait for last operation to be completed */
\r
167 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
169 if(status == FLASH_COMPLETE)
\r
171 /* if the previous operation is completed, proceed to program the new
\r
173 FLASH->PECR |= FLASH_PECR_FPRG;
\r
174 FLASH->PECR |= FLASH_PECR_PROG;
\r
176 /* Write one half page directly with 32 different words */
\r
179 *(__IO uint32_t*) (Address + (4 * count)) = *(pBuffer++);
\r
182 /* Wait for last operation to be completed */
\r
183 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\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
189 /* Return the Write Status */
\r
197 /** @addtogroup FLASH_Group3
\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
222 __RAM_FUNC DATA_EEPROM_EraseDoubleWord(uint32_t Address)
\r
224 FLASH_Status status = FLASH_COMPLETE;
\r
226 /* Wait for last operation to be completed */
\r
227 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
229 if(status == FLASH_COMPLETE)
\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
236 FLASH->PECR |= FLASH_PECR_DATA;
\r
238 /* Write 00000000h to the 2 words to erase */
\r
239 *(__IO uint64_t *)Address = 0x00000000;
\r
241 /* Wait for last operation to be completed */
\r
242 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\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
248 /* Return the erase status */
\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
271 __RAM_FUNC DATA_EEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)
\r
273 FLASH_Status status = FLASH_COMPLETE;
\r
275 /* Wait for last operation to be completed */
\r
276 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
278 if(status == FLASH_COMPLETE)
\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
284 /* Write the 2 words */
\r
285 *(__IO uint32_t *)Address = (uint32_t) Data;
\r
287 *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);
\r
289 /* Wait for last operation to be completed */
\r
290 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\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
296 /* Return the Write Status */
\r
305 * @brief Returns the FLASH Status.
\r
307 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
\r
308 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE
\r
310 static __RAM_FUNC GetStatus(void)
\r
312 FLASH_Status FLASHstatus = FLASH_COMPLETE;
\r
314 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
\r
316 FLASHstatus = FLASH_BUSY;
\r
320 if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
\r
322 FLASHstatus = FLASH_ERROR_WRP;
\r
326 if((FLASH->SR & (uint32_t)0xFEF0) != (uint32_t)0x00)
\r
328 FLASHstatus = FLASH_ERROR_PROGRAM;
\r
332 FLASHstatus = FLASH_COMPLETE;
\r
336 /* Return the FLASH Status */
\r
337 return FLASHstatus;
\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
347 static __RAM_FUNC WaitForLastOperation(uint32_t Timeout)
\r
349 FLASH_Status status = FLASH_COMPLETE;
\r
351 /* Check for the FLASH Status */
\r
352 status = GetStatus();
\r
354 /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
\r
355 while((status == FLASH_BUSY) && (Timeout != 0x00))
\r
357 status = GetStatus();
\r
361 if(Timeout == 0x00 )
\r
363 status = FLASH_TIMEOUT;
\r
365 /* Return the operation status */
\r
369 #if defined ( __TASKING__ )
\r
370 #pragma section_code_init restore
\r
385 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r