8000 stmhal: USB VCP speed improvements · micropython/micropython@d44bc93 · GitHub
[go: up one dir, main page]

Skip to content

Commit d44bc93

Browse files
committed
stmhal: USB VCP speed improvements
- releases TIM3 and uses SOF irqs to trigger CDC tx transfers - increases CDC tx transfer rate sigificantly (up to 10x, depending on usage) - uses a txComplete callback to check immediately for a next tx transfer
1 parent f925165 commit d44bc93

File tree

8 files changed

+31
-17
lines changed

8 files changed

+31
-17
lines changed

stmhal/hal/f4/src/stm32f4xx_hal_pcd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
292292
uint32_t i = 0, ep_intr = 0, epint = 0, epnum = 0;
293293
uint32_t fifoemptymsk = 0, temp = 0;
294294
USB_OTG_EPTypeDef *ep;
295-
296295
/* ensure that we are in device mode */
297296
if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
298297
{

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ int main(void) {
398398
HAL_NVIC_SetPriority(PVD_IRQn, 6, 0); // same priority as USB
399399
HAL_NVIC_EnableIRQ(PVD_IRQn);
400400
#else
401-
timer_tim3_init();
401+
// timer_tim3_init();
402402
#endif
403403
led_init();
404404
#if MICROPY_HW_HAS_SWITCH

stmhal/stm32_it.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ void TIM3_IRQHandler(void) {
500500
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
501501
timer_irq_handler(3);
502502
#else
503-
HAL_TIM_IRQHandler(&TIM3_Handle);
503+
// HAL_TIM_IRQHandler(&TIM3_Handle);
504504
#endif
505505
}
506506

stmhal/timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ TIM_HandleTypeDef *timer_tim6_init(uint freq) {
252252
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
253253
#if !defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
254254
if (htim == &TIM3_Handle) {
255-
USBD_CDC_HAL_TIM_PeriodElapsedCallback();
255+
// USBD_CDC_HAL_TIM_PeriodElapsedCallback();
256256
} else
257257
#endif
258258
if (htim == &TIM5_Handle) {

stmhal/usbd_cdc_interface.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ static int8_t CDC_Itf_DeInit (void);
8787
static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length);
8888
static int8_t CDC_Itf_Receive (uint8_t* pbuf, uint32_t *Len);
8989

90+
extern USBD_CDC_HandleTypeDef CDC_ClassData;
91+
void CDC_TxComplete(void);
92+
// TxComplete callback is routed to USBD_CDC_HAL_TIM_PeriodElapsedCallback
9093
const USBD_CDC_ItfTypeDef USBD_CDC_fops = {
9194
CDC_Itf_Init,
9295
CDC_Itf_DeInit,
9396
CDC_Itf_Control,
94-
CDC_Itf_Receive
97+
CDC_Itf_Receive,
98+
CDC_TxComplete
9599
};
96100

97101
/* Private functions ---------------------------------------------------------*/
@@ -142,7 +146,7 @@ static int8_t CDC_Itf_Init(void)
142146

143147
/*##-4- Start the TIM Base generation in interrupt mode ####################*/
144148
/* Start Channel1 */
145-
__HAL_TIM_ENABLE_IT(&TIM3_Handle, TIM_IT_UPDATE);
149+
// __HAL_TIM_ENABLE_IT(&TIM3_Handle, TIM_IT_UPDATE);
146150

147151
/*##-5- Set Application Buffers ############################################*/
148152
USBD_CDC_SetTxBuffer(&hUSBDDevice, UserTxBuffer, 0);
@@ -258,11 +262,11 @@ static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
258262
}
259263

260264
/**
261-
* @brief TIM period elapsed callback
262-
* @param htim: TIM handle
265+
* @brief callback of USB start-of-frame (SOF) in 1msec intervals
266+
* @param hpcd PCD handle
263267
* @retval None
264268
*/
265-
void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
269+
void USBD_CDC_SOF_callback(PCD_HandleTypeDef *hpcd) {
266270
if (!dev_is_connected) {
267271
// CDC device is not connected to a host, so we are unable to send any data
268272
return;
@@ -276,9 +280,8 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
276280
if (UserTxBufPtrOut != UserTxBufPtrOutShadow) {
277281
// We have sent data and are waiting for the low-level USB driver to
278282
// finish sending it over the USB in-endpoint.
279-
// We have a 15 * 10ms = 150ms timeout
280-
if (UserTxBufPtrWaitCount < 15) {
281-
PCD_HandleTypeDef *hpcd = hUSBDDevice.pData;
283+
// We have a 150 * 1ms = 150ms timeout
284+
if (UserTxBufPtrWaitCount < 150) {
282285
USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
283286
if (USBx_INEP(CDC_IN_EP & 0x7f)->DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ) {
284287
// USB in-endpoint is still reading the data
@@ -320,7 +323,16 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
320323
}
321324
}
322325
}
323-
326+
/**
327+
* @brief CDC_TxComplete
328+
* Callback for tx complete event (in endpoint is empty)
329+
*/
330+
void CDC_TxComplete(void) {
331+
PCD_HandleTypeDef *hpcd = hUSBDDevice.pData;
332+
// check for any pending data and setup new packet
333+
USBD_CDC_SOF_callback(hpcd);
334+
335+
}
324336
/**
325337
* @brief CDC_Itf_DataRx
326338
* Data received over USB OUT endpoint is processed here.
@@ -429,7 +441,6 @@ int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout) {
429441
UserTxBuffer[UserTxBufPtrIn] = buf[i];
430442
UserTxBufPtrIn = (UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1);
431443
}
432-
433444
// Success, return number of bytes read
434445
return len;
435446
}

stmhal/usbd_conf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,15 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
271271
USBD_LL_DataInStage(hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
272272
}
273273

274+
extern void USBD_CDC_SOF_callback(PCD_HandleTypeDef *hpcd);
274275
/**
275276
* @brief SOF callback.
276277
* @param hpcd: PCD handle
277278
* @retval None
278279
*/
279280
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
280281
{
281-
USBD_LL_SOF(hpcd->pData);
282+
USBD_CDC_SOF_callback(hpcd);
282283
}
283284

284285
/**
@@ -394,7 +395,7 @@ if (pdev->id == USB_PHY_FS_ID)
394395
pcd_fs_handle.Init.dma_enable = 0;
395396
pcd_fs_handle.Init.low_power_enable = 0;
396397
pcd_fs_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
397-
pcd_fs_handle.Init.Sof_enable = 0;
398+
pcd_fs_handle.Init.Sof_enable = 1;
398399
pcd_fs_handle.Init.speed = PCD_SPEED_FULL;
399400
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
400401
pcd_fs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0

stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef struct _USBD_CDC_Itf {
3131
int8_t (* DeInit) (void);
3232
int8_t (* Control) (uint8_t, uint8_t * , uint16_t);
3333
int8_t (* Receive) (uint8_t *, uint32_t *);
34+
void (* TxComplete) (void);
3435
} USBD_CDC_ItfTypeDef;
3536

3637
typedef struct {

stmhal/usbdev/class/src/usbd_cdc_msc_hid.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static const uint8_t *hid_report_desc;
9090
static USBD_CDC_ItfTypeDef *CDC_fops;
9191
static USBD_StorageTypeDef *MSC_fops;
9292

93-
static USBD_CDC_HandleTypeDef CDC_ClassData;
93+
USBD_CDC_HandleTypeDef CDC_ClassData;
9494
static USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData;
9595
static USBD_HID_HandleTypeDef HID_ClassData;
9696

@@ -904,6 +904,8 @@ static uint8_t USBD_CDC_MSC_HID_EP0_RxReady(USBD_HandleTypeDef *pdev) {
904904
static uint8_t USBD_CDC_MSC_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) {
905905
if ((usbd_mode & USBD_MODE_CDC) && (epnum == (CDC_IN_EP & 0x7f) || epnum == (CDC_CMD_EP & 0x7f))) {
906906
CDC_ClassData.TxState = 0;
907+
// added tx_complete callback here
908+
CDC_fops->TxComplete();
907909
return USBD_OK;
908910
} else if ((usbd_mode & USBD_MODE_MSC) && epnum == (MSC_IN_EP & 0x7f)) {
909911
MSC_BOT_DataIn(pdev, epnum);

0 commit comments

Comments
 (0)
0