我正在为运行在stm32上的应用程序构建引导加载程序。
这样做的目的是为了能够更新主应用程序。
由于我们的软件非常模块化,我的想法是只配置一个最小版本。所有的初始化都是相同的,它跳转到一个包含所有引导加载功能的
内部闪存的存储布局如下:
引导加载程序主要看起来是这样的。
这几乎可以工作 - “真正”的应用程序被调用,进行初始化,但最终因为未知原因而崩溃。 有趣的是,引导加载程序的故障ISR(
剩下的部分是共享的。
由于我们的软件非常模块化,我的想法是只配置一个最小版本。所有的初始化都是相同的,它跳转到一个包含所有引导加载功能的
main
函数(检查外部闪存上是否有新的固件,如果是,则将其写入内部闪存),最后跳转到实际的应用程序 - 它再次进行所有初始化,但这次带有额外的外设等,最终调用真正的main
。内部闪存的存储布局如下:
|0x08000000 boot loader
|----------------------
|0x08006000 application
引导加载程序主要看起来是这样的。
extern void CallApplication(void);
int main(void) {
printf("starting bootloader\n");
printf("will jump to " TOSTRING(APP_START_ADDRESS) "\n");
CallApplication();
return 0;
}
其中CallApplication
是用汇编语言编写的
#define VTABLE_START_ADDRESS APP_START_ADDRESS
#define NVIC_VTABLE 0xE000ED08 // Vector Table Offset
.globl CallApplication
.thumb_func
CallApplication:
// Set the application's vector table start address.
movw r0, #(VTABLE_START_ADDRESS & 0xffff)
movt r0, #(VTABLE_START_ADDRESS >> 16)
movw r1, #(NVIC_VTABLE & 0xffff)
movt r1, #(NVIC_VTABLE >> 16)
str r0, [r1]
// Load the stack pointer from the application's vector table.
ldr sp, [r0]
// Load the initial PC from the application's vector table and branch to
// the application's entry point.
ldr r0, [r0, #4]
bx r0
这几乎可以工作 - “真正”的应用程序被调用,进行初始化,但最终因为未知原因而崩溃。 有趣的是,引导加载程序的故障ISR(
0x080022ae
)被调用,而不是真正应用程序的ISR(> 0x08006000
),因此显然设置新向量表的某些内容失败了。2016-02-11 00:21:16,958 - INFO # init UART
2016-02-11 00:21:16,963 - INFO # Application: boot_loader
2016-02-11 00:21:16,973 - INFO # -- init done, starting main --
2016-02-11 00:21:16,974 - INFO # starting bootloader
2016-02-11 00:21:16,976 - INFO # will jump to 0x8006000
2016-02-11 00:21:16,978 - INFO # init UART
2016-02-11 00:21:16,985 - INFO # Application: hello_world
2016-02-11 00:21:17,797 - INFO # -- init done, starting main --
(hard fault led starts flashing)
我在这里缺失什么?
主应用程序的链接脚本定义了:
MEMORY
{
FLASH (rx) : ORIGIN = 0x08006000, LENGTH = 488K
SRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}
引导加载程序所做的是
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 24K
SRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}
剩下的部分是共享的。
SECTIONS
{
.text :
{
_text = .;
/*
* The vector table must be placed to the top of the
* memory map. To achieve this, it was assigned to a
* special section called ".isr_vector"
*/
KEEP(*(.isr_vector))
/* followed by .text and .rodata: */
*(.text*)
*(.rodata*)
_etext = .;
} > FLASH
/* Just to make sure that the contents does not exceed the flash size */
. = ORIGIN(FLASH) + LENGTH(FLASH);
/*
* .data and .bss are placed into SRAM:
*/
.data : AT(ADDR(.text) + SIZEOF(.text))
{
_data = .;
*(.data*)
_edata = .;
} > SRAM
.bss :
{
/* _bss and _ebss will be required during initialization */
_bss = .;
*(.bss*)
_ebss = .;
} > SRAM
.aux : {
. = ALIGN(4);
*(.auxdata) /* .auxdata section */
. = ALIGN(4);
} > SRAM
/* Just to make sure that the contents does not exceed the SRAM size */
. = ORIGIN(SRAM) + LENGTH(SRAM);
}
编辑:我重新编写了关于在C中设置VTOR
的部分,这让我更清楚地理解发生了什么,但我仍然最终进入了启动加载程序的DefaultISR。
printf("starting bootloader\n");
printf("will jump to " TOSTRING(APP_START_ADDRESS) "\n");
printf("before: %x\n", SCB->VTOR);
SCB->VTOR += APP_START_ADDRESS;
printf("after: %x\n", SCB->VTOR);
asm volatile("mov r0, #0x6000");
asm volatile("ldr sp, [r0]");
asm volatile("ldr r0, [r0, #4]");
asm volatile("bx r0");
输出
2016-02-11 23:49:31,833 - INFO # starting bootloader
2016-02-11 23:49:31,835 - INFO # will jump to 0x6000
2016-02-11 23:49:31,836 - INFO # before: 8000000
2016-02-11 23:49:31,837 - INFO # after: 8006000
2016-02-11 23:49:31,839 - INFO # init UART
2016-02-11 23:49:31,841 - INFO # …
__set_BASEPRI(0)
启用中断后,程序立即崩溃。 - user1273684VTOR
。很抱歉,我无法再帮助您了。只有一个信息:这不是STM的错!对我来说它可以工作。 - too honest for this siteSCB->VTOR
方面缺少了什么,现在我正在将我的偏移量添加到其中,但旧位置仍然被用来查找要跳转到哪个ISR:( - user1273684