我正在尝试为STM32L471RET6实现银行交换机制。我的代码似乎没有设置和重置选项字节的错误,但调用银行交换函数时没有任何反应。在我使用STM32CubeProgrammer检查选项字节后,我意识到选项字节没有改变。这是我的银行互换功能。
HAL_StatusTypeDef SwapBank(void)
{
FLASH_OBProgramInitTypeDef OB;
HAL_FLASHEx_OBGetConfig(&OB);
if (OB.USERConfig & OB_DUALBANK_DUAL)
{
if (OB.USERConfig & OB_BFB2_ENABLE)
{
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
OB.OptionType = OPTIONBYTE_USER;
OB.USERType = OB_USER_BFB2;
OB.USERConfig = OB_BFB2_DISABLE;
}
else
{
OB.OptionType = OPTIONBYTE_USER;
OB.USERType = OB_USER_BFB2;
OB.USERConfig = OB_BFB2_ENABLE;
}
if (HAL_FLASHEx_OBProgram(&OB) != HAL_OK)
{
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
return HAL_ERROR;
}
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
NVIC_SystemReset();
return HAL_OK;
}
return HAL_ERROR;
}
我检查了 STM32L4xx 参考手册来编程选项字节。我进行了搜索并寻求人工智能工具的帮助,但失败了。我已经成功为 STM32L0 编写了这个程序和机制,它正在工作,但我不知道 STM32L471RET6 有什么问题。
已修复...
HAL_StatusTypeDef SwapBank(void)
{
FLASH_OBProgramInitTypeDef OB;
HAL_FLASHEx_OBGetConfig(&OB);
if (OB.USERConfig & OB_DUALBANK_DUAL)
{
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
if (OB.USERConfig & OB_BFB2_ENABLE)
{
OB.OptionType = OPTIONBYTE_USER;
OB.USERType = OB_USER_BFB2;
OB.USERConfig = OB_BFB2_DISABLE;
}
else
{
OB.OptionType = OPTIONBYTE_USER;
OB.USERType = OB_USER_BFB2;
OB.USERConfig = OB_BFB2_ENABLE;
}
if (HAL_FLASHEx_OBProgram(&OB) != HAL_OK)
{
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
return HAL_ERROR;
}
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
NVIC_SystemReset();
return HAL_OK;
}
return HAL_ERROR;
}