From eda56769b9126460f7721a9b3f04d4e4f6efd60d Mon Sep 17 00:00:00 2001
From: hathach <thach@tinyusb.org>
Date: Wed, 13 Mar 2019 23:14:48 +0700
Subject: [PATCH] dcd_set_address include status response, usbd control stall
 both control in and out

---
 README.md                                     |  5 ++--
 .../cdc_msc_hid/ses/cdc_msc_hid.emProject     |  1 +
 examples/device/cdc_msc_hid/src/tusb_config.h |  2 +-
 hw/bsp/mcb1800/board_mcb1800.c                | 23 ++++++++++++-------
 hw/bsp/readme.md                              | 10 +++++---
 src/device/dcd.h                              | 10 +++++---
 src/device/usbd.c                             |  4 ++--
 src/device/usbd_control.c                     |  4 +++-
 src/device/usbd_pvt.h                         |  2 +-
 src/portable/microchip/samd21/dcd_samd21.c    |  9 +++-----
 src/portable/microchip/samd51/dcd_samd51.c    |  9 +++-----
 src/portable/nordic/nrf5x/dcd_nrf5x.c         |  2 +-
 .../nxp/lpc11_13_15/dcd_lpc11_13_15.c         | 16 ++++---------
 src/portable/nxp/lpc17_40/dcd_lpc17_40.c      |  6 +++--
 src/portable/nxp/lpc18_43/dcd_lpc18_43.c      | 12 ++++------
 src/portable/st/stm32f4/dcd_stm32f4.c         |  3 +++
 16 files changed, 63 insertions(+), 55 deletions(-)

diff --git a/README.md b/README.md
index a165001be..4133173c8 100644
--- a/README.md
+++ b/README.md
@@ -45,9 +45,10 @@ Currently the following OS are supported with tinyusb out of the box with a simp
 
 The stack supports the following MCUs
 
+  - **Nordic:** nRF52840
   - **NXP:** LPC11Uxx, LPC13xx, LPC175x_6x, LPC177x_8x, LPC40xx, LPC43xx
   - **MicroChip:** SAMD21, SAMD51 (device only)
-  - **Nordic:** nRF52840
+  - **ST* STM32F4
 
 [Here is the list of supported Boards](hw/bsp/readme.md)
 
@@ -64,8 +65,8 @@ The stack is developed with GCC compiler, and should be compilable with others.
 TinyUSB is currently used by these other projects:
 
 * [Adafruit nRF52 Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
+* [Adafruit nRF52 Bootloader](https://github.com/adafruit/Adafruit_nRF52_Bootloader)
 * [CircuitPython](https://github.com/adafruit/circuitpython)
-* [nRF52840 UF2 Bootloader](https://github.com/adafruit/Adafruit_nRF52_Bootloader)
 
 ## Porting
 
diff --git a/examples/device/cdc_msc_hid/ses/cdc_msc_hid.emProject b/examples/device/cdc_msc_hid/ses/cdc_msc_hid.emProject
index 9d0672b8c..c02f7a588 100644
--- a/examples/device/cdc_msc_hid/ses/cdc_msc_hid.emProject
+++ b/examples/device/cdc_msc_hid/ses/cdc_msc_hid.emProject
@@ -9,4 +9,5 @@
   <import file_name="lpc11u6x/lpc11u6x.emProject" />
   <import file_name="lpc18xx/lpc18xx.emProject" />
   <import file_name="lpc40xx/lpc40xx.emProject" />
+  <import file_name="stm32f4/stm32f4.emProject" />
 </solution>
diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h
index 3f748c3f0..bd9e9e06a 100644
--- a/examples/device/cdc_msc_hid/src/tusb_config.h
+++ b/examples/device/cdc_msc_hid/src/tusb_config.h
@@ -97,7 +97,7 @@
 // #define CFG_TUD_DESC_PID          0x0001
 
 // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
-// Therefor we need to force endpoint number to correct type on lpc17xx
+// Therefore we need to force endpoint number to correct type on lpc17xx
 #if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
 #define CFG_TUD_DESC_CDC_EPNUM_NOTIF      1
 #define CFG_TUD_DESC_CDC_EPNUM            2
diff --git a/hw/bsp/mcb1800/board_mcb1800.c b/hw/bsp/mcb1800/board_mcb1800.c
index a321ffe61..e76b19a9f 100644
--- a/hw/bsp/mcb1800/board_mcb1800.c
+++ b/hw/bsp/mcb1800/board_mcb1800.c
@@ -88,14 +88,14 @@ static const PINMUX_GRP_T pinclockmuxing[] =
 // Invoked by startup code
 void SystemInit(void)
 {
-	/* Setup system level pin muxing */
-	Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
+  /* Setup system level pin muxing */
+  Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
 
-	/* Clock pins only, group field not used */
-	for (int i = 0; i < (sizeof(pinclockmuxing) / sizeof(pinclockmuxing[0])); i++)
-	{
-		Chip_SCU_ClockPinMuxSet(pinclockmuxing[i].pinnum, pinclockmuxing[i].modefunc);
-	}
+  /* Clock pins only, group field not used */
+  for (uint32_t i = 0; i < (sizeof(pinclockmuxing) / sizeof(pinclockmuxing[0])); i++)
+  {
+    Chip_SCU_ClockPinMuxSet(pinclockmuxing[i].pinnum, pinclockmuxing[i].modefunc);
+  }
 
   Chip_SetupXtalClocking();
 }
@@ -115,7 +115,7 @@ void board_init(void)
   const uint8_t ledports[] = {6, 6, 6, 6, 6, 4, 4, 4};
   const uint8_t ledbits[] = {24, 25, 26, 27, 28, 12, 13, 14};
 
-  for (int i = 0; i < (sizeof(ledports) / sizeof(ledports[0])); i++) 
+  for (uint32_t i = 0; i < (sizeof(ledports) / sizeof(ledports[0])); i++) 
   {
     Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, ledports[i], ledbits[i]);
     Chip_GPIO_SetPinState(LPC_GPIO_PORT, LED_PORT, LED_PIN, false);
@@ -197,10 +197,14 @@ void board_led_control(bool state)
 }
 
 //------------- Buttons -------------//
+/*
 static bool button_read(uint8_t id)
 {
+  (void) id;
 //  return !TU_BIT_TEST( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low
+  return false;
 }
+*/
 
 uint32_t board_buttons(void)
 {
@@ -216,9 +220,12 @@ uint32_t board_buttons(void)
 uint8_t  board_uart_getchar(void)
 {
   //return UART_ReceiveByte(BOARD_UART_PORT);
+  return 0;
 }
+
 void board_uart_putchar(uint8_t c)
 {
+  (void) c;
   //UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING);
 }
 
diff --git a/hw/bsp/readme.md b/hw/bsp/readme.md
index b4105046f..03cd9e08e 100644
--- a/hw/bsp/readme.md
+++ b/hw/bsp/readme.md
@@ -4,7 +4,11 @@
 
 This code base already had supported for a handful of following boards
 
-### NXP MCU ###
+### Nordic nRF5x ###
+
+- [nRF52840-DK (aka pca10056)](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK)
+
+### NXP LPC ###
 
 - [LPCXpresso 11U68](https://www.nxp.com/support/developer-resources/evaluation-and-development-boards/lpcxpresso-boards/lpcxpresso-board-for-lpc11u68:OM13058)
 - [LPCXpresso 1347](https://www.nxp.com/support/developer-resources/evaluation-and-development-boards/lpcxpresso-boards/lpcxpresso-board-for-lpc1347:OM13045)
@@ -18,9 +22,9 @@ This code base already had supported for a handful of following boards
 - [Adafruit Metro M0 Express](https://www.adafruit.com/product/3505)
 - [Adafruit Metro M4 Express](https://www.adafruit.com/product/3382)
 
-### Nordic nRF52840 ###
+### ST STM32 ###
 
-- [nRF52840-DK (aka pca10056)](https://www.nordicsemi.com/eng/Products/nRF52840-DK)
+- [STM32F4DISCOVERY](https://www.st.com/en/evaluation-tools/stm32f4discovery.html)
 
 ## Add your own board ##
 
diff --git a/src/device/dcd.h b/src/device/dcd.h
index abd85a70b..46c11ff1c 100644
--- a/src/device/dcd.h
+++ b/src/device/dcd.h
@@ -94,10 +94,16 @@ TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
  *------------------------------------------------------------------*/
 bool dcd_init       (uint8_t rhport);
 
+// Enable device interrupt
 void dcd_int_enable (uint8_t rhport);
+
+// Disable device interrupt
 void dcd_int_disable(uint8_t rhport);
 
+// Receive Set Address request, mcu port must also include status IN response
 void dcd_set_address(uint8_t rhport, uint8_t dev_addr);
+
+// Receive Set Config request
 void dcd_set_config (uint8_t rhport, uint8_t config_num);
 
 // Get current frame number
@@ -118,15 +124,13 @@ void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr
 // helper to send transfer complete event
 void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
 
-
 /*------------------------------------------------------------------*/
 /* Endpoint API
  *  - open        : Configure endpoint's registers
  *  - xfer        : Submit a transfer. When complete dcd_event_xfer_complete
  *                  must be called to notify the stack
  *  - busy        : Check if endpoint transferring is complete (TODO remove)
- *  - stall       : stall ep. When control endpoint (addr = 0) is stalled,
- *                  both direction (IN & OUT) of control ep must be stalled.
+ *  - stall       : stall endpoint
  *  - clear_stall : clear stall
  *  - stalled     : check if stalled ( TODO remove )
  *------------------------------------------------------------------*/
diff --git a/src/device/usbd.c b/src/device/usbd.c
index 5baea8460..d07c78c13 100644
--- a/src/device/usbd.c
+++ b/src/device/usbd.c
@@ -336,8 +336,8 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
     switch ( p_request->bRequest )
     {
       case TUSB_REQ_SET_ADDRESS:
-        // response with status first before changing device address
-        usbd_control_status(rhport, p_request);
+        // DCD must include zero-length status response since depending on mcu,
+        // status could be sent either before or after changing device address
         dcd_set_address(rhport, (uint8_t) p_request->wValue);
         return true; // skip the rest
       break;
diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c
index ae7e894b7..f06c44f02 100644
--- a/src/device/usbd_control.c
+++ b/src/device/usbd_control.c
@@ -74,7 +74,9 @@ void usbd_control_reset (uint8_t rhport)
 
 void usbd_control_stall(uint8_t rhport)
 {
-  dcd_edpt_stall(rhport, 0);
+  // when stalling control endpoint both IN and OUt will be stalled
+  dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
+  dcd_edpt_stall(rhport, EDPT_CTRL_IN);
 }
 
 bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request)
diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h
index 06951673f..14f880e76 100644
--- a/src/device/usbd_pvt.h
+++ b/src/device/usbd_pvt.h
@@ -61,7 +61,7 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v
 // Send STATUS (zero length) packet
 bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request);
 
-// Stall control endpoint until new setup packet arrived
+// Stall control endpoint (both IN and OUT) until new setup packet arrived
 void usbd_control_stall(uint8_t rhport);
 
 /*------------------------------------------------------------------*/
diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c
index 6140812bc..130825f74 100644
--- a/src/portable/microchip/samd21/dcd_samd21.c
+++ b/src/portable/microchip/samd21/dcd_samd21.c
@@ -110,10 +110,12 @@ void dcd_int_disable(uint8_t rhport)
 
 void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
 {
-  (void) rhport;
+  // Response with status first before changing device address
+  dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
 
   // Wait for EP0 to finish before switching the address.
   while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
+
   USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN;
 }
 
@@ -230,11 +232,6 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
       ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
   } else {
       ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
-
-      // for control, stall both IN & OUT
-      if (ep_addr == 0) {
-        ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
-      }
   }
 }
 
diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c
index 1de3fadb2..27c5c4db4 100644
--- a/src/portable/microchip/samd51/dcd_samd51.c
+++ b/src/portable/microchip/samd51/dcd_samd51.c
@@ -115,10 +115,12 @@ void dcd_int_disable(uint8_t rhport)
 
 void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
 {
-  (void) rhport;
+  // Response with status first before changing device address
+  dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
 
   // Wait for EP0 to finish before switching the address.
   while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
+
   USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN;
 }
 
@@ -234,11 +236,6 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
       ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
   } else {
       ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
-
-      // for control, stall both IN & OUT
-      if (ep_addr == 0) {
-        ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1;
-      }
   }
 }
 
diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c
index 49984eae8..1280fe7c2 100644
--- a/src/portable/nordic/nrf5x/dcd_nrf5x.c
+++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c
@@ -211,7 +211,7 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
 {
   (void) rhport;
   (void) dev_addr;
-  // Set Address is automatically update by hw controller
+  // Set Address is automatically update by hw controller, nothing to do
 }
 
 void dcd_set_config (uint8_t rhport, uint8_t config_num)
diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c
index a9ba5e336..36dcf88ff 100644
--- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c
+++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c
@@ -159,7 +159,8 @@ void dcd_set_config(uint8_t rhport, uint8_t config_num)
 
 void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
 {
-  (void) rhport;
+  // Response with status first before changing device address
+  dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
 
   LPC_USB->DEVCMDSTAT &= ~CMDSTAT_DEVICE_ADDR_MASK;
   LPC_USB->DEVCMDSTAT |= dev_addr;
@@ -196,16 +197,9 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
 {
   (void) rhport;
 
-  if ( tu_edpt_number(ep_addr) == 0 )
-  {
-    // TODO cannot able to STALL Control OUT endpoint !!!!! FIXME try some walk-around
-    _dcd.ep[0][0].stall = _dcd.ep[1][0].stall = 1;
-  }
-  else
-  {
-    uint8_t const ep_id = ep_addr2id(ep_addr);
-    _dcd.ep[ep_id][0].stall = 1;
-  }
+  // TODO cannot able to STALL Control OUT endpoint !!!!! FIXME try some walk-around
+  uint8_t const ep_id = ep_addr2id(ep_addr);
+  _dcd.ep[ep_id][0].stall = 1;
 }
 
 bool dcd_edpt_stalled(uint8_t rhport, uint8_t ep_addr)
diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c
index e3286b1cb..e0ccbf96a 100644
--- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c
+++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c
@@ -199,7 +199,7 @@ bool dcd_init(uint8_t rhport)
   NVIC_ClearPendingIRQ(USB_IRQn);
   NVIC_EnableIRQ(USB_IRQn);
 
-  return TUSB_ERROR_NONE;
+  return true;
 }
 
 void dcd_int_enable(uint8_t rhport)
@@ -216,7 +216,9 @@ void dcd_int_disable(uint8_t rhport)
 
 void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
 {
-  (void) rhport;
+  // Response with status first before changing device address
+  dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
+
   sie_write(SIE_CMDCODE_SET_ADDRESS, 1, 0x80 | dev_addr); // 7th bit is : device_enable
 }
 
diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c
index 65b0728eb..7731b3f03 100644
--- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c
+++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c
@@ -164,6 +164,9 @@ void dcd_int_disable(uint8_t rhport)
 
 void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
 {
+  // Response with status first before changing device address
+  dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
+
   LPC_USB[rhport]->DEVICEADDR = (dev_addr << 25) | TU_BIT(24);
 }
 
@@ -214,14 +217,7 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
   uint8_t const epnum  = tu_edpt_number(ep_addr);
   uint8_t const dir    = tu_edpt_dir(ep_addr);
 
-  if ( epnum == 0)
-  {
-    // Stall both Control IN and OUT
-    LPC_USB[rhport]->ENDPTCTRL[epnum] |= ( (ENDPTCTRL_MASK_STALL << 16) || (ENDPTCTRL_MASK_STALL << 0) );
-  }else
-  {
-    LPC_USB[rhport]->ENDPTCTRL[epnum] |= ENDPTCTRL_MASK_STALL << (dir ? 16 : 0);
-  }
+  LPC_USB[rhport]->ENDPTCTRL[epnum] |= ENDPTCTRL_MASK_STALL << (dir ? 16 : 0);
 }
 
 bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr)
diff --git a/src/portable/st/stm32f4/dcd_stm32f4.c b/src/portable/st/stm32f4/dcd_stm32f4.c
index e3b7b2db4..dace9ed3e 100644
--- a/src/portable/st/stm32f4/dcd_stm32f4.c
+++ b/src/portable/st/stm32f4/dcd_stm32f4.c
@@ -182,6 +182,9 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
   USB_OTG_DeviceTypeDef * dev = DEVICE_BASE;
 
   dev->DCFG |= (dev_addr << USB_OTG_DCFG_DAD_Pos) & USB_OTG_DCFG_DAD_Msk;
+
+  // Response with status after changing device address
+  dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
 }
 
 void dcd_set_config (uint8_t rhport, uint8_t config_num)