STM32F4(Flash讀保護(hù))
1,目的
在實(shí)際的產(chǎn)品發(fā)布中,如果不對(duì)儲(chǔ)存在單片機(jī)Flash中的程序做一些保護(hù)的話,就有可能被一些不法公司,通過(guò)仿真器(J-Link,ST-Link等)把Flash中的程序讀取回來(lái),得到bin文件或hex文件,然后去山寨產(chǎn)品。所以我們需要對(duì)程序進(jìn)行保護(hù),一種比較簡(jiǎn)單可靠的方法就是把Flash設(shè)置成讀保護(hù)。
2,開(kāi)發(fā)環(huán)境
1,適用芯片:STM32F4全部芯片
2,固件庫(kù):STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
3,IDE:MDK517
3,程序源碼
/****************************************************************
* Function: Flash_EnableReadProtection
* Description: Enable the read protection of user flash area.
* Input:
* Output:
* Return: 1: Read Protection successfully enable
* 2: Error: Flash read unprotection failed
*****************************************************************/
uint32_t Flash_EnableReadProtection(void)
{
/* Returns the FLASH Read Protection level. */
if( FLASH_OB_GetRDP() == RESET )
{
/* Unlock the Option Bytes */
FLASH_OB_Unlock();
/* Sets the read protection level. */
FLASH_OB_RDPConfig(OB_RDP_Level_1);
/* Start the Option Bytes programming process. */
if (FLASH_OB_Launch() != FLASH_COMPLETE)
{
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
FLASH_OB_Lock();
/* Error: Flash read unprotection failed */
return (2);
}
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
FLASH_OB_Lock();
/* Read Protection successfully enable */
return (1);
}
/* Read Protection successfully enable */
return (1);
}
/****************************************************************
* Function: Flash_DisableReadProtection
* Description: Disable the read protection of user flash area.
* Input:
* Output:
* Return: 1: Read Protection successfully disable
* 2: Error: Flash read unprotection failed
*****************************************************************/
uint32_t Flash_DisableReadProtection(void)
{
/* Returns the FLASH Read Protection level. */
if( FLASH_OB_GetRDP() != RESET )
{
/* Unlock the Option Bytes */
FLASH_OB_Unlock();
/* Sets the read protection level. */
FLASH_OB_RDPConfig(OB_RDP_Level_0);
/* Start the Option Bytes programming process. */
if (FLASH_OB_Launch() != FLASH_COMPLETE)
{
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
FLASH_OB_Lock();
/* Error: Flash read unprotection failed */
return (2);
}
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
FLASH_OB_Lock();
/* Read Protection successfully disable */
return (1);
}
/* Read Protection successfully disable */
return (1);
}