2 ******************************************************************************
3 * @file stm32f0xx_hal_flash_ex.c
4 * @author MCD Application Team
6 * @date 11-December-2014
7 * @brief Extended FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the FLASH peripheral:
10 * + Extended Initialization/de-initialization functions
11 * + Extended I/O operation functions
12 * + Extended Peripheral Control functions
13 * + Extended Peripheral State functions
16 ==============================================================================
17 ##### Flash peripheral extended features #####
18 ==============================================================================
20 ##### How to use this driver #####
21 ==============================================================================
22 [..] This driver provides functions to configure and program the FLASH memory
23 of all STM32F0xxx devices. It includes
25 (+) Set/Reset the write protection
26 (+) Program the user Option Bytes
27 (+) Get the Read protection Level
30 ******************************************************************************
33 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
35 * Redistribution and use in source and binary forms, with or without modification,
36 * are permitted provided that the following conditions are met:
37 * 1. Redistributions of source code must retain the above copyright notice,
38 * this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright notice,
40 * this list of conditions and the following disclaimer in the documentation
41 * and/or other materials provided with the distribution.
42 * 3. Neither the name of STMicroelectronics nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
53 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 ******************************************************************************
60 /* Includes ------------------------------------------------------------------*/
61 #include "stm32f0xx_hal.h"
63 /** @addtogroup STM32F0xx_HAL_Driver
67 /** @defgroup FLASHEx FLASHEx Extended HAL module driver
68 * @brief FLASH Extended HAL module driver
72 #ifdef HAL_FLASH_MODULE_ENABLED
74 /* Private typedef -----------------------------------------------------------*/
75 /* Private define ------------------------------------------------------------*/
77 /** @addtogroup FLASHEx_Private_Constants FLASHEx Private Constants
80 #define HAL_FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
86 /* Private macro -------------------------------------------------------------*/
87 /* Private variables ---------------------------------------------------------*/
88 /** @addtogroup FLASHEx_Private_Variables FLASHEx Private Variables
92 /* Variables used for Erase pages under interruption*/
93 extern FLASH_ProcessTypeDef pFlash
;
99 /* Private function prototypes -----------------------------------------------*/
100 /** @addtogroup FLASHEx_Private_Functions FLASHEx Private Functions
104 /* Erase operations */
105 extern void FLASH_PageErase(uint32_t PageAddress
);
106 static void FLASH_MassErase(void);
108 /* Option bytes control */
109 static HAL_StatusTypeDef
FLASH_OB_EnableWRP(uint32_t WriteProtectPage
);
110 static HAL_StatusTypeDef
FLASH_OB_DisableWRP(uint32_t WriteProtectPage
);
111 static HAL_StatusTypeDef
FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel
);
112 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint8_t UserConfig
);
113 static HAL_StatusTypeDef
FLASH_OB_ProgramData(uint32_t Address
, uint8_t Data
);
114 static uint32_t FLASH_OB_GetWRP(void);
115 static FlagStatus
FLASH_OB_GetRDP(void);
116 static uint8_t FLASH_OB_GetUser(void);
122 /** @defgroup FLASHEx_Extern_Functions FLASHEx Extern Functions
125 /* Private functions ---------------------------------------------------------*/
126 extern HAL_StatusTypeDef
FLASH_WaitForLastOperation(uint32_t Timeout
);
132 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
136 /** @defgroup FLASHEx_Exported_Functions_Group2 Extended I/O operation functions
137 * @brief Extended I/O operation functions
140 ===============================================================================
141 ##### IO operation functions #####
142 ===============================================================================
148 * @brief Perform a mass erase or erase the specified FLASH memory pages
149 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
150 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
151 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
152 * contains the configuration information for the erasing.
154 * @param[out] PageError: pointer to variable that
155 * contains the configuration information on faulty page in case of error
156 * (0xFFFFFFFF means that all the pages have been correctly erased)
158 * @retval HAL_StatusTypeDef HAL Status
160 HAL_StatusTypeDef
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef
*pEraseInit
, uint32_t *PageError
)
162 HAL_StatusTypeDef status
= HAL_ERROR
;
163 uint32_t address
= 0;
168 /* Check the parameters */
169 assert_param(IS_TYPEERASE(pEraseInit
->TypeErase
));
171 /* Wait for last operation to be completed */
172 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
174 if (status
== HAL_OK
)
176 if (pEraseInit
->TypeErase
== TYPEERASE_MASSERASE
)
178 /*Mass erase to be done*/
181 /* Wait for last operation to be completed */
182 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
184 /* If the erase operation is completed, disable the MER Bit */
185 CLEAR_BIT(FLASH
->CR
, FLASH_CR_MER
);
189 /* Check the parameters */
190 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
));
191 assert_param(IS_FLASH_NB_PAGES(pEraseInit
->PageAddress
, pEraseInit
->NbPages
));
193 /*Initialization of PageError variable*/
194 *PageError
= 0xFFFFFFFF;
196 /* Erase by page by page to be done*/
197 for(address
= pEraseInit
->PageAddress
;
198 address
< (pEraseInit
->PageAddress
+ (pEraseInit
->NbPages
)*FLASH_PAGE_SIZE
);
199 address
+= FLASH_PAGE_SIZE
)
201 FLASH_PageErase(address
);
203 /* Wait for last operation to be completed */
204 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
206 /* If the erase operation is completed, disable the PER Bit */
207 CLEAR_BIT(FLASH
->CR
, FLASH_CR_PER
);
209 if (status
!= HAL_OK
)
211 /* In case of error, stop erase procedure and return the faulty address */
212 *PageError
= address
;
219 /* Process Unlocked */
220 __HAL_UNLOCK(&pFlash
);
226 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
227 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
228 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
229 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
230 * contains the configuration information for the erasing.
232 * @retval HAL_StatusTypeDef HAL Status
234 HAL_StatusTypeDef
HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef
*pEraseInit
)
236 HAL_StatusTypeDef status
= HAL_OK
;
241 /* If procedure already ongoing, reject the next one */
242 if (pFlash
.ProcedureOnGoing
!= FLASH_PROC_NONE
)
247 /* Check the parameters */
248 assert_param(IS_TYPEERASE(pEraseInit
->TypeErase
));
250 /* Enable End of FLASH Operation and Error source interrupts */
251 __HAL_FLASH_ENABLE_IT((FLASH_IT_EOP
| FLASH_IT_ERR
));
253 if (pEraseInit
->TypeErase
== TYPEERASE_MASSERASE
)
255 /*Mass erase to be done*/
256 pFlash
.ProcedureOnGoing
= FLASH_PROC_MASSERASE
;
261 /* Erase by page to be done*/
263 /* Check the parameters */
264 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
));
265 assert_param(IS_FLASH_NB_PAGES(pEraseInit
->PageAddress
, pEraseInit
->NbPages
));
267 pFlash
.ProcedureOnGoing
= FLASH_PROC_PAGEERASE
;
268 pFlash
.DataRemaining
= pEraseInit
->NbPages
;
269 pFlash
.Address
= pEraseInit
->PageAddress
;
271 /*Erase 1st page and wait for IT*/
272 FLASH_PageErase(pEraseInit
->PageAddress
);
282 /** @defgroup FLASHEx_Exported_Functions_Group3 Extended Peripheral Control functions
283 * @brief Extended Peripheral Control functions
286 ===============================================================================
287 ##### Peripheral Control functions #####
288 ===============================================================================
290 This subsection provides a set of functions allowing to control the FLASH
297 * @brief Erases the FLASH option bytes.
298 * @note This functions erases all option bytes except the Read protection (RDP).
299 * The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
300 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
301 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
302 * (system reset will occur)
306 HAL_StatusTypeDef
HAL_FLASHEx_OBErase(void)
308 uint8_t rdptmp
= OB_RDP_LEVEL_0
;
309 HAL_StatusTypeDef status
= HAL_ERROR
;
310 FLASH_OBProgramInitTypeDef optionsbytes
;
312 /* Get the actual read protection Option Byte value */
313 HAL_FLASHEx_OBGetConfig(&optionsbytes
);
314 if(optionsbytes
.RDPLevel
!= RESET
)
316 rdptmp
= OB_RDP_LEVEL_1
;
319 /* Wait for last operation to be completed */
320 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
324 /* Clean the error context */
325 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
327 /* If the previous operation is completed, proceed to erase the option bytes */
328 SET_BIT(FLASH
->CR
, FLASH_CR_OPTER
);
329 SET_BIT(FLASH
->CR
, FLASH_CR_STRT
);
331 /* Wait for last operation to be completed */
332 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
334 /* If the erase operation is completed, disable the OPTER Bit */
335 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTER
);
339 /* Restore the last read protection Option Byte value */
340 optionsbytes
.OptionType
= OPTIONBYTE_RDP
;
341 optionsbytes
.RDPLevel
= rdptmp
;
342 status
= HAL_FLASHEx_OBProgram(&optionsbytes
);
346 /* Return the erase status */
351 * @brief Program option bytes
352 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
353 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
354 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
355 * (system reset will occur)
357 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
358 * contains the configuration information for the programming.
360 * @retval HAL_StatusTypeDef HAL Status
362 HAL_StatusTypeDef
HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef
*pOBInit
)
364 HAL_StatusTypeDef status
= HAL_ERROR
;
366 /* Check the parameters */
367 assert_param(IS_OPTIONBYTE(pOBInit
->OptionType
));
369 /* Write protection configuration */
370 if((pOBInit
->OptionType
& OPTIONBYTE_WRP
) == OPTIONBYTE_WRP
)
372 assert_param(IS_WRPSTATE(pOBInit
->WRPState
));
373 if (pOBInit
->WRPState
== WRPSTATE_ENABLE
)
375 /* Enable of Write protection on the selected page */
376 status
= FLASH_OB_EnableWRP(pOBInit
->WRPPage
);
380 /* Disable of Write protection on the selected page */
381 status
= FLASH_OB_DisableWRP(pOBInit
->WRPPage
);
385 /* Read protection configuration */
386 if((pOBInit
->OptionType
& OPTIONBYTE_RDP
) == OPTIONBYTE_RDP
)
388 status
= FLASH_OB_RDP_LevelConfig(pOBInit
->RDPLevel
);
391 /* USER configuration */
392 if((pOBInit
->OptionType
& OPTIONBYTE_USER
) == OPTIONBYTE_USER
)
394 status
= FLASH_OB_UserConfig(pOBInit
->USERConfig
);
397 /* DATA configuration*/
398 if((pOBInit
->OptionType
& OPTIONBYTE_DATA
) == OPTIONBYTE_DATA
)
400 status
= FLASH_OB_ProgramData(pOBInit
->DATAAddress
, pOBInit
->DATAData
);
407 * @brief Get the Option byte configuration
408 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
409 * contains the configuration information for the programming.
413 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef
*pOBInit
)
415 pOBInit
->OptionType
= OPTIONBYTE_WRP
| OPTIONBYTE_RDP
| OPTIONBYTE_USER
;
418 pOBInit
->WRPPage
= FLASH_OB_GetWRP();
421 pOBInit
->RDPLevel
= FLASH_OB_GetRDP();
424 pOBInit
->USERConfig
= FLASH_OB_GetUser();
435 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
440 * @brief Mass erase of FLASH memory
443 static void FLASH_MassErase(void)
446 /* Clean the error context */
447 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
449 /* Proceed to erase all sectors */
450 SET_BIT(FLASH
->CR
, FLASH_CR_MER
);
451 SET_BIT(FLASH
->CR
, FLASH_CR_STRT
);
455 * @brief Enable the write protection of the desired pages
456 * @note When the memory read protection level is selected (RDP level = 1),
457 * it is not possible to program or erase the flash page i if CortexM4
458 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
460 * @param WriteProtectPage: specifies the page(s) to be write protected.
461 * The value of this parameter depend on device used within the same series
464 static HAL_StatusTypeDef
FLASH_OB_EnableWRP(uint32_t WriteProtectPage
)
466 HAL_StatusTypeDef status
= HAL_OK
;
467 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F070x6) || \
468 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
469 uint16_t WRP0_Data
= 0xFFFF;
470 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
471 uint16_t WRP1_Data
= 0xFFFF;
472 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
473 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
475 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
476 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
477 uint16_t WRP0_Data
= 0xFFFF, WRP1_Data
= 0xFFFF, WRP2_Data
= 0xFFFF, WRP3_Data
= 0xFFFF;
478 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
480 /* Check the parameters */
481 assert_param(IS_OB_WRP(WriteProtectPage
));
483 WriteProtectPage
= (uint32_t)(~WriteProtectPage
);
484 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F070x6) || \
485 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
486 WRP0_Data
= (uint16_t)(WriteProtectPage
& OB_WRP_PAGES0TO31MASK
);
487 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
488 WRP1_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO63MASK
) >> 8);
489 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
490 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
492 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
493 defined(STM32F091xC) || defined(STM32F098xx)|| defined(STM32F030xC)
494 WRP0_Data
= (uint16_t)(WriteProtectPage
& OB_WRP_PAGES0TO15MASK
);
495 WRP1_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES16TO31MASK
) >> 8);
496 WRP2_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO47MASK
) >> 16);
497 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)
498 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO63MASK
) >> 24);
499 #endif /* STM32F071xB || STM32F072xB || STM32F078xx */
500 #if defined(STM32F091xC) || defined(STM32F098xx)
501 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO127MASK
) >> 24);
502 #endif /* STM32F091xC || STM32F098xx */
503 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
505 /* Wait for last operation to be completed */
506 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
510 /* Clean the error context */
511 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
513 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
515 if(WRP0_Data
!= 0xFF)
517 OB
->WRP0
&= WRP0_Data
;
519 /* Wait for last operation to be completed */
520 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
523 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx) || \
524 defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
525 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
526 if((status
== HAL_OK
) && (WRP1_Data
!= 0xFF))
528 OB
->WRP1
&= WRP1_Data
;
530 /* Wait for last operation to be completed */
531 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
533 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx || STM32F071xB || STM32F070xB ||
534 STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */
536 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)|| defined(STM32F070xB) || \
537 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
538 if((status
== HAL_OK
) && (WRP2_Data
!= 0xFF))
540 OB
->WRP2
&= WRP2_Data
;
542 /* Wait for last operation to be completed */
543 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
546 if((status
== HAL_OK
) && (WRP3_Data
!= 0xFF))
548 OB
->WRP3
&= WRP3_Data
;
550 /* Wait for last operation to be completed */
551 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
553 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC*/
555 /* if the program operation is completed, disable the OPTPG Bit */
556 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
563 * @brief Disable the write protection of the desired pages
564 * @note When the memory read protection level is selected (RDP level = 1),
565 * it is not possible to program or erase the flash page i if CortexM4
566 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
568 * @param WriteProtectPage: specifies the page(s) to be write unprotected.
569 * The value of this parameter depend on device used within the same series
572 static HAL_StatusTypeDef
FLASH_OB_DisableWRP(uint32_t WriteProtectPage
)
574 HAL_StatusTypeDef status
= HAL_OK
;
575 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || defined(STM32F070x6) || \
576 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
577 uint16_t WRP0_Data
= 0xFFFF;
578 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
579 uint16_t WRP1_Data
= 0xFFFF;
580 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
581 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
583 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
584 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
585 uint16_t WRP0_Data
= 0xFFFF, WRP1_Data
= 0xFFFF, WRP2_Data
= 0xFFFF, WRP3_Data
= 0xFFFF;
586 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
588 /* Check the parameters */
589 assert_param(IS_OB_WRP(WriteProtectPage
));
591 #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) || STM32F070x6 || \
592 defined(STM32F051x8) || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F058xx)
593 WRP0_Data
= (uint16_t)(WriteProtectPage
& OB_WRP_PAGES0TO31MASK
);
594 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx)
595 WRP1_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO63MASK
) >> 8);
596 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx */
597 #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */
599 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
600 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
601 WRP0_Data
= (uint16_t)(WriteProtectPage
& OB_WRP_PAGES0TO15MASK
);
602 WRP1_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES16TO31MASK
) >> 8);
603 WRP2_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO47MASK
) >> 16);
604 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)
605 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO63MASK
) >> 24);
606 #endif /* STM32F071xB || STM32F072xB || STM32F078xx */
607 #if defined(STM32F091xC) || defined(STM32F098xx)
608 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO127MASK
) >> 24);
609 #endif /* STM32F091xC || STM32F098xx */
610 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC || STM32F070xB */
612 /* Wait for last operation to be completed */
613 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
617 /* Clean the error context */
618 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
620 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
622 if(WRP0_Data
!= 0xFF)
624 OB
->WRP0
|= WRP0_Data
;
626 /* Wait for last operation to be completed */
627 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
630 #if defined(STM32F030x8) || defined(STM32F051x8) || defined(STM32F058xx) || \
631 defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
632 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
633 if((status
== HAL_OK
) && (WRP1_Data
!= 0xFF))
635 OB
->WRP1
|= WRP1_Data
;
637 /* Wait for last operation to be completed */
638 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
640 #endif /* STM32F030x8 || STM32F051x8 || STM32F058xx || STM32F071xB || STM32F070xB ||
641 STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */
643 #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || \
644 defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
645 if((status
== HAL_OK
) && (WRP2_Data
!= 0xFF))
647 OB
->WRP2
|= WRP2_Data
;
649 /* Wait for last operation to be completed */
650 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
653 if((status
== HAL_OK
) && (WRP3_Data
!= 0xFF))
655 OB
->WRP3
|= WRP3_Data
;
657 /* Wait for last operation to be completed */
658 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
660 #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F070xB || STM32F030xC */
662 /* if the program operation is completed, disable the OPTPG Bit */
663 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
669 * @brief Set the read protection level.
670 * @param ReadProtectLevel: specifies the read protection level.
671 * This parameter can be one of the following values:
672 * @arg OB_RDP_LEVEL_0: No protection
673 * @arg OB_RDP_LEVEL_1: Read protection of the memory
674 * @arg OB_RDP_LEVEL_2: Full chip protection
676 * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
680 static HAL_StatusTypeDef
FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel
)
682 HAL_StatusTypeDef status
= HAL_OK
;
684 /* Check the parameters */
685 assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel
));
687 /* Wait for last operation to be completed */
688 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
692 /* Clean the error context */
693 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
695 /* Enable the Option Bytes Programming operation */
696 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
698 WRITE_REG(OB
->RDP
, ReadProtectLevel
);
700 /* Wait for last operation to be completed */
701 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
703 /* if the program operation is completed, disable the OPTPG Bit */
704 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
711 * @brief Program the FLASH User Option Byte.
712 * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
713 * @param UserConfig: The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
714 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
717 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint8_t UserConfig
)
719 HAL_StatusTypeDef status
= HAL_OK
;
721 /* Check the parameters */
722 assert_param(IS_OB_WDG_SOURCE((UserConfig
&OB_WDG_SW
)));
723 assert_param(IS_OB_STOP_SOURCE((UserConfig
&OB_STOP_NO_RST
)));
724 assert_param(IS_OB_STDBY_SOURCE((UserConfig
&OB_STDBY_NO_RST
)));
725 assert_param(IS_OB_BOOT1((UserConfig
&OB_BOOT1_SET
)));
726 assert_param(IS_OB_VDDA_ANALOG((UserConfig
&OB_VDDA_ANALOG_ON
)));
727 assert_param(IS_OB_SRAM_PARITY((UserConfig
&OB_RAM_PARITY_CHECK_RESET
)));
728 #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)|| defined(STM32F070x6)
729 assert_param(IS_OB_BOOT_SEL((UserConfig
&OB_BOOT_SEL_SET
)));
730 assert_param(IS_OB_BOOT0((UserConfig
&OB_BOOT0_SET
)));
731 #endif /* STM32F042x6 || STM32F048xx || STM32F091xC || STM32F098xx || STM32F030xC || STM32F070x6 */
733 /* Wait for last operation to be completed */
734 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
738 /* Clean the error context */
739 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
741 /* Enable the Option Bytes Programming operation */
742 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
744 #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)|| defined(STM32F070x6)
745 OB
->USER
= UserConfig
;
747 OB
->USER
= (UserConfig
| 0x88);
748 #endif /* STM32F042x6 || STM32F048xx || STM32F091xC || STM32F098xx || STM32F030xC || STM32F070x6 */
750 /* Wait for last operation to be completed */
751 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
753 /* if the program operation is completed, disable the OPTPG Bit */
754 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
761 * @brief Programs a half word at a specified Option Byte Data address.
762 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
763 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
764 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
765 * (system reset will occur)
766 * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
767 * @param Address: specifies the address to be programmed.
768 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
769 * @param Data: specifies the data to be programmed.
772 static HAL_StatusTypeDef
FLASH_OB_ProgramData(uint32_t Address
, uint8_t Data
)
774 HAL_StatusTypeDef status
= HAL_ERROR
;
776 /* Check the parameters */
777 assert_param(IS_OB_DATA_ADDRESS(Address
));
779 /* Wait for last operation to be completed */
780 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
784 /* Clean the error context */
785 pFlash
.ErrorCode
= FLASH_ERROR_NONE
;
787 /* Enables the Option Bytes Programming operation */
788 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
789 *(__IO
uint16_t*)Address
= Data
;
791 /* Wait for last operation to be completed */
792 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
794 /* If the program operation is completed, disable the OPTPG Bit */
795 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
797 /* Return the Option Byte Data Program Status */
802 * @brief Return the FLASH Write Protection Option Bytes value.
803 * @retval The FLASH Write Protection Option Bytes value
805 static uint32_t FLASH_OB_GetWRP(void)
807 /* Return the FLASH write protection Register value */
808 return (uint32_t)(READ_REG(FLASH
->WRPR
));
812 * @brief Returns the FLASH Read Protection level.
813 * @retval FLASH ReadOut Protection Status:
814 * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
815 * - RESET, when OB_RDP_Level_0 is set
817 static FlagStatus
FLASH_OB_GetRDP(void)
819 FlagStatus readstatus
= RESET
;
821 if ((uint8_t)READ_BIT(FLASH
->OBR
, FLASH_OBR_RDPRT1
) != RESET
)
830 * @brief Return the FLASH User Option Byte value.
831 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
832 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
834 static uint8_t FLASH_OB_GetUser(void)
836 /* Return the User Option Byte */
837 return (uint8_t)(READ_REG(FLASH
->OBR
) >> 8);
844 #endif /* HAL_FLASH_MODULE_ENABLED */
854 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/