diff options
author | Damien George <damien.p.george@gmail.com> | 2016-01-09 21:59:15 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-01-29 15:21:43 +0000 |
commit | d3631339176b768ce1ffdc535223385245ff906b (patch) | |
tree | 51f2b5063787735a3ec5f936512a0599b1d390ec /stmhal/usbd_cdc_interface.c | |
parent | 7417ccfb0d27c7c09b5ed6468a9e3ca729af41bb (diff) |
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 #1713.
Diffstat (limited to 'stmhal/usbd_cdc_interface.c')
-rw-r--r-- | stmhal/usbd_cdc_interface.c | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/stmhal/usbd_cdc_interface.c b/stmhal/usbd_cdc_interface.c index 61553e27d..801bcb753 100644 --- a/stmhal/usbd_cdc_interface.c +++ b/stmhal/usbd_cdc_interface.c @@ -257,12 +257,10 @@ static int8_t CDC_Itf_Control(uint8_t cmd, uint8_t* pbuf, uint16_t length) { return USBD_OK; } -/** - * @brief TIM period elapsed callback - * @param htim: TIM handle - * @retval None - */ -void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) { +// This function is called to process outgoing data. We hook directly into the +// SOF (start of frame) callback so that it is called exactly at the time it is +// needed (reducing latency), and often enough (increasing bandwidth). +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { if (!dev_is_connected) { // CDC device is not connected to a host, so we are unable to send any data return; @@ -276,9 +274,8 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) { if (UserTxBufPtrOut != UserTxBufPtrOutShadow) { // We have sent data and are waiting for the low-level USB driver to // finish sending it over the USB in-endpoint. - // We have a 15 * 10ms = 150ms timeout - if (UserTxBufPtrWaitCount < 15) { - PCD_HandleTypeDef *hpcd = hUSBDDevice.pData; + // SOF occurs every 1ms, so we have a 150 * 1ms = 150ms timeout + if (UserTxBufPtrWaitCount < 150) { USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; if (USBx_INEP(CDC_IN_EP & 0x7f)->DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ) { // USB in-endpoint is still reading the data @@ -457,7 +454,7 @@ void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len) { } // Some unused code that makes sure the low-level USB buffer is drained. - // Waiting for low-level is handled in USBD_CDC_HAL_TIM_PeriodElapsedCallback. + // Waiting for low-level is handled in HAL_PCD_SOFCallback. /* start = HAL_GetTick(); PCD_HandleTypeDef *hpcd = hUSBDDevice.pData; |