From 4fa41d9c6165ec4ac18e1c5ce54e59c00079ea1a Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 7 Sep 2019 16:07:54 -0400 Subject: [PATCH] stm32: Rework usbotg transmit interrupts Use the XFRC interrupt instead of TXFE. Don't mask/unmask the tx interrupts during runtime. This fixes some race conditions where a tx notification may have previously gotten lost. Signed-off-by: Kevin O'Connor --- src/stm32/usbotg.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/stm32/usbotg.c b/src/stm32/usbotg.c index cf04ea5d..bf8513dc 100644 --- a/src/stm32/usbotg.c +++ b/src/stm32/usbotg.c @@ -169,11 +169,9 @@ usb_send_bulk_in(void *data, uint_fast8_t len) if (!(ctl & USB_OTG_DIEPCTL_USBAEP)) // Controller not enabled - discard data return len; - if (ctl & USB_OTG_DIEPCTL_EPENA) { + if (ctl & USB_OTG_DIEPCTL_EPENA) // Wait for space to transmit - OTGD->DIEPEMPMSK |= (1 << USB_CDC_EP_BULK_IN); return -1; - } return fifo_write_packet(USB_CDC_EP_BULK_IN, data, len); } @@ -235,7 +233,6 @@ usb_send_ep0(const void *data, uint_fast8_t len) } if (EPIN(0)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) { // Wait for space to transmit - OTGD->DIEPEMPMSK |= (1 << 0); OTG->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM; return -1; } @@ -323,11 +320,16 @@ OTG_FS_IRQHandler(void) if (sts & USB_OTG_GINTSTS_IEPINT) { // Can transmit data - disable irq and notify endpoint uint32_t daint = OTGD->DAINT; - OTGD->DIEPEMPMSK &= ~daint; - if (daint & (1 << 0)) + if (daint & (1 << 0)) { + USB_OTG_INEndpointTypeDef *epi = EPIN(0); + epi->DIEPINT = epi->DIEPINT; usb_notify_ep0(); - if (daint & (1 << USB_CDC_EP_BULK_IN)) + } + if (daint & (1 << USB_CDC_EP_BULK_IN)) { + USB_OTG_INEndpointTypeDef *epi = EPIN(USB_CDC_EP_BULK_IN); + epi->DIEPINT = epi->DIEPINT; usb_notify_bulk_in(); + } } } @@ -370,6 +372,7 @@ usb_init(void) // Enable interrupts OTGD->DAINTMSK = (1 << 0) | (1 << USB_CDC_EP_BULK_IN); + OTGD->DIEPMSK = USB_OTG_DIEPMSK_XFRCM; OTG->GINTMSK = USB_OTG_GINTMSK_RXFLVLM | USB_OTG_GINTMSK_IEPINT; OTG->GAHBCFG = USB_OTG_GAHBCFG_GINT; armcm_enable_irq(OTG_FS_IRQHandler, OTG_FS_IRQn, 1);