From 1becef7ca990a4c1a5d26ad4256b9e962d2b8ac4 Mon Sep 17 00:00:00 2001 From: Marcus Eliasson Date: Wed, 5 Jun 2019 13:41:01 +0200 Subject: [PATCH] Improvements on USB communication, fixes #393 --- src/hal/src/usb.c | 25 ++++++++----------- src/hal/src/usblink.c | 2 +- .../STM32_USB_OTG_Driver/src/usb_dcd_int.c | 16 ++++++------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/hal/src/usb.c b/src/hal/src/usb.c index 3aeaff036a..5ba2ab9e52 100644 --- a/src/hal/src/usb.c +++ b/src/hal/src/usb.c @@ -51,6 +51,7 @@ __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END ; static bool isInit = false; +static bool doingTransfer = false; static xQueueHandle usbDataRx; static xQueueHandle usbDataTx; @@ -219,15 +220,8 @@ static uint8_t usbd_cf_DeInit (void *pdev, */ static uint8_t usbd_cf_DataIn (void *pdev, uint8_t epnum) { - portBASE_TYPE xTaskWokenByReceive = pdFALSE; - if (xQueueReceiveFromISR(usbDataTx, &outPacket, &xTaskWokenByReceive) == pdTRUE) - { - DCD_EP_Tx (pdev, - IN_EP, - (uint8_t*)outPacket.data, - outPacket.size); - } + doingTransfer = false; return USBD_OK; } @@ -236,12 +230,15 @@ static uint8_t usbd_cf_SOF (void *pdev) { portBASE_TYPE xTaskWokenByReceive = pdFALSE; - if (xQueueReceiveFromISR(usbDataTx, &outPacket, &xTaskWokenByReceive) == pdTRUE) - { - DCD_EP_Tx (pdev, - IN_EP, - (uint8_t*)outPacket.data, - outPacket.size); + if (!doingTransfer) { + if (xQueueReceiveFromISR(usbDataTx, &outPacket, &xTaskWokenByReceive) == pdTRUE) + { + doingTransfer = true; + DCD_EP_Tx (pdev, + IN_EP, + (uint8_t*)outPacket.data, + outPacket.size); + } } return USBD_OK; diff --git a/src/hal/src/usblink.c b/src/hal/src/usblink.c index 4ee0663a48..c53d04b3c4 100644 --- a/src/hal/src/usblink.c +++ b/src/hal/src/usblink.c @@ -73,7 +73,7 @@ static void usblinkTask(void *param) p.size = usbIn.size - 1; memcpy(&p.raw, usbIn.data, usbIn.size); // This queuing will copy a CRTP packet size from usbIn - xQueueSend(crtpPacketDelivery, &p, 0); + ASSERT(xQueueSend(crtpPacketDelivery, &p, 0) == pdTRUE); } } diff --git a/src/lib/STM32_USB_OTG_Driver/src/usb_dcd_int.c b/src/lib/STM32_USB_OTG_Driver/src/usb_dcd_int.c index 7bd66b4b86..6d3dde11fc 100644 --- a/src/lib/STM32_USB_OTG_Driver/src/usb_dcd_int.c +++ b/src/lib/STM32_USB_OTG_Driver/src/usb_dcd_int.c @@ -434,9 +434,10 @@ static uint32_t DCD_HandleInEP_ISR(USB_OTG_CORE_HANDLE *pdev) uint32_t ep_intr; uint32_t epnum = 0; + uint32_t fifoemptymsk; diepint.d32 = 0; ep_intr = USB_OTG_ReadDevAllInEPItr(pdev); - + while ( ep_intr ) { if (ep_intr&0x1) /* In ITR */ @@ -475,9 +476,13 @@ static uint32_t DCD_HandleInEP_ISR(USB_OTG_CORE_HANDLE *pdev) } if (diepint.b.emptyintr) { - + DCD_WriteEmptyTxFifo(pdev , epnum); + fifoemptymsk = 0x1 << epnum; + USB_OTG_MODIFY_REG32(&pdev->regs.DREGS->DIEPEMPMSK, fifoemptymsk, 0); + + CLEAR_IN_EP_INTR(epnum, emptyintr); } } epnum++; @@ -681,13 +686,6 @@ static uint32_t DCD_WriteEmptyTxFifo(USB_OTG_CORE_HANDLE *pdev, uint32_t epnum) txstatus.d32 = USB_OTG_READ_REG32(&pdev->regs.INEP_REGS[epnum]->DTXFSTS); } - - /* Switch off FIFO empty after all the data has been queued up */ - if (len < ep->maxpacket) - { - uint32_t fifoemptymsk = 0x1 << epnum; - USB_OTG_MODIFY_REG32(&pdev->regs.DREGS->DIEPEMPMSK, fifoemptymsk, 0); - } return 1; }