STM32F4 (Flash read protection)
1. Purpose
In the actual product release, if not stored in a single-chip Flash program is doing something to protect, then there may be some unscrupulous companies, through the emulator (J-Link, ST-Link, etc.) in the Flash program reads back to obtain a bin file or hex file, then go to the cottage products. So we need to protect the program, a relatively simple and reliable method is to set Flash read protection.
2, the development environment
1, for chip: STM32F4 all chip
2, firmware library: STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
3, IDE: MDK517
3, program source code
/****************************************************************
* 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);
}
Original: https: //blog.csdn.net/u012325601/article/details/56298404