STM32f103内部FLASH读写保护设置流程
时间: 2024-01-18 09:04:07 浏览: 50
STM32F103内部FLASH读写保护设置流程如下:
1. 在系统时钟配置中,使能FLASH读写保护时钟。
2. 编写函数来设置FLASH读写保护。可以使用以下代码:
```c
void FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
{
FLASH_OBProgramInitTypeDef OBInit;
HAL_StatusTypeDef result = HAL_OK;
HAL_FLASH_Unlock();
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
/* Get the Dual boot configuration status */
HAL_FLASHEx_OBGetConfig(&OBInit);
/* The user is allowed to change the protection status */
if ((OBInit.USERConfig & FLASH_OPTR_WRP_SECTOR) != protectionstate)
{
/* Modify the parameter data */
if (protectionstate == FLASH_PROTECTION_DISABLE)
{
OBInit.OptionType = OPTIONBYTE_WRP;
OBInit.WRPSector = FLASH_WRPSector_AllPages;
OBInit.WRPState = OB_WRPSTATE_DISABLE;
}
else
{
OBInit.OptionType = OPTIONBYTE_WRP;
OBInit.WRPSector = FLASH_WRPSector_AllPages;
OBInit.WRPState = OB_WRPSTATE_ENABLE;
}
/* Launch the option byte loading */
result = HAL_FLASHEx_OBProgram(&OBInit);
/* Check if the option byte loading has succeeded */
if (result == HAL_OK)
{
/* Check if the programmed data is OK */
if ((OBInit.USERConfig & FLASH_OPTR_WRP_SECTOR) != protectionstate)
{
/* Error occurred while writing data in Flash memory */
result = HAL_ERROR;
}
}
}
/* Locks the FLASH */
HAL_FLASH_Lock();
/* Return the protection operation status */
if (result == HAL_OK)
{
return FLASHIF_OK;
}
else
{
return FLASHIF_FAIL;
}
}
```
3. 在主函数中调用该函数,以设置FLASH读写保护。例如,要禁用FLASH读写保护,请使用以下代码:
```c
FLASH_If_WriteProtectionConfig(FLASH_PROTECTION_DISABLE);
```
4. 在编译和下载代码之前,确保将选项字节设置为与应用程序兼容的值。
以上就是STM32F103内部FLASH读写保护设置的流程。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)