79#define assert_param(expr) ((void)0)
90#define HAL_FLASH_MODULE_ENABLED
92#ifdef HAL_FLASH_MODULE_ENABLED
99#define SECTOR_MASK 0xFFFFFF07U
100#define FLASH_TIMEOUT_VALUE 50000U
131#if defined (FLASH_OPTCR_nDBANK)
133static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \
134 uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot);
137static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, uint32_t Iwdgstdby);
140#if defined (FLASH_OPTCR2_PCROP)
182HAL_StatusTypeDef
HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
184 HAL_StatusTypeDef status = HAL_ERROR;
191 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
199 *SectorError = 0xFFFFFFFFU;
201 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
204#if defined (FLASH_OPTCR_nDBANK)
205 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
214 FLASH->CR &= (~FLASH_MER_BIT);
219 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
222 for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
230 CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));
235 *SectorError = index;
257 HAL_StatusTypeDef status = HAL_OK;
263 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
266 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
269 __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
272 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
273 FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_ERSERR);
275 if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
279#if defined (FLASH_OPTCR_nDBANK)
280 FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
290 assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));
313 HAL_StatusTypeDef status = HAL_ERROR;
319 assert_param(IS_OPTIONBYTE(pOBInit->
OptionType));
322 if((pOBInit->
OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
324 assert_param(IS_WRPSTATE(pOBInit->
WRPState));
325 if(pOBInit->
WRPState == OB_WRPSTATE_ENABLE)
338 if((pOBInit->
OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
344 if((pOBInit->
OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
346#if defined (FLASH_OPTCR_nDBANK)
366 if((pOBInit->
OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
372 if((pOBInit->
OptionType & OPTIONBYTE_BOOTADDR_0) == OPTIONBYTE_BOOTADDR_0)
378 if((pOBInit->
OptionType & OPTIONBYTE_BOOTADDR_1) == OPTIONBYTE_BOOTADDR_1)
383#if defined (FLASH_OPTCR2_PCROP)
385 if((pOBInit->
OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
391 if((pOBInit->
OptionType & OPTIONBYTE_PCROP_RDP) == OPTIONBYTE_PCROP_RDP)
412 pOBInit->
OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER |\
413 OPTIONBYTE_BOR | OPTIONBYTE_BOOTADDR_0 | OPTIONBYTE_BOOTADDR_1;
433#if defined (FLASH_OPTCR2_PCROP)
445#if defined (FLASH_OPTCR_nDBANK)
469 assert_param(IS_VOLTAGERANGE(VoltageRange));
470 assert_param(IS_FLASH_BANK(Banks));
473 FLASH->CR &= CR_PSIZE_MASK;
474 if(Banks == FLASH_BANK_BOTH)
477 FLASH->CR |= FLASH_MER_BIT;
479 else if(Banks == FLASH_BANK_2)
482 FLASH->CR |= FLASH_CR_MER2;
487 FLASH->CR |= FLASH_CR_MER1;
489 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8);
514 uint32_t tmp_psize = 0;
517 assert_param(IS_FLASH_SECTOR(Sector));
518 assert_param(IS_VOLTAGERANGE(VoltageRange));
520 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
522 tmp_psize = FLASH_PSIZE_BYTE;
524 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
526 tmp_psize = FLASH_PSIZE_HALF_WORD;
528 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
530 tmp_psize = FLASH_PSIZE_WORD;
534 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
538 if(Sector > FLASH_SECTOR_11)
544 FLASH->CR &= CR_PSIZE_MASK;
545 FLASH->CR |= tmp_psize;
546 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
547 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
548 FLASH->CR |= FLASH_CR_STRT;
562 return ((uint32_t)(FLASH->OPTCR & 0x0FFF0000));
602static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \
603 uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot)
605 uint32_t useroptionmask = 0x00;
606 uint32_t useroptionvalue = 0x00;
608 HAL_StatusTypeDef status = HAL_OK;
611 assert_param(IS_OB_WWDG_SOURCE(Wwdg));
612 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
613 assert_param(IS_OB_STOP_SOURCE(Stop));
614 assert_param(IS_OB_STDBY_SOURCE(Stdby));
615 assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop));
616 assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby));
617 assert_param(IS_OB_NDBANK(NDBank));
618 assert_param(IS_OB_NDBOOT(NDBoot));
625 useroptionmask = (FLASH_OPTCR_WWDG_SW | FLASH_OPTCR_IWDG_SW | FLASH_OPTCR_nRST_STOP | \
626 FLASH_OPTCR_nRST_STDBY | FLASH_OPTCR_IWDG_STOP | FLASH_OPTCR_IWDG_STDBY | \
627 FLASH_OPTCR_nDBOOT | FLASH_OPTCR_nDBANK);
629 useroptionvalue = (Iwdg | Wwdg | Stop | Stdby | Iwdgstop | Iwdgstdby | NDBoot | NDBank);
632 MODIFY_REG(FLASH->OPTCR, useroptionmask, useroptionvalue);
646 return ((uint32_t)(FLASH->OPTCR & 0xF00000F0U));
668 assert_param(IS_VOLTAGERANGE(VoltageRange));
671 FLASH->CR &= CR_PSIZE_MASK;
672 FLASH->CR |= FLASH_CR_MER;
673 FLASH->CR |= FLASH_CR_STRT | ((uint32_t)VoltageRange <<8);
698 uint32_t tmp_psize = 0;
701 assert_param(IS_FLASH_SECTOR(Sector));
702 assert_param(IS_VOLTAGERANGE(VoltageRange));
704 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)
706 tmp_psize = FLASH_PSIZE_BYTE;
708 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)
710 tmp_psize = FLASH_PSIZE_HALF_WORD;
712 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)
714 tmp_psize = FLASH_PSIZE_WORD;
718 tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
722 FLASH->CR &= CR_PSIZE_MASK;
723 FLASH->CR |= tmp_psize;
724 FLASH->CR &= SECTOR_MASK;
725 FLASH->CR |= FLASH_CR_SER | (Sector << POSITION_VAL(FLASH_CR_SNB));
726 FLASH->CR |= FLASH_CR_STRT;
740 return ((uint32_t)(FLASH->OPTCR & 0x00FF0000));
771static HAL_StatusTypeDef
FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, uint32_t Iwdgstdby)
773 uint32_t useroptionmask = 0x00;
774 uint32_t useroptionvalue = 0x00;
776 HAL_StatusTypeDef status = HAL_OK;
779 assert_param(IS_OB_WWDG_SOURCE(Wwdg));
780 assert_param(IS_OB_IWDG_SOURCE(Iwdg));
781 assert_param(IS_OB_STOP_SOURCE(Stop));
782 assert_param(IS_OB_STDBY_SOURCE(Stdby));
783 assert_param(IS_OB_IWDG_STOP_FREEZE(Iwdgstop));
784 assert_param(IS_OB_IWDG_STDBY_FREEZE(Iwdgstdby));
791 useroptionmask = (FLASH_OPTCR_WWDG_SW | FLASH_OPTCR_IWDG_SW | FLASH_OPTCR_nRST_STOP | \
792 FLASH_OPTCR_nRST_STDBY | FLASH_OPTCR_IWDG_STOP | FLASH_OPTCR_IWDG_STDBY);
794 useroptionvalue = (Iwdg | Wwdg | Stop | Stdby | Iwdgstop | Iwdgstdby);
797 MODIFY_REG(FLASH->OPTCR, useroptionmask, useroptionvalue);
812 return ((uint32_t)(FLASH->OPTCR & 0xC00000F0U));
834 HAL_StatusTypeDef status = HAL_OK;
837 assert_param(IS_OB_WRP_SECTOR(WRPSector));
845 FLASH->OPTCR &= (~WRPSector);
870 HAL_StatusTypeDef status = HAL_OK;
873 assert_param(IS_OB_WRP_SECTOR(WRPSector));
881 FLASH->OPTCR |= (WRPSector);
901 HAL_StatusTypeDef status = HAL_OK;
904 assert_param(IS_OB_RDP_LEVEL(Level));
911 *(__IO uint8_t*)OPTCR_BYTE1_ADDRESS = Level;
930 assert_param(IS_OB_BOR_LEVEL(Level));
933 MODIFY_REG(FLASH->OPTCR, FLASH_OPTCR_BOR_LEV, Level);
960 HAL_StatusTypeDef status = HAL_OK;
963 assert_param(IS_OB_BOOT_ADDRESS(Address));
970 if(BootOption == OPTIONBYTE_BOOTADDR_0)
972 MODIFY_REG(FLASH->OPTCR1, FLASH_OPTCR1_BOOT_ADD0, Address);
976 MODIFY_REG(FLASH->OPTCR1, FLASH_OPTCR1_BOOT_ADD1, (Address << 16));
993 uint8_t readstatus = OB_RDP_LEVEL_0;
995 if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS)) == OB_RDP_LEVEL_0)
997 readstatus = OB_RDP_LEVEL_0;
999 else if ((*(__IO uint8_t*)(OPTCR_BYTE1_ADDRESS)) == OB_RDP_LEVEL_2)
1001 readstatus = OB_RDP_LEVEL_2;
1005 readstatus = OB_RDP_LEVEL_1;
1022 return ((uint32_t)(FLASH->OPTCR & 0x0C));
1044 uint32_t Address = 0;
1047 if(BootOption == OPTIONBYTE_BOOTADDR_0)
1049 Address = FLASH->OPTCR1 & FLASH_OPTCR1_BOOT_ADD0;
1053 Address = ((FLASH->OPTCR1 & FLASH_OPTCR1_BOOT_ADD1) >> 16);
1059#if defined (FLASH_OPTCR2_PCROP)
1071 HAL_StatusTypeDef status = HAL_OK;
1074 assert_param(IS_OB_PCROP_SECTOR(PCROPSector));
1079 if(status == HAL_OK)
1081 MODIFY_REG(FLASH->OPTCR2, FLASH_OPTCR2_PCROP, PCROPSector);
1095 HAL_StatusTypeDef status = HAL_OK;
1098 assert_param(IS_OB_PCROP_RDP_VALUE(Pcrop_Rdp));
1103 if(status == HAL_OK)
1105 MODIFY_REG(FLASH->OPTCR2, FLASH_OPTCR2_PCROP_RDP, Pcrop_Rdp);
1118 return ((uint32_t)(FLASH->OPTCR2 & FLASH_OPTCR2_PCROP));
1128 return ((uint32_t)(FLASH->OPTCR2 & FLASH_OPTCR2_PCROP_RDP));
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
Wait for a FLASH operation to complete.
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.
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
Get the Option byte configuration.
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled.
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 uint32_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.
void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)
Erase the specified FLASH memory sector.
static uint32_t FLASH_OB_GetUser(void)
Return the FLASH User Option Byte value.
static uint32_t FLASH_OB_GetBOR(void)
Returns the FLASH BOR level.
static uint32_t FLASH_OB_GetPCROPRDP(void)
Return the FLASH PCROP_RDP option byte value.
static HAL_StatusTypeDef FLASH_OB_BootAddressConfig(uint32_t BootOption, uint32_t Address)
Configure Boot base address.
static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WRPSector)
Enable the write protection of the desired bank1 or bank2 sectors.
static HAL_StatusTypeDef FLASH_OB_UserConfig(uint32_t Wwdg, uint32_t Iwdg, uint32_t Stop, uint32_t Stdby, uint32_t Iwdgstop, \ uint32_t Iwdgstdby, uint32_t NDBank, uint32_t NDBoot)
Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
static HAL_StatusTypeDef FLASH_OB_PCROP_Config(uint32_t PCROPSector)
Set the PCROP protection for sectors.
static uint32_t FLASH_OB_GetBootAddress(uint32_t BootOption)
Configure Boot base address.
static uint32_t FLASH_OB_GetPCROP(void)
Return the FLASH PCROP Protection Option Bytes value.
static HAL_StatusTypeDef FLASH_OB_PCROP_RDP_Config(uint32_t Pcrop_Rdp)
Set the PCROP_RDP value.
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WRPSector)
Disable the write protection of the desired bank1 or bank 2 sectors.
FLASH_ProcessTypeDef pFlash
Header file of FLASH HAL module.
Header file of FLASH HAL Extension module.
FLASH Option Bytes Program structure definition.
FLASH handle Structure definition
__IO uint32_t NbSectorsToErase
__IO uint8_t VoltageForErase
__IO FLASH_ProcedureTypeDef ProcedureOnGoing