Skip to content

Commit 2cfddac

Browse files
authored
Merge pull request #525 from fpistm/HID_bootloader
Legacy Maple DFU boot loader support
2 parents 80b0fd9 + 357b875 commit 2cfddac

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
lines changed

Diff for: boards.txt

+17-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ GenF1.build.board=GenF1
593593
GenF1.build.mcu=cortex-m3
594594
GenF1.build.series=STM32F1xx
595595
GenF1.build.cmsis_lib_gcc=arm_cortexM3l_math
596-
GenF1.build.extra_flags=-D{build.product_line} {build.enable_usb} {build.xSerial}
596+
GenF1.build.extra_flags=-D{build.product_line} {build.enable_usb} {build.xSerial} {build.bootloader_flags}
597597

598598
# BLUEPILL_F103C6 board
599599
GenF1.menu.pnum.BLUEPILL_F103C6=BluePill F103C6 (32K)
@@ -669,6 +669,22 @@ GenF1.menu.upload_method.bmpMethod=BMP (Black Magic Probe)
669669
GenF1.menu.upload_method.bmpMethod.upload.protocol=gdb_bmp
670670
GenF1.menu.upload_method.bmpMethod.upload.tool=bmp_upload
671671

672+
GenF1.menu.upload_method.dfu2Method=Maple DFU Bootloader 2.0
673+
GenF1.menu.upload_method.dfu2Method.upload.protocol=maple
674+
GenF1.menu.upload_method.dfu2Method.upload.tool=maple_upload
675+
GenF1.menu.upload_method.dfu2Method.upload.usbID=1EAF:0003
676+
GenF1.menu.upload_method.dfu2Method.upload.altID=2
677+
GenF1.menu.upload_method.dfu2Method.build.flash_offset=0x2000
678+
GenF1.menu.upload_method.dfu2Method.build.bootloader_flags=-DBL_LEGACY_LEAF -DVECT_TAB_OFFSET={build.flash_offset}
679+
680+
GenF1.menu.upload_method.dfuoMethod=Maple DFU Bootloader original
681+
GenF1.menu.upload_method.dfuoMethod.upload.protocol=maple
682+
GenF1.menu.upload_method.dfuoMethod.upload.tool=maple_upload
683+
GenF1.menu.upload_method.dfuoMethod.upload.usbID=1EAF:0003
684+
GenF1.menu.upload_method.dfuoMethod.upload.altID=1
685+
GenF1.menu.upload_method.dfuoMethod.build.flash_offset=0x5000
686+
GenF1.menu.upload_method.dfuoMethod.build.bootloader_flags=-DBL_LEGACY_LEAF -DVECT_TAB_OFFSET={build.flash_offset}
687+
672688
################################################################################
673689
# Generic F4
674690

Diff for: cores/arduino/hooks.c

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
#include <stdint.h>
20+
1921
/**
2022
* Empty yield() hook.
2123
*
@@ -30,3 +32,21 @@ static void __empty()
3032
// Empty
3133
}
3234
void yield(void) __attribute__((weak, alias("__empty")));
35+
36+
#ifdef DTR_TOGGLING_SEQ
37+
/**
38+
* Empty dtr_toggling() hook.
39+
*
40+
* This function is intended to be used by library writers to build
41+
* libraries or sketches that require DTR toggling feature.
42+
*
43+
* Its defined as a weak symbol and it can be redefined to implement
44+
* task to achieve in this case.
45+
*/
46+
static void __empty_dtr_toggling(uint8_t *buf, uint32_t *len)
47+
{
48+
(void)buf;
49+
(void)len;
50+
}
51+
void dtr_togglingHook(uint8_t *buf, uint32_t *len) __attribute__((weak, alias("__empty_dtr_toggling")));
52+
#endif

Diff for: cores/arduino/stm32/bootloader.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "bootloader.h"
2+
3+
#include "stm32_def.h"
4+
5+
#ifdef BL_LEGACY_LEAF
6+
void dtr_togglingHook(uint8_t *buf, uint32_t *len)
7+
{
8+
/**
9+
* Four byte is the magic pack "1EAF" that puts the MCU into bootloader.
10+
* Check if the incoming contains the string "1EAF".
11+
* If yes, put the MCU into the bootloader mode.
12+
*/
13+
if ((*len >= 4) && (buf[0] == '1') && (buf[1] == 'E') && (buf[2] == 'A') && (buf[3] == 'F')) {
14+
NVIC_SystemReset();
15+
}
16+
}
17+
#endif /* BL_LEGACY_LEAF */

Diff for: cores/arduino/stm32/bootloader.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _BOOTLOADER_H_
2+
#define _BOOTLOADER_H_
3+
4+
/* Ensure DTR_TOGGLING_SEQ enabled */
5+
#ifdef BL_LEGACY_LEAF
6+
#ifndef DTR_TOGGLING_SEQ
7+
#define DTR_TOGGLING_SEQ
8+
#endif /* DTR_TOGGLING_SEQ */
9+
#endif /* BL_LEGACY_LEAF */
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif /* __cplusplus */
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif /* __cplusplus */
18+
#endif /* _BOOTLOADER_H_ */

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/* Includes ------------------------------------------------------------------*/
2424
#include "usbd_desc.h"
2525
#include "usbd_cdc_if.h"
26+
#include "bootloader.h"
2627

2728
#ifdef USE_USB_HS
2829
#define CDC_MAX_PACKET_SIZE USB_OTG_HS_MAX_PACKET_SIZE
@@ -54,6 +55,11 @@ __IO uint32_t lineState = 0;
5455
__IO bool receivePended = true;
5556
static uint32_t transmitStart = 0;
5657

58+
#ifdef DTR_TOGGLING_SEQ
59+
/* DTR toggling sequence management */
60+
extern void dtr_togglingHook(uint8_t *buf, uint32_t *len);
61+
uint8_t dtr_toggling = 0;
62+
#endif
5763

5864
/** USBD_CDC Private Function Prototypes */
5965

@@ -182,6 +188,9 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
182188
if (lineState) { // Reset the transmit timeout when the port is connected
183189
transmitStart = 0;
184190
}
191+
#ifdef DTR_TOGGLING_SEQ
192+
dtr_toggling++; /* Count DTR toggling */
193+
#endif
185194
break;
186195

187196
case CDC_SEND_BREAK:
@@ -213,7 +222,14 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
213222
*/
214223
static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len)
215224
{
225+
#ifdef DTR_TOGGLING_SEQ
226+
if (dtr_toggling > 3) {
227+
dtr_togglingHook(Buf, Len);
228+
dtr_toggling = 0;
229+
}
230+
#else
216231
UNUSED(Buf);
232+
#endif
217233
/* It always contains required amount of free space for writing */
218234
CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len));
219235
receivePended = false;

Diff for: platform.txt

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ compiler.cpp.flags={compiler.extra_flags} -c {build.flags.optimize} {compiler.wa
3737

3838
compiler.ar.flags=rcs
3939

40-
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
40+
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
4141

4242
compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0
4343

@@ -50,6 +50,7 @@ compiler.define=-DARDUINO=
5050

5151
# These can be overriden in boards.txt
5252
build.extra_flags=
53+
build.bootloader_flags=
5354
build.ldscript=ldscript.ld
5455

5556
# These can be overridden in platform.local.txt
@@ -83,6 +84,7 @@ build.usb_speed=
8384
build.startup_file=
8485
build.flags.optimize=-Os
8586
build.flags.ldspecs=--specs=nano.specs
87+
build.flash_offset=0
8688

8789
# Pre and post build hooks
8890
build.opt.name=build_opt.h
@@ -166,3 +168,14 @@ tools.bmp_upload.upload.speed=230400
166168
tools.bmp_upload.upload.params.verbose=-batch
167169
tools.bmp_upload.upload.params.quiet=--batch-silent
168170
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"
171+
172+
# Upload using Maple bootloader over DFU
173+
tools.maple_upload.cmd=maple_upload
174+
tools.maple_upload.cmd.windows=maple_upload.bat
175+
tools.maple_upload.path={runtime.hardware.path}/tools/win
176+
tools.maple_upload.path.macosx={runtime.hardware.path}/tools/macosx
177+
tools.maple_upload.path.linux={runtime.hardware.path}/tools/linux
178+
tools.maple_upload.path.linux64={runtime.hardware.path}/tools/linux64
179+
tools.maple_upload.upload.params.verbose=-d
180+
tools.maple_upload.upload.params.quiet=n
181+
tools.maple_upload.upload.pattern="{path}/{cmd}" {serial.port.file} {upload.altID} {upload.usbID} "{build.path}/{build.project_name}.bin"

Diff for: variants/BLUEPILL_F103XX/ldscript.ld

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
6161
MEMORY
6262
{
6363
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = LD_MAX_DATA_SIZE
64-
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = LD_MAX_SIZE
64+
FLASH (rx) : ORIGIN = 0x8000000 + LD_FLASH_OFFSET, LENGTH = LD_MAX_SIZE - LD_FLASH_OFFSET
6565
}
6666

6767
/* Define output sections */

Diff for: variants/MAPLEMINI_F103CB/ldscript.ld

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@
5252
ENTRY(Reset_Handler)
5353

5454
/* Highest address of the user mode stack */
55-
_estack = 0x20005000; /* end of RAM */
55+
_estack = 0x20000000 + LD_MAX_DATA_SIZE; /* end of RAM */
5656
/* Generate a link error if heap and stack don't fit into RAM */
5757
_Min_Heap_Size = 0x200; /* required amount of heap */
5858
_Min_Stack_Size = 0x400; /* required amount of stack */
5959

6060
/* Specify the memory areas */
6161
MEMORY
6262
{
63-
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
64-
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 128K
63+
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = LD_MAX_DATA_SIZE
64+
FLASH (rx) : ORIGIN = 0x8000000 + LD_FLASH_OFFSET, LENGTH = LD_MAX_SIZE - LD_FLASH_OFFSET
6565
}
6666

6767
/* Define output sections */

0 commit comments

Comments
 (0)