2 ******************************************************************************
\r
3 * @file stm32l1xx_flash_ramfunc.c
\r
4 * @author MCD Application Team
\r
6 * @date 05-March-2012
\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
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
23 *** ICCARM Compiler ***
\r
24 -----------------------
\r
25 [..] RAM functions are defined using a specific toolchain keyword "__ramfunc".
\r
27 *** GNU Compiler ***
\r
28 --------------------
\r
29 [..] RAM functions are defined using a specific toolchain attribute
\r
30 "__attribute__((section(".data")))".
\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
39 ******************************************************************************
\r
42 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
44 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
45 * You may not use this file except in compliance with the License.
\r
46 * You may obtain a copy of the License at:
\r
48 * http://www.st.com/software_license_agreement_liberty_v2
\r
50 * Unless required by applicable law or agreed to in writing, software
\r
51 * distributed under the License is distributed on an "AS IS" BASIS,
\r
52 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
53 * See the License for the specific language governing permissions and
\r
54 * limitations under the License.
\r
56 ******************************************************************************
\r
59 /* Includes ------------------------------------------------------------------*/
\r
60 #include "stm32l1xx_flash.h"
\r
62 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
66 /** @defgroup FLASH
\r
67 * @brief FLASH driver modules
\r
71 /* Private typedef -----------------------------------------------------------*/
\r
72 /* Private define ------------------------------------------------------------*/
\r
73 /* Private macro -------------------------------------------------------------*/
\r
74 /* Private variables ---------------------------------------------------------*/
\r
75 /* Private function prototypes -----------------------------------------------*/
\r
76 static __RAM_FUNC GetStatus(void);
\r
77 static __RAM_FUNC WaitForLastOperation(uint32_t Timeout);
\r
79 /* Private functions ---------------------------------------------------------*/
\r
81 /** @defgroup FLASH_Private_Functions
\r
85 /** @addtogroup FLASH_Group1
\r
91 #if defined ( __TASKING__ )
\r
92 #pragma section_code_init on
\r
96 * @brief Enable or disable the power down mode during RUN mode.
\r
97 * @note This function can be used only when the user code is running from Internal SRAM.
\r
98 * @param NewState: new state of the power down mode during RUN mode.
\r
99 * This parameter can be: ENABLE or DISABLE.
\r
102 __RAM_FUNC FLASH_RUNPowerDownCmd(FunctionalState NewState)
\r
104 FLASH_Status status = FLASH_COMPLETE;
\r
106 if (NewState != DISABLE)
\r
108 /* Unlock the RUN_PD bit */
\r
109 FLASH->PDKEYR = FLASH_PDKEY1;
\r
110 FLASH->PDKEYR = FLASH_PDKEY2;
\r
112 /* Set the RUN_PD bit in FLASH_ACR register to put Flash in power down mode */
\r
113 FLASH->ACR |= (uint32_t)FLASH_ACR_RUN_PD;
\r
115 if((FLASH->ACR & FLASH_ACR_RUN_PD) != FLASH_ACR_RUN_PD)
\r
117 status = FLASH_ERROR_PROGRAM;
\r
122 /* Clear the RUN_PD bit in FLASH_ACR register to put Flash in idle mode */
\r
123 FLASH->ACR &= (uint32_t)(~(uint32_t)FLASH_ACR_RUN_PD);
\r
126 /* Return the Write Status */
\r
134 /** @addtogroup FLASH_Group2
\r
142 * @brief Erases a specified 2 page in program memory in parallel.
\r
143 * @note This function can be used only for STM32L1XX_HD density devices.
\r
144 * To correctly run this function, the FLASH_Unlock() function
\r
145 * must be called before.
\r
146 * Call the FLASH_Lock() to disable the flash memory access
\r
147 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
148 * @param Page_Address1: The page address in program memory to be erased in
\r
149 * the first Bank (BANK1). This parameter should be between 0x08000000
\r
151 * @param Page_Address2: The page address in program memory to be erased in
\r
152 * the second Bank (BANK2). This parameter should be between 0x08030000
\r
154 * @note A Page is erased in the Program memory only if the address to load
\r
155 * is the start address of a page (multiple of 256 bytes).
\r
156 * @retval FLASH Status: The returned value can be:
\r
157 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
159 FLASH_Status FLASH_EraseParallelPage(uint32_t Page_Address1, uint32_t Page_Address2)
\r
161 FLASH_Status status = FLASH_COMPLETE;
\r
163 /* Wait for last operation to be completed */
\r
164 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
166 if(status == FLASH_COMPLETE)
\r
168 /* If the previous operation is completed, proceed to erase the page */
\r
170 /* Set the PARALLBANK bit */
\r
171 FLASH->PECR |= FLASH_PECR_PARALLBANK;
\r
173 /* Set the ERASE bit */
\r
174 FLASH->PECR |= FLASH_PECR_ERASE;
\r
177 FLASH->PECR |= FLASH_PECR_PROG;
\r
179 /* Write 00000000h to the first word of the first program page to erase */
\r
180 *(__IO uint32_t *)Page_Address1 = 0x00000000;
\r
181 /* Write 00000000h to the first word of the second program page to erase */
\r
182 *(__IO uint32_t *)Page_Address2 = 0x00000000;
\r
184 /* Wait for last operation to be completed */
\r
185 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
187 /* If the erase operation is completed, disable the ERASE, PROG and PARALLBANK bits */
\r
188 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
\r
189 FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);
\r
190 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PARALLBANK);
\r
192 /* Return the Erase Status */
\r
197 * @brief Programs a half page in program memory.
\r
198 * @param Address: specifies the address to be written.
\r
199 * @param pBuffer: pointer to the buffer containing the data to be written to
\r
201 * @note To correctly run this function, the FLASH_Unlock() function
\r
202 * must be called before.
\r
203 * Call the FLASH_Lock() to disable the flash memory access
\r
204 * (recommended to protect the FLASH memory against possible unwanted operation)
\r
205 * @note Half page write is possible only from SRAM.
\r
206 * @note If there are more than 32 words to write, after 32 words another
\r
207 * Half Page programming operation starts and has to be finished.
\r
208 * @note A half page is written to the program memory only if the first
\r
209 * address to load is the start address of a half page (multiple of 128
\r
210 * bytes) and the 31 remaining words to load are in the same half page.
\r
211 * @note During the Program memory half page write all read operations are
\r
212 * forbidden (this includes DMA read operations and debugger read
\r
213 * operations such as breakpoints, periodic updates, etc.).
\r
214 * @note If a PGAERR is set during a Program memory half page write, the
\r
215 * complete write operation is aborted. Software should then reset the
\r
216 * FPRG and PROG/DATA bits and restart the write operation from the
\r
218 * @retval FLASH Status: The returned value can be:
\r
219 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
221 __RAM_FUNC FLASH_ProgramHalfPage(uint32_t Address, uint32_t* pBuffer)
\r
223 uint32_t count = 0;
\r
225 FLASH_Status status = FLASH_COMPLETE;
\r
227 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
\r
228 This bit prevents the interruption of multicycle instructions and therefore
\r
229 will increase the interrupt latency. of Cortex-M3. */
\r
230 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
232 /* Wait for last operation to be completed */
\r
233 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
235 if(status == FLASH_COMPLETE)
\r
237 /* if the previous operation is completed, proceed to program the new
\r
239 FLASH->PECR |= FLASH_PECR_FPRG;
\r
240 FLASH->PECR |= FLASH_PECR_PROG;
\r
242 /* Write one half page directly with 32 different words */
\r
245 *(__IO uint32_t*) (Address + (4 * count)) = *(pBuffer++);
\r
248 /* Wait for last operation to be completed */
\r
249 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
251 /* if the write operation is completed, disable the PROG and FPRG bits */
\r
252 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
\r
253 FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);
\r
256 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
258 /* Return the Write Status */
\r
263 * @brief Programs 2 half page in program memory in parallel.
\r
264 * @param Address1: specifies the first address to be written in the first bank
\r
265 * (BANK1). This parameter should be between 0x08000000 and 0x0802FF80.
\r
266 * @param pBuffer1: pointer to the buffer containing the data to be written
\r
267 * to the first half page in the first bank.
\r
268 * @param Address2: specifies the second address to be written in the second bank
\r
269 * (BANK2). This parameter should be between 0x08030000 and 0x0805FF80.
\r
270 * @param pBuffer2: pointer to the buffer containing the data to be written
\r
271 * to the second half page in the second bank.
\r
272 * @note This function can be used only for STM32L1XX_HD density devices.
\r
273 * @note To correctly run this function, the FLASH_Unlock() function
\r
274 * must be called before.
\r
275 * Call the FLASH_Lock() to disable the flash memory access
\r
276 * (recommended to protect the FLASH memory against possible unwanted operation).
\r
277 * @note Half page write is possible only from SRAM.
\r
278 * @note If there are more than 32 words to write, after 32 words another
\r
279 * Half Page programming operation starts and has to be finished.
\r
280 * @note A half page is written to the program memory only if the first
\r
281 * address to load is the start address of a half page (multiple of 128
\r
282 * bytes) and the 31 remaining words to load are in the same half page.
\r
283 * @note During the Program memory half page write all read operations are
\r
284 * forbidden (this includes DMA read operations and debugger read
\r
285 * operations such as breakpoints, periodic updates, etc.).
\r
286 * @note If a PGAERR is set during a Program memory half page write, the
\r
287 * complete write operation is aborted. Software should then reset the
\r
288 * FPRG and PROG/DATA bits and restart the write operation from the
\r
290 * @retval FLASH Status: The returned value can be:
\r
291 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
293 __RAM_FUNC FLASH_ProgramParallelHalfPage(uint32_t Address1, uint32_t* pBuffer1, uint32_t Address2, uint32_t* pBuffer2)
\r
295 uint32_t count = 0;
\r
297 FLASH_Status status = FLASH_COMPLETE;
\r
299 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
\r
300 This bit prevents the interruption of multicycle instructions and therefore
\r
301 will increase the interrupt latency. of Cortex-M3. */
\r
302 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
304 /* Wait for last operation to be completed */
\r
305 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
307 if(status == FLASH_COMPLETE)
\r
309 /* If the previous operation is completed, proceed to program the new
\r
311 FLASH->PECR |= FLASH_PECR_PARALLBANK;
\r
312 FLASH->PECR |= FLASH_PECR_FPRG;
\r
313 FLASH->PECR |= FLASH_PECR_PROG;
\r
315 /* Write the first half page directly with 32 different words */
\r
318 *(__IO uint32_t*) (Address1 + (4 * count)) = *(pBuffer1++);
\r
322 /* Write the second half page directly with 32 different words */
\r
325 *(__IO uint32_t*) (Address2 + (4 * count)) = *(pBuffer2++);
\r
328 /* Wait for last operation to be completed */
\r
329 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
331 /* if the write operation is completed, disable the PROG, FPRG and PARALLBANK bits */
\r
332 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PROG);
\r
333 FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);
\r
334 FLASH->PECR &= (uint32_t)(~FLASH_PECR_PARALLBANK);
\r
337 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
339 /* Return the Write Status */
\r
347 /** @addtogroup FLASH_Group3
\r
355 * @brief Erase a double word in data memory.
\r
356 * @param Address: specifies the address to be erased.
\r
357 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
358 * must be called before.
\r
359 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
360 * and Flash program erase control register access(recommended to protect
\r
361 * the DATA_EEPROM against possible unwanted operation).
\r
362 * @note Data memory double word erase is possible only from SRAM.
\r
363 * @note A double word is erased to the data memory only if the first address
\r
364 * to load is the start address of a double word (multiple of 8 bytes).
\r
365 * @note During the Data memory double word erase, all read operations are
\r
366 * forbidden (this includes DMA read operations and debugger read
\r
367 * operations such as breakpoints, periodic updates, etc.).
\r
368 * @retval FLASH Status: The returned value can be:
\r
369 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
372 __RAM_FUNC DATA_EEPROM_EraseDoubleWord(uint32_t Address)
\r
374 FLASH_Status status = FLASH_COMPLETE;
\r
376 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
\r
377 This bit prevents the interruption of multicycle instructions and therefore
\r
378 will increase the interrupt latency. of Cortex-M3. */
\r
379 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
381 /* Wait for last operation to be completed */
\r
382 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
384 if(status == FLASH_COMPLETE)
\r
386 /* If the previous operation is completed, proceed to erase the next double word */
\r
387 /* Set the ERASE bit */
\r
388 FLASH->PECR |= FLASH_PECR_ERASE;
\r
391 FLASH->PECR |= FLASH_PECR_DATA;
\r
393 /* Write 00000000h to the 2 words to erase */
\r
394 *(__IO uint32_t *)Address = 0x00000000;
\r
396 *(__IO uint32_t *)Address = 0x00000000;
\r
398 /* Wait for last operation to be completed */
\r
399 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
401 /* If the erase operation is completed, disable the ERASE and DATA bits */
\r
402 FLASH->PECR &= (uint32_t)(~FLASH_PECR_ERASE);
\r
403 FLASH->PECR &= (uint32_t)(~FLASH_PECR_DATA);
\r
406 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
408 /* Return the erase status */
\r
413 * @brief Write a double word in data memory without erase.
\r
414 * @param Address: specifies the address to be written.
\r
415 * @param Data: specifies the data to be written.
\r
416 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
\r
417 * must be called before.
\r
418 * Call the DATA_EEPROM_Lock() to he data EEPROM access
\r
419 * and Flash program erase control register access(recommended to protect
\r
420 * the DATA_EEPROM against possible unwanted operation).
\r
421 * @note Data memory double word write is possible only from SRAM.
\r
422 * @note A data memory double word is written to the data memory only if the
\r
423 * first address to load is the start address of a double word (multiple
\r
425 * @note During the Data memory double word write, all read operations are
\r
426 * forbidden (this includes DMA read operations and debugger read
\r
427 * operations such as breakpoints, periodic updates, etc.).
\r
428 * @retval FLASH Status: The returned value can be:
\r
429 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
\r
431 __RAM_FUNC DATA_EEPROM_ProgramDoubleWord(uint32_t Address, uint64_t Data)
\r
433 FLASH_Status status = FLASH_COMPLETE;
\r
435 /* Set the DISMCYCINT[0] bit in the Auxillary Control Register (0xE000E008)
\r
436 This bit prevents the interruption of multicycle instructions and therefore
\r
437 will increase the interrupt latency. of Cortex-M3. */
\r
438 SCnSCB->ACTLR |= SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
440 /* Wait for last operation to be completed */
\r
441 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
443 if(status == FLASH_COMPLETE)
\r
445 /* If the previous operation is completed, proceed to program the new data*/
\r
446 FLASH->PECR |= FLASH_PECR_FPRG;
\r
447 FLASH->PECR |= FLASH_PECR_DATA;
\r
449 /* Write the 2 words */
\r
450 *(__IO uint32_t *)Address = (uint32_t) Data;
\r
452 *(__IO uint32_t *)Address = (uint32_t) (Data >> 32);
\r
454 /* Wait for last operation to be completed */
\r
455 status = WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
\r
457 /* If the write operation is completed, disable the FPRG and DATA bits */
\r
458 FLASH->PECR &= (uint32_t)(~FLASH_PECR_FPRG);
\r
459 FLASH->PECR &= (uint32_t)(~FLASH_PECR_DATA);
\r
462 SCnSCB->ACTLR &= ~SCnSCB_ACTLR_DISMCYCINT_Msk;
\r
464 /* Return the Write Status */
\r
473 * @brief Returns the FLASH Status.
\r
475 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
\r
476 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE
\r
478 static __RAM_FUNC GetStatus(void)
\r
480 FLASH_Status FLASHstatus = FLASH_COMPLETE;
\r
482 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
\r
484 FLASHstatus = FLASH_BUSY;
\r
488 if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
\r
490 FLASHstatus = FLASH_ERROR_WRP;
\r
494 if((FLASH->SR & (uint32_t)0x1E00) != (uint32_t)0x00)
\r
496 FLASHstatus = FLASH_ERROR_PROGRAM;
\r
500 FLASHstatus = FLASH_COMPLETE;
\r
504 /* Return the FLASH Status */
\r
505 return FLASHstatus;
\r
509 * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
\r
510 * @param Timeout: FLASH programming Timeout
\r
511 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
\r
512 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or
\r
515 static __RAM_FUNC WaitForLastOperation(uint32_t Timeout)
\r
517 __IO FLASH_Status status = FLASH_COMPLETE;
\r
519 /* Check for the FLASH Status */
\r
520 status = GetStatus();
\r
522 /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
\r
523 while((status == FLASH_BUSY) && (Timeout != 0x00))
\r
525 status = GetStatus();
\r
529 if(Timeout == 0x00 )
\r
531 status = FLASH_TIMEOUT;
\r
533 /* Return the operation status */
\r
537 #if defined ( __TASKING__ )
\r
538 #pragma section_code_init restore
\r
553 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r