Skip to content

Legacy Maple DFU boot loader support #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions cores/arduino/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <stdint.h>

/**
* Empty yield() hook.
*
Expand All @@ -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
17 changes: 17 additions & 0 deletions cores/arduino/stm32/bootloader.c
Original file line number Diff line number Diff line change
@@ -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 */
18 changes: 18 additions & 0 deletions cores/arduino/stm32/bootloader.h
Original file line number Diff line number Diff line change
@@ -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_ */
16 changes: 16 additions & 0 deletions cores/arduino/stm32/usb/cdc/usbd_cdc_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
2 changes: 1 addition & 1 deletion variants/BLUEPILL_F103XX/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions variants/MAPLEMINI_F103CB/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@
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 */

/* 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 */
Expand Down