Skip to content

Commit

Permalink
Add bootloader for A/B update support
Browse files Browse the repository at this point in the history
  • Loading branch information
mickdermack authored and CommanderRedYT committed Mar 16, 2022
1 parent 5ebd6e6 commit 24d1649
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 23 deletions.
72 changes: 56 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ set(CMAKE_CXX_FLAGS "${COMMON_FLAGS}")
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

set(COMMON_LINKER_FLAGS "-specs=nosys.specs -T${CMAKE_SOURCE_DIR}/STM32F103RCTx_FLASH.ld -lc -lm -lnosys -lstdc++ -Wl,--gc-sections -Wl,-Map=${CMAKE_BINARY_DIR}/hover.map,--cref")
set(CMAKE_EXE_LINKER_FLAGS "${COMMON_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_LINKER_FLAGS}")
set(A_FIRMWARE_LINKER_FLAGS "-T${CMAKE_SOURCE_DIR}/STM32F103RCTx_FLASH_a.ld")
set(B_FIRMWARE_LINKER_FLAGS "-T${CMAKE_SOURCE_DIR}/STM32F103RCTx_FLASH_b.ld")
set(AB_BOOT_FIRMWARE_LINKER_FLAGS "-T${CMAKE_SOURCE_DIR}/ab_boot/STM32F103RCTx_FLASH_boot.ld")
set(COMMON_LINKER_FLAGS -specs=nosys.specs -lc -lm -lnosys -lstdc++ -Wl,--gc-sections "-Wl,-Map=${CMAKE_BINARY_DIR}/hover.map,--cref")

set(A_FIRMWARE_APP_BASE 0x08002000)
set(B_FIRMWARE_APP_BASE 0x08020800)

get_property(IS_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
if(IS_IN_TRY_COMPILE)
set(CMAKE_EXE_LINKER_FLAGS ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
list(JOIN CMAKE_EXE_LINKER_FLAGS " " CMAKE_EXE_LINKER_FLAGS)
set(CMAKE_SHARED_LINKER_FLAGS ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
list(JOIN CMAKE_SHARED_LINKER_FLAGS " " CMAKE_SHARED_LINKER_FLAGS)
endif(IS_IN_TRY_COMPILE)

project(bobbycar-controller-firmware ASM C CXX)

Expand Down Expand Up @@ -52,7 +64,6 @@ add_library(stm32_hal STATIC
STM32CubeF1/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c
STM32CubeF1/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_can.c
startup_stm32f103xe.s
system_stm32f1xx.c
)

add_library(emanuel_foc_model STATIC
Expand All @@ -66,12 +77,18 @@ add_library(bobbycar-protocol INTERFACE
)



set(COMMON_SOURCES
config.h
defines.h
main.cpp
system_stm32f1xx.c
)

#
# motor test
#
add_executable(motortest.elf config.h defines.h main.cpp)
add_executable(motortest.elf ${COMMON_SOURCES})
target_link_options(motortest.elf PRIVATE ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
target_link_libraries(motortest.elf stm32_hal emanuel_foc_model bobbycar-protocol)
target_compile_options(motortest.elf PRIVATE
-DMOTOR_TEST
Expand All @@ -88,16 +105,19 @@ target_compile_options(motortest.elf PRIVATE
# -DFEATURE_CAN
# -DCAN_LOG_UNKNOWN_ADDR
# -DIS_BACK

-DVECT_TAB_OFFSET=${A_FIRMWARE_APP_BASE}
)
add_custom_command(OUTPUT motortest.hex COMMAND arm-none-eabi-objcopy -O ihex motortest.elf motortest.hex DEPENDS motortest.elf)
add_custom_command(OUTPUT motortest.bin COMMAND arm-none-eabi-objcopy -O binary -S motortest.elf motortest.bin DEPENDS motortest.elf)
add_custom_target(motortest ALL SOURCES motortest.hex motortest.bin)
add_custom_target(flash-motortest COMMAND st-flash --reset write motortest.bin 0x8000000 SOURCES motortest.bin DEPENDS motortest.bin)
add_custom_target(flash-motortest COMMAND st-flash --reset write motortest.bin ${A_FIRMWARE_APP_BASE} SOURCES motortest.bin DEPENDS motortest.bin)

#
# motor test peter
#
add_executable(motortest_peter.elf config.h defines.h main.cpp)
add_executable(motortest_peter.elf ${COMMON_SOURCES})
target_link_options(motortest_peter.elf PRIVATE ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
target_link_libraries(motortest_peter.elf stm32_hal emanuel_foc_model bobbycar-protocol)
target_compile_options(motortest_peter.elf PRIVATE
#-DMOTOR_TEST
Expand All @@ -115,17 +135,20 @@ target_compile_options(motortest_peter.elf PRIVATE
-DFEATURE_CAN
# -DCAN_LOG_UNKNOWN_ADDR
-DIS_BACK

-DVECT_TAB_OFFSET=${A_FIRMWARE_APP_BASE}
)
add_custom_command(OUTPUT motortest_peter.hex COMMAND arm-none-eabi-objcopy -O ihex motortest_peter.elf motortest_peter.hex DEPENDS motortest_peter.elf)
add_custom_command(OUTPUT motortest_peter.bin COMMAND arm-none-eabi-objcopy -O binary -S motortest_peter.elf motortest_peter.bin DEPENDS motortest_peter.elf)
add_custom_target(motortest_peter ALL SOURCES motortest_peter.hex motortest_peter.bin)
add_custom_target(flash-motortest_peter COMMAND st-flash --reset write motortest_peter.bin 0x8000000 SOURCES motortest_peter.bin DEPENDS motortest_peter.bin)
add_custom_target(flash-motortest_peter COMMAND st-flash --reset write motortest_peter.bin ${A_FIRMWARE_APP_BASE} SOURCES motortest_peter.bin DEPENDS motortest_peter.bin)


#
# feedc0de front
#
add_executable(feedcode-front.elf config.h defines.h main.cpp)
add_executable(feedcode-front.elf ${COMMON_SOURCES})
target_link_options(feedcode-front.elf PRIVATE ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
target_link_libraries(feedcode-front.elf stm32_hal emanuel_foc_model bobbycar-protocol)
target_compile_options(feedcode-front.elf PRIVATE
# -DMOTOR_TEST
Expand All @@ -142,16 +165,19 @@ target_compile_options(feedcode-front.elf PRIVATE
-DFEATURE_CAN
# -DCAN_LOG_UNKNOWN_ADDR
# -DIS_BACK

-DVECT_TAB_OFFSET=${A_FIRMWARE_APP_BASE}
)
add_custom_command(OUTPUT feedcode-front.hex COMMAND arm-none-eabi-objcopy -O ihex feedcode-front.elf feedcode-front.hex DEPENDS feedcode-front.elf)
add_custom_command(OUTPUT feedcode-front.bin COMMAND arm-none-eabi-objcopy -O binary -S feedcode-front.elf feedcode-front.bin DEPENDS feedcode-front.elf)
add_custom_target(feedcode-front ALL SOURCES feedcode-front.hex feedcode-front.bin)
add_custom_target(flash-feedcode-front COMMAND st-flash --reset write feedcode-front.bin 0x8000000 SOURCES feedcode-front.bin DEPENDS feedcode-front.bin)
add_custom_target(flash-feedcode-front COMMAND st-flash --reset write feedcode-front.bin ${A_FIRMWARE_APP_BASE} SOURCES feedcode-front.bin DEPENDS feedcode-front.bin)

#
# feedc0de back
#
add_executable(feedcode-back.elf config.h defines.h main.cpp)
add_executable(feedcode-back.elf ${COMMON_SOURCES})
target_link_options(feedcode-back.elf PRIVATE ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
target_link_libraries(feedcode-back.elf stm32_hal emanuel_foc_model bobbycar-protocol)
target_compile_options(feedcode-back.elf PRIVATE
# -DMOTOR_TEST
Expand All @@ -168,16 +194,19 @@ target_compile_options(feedcode-back.elf PRIVATE
-DFEATURE_CAN
# -DCAN_LOG_UNKNOWN_ADDR
-DIS_BACK

-DVECT_TAB_OFFSET=${A_FIRMWARE_APP_BASE}
)
add_custom_command(OUTPUT feedcode-back.hex COMMAND arm-none-eabi-objcopy -O ihex feedcode-back.elf feedcode-back.hex DEPENDS feedcode-back.elf)
add_custom_command(OUTPUT feedcode-back.bin COMMAND arm-none-eabi-objcopy -O binary -S feedcode-back.elf feedcode-back.bin DEPENDS feedcode-back.elf)
add_custom_target(feedcode-back ALL SOURCES feedcode-back.hex feedcode-back.bin)
add_custom_target(flash-feedcode-back COMMAND st-flash --reset write feedcode-back.bin 0x8000000 SOURCES feedcode-back.bin DEPENDS feedcode-back.bin)
add_custom_target(flash-feedcode-back COMMAND st-flash --reset write feedcode-back.bin ${A_FIRMWARE_APP_BASE} SOURCES feedcode-back.bin DEPENDS feedcode-back.bin)

#
# greyhash
#
add_executable(greyhash.elf config.h defines.h main.cpp)
add_executable(greyhash.elf ${COMMON_SOURCES})
target_link_options(greyhash.elf PRIVATE ${COMMON_LINKER_FLAGS} ${A_FIRMWARE_LINKER_FLAGS})
target_link_libraries(greyhash.elf stm32_hal emanuel_foc_model bobbycar-protocol)
target_compile_options(greyhash.elf PRIVATE
# -DMOTOR_TEST
Expand All @@ -194,14 +223,25 @@ target_compile_options(greyhash.elf PRIVATE
# -DFEATURE_CAN
# -DCAN_LOG_UNKNOWN_ADDR
# -DIS_BACK

-DVECT_TAB_OFFSET=${A_FIRMWARE_APP_BASE}
)
add_custom_command(OUTPUT greyhash.hex COMMAND arm-none-eabi-objcopy -O ihex greyhash.elf greyhash.hex DEPENDS greyhash.elf)
add_custom_command(OUTPUT greyhash.bin COMMAND arm-none-eabi-objcopy -O binary -S greyhash.elf greyhash.bin DEPENDS greyhash.elf)
add_custom_target(greyhash ALL SOURCES greyhash.hex greyhash.bin)
add_custom_target(flash-greyhash COMMAND st-flash --reset write greyhash.bin 0x8000000 SOURCES greyhash.bin DEPENDS greyhash.bin)

add_custom_target(flash-greyhash COMMAND st-flash --reset write greyhash.bin ${A_FIRMWARE_APP_BASE} SOURCES greyhash.bin DEPENDS greyhash.bin)


add_executable(ab_boot.elf ab_boot/ab_boot.c system_stm32f1xx.c)
target_link_options(ab_boot.elf PRIVATE ${COMMON_LINKER_FLAGS} ${AB_BOOT_FIRMWARE_LINKER_FLAGS})
target_link_libraries(ab_boot.elf stm32_hal)
target_compile_options(ab_boot.elf PRIVATE
-DVECT_TAB_OFFSET=0x00000000
)
add_custom_command(OUTPUT ab_boot.hex COMMAND arm-none-eabi-objcopy -O ihex ab_boot.elf ab_boot.hex DEPENDS ab_boot.elf)
add_custom_command(OUTPUT ab_boot.bin COMMAND arm-none-eabi-objcopy -O binary -S ab_boot.elf ab_boot.bin DEPENDS ab_boot.elf)
add_custom_target(ab_boot ALL SOURCES ab_boot.hex ab_boot.bin)
add_custom_target(flash-boot COMMAND st-flash --reset write ab_boot.bin 0x08000000 SOURCES ab_boot.bin DEPENDS ab_boot.bin)


# util targets
Expand Down
168 changes: 168 additions & 0 deletions STM32F103RCTx_FLASH_a.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
*****************************************************************************
**

** File : LinkerScript.ld
**
** Abstract : Linker script for STM32F103RCTx Device with
** 256KByte FLASH, 48KByte RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : STMicroelectronics STM32
**
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
** (c)Copyright Ac6.
** You may use this file as-is or modify it according to the needs of your
** project. Distribution of this file (unmodified or modified) is not
** permitted. Ac6 permit registered System Workbench for MCU users the
** rights to distribute the assembled, compiled & linked contents of this
** file as part of an application binary file, provided that it is built
** using the System Workbench for MCU toolchain.
**
*****************************************************************************
*/

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x2000C000; /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K
FLASH (rx) : ORIGIN = 0x08002000, LENGTH = 122K
CONFIG (r) : ORIGIN = 0x0803f800, LENGTH = 2K
}

/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH

/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)

KEEP (*(.init))
KEEP (*(.fini))

. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH

/* Constant data goes into FLASH */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH

.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH

.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH

/* used by the startup to initialize data */
_sidata = LOADADDR(.data);

/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */

. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH


/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)

. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM

/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM



/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}

.ARM.attributes 0 : { *(.ARM.attributes) }
}
Loading

0 comments on commit 24d1649

Please sign in to comment.