Skip to content

Commit 7b70b39

Browse files
committed
Read epin maxpacket to determine the max size of USBD_CDC_TransmitPacket() in CDC_continue_transmit()
1 parent 9333e7f commit 7b70b39

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

libraries/USBDevice/inc/cdc_queue.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ extern "C" {
5454
#define CDC_QUEUE_MAX_PACKET_SIZE USB_FS_MAX_PACKET_SIZE
5555
#endif
5656

57-
#define CDC_TRANSMIT_MAX_BUFFER_SIZE 65472 //STM32 USB OTG DIEPTSIZ PKTCNT maximum 0x3ff
5857
#define CDC_RECEIVE_MAX_BUFFER_SIZE CDC_QUEUE_MAX_PACKET_SIZE
5958

6059
#ifndef CDC_TRANSMIT_QUEUE_BUFFER_PACKET_NUMBER
@@ -84,7 +83,7 @@ void CDC_TransmitQueue_Init(CDC_TransmitQueue_TypeDef *queue);
8483
int CDC_TransmitQueue_WriteSize(CDC_TransmitQueue_TypeDef *queue);
8584
int CDC_TransmitQueue_ReadSize(CDC_TransmitQueue_TypeDef *queue);
8685
void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue, const uint8_t *buffer, uint32_t size);
87-
uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue, uint32_t *size);
86+
uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue, uint32_t *size, uint32_t maxsize);
8887
void CDC_TransmitQueue_CommitRead(CDC_TransmitQueue_TypeDef *queue);
8988

9089
void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue);

libraries/USBDevice/inc/usbd_cdc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
145145
USBD_CDC_ItfTypeDef *fops);
146146

147147
#ifdef USE_USBD_COMPOSITE
148+
uint32_t USBD_CDC_GetTxMaxSize(USBD_HandleTypeDef *pdev, uint8_t ClassId);
148149
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff,
149150
uint32_t length, uint8_t ClassId);
150151
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, uint8_t ClassId);
151152
uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev, uint8_t ClassId);
152153
#else
154+
uint32_t USBD_CDC_GetTxMaxSize(USBD_HandleTypeDef *pdev);
153155
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff,
154156
uint32_t length);
155157
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev);

libraries/USBDevice/src/cdc/cdc_queue.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue,
7777

7878
// Read flat block from queue biggest as possible, but max QUEUE_MAX_PACKET_SIZE
7979
uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue,
80-
uint32_t *size)
80+
uint32_t *size,
81+
uint32_t maxsize)
8182
{
8283
if (queue->write >= queue->read) {
8384
*size = queue->write - queue->read;
8485
} else {
8586
*size = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read;
8687
}
87-
if(*size > CDC_TRANSMIT_MAX_BUFFER_SIZE)
88-
*size = CDC_TRANSMIT_MAX_BUFFER_SIZE;
88+
if(*size > maxsize) *size = maxsize;
8989

9090
queue->reserved = *size;
9191
return &queue->buffer[queue->read];

libraries/USBDevice/src/cdc/usbd_cdc.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
/** @defgroup USBD_CDC_Private_Defines
8080
* @{
8181
*/
82+
#define USB_MAX_PKTCNT 1023
8283
/**
8384
* @}
8485
*/
@@ -896,6 +897,25 @@ uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
896897
return (uint8_t)USBD_OK;
897898
}
898899

900+
/**
901+
* @brief USBD_CDC_GetTxMaxSize
902+
* @param pdev: device instance
903+
* @param ClassId: The Class ID
904+
* @retval status
905+
*/
906+
#ifdef USE_USBD_COMPOSITE
907+
uint8_t USBD_CDC_GetTxMaxSize(USBD_HandleTypeDef *pdev, uint8_t ClassId)
908+
{
909+
/* Get the Endpoints addresses allocated for this class instance */
910+
CDCInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_BULK, ClassId);
911+
#else
912+
uint8_t USBD_CDC_GetTxMaxSize(USBD_HandleTypeDef *pdev)
913+
{
914+
#endif /* USE_USBD_COMPOSITE */
915+
916+
return pdev->ep_in[CDCInEpAdd & 0xFU].maxpacket * USB_MAX_PKTCNT;
917+
}
918+
899919
/**
900920
* @brief USBD_CDC_SetTxBuffer
901921
* @param pdev: device instance

libraries/USBDevice/src/cdc/usbd_cdc_if.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
#include "usbd_cdc_if.h"
2626
#include "bootloader.h"
2727

28-
#ifdef USE_USB_HS
29-
#define CDC_MAX_PACKET_SIZE USB_OTG_HS_MAX_PACKET_SIZE
30-
#elif defined(USB_OTG_FS) || defined(USB_OTG_FS_MAX_PACKET_SIZE)
31-
#define CDC_MAX_PACKET_SIZE USB_OTG_FS_MAX_PACKET_SIZE
32-
#else /* USB */
33-
#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE
34-
#endif
3528

3629
/*
3730
* The value USB_CDC_TRANSMIT_TIMEOUT is defined in terms of HAL_GetTick() units.
@@ -342,7 +335,8 @@ void CDC_continue_transmit(void)
342335
* is higher than that of the main thread. So this method is thread safe.
343336
*/
344337
if (hcdc->TxState == 0U) {
345-
buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size);
338+
uint32_t maxsize = USBD_CDC_GetTxMaxSize(&hUSBD_Device_CDC);
339+
buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size, maxsize);
346340
if (size > 0) {
347341
transmitStart = HAL_GetTick();
348342
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, buffer, size);

0 commit comments

Comments
 (0)