8000 stmhal: Make USB CDC driver use SOF instead of TIM3 for outgoing data. · lispmeister/micropython@d363133 · GitHub
[go: up one dir, main page]

Skip to content

Commit d363133

Browse files
committed
stmhal: Make USB CDC driver use SOF instead of TIM3 for outgoing data.
Previous to this patch the USB CDC driver used TIM3 to trigger the sending of outgoing data over USB serial. This patch changes the behaviour so that the USB SOF interrupt is used to trigger the processing of the sending. This reduces latency and increases bandwidth of outgoing data. Thanks to Martin Fischer, aka @hoihu, for the idea and initial prototype. See PR micropython#1713.
1 parent 7417ccf commit d363133

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

stmhal/stm32_it.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,6 @@ void EXTI15_10_IRQHandler(void) {
480480
void PVD_IRQHandler(void) {
481481
IRQ_ENTER(PVD_IRQn);
482482
#if defined(MICROPY_HW_USE_ALT_IRQ_FOR_CDC)
483-
extern void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void);
484-
USBD_CDC_HAL_TIM_PeriodElapsedCallback();
485483
#endif
486484
Handle_EXTI_Irq(EXTI_PVD_OUTPUT);
487485
IRQ_EXIT(PVD_IRQn);

stmhal/timer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ 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();
256255
} else
257256
#endif
258257
if (htim == &TIM5_Handle) {

stmhal/usbd_cdc_interface.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,10 @@ static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
257257
return USBD_OK;
258258
}
259259

260-
/**
261-
* @brief TIM period elapsed callback
262-
* @param htim: TIM handle
263-
* @retval None
264-
*/
265-
void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
260+
// This function is called to process outgoing data. We hook directly into the
261+
// SOF (start of frame) callback so that it is called exactly at the time it is
262+
// needed (reducing latency), and often enough (increasing bandwidth).
263+
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
266264
if (!dev_is_connected) {
267265
// CDC device is not connected to a host, so we are unable to send any data
268266
return;
@@ -276,9 +274,8 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
276274
if (UserTxBufPtrOut != UserTxBufPtrOutShadow) {
277275
// We have sent data and are waiting for the low-level USB driver to
278276
// 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;
277+
// SOF occurs every 1ms, so we have a 150 * 1ms = 150ms timeout
278+
if (UserTxBufPtrWaitCount < 150) {
282279
USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
283280
if (USBx_INEP(CDC_IN_EP & 0x7f)->DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ) {
284281
// USB in-endpoint is still reading the data
@@ -457,7 +454,7 @@ void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len) {
457454
}
458455

459456
// Some unused code that makes sure the low-level USB buffer is drained.
460-
// Waiting for low-level is handled in USBD_CDC_HAL_TIM_PeriodElapsedCallback.
457+
// Waiting for low-level is handled in HAL_PCD_SOFCallback.
461458
/*
462459
start = HAL_GetTick();
463460
PCD_HandleTypeDef *hpcd = hUSBDDevice.pData;

stmhal/usbd_cdc_interface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
extern const USBD_CDC_ItfTypeDef USBD_CDC_fops;
3333

34-
void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void);
35-
3634
int USBD_CDC_IsConnected(void);
3735
void USBD_CDC_SetInterrupt(int chr, void *data);
3836

stmhal/usbd_conf.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,13 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
276276
* @param hpcd: PCD handle
277277
* @retval None
278278
*/
279+
/*
280+
This is now handled by the USB CDC interface.
279281
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
280282
{
281283
USBD_LL_SOF(hpcd->pData);
282284
}
285+
*/
283286

284287
/**
285288
* @brief Reset callback.
@@ -394,7 +397,7 @@ if (pdev->id == USB_PHY_FS_ID)
394397
pcd_fs_handle.Init.dma_enable = 0;
395398
pcd_fs_handle.Init.low_power_enable = 0;
396399
pcd_fs_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
397-
pcd_fs_handle.Init.Sof_enable = 0;
400+
pcd_fs_handle.Init.Sof_enable = 1;
398401
pcd_fs_handle.Init.speed = PCD_SPEED_FULL;
399402
#if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
400403
pcd_fs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0

0 commit comments

Comments
 (0)
0