86#define assert_param(expr) ((void)0)
88#define HAL_FLASH_MODULE_ENABLED
89#define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))
104#ifdef HAL_FLASH_MODULE_ENABLED
111#define FLASH_TIMEOUT_VALUE ((uint32_t)50000)
143#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) ||\
149#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
150static HAL_StatusTypeDef
FLASH_OB_EnablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
151static HAL_StatusTypeDef
FLASH_OB_DisablePCROP(uint32_t SectorBank1, uint32_t SectorBank2, uint32_t Banks);
192 HAL_StatusTypeDef status = HAL_ERROR;
199 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
207 *SectorError = 0xFFFFFFFF;
209 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
212 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
218 FLASH->CR &= (~FLASH_MER_BIT);
223 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
226 for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
234 CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
239 *SectorError = index;
263 HAL_StatusTypeDef status = HAL_OK;
269 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
272 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
275 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
278 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
279 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
281 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
286 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
293 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
316 HAL_StatusTypeDef status = HAL_ERROR;
322 assert_param(IS_OPTIONBYTE(pOBInit->
OptionType));
325 if((pOBInit->
OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
327 assert_param(IS_WRPSTATE(pOBInit->
WRPState));
328 if(pOBInit->
WRPState == OB_WRPSTATE_ENABLE)
341 if((pOBInit->
OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
347 if((pOBInit->
OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
355 if((pOBInit->
OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
373 if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN))
376 __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
378 __HAL_FLASH_INSTRUCTION_CACHE_RESET();
380 __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
384 if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN))
387 __HAL_FLASH_DATA_CACHE_DISABLE();
389 __HAL_FLASH_DATA_CACHE_RESET();
391 __HAL_FLASH_DATA_CACHE_ENABLE();
404 pOBInit->
OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
419#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
420 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
421 defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F469xx) ||\
432 HAL_StatusTypeDef status = HAL_ERROR;
435 assert_param(IS_OBEX(pAdvOBInit->
OptionType));
438 if(((pAdvOBInit->
OptionType) & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
441 assert_param(IS_PCROPSTATE(pAdvOBInit->
PCROPState));
442 if((pAdvOBInit->
PCROPState) == OB_PCROP_STATE_ENABLE)
445#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
446 defined(STM32F411xE) || defined(STM32F446xx)
455#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
456 defined(STM32F411xE) || defined(STM32F446xx)
464#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
466 if(((pAdvOBInit->
OptionType) & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
484#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
485 defined(STM32F411xE) || defined(STM32F446xx)
487 pAdvOBInit->
Sectors = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
490 pAdvOBInit->
SectorsBank1 = (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
493 pAdvOBInit->
SectorsBank2 = (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
496 pAdvOBInit->
BootConfig = *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS;
514 uint8_t optiontmp = 0xFF;
517 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
520 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_SELECTED | optiontmp);
539 uint8_t optiontmp = 0xFF;
542 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE3_ADDRESS) & (uint8_t)0x7F);
545 *(__IO uint8_t *)OPTCR_BYTE3_ADDRESS = (uint8_t)(OB_PCROP_DESELECTED | optiontmp);
552#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
561 return (*(__IO uint16_t *)(OPTCR1_BYTE2_ADDRESS));
569#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
593 uint32_t tmp_psize = 0;
596 assert_param(IS_VOLTAGERANGE(VoltageRange));
597 assert_param(IS_FLASH_BANK(Banks));
600 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
601 FLASH->CR |= tmp_psize;
602 if(Banks == FLASH_BANK_BOTH)
605 FLASH->CR |= FLASH_MER_BIT;
607 else if(Banks == FLASH_BANK_1)
610 FLASH->CR |= FLASH_CR_MER1;
615 FLASH->CR |= FLASH_CR_MER2;
617 FLASH->CR |= FLASH_CR_STRT;
639 uint32_t tmp_psize = 0;
642 assert_param(IS_FLASH_SECTOR(Sector));
643 assert_param(IS_VOLTAGERANGE(VoltageRange));
645 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
647 tmp_psize = FLASH_PSIZE_BYTE;
649 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
651 tmp_psize = FLASH_PSIZE_HALF_WORD;
653 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
655 tmp_psize = FLASH_PSIZE_WORD;
659 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
663 if(Sector > FLASH_SECTOR_11)
668 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
669 FLASH->CR |= tmp_psize;
670 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
671 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
672 FLASH->CR |= FLASH_CR_STRT;
699 HAL_StatusTypeDef status = HAL_OK;
702 assert_param(IS_OB_WRP_SECTOR(WRPSector));
703 assert_param(IS_FLASH_BANK(Banks));
710 if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
711 (WRPSector < OB_WRP_SECTOR_12))
713 if(WRPSector == OB_WRP_SECTOR_All)
716 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~(WRPSector>>12));
721 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
727 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
731 if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
738 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~(WRPSector>>12));
770 HAL_StatusTypeDef status = HAL_OK;
773 assert_param(IS_OB_WRP_SECTOR(WRPSector));
774 assert_param(IS_FLASH_BANK(Banks));
781 if(((WRPSector == OB_WRP_SECTOR_All) && ((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))) ||
782 (WRPSector < OB_WRP_SECTOR_12))
784 if(WRPSector == OB_WRP_SECTOR_All)
787 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
792 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
798 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
802 if((WRPSector == OB_WRP_SECTOR_All) && (Banks == FLASH_BANK_BOTH))
809 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)(WRPSector>>12);
831 HAL_StatusTypeDef status = HAL_OK;
834 assert_param(IS_OB_BOOT(BootConfig));
842 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BFB2);
843 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= BootConfig;
871 HAL_StatusTypeDef status = HAL_OK;
873 assert_param(IS_FLASH_BANK(Banks));
880 if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
882 assert_param(IS_OB_PCROP(SectorBank1));
884 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)SectorBank1;
888 assert_param(IS_OB_PCROP(SectorBank2));
890 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
894 if(Banks == FLASH_BANK_BOTH)
896 assert_param(IS_OB_PCROP(SectorBank2));
903 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS |= (uint16_t)SectorBank2;
935 HAL_StatusTypeDef status = HAL_OK;
938 assert_param(IS_FLASH_BANK(Banks));
945 if((Banks == FLASH_BANK_1) || (Banks == FLASH_BANK_BOTH))
947 assert_param(IS_OB_PCROP(SectorBank1));
949 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~SectorBank1);
954 assert_param(IS_OB_PCROP(SectorBank2));
955 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
959 if(Banks == FLASH_BANK_BOTH)
961 assert_param(IS_OB_PCROP(SectorBank2));
968 *(__IO uint16_t*)OPTCR1_BYTE2_ADDRESS &= (~SectorBank2);
980#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
981 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) ||\
982 defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx)
1007 uint32_t tmp_psize = 0;
1010 assert_param(IS_VOLTAGERANGE(VoltageRange));
1011 assert_param(IS_FLASH_BANK(Banks));
1014 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
1015 FLASH->CR |= tmp_psize;
1016 FLASH->CR |= FLASH_CR_MER;
1017 FLASH->CR |= FLASH_CR_STRT;
1039 uint32_t tmp_psize = 0;
1042 assert_param(IS_FLASH_SECTOR(Sector));
1043 assert_param(IS_VOLTAGERANGE(VoltageRange));
1045 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
1047 tmp_psize = FLASH_PSIZE_BYTE;
1049 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
1051 tmp_psize = FLASH_PSIZE_HALF_WORD;
1053 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
1055 tmp_psize = FLASH_PSIZE_WORD;
1059 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
1063 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
1064 FLASH->CR |= tmp_psize;
1065 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
1066 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
1067 FLASH->CR |= FLASH_CR_STRT;
1091 HAL_StatusTypeDef status = HAL_OK;
1094 assert_param(IS_OB_WRP_SECTOR(WRPSector));
1095 assert_param(IS_FLASH_BANK(Banks));
1100 if(status == HAL_OK)
1102 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~WRPSector);
1129 HAL_StatusTypeDef status = HAL_OK;
1132 assert_param(IS_OB_WRP_SECTOR(WRPSector));
1133 assert_param(IS_FLASH_BANK(Banks));
1138 if(status == HAL_OK)
1140 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)WRPSector;
1147#if defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) ||\
1148 defined(STM32F411xE) || defined(STM32F446xx)
1160 HAL_StatusTypeDef status = HAL_OK;
1163 assert_param(IS_OB_PCROP(Sector));
1168 if(status == HAL_OK)
1170 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)Sector;
1188 HAL_StatusTypeDef status = HAL_OK;
1191 assert_param(IS_OB_PCROP(Sector));
1196 if(status == HAL_OK)
1198 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~Sector);
1220 HAL_StatusTypeDef status = HAL_OK;
1223 assert_param(IS_OB_RDP_LEVEL(Level));
1228 if(status == HAL_OK)
1230 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
1254 uint8_t optiontmp = 0xFF;
1255 HAL_StatusTypeDef status = HAL_OK;
1258 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
1259 assert_param(IS_OB_STOP_SOURCE(Stop));
1260 assert_param(IS_OB_STDBY_SOURCE(Stdby));
1265 if(status == HAL_OK)
1268 optiontmp = (uint8_t)((*(__IO uint8_t *)OPTCR_BYTE0_ADDRESS) & (uint8_t)0x1F);
1271 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS = Iwdg | (uint8_t)(Stdby | (uint8_t)(Stop | ((uint8_t)optiontmp)));
1290 assert_param(IS_OB_BOR_LEVEL(Level));
1293 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS &= (~FLASH_OPTCR_BOR_LEV);
1294 *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= Level;
1308 return ((uint8_t)(FLASH->OPTCR & 0xE0));
1318 return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
1331 uint8_t readstatus = OB_RDP_LEVEL_0;
1333 if (*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_2)
1335 readstatus = OB_RDP_LEVEL_2;
1337 else if (*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS) == (uint8_t)OB_RDP_LEVEL_1)
1339 readstatus = OB_RDP_LEVEL_1;
1343 readstatus = OB_RDP_LEVEL_0;
1360 return (uint8_t)(*(__IO uint8_t *)(OPTCR_BYTE0_ADDRESS) & (uint8_t)0x0C);
HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
Program option bytes.
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
Perform a mass erase or erase the specified FLASH memory sectors.
uint16_t HAL_FLASHEx_OB_GetBank2WRP(void)
Returns the FLASH Write Protection Option Bytes value for Bank 2.
void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
Get the OBEX byte configuration.
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
Get the Option byte configuration.
HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
Select the Protection Mode.
HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
Deselect the Protection Mode.
HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
Program option bytes.
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled.
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
Erase the specified FLASH memory sector.
void FLASH_FlushCaches(void)
Flush the instruction and data caches.
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
Disable the write protection of the desired bank1 or bank 2 sectors.
static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t Level)
Set the read protection level.
static HAL_StatusTypeDef FLASH_OB_BOR_LevelConfig(uint8_t Level)
Set the BOR Level.
static uint8_t FLASH_OB_GetRDP(void)
Returns the FLASH Read Protection level.
static uint16_t FLASH_OB_GetWRP(void)
Return the FLASH Write Protection Option Bytes value.
static void FLASH_MassErase(uint8_t VoltageRange, uint32_t Banks)
Full erase of FLASH memory sectors.
static HAL_StatusTypeDef FLASH_OB_EnablePCROP(uint32_t Sector)
Enable the read/write protection (PCROP) of the desired sectors.
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t Iwdg, uint8_t Stop, uint8_t Stdby)
Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
static HAL_StatusTypeDef FLASH_OB_DisablePCROP(uint32_t Sector)
Disable the read/write protection (PCROP) of the desired sectors.
static uint8_t FLASH_OB_GetUser(void)
Return the FLASH User Option Byte value.
static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
Enable the write protection of the desired bank1 or bank 2 sectors.
static uint8_t FLASH_OB_GetBOR(void)
Returns the FLASH BOR level.
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
Wait for a FLASH operation to complete.
static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t BootConfig)
Configure the Dual Bank Boot.
FLASH_ProcessTypeDef pFlash
Header file of FLASH HAL module.
Header file of FLASH HAL Extension module.
FLASH Advanced Option Bytes Program structure definition.
FLASH Option Bytes Program structure definition.
FLASH handle Structure definition
__IO uint32_t NbSectorsToErase
__IO uint8_t VoltageForErase
__IO FLASH_ProcedureTypeDef ProcedureOnGoing