Skip to content

Commit 72ababf

Browse files
Move USB bootloader timeout to use SOF callback
1 parent 402a461 commit 72ababf

File tree

9 files changed

+60
-47
lines changed

9 files changed

+60
-47
lines changed

cores/stm32l4/CDC.cpp

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,22 @@ void CDC::begin(unsigned long baudrate, uint16_t config)
7878
/* If USBD_CDC has already been enabled/initialized by STDIO, just add the notify.
7979
*/
8080
if (_usbd_cdc->state == USBD_CDC_STATE_INIT) {
81-
stm32l4_usbd_cdc_enable(_usbd_cdc, &_rx_data[0], sizeof(_rx_data), 0, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
81+
stm32l4_usbd_cdc_enable(_usbd_cdc, &_rx_data[0], sizeof(_rx_data), 0, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_SOF | USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
8282

8383
if (stm32l4_stdio_put == NULL) {
8484
stm32l4_stdio_put = serialusb_stdio_put;
8585
}
8686
} else {
8787
flush();
8888

89-
stm32l4_usbd_cdc_notify(_usbd_cdc, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
89+
stm32l4_usbd_cdc_notify(_usbd_cdc, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_SOF | USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
9090
}
91-
92-
USBD_SOFCallback(CDC::_sof_callback, (void*)this);
9391
}
9492

9593
void CDC::end()
9694
{
9795
flush();
9896

99-
USBD_SOFCallback(NULL, NULL);
100-
10197
if (stm32l4_stdio_put == serialusb_stdio_put) {
10298
stm32l4_stdio_put = NULL;
10399
}
@@ -324,7 +320,7 @@ void CDC::onReceive(void(*callback)(void))
324320
void CDC::EventCallback(uint32_t events)
325321
{
326322
unsigned int tx_read, tx_size;
327-
323+
328324
if (events & USBD_CDC_EVENT_RECEIVE) {
329325
if (_receiveCallback) {
330326
armv7m_pendsv_enqueue((armv7m_pendsv_routine_t)_receiveCallback, NULL, 0);
@@ -380,33 +376,30 @@ void CDC::EventCallback(uint32_t events)
380376
}
381377
}
382378
}
383-
}
384379

385-
void CDC::SOFCallback()
386-
{
387-
unsigned int tx_read, tx_size;
380+
if (events & USBD_CDC_EVENT_SOF) {
381+
if (_tx_count && !_tx_size && !_tx_size2) {
388382

389-
if (_tx_count && !_tx_size && !_tx_size2)
390-
{
391-
_tx_timeout++;
383+
_tx_timeout++;
392384

393-
// Small packets get only send after 8ms latency
394-
if (_tx_timeout >= 8)
395-
{
396-
tx_size = _tx_count;
397-
tx_read = _tx_read;
385+
// Small packets get only send after 8ms latency
386+
if (_tx_timeout >= 8)
387+
{
388+
tx_size = _tx_count;
389+
tx_read = _tx_read;
398390

399-
if (tx_size > (CDC_TX_BUFFER_SIZE - tx_read)) {
400-
tx_size = (CDC_TX_BUFFER_SIZE - tx_read);
401-
}
391+
if (tx_size > (CDC_TX_BUFFER_SIZE - tx_read)) {
392+
tx_size = (CDC_TX_BUFFER_SIZE - tx_read);
393+
}
402394

403-
if (tx_size > CDC_TX_PACKET_SIZE) {
404-
tx_size = CDC_TX_PACKET_SIZE;
405-
}
395+
if (tx_size > CDC_TX_PACKET_SIZE) {
396+
tx_size = CDC_TX_PACKET_SIZE;
397+
}
406398

407-
_tx_size = tx_size;
399+
_tx_size = tx_size;
408400

409-
stm32l4_usbd_cdc_transmit(_usbd_cdc, &_tx_data[tx_read], tx_size);
401+
stm32l4_usbd_cdc_transmit(_usbd_cdc, &_tx_data[tx_read], tx_size);
402+
}
410403
}
411404
}
412405
}
@@ -416,11 +409,6 @@ void CDC::_event_callback(void *context, uint32_t events)
416409
reinterpret_cast<class CDC*>(context)->EventCallback(events);
417410
}
418411

419-
void CDC::_sof_callback(void *context)
420-
{
421-
reinterpret_cast<class CDC*>(context)->SOFCallback();
422-
}
423-
424412
CDC::operator bool()
425413
{
426414
return stm32l4_usbd_cdc_connected(_usbd_cdc);

cores/stm32l4/USBAPI.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,5 @@ class CDC : public HardwareSerial
125125
void (*_receiveCallback)(void);
126126

127127
static void _event_callback(void *context, uint32_t events);
128-
static void _sof_callback(void *context);
129128
void EventCallback(uint32_t events);
130-
void SOFCallback();
131129
};

cores/stm32l4/stm32l4_wiring_private.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ extern void USBD_Poll(void);
8484
extern bool USBD_Connected(void);
8585
extern bool USBD_Configured(void);
8686
extern bool USBD_Suspended(void);
87-
extern void USBD_SOFCallback(void(*callback)(void*), void*);
8887

8988
/************************************************************************
9089
*

system/STM32L4xx/Include/stm32l4_usbd_cdc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct _stm32l4_usbd_cdc_info_t {
5454

5555
extern volatile stm32l4_usbd_cdc_info_t stm32l4_usbd_cdc_info;
5656

57+
#define USBD_CDC_EVENT_SOF 0x20000000
5758
#define USBD_CDC_EVENT_RECEIVE 0x40000000
5859
#define USBD_CDC_EVENT_TRANSMIT 0x80000000
5960

-272 Bytes
Binary file not shown.
-272 Bytes
Binary file not shown.
-264 Bytes
Binary file not shown.

system/STM32L4xx/Source/USB/usbd_conf.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ static PCD_HandleTypeDef hpcd;
6767
static unsigned int usbd_pin_vbus;
6868
static unsigned int usbd_pin_vbus_count = 0;
6969
static bool usbd_connected = false;
70-
static void (*usbd_sof_callback)(void*) = NULL;
71-
static void *usbd_sof_context;
70+
static void (*usbd_sof_callback)(void) = NULL;
7271

7372
/* Private functions ---------------------------------------------------------*/
7473

@@ -271,10 +270,9 @@ bool USBD_Suspended(void)
271270
return (USBD_Device.dev_state == USBD_STATE_SUSPENDED);
272271
}
273272

274-
void USBD_SOFCallback(void(*callback)(void*), void *context)
273+
void USBD_SOFCallback(void(*callback)(void))
275274
{
276275
usbd_sof_callback = callback;
277-
usbd_sof_context = context;
278276
}
279277

280278
/*******************************************************************************
@@ -415,7 +413,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
415413
USBD_LL_SOF(hpcd->pData);
416414

417415
if (usbd_sof_callback) {
418-
(*usbd_sof_callback)(usbd_sof_context);
416+
(*usbd_sof_callback)();
419417
}
420418
}
421419

system/STM32L4xx/Source/stm32l4_usbd_cdc.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
#include "usbd_cdc.h"
3838

39+
extern void USBD_Detach(void);
40+
extern void USBD_SOFCallback(void(*callback)(void));
41+
3942
volatile stm32l4_usbd_cdc_info_t stm32l4_usbd_cdc_info;
4043

4144
typedef struct _stm32l4_usbd_cdc_driver_t {
@@ -49,42 +52,68 @@ typedef struct _stm32l4_usbd_cdc_device_t {
4952
volatile uint8_t rx_busy;
5053
volatile uint8_t tx_busy;
5154
uint64_t connect;
52-
armv7m_timer_t timeout;
55+
uint32_t timeout;
5356
} stm32l4_usbd_cdc_device_t;
5457

5558
static stm32l4_usbd_cdc_device_t stm32l4_usbd_cdc_device;
5659

60+
static void stm32l4_usbd_cdc_sof_callback(void)
61+
{
62+
stm32l4_usbd_cdc_t *usbd_cdc = stm32l4_usbd_cdc_driver.instances[0];
63+
64+
if (stm32l4_usbd_cdc_device.timeout)
65+
{
66+
if (stm32l4_usbd_cdc_device.timeout == 1)
67+
{
68+
stm32l4_system_dfu();
69+
}
70+
else
71+
{
72+
stm32l4_usbd_cdc_device.timeout--;
73+
}
74+
}
75+
76+
if (usbd_cdc && (usbd_cdc->state > USBD_CDC_STATE_INIT))
77+
{
78+
if (usbd_cdc->events & USBD_CDC_EVENT_SOF)
79+
{
80+
(*usbd_cdc->callback)(usbd_cdc->context, USBD_CDC_EVENT_SOF);
81+
}
82+
}
83+
}
84+
5785
static void stm32l4_usbd_cdc_init(USBD_HandleTypeDef *USBD)
5886
{
5987
stm32l4_usbd_cdc_t *usbd_cdc = stm32l4_usbd_cdc_driver.instances[0];
6088

6189
stm32l4_usbd_cdc_device.USBD = USBD;
6290
stm32l4_usbd_cdc_device.rx_busy = 0;
6391
stm32l4_usbd_cdc_device.tx_busy = 0;
64-
stm32l4_usbd_cdc_device.connect = 0;;
92+
stm32l4_usbd_cdc_device.connect = 0;
93+
stm32l4_usbd_cdc_device.timeout = 0;
6594

6695
stm32l4_usbd_cdc_info.dwDTERate = 115200;
6796
stm32l4_usbd_cdc_info.bCharFormat = 0;
6897
stm32l4_usbd_cdc_info.bParityType = 0;
6998
stm32l4_usbd_cdc_info.bDataBits = 8;
7099
stm32l4_usbd_cdc_info.lineState = 0;
71100

72-
armv7m_timer_create(&stm32l4_usbd_cdc_device.timeout, (armv7m_timer_callback_t)&stm32l4_system_dfu);
73-
74101
if (usbd_cdc && (usbd_cdc->state > USBD_CDC_STATE_INIT))
75102
{
76103
USBD_CDC_SetRxBuffer(stm32l4_usbd_cdc_device.USBD, &usbd_cdc->rx_data[usbd_cdc->rx_write]);
77104
USBD_CDC_ReceivePacket(stm32l4_usbd_cdc_device.USBD);
78105

79106
stm32l4_usbd_cdc_device.rx_busy = 1;
80107
}
108+
109+
USBD_SOFCallback(stm32l4_usbd_cdc_sof_callback);
81110
}
82111

83112
static void stm32l4_usbd_cdc_deinit(void)
84113
{
85114
stm32l4_usbd_cdc_t *usbd_cdc;
86115

87-
armv7m_timer_stop(&stm32l4_usbd_cdc_device.timeout);
116+
USBD_SOFCallback(NULL);
88117

89118
stm32l4_usbd_cdc_info.lineState = 0;
90119

@@ -145,12 +174,12 @@ static void stm32l4_usbd_cdc_control(uint8_t command, uint8_t *data, uint16_t le
145174
if ((stm32l4_usbd_cdc_info.dwDTERate == 1200) && !(stm32l4_usbd_cdc_info.lineState & 1))
146175
{
147176
/* start reset timer */
148-
armv7m_timer_start(&stm32l4_usbd_cdc_device.timeout, 250);
177+
stm32l4_usbd_cdc_device.timeout = 250;
149178
}
150179
else
151180
{
152181
/* stop reset timer */
153-
armv7m_timer_stop(&stm32l4_usbd_cdc_device.timeout);
182+
stm32l4_usbd_cdc_device.timeout = 0;
154183
}
155184
}
156185
}

0 commit comments

Comments
 (0)