2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_flash.c
\r
4 * @author MCD Application Team
\r
5 * @brief FLASH HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the internal FLASH memory:
\r
8 * + Program operations functions
\r
9 * + Memory Control functions
\r
10 * + Peripheral Errors functions
\r
13 ==============================================================================
\r
14 ##### FLASH peripheral features #####
\r
15 ==============================================================================
\r
17 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
\r
18 to the Flash memory. It implements the erase and program Flash memory operations
\r
19 and the read and write protection mechanisms.
\r
21 [..] The Flash memory interface accelerates code execution with a system of instruction
\r
22 prefetch and cache lines.
\r
24 [..] The FLASH main features are:
\r
25 (+) Flash memory read operations
\r
26 (+) Flash memory program/erase operations
\r
27 (+) Read / write protections
\r
28 (+) Option bytes programming
\r
29 (+) Prefetch on I-Code
\r
30 (+) 32 cache lines of 4*64 bits on I-Code
\r
31 (+) 8 cache lines of 4*64 bits on D-Code
\r
32 (+) Error code correction (ECC) : Data in flash are 72-bits word
\r
33 (8 bits added per double word)
\r
36 ##### How to use this driver #####
\r
37 ==============================================================================
\r
39 This driver provides functions and macros to configure and program the FLASH
\r
40 memory of all STM32L4xx devices.
\r
42 (#) Flash Memory IO Programming functions:
\r
43 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
\r
44 HAL_FLASH_Lock() functions
\r
45 (++) Program functions: double word and fast program (full row programming)
\r
46 (++) There Two modes of programming :
\r
47 (+++) Polling mode using HAL_FLASH_Program() function
\r
48 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
\r
50 (#) Interrupts and flags management functions :
\r
51 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
\r
52 (++) Callback functions are called when the flash operations are finished :
\r
53 HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
\r
54 HAL_FLASH_OperationErrorCallback()
\r
55 (++) Get error flag status by calling HAL_GetError()
\r
57 (#) Option bytes management functions :
\r
58 (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
\r
59 HAL_FLASH_OB_Lock() functions
\r
60 (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
\r
61 In this case, a reset is generated
\r
64 In addition to these functions, this driver includes a set of macros allowing
\r
65 to handle the following operations:
\r
67 (+) Enable/Disable the prefetch buffer
\r
68 (+) Enable/Disable the Instruction cache and the Data cache
\r
69 (+) Reset the Instruction cache and the Data cache
\r
70 (+) Enable/Disable the Flash power-down during low-power run and sleep modes
\r
71 (+) Enable/Disable the Flash interrupts
\r
72 (+) Monitor the Flash flags status
\r
75 ******************************************************************************
\r
78 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
79 * All rights reserved.</center></h2>
\r
81 * This software component is licensed by ST under BSD 3-Clause license,
\r
82 * the "License"; You may not use this file except in compliance with the
\r
83 * License. You may obtain a copy of the License at:
\r
84 * opensource.org/licenses/BSD-3-Clause
\r
86 ******************************************************************************
\r
89 /* Includes ------------------------------------------------------------------*/
\r
90 #include "stm32l4xx_hal.h"
\r
92 /** @addtogroup STM32L4xx_HAL_Driver
\r
96 /** @defgroup FLASH FLASH
\r
97 * @brief FLASH HAL module driver
\r
101 #ifdef HAL_FLASH_MODULE_ENABLED
\r
103 /* Private typedef -----------------------------------------------------------*/
\r
104 /* Private defines -----------------------------------------------------------*/
\r
105 #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
\r
106 #define FLASH_NB_DOUBLE_WORDS_IN_ROW 64
\r
108 #define FLASH_NB_DOUBLE_WORDS_IN_ROW 32
\r
110 /* Private macros ------------------------------------------------------------*/
\r
111 /* Private variables ---------------------------------------------------------*/
\r
112 /** @defgroup FLASH_Private_Variables FLASH Private Variables
\r
116 * @brief Variable used for Program/Erase sectors under interruption
\r
118 FLASH_ProcessTypeDef pFlash = {.Lock = HAL_UNLOCKED, \
\r
119 .ErrorCode = HAL_FLASH_ERROR_NONE, \
\r
120 .ProcedureOnGoing = FLASH_PROC_NONE, \
\r
122 .Bank = FLASH_BANK_1, \
\r
124 .NbPagesToErase = 0U, \
\r
125 .CacheToReactivate = FLASH_CACHE_DISABLED};
\r
130 /* Private function prototypes -----------------------------------------------*/
\r
131 /** @defgroup FLASH_Private_Functions FLASH Private Functions
\r
134 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
\r
135 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);
\r
140 /* Exported functions --------------------------------------------------------*/
\r
141 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
\r
145 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
\r
146 * @brief Programming operation functions
\r
149 ===============================================================================
\r
150 ##### Programming operation functions #####
\r
151 ===============================================================================
\r
153 This subsection provides a set of functions allowing to manage the FLASH
\r
154 program operations.
\r
161 * @brief Program double word or fast program of a row at a specified address.
\r
162 * @param TypeProgram: Indicate the way to program at a specified address.
\r
163 * This parameter can be a value of @ref FLASH_Type_Program
\r
164 * @param Address: specifies the address to be programmed.
\r
165 * @param Data: specifies the data to be programmed
\r
166 * This parameter is the data for the double word program and the address where
\r
167 * are stored the data for the row fast program
\r
169 * @retval HAL_StatusTypeDef HAL Status
\r
171 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
\r
173 HAL_StatusTypeDef status;
\r
174 uint32_t prog_bit = 0;
\r
176 /* Process Locked */
\r
177 __HAL_LOCK(&pFlash);
\r
179 /* Check the parameters */
\r
180 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
\r
182 /* Wait for last operation to be completed */
\r
183 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
\r
185 if(status == HAL_OK)
\r
187 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
\r
189 /* Deactivate the data cache if they are activated to avoid data misbehavior */
\r
190 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
\r
192 /* Disable data cache */
\r
193 __HAL_FLASH_DATA_CACHE_DISABLE();
\r
194 pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
\r
198 pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
\r
201 if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
\r
203 /* Program double-word (64-bit) at a specified address */
\r
204 FLASH_Program_DoubleWord(Address, Data);
\r
205 prog_bit = FLASH_CR_PG;
\r
207 else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
\r
209 /* Fast program a 32 row double-word (64-bit) at a specified address */
\r
210 FLASH_Program_Fast(Address, (uint32_t)Data);
\r
212 /* If it is the last row, the bit will be cleared at the end of the operation */
\r
213 if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
\r
215 prog_bit = FLASH_CR_FSTPG;
\r
220 /* Nothing to do */
\r
223 /* Wait for last operation to be completed */
\r
224 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
\r
226 /* If the program operation is completed, disable the PG or FSTPG Bit */
\r
227 if (prog_bit != 0U)
\r
229 CLEAR_BIT(FLASH->CR, prog_bit);
\r
232 /* Flush the caches to be sure of the data consistency */
\r
233 FLASH_FlushCaches();
\r
236 /* Process Unlocked */
\r
237 __HAL_UNLOCK(&pFlash);
\r
243 * @brief Program double word or fast program of a row at a specified address with interrupt enabled.
\r
244 * @param TypeProgram: Indicate the way to program at a specified address.
\r
245 * This parameter can be a value of @ref FLASH_Type_Program
\r
246 * @param Address: specifies the address to be programmed.
\r
247 * @param Data: specifies the data to be programmed
\r
248 * This parameter is the data for the double word program and the address where
\r
249 * are stored the data for the row fast program
\r
251 * @retval HAL Status
\r
253 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
\r
255 HAL_StatusTypeDef status = HAL_OK;
\r
257 /* Check the parameters */
\r
258 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
\r
260 /* Process Locked */
\r
261 __HAL_LOCK(&pFlash);
\r
263 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
\r
265 /* Deactivate the data cache if they are activated to avoid data misbehavior */
\r
266 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
\r
268 /* Disable data cache */
\r
269 __HAL_FLASH_DATA_CACHE_DISABLE();
\r
270 pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
\r
274 pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
\r
277 /* Set internal variables used by the IRQ handler */
\r
278 if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
\r
280 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;
\r
284 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
\r
286 pFlash.Address = Address;
\r
288 /* Enable End of Operation and Error interrupts */
\r
289 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
\r
291 if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
\r
293 /* Program double-word (64-bit) at a specified address */
\r
294 FLASH_Program_DoubleWord(Address, Data);
\r
296 else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
\r
298 /* Fast program a 32 row double-word (64-bit) at a specified address */
\r
299 FLASH_Program_Fast(Address, (uint32_t)Data);
\r
303 /* Nothing to do */
\r
310 * @brief Handle FLASH interrupt request.
\r
313 void HAL_FLASH_IRQHandler(void)
\r
317 FLASH_ProcedureTypeDef procedure;
\r
319 /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */
\r
320 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));
\r
321 #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
\r
322 defined (STM32L496xx) || defined (STM32L4A6xx) || \
\r
323 defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
\r
324 CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);
\r
327 /* Disable the FSTPG Bit only if it is the last row programmed */
\r
328 if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
\r
330 CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
\r
333 /* Check FLASH operation error flags */
\r
334 error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
\r
335 error |= (FLASH->ECCR & FLASH_FLAG_ECCC);
\r
339 /*Save the error code*/
\r
340 pFlash.ErrorCode |= error;
\r
342 /* Clear error programming flags */
\r
343 __HAL_FLASH_CLEAR_FLAG(error);
\r
345 /* Flush the caches to be sure of the data consistency */
\r
346 FLASH_FlushCaches() ;
\r
348 /* FLASH error interrupt user callback */
\r
349 procedure = pFlash.ProcedureOnGoing;
\r
350 if(procedure == FLASH_PROC_PAGE_ERASE)
\r
352 HAL_FLASH_OperationErrorCallback(pFlash.Page);
\r
354 else if(procedure == FLASH_PROC_MASS_ERASE)
\r
356 HAL_FLASH_OperationErrorCallback(pFlash.Bank);
\r
358 else if((procedure == FLASH_PROC_PROGRAM) ||
\r
359 (procedure == FLASH_PROC_PROGRAM_LAST))
\r
361 HAL_FLASH_OperationErrorCallback(pFlash.Address);
\r
365 HAL_FLASH_OperationErrorCallback(0U);
\r
368 /*Stop the procedure ongoing*/
\r
369 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
372 /* Check FLASH End of Operation flag */
\r
373 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != 0U)
\r
375 /* Clear FLASH End of Operation pending bit */
\r
376 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
\r
378 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
\r
380 /* Nb of pages to erased can be decreased */
\r
381 pFlash.NbPagesToErase--;
\r
383 /* Check if there are still pages to erase*/
\r
384 if(pFlash.NbPagesToErase != 0U)
\r
386 /* Indicate user which page has been erased*/
\r
387 HAL_FLASH_EndOfOperationCallback(pFlash.Page);
\r
389 /* Increment page number */
\r
391 tmp_page = pFlash.Page;
\r
392 FLASH_PageErase(tmp_page, pFlash.Bank);
\r
396 /* No more pages to Erase */
\r
397 /* Reset Address and stop Erase pages procedure */
\r
398 pFlash.Page = 0xFFFFFFFFU;
\r
399 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
401 /* Flush the caches to be sure of the data consistency */
\r
402 FLASH_FlushCaches() ;
\r
404 /* FLASH EOP interrupt user callback */
\r
405 HAL_FLASH_EndOfOperationCallback(pFlash.Page);
\r
410 /* Flush the caches to be sure of the data consistency */
\r
411 FLASH_FlushCaches() ;
\r
413 procedure = pFlash.ProcedureOnGoing;
\r
414 if(procedure == FLASH_PROC_MASS_ERASE)
\r
416 /* MassErase ended. Return the selected bank */
\r
417 /* FLASH EOP interrupt user callback */
\r
418 HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
\r
420 else if((procedure == FLASH_PROC_PROGRAM) ||
\r
421 (procedure == FLASH_PROC_PROGRAM_LAST))
\r
423 /* Program ended. Return the selected address */
\r
424 /* FLASH EOP interrupt user callback */
\r
425 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
\r
429 /* Nothing to do */
\r
432 /*Clear the procedure ongoing*/
\r
433 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
\r
437 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
\r
439 /* Disable End of Operation and Error interrupts */
\r
440 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
\r
442 /* Process Unlocked */
\r
443 __HAL_UNLOCK(&pFlash);
\r
448 * @brief FLASH end of operation interrupt callback.
\r
449 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
\r
450 * Mass Erase: Bank number which has been requested to erase
\r
451 * Page Erase: Page which has been erased
\r
452 * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
\r
453 * Program: Address which was selected for data program
\r
456 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
\r
458 /* Prevent unused argument(s) compilation warning */
\r
459 UNUSED(ReturnValue);
\r
461 /* NOTE : This function should not be modified, when the callback is needed,
\r
462 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
\r
467 * @brief FLASH operation error interrupt callback.
\r
468 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
\r
469 * Mass Erase: Bank number which has been requested to erase
\r
470 * Page Erase: Page number which returned an error
\r
471 * Program: Address which was selected for data program
\r
474 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
\r
476 /* Prevent unused argument(s) compilation warning */
\r
477 UNUSED(ReturnValue);
\r
479 /* NOTE : This function should not be modified, when the callback is needed,
\r
480 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
\r
488 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
\r
489 * @brief Management functions
\r
492 ===============================================================================
\r
493 ##### Peripheral Control functions #####
\r
494 ===============================================================================
\r
496 This subsection provides a set of functions allowing to control the FLASH
\r
504 * @brief Unlock the FLASH control register access.
\r
505 * @retval HAL Status
\r
507 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
\r
509 HAL_StatusTypeDef status = HAL_OK;
\r
511 if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
\r
513 /* Authorize the FLASH Registers access */
\r
514 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
\r
515 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
\r
517 /* Verify Flash is unlocked */
\r
518 if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
\r
520 status = HAL_ERROR;
\r
528 * @brief Lock the FLASH control register access.
\r
529 * @retval HAL Status
\r
531 HAL_StatusTypeDef HAL_FLASH_Lock(void)
\r
533 /* Set the LOCK Bit to lock the FLASH Registers access */
\r
534 SET_BIT(FLASH->CR, FLASH_CR_LOCK);
\r
540 * @brief Unlock the FLASH Option Bytes Registers access.
\r
541 * @retval HAL Status
\r
543 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
\r
545 if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)
\r
547 /* Authorizes the Option Byte register programming */
\r
548 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
\r
549 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
\r
560 * @brief Lock the FLASH Option Bytes Registers access.
\r
561 * @retval HAL Status
\r
563 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
\r
565 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
\r
566 SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);
\r
572 * @brief Launch the option byte loading.
\r
573 * @retval HAL Status
\r
575 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
\r
577 /* Set the bit to force the option byte reloading */
\r
578 SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
\r
580 /* Wait for last operation to be completed */
\r
581 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
\r
588 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
\r
589 * @brief Peripheral Errors functions
\r
592 ===============================================================================
\r
593 ##### Peripheral Errors functions #####
\r
594 ===============================================================================
\r
596 This subsection permits to get in run-time Errors of the FLASH peripheral.
\r
603 * @brief Get the specific FLASH error flag.
\r
604 * @retval FLASH_ErrorCode: The returned value can be:
\r
605 * @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
\r
606 * @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag
\r
607 * @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag
\r
608 * @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
\r
609 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
\r
610 * @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag
\r
611 * @arg HAL_FLASH_ERROR_NONE: No error set
\r
612 * @arg HAL_FLASH_ERROR_OP: FLASH Operation error
\r
613 * @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
\r
614 * @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
\r
615 * @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
\r
616 * @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
\r
617 * @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
\r
618 * @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error
\r
619 * @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error
\r
620 * @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error
\r
621 * @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error
\r
622 * @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)
\r
623 * @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected
\r
625 uint32_t HAL_FLASH_GetError(void)
\r
627 return pFlash.ErrorCode;
\r
638 /* Private functions ---------------------------------------------------------*/
\r
640 /** @addtogroup FLASH_Private_Functions
\r
645 * @brief Wait for a FLASH operation to complete.
\r
646 * @param Timeout: maximum flash operation timeout
\r
647 * @retval HAL_StatusTypeDef HAL Status
\r
649 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
\r
651 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
\r
652 Even if the FLASH operation fails, the BUSY flag will be reset and an error
\r
653 flag will be set */
\r
655 uint32_t tickstart = HAL_GetTick();
\r
658 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
\r
660 if(Timeout != HAL_MAX_DELAY)
\r
662 if((HAL_GetTick() - tickstart) >= Timeout)
\r
664 return HAL_TIMEOUT;
\r
669 error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
\r
670 error |= (FLASH->ECCR & FLASH_FLAG_ECCD);
\r
674 /*Save the error code*/
\r
675 pFlash.ErrorCode |= error;
\r
677 /* Clear error programming flags */
\r
678 __HAL_FLASH_CLEAR_FLAG(error);
\r
683 /* Check FLASH End of Operation flag */
\r
684 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
\r
686 /* Clear FLASH End of Operation pending bit */
\r
687 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
\r
690 /* If there is an error flag set */
\r
695 * @brief Program double-word (64-bit) at a specified address.
\r
696 * @param Address: specifies the address to be programmed.
\r
697 * @param Data: specifies the data to be programmed.
\r
700 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
\r
702 /* Check the parameters */
\r
703 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
\r
706 SET_BIT(FLASH->CR, FLASH_CR_PG);
\r
708 /* Program first word */
\r
709 *(__IO uint32_t*)Address = (uint32_t)Data;
\r
711 /* Barrier to ensure programming is performed in 2 steps, in right order
\r
712 (independently of compiler optimization behavior) */
\r
715 /* Program second word */
\r
716 *(__IO uint32_t*)(Address+4U) = (uint32_t)(Data >> 32);
\r
720 * @brief Fast program a row double-word (64-bit) at a specified address.
\r
721 * @param Address: specifies the address to be programmed.
\r
722 * @param DataAddress: specifies the address where the data are stored.
\r
725 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
\r
727 uint32_t primask_bit;
\r
728 uint8_t row_index = (2*FLASH_NB_DOUBLE_WORDS_IN_ROW);
\r
729 __IO uint32_t *dest_addr = (__IO uint32_t*)Address;
\r
730 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
\r
732 /* Check the parameters */
\r
733 assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
\r
735 /* Set FSTPG bit */
\r
736 SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
\r
738 /* Disable interrupts to avoid any interruption during the loop */
\r
739 primask_bit = __get_PRIMASK();
\r
742 /* Program the double word of the row */
\r
745 *dest_addr = *src_addr;
\r
749 } while (row_index != 0U);
\r
751 /* Re-enable the interrupts */
\r
752 __set_PRIMASK(primask_bit);
\r
759 #endif /* HAL_FLASH_MODULE_ENABLED */
\r
769 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r