diff --git a/boards.txt b/boards.txt index 61c2cd8c69..dba9ece337 100644 --- a/boards.txt +++ b/boards.txt @@ -593,7 +593,7 @@ GenF1.build.board=GenF1 GenF1.build.mcu=cortex-m3 GenF1.build.series=STM32F1xx GenF1.build.cmsis_lib_gcc=arm_cortexM3l_math -GenF1.build.extra_flags=-D{build.product_line} {build.enable_usb} {build.xSerial} +GenF1.build.extra_flags=-D{build.product_line} {build.enable_usb} {build.xSerial} {build.bootloader_flags} # BLUEPILL_F103C6 board GenF1.menu.pnum.BLUEPILL_F103C6=BluePill F103C6 (32K) @@ -669,6 +669,22 @@ GenF1.menu.upload_method.bmpMethod=BMP (Black Magic Probe) GenF1.menu.upload_method.bmpMethod.upload.protocol=gdb_bmp GenF1.menu.upload_method.bmpMethod.upload.tool=bmp_upload +GenF1.menu.upload_method.dfu2Method=Maple DFU Bootloader 2.0 +GenF1.menu.upload_method.dfu2Method.upload.protocol=maple +GenF1.menu.upload_method.dfu2Method.upload.tool=maple_upload +GenF1.menu.upload_method.dfu2Method.upload.usbID=1EAF:0003 +GenF1.menu.upload_method.dfu2Method.upload.altID=2 +GenF1.menu.upload_method.dfu2Method.build.flash_offset=0x2000 +GenF1.menu.upload_method.dfu2Method.build.bootloader_flags=-DBL_LEGACY_LEAF -DVECT_TAB_OFFSET={build.flash_offset} + +GenF1.menu.upload_method.dfuoMethod=Maple DFU Bootloader original +GenF1.menu.upload_method.dfuoMethod.upload.protocol=maple +GenF1.menu.upload_method.dfuoMethod.upload.tool=maple_upload +GenF1.menu.upload_method.dfuoMethod.upload.usbID=1EAF:0003 +GenF1.menu.upload_method.dfuoMethod.upload.altID=1 +GenF1.menu.upload_method.dfuoMethod.build.flash_offset=0x5000 +GenF1.menu.upload_method.dfuoMethod.build.bootloader_flags=-DBL_LEGACY_LEAF -DVECT_TAB_OFFSET={build.flash_offset} + ################################################################################ # Generic F4 diff --git a/cores/arduino/hooks.c b/cores/arduino/hooks.c index 2d1bc5a491..a85150bca5 100644 --- a/cores/arduino/hooks.c +++ b/cores/arduino/hooks.c @@ -16,6 +16,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + /** * Empty yield() hook. * @@ -30,3 +32,21 @@ static void __empty() // Empty } void yield(void) __attribute__((weak, alias("__empty"))); + +#ifdef DTR_TOGGLING_SEQ +/** + * Empty dtr_toggling() hook. + * + * This function is intended to be used by library writers to build + * libraries or sketches that require DTR toggling feature. + * + * Its defined as a weak symbol and it can be redefined to implement + * task to achieve in this case. + */ +static void __empty_dtr_toggling(uint8_t *buf, uint32_t *len) +{ + (void)buf; + (void)len; +} +void dtr_togglingHook(uint8_t *buf, uint32_t *len) __attribute__((weak, alias("__empty_dtr_toggling"))); +#endif \ No newline at end of file diff --git a/cores/arduino/stm32/bootloader.c b/cores/arduino/stm32/bootloader.c new file mode 100644 index 0000000000..a5bb10c0b0 --- /dev/null +++ b/cores/arduino/stm32/bootloader.c @@ -0,0 +1,17 @@ +#include "bootloader.h" + +#include "stm32_def.h" + +#ifdef BL_LEGACY_LEAF +void dtr_togglingHook(uint8_t *buf, uint32_t *len) +{ + /** + * Four byte is the magic pack "1EAF" that puts the MCU into bootloader. + * Check if the incoming contains the string "1EAF". + * If yes, put the MCU into the bootloader mode. + */ + if ((*len >= 4) && (buf[0] == '1') && (buf[1] == 'E') && (buf[2] == 'A') && (buf[3] == 'F')) { + NVIC_SystemReset(); + } +} +#endif /* BL_LEGACY_LEAF */ diff --git a/cores/arduino/stm32/bootloader.h b/cores/arduino/stm32/bootloader.h new file mode 100644 index 0000000000..da3de5bc99 --- /dev/null +++ b/cores/arduino/stm32/bootloader.h @@ -0,0 +1,18 @@ +#ifndef _BOOTLOADER_H_ +#define _BOOTLOADER_H_ + +/* Ensure DTR_TOGGLING_SEQ enabled */ +#ifdef BL_LEGACY_LEAF +#ifndef DTR_TOGGLING_SEQ +#define DTR_TOGGLING_SEQ +#endif /* DTR_TOGGLING_SEQ */ +#endif /* BL_LEGACY_LEAF */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* _BOOTLOADER_H_ */ diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c index 041fb46a88..b4b1933a6a 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c @@ -23,6 +23,7 @@ /* Includes ------------------------------------------------------------------*/ #include "usbd_desc.h" #include "usbd_cdc_if.h" +#include "bootloader.h" #ifdef USE_USB_HS #define CDC_MAX_PACKET_SIZE USB_OTG_HS_MAX_PACKET_SIZE @@ -54,6 +55,11 @@ __IO uint32_t lineState = 0; __IO bool receivePended = true; static uint32_t transmitStart = 0; +#ifdef DTR_TOGGLING_SEQ +/* DTR toggling sequence management */ +extern void dtr_togglingHook(uint8_t *buf, uint32_t *len); +uint8_t dtr_toggling = 0; +#endif /** USBD_CDC Private Function Prototypes */ @@ -182,6 +188,9 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) if (lineState) { // Reset the transmit timeout when the port is connected transmitStart = 0; } +#ifdef DTR_TOGGLING_SEQ + dtr_toggling++; /* Count DTR toggling */ +#endif break; case CDC_SEND_BREAK: @@ -213,7 +222,14 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) */ static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len) { +#ifdef DTR_TOGGLING_SEQ + if (dtr_toggling > 3) { + dtr_togglingHook(Buf, Len); + dtr_toggling = 0; + } +#else UNUSED(Buf); +#endif /* It always contains required amount of free space for writing */ CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len)); receivePended = false; diff --git a/platform.txt b/platform.txt index c961db3fed..c8ec895fbf 100644 --- a/platform.txt +++ b/platform.txt @@ -37,7 +37,7 @@ compiler.cpp.flags={compiler.extra_flags} -c {build.flags.optimize} {compiler.wa compiler.ar.flags=rcs -compiler.c.elf.flags=-mcpu={build.mcu} -mthumb {build.flags.optimize} {build.flags.ldspecs} -Wl,--defsym=LD_MAX_SIZE={upload.maximum_size} -Wl,--defsym=LD_MAX_DATA_SIZE={upload.maximum_data_size} -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common +compiler.c.elf.flags=-mcpu={build.mcu} -mthumb {build.flags.optimize} {build.flags.ldspecs} -Wl,--defsym=LD_FLASH_OFFSET={build.flash_offset} -Wl,--defsym=LD_MAX_SIZE={upload.maximum_size} -Wl,--defsym=LD_MAX_DATA_SIZE={upload.maximum_data_size} -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 @@ -50,6 +50,7 @@ compiler.define=-DARDUINO= # These can be overriden in boards.txt build.extra_flags= +build.bootloader_flags= build.ldscript=ldscript.ld # These can be overridden in platform.local.txt @@ -83,6 +84,7 @@ build.usb_speed= build.startup_file= build.flags.optimize=-Os build.flags.ldspecs=--specs=nano.specs +build.flash_offset=0 # Pre and post build hooks build.opt.name=build_opt.h @@ -166,3 +168,14 @@ tools.bmp_upload.upload.speed=230400 tools.bmp_upload.upload.params.verbose=-batch tools.bmp_upload.upload.params.quiet=--batch-silent tools.bmp_upload.upload.pattern="{path}{cmd}" -nx -b {upload.speed} {upload.verbose} -ex "set confirm off" -ex "target extended-remote {serial.port}" -ex "monitor swdp_scan" -ex "attach 1" -ex "load" -ex "compare-sections" -ex "kill" "{build.path}/{build.project_name}.elf" + +# Upload using Maple bootloader over DFU +tools.maple_upload.cmd=maple_upload +tools.maple_upload.cmd.windows=maple_upload.bat +tools.maple_upload.path={runtime.hardware.path}/tools/win +tools.maple_upload.path.macosx={runtime.hardware.path}/tools/macosx +tools.maple_upload.path.linux={runtime.hardware.path}/tools/linux +tools.maple_upload.path.linux64={runtime.hardware.path}/tools/linux64 +tools.maple_upload.upload.params.verbose=-d +tools.maple_upload.upload.params.quiet=n +tools.maple_upload.upload.pattern="{path}/{cmd}" {serial.port.file} {upload.altID} {upload.usbID} "{build.path}/{build.project_name}.bin" diff --git a/variants/BLUEPILL_F103XX/ldscript.ld b/variants/BLUEPILL_F103XX/ldscript.ld index b56dad0d04..a51c1f685e 100644 --- a/variants/BLUEPILL_F103XX/ldscript.ld +++ b/variants/BLUEPILL_F103XX/ldscript.ld @@ -61,7 +61,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */ MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = LD_MAX_DATA_SIZE -FLASH (rx) : ORIGIN = 0x8000000, LENGTH = LD_MAX_SIZE +FLASH (rx) : ORIGIN = 0x8000000 + LD_FLASH_OFFSET, LENGTH = LD_MAX_SIZE - LD_FLASH_OFFSET } /* Define output sections */ diff --git a/variants/MAPLEMINI_F103CB/ldscript.ld b/variants/MAPLEMINI_F103CB/ldscript.ld index fca3184d57..23aa50d5ab 100644 --- a/variants/MAPLEMINI_F103CB/ldscript.ld +++ b/variants/MAPLEMINI_F103CB/ldscript.ld @@ -52,7 +52,7 @@ ENTRY(Reset_Handler) /* Highest address of the user mode stack */ -_estack = 0x20005000; /* end of RAM */ +_estack = 0x20000000 + LD_MAX_DATA_SIZE; /* 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 */ @@ -60,8 +60,8 @@ _Min_Stack_Size = 0x400; /* required amount of stack */ /* Specify the memory areas */ MEMORY { -RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K -FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 128K +RAM (xrw) : ORIGIN = 0x20000000, LENGTH = LD_MAX_DATA_SIZE +FLASH (rx) : ORIGIN = 0x8000000 + LD_FLASH_OFFSET, LENGTH = LD_MAX_SIZE - LD_FLASH_OFFSET } /* Define output sections */