2 ******************************************************************************
3 * @file stm32l1xx_hal_flash_ex.c
4 * @author MCD Application Team
6 * @date 5-September-2014
7 * @brief FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the internal FLASH memory:
10 * + FLASH Interface configuration
11 * + FLASH Memory Erasing
12 * + DATA EEPROM Programming/Erasing
13 * + Option Bytes Programming
14 * + Interrupts management
17 ==============================================================================
18 ##### Flash peripheral Extended features #####
19 ==============================================================================
21 [..] Comparing to other products, the FLASH interface for STM32L1xx
22 devices contains the following additional features
24 (+) DATA_EEPROM memory management
25 (+) BOOT option bit configuration
26 (+) PCROP protection for all sectors
28 ##### How to use this driver #####
29 ==============================================================================
30 [..] This driver provides functions to configure and program the FLASH memory
31 of all STM32L1xx. It includes:
32 (+) Full DATA_EEPROM erase and program management
34 (+) PCROP protection configuration and control for all pages
37 ******************************************************************************
40 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
42 * Redistribution and use in source and binary forms, with or without modification,
43 * are permitted provided that the following conditions are met:
44 * 1. Redistributions of source code must retain the above copyright notice,
45 * this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright notice,
47 * this list of conditions and the following disclaimer in the documentation
48 * and/or other materials provided with the distribution.
49 * 3. Neither the name of STMicroelectronics nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
60 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 ******************************************************************************
67 /* Includes ------------------------------------------------------------------*/
68 #include "stm32l1xx_hal.h"
70 /** @addtogroup STM32L1xx_HAL_Driver
74 /** @defgroup FLASHEx FLASHEx
75 * @brief FLASH HAL Extension module driver
79 #ifdef HAL_FLASH_MODULE_ENABLED
81 /* Private typedef -----------------------------------------------------------*/
82 /* Private define ------------------------------------------------------------*/
83 /* Private macro -------------------------------------------------------------*/
84 /* Private variables ---------------------------------------------------------*/
85 /* Private function prototypes -----------------------------------------------*/
86 static void FLASH_SetErrorCode(void);
87 static void FLASH_ErasePage(uint32_t PageAddress);
89 static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState);
90 static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState);
91 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
92 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
93 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
94 static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState);
95 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
96 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
97 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
98 static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState);
99 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
100 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
101 static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState);
102 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
103 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
104 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
105 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
106 static uint8_t FLASH_OB_GetRDP(void);
107 static uint8_t FLASH_OB_GetUser(void);
108 static uint8_t FLASH_OB_GetBOR(void);
109 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
110 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
111 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState);
112 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
113 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
114 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
115 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT);
116 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
118 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data);
119 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);
120 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data);
121 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data);
122 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);
123 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data);
125 /* Exported functions ---------------------------------------------------------*/
127 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported functions
131 /** @defgroup FLASHEx_Exported_Functions_Group1 FLASH Memory Erasing functions
132 * @brief FLASH Memory Erasing functions
135 ==============================================================================
136 ##### FLASH Erasing Programming functions #####
137 ==============================================================================
139 [..] The FLASH Memory Erasing functions, includes the following functions:
140 (+) HAL_FLASHEx_Erase: return only when erase has been done
141 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback is called with parameter
144 [..] Any operation of erase should follow these steps:
145 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
146 program memory access.
147 (#) Call the desired function to erase page.
148 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
149 (recommended to protect the FLASH memory against possible unwanted operation).
156 * @brief Erase the specified FLASH memory Pages
157 * @note To correctly run this function, the HAL_FLASH_Unlock() function
158 * must be called before.
159 * Call the HAL_FLASH_Lock() to disable the flash memory access
160 * (recommended to protect the FLASH memory against possible unwanted operation)
161 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
162 * contains the configuration information for the erasing.
164 * @param[out] PageError: pointer to variable that
165 * contains the configuration information on faulty sector in case of error
166 * (0xFFFFFFFF means that all the sectors have been correctly erased)
168 * @retval HAL_StatusTypeDef HAL Status
170 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
172 HAL_StatusTypeDef status = HAL_ERROR;
176 __HAL_LOCK(&ProcFlash);
178 /* Wait for last operation to be completed */
179 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
181 if (status == HAL_OK)
183 /*Initialization of PageError variable*/
184 *PageError = 0xFFFFFFFF;
186 /* Check the parameters */
187 assert_param(IS_NBPAGES(pEraseInit->NbPages));
188 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
189 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
190 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
192 /* Erase by sector by sector to be done*/
193 for(index = pEraseInit->PageAddress; index < ((pEraseInit->NbPages * FLASH_PAGE_SIZE)+ pEraseInit->PageAddress); index += FLASH_PAGE_SIZE)
195 FLASH_ErasePage(index);
197 /* Wait for last operation to be completed */
198 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
200 /* If the erase operation is completed, disable the ERASE Bit */
201 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
202 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
204 if (status != HAL_OK)
206 /* In case of error, stop erase procedure and return the faulty sector*/
213 /* Process Unlocked */
214 __HAL_UNLOCK(&ProcFlash);
220 * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
221 * @note To correctly run this function, the HAL_FLASH_Unlock() function
222 * must be called before.
223 * Call the HAL_FLASH_Lock() to disable the flash memory access
224 * (recommended to protect the FLASH memory against possible unwanted operation)
225 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
226 * contains the configuration information for the erasing.
228 * @retval HAL_StatusTypeDef HAL Status
230 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
232 HAL_StatusTypeDef status = HAL_OK;
235 __HAL_LOCK(&ProcFlash);
237 /* Enable End of FLASH Operation interrupt */
238 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
240 /* Enable Error source interrupt */
241 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
243 /* Clear pending flags (if any) */
244 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
246 /* Check the parameters */
247 assert_param(IS_NBPAGES(pEraseInit->NbPages));
248 assert_param(IS_TYPEERASE(pEraseInit->TypeErase));
249 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
250 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1));
252 ProcFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
253 ProcFlash.NbPagesToErase = pEraseInit->NbPages;
254 ProcFlash.Page = pEraseInit->PageAddress;
256 /*Erase 1st page and wait for IT*/
257 FLASH_ErasePage(pEraseInit->PageAddress);
267 /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
268 * @brief Option Bytes Programming functions
271 ==============================================================================
272 ##### Option Bytes Programming functions #####
273 ==============================================================================
275 [..] Any operation of erase or program should follow these steps:
276 (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
278 (#) Call following function to program the desired option bytes.
279 (++) HAL_FLASHEx_OBProgram:
280 - To Enable/Disable the desired sector write protection.
281 - To set the desired read Protection Level.
282 - To configure the user option Bytes: IWDG, STOP and the Standby.
283 - To Set the BOR level.
284 (#) Once all needed option bytes to be programmed are correctly written, call the
285 HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
286 (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
287 to protect the option Bytes against possible unwanted operations).
289 [..] Proprietary code Read Out Protection (PcROP):
290 (#) The PcROP sector is selected by using the same option bytes as the Write
291 protection (nWRPi bits). As a result, these 2 options are exclusive each other.
292 (#) In order to activate the PcROP (change the function of the nWRPi option bits),
293 the SPRMOD option bit must be activated.
294 (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
295 means: if SPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
296 is read/write protected.
297 (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
298 (++) HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
299 (++) HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
300 (#) PcROP is available only in STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices.
307 * @brief Program option bytes
308 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
309 * contains the configuration information for the programming.
311 * @retval HAL_StatusTypeDef HAL Status
313 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
315 HAL_StatusTypeDef status = HAL_ERROR;
318 __HAL_LOCK(&ProcFlash);
320 /* Check the parameters */
321 assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
323 /*Write protection configuration*/
324 if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
326 assert_param(IS_WRPSTATE(pOBInit->WRPState));
327 if (pOBInit->WRPState == WRPSTATE_ENABLE)
329 /* Enable of Write protection on the selected Sector*/
330 status = FLASH_OB_WRPConfig(pOBInit, ENABLE);
334 /* Disable of Write protection on the selected Sector*/
335 status = FLASH_OB_WRPConfig(pOBInit, DISABLE);
337 if (status != HAL_OK)
339 /* Process Unlocked */
340 __HAL_UNLOCK(&ProcFlash);
345 /* Read protection configuration*/
346 if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
348 status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
349 if (status != HAL_OK)
351 /* Process Unlocked */
352 __HAL_UNLOCK(&ProcFlash);
357 /* USER configuration*/
358 if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
360 status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
361 pOBInit->USERConfig & OB_STOP_NORST,
362 pOBInit->USERConfig & OB_STDBY_NORST);
363 if (status != HAL_OK)
365 /* Process Unlocked */
366 __HAL_UNLOCK(&ProcFlash);
371 /* BOR Level configuration*/
372 if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
374 status = FLASH_OB_BORConfig(pOBInit->BORLevel);
376 /* Process Unlocked */
377 __HAL_UNLOCK(&ProcFlash);
383 * @brief Get the Option byte configuration
384 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
385 * contains the configuration information for the programming.
389 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
391 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
394 pOBInit->WRPSector0To31 = (uint32_t)(FLASH->WRPR1);
396 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
397 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
398 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
401 pOBInit->WRPSector32To63 = (uint32_t)(FLASH->WRPR2);
403 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
405 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
406 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
409 pOBInit->WRPSector64To95 = (uint32_t)(FLASH->WRPR3);
411 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
413 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
416 pOBInit->WRPSector96To127 = (uint32_t)(FLASH->WRPR4);
418 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
421 pOBInit->RDPLevel = FLASH_OB_GetRDP();
424 pOBInit->USERConfig = FLASH_OB_GetUser();
427 pOBInit->BORLevel = FLASH_OB_GetBOR();
431 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
432 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
433 defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
434 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
437 * @brief Program option bytes
438 * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
439 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
440 * contains the configuration information for the programming.
442 * @retval HAL_StatusTypeDef HAL Status
444 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
446 HAL_StatusTypeDef status = HAL_ERROR;
448 /* Check the parameters */
449 assert_param(IS_OBEX(pAdvOBInit->OptionType));
451 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
452 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
454 /* Cat2 & Cat3 devices only */
455 /*Program PCROP option byte*/
456 if ((pAdvOBInit->OptionType & OBEX_PCROP) == OBEX_PCROP)
458 /* Check the parameters */
459 assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
460 if (pAdvOBInit->PCROPState == PCROPSTATE_ENABLE)
462 /*Enable of Write protection on the selected Sector*/
463 status = FLASH_OB_PCROPConfig(pAdvOBInit, ENABLE);
464 if (status != HAL_OK)
471 /*Disable of Write protection on the selected Sector*/
472 status = FLASH_OB_PCROPConfig(pAdvOBInit, DISABLE);
473 if (status != HAL_OK)
480 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
482 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
483 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
485 /* Cat4 & Cat5 devices only */
486 /*Program BOOT config option byte*/
487 if ((pAdvOBInit->OptionType & OBEX_BOOTCONFIG) == OBEX_BOOTCONFIG)
489 status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
492 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
498 * @brief Get the OBEX byte configuration
499 * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
500 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
501 * contains the configuration information for the programming.
505 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
507 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
508 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
510 pAdvOBInit->OptionType = OBEX_PCROP;
513 pAdvOBInit->PCROPState = (FLASH->OBR & FLASH_OBR_SPRMOD) >> POSITION_VAL(FLASH_OBR_SPRMOD);
515 /*Get PCROP protected sector from 0 to 31 */
516 pAdvOBInit->PCROPSector0To31 = FLASH->WRPR1;
518 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
520 /*Get PCROP protected sector from 32 to 63 */
521 pAdvOBInit->PCROPSector32To63 = FLASH->WRPR2;
523 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
525 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
527 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
528 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
530 pAdvOBInit->OptionType = OBEX_BOOTCONFIG;
532 /*Get Boot config OB*/
533 pAdvOBInit->BootConfig = (FLASH->OBR & 0x80000000) >> 24;
535 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
538 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
540 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
541 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
544 * @brief Select the Protection Mode (SPRMOD).
545 * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
546 * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
547 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
550 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
552 HAL_StatusTypeDef status = HAL_OK;
555 uint8_t optiontmp = 0;
556 uint16_t optiontmp2 = 0;
558 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
561 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
563 /* Update Option Byte */
564 optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
566 /* calculate the option byte to write */
567 tmp1 = (uint16_t)(~(optiontmp2 ));
568 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
576 /* Wait for last operation to be completed */
577 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
579 /* Return the Read protection operation Status */
584 * @brief Deselect the Protection Mode (SPRMOD).
585 * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
586 * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
587 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
590 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
592 HAL_StatusTypeDef status = HAL_OK;
595 uint8_t optiontmp = 0;
596 uint16_t optiontmp2 = 0;
598 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
601 optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
603 /* Update Option Byte */
604 optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
606 /* calculate the option byte to write */
607 tmp1 = (uint16_t)(~(optiontmp2 ));
608 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)optiontmp2));
616 /* Wait for last operation to be completed */
617 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
619 /* Return the Read protection operation Status */
623 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
629 /** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
630 * @brief DATA EEPROM Programming functions
633 ===============================================================================
634 ##### DATA EEPROM Programming functions #####
635 ===============================================================================
637 [..] Any operation of erase or program should follow these steps:
638 (#) Call the HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
639 and Flash program erase control register access.
640 (#) Call the desired function to erase or program data.
641 (#) Call the HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
642 and Flash program erase control register access(recommended
643 to protect the DATA_EEPROM against possible unwanted operation).
650 * @brief Unlocks the data memory and FLASH_PECR register access.
651 * @retval HAL_StatusTypeDef HAL Status
653 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
655 if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
657 /* Unlocking the Data memory and FLASH_PECR register access*/
658 FLASH->PEKEYR = FLASH_PEKEY1;
659 FLASH->PEKEYR = FLASH_PEKEY2;
669 * @brief Locks the Data memory and FLASH_PECR register access.
670 * @retval HAL_StatusTypeDef HAL Status
672 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
674 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
675 SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
681 * @brief Erase a word in data memory.
682 * @param Address: specifies the address to be erased.
683 * @param TypeErase: Indicate the way to erase at a specified address.
684 * This parameter can be a value of @ref FLASH_Type_Program
685 * @note To correctly run this function, the DATA_EEPROM_Unlock() function
686 * must be called before.
687 * Call the DATA_EEPROM_Lock() to the data EEPROM access
688 * and Flash program erase control register access(recommended to protect
689 * the DATA_EEPROM against possible unwanted operation).
690 * @retval FLASH Status: The returned value can be:
691 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
693 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
695 HAL_StatusTypeDef status = HAL_OK;
697 /* Check the parameters */
698 assert_param(IS_TYPEPROGRAMDATA(TypeErase));
699 assert_param(IS_FLASH_DATA_ADDRESS(Address));
701 /* Wait for last operation to be completed */
702 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
706 if(TypeErase == TYPEERASEDATA_WORD)
708 /* Write 00000000h to valid address in the data memory */
709 *(__IO uint32_t *) Address = 0x00000000;
712 if(TypeErase == TYPEERASEDATA_HALFWORD)
714 /* Write 0000h to valid address in the data memory */
715 *(__IO uint16_t *) Address = (uint16_t)0x0000;
718 if(TypeErase == TYPEERASEDATA_BYTE)
720 /* Write 00h to valid address in the data memory */
721 *(__IO uint8_t *) Address = (uint8_t)0x00;
725 /* Return the erase status */
730 * @brief Program word at a specified address
731 * @note To correctly run this function, the HAL_FLASH_EEPROM_Unlock() function
732 * must be called before.
733 * Call the HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
734 * and Flash program erase control register access(recommended to protect
735 * the DATA_EEPROM against possible unwanted operation).
736 * @note The function HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
737 * this function to configure the Fixed Time Programming.
738 * @param TypeProgram: Indicate the way to program at a specified address.
739 * This parameter can be a value of @ref FLASHEx_Type_Program_Data
740 * @param Address: specifies the address to be programmed.
741 * @param Data: specifies the data to be programmed
743 * @retval HAL_StatusTypeDef HAL Status
746 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
748 HAL_StatusTypeDef status = HAL_ERROR;
751 __HAL_LOCK(&ProcFlash);
753 /* Check the parameters */
754 assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
756 /* Wait for last operation to be completed */
757 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
761 if(TypeProgram == TYPEPROGRAMDATA_FASTBYTE)
763 /*Program word (8-bit) at a specified address.*/
764 FLASH_DATAEEPROM_FastProgramByte(Address, (uint8_t) Data);
767 if(TypeProgram == TYPEPROGRAMDATA_FASTHALFWORD)
769 /*Program word (16-bit) at a specified address.*/
770 FLASH_DATAEEPROM_FastProgramHalfWord(Address, (uint16_t) Data);
773 if(TypeProgram == TYPEPROGRAMDATA_FASTWORD)
775 /*Program word (32-bit) at a specified address.*/
776 FLASH_DATAEEPROM_FastProgramWord(Address, (uint32_t) Data);
779 if(TypeProgram == TYPEPROGRAMDATA_WORD)
781 /*Program word (32-bit) at a specified address.*/
782 FLASH_DATAEEPROM_ProgramWord(Address, (uint32_t) Data);
785 if(TypeProgram == TYPEPROGRAMDATA_HALFWORD)
787 /*Program word (16-bit) at a specified address.*/
788 FLASH_DATAEEPROM_ProgramHalfWord(Address, (uint16_t) Data);
791 if(TypeProgram == TYPEPROGRAMDATA_BYTE)
793 /*Program word (8-bit) at a specified address.*/
794 FLASH_DATAEEPROM_ProgramByte(Address, (uint8_t) Data);
797 /* Wait for last operation to be completed */
798 status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
801 /* Process Unlocked */
802 __HAL_UNLOCK(&ProcFlash);
808 * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
811 void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
813 SET_BIT(FLASH->PECR, FLASH_PECR_FTDW);
817 * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
820 void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
822 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
833 /** @defgroup FLASHEx_Private_Functions FLASHEx Private functions
838 ==============================================================================
839 ##### FLASH STATIC functions #####
840 ==============================================================================
844 ==============================================================================
846 ==============================================================================
850 * @brief Set the specific FLASH error flag.
853 static void FLASH_SetErrorCode(void)
855 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
857 ProcFlash.ErrorCode = FLASH_ERROR_WRP;
860 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
862 ProcFlash.ErrorCode |= FLASH_ERROR_PGA;
865 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET)
867 ProcFlash.ErrorCode |= FLASH_ERROR_SIZE;
870 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET)
872 ProcFlash.ErrorCode |= FLASH_ERROR_OPTV;
875 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
876 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
877 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
879 ProcFlash.ErrorCode |= FLASH_ERROR_RD;
881 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
883 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
884 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
885 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
886 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET)
888 ProcFlash.ErrorCode |= FLASH_ERROR_OPTVUSR;
890 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
894 * @brief Erases a specified page in program memory.
895 * @param PageAddress: The page address in program memory to be erased.
896 * @note A Page is erased in the Program memory only if the address to load
897 * is the start address of a page (multiple of 256 bytes).
900 static void FLASH_ErasePage(uint32_t PageAddress)
902 /* Set the ERASE bit */
903 SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
906 SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
908 /* Write 00000000h to the first word of the program page to erase */
909 *(__IO uint32_t *)PageAddress = 0x00000000;
914 ==============================================================================
916 ==============================================================================
919 * @brief Enables or disables the read out protection.
920 * @note To correctly run this function, the HAL_FLASH_OB_Unlock() function
921 * must be called before.
922 * @param OB_RDP: specifies the read protection level.
923 * This parameter can be:
924 * @arg OB_RDP_LEVEL0: No protection
925 * @arg OB_RDP_LEVEL1: Read protection of the memory
926 * @arg OB_RDP_LEVEL2: Chip protection
928 * !!!Warning!!! When enabling OB_RDP_LEVEL2 it's no more possible to go back to level 1 or 0
932 static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
934 HAL_StatusTypeDef status = HAL_OK;
935 uint32_t tmp1 = 0, tmp2 = 0, sprmod = 0;
937 /* Check the parameters */
938 assert_param(IS_OB_RDP(OB_RDP));
940 /* According to errata sheet, DocID022054 Rev 5, par2.1.5
941 Before setting Level0 in the RDP register, check that the current level is not equal to Level0.
942 If the current level is not equal to Level0, Level0 can be activated.
943 If the current level is Level0 then the RDP register must not be written again with Level0. */
944 tmp1 = (uint32_t)(OB->RDP & 0x000000FF);
946 if ((tmp1 == OB_RDP_LEVEL0) && (OB_RDP == OB_RDP_LEVEL0))
948 /*current level is Level0 then the RDP register must not be written again with Level0. */
953 /* Mask SPRMOD bit */
954 sprmod = (uint32_t)(OB->RDP & 0x00000100);
956 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
958 /* calculate the option byte to write */
959 tmp1 = (~((uint32_t)(OB_RDP | sprmod)));
960 tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)(OB_RDP | sprmod)));
964 /* program read protection level */
968 /* Wait for last operation to be completed */
969 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
972 /* Return the Read protection operation Status */
977 * @brief Programs the FLASH brownout reset threshold level Option Byte.
978 * @param OB_BOR: Selects the brownout reset threshold level.
979 * This parameter can be one of the following values:
980 * @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD
981 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
982 * @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
983 * @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
984 * @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
985 * @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
986 * @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
989 static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
991 HAL_StatusTypeDef status = HAL_OK;
992 uint32_t tmp = 0, tmp1 = 0;
994 /* Check the parameters */
995 assert_param(IS_OB_BOR_LEVEL(OB_BOR));
997 /* Get the User Option byte register */
998 tmp1 = (FLASH->OBR & (FLASH_OBR_USER)) >> 16;
1000 /* Calculate the option byte to write - [0xFF | nUSER | 0x00 | USER]*/
1001 tmp = (uint32_t)~((OB_BOR | tmp1)) << 16;
1002 tmp |= (OB_BOR | tmp1);
1004 /* Wait for last operation to be completed */
1005 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1007 if(status == HAL_OK)
1009 /* Write the BOR Option Byte */
1013 /* Wait for last operation to be completed */
1014 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1016 /* Return the Option Byte program Status */
1021 * @brief Returns the FLASH User Option Bytes values.
1022 * @retval The FLASH User Option Bytes.
1024 static uint8_t FLASH_OB_GetUser(void)
1026 /* Return the User Option Byte */
1027 return (uint8_t)((FLASH->OBR & FLASH_OBR_USER) >> POSITION_VAL(FLASH_OBR_USER));
1031 * @brief Checks whether the FLASH Read out Protection Status is set or not.
1032 * @retval FLASH ReadOut Protection
1034 static uint8_t FLASH_OB_GetRDP(void)
1036 return (uint8_t)(FLASH->OBR & FLASH_OBR_RDPRT);
1040 * @brief Returns the FLASH BOR level.
1041 * @retval The FLASH User Option Bytes.
1043 static uint8_t FLASH_OB_GetBOR(void)
1045 /* Return the BOR level */
1046 return (uint8_t)((FLASH->OBR & (uint32_t)FLASH_OBR_BOR_LEV) >> POSITION_VAL(FLASH_OBR_BOR_LEV));
1050 * @brief Write protects the desired pages of the first 64KB of the Flash.
1051 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
1052 * contains WRP parameters.
1053 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1054 * This parameter can be: ENABLE or DISABLE.
1055 * @retval HAL_StatusTypeDef
1057 static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState)
1059 HAL_StatusTypeDef status = HAL_OK;
1061 /* Wait for last operation to be completed */
1062 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1064 if(status == HAL_OK)
1066 /* WRP for sector between 0 to 31 */
1067 if (pOBInit->WRPSector0To31 != 0)
1069 FLASH_OB_WRPConfigWRP1OrPCROP1(pOBInit->WRPSector0To31, NewState);
1072 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1073 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1074 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1076 /* Pages for Cat3, Cat4 & Cat5 devices*/
1077 /* WRP for sector between 32 to 63 */
1078 if (pOBInit->WRPSector32To63 != 0)
1080 FLASH_OB_WRPConfigWRP2OrPCROP2(pOBInit->WRPSector32To63, NewState);
1083 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1085 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
1086 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1088 /* Pages for devices with FLASH >= 256KB*/
1089 /* WRP for sector between 64 to 95 */
1090 if (pOBInit->WRPSector64To95 != 0)
1092 FLASH_OB_WRPConfigWRP3(pOBInit->WRPSector64To95, NewState);
1095 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1097 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1099 /* Pages for Cat5 devices*/
1100 /* WRP for sector between 96 to 127 */
1101 if (pOBInit->WRPSector96To127 != 0)
1103 FLASH_OB_WRPConfigWRP4(pOBInit->WRPSector96To127, NewState);
1106 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
1108 /* Wait for last operation to be completed */
1109 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1112 /* Return the write protection operation Status */
1116 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
1117 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1119 * @brief Enables the read/write protection (PCROP) of the desired
1121 * @note This function can be used only for Cat2 & Cat3 devices
1122 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
1123 * contains PCROP parameters.
1124 * @param NewState: new state of the specified FLASH Pages read/Write protection.
1125 * This parameter can be: ENABLE or DISABLE.
1126 * @retval HAL status
1128 static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState)
1130 HAL_StatusTypeDef status = HAL_OK;
1131 FunctionalState pcropstate = DISABLE;
1133 /* Wait for last operation to be completed */
1134 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1136 /* Invert state to use same function of WRP */
1137 if (NewState == DISABLE)
1139 pcropstate = ENABLE;
1142 if(status == HAL_OK)
1144 /* Pages for Cat2 devices*/
1145 /* PCROP for sector between 0 to 31 */
1146 if (pAdvOBInit->PCROPSector0To31 != 0)
1148 FLASH_OB_WRPConfigWRP1OrPCROP1(pAdvOBInit->PCROPSector0To31, pcropstate);
1151 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1153 /* Pages for Cat3 devices*/
1154 /* WRP for sector between 32 to 63 */
1155 if (pAdvOBInit->PCROPSector32To63 != 0)
1157 FLASH_OB_WRPConfigWRP2OrPCROP2(pAdvOBInit->PCROPSector32To63, pcropstate);
1160 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
1162 /* Wait for last operation to be completed */
1163 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1166 /* Return the write protection operation Status */
1169 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1172 * @brief Write protects the desired pages of the first 128KB of the Flash.
1173 * @param WRP1OrPCROP1: specifies the address of the pages to be write protected.
1174 * This parameter can be:
1175 * @arg value between OB_WRP1/PCROP1_PAGES0TO15 and OB_WRP1/PCROP1_PAGES496TO511
1176 * @arg OB_WRP1/PCROP1_ALLPAGES
1177 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1178 * This parameter can be: ENABLE or DISABLE.
1181 static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState)
1183 uint32_t wrp01data = 0, wrp23data = 0;
1185 uint32_t tmp1 = 0, tmp2 = 0;
1187 if (NewState != DISABLE)
1189 wrp01data = (uint16_t)(((WRP1OrPCROP1 & WRP_MASK_LOW) | OB->WRP01));
1190 wrp23data = (uint16_t)((((WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 | OB->WRP23)));
1191 tmp1 = (uint32_t)(~(wrp01data) << 16)|(wrp01data);
1194 tmp2 = (uint32_t)(~(wrp23data) << 16)|(wrp23data);
1199 wrp01data = (uint16_t)(~WRP1OrPCROP1 & (WRP_MASK_LOW & OB->WRP01));
1200 wrp23data = (uint16_t)((((~WRP1OrPCROP1 & WRP_MASK_HIGH)>>16 & OB->WRP23)));
1202 tmp1 = (uint32_t)((~wrp01data) << 16)|(wrp01data);
1205 tmp2 = (uint32_t)((~wrp23data) << 16)|(wrp23data);
1210 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1211 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1212 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1214 * @brief Enable Write protects the desired pages of the second 128KB of the Flash.
1215 * @note This function can be used only for Cat3, Cat4 & Cat5 devices.
1216 * @param WRP2OrPCROP2: specifies the address of the pages to be write protected.
1217 * This parameter can be:
1218 * @arg value between OB_WRP2/PCROP2_PAGES512TO527 and OB_WRP2/PCROP2_PAGES1008TO1023
1219 * @arg OB_WRP2/PCROP2_ALLPAGES
1220 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1221 * This parameter can be: ENABLE or DISABLE.
1224 static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState)
1226 uint32_t wrp45data = 0, wrp67data = 0;
1228 uint32_t tmp1 = 0, tmp2 = 0;
1230 if (NewState != DISABLE)
1232 wrp45data = (uint16_t)(((WRP2OrPCROP2 & WRP_MASK_LOW) | OB->WRP45));
1233 wrp67data = (uint16_t)((((WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 | OB->WRP67)));
1234 tmp1 = (uint32_t)(~(wrp45data) << 16)|(wrp45data);
1237 tmp2 = (uint32_t)(~(wrp67data) << 16)|(wrp67data);
1242 wrp45data = (uint16_t)(~WRP2OrPCROP2 & (WRP_MASK_LOW & OB->WRP45));
1243 wrp67data = (uint16_t)((((~WRP2OrPCROP2 & WRP_MASK_HIGH)>>16 & OB->WRP67)));
1245 tmp1 = (uint32_t)((~wrp45data) << 16)|(wrp45data);
1248 tmp2 = (uint32_t)((~wrp67data) << 16)|(wrp67data);
1252 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1254 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
1255 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1257 * @brief Enable Write protects the desired pages of the third 128KB of the Flash.
1258 * @note This function can be used only for STM32L151xD, STM32L152xD, STM32L162xD & Cat5 devices.
1259 * @param WRP3: specifies the address of the pages to be write protected.
1260 * This parameter can be:
1261 * @arg value between WRP3_PAGES1024TO1039 and OB_WRP3_PAGES1520TO1535
1262 * @arg OB_WRP3_ALLPAGES
1263 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1264 * This parameter can be: ENABLE or DISABLE.
1267 static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState)
1269 uint32_t wrp89data = 0, wrp1011data = 0;
1271 uint32_t tmp1 = 0, tmp2 = 0;
1273 if (NewState != DISABLE)
1275 wrp89data = (uint16_t)(((WRP3 & WRP_MASK_LOW) | OB->WRP89));
1276 wrp1011data = (uint16_t)((((WRP3 & WRP_MASK_HIGH)>>16 | OB->WRP1011)));
1277 tmp1 = (uint32_t)(~(wrp89data) << 16)|(wrp89data);
1280 tmp2 = (uint32_t)(~(wrp1011data) << 16)|(wrp1011data);
1285 wrp89data = (uint16_t)(~WRP3 & (WRP_MASK_LOW & OB->WRP89));
1286 wrp1011data = (uint16_t)((((~WRP3 & WRP_MASK_HIGH)>>16 & OB->WRP1011)));
1288 tmp1 = (uint32_t)((~wrp89data) << 16)|(wrp89data);
1291 tmp2 = (uint32_t)((~wrp1011data) << 16)|(wrp1011data);
1295 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1297 #if defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1299 * @brief Enable Write protects the desired pages of the Fourth 128KB of the Flash.
1300 * @note This function can be used only for Cat5 devices.
1301 * @param WRP4: specifies the address of the pages to be write protected.
1302 * This parameter can be:
1303 * @arg value between OB_WRP4_PAGES1536TO1551 and OB_WRP4_PAGES2032TO2047
1304 * @arg OB_WRP4_ALLPAGES
1305 * @param NewState: new state of the specified FLASH Pages Wtite protection.
1306 * This parameter can be: ENABLE or DISABLE.
1309 static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState)
1311 uint32_t wrp1213data = 0, wrp1415data = 0;
1313 uint32_t tmp1 = 0, tmp2 = 0;
1315 if (NewState != DISABLE)
1317 wrp1213data = (uint16_t)(((WRP4 & WRP_MASK_LOW) | OB->WRP1213));
1318 wrp1415data = (uint16_t)((((WRP4 & WRP_MASK_HIGH)>>16 | OB->WRP1415)));
1319 tmp1 = (uint32_t)(~(wrp1213data) << 16)|(wrp1213data);
1322 tmp2 = (uint32_t)(~(wrp1415data) << 16)|(wrp1415data);
1327 wrp1213data = (uint16_t)(~WRP4 & (WRP_MASK_LOW & OB->WRP1213));
1328 wrp1415data = (uint16_t)((((~WRP4 & WRP_MASK_HIGH)>>16 & OB->WRP1415)));
1330 tmp1 = (uint32_t)((~wrp1213data) << 16)|(wrp1213data);
1333 tmp2 = (uint32_t)((~wrp1415data) << 16)|(wrp1415data);
1337 #endif /* STM32L151xE || STM32L152xE || STM32L162xE */
1340 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
1341 * @param OB_IWDG: Selects the WDG mode.
1342 * This parameter can be one of the following values:
1343 * @arg OB_IWDG_SW: Software WDG selected
1344 * @arg OB_IWDG_HW: Hardware WDG selected
1345 * @param OB_STOP: Reset event when entering STOP mode.
1346 * This parameter can be one of the following values:
1347 * @arg OB_STOP_NORST: No reset generated when entering in STOP
1348 * @arg OB_STOP_RST: Reset generated when entering in STOP
1349 * @param OB_STDBY: Reset event when entering Standby mode.
1350 * This parameter can be one of the following values:
1351 * @arg OB_STDBY_NORST: No reset generated when entering in STANDBY
1352 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
1353 * @retval HAL status
1355 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
1357 HAL_StatusTypeDef status = HAL_OK;
1358 uint32_t tmp = 0, tmp1 = 0;
1360 /* Check the parameters */
1361 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
1362 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
1363 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
1365 /* Get the User Option byte register */
1366 tmp1 = (FLASH->OBR & FLASH_OBR_BOR_LEV) >> 16;
1368 /* Calculate the user option byte to write */
1369 tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16);
1370 tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
1372 /* Wait for last operation to be completed */
1373 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1375 if(status == HAL_OK)
1377 /* Write the User Option Byte */
1381 /* Wait for last operation to be completed */
1382 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1384 /* Return the Option Byte program Status */
1388 #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) || \
1389 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1391 * @brief Configures to boot from Bank1 or Bank2.
1392 * @param OB_BOOT: select the FLASH Bank to boot from.
1393 * This parameter can be one of the following values:
1394 * @arg OB_BOOT_BANK2: At startup, if boot pins are set in boot from user Flash
1395 * position and this parameter is selected the device will boot from Bank2 or Bank1,
1396 * depending on the activation of the bank. The active banks are checked in
1397 * the following order: Bank2, followed by Bank1.
1398 * The active bank is recognized by the value programmed at the base address
1399 * of the respective bank (corresponding to the initial stack pointer value
1400 * in the interrupt vector table).
1401 * @arg OB_BOOT_BANK1: At startup, if boot pins are set in boot from user Flash
1402 * position and this parameter is selected the device will boot from Bank1(Default).
1403 * For more information, please refer to AN2606 from www.st.com.
1404 * @retval HAL status
1406 static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
1408 HAL_StatusTypeDef status = HAL_OK;
1409 uint32_t tmp = 0, tmp1 = 0;
1411 /* Check the parameters */
1412 assert_param(IS_OB_BOOT_BANK(OB_BOOT));
1414 /* Get the User Option byte register and BOR Level*/
1415 tmp1 = (FLASH->OBR & (FLASH_OBR_nRST_STDBY | FLASH_OBR_nRST_STOP | FLASH_OBR_IWDG_SW | FLASH_OBR_BOR_LEV)) >> 16;
1417 /* Calculate the option byte to write */
1418 tmp = (uint32_t)~(OB_BOOT | tmp1) << 16;
1419 tmp |= (OB_BOOT | tmp1);
1421 /* Wait for last operation to be completed */
1422 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1424 if(status == HAL_OK)
1426 /* Write the BOOT Option Byte */
1430 /* Wait for last operation to be completed */
1431 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1433 /* Return the Option Byte program Status */
1437 #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1440 ==============================================================================
1442 ==============================================================================
1446 * @brief Write a Byte at a specified address in data memory.
1447 * @param Address: specifies the address to be written.
1448 * @param Data: specifies the data to be written.
1449 * @note This function assumes that the is data word is already erased.
1450 * @retval HAL status
1452 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
1454 HAL_StatusTypeDef status = HAL_OK;
1455 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1456 uint32_t tmp = 0, tmpaddr = 0;
1457 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1459 /* Check the parameters */
1460 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1462 /* Wait for last operation to be completed */
1463 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1465 if(status == HAL_OK)
1467 /* Clear the FTDW bit */
1468 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1470 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1471 /* Possible only on Cat1 devices */
1472 if(Data != (uint8_t)0x00)
1474 /* If the previous operation is completed, proceed to write the new Data */
1475 *(__IO uint8_t *)Address = Data;
1477 /* Wait for last operation to be completed */
1478 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1482 tmpaddr = Address & 0xFFFFFFFC;
1483 tmp = * (__IO uint32_t *) tmpaddr;
1484 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
1486 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1487 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1490 /* If the previous operation is completed, proceed to write the new Data */
1491 *(__IO uint8_t *)Address = Data;
1493 /* Wait for last operation to be completed */
1494 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1495 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1497 /* Return the Write Status */
1502 * @brief Writes a half word at a specified address in data memory.
1503 * @param Address: specifies the address to be written.
1504 * @param Data: specifies the data to be written.
1505 * @note This function assumes that the is data word is already erased.
1506 * @retval HAL status
1508 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
1510 HAL_StatusTypeDef status = HAL_OK;
1511 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1512 uint32_t tmp = 0, tmpaddr = 0;
1513 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1515 /* Check the parameters */
1516 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1518 /* Wait for last operation to be completed */
1519 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1521 if(status == HAL_OK)
1523 /* Clear the FTDW bit */
1524 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1526 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1527 /* Possible only on Cat1 devices */
1528 if(Data != (uint16_t)0x0000)
1530 /* If the previous operation is completed, proceed to write the new data */
1531 *(__IO uint16_t *)Address = Data;
1533 /* Wait for last operation to be completed */
1534 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1538 if((Address & 0x3) != 0x3)
1540 tmpaddr = Address & 0xFFFFFFFC;
1541 tmp = * (__IO uint32_t *) tmpaddr;
1542 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
1544 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1545 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1549 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
1550 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
1554 /* If the previous operation is completed, proceed to write the new data */
1555 *(__IO uint16_t *)Address = Data;
1557 /* Wait for last operation to be completed */
1558 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1559 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1561 /* Return the Write Status */
1566 * @brief Programs a word at a specified address in data memory.
1567 * @param Address: specifies the address to be written.
1568 * @param Data: specifies the data to be written.
1569 * @note This function assumes that the is data word is already erased.
1570 * @retval HAL status
1572 static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data)
1574 HAL_StatusTypeDef status = HAL_OK;
1576 /* Check the parameters */
1577 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1579 /* Wait for last operation to be completed */
1580 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1582 if(status == HAL_OK)
1584 /* Clear the FTDW bit */
1585 CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
1587 /* If the previous operation is completed, proceed to program the new data */
1588 *(__IO uint32_t *)Address = Data;
1590 /* Wait for last operation to be completed */
1591 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1593 /* Return the Write Status */
1598 * @brief Write a Byte at a specified address in data memory without erase.
1599 * @param Address: specifies the address to be written.
1600 * @param Data: specifies the data to be written.
1601 * @retval HAL status
1603 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data)
1605 HAL_StatusTypeDef status = HAL_OK;
1606 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1607 uint32_t tmp = 0, tmpaddr = 0;
1608 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1610 /* Check the parameters */
1611 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1613 /* Wait for last operation to be completed */
1614 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1616 if(status == HAL_OK)
1618 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1619 if(Data != (uint8_t) 0x00)
1621 *(__IO uint8_t *)Address = Data;
1623 /* Wait for last operation to be completed */
1624 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1629 tmpaddr = Address & 0xFFFFFFFC;
1630 tmp = * (__IO uint32_t *) tmpaddr;
1631 tmpaddr = 0xFF << ((uint32_t) (0x8 * (Address & 0x3)));
1633 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1634 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1637 *(__IO uint8_t *)Address = Data;
1639 /* Wait for last operation to be completed */
1640 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1641 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1643 /* Return the Write Status */
1648 * @brief Writes a half word at a specified address in data memory without erase.
1649 * @param Address: specifies the address to be written.
1650 * @param Data: specifies the data to be written.
1651 * @retval HAL status
1653 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
1655 HAL_StatusTypeDef status = HAL_OK;
1656 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1657 uint32_t tmp = 0, tmpaddr = 0;
1658 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1660 /* Check the parameters */
1661 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1663 /* Wait for last operation to be completed */
1664 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1666 if(status == HAL_OK)
1668 #if defined(STM32L100xB) || defined (STM32L151xB) || defined (STM32L152xB)
1669 if(Data != (uint16_t)0x0000)
1671 *(__IO uint16_t *)Address = Data;
1673 /* Wait for last operation to be completed */
1674 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1678 if((Address & 0x3) != 0x3)
1680 tmpaddr = Address & 0xFFFFFFFC;
1681 tmp = * (__IO uint32_t *) tmpaddr;
1682 tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
1684 status = HAL_FLASHEx_DATAEEPROM_Erase(TYPEERASEDATA_WORD, Address & 0xFFFFFFFC);
1685 status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFC), tmp);
1689 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address, 0x00);
1690 HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_FASTBYTE, Address + 1, 0x00);
1694 *(__IO uint16_t *)Address = Data;
1696 /* Wait for last operation to be completed */
1697 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1698 #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
1700 /* Return the Write Status */
1705 * @brief Programs a word at a specified address in data memory without erase.
1706 * @param Address: specifies the address to be written.
1707 * @param Data: specifies the data to be written.
1708 * @retval HAL status
1710 static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data)
1712 HAL_StatusTypeDef status = HAL_OK;
1714 /* Check the parameters */
1715 assert_param(IS_FLASH_DATA_ADDRESS(Address));
1717 /* Wait for last operation to be completed */
1718 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1720 if(status == HAL_OK)
1722 *(__IO uint32_t *)Address = Data;
1724 /* Wait for last operation to be completed */
1725 status = FLASH_WaitForLastOperation(HAL_FLASH_TIMEOUT_VALUE);
1727 /* Return the Write Status */
1739 /** @addtogroup FLASH
1743 /** @addtogroup FLASH_Exported_Functions
1747 /** @addtogroup FLASH_Exported_Functions_Group1
1748 * @brief Interrupts functions
1751 ==============================================================================
1752 ##### Interrupts functions #####
1753 ==============================================================================
1760 * @brief This function handles FLASH interrupt request.
1763 void HAL_FLASH_IRQHandler(void)
1767 /* If the program operation is completed, disable the PROG Bit */
1768 CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
1770 /* If the erase operation is completed, disable the ERASE Bit */
1771 CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
1773 /* Check FLASH End of Operation flag */
1774 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
1776 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
1778 /*Nb of sector to erased can be decreased*/
1779 ProcFlash.NbPagesToErase--;
1781 /* Check if there are still sectors to erase*/
1782 if(ProcFlash.NbPagesToErase != 0)
1784 temp = ProcFlash.Page;
1785 /*Indicate user which sector has been erased*/
1786 HAL_FLASH_EndOfOperationCallback(temp);
1788 /* Clear pending flags (if any) */
1789 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
1791 /*Increment sector number*/
1792 temp = ProcFlash.Page + FLASH_PAGE_SIZE;
1793 ProcFlash.Page = ProcFlash.Page + FLASH_PAGE_SIZE;
1794 FLASH_ErasePage(temp);
1798 /*No more sectors to Erase, user callback can be called.*/
1799 /*Reset Sector and stop Erase sectors procedure*/
1800 ProcFlash.Page = temp = 0xFFFFFFFF;
1801 ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
1802 /* FLASH EOP interrupt user callback */
1803 HAL_FLASH_EndOfOperationCallback(temp);
1804 /* Clear FLASH End of Operation pending bit */
1805 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
1810 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM)
1812 /*Program ended. Return the selected address*/
1813 /* FLASH EOP interrupt user callback */
1814 HAL_FLASH_EndOfOperationCallback(ProcFlash.Address);
1816 ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
1817 /* Clear FLASH End of Operation pending bit */
1818 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
1823 /* Check FLASH operation error flags */
1824 if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) ||
1825 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) ||
1826 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) ||
1827 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
1828 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1829 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) ||
1830 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1831 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1832 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1833 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1834 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET) ||
1835 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1836 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) )
1838 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
1840 /*return the faulty sector*/
1841 temp = ProcFlash.Page;
1842 ProcFlash.Page = 0xFFFFFFFF;
1846 /*retrun the faulty address*/
1847 temp = ProcFlash.Address;
1850 /*Save the Error code*/
1851 FLASH_SetErrorCode();
1853 /* FLASH error interrupt user callback */
1854 HAL_FLASH_OperationErrorCallback(temp);
1856 /* Clear FLASH error pending bits */
1857 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
1859 /*Stop the procedure ongoing*/
1860 ProcFlash.ProcedureOnGoing = FLASH_PROC_NONE;
1863 if(ProcFlash.ProcedureOnGoing == FLASH_PROC_NONE)
1865 /* Disable End of FLASH Operation interrupt */
1866 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
1868 /* Disable Error source interrupt */
1869 __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
1871 /* Process Unlocked */
1872 __HAL_UNLOCK(&ProcFlash);
1885 /** @defgroup FLASH_Internal_Functions FLASH Internal function
1890 * @brief Wait for a FLASH operation to complete.
1891 * @param Timeout: maximum flash operationtimeout
1892 * @retval HAL status
1894 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
1896 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
1897 Even if the FLASH operation fails, the BUSY flag will be reset and an error
1900 uint32_t tickstart = HAL_GetTick();
1902 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET)
1904 if(Timeout != HAL_MAX_DELAY)
1906 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1913 if( (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET) ||
1914 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET) ||
1915 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR) != RESET) ||
1916 #if defined (STM32L151xBA) || defined (STM32L152xBA) || \
1917 defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC)
1918 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET) ||
1919 #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
1920 #if defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
1921 defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
1922 defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
1923 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERRUSR) != RESET) ||
1924 #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1925 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) != RESET) )
1927 /*Save the error code*/
1928 FLASH_SetErrorCode();
1932 /* There is no error flag set */
1941 #endif /* HAL_FLASH_MODULE_ENABLED */
1951 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/