STM8L151系列 (OTA) Bootloader功能的开启 和 关闭
2020-04-01 来源:eefocus
IAR软件,使用官方halt库
#define OPT_BL_ADDR_L 0x480B
#define OPT_BL_ADDR_H 0x480C
void enable_ota_action(void)
{
uint16_t optbl = 0;
FLASH_DeInit();
FLASH_Unlock(FLASH_MemType_Data);//解锁EEPROM区域(注意type是Data)
// [ROP]默认为0xAA; Read Out Protection OFF;屏蔽读保护;
optbl = ((uint16_t)FLASH_ReadByte(OPT_BL_ADDR_L) << 8) | FLASH_ReadByte(OPT_BL_ADDR_H);
if (optbl != 0x55AA)
{
printf('[OPTBL] en: 0x%Xrn', optbl);
// OPTBL: Option Byte 可选的字节
FLASH_ProgramOptionByte(OPT_BL_ADDR_L, 0x55); // [BLCHECK1]为0x55; BootLoader Check1 Enabled;
FLASH_ProgramOptionByte(OPT_BL_ADDR_H, 0xAA); // [BLCHECK2]为0xAA; BootLoader Check2 Enabled;
}
FLASH_Lock(FLASH_MemType_Data);
}
void disable_ota_action(void)
{
uint16_t optbl = 0;
FLASH_DeInit();
FLASH_Unlock(FLASH_MemType_Data);//解锁EEPROM区域(注意type是Data)
// [ROP]默认为0xAA; Read Out Protection OFF;屏蔽读保护;
// FLASH_GetReadOutProtectionStatus(xx); FLASH_ReadOptionByte();
// https://github.com/Spider84/PLX_Extends/blob/master/main.c
optbl = ((uint16_t)FLASH_ReadByte(OPT_BL_ADDR_L) << 8) | FLASH_ReadByte(OPT_BL_ADDR_H);
if (optbl != 0x0000)
{
printf('[OPTBL] dis: 0x%Xrn', optbl);
// OPTBL: Option Byte 可选的字节
FLASH_ProgramOptionByte(OPT_BL_ADDR_L, 0x00); // [BLCHECK1]为0x00; BootLoader Check1 Disabled;
FLASH_ProgramOptionByte(OPT_BL_ADDR_H, 0x00); // [BLCHECK2]为0x00; BootLoader Check2 Disabled;
}
FLASH_Lock(FLASH_MemType_Data);
}