Skip to content

Commit d69a62c

Browse files
matthijskooijmanfpistm
authored andcommitted
Delay autoreset to bootloader
This waits for 250ms, just like the Arduino SAM and SAMD cores do (AVR waits only 120ms), to allow the USB host and application a bit more time to close the port and clean up.
1 parent fbf3cce commit d69a62c

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

cores/arduino/stm32/bootloader.h

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
extern "C" {
1515
#endif /* __cplusplus */
1616

17+
void scheduleBootloaderReset();
18+
void cancelBootloaderReset();
19+
void bootloaderSystickHandler();
20+
1721
/* Request to jump to system memory boot */
1822
void jumpToBootloaderRequested(void);
1923

cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE
3434
#endif
3535

36+
// TODO: Put this elsewhere
37+
#define BOOTLOADER_RESET_1200_BAUD
38+
3639
/*
3740
* The value USB_CDC_TRANSMIT_TIMEOUT is defined in terms of HAL_GetTick() units.
3841
* Typically it is 1ms value. The timeout determines when we would consider the
@@ -169,9 +172,6 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
169172
linecoding.format = pbuf[4];
170173
linecoding.paritytype = pbuf[5];
171174
linecoding.datatype = pbuf[6];
172-
if (linecoding.bitrate == 1200) {
173-
jumpToBootloaderRequested();
174-
}
175175
break;
176176

177177
case CDC_GET_LINE_CODING:
@@ -203,6 +203,19 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
203203
break;
204204
}
205205

206+
#ifdef BOOTLOADER_RESET_1200_BAUD
207+
if (cmd == CDC_SET_LINE_CODING || cmd == CDC_SET_CONTROL_LINE_STATE) {
208+
// Auto-reset into the bootloader is triggered when the port, already
209+
// open at 1200 bps, is closed. Cancel the reset when the port is
210+
// opened again.
211+
if (linecoding.bitrate == 1200 && !lineState) {
212+
scheduleBootloaderReset();
213+
} else {
214+
cancelBootloaderReset();
215+
}
216+
}
217+
#endif /* BOOTLOADER_RESET_1200_BAUD */
218+
206219
return ((int8_t)USBD_OK);
207220
}
208221

libraries/SrcWrapper/src/stm32/bootloader.c

+29
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
* STM32 built-in bootloader in system memory support
1212
*/
1313

14+
static const uint32_t BOOTLOADER_DELAY_MS = 250;
1415
static bool BootIntoBootloaderAfterReset;
16+
static uint32_t countdown = 0;
17+
1518

1619
/* Request to jump to system memory boot */
1720
WEAK void jumpToBootloaderRequested(void)
@@ -116,6 +119,32 @@ WEAK void jumpToBootloaderIfRequested(void)
116119
}
117120
}
118121

122+
/**
123+
* Scheduler a reset into the bootloader after a delay.
124+
*/
125+
void scheduleBootloaderReset()
126+
{
127+
countdown = BOOTLOADER_DELAY_MS;
128+
}
129+
130+
/**
131+
* Cancel a previously scheduled bootloader reset.
132+
*/
133+
void cancelBootloaderReset()
134+
{
135+
countdown = 0;
136+
}
137+
138+
/**
139+
* Bootloader systick handler, should be called every ms
140+
*/
141+
void bootloaderSystickHandler()
142+
{
143+
if (countdown && --countdown == 0) {
144+
jumpToBootloaderRequested();
145+
}
146+
}
147+
119148
/*
120149
* Legacy maple bootloader support
121150
*/

libraries/SrcWrapper/src/stm32/clock.c

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838
#include "backup.h"
3939
#include "clock.h"
40+
#include "bootloader.h"
4041
#include "stm32yyxx_ll_cortex.h"
4142

4243
#ifdef __cplusplus
@@ -88,6 +89,7 @@ void SysTick_Handler(void)
8889
HAL_IncTick();
8990
HAL_SYSTICK_IRQHandler();
9091
osSystickHandler();
92+
bootloaderSystickHandler();
9193
}
9294

9395
/**

0 commit comments

Comments
 (0)