当前位置:首页 > 单片机 > 单片机
[导读] 1,目的 在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山

1,目的

在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。


2,开发环境

1,适用芯片:STM32F4全部芯片

2,固件库: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);

}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

中国北京(2025年9月10日)—— 业界领先的半导体器件供应商兆易创新GigaDevice(股票代码 603986)亮相于深圳国际会展中心举办的第26届中国国际光电博览会(展位号:12C12),全面展示GD25 SPI...

关键字: 光通信 MCU Flash

在嵌入式系统中,Flash存储器因其非易失性、高密度和低成本特性,成为代码存储和关键数据保存的核心组件。然而,MCU驱动Flash读写时,开发者常因对硬件特性理解不足或操作流程疏忽,陷入性能下降、数据损坏甚至硬件损坏的陷...

关键字: MCU驱动 Flash

深圳2025年4月24日 /美通社/ -- 4月23日,2025上海国际车展盛大启幕,全球汽车产业的目光聚焦于这场科技与创新的盛宴。在众多展示亮点中,汽车AI+应用无疑是最大的热点之一,“驾控超级大脑”技术概念、车机交互...

关键字: GB EMMC Flash PS

在自动驾驶、机器人与工业检测领域,激光雷达作为环境感知的核心传感器,其技术路线正从机械式向固态化演进。MEMS、OPA与Flash作为固态激光雷达的三大主流架构,分别通过微机电系统、光学相控阵与泛光面阵技术实现扫描与探测...

关键字: MEMS OPA Flash

在嵌入式系统开发中,外部存储介质的选择和管理至关重要。SPI Flash以其高容量、低功耗、高速率等特点,成为众多嵌入式设备的首选存储方案。然而,如何高效管理SPI Flash存储,避免数据丢失、提高读写性能,是开发者必...

关键字: 嵌入式SPI Flash LittleFS

STM32单片机作为一种高性能、低功耗的嵌入式微控制器,广泛应用于各种电子设备中。在实际应用中,为了扩展存储空间或实现数据的持久化存储,经常需要使用外部FLASH存储器。本文将详细介绍STM32单片机如何读写外部FLAS...

关键字: STM32 Flash

在嵌入式系统和存储设备领域,Flash和EEPROM(电可擦可编程只读存储器)因其非易失性存储特性而被广泛应用。这些存储设备能够在断电后保持数据,对于需要长期保存配置参数、程序代码或用户数据的应用来说至关重要。然而,关于...

关键字: Flash EEPROM
关闭