stm32: Disable and flush usbotg bulk transmit pipe on usb_set_configure()

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-08-30 11:56:15 -04:00
parent 7eba063ac8
commit 4a178824aa
1 changed files with 11 additions and 19 deletions

View File

@ -254,7 +254,7 @@ usb_set_address(uint_fast8_t addr)
void void
usb_set_configure(void) usb_set_configure(void)
{ {
// Configure and enable endpoints // Configure and enable USB_CDC_EP_ACM
USB_OTG_INEndpointTypeDef *epi = EPIN(USB_CDC_EP_ACM); USB_OTG_INEndpointTypeDef *epi = EPIN(USB_CDC_EP_ACM);
epi->DIEPTSIZ = (USB_CDC_EP_ACM_SIZE epi->DIEPTSIZ = (USB_CDC_EP_ACM_SIZE
| (1 << USB_OTG_DIEPTSIZ_PKTCNT_Pos)); | (1 << USB_OTG_DIEPTSIZ_PKTCNT_Pos));
@ -264,6 +264,7 @@ usb_set_configure(void)
| (USB_CDC_EP_ACM << USB_OTG_DIEPCTL_TXFNUM_Pos) | (USB_CDC_EP_ACM << USB_OTG_DIEPCTL_TXFNUM_Pos)
| (USB_CDC_EP_ACM_SIZE << USB_OTG_DIEPCTL_MPSIZ_Pos)); | (USB_CDC_EP_ACM_SIZE << USB_OTG_DIEPCTL_MPSIZ_Pos));
// Configure and enable USB_CDC_EP_BULK_OUT
USB_OTG_OUTEndpointTypeDef *epo = EPOUT(USB_CDC_EP_BULK_OUT); USB_OTG_OUTEndpointTypeDef *epo = EPOUT(USB_CDC_EP_BULK_OUT);
epo->DOEPTSIZ = 64 | (1 << USB_OTG_DOEPTSIZ_PKTCNT_Pos); epo->DOEPTSIZ = 64 | (1 << USB_OTG_DOEPTSIZ_PKTCNT_Pos);
epo->DOEPCTL = ( epo->DOEPCTL = (
@ -271,14 +272,21 @@ usb_set_configure(void)
| (0x02 << USB_OTG_DOEPCTL_EPTYP_Pos) | USB_OTG_DOEPCTL_SD0PID_SEVNFRM | (0x02 << USB_OTG_DOEPCTL_EPTYP_Pos) | USB_OTG_DOEPCTL_SD0PID_SEVNFRM
| (USB_CDC_EP_BULK_OUT_SIZE << USB_OTG_DOEPCTL_MPSIZ_Pos)); | (USB_CDC_EP_BULK_OUT_SIZE << USB_OTG_DOEPCTL_MPSIZ_Pos));
// Configure and flush USB_CDC_EP_BULK_IN
epi = EPIN(USB_CDC_EP_BULK_IN); epi = EPIN(USB_CDC_EP_BULK_IN);
epi->DIEPTSIZ = (USB_CDC_EP_BULK_IN_SIZE epi->DIEPTSIZ = (USB_CDC_EP_BULK_IN_SIZE
| (1 << USB_OTG_DIEPTSIZ_PKTCNT_Pos)); | (1 << USB_OTG_DIEPTSIZ_PKTCNT_Pos));
epi->DIEPCTL = ( epi->DIEPCTL = (
USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_USBAEP
| (0x02 << USB_OTG_DIEPCTL_EPTYP_Pos) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM | (0x02 << USB_OTG_DIEPCTL_EPTYP_Pos) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM
| (USB_CDC_EP_BULK_IN << USB_OTG_DIEPCTL_TXFNUM_Pos) | (USB_CDC_EP_BULK_IN << USB_OTG_DIEPCTL_TXFNUM_Pos)
| (USB_CDC_EP_BULK_IN_SIZE << USB_OTG_DIEPCTL_MPSIZ_Pos)); | (USB_CDC_EP_BULK_IN_SIZE << USB_OTG_DIEPCTL_MPSIZ_Pos));
while (epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA)
;
OTG->GRSTCTL = ((USB_CDC_EP_BULK_IN << USB_OTG_GRSTCTL_TXFNUM_Pos)
| USB_OTG_GRSTCTL_TXFFLSH);
while (OTG->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH)
;
} }
void void
@ -300,11 +308,6 @@ usb_reset(void)
while (OTG->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) while (OTG->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH)
; ;
// Flush Tx queues
OTG->GRSTCTL = (16 << USB_OTG_GRSTCTL_TXFNUM_Pos) | USB_OTG_GRSTCTL_TXFFLSH;
while (OTG->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH)
;
// Configure and enable ep0 // Configure and enable ep0
uint32_t mpsize_ep0 = 2; uint32_t mpsize_ep0 = 2;
USB_OTG_INEndpointTypeDef *epi = EPIN(0); USB_OTG_INEndpointTypeDef *epi = EPIN(0);
@ -315,13 +318,6 @@ usb_reset(void)
epo->DOEPCTL = mpsize_ep0 | USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK; epo->DOEPCTL = mpsize_ep0 | USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
} }
// Handle a USB disconnect
static void
usb_suspend(void)
{
EPIN(USB_CDC_EP_BULK_IN)->DIEPCTL &= ~USB_OTG_DIEPCTL_USBAEP;
}
// Main irq handler // Main irq handler
void void
OTG_FS_IRQHandler(void) OTG_FS_IRQHandler(void)
@ -331,10 +327,6 @@ OTG_FS_IRQHandler(void)
OTG->GINTSTS = USB_OTG_GINTSTS_USBRST; OTG->GINTSTS = USB_OTG_GINTSTS_USBRST;
usb_reset(); usb_reset();
} }
if (sts & USB_OTG_GINTSTS_USBSUSP) {
OTG->GINTSTS = USB_OTG_GINTSTS_USBSUSP;
usb_suspend();
}
if (sts & USB_OTG_GINTSTS_RXFLVL) { if (sts & USB_OTG_GINTSTS_RXFLVL) {
// Received data - disable irq and notify endpoint // Received data - disable irq and notify endpoint
OTG->GINTMSK &= ~USB_OTG_GINTMSK_RXFLVLM; OTG->GINTMSK &= ~USB_OTG_GINTMSK_RXFLVLM;
@ -385,7 +377,7 @@ usb_init(void)
// Enable interrupts // Enable interrupts
OTGD->DAINTMSK = (1 << 0) | (1 << USB_CDC_EP_BULK_IN); OTGD->DAINTMSK = (1 << 0) | (1 << USB_CDC_EP_BULK_IN);
OTG->GINTMSK = (USB_OTG_GINTMSK_USBRST | USB_OTG_GINTSTS_USBSUSP OTG->GINTMSK = (USB_OTG_GINTMSK_USBRST
| USB_OTG_GINTMSK_RXFLVLM | USB_OTG_GINTMSK_IEPINT); | USB_OTG_GINTMSK_RXFLVLM | USB_OTG_GINTMSK_IEPINT);
OTG->GAHBCFG = USB_OTG_GAHBCFG_GINT; OTG->GAHBCFG = USB_OTG_GAHBCFG_GINT;
armcm_enable_irq(OTG_FS_IRQHandler, OTG_FS_IRQn, 1); armcm_enable_irq(OTG_FS_IRQHandler, OTG_FS_IRQn, 1);