77 #include "stm32h7xx_hal.h"
88 #ifdef HAL_FLASH_MODULE_ENABLED
95 #define FLASH_TIMEOUT_VALUE 50000U
106 static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks);
107 static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks);
108 static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Bank);
109 static void FLASH_OB_GetWRP(uint32_t *WRPState, uint32_t *WRPSector, uint32_t Bank);
110 static void FLASH_OB_RDPConfig(uint32_t RDPLevel);
111 static uint32_t FLASH_OB_GetRDP(
void);
112 static void FLASH_OB_PCROPConfig(uint32_t PCROConfigRDP, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks);
113 static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr,uint32_t *PCROPEndAddr, uint32_t Bank);
114 static void FLASH_OB_BOR_LevelConfig(uint32_t
Level);
115 static uint32_t FLASH_OB_GetBOR(
void);
116 static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig);
117 static uint32_t FLASH_OB_GetUser(
void);
118 static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1);
119 static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1);
120 static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks);
121 static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank);
122 static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank);
123 static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank);
125 #if defined (DUAL_CORE)
126 static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1);
127 static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1);
130 #if defined (FLASH_OTPBL_LOCKBL)
131 static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block);
132 static uint32_t FLASH_OB_OTP_GetLock(
void);
135 #if defined (FLASH_OPTSR2_TCM_AXI_SHARED)
136 static void FLASH_OB_SharedRAM_Config(uint32_t SharedRamConfig);
137 static uint32_t FLASH_OB_SharedRAM_GetConfig(
void);
140 #if defined (FLASH_OPTSR2_CPUFREQ_BOOST)
141 static void FLASH_OB_CPUFreq_BoostConfig(uint32_t FreqBoost);
142 static uint32_t FLASH_OB_CPUFreq_GetBoost(
void);
181 uint32_t sector_index;
202 #if defined (DUAL_BANK)
204 if((pEraseInit->
Banks & FLASH_BANK_2) == FLASH_BANK_2)
230 #if defined (DUAL_BANK)
232 if((pEraseInit->
Banks & FLASH_BANK_2) == FLASH_BANK_2)
246 *SectorError = 0xFFFFFFFFU;
249 for(sector_index = pEraseInit->
Sector; sector_index < (pEraseInit->
NbSectors + pEraseInit->
Sector); sector_index++)
261 #if defined (DUAL_BANK)
262 if((pEraseInit->
Banks & FLASH_BANK_2) == FLASH_BANK_2)
275 *SectorError = sector_index;
318 #if defined (DUAL_BANK)
320 if((pEraseInit->
Banks & FLASH_BANK_2) == FLASH_BANK_2)
339 #if defined (FLASH_CR_OPERRIE)
347 #if defined (DUAL_BANK)
348 if((pEraseInit->
Banks & FLASH_BANK_2) == FLASH_BANK_2)
351 #if defined (FLASH_CR_OPERRIE)
368 #if defined (DUAL_BANK)
369 else if(pEraseInit->
Banks == FLASH_BANK_2)
384 #if defined (DUAL_BANK)
434 #if defined (DUAL_BANK)
468 FLASH_OB_RDPConfig(pOBInit->
RDPLevel);
490 FLASH_OB_BOR_LevelConfig(pOBInit->
BORLevel);
493 #if defined(DUAL_CORE)
495 if((pOBInit->
OptionType & OPTIONBYTE_CM7_BOOTADD) == OPTIONBYTE_CM7_BOOTADD)
501 if((pOBInit->
OptionType & OPTIONBYTE_CM4_BOOTADD) == OPTIONBYTE_CM4_BOOTADD)
503 FLASH_OB_CM4BootAddConfig(pOBInit->CM4BootConfig, pOBInit->CM4BootAddr0, pOBInit->CM4BootAddr1);
519 #if defined(FLASH_OTPBL_LOCKBL)
521 if((pOBInit->
OptionType & OPTIONBYTE_OTP_LOCK) == OPTIONBYTE_OTP_LOCK)
523 FLASH_OB_OTP_LockConfig(pOBInit->OTPBlockLock);
527 #if defined(FLASH_OPTSR2_TCM_AXI_SHARED)
529 if((pOBInit->
OptionType & OPTIONBYTE_SHARED_RAM) == OPTIONBYTE_SHARED_RAM)
531 FLASH_OB_SharedRAM_Config(pOBInit->SharedRamConfig);
535 #if defined(FLASH_OPTSR2_CPUFREQ_BOOST)
537 if((pOBInit->
OptionType & OPTIONBYTE_FREQ_BOOST) == OPTIONBYTE_FREQ_BOOST)
539 FLASH_OB_CPUFreq_BoostConfig(pOBInit->FreqBoostState);
564 pOBInit->
RDPLevel = FLASH_OB_GetRDP();
570 pOBInit->
BORLevel = FLASH_OB_GetBOR();
572 #if defined (DUAL_BANK)
592 #if defined(DUAL_CORE)
593 pOBInit->
OptionType |= OPTIONBYTE_CM7_BOOTADD | OPTIONBYTE_CM4_BOOTADD;
596 FLASH_OB_GetCM4BootAdd(&(pOBInit->CM4BootAddr0), &(pOBInit->CM4BootAddr1));
601 #if defined (FLASH_OTPBL_LOCKBL)
605 pOBInit->OTPBlockLock = FLASH_OB_OTP_GetLock();
608 #if defined (FLASH_OPTSR2_TCM_AXI_SHARED)
612 pOBInit->SharedRamConfig = FLASH_OB_SharedRAM_GetConfig();
615 #if defined (FLASH_OPTSR2_CPUFREQ_BOOST)
619 pOBInit->FreqBoostState = FLASH_OB_CPUFreq_GetBoost();
656 #if defined (DUAL_BANK)
705 uint32_t sector_index;
733 for(sector_index = pCRCInit->
Sector; sector_index < (pCRCInit->
NbSectors + pCRCInit->
Sector); sector_index++)
756 (*CRC_Result) =
FLASH->CRCDATA;
764 #if defined (DUAL_BANK)
782 for(sector_index = pCRCInit->
Sector; sector_index < (pCRCInit->
NbSectors + pCRCInit->
Sector); sector_index++)
784 FLASH_CRC_AddSector(sector_index, FLASH_BANK_2);
805 (*CRC_Result) =
FLASH->CRCDATA;
850 static void FLASH_MassErase(uint32_t VoltageRange, uint32_t Banks)
853 #if defined (FLASH_CR_PSIZE)
860 #if defined (DUAL_BANK)
862 if((Banks & FLASH_BANK_BOTH) == FLASH_BANK_BOTH)
864 #if defined (FLASH_CR_PSIZE)
870 FLASH->CR1 |= VoltageRange;
871 FLASH->CR2 |= VoltageRange;
883 #if defined (FLASH_CR_PSIZE)
886 FLASH->CR1 |= VoltageRange;
893 #if defined (DUAL_BANK)
894 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
896 #if defined (FLASH_CR_PSIZE)
899 FLASH->CR2 |= VoltageRange;
931 #if defined (FLASH_CR_PSIZE)
939 #if defined (FLASH_CR_PSIZE)
952 #if defined (DUAL_BANK)
953 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
955 #if defined (FLASH_CR_PSIZE)
984 static void FLASH_OB_EnableWRP(uint32_t WRPSector, uint32_t Banks)
996 #if defined (DUAL_BANK)
997 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1019 static void FLASH_OB_DisableWRP(uint32_t WRPSector, uint32_t Banks)
1031 #if defined (DUAL_BANK)
1032 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1058 static void FLASH_OB_GetWRP(uint32_t *WRPState, uint32_t *WRPSector, uint32_t Bank)
1060 uint32_t regvalue = 0U;
1064 regvalue =
FLASH->WPSN_CUR1;
1067 #if defined (DUAL_BANK)
1068 if(Bank == FLASH_BANK_2)
1070 regvalue =
FLASH->WPSN_CUR2;
1076 if(*WRPSector == 0U)
1104 static void FLASH_OB_RDPConfig(uint32_t RDPLevel)
1121 static uint32_t FLASH_OB_GetRDP(
void)
1135 #if defined(DUAL_CORE)
1177 static void FLASH_OB_UserConfig(uint32_t UserType, uint32_t UserConfig)
1179 uint32_t optr_reg_val = 0;
1180 uint32_t optr_reg_mask = 0;
1194 #if defined(DUAL_CORE)
1195 if((UserType & OB_USER_IWDG2_SW) != 0U)
1265 #if defined(DUAL_CORE)
1266 if((UserType & OB_USER_BCM4) != 0U)
1276 if((UserType & OB_USER_BCM7) != 0U)
1287 #if defined (FLASH_OPTSR_NRST_STOP_D2)
1288 if((UserType & OB_USER_NRST_STOP_D2) != 0U)
1298 if((UserType & OB_USER_NRST_STDBY_D2) != 0U)
1309 #if defined (DUAL_BANK)
1310 if((UserType & OB_USER_SWAP_BANK) != 0U)
1331 #if defined (FLASH_OPTSR_VDDMMC_HSLV)
1332 if((UserType & OB_USER_VDDMMC_HSLV) != 0U)
1335 assert_param(IS_OB_USER_VDDMMC_HSLV(UserConfig & FLASH_OPTSR_VDDMMC_HSLV));
1338 optr_reg_val |= (UserConfig & FLASH_OPTSR_VDDMMC_HSLV);
1339 optr_reg_mask |= FLASH_OPTSR_VDDMMC_HSLV;
1347 #if defined(DUAL_CORE)
1365 static uint32_t FLASH_OB_GetUser(
void)
1399 static void FLASH_OB_PCROPConfig(uint32_t PCROPConfig, uint32_t PCROPStartAddr, uint32_t PCROPEndAddr, uint32_t Banks)
1416 #if defined (DUAL_BANK)
1417 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1419 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(PCROPStartAddr));
1420 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(PCROPEndAddr));
1448 static void FLASH_OB_GetPCROP(uint32_t *PCROPConfig, uint32_t *PCROPStartAddr, uint32_t *PCROPEndAddr, uint32_t Bank)
1450 uint32_t regvalue = 0;
1451 uint32_t bankBase = 0;
1455 regvalue =
FLASH->PRAR_CUR1;
1459 #if defined (DUAL_BANK)
1460 if(Bank == FLASH_BANK_2)
1462 regvalue =
FLASH->PRAR_CUR2;
1471 (*PCROPEndAddr) = ((*PCROPEndAddr) << 8) + bankBase;
1484 static void FLASH_OB_BOR_LevelConfig(uint32_t
Level)
1501 static uint32_t FLASH_OB_GetBOR(
void)
1516 static void FLASH_OB_BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
1527 #if defined(DUAL_CORE)
1540 #if defined(DUAL_CORE)
1554 static void FLASH_OB_GetBootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
1558 #if defined(DUAL_CORE)
1559 regvalue =
FLASH->BOOT7_CUR;
1564 regvalue =
FLASH->BOOT_CUR;
1571 #if defined(DUAL_CORE)
1582 static void FLASH_OB_CM4BootAddConfig(uint32_t BootOption, uint32_t BootAddress0, uint32_t BootAddress1)
1613 static void FLASH_OB_GetCM4BootAdd(uint32_t *BootAddress0, uint32_t *BootAddress1)
1617 regvalue =
FLASH->BOOT4_CUR;
1638 static void FLASH_OB_SecureAreaConfig(uint32_t SecureAreaConfig, uint32_t SecureAreaStartAddr, uint32_t SecureAreaEndAddr, uint32_t Banks)
1656 #if defined (DUAL_BANK)
1657 if((Banks & FLASH_BANK_2) == FLASH_BANK_2)
1660 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(SecureAreaStartAddr));
1661 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(SecureAreaEndAddr));
1680 static void FLASH_OB_GetSecureArea(uint32_t *SecureAreaConfig, uint32_t *SecureAreaStartAddr, uint32_t *SecureAreaEndAddr, uint32_t Bank)
1682 uint32_t regvalue = 0;
1683 uint32_t bankBase = 0;
1688 regvalue =
FLASH->SCAR_CUR1;
1692 #if defined (DUAL_BANK)
1693 if(Bank == FLASH_BANK_2)
1695 regvalue =
FLASH->SCAR_CUR2;
1704 (*SecureAreaEndAddr) = ((*SecureAreaEndAddr) << 8) + bankBase;
1713 static void FLASH_CRC_AddSector(uint32_t Sector, uint32_t Bank)
1726 #if defined (DUAL_BANK)
1745 static void FLASH_CRC_SelectAddress(uint32_t CRCStartAddr, uint32_t CRCEndAddr, uint32_t Bank)
1753 FLASH->CRCSADD1 = CRCStartAddr;
1754 FLASH->CRCEADD1 = CRCEndAddr;
1756 #if defined (DUAL_BANK)
1759 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(CRCStartAddr));
1760 assert_param(IS_FLASH_PROGRAM_ADDRESS_BANK2(CRCEndAddr));
1763 FLASH->CRCSADD2 = CRCStartAddr;
1764 FLASH->CRCEADD2 = CRCEndAddr;
1772 #if defined (FLASH_OTPBL_LOCKBL)
1779 static void FLASH_OB_OTP_LockConfig(uint32_t OTP_Block)
1785 FLASH->OTPBL_PRG |= (OTP_Block & FLASH_OTPBL_LOCKBL);
1793 static uint32_t FLASH_OB_OTP_GetLock(
void)
1795 return (
FLASH->OTPBL_CUR);
1799 #if defined (FLASH_OPTSR2_TCM_AXI_SHARED)
1806 static void FLASH_OB_SharedRAM_Config(uint32_t SharedRamConfig)
1809 assert_param(IS_OB_USER_TCM_AXI_SHARED(SharedRamConfig));
1820 static uint32_t FLASH_OB_SharedRAM_GetConfig(
void)
1826 #if defined (FLASH_OPTSR2_CPUFREQ_BOOST)
1833 static void FLASH_OB_CPUFreq_BoostConfig(uint32_t FreqBoost)
1847 static uint32_t FLASH_OB_CPUFreq_GetBoost(
void)