summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2020-01-16 11:37:35 +0700
committerGitHub <[email protected]>2020-01-16 11:37:35 +0700
commit4daeb3818571b9f1bf356f13c65e47af91e4ad79 (patch)
treeeff2302b9c7df27d18d632e21ff816a94e64d8b4
parent7b20b80dc0694cfe49b63222f47a8c3d18906e41 (diff)
parent12b191510a4dc759884c9718be92871f57de6850 (diff)
Merge pull request #273 from majbthrd/nucbusreset
nuc121: survive USB bus reset
-rw-r--r--src/portable/nuvoton/nuc121/dcd_nuc121.c61
1 files changed, 33 insertions, 28 deletions
diff --git a/src/portable/nuvoton/nuc121/dcd_nuc121.c b/src/portable/nuvoton/nuc121/dcd_nuc121.c
index 4e0a583e7..25d723d96 100644
--- a/src/portable/nuvoton/nuc121/dcd_nuc121.c
+++ b/src/portable/nuvoton/nuc121/dcd_nuc121.c
@@ -148,30 +148,15 @@ static void dcd_in_xfer(struct xfer_ctl_t *xfer, USBD_EP_T *ep)
ep->MXPLD = bytes_now;
}
-/* centralized location for USBD interrupt enable bit mask */
-static const uint32_t enabled_irqs = USBD_INTSTS_VBDETIF_Msk | USBD_INTSTS_BUSIF_Msk | USBD_INTSTS_SETUP_Msk | USBD_INTSTS_USBIF_Msk | USBD_INTSTS_SOFIF_Msk;
-
-/*
- NUC121/NUC125/NUC126 TinyUSB API driver implementation
-*/
-
-void dcd_init(uint8_t rhport)
+/* called by dcd_init() as well as by the ISR during a USB bus reset */
+static void bus_reset(void)
{
- (void) rhport;
-
-#ifdef SUPPORT_LPM
- USBD->ATTR = 0x7D0 | USBD_LPMACK;
-#else
- USBD->ATTR = 0x7D0;
-#endif
-
- usb_detach();
-
USBD->STBUFSEG = PERIPH_SETUP_BUF_BASE;
for (enum ep_enum ep_index = PERIPH_EP0; ep_index < PERIPH_MAX_EP; ep_index++)
{
- USBD->EP[ep_index].CFGP &= ~USBD_CFG_STATE_Msk;
+ USBD->EP[ep_index].CFG = 0;
+ USBD->EP[ep_index].CFGP = 0;
}
/* allocate the default EP0 endpoints */
@@ -187,6 +172,34 @@ void dcd_init(uint8_t rhport)
/* USB RAM beyond what we've allocated above is available to the user */
bufseg_addr = PERIPH_EP2_BUF_BASE;
+ /* Reset USB device address */
+ USBD->FADDR = 0;
+
+ /* reset EP0_IN flag */
+ active_ep0_xfer = false;
+}
+
+/* centralized location for USBD interrupt enable bit mask */
+static const uint32_t enabled_irqs = USBD_INTSTS_VBDETIF_Msk | USBD_INTSTS_BUSIF_Msk | USBD_INTSTS_SETUP_Msk | USBD_INTSTS_USBIF_Msk | USBD_INTSTS_SOFIF_Msk;
+
+/*
+ NUC121/NUC125/NUC126 TinyUSB API driver implementation
+*/
+
+void dcd_init(uint8_t rhport)
+{
+ (void) rhport;
+
+#ifdef SUPPORT_LPM
+ USBD->ATTR = 0x7D0 | USBD_LPMACK;
+#else
+ USBD->ATTR = 0x7D0;
+#endif
+
+ usb_detach();
+
+ bus_reset();
+
usb_attach();
USBD->INTSTS = enabled_irqs;
@@ -329,15 +342,7 @@ void USBD_IRQHandler(void)
/* USB bus reset */
USBD->ATTR |= USBD_ATTR_USBEN_Msk | USBD_ATTR_PHYEN_Msk;
- /* Reset all endpoints to DATA0 */
- for(enum ep_enum ep_index = PERIPH_EP0; ep_index < PERIPH_MAX_EP; ep_index++)
- USBD->EP[ep_index].CFG &= ~USBD_CFG_DSQSYNC_Msk;
-
- /* Reset USB device address */
- USBD->FADDR = 0;
-
- /* reset EP0_IN flag */
- active_ep0_xfer = false;
+ bus_reset();
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
}