Skip to content

Commit ccb5b04

Browse files
authored
Merge pull request #97 from oresat/f0-bootloader
F0 bootloader overhaul
2 parents 4b3ac5d + 2d0ecd2 commit ccb5b04

20 files changed

+1181
-879
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ build*/
8282

8383
# Vagrant
8484
/.vagrant/
85+
86+
# Python
87+
__pycache__
88+
.direnv

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ how to install these tools, as it varies between systems.
7777
Tools required:
7878
* make
7979
* arm-none-eabi-gcc
80-
* arm-none-eabi-gdb (may require a symlink to gdb-multiarch on Debian systems)
80+
* gdb-multiarch
8181
* openocd (Required, this is how we program and debug things)
8282
* stlink (Gets udev rules up and running, may be helpful)
8383
* srecord

common/can_bootloader.c

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ uint8_t m0_firmware_temp_buffer[M0_FIRMWARE_UPDATE_WRITE_CHUNK_SIZE];
2424
#endif
2525

2626

27+
#define ORESAT_F0_FLASH_START_ADDRESS 0x08000000
28+
#define ORESAT_F0_FIRMWARE_CRC_ADDRESS 0x0800A7F4
29+
#define ORESAT_F0_FIRMWARE_CODE_ADDRESS 0x0800A804
30+
2731
/**
2832
* Used to fully initialize a can_bootloader_config_t structure.
2933
*

common/include/oresat_f0.h

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
#ifndef COMMON_INCLUDE_ORESAT_F0_H_
22
#define COMMON_INCLUDE_ORESAT_F0_H_
33

4-
/* TODO: It'd be nice to derive these values from the linker script as the authoratative source */
5-
#define ORESAT_F0_FLASH_START_ADDRESS 0x08000000
6-
7-
#define ORESAT_F0_FIRMWARE_CRC_ADDRESS 0x0800A000
8-
#define ORESAT_F0_FIRMWARE_LENGTH_ADDRESS (ORESAT_F0_FIRMWARE_CRC_ADDRESS + 0x4)
9-
#define ORESAT_F0_FIRMWARE_VERSION_ADDRESS (ORESAT_F0_FIRMWARE_CRC_ADDRESS + 0x8)
10-
#define ORESAT_F0_FIRMWARE_CODE_ADDRESS (ORESAT_F0_FIRMWARE_CRC_ADDRESS + 0x400)
11-
#define ORESAT_F0_FIRMWARE_CODE_END_ADDRESS 0x0803C000
12-
13-
#define ORESAT_F0_FIRMWARE_MAXIMUM_LENGTH (ORESAT_F0_FIRMWARE_CODE_END_ADDRESS - ORESAT_F0_FIRMWARE_CRC_ADDRESS)
14-
15-
164
#ifdef __cplusplus
175
extern "C" {
186
#endif
197

20-
extern int __flash0_base__;
21-
extern size_t __flash0_size__;
22-
extern int __flash0_end__;
23-
extern int __flash1_base__;
24-
extern size_t __flash1_size__;
25-
extern int __flash1_end__;
8+
// See STM32F091xC-bootloader.ld/STM32F4091xC-app.ld for definitions and
9+
// rules_memory.ld for declarations.
10+
extern uint8_t __flash0_base__[];
11+
extern uint8_t __flash0_end__[];
12+
extern uint8_t __flash1_base__[];
13+
extern uint8_t __flash1_end__[];
14+
extern uint8_t __flash2_base__[];
15+
extern uint8_t __flash2_end__[];
16+
extern uint8_t __ram0_base__[];
17+
extern uint8_t __ram0_end__[];
18+
extern uint8_t __ram1_base__[];
19+
extern uint8_t __ram1_end__[];
20+
21+
// The __*_size__ symbols are weird. The contents aren't relevent, it's the
22+
// address, converted to a number, which is the represented region size.
23+
extern const void __flash0_size__;
24+
extern const void __flash1_size__;
25+
extern const void __flash2_size__;
26+
extern const void __ram0_size__;
27+
extern const void __ram1_size__;
28+
#define FLASH0_SIZE ((const uint32_t)(&__flash0_size__))
29+
#define FLASH1_SIZE ((const uint32_t)(&__flash1_size__))
30+
#define FLASH2_SIZE ((const uint32_t)(&__flash2_size__))
31+
#define RAM0_SIZE ((const uint32_t)(&__ram0_size__))
32+
#define RAM1_SIZE ((const uint32_t)(&__ram1_size__))
2633

2734
#ifdef __cplusplus
2835
}

common/oresat_f0.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55

66
/**
7-
* This function should be called on applications using the STM32F091xC-app.ld linker script and using the bootloader to run them.
7+
* This function is called by applications using the STM32F091xC-app.ld
8+
* linker script and using the bootloader to run them. It overwrites a weak
9+
* symbol from ChibiOS's CRT0 and so will be called late in the init sequence.
810
*/
911
void __late_init(void) {
10-
/* Relocate by software the vector table to the internal SRAM at 0x20000000 ***/
11-
/* Copy the vector table from the Flash (mapped at the base of the application
12-
load address 0x08003000) to the base address of the SRAM at 0x20000000. */
12+
// Relocate by software the vector table to the internal SRAM at 0x20000000.
13+
// The vector table comes from flash, mapped at the base of the application
14+
// region, load address __flash2_base__ (0x0800A800).
15+
//
16+
// The SRAM then gets remapped to 0x00000000 to function as the real vector
17+
// table
1318

14-
memcpy((void *)0x20000000, (void *)ORESAT_F0_FIRMWARE_CODE_ADDRESS, 0xC0);
19+
memcpy(__ram0_base__, __flash2_base__, 0xC0);
1520
__DSB();
1621
/* Remap SRAM at 0x00000000 */
1722
SYSCFG->CFGR1 |= SYSCFG_CFGR1_MEM_MODE;

doc/toolchain.md

-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ The packages needed for Debian are as follow:
3737
libusb-1.0-0 libusb-1.0-0-dev make pkg-config python3 srecord
3838
stlink-tools tcl xxd`
3939

40-
Make a symbollic link to `arm-none-eabi-gdb`:
41-
42-
`$ sudo ln -s /usr/bin/gdb-multiarch /usr/bin/arm-none-eabi-gdb`
43-
4440
OpenOCD can be built as follow:
4541

4642
- `$ git clone https://git.code.sf.net/p/openocd/code openocd`

ld/STM32F091xC-app.ld

+17-35
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
/*
2-
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
17-
/*
18-
* STM32F091xC memory setup.
19-
*/
1+
/* STM32F091xC memory setup. It has 256k flash (128 2k pages) and 32k SRAM */
202
MEMORY
213
{
224
flash0 (rx) : org = 0x08000000, len = 40k /* Bootloader */
23-
flash1 (rx) : org = 0x0800A400, len = 199k /* Application */
24-
flash2 (rx) : org = 0x00000000, len = 0
5+
flash1 (rx) : org = 0x0800A000, len = 2k /* Metadata */
6+
flash2 (rx) : org = 0x0800A800, len = 214k /* Application */
257
flash3 (rx) : org = 0x00000000, len = 0
268
flash4 (rx) : org = 0x00000000, len = 0
279
flash5 (rx) : org = 0x00000000, len = 0
2810
flash6 (rx) : org = 0x00000000, len = 0
2911
flash7 (rx) : org = 0x00000000, len = 0
30-
ram0 (wx) : org = 0x20000000, len = 32k
31-
ram1 (wx) : org = 0x20000400, len = 31k
12+
ram0 (wx) : org = 0x20000000, len = 32708 /* 32k - 4 (bootloader magic) */
13+
ram1 (wx) : org = 0x20000400, len = 31k /* 32k - 1k (fw vector table) */
3214
ram2 (wx) : org = 0x00000000, len = 0
3315
ram3 (wx) : org = 0x00000000, len = 0
3416
ram4 (wx) : org = 0x00000000, len = 0
@@ -41,27 +23,27 @@ MEMORY
4123
and a load region (_LMA suffix).*/
4224

4325
/* Flash region to be used for exception vectors.*/
44-
REGION_ALIAS("VECTORS_FLASH", flash1);
45-
REGION_ALIAS("VECTORS_FLASH_LMA", flash1);
26+
REGION_ALIAS("VECTORS_FLASH", flash2);
27+
REGION_ALIAS("VECTORS_FLASH_LMA", flash2);
4628

4729
/* Flash region to be used for constructors and destructors.*/
48-
REGION_ALIAS("XTORS_FLASH", flash1);
49-
REGION_ALIAS("XTORS_FLASH_LMA", flash1);
30+
REGION_ALIAS("XTORS_FLASH", flash2);
31+
REGION_ALIAS("XTORS_FLASH_LMA", flash2);
5032

5133
/* Flash region to be used for code text.*/
52-
REGION_ALIAS("TEXT_FLASH", flash1);
53-
REGION_ALIAS("TEXT_FLASH_LMA", flash1);
34+
REGION_ALIAS("TEXT_FLASH", flash2);
35+
REGION_ALIAS("TEXT_FLASH_LMA", flash2);
5436

5537
/* Flash region to be used for read only data.*/
56-
REGION_ALIAS("RODATA_FLASH", flash1);
57-
REGION_ALIAS("RODATA_FLASH_LMA", flash1);
38+
REGION_ALIAS("RODATA_FLASH", flash2);
39+
REGION_ALIAS("RODATA_FLASH_LMA", flash2);
5840

5941
/* Flash region to be used for various.*/
60-
REGION_ALIAS("VARIOUS_FLASH", flash1);
61-
REGION_ALIAS("VARIOUS_FLASH_LMA", flash1);
42+
REGION_ALIAS("VARIOUS_FLASH", flash2);
43+
REGION_ALIAS("VARIOUS_FLASH_LMA", flash2);
6244

6345
/* Flash region to be used for RAM(n) initialization data.*/
64-
REGION_ALIAS("RAM_INIT_FLASH_LMA", flash1);
46+
REGION_ALIAS("RAM_INIT_FLASH_LMA", flash2);
6547

6648
/* RAM region to be used for Main stack. This stack accommodates the processing
6749
of all exceptions and interrupts.*/
@@ -73,7 +55,7 @@ REGION_ALIAS("PROCESS_STACK_RAM", ram1);
7355

7456
/* RAM region to be used for data segment.*/
7557
REGION_ALIAS("DATA_RAM", ram1);
76-
REGION_ALIAS("DATA_RAM_LMA", flash1);
58+
REGION_ALIAS("DATA_RAM_LMA", flash2);
7759

7860
/* RAM region to be used for BSS segment.*/
7961
REGION_ALIAS("BSS_RAM", ram1);

ld/STM32F091xC-bootloader.ld

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
/*
2-
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
17-
/*
18-
* STM32F091xC memory setup.
19-
*/
1+
/* STM32F091xC memory setup. It has 256k flash (128 2k pages) and 32k SRAM */
202
MEMORY
213
{
224
flash0 (rx) : org = 0x08000000, len = 40k /* Bootloader */
23-
flash1 (rx) : org = 0x0800A400, len = 199k /* Application */
24-
flash2 (rx) : org = 0x00000000, len = 0
5+
flash1 (rx) : org = 0x0800A000, len = 2k /* Metadata */
6+
flash2 (rx) : org = 0x0800A800, len = 214k /* Application */
257
flash3 (rx) : org = 0x00000000, len = 0
268
flash4 (rx) : org = 0x00000000, len = 0
279
flash5 (rx) : org = 0x00000000, len = 0
2810
flash6 (rx) : org = 0x00000000, len = 0
2911
flash7 (rx) : org = 0x00000000, len = 0
30-
ram0 (wx) : org = 0x20000000, len = 32k
31-
ram1 (wx) : org = 0x20000400, len = 31k
12+
ram0 (wx) : org = 0x20000000, len = 32708 /* 32k - 4 for bootloader magic */
13+
ram1 (wx) : org = 0x20000400, len = 31k /* 32k - 1k for vector table */
3214
ram2 (wx) : org = 0x00000000, len = 0
3315
ram3 (wx) : org = 0x00000000, len = 0
3416
ram4 (wx) : org = 0x00000000, len = 0

src/f0/app_bootloader/Makefile

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Compiler options here.
77
ifeq ($(USE_OPT),)
8-
USE_OPT = -Og -ggdb -fomit-frame-pointer -falign-functions=16
8+
USE_OPT = -Og -ggdb -falign-functions=16
99
endif
1010

1111
# C specific options here (added to USE_OPT).
@@ -106,8 +106,6 @@ DEPDIR := $(APP_ROOT)/.dep
106106
BOARDDIR = $(PROJ_ROOT)/boards/$(BOARD)
107107

108108
# Project specific files.
109-
#include $(PROJ_SRC)/oresat.mk
110-
include $(PROJ_SRC)/bootloader.mk
111109
include $(PROJ_SRC)/util.mk
112110

113111
# Licensing files.
@@ -125,9 +123,6 @@ include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v6m.mk
125123
# Auto-build files in ./source recursively.
126124
include $(CHIBIOS)/tools/mk/autobuild.mk
127125
# Other files (optional).
128-
#include $(CHIBIOS)/test/lib/test.mk
129-
#include $(CHIBIOS)/test/rt/rt_test.mk
130-
#include $(CHIBIOS)/test/oslib/oslib_test.mk
131126
include $(CHIBIOS)/os/hal/lib/streams/streams.mk
132127

133128
# Define linker script file here

0 commit comments

Comments
 (0)