OTA 软复位干净跳转

测试环境:HC32F460 · Keil MDK · RT-Thread(rt-fota)

一、概述

1.1 直接跳转的问题

bootloader 完成升级后跳转 APP,常规做法是直接跳:

1
2
__set_MSP(*(__IO uint32_t *)app_addr);                 // 取 APP 向量表第 0 字:栈顶
((void (*)(void))(*(__IO uint32_t *)(app_addr + 4)))(); // 取第 1 字:复位向量

但 bootloader 跑过之后芯片已不是“干净”状态,跳转前必须逐项 deinit:

  • 时钟:PLL/分频要恢复默认,否则 APP 的 SystemInit() 无法正确重配时钟;

  • 中断:NVIC 使能位、挂起位要逐一清除,还要关全局中断;

  • SysTick:停止并清零;

  • 外设:用过的 UART/CAN/定时器逐个反初始化,引脚复用恢复默认;

  • FPU:CP10/CP11 使能状态要处理。

如果不做deinit,APP 可能表现为跑飞、HardFault、串口乱码等“玄学”问题。

1.2 软复位跳转思路

  1. Bootloader 运行时解析 APP 入口,把 MSP、PC(复位向量)存到 noinit(不初始化 RAM)区域,设置跳转标记;

  2. 执行 NVIC 软复位 → 所有寄存器、外设、NVIC 自动回到默认值,等效于干净上电,且天然处于关中断状态,省去一个个函数 deinit;

  3. 复位后在最早的启动阶段(SystemInit() 最开头)检测标记:有效则直接拿 noinit 里的 MSP、PC 跳转 APP;无效则正常启动 bootloader。

二、原理

2.1 Cortex-M 上电 / 复位启动流程

  • 硬件自动从向量表(映射在 0x00000000)取第 0 个字加载到 MSP(栈顶指针),第 1 个字(复位向量)加载到 PC;

  • 执行 Reset_Handler(),Keil 启动文件中依次调用 SystemInit() → __main();

  • __main() 做 scatter-loading:把 .data 初值从 Flash 搬到 RAM、.bss(ZI)清零,然后进入 main()。

软复位与上电的唯一区别:SRAM 不掉电,内容保持不变(上电时是随机值)。标记 + 双重校验就是围绕这一点设计的。

2.2 整体流程


三、实现步骤

3.1 noinit 变量定义

Keil(AC5/AC6)下把变量放进自定义段 .bss.noinit,该段在 scatter 中标记为 UNINIT,__main() 就不会对它做清零/搬运,软复位后数值得以保留。HC32F460 的 DDL 库已提供 __NO_INIT 宏;若其他工程没有,可自行定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef __NO_INIT
#define __NO_INIT __attribute__((section(".bss.noinit")))
#endif

/*!< System clock frequency (Core clock) */
__NO_INIT uint32_t SystemCoreClock;
/*!< High speed RC frequency (HCR clock) */
__NO_INIT uint32_t HRC_VALUE;

// boot跳转
#define BOOT_MAGIC 0xDEADBEEF
__NO_INIT volatile uint32_t boot_flag; // 跳转标志
__NO_INIT volatile uint32_t app_msp; // 要跳转的MSP值
__NO_INIT volatile uint32_t app_addr; // 要跳转的地址

/* 应用启动函数指针 */
typedef void (*rt_fota_app_func)(void);
static rt_fota_app_func app_func = NULL;

3.2 跳转处理:置标记 + 复位 + 复位后检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 设置跳转信息
void boot_set(uint32_t _msp, uint32_t _addr)
{
rt_hw_interrupt_disable();

boot_flag = BOOT_MAGIC;
app_msp = _msp;
app_addr = _addr; // 标记写最后

__NVIC_SystemReset(); // 软复位
}

// 跳转处理函数(在 SystemInit 最开头被调用)
void boot_handler()
{
if (boot_flag == BOOT_MAGIC) {
boot_flag = 0; // ★ 先清标记再跳转,防止 APP 内再次复位时误跳
/* 验证应用入口地址:复位向量必须落在 0x00xxxxxx(内部 Flash 区) */
if ((app_addr & 0xff000000) != 0x00000000){
return;
}
/* 验证栈顶地址:必须落在 SRAM1(0x1FFF8000 ~ +0x2F000)范围内 */
if (app_msp < 0x1FFF8000 || app_msp >= (0x1FFF8000 + 0x2F000)){
return;
}
/* 设置应用入口 */
app_func = (rt_fota_app_func)app_addr;
/* 设置主栈指针 */
__set_MSP(app_msp);
app_addr=0;
app_msp=0;
/* 跳转到应用程序(不会返回) */
app_func();
}
}

/**
* @brief Setup the microcontroller system. Initialize the System and update
* the SystemCoreClock variable.
* @param None
* @retval None
*/
void SystemInit(void)
{
boot_handler(); //跳转判断处理,★ 必须放在最前面,实现最快跳转

/* FPU settings */
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 20) | (3UL << 22)); /* set CP10 and CP11 Full Access */
#endif
SystemCoreClockUpdate();
#if defined (ROM_EXT_QSPI)
SystemInit_QspiMem();
#endif /* ROM_EXT_QSPI */
/* Configure the Vector Table relocation */
SCB->VTOR = VECT_TAB_OFFSET; /* Vector Table Relocation */
}

3.3 Bootloader 侧发起跳转

bootloader(rt-fota)完成升级、或开机检查无升级任务后,统一走本函数——不直接跳,而是记录参数后软复位:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* 启动应用程序 */
static int rt_fota_start_application(void)
{
int fota_res = RT_FOTA_NO_ERR;
const struct fal_partition *part;
rt_uint32_t app_addr; // 应用起始地址

/* 查找应用分区 */
part = fal_partition_find(RT_FOTA_APP_PART_NAME);
if (part == RT_NULL)
{
LOG_W("Partition[%s] not found.", fota_part_head.app_part_name);
fota_res = RT_FOTA_GENERAL_ERR;
goto __exit_start_application;
}

/* 计算应用入口地址 */
app_addr = part->offset + 0x00000000;

/* 验证应用入口地址 */
if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x00000000)
{
LOG_W("Illegal Flash code.");
fota_res = RT_FOTA_GENERAL_ERR;
goto __exit_start_application;
}

/* 验证栈顶地址 */
uint32_t stack_addr = (*(__IO uint32_t *)app_addr);
if (stack_addr < 0x1FFF8000 || stack_addr >= (0x1FFF8000 + 0x2F000))
{
LOG_I("Illegal Stack code.");
fota_res = RT_FOTA_GENERAL_ERR;
goto __exit_start_application;
}

LOG_I("Implement application now.");

extern void boot_set(uint32_t _msp, uint32_t _addr);
boot_set(*(__IO uint32_t *)app_addr, *(__IO uint32_t *)(app_addr + 4)); // 保存跳转信息

__exit_start_application:
LOG_W("Implement application failed.");

return fota_res;
}

3.4 noinit 段的 scatter 配置(link.sct)

对应 Keil 工程里的自定义散列文件(Options → Linker → 取消 Use Memory Layout from Target Dialog → 指定 Scatter File):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x00000000 0x00080000 { ; 加载域:整个 Flash(512KB)
ER_IROM1 0x00000000 0x00080000 { ; 执行域:代码/常量
*.o (RESET, +First) ; 中断向量表必须放最前面
*(InRoot$$Sections) ; C 库根段
.ANY (+RO)
.ANY (+XO)
}
RW_IRAM1 0x1FFF8000 UNINIT 0x00000018 { ; ★ noinit 区:SRAM1 起始 24 字节
*.bss.noinit ; UNINIT = __main 不初始化此区域
}
RW_IRAM2 0x1FFF8018 0x0002EFF8 { ; ★ 普通 RW/ZI 区:紧跟 noinit 区之后
.ANY (+RW +ZI) ; 起始地址 0x1FFF8018,避免重叠
.ANY (RAMCODE)
}
RW_IRAMB 0x200F0000 0x00010000 { ; 另一块 RAM(按工程实际配置)
.ANY (+RW +ZI)
}
}

关于 noinit 区大小的几点说明:

  • 0x00000018 = 24 字节,可存 6 个 uint32_t:目前用了 3 个(boot_flag / app_msp / app_addr),剩余可预留(如 APP 版本号、固件长度、校验和);

  • 最小对齐单位是 8 字节,所以 0x00000014 不行;

  • 两个红框地址的对应关系:noinit 区占用 [0x1FFF8000, 0x1FFF8018),因此 RW_IRAM2 的起始地址必须是 0x1FFF8018,正好衔接且不重叠;

  • boot 工程与 APP 工程都要在各自的 sct 中加上 RW_IRAM1 这一段(两边共用同一份 system_hc32f460.c 时,APP 工程不加会报 .bss.noinit 段无处安放的链接错误)。