stm32 hal 读保护代码 用中文注释
时间: 2023-09-13 07:09:58 浏览: 48
以下是 STM32 HAL 库中的读保护代码,带有中文注释:
```c
/* 使能 FLASH 读保护 */
HAL_StatusTypeDef HAL_FLASHEx_EnableReadProtection(uint32_t Banks)
{
FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
HAL_StatusTypeDef status = HAL_OK;
/* 1. 读取 Options Bytes 寄存器的值 */
status = HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
if (status == HAL_OK)
{
/* 2. 设置 Options Bytes 寄存器的值,使其启用读保护 */
OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1; /* 使能 Level 1 读保护 */
status = HAL_FLASHEx_OBProgram(&OptionsBytesStruct);
if (status == HAL_OK)
{
/* 3. 对 Flash 进行擦除操作 */
status = HAL_FLASH_Unlock();
if (status == HAL_OK)
{
/* 4. 擦除 Options Bytes 区域 */
status = HAL_FLASHEx_Erase_OB();
if (status == HAL_OK)
{
/* 5. 锁定 Flash */
status = HAL_FLASH_Lock();
}
}
}
}
return status;
}
```
希望能对你有所帮助!