Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit 5e5251b

Browse files
cparataalfran
authored andcommitted
Fix DFU mode issue
Issue was due to the fact that sometimes the code remained blocked inside the timer callback after the closure of the USB serial port. In order to avoid that, some checks inside the timer callbeck and the CDC_Flush API have been added. Improving the calculation of the period and prescaler of the timer. Signed-off-by: Carlo.Parata <carlo.parata@st.com>
1 parent 77aa866 commit 5e5251b

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

cores/arduino/stm32/usbd_cdc_if.c

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ uint8_t USBPackSize;
107107
/* Default configuration: 115200, 8N1 */
108108
uint8_t lineSetup[] = {0x00, 0xc2, 0x01, 0x00, 0x00, 0x00, 0x08};
109109

110-
#define CDC_POLLING_INTERVAL 1 /* in ms. The max is 65 and the min is 1 */
110+
#define CDC_POLLING_INTERVAL 2 /* in ms. The max is 65 and the min is 1 */
111111

112112
stimer_t TimHandle;
113113

114114
volatile uint8_t dfu_request = 0;
115115
/* For a bug in some Linux 64 bit PC we need to delay the reset of the CPU of 500ms seconds */
116-
int counter_dfu_reset = 500; /* the unit is equal to CDC_POLLING_INTERVAL that is 5ms by default */
116+
int counter_dfu_reset = 250; /* the unit is equal to CDC_POLLING_INTERVAL that is 2ms by default */
117117

118118
static void TIM_Config(void);
119119

@@ -309,38 +309,44 @@ void CDC_flush(void)
309309
{
310310
uint8_t status;
311311

312-
if(UserTxBufPtrOut != UserTxBufPtrIn)
312+
if (!(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)))
313313
{
314-
if(UserTxBufPtrOut > UserTxBufPtrIn) /* Roll-back */
314+
if(UserTxBufPtrOut != UserTxBufPtrIn)
315315
{
316-
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
316+
if(UserTxBufPtrOut > UserTxBufPtrIn) /* Roll-back */
317+
{
318+
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
317319

318-
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], UserTxBufPtrIn);
320+
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], UserTxBufPtrIn);
319321

320-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
322+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
321323

322-
do {
323-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
324-
} while(status == USBD_BUSY);
324+
do {
325+
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
326+
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
325327

326-
if(status == USBD_OK)
327-
{
328-
UserTxBufPtrOut = UserTxBufPtrIn;
328+
if(status == USBD_OK)
329+
{
330+
UserTxBufPtrOut = UserTxBufPtrIn;
331+
}
329332
}
330-
}
331-
else
332-
{
333-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
333+
else
334+
{
335+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
334336

335-
do {
336-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
337-
} while(status == USBD_BUSY);
337+
do {
338+
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
339+
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
338340

339-
if(status == USBD_OK)
340-
{
341-
UserTxBufPtrOut = UserTxBufPtrIn;
341+
if(status == USBD_OK)
342+
{
343+
UserTxBufPtrOut = UserTxBufPtrIn;
344+
}
342345
}
343346
}
347+
} else
348+
{
349+
UserTxBufPtrOut = UserTxBufPtrIn;
344350
}
345351
}
346352

@@ -364,7 +370,7 @@ static void TIM_Config(void)
364370
+ ClockDivision = 0
365371
+ Counter direction = Up
366372
*/
367-
TimerHandleInit(&TimHandle, (uint16_t)((CDC_POLLING_INTERVAL*1000) - 1), (84 - 1)); //CDC_POLLING_INTERVAL
373+
TimerHandleInit(&TimHandle, (uint16_t)((CDC_POLLING_INTERVAL*1000) - 1), ((uint32_t)(getTimerClkFreq(TIM6) / (1000000)) - 1)); //CDC_POLLING_INTERVAL
368374

369375
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 6, 0);
370376

@@ -401,33 +407,37 @@ void TIM6_PeriodElapsedCallback(stimer_t *htim)
401407
USBD_CDC_ReceivePacket(&hUSBD_Device_CDC);
402408
}
403409

404-
if(UserTxBufPtrOut != UserTxBufPtrIn) {
405-
if(UserTxBufPtrOut > UserTxBufPtrIn) { /* Roll-back */
406-
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
410+
if (!(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN))) {
411+
if(UserTxBufPtrOut != UserTxBufPtrIn) {
412+
if(UserTxBufPtrOut > UserTxBufPtrIn) { /* Roll-back */
413+
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
407414

408-
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], UserTxBufPtrIn);
415+
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], UserTxBufPtrIn);
409416

410-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
417+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
411418

412-
do {
413-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
414-
} while(status == USBD_BUSY);
419+
do {
420+
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
421+
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
415422

416-
if(status == USBD_OK) {
417-
UserTxBufPtrOut = UserTxBufPtrIn;
423+
if(status == USBD_OK) {
424+
UserTxBufPtrOut = UserTxBufPtrIn;
425+
}
418426
}
419-
}
420-
else {
421-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
427+
else {
428+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
422429

423-
do {
424-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
425-
} while(status == USBD_BUSY);
430+
do {
431+
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
432+
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
426433

427-
if(status == USBD_OK) {
428-
UserTxBufPtrOut = UserTxBufPtrIn;
434+
if(status == USBD_OK) {
435+
UserTxBufPtrOut = UserTxBufPtrIn;
436+
}
429437
}
430438
}
439+
} else {
440+
UserTxBufPtrOut = UserTxBufPtrIn;
431441
}
432442
}
433443

0 commit comments

Comments
 (0)