ReadProtection은 설정하고 나면 옵션을 지우고 플래쉬가 삭제되어야만 사용할 수 있으니 주의가 필요합니다.
#define BORLEVEL OB_BOR_LEVEL1
#define RDPLEVEL OB_RDP_LEVEL_1
#define BOR_FLAG_TIMEOUT 3000
/*******************************************************************************
* Function Name :
* Parameters : None
* Return : None
* Description :
*******************************************************************************/
void Config_BOR(void)
{
uint32_t BORTimeout = BOR_FLAG_TIMEOUT;
FLASH_OBProgramInitTypeDef FLASH_Handle;
/* Get FLASH_WRP_SECTORS write protection status */
FLASH_Handle.Banks = FLASH_BANK_1;
HAL_FLASHEx_OBGetConfig(&FLASH_Handle);
DEBUGPRINT("USERConfig %X\r\n", FLASH_Handle.USERConfig);
DEBUGPRINT("FLASH_Handle.BORLevel %X\r\n", FLASH_Handle.BORLevel);
if(Get_BORConfig() != BORLEVEL){
FLASH_Handle.OptionType = OPTIONBYTE_BOR;
FLASH_Handle.BORLevel = BORLEVEL;
while (HAL_FLASH_Unlock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_Unlock Fail\r\n");break;}
DEBUGPRINT("HAL_FLASH_Unlock\r\n");
}
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_OB_Unlock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_OB_Unlock Fail\r\n");break;}
}
DEBUGPRINT("USERConfig %X\r\n", FLASH_Handle.USERConfig);
DEBUGPRINT("FLASH_Handle.BORLevel %X\r\n", FLASH_Handle.BORLevel);
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASHEx_OBProgram(&FLASH_Handle) != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASHEx_OBProgram Fail\r\n");break;}
DEBUGPRINT("HAL_FLASHEx_OBProgram\r\n");
}
/* Launch Option bytes loading */
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_OB_Launch() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASHEx_OBProgram Fail\r\n");break;}
DEBUGPRINT("HAL_FLASHEx_OBProgram\r\n");
}
/*
as the CPU is executing from the Flash Bank1, and the I-Cache is enabled :
Instruction cache must be invalidated after bank switching to ensure that
CPU will fetch correct instructions from the flash.
*/
SCB_InvalidateICache();
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_OB_Lock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_OB_Lock Fail\r\n");break;}
DEBUGPRINT("HAL_FLASH_OB_Lock\r\n");
}
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_Lock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_Lock Fail\r\n");break;}
DEBUGPRINT("HAL_FLASH_Lock\r\n");
}
}
}
/*******************************************************************************
* Function Name :
* Parameters : None
* Return : None
* Description :
*******************************************************************************/
void Config_RDP(void)
{
uint32_t BORTimeout = BOR_FLAG_TIMEOUT;
FLASH_OBProgramInitTypeDef FLASH_Handle;
/* Get FLASH_WRP_SECTORS write protection status */
FLASH_Handle.Banks = FLASH_BANK_1;
HAL_FLASHEx_OBGetConfig(&FLASH_Handle);
DEBUGPRINT("USERConfig %X\r\n", FLASH_Handle.USERConfig);
DEBUGPRINT("FLASH_Handle.RDPLevel %X \r\n", FLASH_Handle.RDPLevel);
if(Get_RDPConfig() != RDPLEVEL){
FLASH_Handle.OptionType = OPTIONBYTE_RDP;
FLASH_Handle.RDPLevel = RDPLEVEL;
while (HAL_FLASH_Unlock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_Unlock Fail\r\n");break;}
DEBUGPRINT("HAL_FLASH_Unlock\r\n");
}
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_OB_Unlock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_OB_Unlock Fail\r\n");break;}
}
DEBUGPRINT("USERConfig %X\r\n", FLASH_Handle.USERConfig);
DEBUGPRINT("FLASH_Handle.RDPLevel %X \r\n", FLASH_Handle.RDPLevel);
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASHEx_OBProgram(&FLASH_Handle) != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASHEx_OBProgram Fail\r\n");break;}
DEBUGPRINT("HAL_FLASHEx_OBProgram\r\n");
}
/* Launch Option bytes loading */
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_OB_Launch() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASHEx_OBProgram Fail\r\n");break;}
DEBUGPRINT("HAL_FLASHEx_OBProgram\r\n");
}
/*
as the CPU is executing from the Flash Bank1, and the I-Cache is enabled :
Instruction cache must be invalidated after bank switching to ensure that
CPU will fetch correct instructions from the flash.
*/
SCB_InvalidateICache();
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_OB_Lock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_OB_Lock Fail\r\n");break;}
DEBUGPRINT("HAL_FLASH_OB_Lock\r\n");
}
BORTimeout = BOR_FLAG_TIMEOUT;
while (HAL_FLASH_Lock() != HAL_OK){
if((BORTimeout--) == 0){DEBUGPRINT("HAL_FLASH_Lock Fail\r\n");break;}
DEBUGPRINT("HAL_FLASH_Lock\r\n");
}
}
}
/*******************************************************************************
* Function Name :
* Parameters : None
* Return : None
* Description :
*******************************************************************************/
uint32_t Get_USRConfig(void)
{
FLASH_OBProgramInitTypeDef FLASH_Handle;
FLASH_Handle.Banks = FLASH_BANK_1;
/* Read option bytes */
HAL_FLASHEx_OBGetConfig(&FLASH_Handle);
return (FLASH_Handle.USERConfig);
}
/*******************************************************************************
* Function Name :
* Parameters : None
* Return : None
* Description :
*******************************************************************************/
uint32_t Get_BORConfig(void)
{
FLASH_OBProgramInitTypeDef FLASH_Handle;
FLASH_Handle.Banks = FLASH_BANK_1;
/* Read option bytes */
HAL_FLASHEx_OBGetConfig(&FLASH_Handle);
return (FLASH_Handle.BORLevel);
}
/*******************************************************************************
* Function Name :
* Parameters : None
* Return : None
* Description :
*******************************************************************************/
uint32_t Get_RDPConfig(void)
{
FLASH_OBProgramInitTypeDef FLASH_Handle;
FLASH_Handle.Banks = FLASH_BANK_1;
/* Read option bytes */
HAL_FLASHEx_OBGetConfig(&FLASH_Handle);
return (FLASH_Handle.RDPLevel);
}
'초보의 아웅다웅 설계하기 > STM32' 카테고리의 다른 글
STM32L432 Write Protection (0) | 2020.03.04 |
---|---|
STM32 PID 제어 (0) | 2020.01.16 |
STM32H750 내부 온도 센서 값 읽기 (0) | 2020.01.06 |
STM32H750 내부 Flash 사용하기 (0) | 2019.12.28 |
STM32F723 UART LL_DRIVER 예제 (0) | 2019.11.17 |