From 44f0df51f70021574355d9a64001e82e4556b949 Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 2 Jun 2017 12:33:45 -0400 Subject: [PATCH] bootloader --- firmware/Makefile | 10 ++++ firmware/bootloader/compile_bootloader.bat | 20 ++++++++ firmware/bootloader/src/dfu.h | 55 ++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 firmware/bootloader/compile_bootloader.bat create mode 100644 firmware/bootloader/src/dfu.h diff --git a/firmware/Makefile b/firmware/Makefile index 5b220d9ad7..7785798352 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -58,6 +58,10 @@ ifeq ($(USE_SMART_BUILD),) USE_SMART_BUILD = no endif +ifeq ($(USE_BOOTLOADER),) + USE_BOOTLOADER = no +endif + # # Build global options ############################################################################## @@ -137,6 +141,10 @@ include $(PROJECT_DIR)/controllers/system/system.mk include $(PROJECT_DIR)/controllers/trigger/trigger.mk include $(PROJECT_DIR)/console/console.mk +ifeq ($(USE_BOOTLOADER),yes) +include $(PROJECT_DIR)/bootloader/bootloader.mk +endif + # Define linker script file here ifeq ($(LDSCRIPT),) LDSCRIPT= config/stm32f4ems/STM32F407xG_CCM.ld @@ -151,6 +159,7 @@ CSRC = $(STARTUPSRC) \ $(HALSRC) \ $(PLATFORMSRC) \ $(BOARDSRC) \ + $(BOOTLOADERSRC) \ $(CHIBIOS)/os/various/syscalls.c \ $(CHIBIOS)/os/hal/lib/streams/memstreams.c \ $(CHIBIOS)/os/hal/lib/streams/chprintf.c \ @@ -222,6 +231,7 @@ INCDIR = $(PORTINC) \ $(HALINC) \ $(PLATFORMINC) \ $(BOARDINC) \ + $(BOOTLOADERINC) \ $(CHCPPINC) \ $(CHIBIOS)/os/hal/lib/streams \ $(CHIBIOS)/os/various \ diff --git a/firmware/bootloader/compile_bootloader.bat b/firmware/bootloader/compile_bootloader.bat new file mode 100644 index 0000000000..f24f43bb9a --- /dev/null +++ b/firmware/bootloader/compile_bootloader.bat @@ -0,0 +1,20 @@ +@echo off + +rem Needed for board overrides +IF "%BOOTLOADER_CODE_PATH%"=="" (SET BOOTLOADER_CODE_PATH="..") + +echo Starting compilation +make -f src/Makefile %1 %2 %3 + +if errorlevel 1 echo make compilation failed +if errorlevel 1 exit -1 + +echo Build complete success. + +cd blbuild +rem Generate a header file with binary bootloader code +java -jar ../../../java_tools/bin2header.jar bootloader.bin %BOOTLOADER_CODE_PATH%/bootloader_generated.hxx "static const volatile uint8_t bootloader_code[] BOOTLOADER_SECTION" +cd .. + +rem Touch 'bootloader_storage.c' to update its modification date (needed for make) +copy /b bootloader_storage.c +,, diff --git a/firmware/bootloader/src/dfu.h b/firmware/bootloader/src/dfu.h new file mode 100644 index 0000000000..f47347e535 --- /dev/null +++ b/firmware/bootloader/src/dfu.h @@ -0,0 +1,55 @@ +#ifndef DFU_H_ +#define DFU_H_ + +#include "tunerstudio_io.h" + +// This is where the bootloader starts +#define BOOTLOADER_ADDR 0x08000000 +// Bootloader code max. size, in bytes +#define BOOTLOADER_SIZE 0x4000 +// Number of sectors for the bootloader +#define BOOTLOADER_NUM_SECTORS (BOOTLOADER_SIZE/0x4000) + +// This is where the application starts +#define APPLICATION_ADDR 0x08008000 + +#define DFU_BUFFER_SIZE 258 // Max. 256 bytes at a time plus 2 bytes (numBytes+checksum) + + +// DFU/USART Protocol is described in AN3155 document "Application note. USART protocol used in the STM32 bootloader" +// http://www.st.com/resource/en/application_note/cd00264342.pdf + +#define DFU_UART_CHECK 0x7F // "UART handshake" escape byte + +#define DFU_GET_LIST_CMD 0x00 // "Get supported commands list" command +#define DFU_DEVICE_ID_CMD 0x02 // "Get device ID" command +#define DFU_READ_CMD 0x11 // "Read memory" command +#define DFU_GO_CMD 0x21 // "Go" command +#define DFU_WRITE_CMD 0x31 // "Write memory" command +#define DFU_ERASE_CMD 0x44 // "Erase memory" command + +#define DFU_VERSION_NUMBER 0x31 // The DFU protocol version number +#define DFU_ACK_BYTE 0x79 // Acknowledge byte ID +#define DFU_NACK_BYTE 0x1F // Not-Acknowledge byte ID + +#define DFU_SR5_TIMEOUT_FIRST MS2ST(100) +#define DFU_SR5_TIMEOUT_NORMAL MS2ST(1000) + +#define MCU_REVISION_MASK 0xfff // MCU Revision ID is needed by DFU protocol + +// The address in MCU system memory where the bootloader version number is stored (2 bytes) +#define DFU_BOOTLOADER_VERSION_ADDRESS 0x1FFF76DE + +/** + * @brief This function waits for the command to apply (write, read etc...) + */ +bool dfuStartLoop(void); +/** + * @brief Jump to the application + */ +void dfuJumpToApp(uint32_t addr); + +ts_channel_s *getTsChannel(); + + +#endif /* DFU_H_ */