Skip to content

Commit b872823

Browse files
committed
Fixed error, when host send some buffer, less then minimum, and new buffer can't be allocated
1 parent adbf119 commit b872823

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,23 @@ uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
882882
}
883883
}
884884

885+
uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev)
886+
{
887+
/* Suspend or Resume USB Out process */
888+
if (pdev->pClassData != NULL) {
889+
if (pdev->dev_speed == USBD_SPEED_HIGH) {
890+
/* Prepare Out endpoint to receive next packet */
891+
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, 0, 0);
892+
} else {
893+
/* Prepare Out endpoint to receive next packet */
894+
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, 0, 0);
895+
}
896+
return USBD_OK;
897+
} else {
898+
return USBD_FAIL;
899+
}
900+
}
901+
885902
#endif /* USBD_USE_CDC */
886903
#endif /* USBCON */
887904
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc.h

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev,
157157

158158
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev);
159159

160+
uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev);
161+
160162
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev);
161163
/**
162164
* @}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len)
205205
CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len));
206206
receivePended = false;
207207
/* If enough space in the queue for a full buffer then continue receive */
208-
CDC_resume_receive();
208+
if (!CDC_resume_receive()) {
209+
USBD_CDC_ClearBuffer(&hUSBD_Device_CDC);
210+
}
209211
return USBD_OK;
210212
}
211213

@@ -271,7 +273,7 @@ void CDC_continue_transmit(void)
271273
}
272274
}
273275

274-
void CDC_resume_receive(void)
276+
bool CDC_resume_receive(void)
275277
{
276278
/*
277279
* TS: main and IRQ threads can't pass it at same time, because
@@ -284,7 +286,9 @@ void CDC_resume_receive(void)
284286
/* Set new buffer */
285287
USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, block);
286288
USBD_CDC_ReceivePacket(&hUSBD_Device_CDC);
289+
return true;
287290
}
291+
return false;
288292
}
289293
}
290294

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern CDC_ReceiveQueue_TypeDef ReceiveQueue;
4747
/* Exported macro ------------------------------------------------------------*/
4848
/* Exported functions ------------------------------------------------------- */
4949
void CDC_continue_transmit(void);
50-
void CDC_resume_receive(void);
50+
bool CDC_resume_receive(void);
5151
void CDC_init(void);
5252
void CDC_deInit(void);
5353

0 commit comments

Comments
 (0)