2 ******************************************************************************
3 * @file stm32f3xx_hal_flash_ex.c
4 * @author MCD Application Team
7 * @brief Extended FLASH HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the FLASH peripheral:
11 * + Extended Initialization/de-initialization functions
12 * + Extended I/O operation functions
13 * + Extended Peripheral Control functions
14 * + Extended Peripheral State functions
17 ==============================================================================
18 ##### Flash peripheral extended features #####
19 ==============================================================================
21 ##### How to use this driver #####
22 ==============================================================================
23 [..] This driver provides functions to configure and program the FLASH memory
24 of all STM32F3xxx devices. It includes
26 (++) Set/Reset the write protection
27 (++) Program the user Option Bytes
28 (++) Get the Read protection Level
31 ******************************************************************************
34 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
36 * Redistribution and use in source and binary forms, with or without modification,
37 * are permitted provided that the following conditions are met:
38 * 1. Redistributions of source code must retain the above copyright notice,
39 * this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright notice,
41 * this list of conditions and the following disclaimer in the documentation
42 * and/or other materials provided with the distribution.
43 * 3. Neither the name of STMicroelectronics nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 ******************************************************************************
61 /* Includes ------------------------------------------------------------------*/
62 #include "stm32f3xx_hal.h"
64 /** @addtogroup STM32F3xx_HAL_Driver
68 /** @addtogroup FLASHEx FLASH Extended HAL module driver
69 * @brief FLASH Extended HAL module driver
73 #ifdef HAL_FLASH_MODULE_ENABLED
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 /** @defgroup FLASHEx_Private_Defines FLASH Extended Private Define
80 #define HAL_FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
85 /* Private macro -------------------------------------------------------------*/
86 /* Private variables ---------------------------------------------------------*/
87 /** @defgroup FLASHEx_Private_Variables FLASH Extended Private Variables
90 /* Variables used for Erase pages under interruption*/
91 extern FLASH_ProcessTypeDef pFlash
;
96 /* Private function prototypes -----------------------------------------------*/
97 /** @defgroup FLASHEx_Private_Functions FLASH Extended Private Functions
100 /* Erase operations */
101 static void FLASH_MassErase(void);
103 /* Option bytes control */
104 static HAL_StatusTypeDef
FLASH_OB_EnableWRP(uint32_t WriteProtectPage
);
105 static HAL_StatusTypeDef
FLASH_OB_DisableWRP(uint32_t WriteProtectPage
);
106 static HAL_StatusTypeDef
FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel
);
107 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint8_t UserConfig
);
108 static HAL_StatusTypeDef
FLASH_OB_ProgramData(uint32_t Address
, uint8_t Data
);
109 static uint32_t FLASH_OB_GetWRP(void);
110 static FlagStatus
FLASH_OB_GetRDP(void);
111 static uint8_t FLASH_OB_GetUser(void);
116 /* Exported functions ---------------------------------------------------------*/
117 /** @defgroup FLASHEx_Exported_Functions FLASH Extended Exported Functions
121 /** @defgroup FLASHEx_Exported_Functions_Group1 Extended Input and Output operation functions
122 * @brief I/O operation functions
125 ===============================================================================
126 ##### IO operation functions #####
127 ===============================================================================
133 * @brief Perform a mass erase or erase the specified FLASH memory pages
134 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
135 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
136 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
137 * contains the configuration information for the erasing.
139 * @param[out] PageError: pointer to variable that
140 * contains the configuration information on faulty page in case of error
141 * (0xFFFFFFFF means that all the pages have been correctly erased)
143 * @retval HAL_StatusTypeDef HAL Status
145 HAL_StatusTypeDef
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef
*pEraseInit
, uint32_t *PageError
)
147 HAL_StatusTypeDef status
= HAL_ERROR
;
148 uint32_t address
= 0;
153 /* Check the parameters */
154 assert_param(IS_TYPEERASE(pEraseInit
->TypeErase
));
156 /* Wait for last operation to be completed */
157 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
159 if (status
== HAL_OK
)
161 if (pEraseInit
->TypeErase
== TYPEERASE_MASSERASE
)
163 /*Mass erase to be done*/
166 /* Wait for last operation to be completed */
167 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
169 /* Check FLASH End of Operation flag */
170 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP
))
172 /* Clear FLASH End of Operation pending bit */
173 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
);
176 /* If the erase operation is completed, disable the MER Bit */
177 CLEAR_BIT(FLASH
->CR
, FLASH_CR_MER
);
181 /* Check the parameters */
182 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
));
183 assert_param(IS_FLASH_NB_PAGES(pEraseInit
->PageAddress
, pEraseInit
->NbPages
));
185 /*Initialization of PageError variable*/
186 *PageError
= 0xFFFFFFFF;
188 /* Erase by page by page to be done*/
189 for(address
= pEraseInit
->PageAddress
;
190 address
< (pEraseInit
->PageAddress
+ (pEraseInit
->NbPages
)*FLASH_PAGE_SIZE
);
191 address
+= FLASH_PAGE_SIZE
)
193 FLASH_PageErase(address
);
195 /* Wait for last operation to be completed */
196 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
198 /* Check FLASH End of Operation flag */
199 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP
))
201 /* Clear FLASH End of Operation pending bit */
202 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
);
205 /* If the erase operation is completed, disable the PER Bit */
206 CLEAR_BIT(FLASH
->CR
, FLASH_CR_PER
);
208 if (status
!= HAL_OK
)
210 /* In case of error, stop erase procedure and return the faulty address */
211 *PageError
= address
;
218 /* Process Unlocked */
219 __HAL_UNLOCK(&pFlash
);
225 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
226 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
227 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
228 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
229 * contains the configuration information for the erasing.
231 * @retval HAL_StatusTypeDef HAL Status
233 HAL_StatusTypeDef
HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef
*pEraseInit
)
235 HAL_StatusTypeDef status
= HAL_OK
;
240 /* Check the parameters */
241 assert_param(IS_TYPEERASE(pEraseInit
->TypeErase
));
243 /* Enable End of FLASH Operation and Error source interrupts */
244 __HAL_FLASH_ENABLE_IT((FLASH_IT_EOP
| FLASH_IT_ERR
));
246 if (pEraseInit
->TypeErase
== TYPEERASE_MASSERASE
)
248 /*Mass erase to be done*/
249 pFlash
.ProcedureOnGoing
= FLASH_PROC_MASSERASE
;
254 /* Erase by page to be done*/
256 /* Check the parameters */
257 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit
->PageAddress
));
258 assert_param(IS_FLASH_NB_PAGES(pEraseInit
->PageAddress
, pEraseInit
->NbPages
));
260 pFlash
.ProcedureOnGoing
= FLASH_PROC_PAGEERASE
;
261 pFlash
.DataRemaining
= pEraseInit
->NbPages
;
262 pFlash
.Address
= pEraseInit
->PageAddress
;
264 /*Erase 1st page and wait for IT*/
265 FLASH_PageErase(pEraseInit
->PageAddress
);
275 /** @defgroup FLASHEx_Exported_Functions_Group2 Extended Peripheral Control functions
276 * @brief Peripheral Control functions
279 ===============================================================================
280 ##### Peripheral Control functions #####
281 ===============================================================================
283 This subsection provides a set of functions allowing to control the FLASH
290 * @brief Erases the FLASH option bytes.
291 * @note This functions erases all option bytes except the Read protection (RDP).
292 * The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
293 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
294 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
295 * (system reset will occur)
299 HAL_StatusTypeDef
HAL_FLASHEx_OBErase(void)
301 uint8_t rdptmp
= OB_RDP_LEVEL_0
;
302 HAL_StatusTypeDef status
= HAL_ERROR
;
303 FLASH_OBProgramInitTypeDef optionsbytes
;
305 /* Get the actual read protection Option Byte value */
306 HAL_FLASHEx_OBGetConfig(&optionsbytes
);
307 if(optionsbytes
.RDPLevel
!= RESET
)
309 rdptmp
= OB_RDP_LEVEL_1
;
312 /* Wait for last operation to be completed */
313 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
315 /* Clear pending flags (if any) */
316 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
320 /* If the previous operation is completed, proceed to erase the option bytes */
321 SET_BIT(FLASH
->CR
, FLASH_CR_OPTER
);
322 SET_BIT(FLASH
->CR
, FLASH_CR_STRT
);
324 /* Wait for last operation to be completed */
325 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
327 /* If the erase operation is completed, disable the OPTER Bit */
328 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTER
);
332 /* Restore the last read protection Option Byte value */
333 optionsbytes
.OptionType
= OPTIONBYTE_RDP
;
334 optionsbytes
.RDPLevel
= rdptmp
;
335 status
= HAL_FLASHEx_OBProgram(&optionsbytes
);
339 /* Return the erase status */
344 * @brief Program option bytes
345 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
346 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
347 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
348 * (system reset will occur)
350 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
351 * contains the configuration information for the programming.
353 * @retval HAL_StatusTypeDef HAL Status
355 HAL_StatusTypeDef
HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef
*pOBInit
)
357 HAL_StatusTypeDef status
= HAL_ERROR
;
359 /* Check the parameters */
360 assert_param(IS_OPTIONBYTE(pOBInit
->OptionType
));
362 /* Write protection configuration */
363 if((pOBInit
->OptionType
& OPTIONBYTE_WRP
) == OPTIONBYTE_WRP
)
365 assert_param(IS_WRPSTATE(pOBInit
->WRPState
));
366 if (pOBInit
->WRPState
== WRPSTATE_ENABLE
)
368 /* Enable of Write protection on the selected page */
369 status
= FLASH_OB_EnableWRP(pOBInit
->WRPPage
);
373 /* Disable of Write protection on the selected page */
374 status
= FLASH_OB_DisableWRP(pOBInit
->WRPPage
);
378 /* Read protection configuration */
379 if((pOBInit
->OptionType
& OPTIONBYTE_RDP
) == OPTIONBYTE_RDP
)
381 status
= FLASH_OB_RDP_LevelConfig(pOBInit
->RDPLevel
);
384 /* USER configuration */
385 if((pOBInit
->OptionType
& OPTIONBYTE_USER
) == OPTIONBYTE_USER
)
387 status
= FLASH_OB_UserConfig(pOBInit
->USERConfig
);
390 /* DATA configuration*/
391 if((pOBInit
->OptionType
& OPTIONBYTE_DATA
) == OPTIONBYTE_DATA
)
393 status
= FLASH_OB_ProgramData(pOBInit
->DATAAddress
, pOBInit
->DATAData
);
400 * @brief Get the Option byte configuration
401 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
402 * contains the configuration information for the programming.
406 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef
*pOBInit
)
408 pOBInit
->OptionType
= OPTIONBYTE_WRP
| OPTIONBYTE_RDP
| OPTIONBYTE_USER
;
411 pOBInit
->WRPPage
= FLASH_OB_GetWRP();
414 pOBInit
->RDPLevel
= FLASH_OB_GetRDP();
417 pOBInit
->USERConfig
= FLASH_OB_GetUser();
428 /** @addtogroup FLASHEx_Private_Functions FLASH Extended Private Functions
433 * @brief Mass erase of FLASH memory
437 static void FLASH_MassErase(void)
439 /* Clear pending flags (if any) */
440 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
442 /* Proceed to erase all sectors */
443 SET_BIT(FLASH
->CR
, FLASH_CR_MER
);
444 SET_BIT(FLASH
->CR
, FLASH_CR_STRT
);
452 * @brief Enable the write protection of the desired pages
453 * @note When the memory read protection level is selected (RDP level = 1),
454 * it is not possible to program or erase the flash page i if CortexM4
455 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
457 * @param WriteProtectPage: specifies the page(s) to be write protected.
458 * The value of this parameter depend on device used within the same series
461 static HAL_StatusTypeDef
FLASH_OB_EnableWRP(uint32_t WriteProtectPage
)
463 HAL_StatusTypeDef status
= HAL_OK
;
464 uint16_t WRP0_Data
= 0xFFFF, WRP1_Data
= 0xFFFF;
465 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
466 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
467 defined(STM32F373xC) || defined(STM32F378xx)
468 uint16_t WRP2_Data
= 0xFFFF, WRP3_Data
= 0xFFFF;
469 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
470 /* STM32F302xC || STM32F303xC || STM32F358xx || */
471 /* STM32F373xC || STM32F378xx */
473 /* Check the parameters */
474 assert_param(IS_OB_WRP(WriteProtectPage
));
476 WriteProtectPage
= (uint32_t)(~WriteProtectPage
);
477 WRP0_Data
= (uint16_t)(WriteProtectPage
& OB_WRP_PAGES0TO15MASK
);
478 WRP1_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES16TO31MASK
) >> 8);
479 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
480 defined(STM32F373xC) || defined(STM32F378xx)
481 WRP2_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO47MASK
) >> 16);
482 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO127MASK
) >> 24);
483 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
484 /* STM32F373xC || STM32F378xx */
486 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
487 WRP2_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO47MASK
) >> 16);
488 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO255MASK
) >> 24);
489 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
491 /* Wait for last operation to be completed */
492 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
494 /* Clear pending flags (if any) */
495 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
499 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
501 if(WRP0_Data
!= 0xFF)
503 OB
->WRP0
&= WRP0_Data
;
505 /* Wait for last operation to be completed */
506 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
509 if((status
== HAL_OK
) && (WRP1_Data
!= 0xFF))
511 OB
->WRP1
&= WRP1_Data
;
513 /* Wait for last operation to be completed */
514 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
517 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
518 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
519 defined(STM32F373xC) || defined(STM32F378xx)
520 if((status
== HAL_OK
) && (WRP2_Data
!= 0xFF))
522 OB
->WRP2
&= WRP2_Data
;
524 /* Wait for last operation to be completed */
525 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
528 if((status
== HAL_OK
) && (WRP3_Data
!= 0xFF))
530 OB
->WRP3
&= WRP3_Data
;
532 /* Wait for last operation to be completed */
533 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
535 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
536 /* STM32F302xC || STM32F303xC || STM32F358xx || */
537 /* STM32F373xC || STM32F378xx */
539 /* if the program operation is completed, disable the OPTPG Bit */
540 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
548 * @brief Disable the write protection of the desired pages
549 * @note When the memory read protection level is selected (RDP level = 1),
550 * it is not possible to program or erase the flash page i if CortexM4
551 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
553 * @param WriteProtectPage: specifies the page(s) to be write unprotected.
554 * The value of this parameter depend on device used within the same series
557 static HAL_StatusTypeDef
FLASH_OB_DisableWRP(uint32_t WriteProtectPage
)
559 HAL_StatusTypeDef status
= HAL_OK
;
560 uint16_t WRP0_Data
= 0xFFFF, WRP1_Data
= 0xFFFF;
561 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
562 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
563 defined(STM32F373xC) || defined(STM32F378xx)
564 uint16_t WRP2_Data
= 0xFFFF, WRP3_Data
= 0xFFFF;
565 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
566 /* STM32F302xC || STM32F303xC || STM32F358xx || */
567 /* STM32F373xC || STM32F378xx */
569 /* Check the parameters */
570 assert_param(IS_OB_WRP(WriteProtectPage
));
572 WRP0_Data
= (uint16_t)(WriteProtectPage
& OB_WRP_PAGES0TO15MASK
);
573 WRP1_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES16TO31MASK
) >> 8);
574 #if defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
575 defined(STM32F373xC) || defined(STM32F378xx)
576 WRP2_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO47MASK
) >> 16);
577 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO127MASK
) >> 24);
578 #endif /* STM32F302xC || STM32F303xC || STM32F358xx || */
579 /* STM32F373xC || STM32F378xx */
581 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
582 WRP2_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES32TO47MASK
) >> 16);
583 WRP3_Data
= (uint16_t)((WriteProtectPage
& OB_WRP_PAGES48TO255MASK
) >> 24);
584 #endif /* STM32F303xE || STM32F303xE || STM32F398xx */
586 /* Wait for last operation to be completed */
587 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
589 /* Clear pending flags (if any) */
590 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
594 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
596 if(WRP0_Data
!= 0xFF)
598 OB
->WRP0
|= WRP0_Data
;
600 /* Wait for last operation to be completed */
601 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
604 if((status
== HAL_OK
) && (WRP1_Data
!= 0xFF))
606 OB
->WRP1
|= WRP1_Data
;
608 /* Wait for last operation to be completed */
609 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
612 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
613 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
614 defined(STM32F373xC) || defined(STM32F378xx)
615 if((status
== HAL_OK
) && (WRP2_Data
!= 0xFF))
617 OB
->WRP2
|= WRP2_Data
;
619 /* Wait for last operation to be completed */
620 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
623 if((status
== HAL_OK
) && (WRP3_Data
!= 0xFF))
625 OB
->WRP3
|= WRP3_Data
;
627 /* Wait for last operation to be completed */
628 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
630 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
631 /* STM32F302xC || STM32F303xC || STM32F358xx || */
632 /* STM32F373xC || STM32F378xx */
634 /* if the program operation is completed, disable the OPTPG Bit */
635 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
642 * @brief Set the read protection level.
643 * @param ReadProtectLevel: specifies the read protection level.
644 * This parameter can be one of the following values:
645 * @arg OB_RDP_LEVEL_0: No protection
646 * @arg OB_RDP_LEVEL_1: Read protection of the memory
647 * @arg OB_RDP_LEVEL_2: Full chip protection
649 * @note Warning: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
653 static HAL_StatusTypeDef
FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel
)
655 HAL_StatusTypeDef status
= HAL_OK
;
657 /* Check the parameters */
658 assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel
));
660 /* Wait for last operation to be completed */
661 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
663 /* Clear pending flags (if any) */
664 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
668 /* Enable the Option Bytes Programming operation */
669 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
671 WRITE_REG(OB
->RDP
, ReadProtectLevel
);
673 /* Wait for last operation to be completed */
674 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
676 /* if the program operation is completed, disable the OPTPG Bit */
677 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
684 * @brief Program the FLASH User Option Byte.
685 * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
686 * @param UserConfig: The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
687 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
688 * And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 .
691 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint8_t UserConfig
)
693 HAL_StatusTypeDef status
= HAL_OK
;
695 /* Check the parameters */
696 assert_param(IS_OB_IWDG_SOURCE((UserConfig
&OB_IWDG_SW
)));
697 assert_param(IS_OB_STOP_SOURCE((UserConfig
&OB_STOP_NO_RST
)));
698 assert_param(IS_OB_STDBY_SOURCE((UserConfig
&OB_STDBY_NO_RST
)));
699 assert_param(IS_OB_BOOT1((UserConfig
&OB_BOOT1_SET
)));
700 assert_param(IS_OB_VDDA_ANALOG((UserConfig
&OB_VDDA_ANALOG_ON
)));
701 assert_param(IS_OB_SRAM_PARITY((UserConfig
&OB_SRAM_PARITY_RESET
)));
702 #if defined(STM32F373xC) || defined(STM32F378xx)
703 assert_param(IS_OB_SDACD_VDD_MONITOR((UserConfig
&OB_SDACD_VDD_MONITOR_SET
)));
704 #endif /* STM32F373xC || STM32F378xx */
706 /* Wait for last operation to be completed */
707 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
709 /* Clear pending flags (if any) */
710 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
714 /* Enable the Option Bytes Programming operation */
715 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
717 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
718 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
719 defined(STM32F303x8) || defined(STM32F334x8) || \
720 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
721 OB
->USER
= (UserConfig
| 0x88);
722 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
723 /* STM32F302xC || STM32F303xC || STM32F358xx || */
724 /* STM32F303x8 || STM32F334x8 || */
725 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
727 #if defined(STM32F373xC) || defined(STM32F378xx)
728 OB
->USER
= (UserConfig
| 0x08);
729 #endif /* STM32F373xC || STM32F378xx */
731 /* Wait for last operation to be completed */
732 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
734 /* if the program operation is completed, disable the OPTPG Bit */
735 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
742 * @brief Programs a half word at a specified Option Byte Data address.
743 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
744 * The function HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes
745 * The function HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes
746 * (system reset will occur)
747 * Programming of the OB should be performed only after an erase (otherwise PGERR occurs)
748 * @param Address: specifies the address to be programmed.
749 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
750 * @param Data: specifies the data to be programmed.
753 static HAL_StatusTypeDef
FLASH_OB_ProgramData(uint32_t Address
, uint8_t Data
)
755 HAL_StatusTypeDef status
= HAL_ERROR
;
757 /* Check the parameters */
758 assert_param(IS_OB_DATA_ADDRESS(Address
));
760 /* Wait for last operation to be completed */
761 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
763 /* Clear pending flags (if any) */
764 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGERR
);
768 /* Enables the Option Bytes Programming operation */
769 SET_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
770 *(__IO
uint16_t*)Address
= Data
;
772 /* Wait for last operation to be completed */
773 status
= FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE
);
775 /* If the program operation is completed, disable the OPTPG Bit */
776 CLEAR_BIT(FLASH
->CR
, FLASH_CR_OPTPG
);
778 /* Return the Option Byte Data Program Status */
783 * @brief Return the FLASH Write Protection Option Bytes value.
784 * @retval The FLASH Write Protection Option Bytes value
786 static uint32_t FLASH_OB_GetWRP(void)
788 /* Return the FLASH write protection Register value */
789 return (uint32_t)(READ_REG(FLASH
->WRPR
));
793 * @brief Returns the FLASH Read Protection level.
794 * @retval FLASH ReadOut Protection Status:
795 * - SET, when OB_RDP_Level_1 or OB_RDP_Level_2 is set
796 * - RESET, when OB_RDP_Level_0 is set
798 static FlagStatus
FLASH_OB_GetRDP(void)
800 FlagStatus readstatus
= RESET
;
802 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
803 defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
804 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
805 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
806 if ((uint8_t)READ_BIT(FLASH
->OBR
, FLASH_OBR_RDPRT
) != RESET
)
807 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
808 /* STM32F302xC || STM32F303xC || STM32F358xx || */
809 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
810 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
812 #if defined(STM32F373xC) || defined(STM32F378xx)
813 if ((uint8_t)READ_BIT(FLASH
->OBR
, (FLASH_OBR_LEVEL1_PROT
| FLASH_OBR_LEVEL2_PROT
)) != RESET
)
814 #endif /* STM32F373xC || STM32F378xx */
826 * @brief Return the FLASH User Option Byte value.
827 * @retval The FLASH User Option Bytes values: IWDG_SW(Bit0), RST_STOP(Bit1), RST_STDBY(Bit2), BOOT1(Bit4),
828 * VDDA_Analog_Monitoring(Bit5) and SRAM_Parity_Enable(Bit6).
829 * And SDADC12_VDD_MONITOR(Bit7) for STM32F373 or STM32F378 .
831 static uint8_t FLASH_OB_GetUser(void)
833 /* Return the User Option Byte */
834 return (uint8_t)(READ_REG(FLASH
->OBR
) >> 8);
841 #endif /* HAL_FLASH_MODULE_ENABLED */
852 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/