2 ******************************************************************************
3 * @file stm32l0xx_hal_flash_ex.c
4 * @author MCD Application Team
6 * @date 06-February-2015
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 STM32L0xx
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 STM32L0xx. It includes:
32 (+) Full DATA_EEPROM erase and program management
34 (+) PCROP protection configuration and control for all sectors
37 ******************************************************************************
40 * <h2><center>© COPYRIGHT(c) 2015 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 "stm32l0xx_hal.h"
70 /** @addtogroup STM32L0xx_HAL_Driver
74 /** @addtogroup 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 HAL_StatusTypeDef
FLASH_OB_RDPConfig(uint8_t OB_RDP
);
87 static HAL_StatusTypeDef
FLASH_OB_BORConfig(uint8_t OB_BOR
);
88 static uint8_t FLASH_OB_GetUser(void);
89 static uint8_t FLASH_OB_GetRDP(void);
90 static uint8_t FLASH_OB_GetBOR(void);
91 static uint8_t FLASH_OB_GetBOOTBit1(void);
92 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
93 static HAL_StatusTypeDef
FLASH_OB_ProtectedSectorsConfig(uint32_t Sector
, uint32_t Sector2
, uint32_t NewState
);
95 static HAL_StatusTypeDef
FLASH_OB_ProtectedSectorsConfig(uint32_t Sector
, uint32_t NewState
);
97 static HAL_StatusTypeDef
FLASH_OB_PCROPSelectionConfig(uint32_t WPRMOD
);
98 static uint32_t FLASH_OB_GetWRP(void);
99 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
100 static uint32_t FLASH_OB_GetWRP2(void);
102 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint8_t OB_IWDG
, uint8_t OB_STOP
, uint8_t OB_STDBY
);
103 static HAL_StatusTypeDef
FLASH_OB_BOOTBit1Config(uint8_t OB_BOOT_Bit1
);
104 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
105 static HAL_StatusTypeDef
FLASH_OB_BFB2Config(uint8_t OB_BFB2
);
108 /* Exported functions ---------------------------------------------------------*/
110 /** @addtogroup FLASHEx_Exported_Functions
114 /** @addtogroup FLASHEx_Exported_Functions_Group1
115 * @brief FLASH Memory Erasing functions
118 ==============================================================================
119 ##### FLASH Erasing Programming functions #####
120 ==============================================================================
122 [..] The FLASH Memory Erasing functions, includes the following functions:
123 (+) HAL_FLASHEx_Erase: return only when erase has been done
124 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback is called with parameter
127 [..] Any operation of erase should follow these steps:
128 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
129 program memory access.
130 (#) Call the desired function to erase page.
131 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
132 (recommended to protect the FLASH memory against possible unwanted operation).
139 * @brief Erase the specified FLASH memory Pages
140 * @note To correctly run this function, the HAL_FLASH_Unlock() function
141 * must be called before.
142 * Call the HAL_FLASH_Lock() to disable the flash memory access
143 * (recommended to protect the FLASH memory against possible unwanted operation)
144 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
145 * contains the configuration information for the erasing.
147 * @param[out] PageError: pointer to variable that
148 * contains the configuration information on faulty sector in case of error
149 * (0xFFFFFFFF means that all the sectors have been correctly erased)
151 * @retval HAL_StatusTypeDef HAL Status
153 HAL_StatusTypeDef
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef
*pEraseInit
, uint32_t *PageError
)
155 HAL_StatusTypeDef status
= HAL_ERROR
;
159 __HAL_LOCK(&ProcFlash
);
161 /* Wait for last operation to be completed */
162 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
164 if (status
== HAL_OK
)
166 /* Clean the error context */
167 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
169 /*Initialization of PageError variable*/
170 *PageError
= 0xFFFFFFFF;
172 /* Check the parameters */
173 assert_param(IS_NBPAGES(pEraseInit
->NbPages
));
174 assert_param(IS_FLASH_TYPEERASE(pEraseInit
->TypeErase
));
175 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
));
176 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
+ pEraseInit
->NbPages
* FLASH_PAGE_SIZE
- 1));
178 /* Erase by sector by sector to be done*/
179 for(index
= pEraseInit
->PageAddress
; index
< ((pEraseInit
->NbPages
* FLASH_PAGE_SIZE
)+ pEraseInit
->PageAddress
); index
+= FLASH_PAGE_SIZE
)
181 FLASH_ErasePage(index
);
183 /* Wait for last operation to be completed */
184 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
186 /* If the erase operation is completed, disable the ERASE Bit */
187 CLEAR_BIT(FLASH
->PECR
, FLASH_PECR_PROG
);
188 CLEAR_BIT(FLASH
->PECR
, FLASH_PECR_ERASE
);
190 if (status
!= HAL_OK
)
192 /* In case of error, stop erase procedure and return the faulty sector*/
199 /* Process Unlocked */
200 __HAL_UNLOCK(&ProcFlash
);
206 * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
207 * @note To correctly run this function, the HAL_FLASH_Unlock() function
208 * must be called before.
209 * Call the HAL_FLASH_Lock() to disable the flash memory access
210 * (recommended to protect the FLASH memory against possible unwanted operation).
211 End of erase is done when HAL_FLASH_EndOfOperationCallback is called with parameter
213 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
214 * contains the configuration information for the erasing.
216 * @retval HAL_StatusTypeDef HAL Status
218 HAL_StatusTypeDef
HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef
*pEraseInit
)
220 HAL_StatusTypeDef status
= HAL_OK
;
223 __HAL_LOCK(&ProcFlash
);
225 /* Enable End of FLASH Operation interrupt */
226 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP
);
228 /* Enable Error source interrupt */
229 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR
);
231 /* Check the parameters */
232 assert_param(IS_NBPAGES(pEraseInit
->NbPages
));
233 assert_param(IS_FLASH_TYPEERASE(pEraseInit
->TypeErase
));
234 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
));
235 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
+ pEraseInit
->NbPages
* FLASH_PAGE_SIZE
- 1));
237 /* Clean the error context */
238 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
239 ProcFlash
.ProcedureOnGoing
= FLASH_PROC_PAGEERASE
;
240 ProcFlash
.NbPagesToErase
= pEraseInit
->NbPages
;
241 ProcFlash
.Page
= pEraseInit
->PageAddress
;
243 /* Erase 1st page and wait for IT */
244 FLASH_ErasePage(pEraseInit
->PageAddress
);
253 /** @addtogroup FLASHEx_Exported_Functions_Group2
254 * @brief Option Bytes Programming functions
257 ==============================================================================
258 ##### Option Bytes Programming functions #####
259 ==============================================================================
261 [..] Any operation of erase or program should follow these steps:
262 (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
264 (#) Call following function to program the desired option bytes.
265 (++) HAL_FLASHEx_OBProgram:
266 - To Enable/Disable the desired sector write protection.
267 - To set the desired read Protection Level.
268 - To configure the user option Bytes: IWDG, STOP and the Standby.
269 - To Set the BOR level.
270 (#) Once all needed option bytes to be programmed are correctly written, call the
271 HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
272 (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
273 to protect the option Bytes against possible unwanted operations).
275 [..] Proprietary code Read Out Protection (PcROP):
276 (#) The PcROP sector is selected by using the same option bytes as the Write
277 protection (nWRPi bits). As a result, these 2 options are exclusive each other.
278 (#) In order to activate the PcROP (change the function of the nWRPi option bits),
279 the WPRMOD option bit must be activated.
280 (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
281 means: if WPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
282 is read/write protected.
283 (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
284 (++) HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
285 (++) HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
291 * @brief Program option bytes
292 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
293 * contains the configuration information for the programming.
295 * @retval HAL_StatusTypeDef HAL Status
297 HAL_StatusTypeDef
HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef
*pOBInit
)
299 HAL_StatusTypeDef status
= HAL_ERROR
;
302 __HAL_LOCK(&ProcFlash
);
304 /* Check the parameters */
305 assert_param(IS_OPTIONBYTE(pOBInit
->OptionType
));
307 /* Write protection configuration */
308 if((pOBInit
->OptionType
& OPTIONBYTE_WRP
) == OPTIONBYTE_WRP
)
310 assert_param(IS_WRPSTATE(pOBInit
->WRPState
));
311 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
312 status
= FLASH_OB_ProtectedSectorsConfig(pOBInit
->WRPSector
, pOBInit
->WRPSector2
, pOBInit
->WRPState
);
314 status
= FLASH_OB_ProtectedSectorsConfig(pOBInit
->WRPSector
, pOBInit
->WRPState
);
316 if (status
!= HAL_OK
)
318 /* Process Unlocked */
319 __HAL_UNLOCK(&ProcFlash
);
324 /* Read protection configuration */
325 if((pOBInit
->OptionType
& OPTIONBYTE_RDP
) == OPTIONBYTE_RDP
)
327 status
= FLASH_OB_RDPConfig(pOBInit
->RDPLevel
);
328 if (status
!= HAL_OK
)
330 /* Process Unlocked */
331 __HAL_UNLOCK(&ProcFlash
);
336 /* USER configuration */
337 if((pOBInit
->OptionType
& OPTIONBYTE_USER
) == OPTIONBYTE_USER
)
339 status
= FLASH_OB_UserConfig(pOBInit
->USERConfig
& OB_IWDG_SW
,
340 pOBInit
->USERConfig
& OB_STOP_NORST
,
341 pOBInit
->USERConfig
& OB_STDBY_NORST
);
342 if (status
!= HAL_OK
)
344 /* Process Unlocked */
345 __HAL_UNLOCK(&ProcFlash
);
350 /* BOR Level configuration */
351 if((pOBInit
->OptionType
& OPTIONBYTE_BOR
) == OPTIONBYTE_BOR
)
353 status
= FLASH_OB_BORConfig(pOBInit
->BORLevel
);
356 /* Program BOOT Bit1 config option byte */
357 if ((pOBInit
->OptionType
& OPTIONBYTE_BOOT_BIT1
) == OPTIONBYTE_BOOT_BIT1
)
359 status
= FLASH_OB_BOOTBit1Config(pOBInit
->BOOTBit1Config
);
362 /* Process Unlocked */
363 __HAL_UNLOCK(&ProcFlash
);
368 * @brief Get the Option byte configuration
369 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
370 * contains the configuration information for the programming.
374 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef
*pOBInit
)
376 pOBInit
->OptionType
= OPTIONBYTE_WRP
| OPTIONBYTE_RDP
| OPTIONBYTE_USER
| OPTIONBYTE_BOR
;
379 pOBInit
->WRPSector
= FLASH_OB_GetWRP();
381 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
382 pOBInit
->WRPSector2
= FLASH_OB_GetWRP2();
386 pOBInit
->RDPLevel
= FLASH_OB_GetRDP();
389 pOBInit
->USERConfig
= FLASH_OB_GetUser();
392 pOBInit
->BORLevel
= FLASH_OB_GetBOR();
394 /* Get BOOT bit 1 config OB */
395 pOBInit
->BOOTBit1Config
= FLASH_OB_GetBOOTBit1();
400 * @brief Program option bytes
401 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
402 * contains the configuration information for the programming.
404 * @retval HAL_StatusTypeDef HAL Status
406 HAL_StatusTypeDef
HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef
*pAdvOBInit
)
408 HAL_StatusTypeDef status
= HAL_ERROR
;
410 /* Check the parameters */
411 assert_param(IS_OBEX(pAdvOBInit
->OptionType
));
413 /* Program PCROP option byte */
414 if ((pAdvOBInit
->OptionType
& OPTIONBYTE_PCROP
) == OPTIONBYTE_PCROP
)
416 /* Check the parameters */
417 assert_param(IS_PCROPSTATE(pAdvOBInit
->PCROPState
));
418 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
419 status
= FLASH_OB_ProtectedSectorsConfig(pAdvOBInit
->PCROPSector
, pAdvOBInit
->PCROPSector2
, pAdvOBInit
->PCROPState
);
421 status
= FLASH_OB_ProtectedSectorsConfig(pAdvOBInit
->PCROPSector
, pAdvOBInit
->PCROPState
);
424 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
425 if ((pAdvOBInit
->OptionType
& OPTIONBYTE_BOOTCONFIG
) == OPTIONBYTE_BOOTCONFIG
)
427 status
= FLASH_OB_BFB2Config(pAdvOBInit
->BootConfig
);
435 * @brief Get the OBEX byte configuration
436 * @param pAdvOBInit: pointer to an FLASH_AdvOBProgramInitTypeDef structure that
437 * contains the configuration information for the programming.
441 void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef
*pAdvOBInit
)
443 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
444 pAdvOBInit
->OptionType
= OPTIONBYTE_PCROP
| OPTIONBYTE_BOOTCONFIG
;
446 pAdvOBInit
->OptionType
= OPTIONBYTE_PCROP
;
448 /* Get PCROP state */
449 pAdvOBInit
->PCROPState
= (FLASH
->OPTR
& FLASH_OPTR_WPRMOD
) >> 8;
450 /* Get PCROP protected sector */
451 pAdvOBInit
->PCROPSector
= FLASH
->WRPR
;
453 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
454 /* Get PCROP protected sector */
455 pAdvOBInit
->PCROPSector2
= FLASH
->WRPR2
;
457 /* Get boot bank config */
458 pAdvOBInit
->BootConfig
= (FLASH
->OPTR
& FLASH_OPTR_BFB2
) >> 23;
463 * @brief Select the Protection Mode (WPRMOD).
464 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
465 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
469 HAL_StatusTypeDef
HAL_FLASHEx_OB_SelectPCROP(void)
471 return (FLASH_OB_PCROPSelectionConfig(1));
475 * @brief Deselect the Protection Mode (WPRMOD).
476 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
477 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
481 HAL_StatusTypeDef
HAL_FLASHEx_OB_DeSelectPCROP(void)
483 return (FLASH_OB_PCROPSelectionConfig(0));
490 /** @addtogroup FLASHEx_Exported_Functions_Group3
491 * @brief DATA EEPROM Programming functions
494 ===============================================================================
495 ##### DATA EEPROM Programming functions #####
496 ===============================================================================
498 [..] The FLASH_DATAEEPROM Programming_Functions, includes the following functions:
499 (+) HAL_FLASHEx_DATAEEPROM_Unlock(void);
500 (+) HAL_FLASHEx_DATAEEPROM_Lock(void);
501 (+) HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address)
502 (+) HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
504 [..] Any operation of erase or program should follow these steps:
505 (#) Call the HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
506 and Flash program erase control register access.
507 (#) Call the desired function to erase or program data.
508 (#) Call the HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
509 and Flash program erase control register access(recommended
510 to protect the DATA_EEPROM against possible unwanted operation).
516 * @brief Unlocks the data memory and FLASH_PECR register access.
518 * @retval HAL_StatusTypeDef HAL Status
520 HAL_StatusTypeDef
HAL_FLASHEx_DATAEEPROM_Unlock(void)
522 if((FLASH
->PECR
& FLASH_PECR_PELOCK
) != RESET
)
524 /* Unlocking the Data memory and FLASH_PECR register access */
525 FLASH
->PEKEYR
= FLASH_PEKEY1
;
526 FLASH
->PEKEYR
= FLASH_PEKEY2
;
536 * @brief Locks the Data memory and FLASH_PECR register access.
538 * @retval HAL_StatusTypeDef HAL Status
540 HAL_StatusTypeDef
HAL_FLASHEx_DATAEEPROM_Lock(void)
542 /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
543 SET_BIT(FLASH
->PECR
, FLASH_PECR_PELOCK
);
548 * @brief Erase a word in data memory.
549 * @param Address: specifies the address to be erased.
550 * @note To correctly run this function, the HAL_FLASHEx_DATAEEPROM_Unlock() function
551 * must be called before.
552 * Call the HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
553 * and Flash program erase control register access(recommended to protect
554 * the DATA_EEPROM against possible unwanted operation).
557 HAL_StatusTypeDef
HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address
)
559 HAL_StatusTypeDef status
= HAL_OK
;
561 /* Check the parameters */
562 assert_param(IS_FLASH_DATA_ADDRESS(Address
));
564 /* Wait for last operation to be completed */
565 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
569 /* Clean the error context */
570 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
572 /* Write "00000000h" to valid address in the data memory" */
573 *(__IO
uint32_t *) Address
= 0x00000000;
579 * @brief Program word at a specified address
580 * @param TypeProgram: Indicate the way to program at a specified address.
581 * This parameter can be a value of @ref FLASHEx_Type_Program_Data
582 * @param Address: specifies the address to be programmed.
583 * @param Data: specifies the data to be programmed
585 * @retval HAL_StatusTypeDef HAL Status
588 HAL_StatusTypeDef
HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram
, uint32_t Address
, uint32_t Data
)
590 HAL_StatusTypeDef status
= HAL_ERROR
;
593 __HAL_LOCK(&ProcFlash
);
595 /* Check the parameters */
596 assert_param(IS_TYPEPROGRAMDATA(TypeProgram
));
597 assert_param(IS_FLASH_DATA_ADDRESS(Address
));
599 /* Wait for last operation to be completed */
600 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
604 /* Clean the error context */
605 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
607 if(TypeProgram
== FLASH_TYPEPROGRAMDATA_WORD
)
609 /* Program word (32-bit) at a specified address */
610 *(__IO
uint32_t *)Address
= Data
;
612 else if(TypeProgram
== FLASH_TYPEPROGRAMDATA_HALFWORD
)
614 /* Program word (16-bit) at a specified address */
615 *(__IO
uint16_t *)Address
= (uint16_t) Data
;
617 else if(TypeProgram
== FLASH_TYPEPROGRAMDATA_BYTE
)
619 /*Program word (8-bit) at a specified address */
620 *(__IO
uint8_t *)Address
= (uint8_t) Data
;
627 /* Wait for last operation to be completed */
628 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
630 /* Process Unlocked */
631 __HAL_UNLOCK(&ProcFlash
);
636 * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
639 void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
641 SET_BIT(FLASH
->PECR
, FLASH_PECR_FIX
);
645 * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
648 void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
650 CLEAR_BIT(FLASH
->PECR
, FLASH_PECR_FIX
);
658 * @brief Returns the FLASH User Option Bytes values.
660 * @retval The FLASH User Option Bytes.
662 static uint8_t FLASH_OB_GetUser(void)
664 /* Return the User Option Byte */
665 return (uint8_t)((FLASH
->OPTR
& FLASH_OPTR_USER
) >> 16);
669 * @brief Returns the FLASH Read out Protection Level.
671 * @retval FLASH RDP level.
673 static uint8_t FLASH_OB_GetRDP(void)
675 return (uint8_t)(FLASH
->OPTR
& FLASH_OPTR_RDPROT
);
679 * @brief Returns the FLASH BOR level.
681 * @retval The BOR level Option Bytes.
683 static uint8_t FLASH_OB_GetBOR(void)
685 /* Return the BOR level */
686 return (uint8_t)((FLASH
->OPTR
& (uint32_t)FLASH_OPTR_BOR_LEV
) >> 16);
690 * @brief Returns the FLASH BOOT bit1 value.
692 * @retval The BOOT bit 1 value Option Bytes.
694 static uint8_t FLASH_OB_GetBOOTBit1(void)
696 /* Return the BOR level */
697 return (FLASH
->OPTR
& FLASH_OPTR_BOOT1
) >> 31;
702 * @brief Returns the FLASH Write Protection Option Bytes value.
704 * @retval The FLASH Write Protection Option Bytes value.
706 static uint32_t FLASH_OB_GetWRP(void)
708 /* Return the FLASH write protection Register value */
709 return (uint32_t)(FLASH
->WRPR
);
712 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
714 * @brief Returns the FLASH Write Protection Option Bytes value.
716 * @retval The FLASH Write Protection Option Bytes value.
718 static uint32_t FLASH_OB_GetWRP2(void)
720 /* Return the FLASH write protection Register value */
721 return (uint32_t)(FLASH
->WRPR2
);
723 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
726 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
727 * @param OB_IWDG: Selects the WDG mode.
728 * This parameter can be one of the following values:
729 * @arg OB_IWDG_SW: Software WDG selected
730 * @arg OB_IWDG_HW: Hardware WDG selected
731 * @param OB_STOP: Reset event when entering STOP mode.
732 * This parameter can be one of the following values:
733 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
734 * @arg OB_STOP_RST: Reset generated when entering in STOP
735 * @param OB_STDBY: Reset event when entering Standby mode.
736 * This parameter can be one of the following values:
737 * @arg OB_STDBY_NORST: No reset generated when entering in STANDBY
738 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
741 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint8_t OB_IWDG
, uint8_t OB_STOP
, uint8_t OB_STDBY
)
743 HAL_StatusTypeDef status
= HAL_OK
;
744 uint32_t tmp
= 0, tmp1
= 0, OB_Bits
= (uint32_t) (OB_IWDG
| OB_STOP
| OB_STDBY
);
746 /* Check the parameters */
747 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG
));
748 assert_param(IS_OB_STOP_SOURCE(OB_STOP
));
749 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY
));
751 /* Clean the error context */
752 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
754 /* Get the User Option byte register */
755 tmp1
= OB
->USER
& ((~FLASH_OPTR_USER
) >> 16);
757 /* Calculate the user option byte to write */
758 tmp
= (~(OB_Bits
| tmp1
)) << 16;
759 tmp
|= OB_Bits
| tmp1
;
761 /* Wait for last operation to be completed */
762 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
766 /* Clean the error context */
767 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
770 /* Wait for last operation to be completed */
771 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
777 * @brief Enables or disables the read out protection.
778 * @note To correctly run this function, the FLASH_OB_Unlock() function
779 * must be called before.
780 * @param OB_RDP: specifies the read protection level.
781 * This parameter can be:
782 * @arg OB_RDP_LEVEL0: No protection
783 * @arg OB_RDP_LEVEL1: Read protection of the memory
784 * @arg OB_RDP_LEVEL2: Chip protection
786 * !!!Warning!!! When enabling OB_RDP_LEVEL2 it's no more possible to go back to level 1 or 0
790 static HAL_StatusTypeDef
FLASH_OB_RDPConfig(uint8_t OB_RDP
)
792 HAL_StatusTypeDef status
;
793 uint32_t tmp
= 0, tmp1
= 0, OB_Bits
= (uint32_t) OB_RDP
;
795 /* Check the parameters */
796 assert_param(IS_OB_RDP(OB_RDP
));
798 /* Calculate the option byte to write */
799 tmp
= (OB
->RDP
& ((~FLASH_OPTR_RDPROT
) & 0x0000FFFF)) | OB_Bits
;
800 tmp1
= (~tmp
<< 16) | tmp
;
802 /* Wait for last operation to be completed */
803 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
807 /* Clean the error context */
808 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
811 /* Wait for last operation to be completed */
812 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
818 * @brief Programs the FLASH brownout reset threshold level Option Byte.
819 * @param OB_BOR: Selects the brownout reset threshold level.
820 * This parameter can be one of the following values:
821 * @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD
822 * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
823 * @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
824 * @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
825 * @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
826 * @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
827 * @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
830 static HAL_StatusTypeDef
FLASH_OB_BORConfig(uint8_t OB_BOR
)
832 HAL_StatusTypeDef status
= HAL_OK
;
833 uint32_t tmp
= 0, tmp1
= 0, OB_Bits
= (uint32_t) OB_BOR
;
835 /* Check the parameters */
836 assert_param(IS_OB_BOR_LEVEL(OB_BOR
));
838 /* Get the User Option byte register */
839 tmp1
= OB
->USER
& ((~FLASH_OPTR_BOR_LEV
) >> 16);
841 /* Calculate the user option byte to write */
842 tmp
= (~(OB_Bits
| tmp1
)) << 16;
843 tmp
|= OB_Bits
| tmp1
;
845 /* Wait for last operation to be completed */
846 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
850 /* Clean the error context */
851 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
854 /* Wait for last operation to be completed */
855 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
862 * @brief Sets or resets the BOOT bit1 option bit.
863 * @param OB_BOOT_BIT1: Set or Reset the BOOT bit1 option bit.
864 * This parameter can be one of the following values:
865 * @arg OB_BOOT_BIT1_RESET: BOOT1 option bit reset
866 * @arg OB_BOOT_BIT1_SET: BOOT1 option bit set
869 static HAL_StatusTypeDef
FLASH_OB_BOOTBit1Config(uint8_t OB_BOOT_BIT1
)
871 HAL_StatusTypeDef status
= HAL_OK
;
872 uint32_t tmp
= 0, tmp1
= 0, OB_Bits
= ((uint32_t) OB_BOOT_BIT1
) << 15;
874 /* Check the parameters */
875 assert_param(IS_OB_BOOT1(OB_BOOT_BIT1
));
877 /* Get the User Option byte register */
878 tmp1
= OB
->USER
& ((~FLASH_OPTR_BOOT1
) >> 16);
880 /* Calculate the user option byte to write */
881 tmp
= (~(OB_Bits
| tmp1
)) << 16;
882 tmp
|= OB_Bits
| tmp1
;
884 /* Wait for last operation to be completed */
885 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
889 /* Clean the error context */
890 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
893 /* Wait for last operation to be completed */
894 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
901 * @brief Select the Protection Mode (WPRMOD).
902 * @note Once WPRMOD bit is active, unprotection of a protected sector is not possible
903 * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
904 * @param OB_PcROP: Select the Protection Mode of WPR bits.
905 * This parameter can be:
906 * @arg OB_PCROP_SELECTED: nWRP control the read&write protection (PcROP) of respective user sectors.
907 * @arg OB_PCROP_DESELECTED: nWRP control the write protection of respective user sectors.
910 static HAL_StatusTypeDef
FLASH_OB_PCROPSelectionConfig(uint32_t WPRMOD
)
912 HAL_StatusTypeDef status
;
913 uint32_t tmp
= 0, tmp1
= 0, OB_Bits
= ((uint32_t) WPRMOD
) << 8;
915 /* Get the User Option byte register */
916 tmp1
= OB
->USER
& ((~FLASH_OPTR_WPRMOD
) >> 16);
918 /* Calculate the user option byte to write */
919 tmp
= (~(OB_Bits
| tmp1
)) << 16;
920 tmp
|= OB_Bits
| tmp1
;
922 /* Wait for last operation to be completed */
923 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
927 /* Clean the error context */
928 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
931 /* Wait for last operation to be completed */
932 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
938 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
940 * @brief Sets or resets the BFB2 option bit.
941 * @param BFB2: Set or Reset the BFB2 option bit.
942 * This parameter can be one of the following values:
943 * @arg OB_BOOT_BANK1: BFB2 option bit reset
944 * @arg OB_BOOT_BANK2: BFB2 option bit set
947 static HAL_StatusTypeDef
FLASH_OB_BFB2Config(uint8_t OB_BFB2
)
949 HAL_StatusTypeDef status
= HAL_OK
;
950 uint32_t tmp
= 0, tmp1
= 0, OB_Bits
= ((uint32_t) OB_BFB2
) << 7;
952 /* Check the parameters */
953 assert_param(IS_OB_BOOT_BANK(OB_BFB2
));
955 /* Get the User Option byte register */
956 tmp1
= OB
->USER
& ((~FLASH_OPTR_BFB2
) >> 16);
958 /* Calculate the user option byte to write */
959 tmp
= (~(OB_Bits
| tmp1
)) << 16;
960 tmp
|= OB_Bits
| tmp1
;
962 /* Wait for last operation to be completed */
963 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
967 /* Clean the error context */
968 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
973 /* Wait for last operation to be completed */
974 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
981 * @brief Write Option Byte of the desired pages of the Flash.
982 * @param Sector: specifies the sectors to be write protected.
983 * @param Sector2: specifies the sectors to be write protected only stm32l07xxx and stm32l08xxx devices
984 * @param NewState: new state of the specified FLASH Pages Wite protection.
985 * This parameter can be: ENABLE or DISABLE.
986 * @retval HAL_StatusTypeDef
988 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
989 static HAL_StatusTypeDef
FLASH_OB_ProtectedSectorsConfig(uint32_t Sector
, uint32_t Sector2
, uint32_t NewState
)
991 static HAL_StatusTypeDef
FLASH_OB_ProtectedSectorsConfig(uint32_t Sector
, uint32_t NewState
)
994 HAL_StatusTypeDef status
= HAL_OK
;
995 uint32_t WRP_Data
= 0;
996 uint32_t OB_WRP
= Sector
;
998 /* Check the parameters */
999 assert_param(IS_FUNCTIONAL_STATE(NewState
));
1001 /* Wait for last operation to be completed */
1002 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
1004 if(status
== HAL_OK
)
1006 /* Clean the error context */
1007 ProcFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
1009 if (OB_WRP
& 0x0000FFFF)
1011 if (NewState
!= OB_WRPSTATE_DISABLE
)
1013 WRP_Data
= (uint16_t)(((OB_WRP
& WRP_MASK_LOW
) | OB
->WRP01
));
1014 OB
->WRP01
= (uint32_t)(~(WRP_Data
) << 16) | (WRP_Data
);
1018 WRP_Data
= (uint16_t)(~OB_WRP
& (WRP_MASK_LOW
& OB
->WRP01
));
1019 OB
->WRP01
= (uint32_t)((~WRP_Data
) << 16) | (WRP_Data
);
1022 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
1023 if (OB_WRP
& 0xFFFF0000)
1025 if (NewState
!= OB_WRPSTATE_DISABLE
)
1027 WRP_Data
= (uint16_t)((((OB_WRP
& WRP_MASK_HIGH
) >> 16 | OB
->WRP23
)));
1028 OB
->WRP23
= (uint32_t)(~(WRP_Data
) << 16) | (WRP_Data
);
1032 WRP_Data
= (uint16_t)((((~OB_WRP
& WRP_MASK_HIGH
) >> 16 & OB
->WRP23
)));
1033 OB
->WRP23
= (uint32_t)((~WRP_Data
) << 16) | (WRP_Data
);
1038 if (OB_WRP
& 0x0000FFFF)
1040 if (NewState
!= OB_WRPSTATE_DISABLE
)
1042 WRP_Data
= (uint16_t)(((OB_WRP
& WRP_MASK_LOW
) | OB
->WRP45
));
1043 OB
->WRP45
=(uint32_t)(~(WRP_Data
) << 16) | (WRP_Data
);
1047 WRP_Data
= (uint16_t)(~OB_WRP
& (WRP_MASK_LOW
& OB
->WRP45
));
1048 OB
->WRP45
= (uint32_t)((~WRP_Data
) << 16) | (WRP_Data
);
1051 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
1053 /* Wait for last operation to be completed */
1054 status
= FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE
);
1056 /* Return the write protection operation Status */
1063 #endif /* HAL_FLASH_MODULE_ENABLED */
1065 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/