Skip to content

Commit cac28b7

Browse files
committed
Allocated data section on flash for active image number
1 parent 1f1378e commit cac28b7

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

firmware/bootloader/main.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
/* Flash layout
1212
* ------------ 0x08000000
1313
* |bootloader |
14-
* | 16K |
14+
* |14K |
15+
* -------------0x08003800
16+
* |data |
17+
* |2K |
1518
* ------------ 0x08004000
1619
* |image1 |
1720
* |120K |
@@ -26,27 +29,50 @@
2629
#define VERSION "\r\nBootloader ver: " MAKE_STR(SW_VERSION_MAJOR) "." \
2730
MAKE_STR(SW_VERSION_MINOR) "." MAKE_STR(SW_VERSION_BUILD) "\r\n"
2831

29-
#define APP_ADDRESS_OFFSET 0x4000
30-
#define APP_ADDRESS (FLASH_BASE + APP_ADDRESS_OFFSET)
32+
#define APP1_ADDRESS_OFFSET 0x4000
33+
#define APP1_ADDRESS (FLASH_BASE + APP1_ADDRESS_OFFSET)
34+
#define APP2_ADDRESS_OFFSET 0x22000
35+
#define APP2_ADDRESS (FLASH_BASE + APP2_ADDRESS_OFFSET)
3136

3237
typedef void (*app_func_t)(void);
38+
typedef struct __attribute__((__packed__))
39+
{
40+
uint8_t active_image;
41+
} config_t;
42+
43+
__attribute__((__section__(".user_data"))) const char data[1];
3344

3445
int main()
3546
{
3647
app_func_t app;
37-
uint32_t jump_addr;
48+
uint32_t jump_addr, vt_offset, sp_addr;
49+
config_t *config = (config_t *)data;
3850

3951
uart_init();
4052
print(VERSION);
4153

54+
print("Start application: ");
55+
if (!config->active_image)
56+
{
57+
print("0\r\n");
58+
vt_offset = APP1_ADDRESS_OFFSET;
59+
jump_addr = *(__IO uint32_t *)(APP1_ADDRESS + 4);
60+
sp_addr = *(__IO uint32_t *)APP1_ADDRESS;
61+
}
62+
else
63+
{
64+
print("1\r\n");
65+
vt_offset = APP2_ADDRESS_OFFSET;
66+
jump_addr = *(__IO uint32_t *)(APP2_ADDRESS + 4);
67+
sp_addr = *(__IO uint32_t *)APP2_ADDRESS;
68+
}
69+
4270
/* Relocate vector table */
43-
NVIC_SetVectorTable(NVIC_VectTab_FLASH, APP_ADDRESS_OFFSET);
44-
/* Set application address */
45-
jump_addr = *(__IO uint32_t *)(APP_ADDRESS + 4);
46-
app = (app_func_t)jump_addr;
71+
NVIC_SetVectorTable(NVIC_VectTab_FLASH, vt_offset);
4772
/* Initialize application's Stack Pointer */
48-
__set_MSP(*(__IO uint32_t *)APP_ADDRESS);
73+
__set_MSP(sp_addr);
4974
/* Start application */
75+
app = (app_func_t)jump_addr;
5076
app();
5177

5278
return 0;

firmware/bootloader/stm32_flash.ld

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ _estack = 0x2000c000;
1515

1616
MEMORY
1717
{
18-
FLASH(rx) : ORIGIN = 0x08000000, LENGTH = 16K
18+
FLASH(rx) : ORIGIN = 0x08000000, LENGTH = 14K
19+
DATA(rw) : ORIGIN = 0x08003800, LENGTH = 2K
1920
RAM(xrw) : ORIGIN = 0x20000000, LENGTH = 48K
2021
}
2122

@@ -89,6 +90,13 @@ SECTIONS
8990
PROVIDE_HIDDEN(__fini_array_end = .);
9091
} > FLASH
9192

93+
.user_data :
94+
{
95+
. = ALIGN(4);
96+
KEEP(*(.user_data))
97+
. = ALIGN(4);
98+
} > DATA
99+
92100
/* Used by startup code to initialize data */
93101
_sidata = .;
94102

0 commit comments

Comments
 (0)