2 ******************************************************************************
3 * @file stm32f7xx_hal_flash_ex.c
4 * @author MCD Application Team
7 * @brief Extended FLASH HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the FLASH extension peripheral:
10 * + Extended programming operations functions
13 ==============================================================================
14 ##### Flash Extension features #####
15 ==============================================================================
17 [..] Comparing to other previous devices, the FLASH interface for STM32F76xx/STM32F77xx
18 devices contains the following additional features
20 (+) Capacity up to 2 Mbyte with dual bank architecture supporting read-while-write
22 (+) Dual bank memory organization
25 ##### How to use this driver #####
26 ==============================================================================
27 [..] This driver provides functions to configure and program the FLASH memory
28 of all STM32F7xx devices. It includes
29 (#) FLASH Memory Erase functions:
30 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
31 HAL_FLASH_Lock() functions
32 (++) Erase function: Erase sector, erase all sectors
33 (++) There are two modes of erase :
34 (+++) Polling Mode using HAL_FLASHEx_Erase()
35 (+++) Interrupt Mode using HAL_FLASHEx_Erase_IT()
37 (#) Option Bytes Programming functions: Use HAL_FLASHEx_OBProgram() to :
38 (++) Set/Reset the write protection
39 (++) Set the Read protection Level
40 (++) Set the BOR level
41 (++) Program the user Option Bytes
44 ******************************************************************************
47 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
49 * Redistribution and use in source and binary forms, with or without modification,
50 * are permitted provided that the following conditions are met:
51 * 1. Redistributions of source code must retain the above copyright notice,
52 * this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright notice,
54 * this list of conditions and the following disclaimer in the documentation
55 * and/or other materials provided with the distribution.
56 * 3. Neither the name of STMicroelectronics nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
61 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
67 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
68 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 ******************************************************************************
74 /* Includes ------------------------------------------------------------------*/
75 #include "stm32f7xx_hal.h"
77 /** @addtogroup STM32F7xx_HAL_Driver
81 /** @defgroup FLASHEx FLASHEx
82 * @brief FLASH HAL Extension module driver
86 #ifdef HAL_FLASH_MODULE_ENABLED
88 /* Private typedef -----------------------------------------------------------*/
89 /* Private define ------------------------------------------------------------*/
90 /** @addtogroup FLASHEx_Private_Constants
93 #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
94 #define FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
99 /* Private macro -------------------------------------------------------------*/
100 /* Private variables ---------------------------------------------------------*/
101 /** @addtogroup FLASHEx_Private_Variables
104 extern FLASH_ProcessTypeDef pFlash
;
109 /* Private function prototypes -----------------------------------------------*/
110 /** @addtogroup FLASHEx_Private_Functions
113 /* Option bytes control */
114 static HAL_StatusTypeDef
FLASH_OB_EnableWRP(uint32_t WRPSector
);
115 static HAL_StatusTypeDef
FLASH_OB_DisableWRP(uint32_t WRPSector
);
116 static HAL_StatusTypeDef
FLASH_OB_RDP_LevelConfig(uint8_t Level
);
117 static HAL_StatusTypeDef
FLASH_OB_BOR_LevelConfig(uint8_t Level
);
118 static HAL_StatusTypeDef
FLASH_OB_BootAddressConfig(uint32_t BootOption
, uint32_t Address
);
119 static uint32_t FLASH_OB_GetUser(void);
120 static uint32_t FLASH_OB_GetWRP(void);
121 static uint8_t FLASH_OB_GetRDP(void);
122 static uint32_t FLASH_OB_GetBOR(void);
123 static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption
);
125 #if defined (FLASH_OPTCR_nDBANK)
126 static void FLASH_MassErase(uint8_t VoltageRange
, uint32_t Banks
);
127 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg
, uint32_t Iwdg
, uint32_t Stop
, uint32_t Stdby
, uint32_t Iwdgstop
, \
128 uint32_t Iwdgstdby
, uint32_t NDBank
, uint32_t NDBoot
);
130 static void FLASH_MassErase(uint8_t VoltageRange
);
131 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg
, uint32_t Iwdg
, uint32_t Stop
, uint32_t Stdby
, uint32_t Iwdgstop
, uint32_t Iwdgstdby
);
132 #endif /* FLASH_OPTCR_nDBANK */
134 extern HAL_StatusTypeDef
FLASH_WaitForLastOperation(uint32_t Timeout
);
139 /* Exported functions --------------------------------------------------------*/
140 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
144 /** @defgroup FLASHEx_Exported_Functions_Group1 Extended IO operation functions
145 * @brief Extended IO operation functions
148 ===============================================================================
149 ##### Extended programming operation functions #####
150 ===============================================================================
152 This subsection provides a set of functions allowing to manage the Extension FLASH
153 programming operations Operations.
159 * @brief Perform a mass erase or erase the specified FLASH memory sectors
160 * @param[in] pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
161 * contains the configuration information for the erasing.
163 * @param[out] SectorError: pointer to variable that
164 * contains the configuration information on faulty sector in case of error
165 * (0xFFFFFFFF means that all the sectors have been correctly erased)
169 HAL_StatusTypeDef
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef
*pEraseInit
, uint32_t *SectorError
)
171 HAL_StatusTypeDef status
= HAL_ERROR
;
177 /* Check the parameters */
178 assert_param(IS_FLASH_TYPEERASE(pEraseInit
->TypeErase
));
180 /* Wait for last operation to be completed */
181 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
185 /*Initialization of SectorError variable*/
186 *SectorError
= 0xFFFFFFFFU
;
188 if(pEraseInit
->TypeErase
== FLASH_TYPEERASE_MASSERASE
)
190 /*Mass erase to be done*/
191 #if defined (FLASH_OPTCR_nDBANK)
192 FLASH_MassErase((uint8_t) pEraseInit
->VoltageRange
, pEraseInit
->Banks
);
194 FLASH_MassErase((uint8_t) pEraseInit
->VoltageRange
);
195 #endif /* FLASH_OPTCR_nDBANK */
197 /* Wait for last operation to be completed */
198 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
200 /* if the erase operation is completed, disable the MER Bit */
201 FLASH
->CR
&= (~FLASH_MER_BIT
);
205 /* Check the parameters */
206 assert_param(IS_FLASH_NBSECTORS(pEraseInit
->NbSectors
+ pEraseInit
->Sector
));
208 /* Erase by sector by sector to be done*/
209 for(index
= pEraseInit
->Sector
; index
< (pEraseInit
->NbSectors
+ pEraseInit
->Sector
); index
++)
211 FLASH_Erase_Sector(index
, (uint8_t) pEraseInit
->VoltageRange
);
213 /* Wait for last operation to be completed */
214 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
216 /* If the erase operation is completed, disable the SER Bit and SNB Bits */
217 CLEAR_BIT(FLASH
->CR
, (FLASH_CR_SER
| FLASH_CR_SNB
));
221 /* In case of error, stop erase procedure and return the faulty sector*/
222 *SectorError
= index
;
229 /* Process Unlocked */
230 __HAL_UNLOCK(&pFlash
);
236 * @brief Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
237 * @param pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
238 * contains the configuration information for the erasing.
242 HAL_StatusTypeDef
HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef
*pEraseInit
)
244 HAL_StatusTypeDef status
= HAL_OK
;
249 /* Check the parameters */
250 assert_param(IS_FLASH_TYPEERASE(pEraseInit
->TypeErase
));
252 /* Enable End of FLASH Operation interrupt */
253 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP
);
255 /* Enable Error source interrupt */
256 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR
);
258 /* Clear pending flags (if any) */
259 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
|\
260 FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_ERSERR
);
262 if(pEraseInit
->TypeErase
== FLASH_TYPEERASE_MASSERASE
)
264 /*Mass erase to be done*/
265 pFlash
.ProcedureOnGoing
= FLASH_PROC_MASSERASE
;
266 #if defined (FLASH_OPTCR_nDBANK)
267 FLASH_MassErase((uint8_t) pEraseInit
->VoltageRange
, pEraseInit
->Banks
);
269 FLASH_MassErase((uint8_t) pEraseInit
->VoltageRange
);
270 #endif /* FLASH_OPTCR_nDBANK */
274 /* Erase by sector to be done*/
276 /* Check the parameters */
277 assert_param(IS_FLASH_NBSECTORS(pEraseInit
->NbSectors
+ pEraseInit
->Sector
));
279 pFlash
.ProcedureOnGoing
= FLASH_PROC_SECTERASE
;
280 pFlash
.NbSectorsToErase
= pEraseInit
->NbSectors
;
281 pFlash
.Sector
= pEraseInit
->Sector
;
282 pFlash
.VoltageForErase
= (uint8_t)pEraseInit
->VoltageRange
;
284 /*Erase 1st sector and wait for IT*/
285 FLASH_Erase_Sector(pEraseInit
->Sector
, pEraseInit
->VoltageRange
);
292 * @brief Program option bytes
293 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
294 * contains the configuration information for the programming.
298 HAL_StatusTypeDef
HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef
*pOBInit
)
300 HAL_StatusTypeDef status
= HAL_ERROR
;
305 /* Check the parameters */
306 assert_param(IS_OPTIONBYTE(pOBInit
->OptionType
));
308 /* Write protection configuration */
309 if((pOBInit
->OptionType
& OPTIONBYTE_WRP
) == OPTIONBYTE_WRP
)
311 assert_param(IS_WRPSTATE(pOBInit
->WRPState
));
312 if(pOBInit
->WRPState
== OB_WRPSTATE_ENABLE
)
314 /*Enable of Write protection on the selected Sector*/
315 status
= FLASH_OB_EnableWRP(pOBInit
->WRPSector
);
319 /*Disable of Write protection on the selected Sector*/
320 status
= FLASH_OB_DisableWRP(pOBInit
->WRPSector
);
324 /* Read protection configuration */
325 if((pOBInit
->OptionType
& OPTIONBYTE_RDP
) == OPTIONBYTE_RDP
)
327 status
= FLASH_OB_RDP_LevelConfig(pOBInit
->RDPLevel
);
330 /* USER configuration */
331 if((pOBInit
->OptionType
& OPTIONBYTE_USER
) == OPTIONBYTE_USER
)
333 #if defined (FLASH_OPTCR_nDBANK)
334 status
= FLASH_OB_UserConfig(pOBInit
->USERConfig
& OB_WWDG_SW
,
335 pOBInit
->USERConfig
& OB_IWDG_SW
,
336 pOBInit
->USERConfig
& OB_STOP_NO_RST
,
337 pOBInit
->USERConfig
& OB_STDBY_NO_RST
,
338 pOBInit
->USERConfig
& OB_IWDG_STOP_ACTIVE
,
339 pOBInit
->USERConfig
& OB_IWDG_STDBY_ACTIVE
,
340 pOBInit
->USERConfig
& OB_NDBANK_SINGLE_BANK
,
341 pOBInit
->USERConfig
& OB_DUAL_BOOT_DISABLE
);
343 status
= FLASH_OB_UserConfig(pOBInit
->USERConfig
& OB_WWDG_SW
,
344 pOBInit
->USERConfig
& OB_IWDG_SW
,
345 pOBInit
->USERConfig
& OB_STOP_NO_RST
,
346 pOBInit
->USERConfig
& OB_STDBY_NO_RST
,
347 pOBInit
->USERConfig
& OB_IWDG_STOP_ACTIVE
,
348 pOBInit
->USERConfig
& OB_IWDG_STDBY_ACTIVE
);
349 #endif /* FLASH_OPTCR_nDBANK */
352 /* BOR Level configuration */
353 if((pOBInit
->OptionType
& OPTIONBYTE_BOR
) == OPTIONBYTE_BOR
)
355 status
= FLASH_OB_BOR_LevelConfig(pOBInit
->BORLevel
);
358 /* Boot 0 Address configuration */
359 if((pOBInit
->OptionType
& OPTIONBYTE_BOOTADDR_0
) == OPTIONBYTE_BOOTADDR_0
)
361 status
= FLASH_OB_BootAddressConfig(OPTIONBYTE_BOOTADDR_0
, pOBInit
->BootAddr0
);
364 /* Boot 1 Address configuration */
365 if((pOBInit
->OptionType
& OPTIONBYTE_BOOTADDR_1
) == OPTIONBYTE_BOOTADDR_1
)
367 status
= FLASH_OB_BootAddressConfig(OPTIONBYTE_BOOTADDR_1
, pOBInit
->BootAddr1
);
370 /* Process Unlocked */
371 __HAL_UNLOCK(&pFlash
);
377 * @brief Get the Option byte configuration
378 * @param pOBInit: pointer to an FLASH_OBInitStruct structure that
379 * contains the configuration information for the programming.
383 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef
*pOBInit
)
385 pOBInit
->OptionType
= OPTIONBYTE_WRP
| OPTIONBYTE_RDP
| OPTIONBYTE_USER
|\
386 OPTIONBYTE_BOR
| OPTIONBYTE_BOOTADDR_0
| OPTIONBYTE_BOOTADDR_1
;
389 pOBInit
->WRPSector
= FLASH_OB_GetWRP();
392 pOBInit
->RDPLevel
= FLASH_OB_GetRDP();
395 pOBInit
->USERConfig
= FLASH_OB_GetUser();
398 pOBInit
->BORLevel
= FLASH_OB_GetBOR();
400 /*Get Boot Address when Boot pin = 0 */
401 pOBInit
->BootAddr0
= FLASH_OB_GetBootAddress(OPTIONBYTE_BOOTADDR_0
);
403 /*Get Boot Address when Boot pin = 1 */
404 pOBInit
->BootAddr1
= FLASH_OB_GetBootAddress(OPTIONBYTE_BOOTADDR_1
);
410 #if defined (FLASH_OPTCR_nDBANK)
412 * @brief Full erase of FLASH memory sectors
413 * @param VoltageRange: The device voltage range which defines the erase parallelism.
414 * This parameter can be one of the following values:
415 * @arg VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
416 * the operation will be done by byte (8-bit)
417 * @arg VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
418 * the operation will be done by half word (16-bit)
419 * @arg VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
420 * the operation will be done by word (32-bit)
421 * @arg VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
422 * the operation will be done by double word (64-bit)
423 * @param Banks: Banks to be erased
424 * This parameter can be one of the following values:
425 * @arg FLASH_BANK_1: Bank1 to be erased
426 * @arg FLASH_BANK_2: Bank2 to be erased
427 * @arg FLASH_BANK_BOTH: Bank1 and Bank2 to be erased
431 static void FLASH_MassErase(uint8_t VoltageRange
, uint32_t Banks
)
433 /* Check the parameters */
434 assert_param(IS_VOLTAGERANGE(VoltageRange
));
435 assert_param(IS_FLASH_BANK(Banks
));
437 /* if the previous operation is completed, proceed to erase all sectors */
438 FLASH
->CR
&= CR_PSIZE_MASK
;
439 if(Banks
== FLASH_BANK_BOTH
)
441 /* bank1 & bank2 will be erased*/
442 FLASH
->CR
|= FLASH_MER_BIT
;
444 else if(Banks
== FLASH_BANK_2
)
446 /*Only bank2 will be erased*/
447 FLASH
->CR
|= FLASH_CR_MER2
;
451 /*Only bank1 will be erased*/
452 FLASH
->CR
|= FLASH_CR_MER1
;
454 FLASH
->CR
|= FLASH_CR_STRT
| ((uint32_t)VoltageRange
<<8);
455 /* Data synchronous Barrier (DSB) Just after the write operation
456 This will force the CPU to respect the sequence of instruction (no optimization).*/
461 * @brief Erase the specified FLASH memory sector
462 * @param Sector: FLASH sector to erase
463 * The value of this parameter depend on device used within the same series
464 * @param VoltageRange: The device voltage range which defines the erase parallelism.
465 * This parameter can be one of the following values:
466 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
467 * the operation will be done by byte (8-bit)
468 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
469 * the operation will be done by half word (16-bit)
470 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
471 * the operation will be done by word (32-bit)
472 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
473 * the operation will be done by double word (64-bit)
477 void FLASH_Erase_Sector(uint32_t Sector
, uint8_t VoltageRange
)
479 uint32_t tmp_psize
= 0;
481 /* Check the parameters */
482 assert_param(IS_FLASH_SECTOR(Sector
));
483 assert_param(IS_VOLTAGERANGE(VoltageRange
));
485 if(VoltageRange
== FLASH_VOLTAGE_RANGE_1
)
487 tmp_psize
= FLASH_PSIZE_BYTE
;
489 else if(VoltageRange
== FLASH_VOLTAGE_RANGE_2
)
491 tmp_psize
= FLASH_PSIZE_HALF_WORD
;
493 else if(VoltageRange
== FLASH_VOLTAGE_RANGE_3
)
495 tmp_psize
= FLASH_PSIZE_WORD
;
499 tmp_psize
= FLASH_PSIZE_DOUBLE_WORD
;
502 /* Need to add offset of 4 when sector higher than FLASH_SECTOR_11 */
503 if(Sector
> FLASH_SECTOR_11
)
508 /* If the previous operation is completed, proceed to erase the sector */
509 FLASH
->CR
&= CR_PSIZE_MASK
;
510 FLASH
->CR
|= tmp_psize
;
511 CLEAR_BIT(FLASH
->CR
, FLASH_CR_SNB
);
512 FLASH
->CR
|= FLASH_CR_SER
| (Sector
<< POSITION_VAL(FLASH_CR_SNB
));
513 FLASH
->CR
|= FLASH_CR_STRT
;
515 /* Data synchronous Barrier (DSB) Just after the write operation
516 This will force the CPU to respect the sequence of instruction (no optimization).*/
521 * @brief Return the FLASH Write Protection Option Bytes value.
522 * @retval uint32_t FLASH Write Protection Option Bytes value
524 static uint32_t FLASH_OB_GetWRP(void)
526 /* Return the FLASH write protection Register value */
527 return ((uint32_t)(FLASH
->OPTCR
& 0x0FFF0000));
531 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
532 * @param Wwdg: Selects the IWDG mode
533 * This parameter can be one of the following values:
534 * @arg OB_WWDG_SW: Software WWDG selected
535 * @arg OB_WWDG_HW: Hardware WWDG selected
536 * @param Iwdg: Selects the WWDG mode
537 * This parameter can be one of the following values:
538 * @arg OB_IWDG_SW: Software IWDG selected
539 * @arg OB_IWDG_HW: Hardware IWDG selected
540 * @param Stop: Reset event when entering STOP mode.
541 * This parameter can be one of the following values:
542 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
543 * @arg OB_STOP_RST: Reset generated when entering in STOP
544 * @param Stdby: Reset event when entering Standby mode.
545 * This parameter can be one of the following values:
546 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
547 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
548 * @param Iwdgstop: Independent watchdog counter freeze in Stop mode.
549 * This parameter can be one of the following values:
550 * @arg OB_IWDG_STOP_FREEZE: Freeze IWDG counter in STOP
551 * @arg OB_IWDG_STOP_ACTIVE: IWDG counter active in STOP
552 * @param Iwdgstdby: Independent watchdog counter freeze in standby mode.
553 * This parameter can be one of the following values:
554 * @arg OB_IWDG_STDBY_FREEZE: Freeze IWDG counter in STANDBY
555 * @arg OB_IWDG_STDBY_ACTIVE: IWDG counter active in STANDBY
556 * @param NDBank: Flash Single Bank mode enabled.
557 * This parameter can be one of the following values:
558 * @arg OB_NDBANK_SINGLE_BANK: enable 256 bits mode (Flash is a single bank)
559 * @arg OB_NDBANK_DUAL_BANK: disable 256 bits mode (Flash is a dual bank in 128 bits mode)
560 * @param NDBoot: Flash Dual boot mode disable.
561 * This parameter can be one of the following values:
562 * @arg OB_DUAL_BOOT_DISABLE: Disable Dual Boot
563 * @arg OB_DUAL_BOOT_ENABLE: Enable Dual Boot
567 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg
, uint32_t Iwdg
, uint32_t Stop
, uint32_t Stdby
, uint32_t Iwdgstop
, \
568 uint32_t Iwdgstdby
, uint32_t NDBank
, uint32_t NDBoot
)
570 uint32_t useroptionmask
= 0x00;
571 uint32_t useroptionvalue
= 0x00;
573 HAL_StatusTypeDef status
= HAL_OK
;
575 /* Check the parameters */
576 assert_param(IS_OB_WWDG_SOURCE(Wwdg
));
577 assert_param(IS_OB_IWDG_SOURCE(Iwdg
));
578 assert_param(IS_OB_STOP_SOURCE(Stop
));
579 assert_param(IS_OB_STDBY_SOURCE(Stdby
));
580 assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop
));
581 assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby
));
582 assert_param(IS_OB_NDBANK(NDBank
));
583 assert_param(IS_OB_NDBOOT(NDBoot
));
585 /* Wait for last operation to be completed */
586 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
590 useroptionmask
= (FLASH_OPTCR_WWDG_SW
| FLASH_OPTCR_IWDG_SW
| FLASH_OPTCR_nRST_STOP
| \
591 FLASH_OPTCR_nRST_STDBY
| FLASH_OPTCR_IWDG_STOP
| FLASH_OPTCR_IWDG_STDBY
| \
592 FLASH_OPTCR_nDBOOT
| FLASH_OPTCR_nDBANK
);
594 useroptionvalue
= (Iwdg
| Wwdg
| Stop
| Stdby
| Iwdgstop
| Iwdgstdby
| NDBoot
| NDBank
);
596 /* Update User Option Byte */
597 MODIFY_REG(FLASH
->OPTCR
, useroptionmask
, useroptionvalue
);
604 * @brief Return the FLASH User Option Byte value.
605 * @retval uint32_t FLASH User Option Bytes values: WWDG_SW(Bit4), IWDG_SW(Bit5), nRST_STOP(Bit6),
606 * nRST_STDBY(Bit7), nDBOOT(Bit28), nDBANK(Bit29), IWDG_STDBY(Bit30) and IWDG_STOP(Bit31).
608 static uint32_t FLASH_OB_GetUser(void)
610 /* Return the User Option Byte */
611 return ((uint32_t)(FLASH
->OPTCR
& 0xF00000F0U
));
616 * @brief Full erase of FLASH memory sectors
617 * @param VoltageRange: The device voltage range which defines the erase parallelism.
618 * This parameter can be one of the following values:
619 * @arg VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
620 * the operation will be done by byte (8-bit)
621 * @arg VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
622 * the operation will be done by half word (16-bit)
623 * @arg VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
624 * the operation will be done by word (32-bit)
625 * @arg VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
626 * the operation will be done by double word (64-bit)
630 static void FLASH_MassErase(uint8_t VoltageRange
)
632 /* Check the parameters */
633 assert_param(IS_VOLTAGERANGE(VoltageRange
));
635 /* if the previous operation is completed, proceed to erase all sectors */
636 FLASH
->CR
&= CR_PSIZE_MASK
;
637 FLASH
->CR
|= FLASH_CR_MER
;
638 FLASH
->CR
|= FLASH_CR_STRT
| ((uint32_t)VoltageRange
<<8);
639 /* Data synchronous Barrier (DSB) Just after the write operation
640 This will force the CPU to respect the sequence of instruction (no optimization).*/
645 * @brief Erase the specified FLASH memory sector
646 * @param Sector: FLASH sector to erase
647 * The value of this parameter depend on device used within the same series
648 * @param VoltageRange: The device voltage range which defines the erase parallelism.
649 * This parameter can be one of the following values:
650 * @arg FLASH_VOLTAGE_RANGE_1: when the device voltage range is 1.8V to 2.1V,
651 * the operation will be done by byte (8-bit)
652 * @arg FLASH_VOLTAGE_RANGE_2: when the device voltage range is 2.1V to 2.7V,
653 * the operation will be done by half word (16-bit)
654 * @arg FLASH_VOLTAGE_RANGE_3: when the device voltage range is 2.7V to 3.6V,
655 * the operation will be done by word (32-bit)
656 * @arg FLASH_VOLTAGE_RANGE_4: when the device voltage range is 2.7V to 3.6V + External Vpp,
657 * the operation will be done by double word (64-bit)
661 void FLASH_Erase_Sector(uint32_t Sector
, uint8_t VoltageRange
)
663 uint32_t tmp_psize
= 0;
665 /* Check the parameters */
666 assert_param(IS_FLASH_SECTOR(Sector
));
667 assert_param(IS_VOLTAGERANGE(VoltageRange
));
669 if(VoltageRange
== FLASH_VOLTAGE_RANGE_1
)
671 tmp_psize
= FLASH_PSIZE_BYTE
;
673 else if(VoltageRange
== FLASH_VOLTAGE_RANGE_2
)
675 tmp_psize
= FLASH_PSIZE_HALF_WORD
;
677 else if(VoltageRange
== FLASH_VOLTAGE_RANGE_3
)
679 tmp_psize
= FLASH_PSIZE_WORD
;
683 tmp_psize
= FLASH_PSIZE_DOUBLE_WORD
;
686 /* If the previous operation is completed, proceed to erase the sector */
687 FLASH
->CR
&= CR_PSIZE_MASK
;
688 FLASH
->CR
|= tmp_psize
;
689 FLASH
->CR
&= SECTOR_MASK
;
690 FLASH
->CR
|= FLASH_CR_SER
| (Sector
<< POSITION_VAL(FLASH_CR_SNB
));
691 FLASH
->CR
|= FLASH_CR_STRT
;
693 /* Data synchronous Barrier (DSB) Just after the write operation
694 This will force the CPU to respect the sequence of instruction (no optimization).*/
699 * @brief Return the FLASH Write Protection Option Bytes value.
700 * @retval uint32_t FLASH Write Protection Option Bytes value
702 static uint32_t FLASH_OB_GetWRP(void)
704 /* Return the FLASH write protection Register value */
705 return ((uint32_t)(FLASH
->OPTCR
& 0x00FF0000));
709 * @brief Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
710 * @param Wwdg: Selects the IWDG mode
711 * This parameter can be one of the following values:
712 * @arg OB_WWDG_SW: Software WWDG selected
713 * @arg OB_WWDG_HW: Hardware WWDG selected
714 * @param Iwdg: Selects the WWDG mode
715 * This parameter can be one of the following values:
716 * @arg OB_IWDG_SW: Software IWDG selected
717 * @arg OB_IWDG_HW: Hardware IWDG selected
718 * @param Stop: Reset event when entering STOP mode.
719 * This parameter can be one of the following values:
720 * @arg OB_STOP_NO_RST: No reset generated when entering in STOP
721 * @arg OB_STOP_RST: Reset generated when entering in STOP
722 * @param Stdby: Reset event when entering Standby mode.
723 * This parameter can be one of the following values:
724 * @arg OB_STDBY_NO_RST: No reset generated when entering in STANDBY
725 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
726 * @param Iwdgstop: Independent watchdog counter freeze in Stop mode.
727 * This parameter can be one of the following values:
728 * @arg OB_IWDG_STOP_FREEZE: Freeze IWDG counter in STOP
729 * @arg OB_IWDG_STOP_ACTIVE: IWDG counter active in STOP
730 * @param Iwdgstdby: Independent watchdog counter freeze in standby mode.
731 * This parameter can be one of the following values:
732 * @arg OB_IWDG_STDBY_FREEZE: Freeze IWDG counter in STANDBY
733 * @arg OB_IWDG_STDBY_ACTIVE: IWDG counter active in STANDBY
736 static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg
, uint32_t Iwdg
, uint32_t Stop
, uint32_t Stdby
, uint32_t Iwdgstop
, uint32_t Iwdgstdby
)
738 uint32_t useroptionmask
= 0x00;
739 uint32_t useroptionvalue
= 0x00;
741 HAL_StatusTypeDef status
= HAL_OK
;
743 /* Check the parameters */
744 assert_param(IS_OB_WWDG_SOURCE(Wwdg
));
745 assert_param(IS_OB_IWDG_SOURCE(Iwdg
));
746 assert_param(IS_OB_STOP_SOURCE(Stop
));
747 assert_param(IS_OB_STDBY_SOURCE(Stdby
));
748 assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop
));
749 assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby
));
751 /* Wait for last operation to be completed */
752 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
756 useroptionmask
= (FLASH_OPTCR_WWDG_SW
| FLASH_OPTCR_IWDG_SW
| FLASH_OPTCR_nRST_STOP
| \
757 FLASH_OPTCR_nRST_STDBY
| FLASH_OPTCR_IWDG_STOP
| FLASH_OPTCR_IWDG_STDBY
);
759 useroptionvalue
= (Iwdg
| Wwdg
| Stop
| Stdby
| Iwdgstop
| Iwdgstdby
);
761 /* Update User Option Byte */
762 MODIFY_REG(FLASH
->OPTCR
, useroptionmask
, useroptionvalue
);
770 * @brief Return the FLASH User Option Byte value.
771 * @retval uint32_t FLASH User Option Bytes values: WWDG_SW(Bit4), IWDG_SW(Bit5), nRST_STOP(Bit6),
772 * nRST_STDBY(Bit7), IWDG_STDBY(Bit30) and IWDG_STOP(Bit31).
774 static uint32_t FLASH_OB_GetUser(void)
776 /* Return the User Option Byte */
777 return ((uint32_t)(FLASH
->OPTCR
& 0xC00000F0));
779 #endif /* FLASH_OPTCR_nDBANK */
782 * @brief Enable the write protection of the desired bank1 or bank2 sectors
784 * @note When the memory read protection level is selected (RDP level = 1),
785 * it is not possible to program or erase the flash sector i if CortexM7
786 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
788 * @param WRPSector: specifies the sector(s) to be write protected.
789 * This parameter can be one of the following values:
790 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_7 (for STM32F74xxx/STM32F75xxx devices)
791 * or a value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_11 (in Single Bank mode for STM32F76xxx/STM32F77xxx devices)
792 * or a value between OB_WRP_DB_SECTOR_0 and OB_WRP_DB_SECTOR_23 (in Dual Bank mode for STM32F76xxx/STM32F77xxx devices)
793 * @arg OB_WRP_SECTOR_All
795 * @retval HAL FLASH State
797 static HAL_StatusTypeDef
FLASH_OB_EnableWRP(uint32_t WRPSector
)
799 HAL_StatusTypeDef status
= HAL_OK
;
801 /* Check the parameters */
802 assert_param(IS_OB_WRP_SECTOR(WRPSector
));
804 /* Wait for last operation to be completed */
805 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
809 /*Write protection enabled on sectors */
810 FLASH
->OPTCR
&= (~WRPSector
);
817 * @brief Disable the write protection of the desired bank1 or bank 2 sectors
819 * @note When the memory read protection level is selected (RDP level = 1),
820 * it is not possible to program or erase the flash sector i if CortexM4
821 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1
823 * @param WRPSector: specifies the sector(s) to be write protected.
824 * This parameter can be one of the following values:
825 * @arg WRPSector: A value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_7 (for STM32F74xxx/STM32F75xxx devices)
826 * or a value between OB_WRP_SECTOR_0 and OB_WRP_SECTOR_11 (in Single Bank mode for STM32F76xxx/STM32F77xxx devices)
827 * or a value between OB_WRP_DB_SECTOR_0 and OB_WRP_DB_SECTOR_23 (in Dual Bank mode for STM32F76xxx/STM32F77xxx devices)
828 * @arg OB_WRP_Sector_All
833 static HAL_StatusTypeDef
FLASH_OB_DisableWRP(uint32_t WRPSector
)
835 HAL_StatusTypeDef status
= HAL_OK
;
837 /* Check the parameters */
838 assert_param(IS_OB_WRP_SECTOR(WRPSector
));
840 /* Wait for last operation to be completed */
841 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
845 /* Write protection disabled on sectors */
846 FLASH
->OPTCR
|= (WRPSector
);
853 * @brief Set the read protection level.
854 * @param Level: specifies the read protection level.
855 * This parameter can be one of the following values:
856 * @arg OB_RDP_LEVEL_0: No protection
857 * @arg OB_RDP_LEVEL_1: Read protection of the memory
858 * @arg OB_RDP_LEVEL_2: Full chip protection
860 * @note WARNING: When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
864 static HAL_StatusTypeDef
FLASH_OB_RDP_LevelConfig(uint8_t Level
)
866 HAL_StatusTypeDef status
= HAL_OK
;
868 /* Check the parameters */
869 assert_param(IS_OB_RDP_LEVEL(Level
));
871 /* Wait for last operation to be completed */
872 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
876 *(__IO
uint8_t*)OPTCR_BYTE1_ADDRESS
= Level
;
883 * @brief Set the BOR Level.
884 * @param Level: specifies the Option Bytes BOR Reset Level.
885 * This parameter can be one of the following values:
886 * @arg OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
887 * @arg OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
888 * @arg OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
889 * @arg OB_BOR_OFF: Supply voltage ranges from 1.62 to 2.1 V
892 static HAL_StatusTypeDef
FLASH_OB_BOR_LevelConfig(uint8_t Level
)
894 /* Check the parameters */
895 assert_param(IS_OB_BOR_LEVEL(Level
));
897 /* Set the BOR Level */
898 MODIFY_REG(FLASH
->OPTCR
, FLASH_OPTCR_BOR_LEV
, Level
);
905 * @brief Configure Boot base address.
907 * @param BootOption : specifies Boot base address depending from Boot pin = 0 or pin = 1
908 * This parameter can be one of the following values:
909 * @arg OPTIONBYTE_BOOTADDR_0 : Boot address based when Boot pin = 0
910 * @arg OPTIONBYTE_BOOTADDR_1 : Boot address based when Boot pin = 1
911 * @param Address: specifies Boot base address
912 * This parameter can be one of the following values:
913 * @arg OB_BOOTADDR_ITCM_RAM : Boot from ITCM RAM (0x00000000)
914 * @arg OB_BOOTADDR_SYSTEM : Boot from System memory bootloader (0x00100000)
915 * @arg OB_BOOTADDR_ITCM_FLASH : Boot from Flash on ITCM interface (0x00200000)
916 * @arg OB_BOOTADDR_AXIM_FLASH : Boot from Flash on AXIM interface (0x08000000)
917 * @arg OB_BOOTADDR_DTCM_RAM : Boot from DTCM RAM (0x20000000)
918 * @arg OB_BOOTADDR_SRAM1 : Boot from SRAM1 (0x20010000)
919 * @arg OB_BOOTADDR_SRAM2 : Boot from SRAM2 (0x2004C000)
923 static HAL_StatusTypeDef
FLASH_OB_BootAddressConfig(uint32_t BootOption
, uint32_t Address
)
925 HAL_StatusTypeDef status
= HAL_OK
;
927 /* Check the parameters */
928 assert_param(IS_OB_BOOT_ADDRESS(Address
));
930 /* Wait for last operation to be completed */
931 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
935 if(BootOption
== OPTIONBYTE_BOOTADDR_0
)
937 MODIFY_REG(FLASH
->OPTCR1
, FLASH_OPTCR1_BOOT_ADD0
, Address
);
941 MODIFY_REG(FLASH
->OPTCR1
, FLASH_OPTCR1_BOOT_ADD1
, (Address
<< 16));
949 * @brief Returns the FLASH Read Protection level.
950 * @retval FlagStatus FLASH ReadOut Protection Status:
951 * This parameter can be one of the following values:
952 * @arg OB_RDP_LEVEL_0: No protection
953 * @arg OB_RDP_LEVEL_1: Read protection of the memory
954 * @arg OB_RDP_LEVEL_2: Full chip protection
956 static uint8_t FLASH_OB_GetRDP(void)
958 uint8_t readstatus
= OB_RDP_LEVEL_0
;
960 if ((*(__IO
uint8_t*)(OPTCR_BYTE1_ADDRESS
)) == OB_RDP_LEVEL_0
)
962 readstatus
= OB_RDP_LEVEL_0
;
964 else if ((*(__IO
uint8_t*)(OPTCR_BYTE1_ADDRESS
)) == OB_RDP_LEVEL_2
)
966 readstatus
= OB_RDP_LEVEL_2
;
970 readstatus
= OB_RDP_LEVEL_1
;
977 * @brief Returns the FLASH BOR level.
978 * @retval uint32_t The FLASH BOR level:
979 * - OB_BOR_LEVEL3: Supply voltage ranges from 2.7 to 3.6 V
980 * - OB_BOR_LEVEL2: Supply voltage ranges from 2.4 to 2.7 V
981 * - OB_BOR_LEVEL1: Supply voltage ranges from 2.1 to 2.4 V
982 * - OB_BOR_OFF : Supply voltage ranges from 1.62 to 2.1 V
984 static uint32_t FLASH_OB_GetBOR(void)
986 /* Return the FLASH BOR level */
987 return ((uint32_t)(FLASH
->OPTCR
& 0x0C));
991 * @brief Configure Boot base address.
993 * @param BootOption : specifies Boot base address depending from Boot pin = 0 or pin = 1
994 * This parameter can be one of the following values:
995 * @arg OPTIONBYTE_BOOTADDR_0 : Boot address based when Boot pin = 0
996 * @arg OPTIONBYTE_BOOTADDR_1 : Boot address based when Boot pin = 1
998 * @retval uint32_t Boot Base Address:
999 * - OB_BOOTADDR_ITCM_RAM : Boot from ITCM RAM (0x00000000)
1000 * - OB_BOOTADDR_SYSTEM : Boot from System memory bootloader (0x00100000)
1001 * - OB_BOOTADDR_ITCM_FLASH : Boot from Flash on ITCM interface (0x00200000)
1002 * - OB_BOOTADDR_AXIM_FLASH : Boot from Flash on AXIM interface (0x08000000)
1003 * - OB_BOOTADDR_DTCM_RAM : Boot from DTCM RAM (0x20000000)
1004 * - OB_BOOTADDR_SRAM1 : Boot from SRAM1 (0x20010000)
1005 * - OB_BOOTADDR_SRAM2 : Boot from SRAM2 (0x2004C000)
1007 static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption
)
1009 uint32_t Address
= 0;
1011 /* Return the Boot base Address */
1012 if(BootOption
== OPTIONBYTE_BOOTADDR_0
)
1014 Address
= FLASH
->OPTCR1
& FLASH_OPTCR1_BOOT_ADD0
;
1018 Address
= ((FLASH
->OPTCR1
& FLASH_OPTCR1_BOOT_ADD1
) >> 16);
1028 #endif /* HAL_FLASH_MODULE_ENABLED */
1038 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/