From c14c0cfc9fec9101b63df682c5fa5ac0576de0f8 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 29 Apr 2020 10:49:58 +0700 Subject: [PATCH 01/52] support class drivers implemented by application --- src/device/usbd.c | 116 +++++++++++++++++++++++------------------- src/device/usbd_pvt.h | 23 +++++++++ 2 files changed, 88 insertions(+), 51 deletions(-) diff --git a/src/device/usbd.c b/src/device/usbd.c index 1f660f0f2..432786140 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -79,21 +79,7 @@ enum { DRVID_INVALID = 0xFFu }; #define DRIVER_NAME(_name) #endif -typedef struct -{ - #if CFG_TUSB_DEBUG >= 2 - char const* name; - #endif - - void (* init ) (void); - void (* reset ) (uint8_t rhport); - bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); - bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); - bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request); - bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); - void (* sof ) (uint8_t rhport); /* optional */ -} usbd_class_driver_t; - +// Built-in class drivers static usbd_class_driver_t const _usbd_driver[] = { #if CFG_TUD_CDC @@ -203,6 +189,29 @@ static usbd_class_driver_t const _usbd_driver[] = enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) }; +// Additional class drivers implemented by application +static usbd_class_driver_t const * _app_driver = NULL; +static uint8_t _app_driver_count = 0; + +// virtually joins built-in and application drivers together +// All drivers = built-in + application +static inline usbd_class_driver_t const * get_driver(uint8_t drvid) +{ + // Built-in drivers + if (drvid < USBD_CLASS_DRIVER_COUNT) return &_usbd_driver[drvid]; + + // App drivers + if ( usbd_app_driver_get_cb ) + { + drvid -= USBD_CLASS_DRIVER_COUNT; + if ( drvid < _app_driver_count ) return &_app_driver[drvid]; + } + + return NULL; +} + +#define TOTAL_DRIVER_COUNT (USBD_CLASS_DRIVER_COUNT+_app_driver_count) + //--------------------------------------------------------------------+ // DCD Event //--------------------------------------------------------------------+ @@ -263,11 +272,12 @@ static char const* const _tusb_std_request_str[] = // for usbd_control to print the name of control complete driver void usbd_driver_print_control_complete_name(bool (*control_complete) (uint8_t, tusb_control_request_t const * )) { - for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { - if (_usbd_driver[i].control_complete == control_complete ) + usbd_class_driver_t const * driver = get_driver(i); + if ( driver->control_complete == control_complete ) { - TU_LOG2(" %s control complete\r\n", _usbd_driver[i].name); + TU_LOG2(" %s control complete\r\n", driver->name); return; } } @@ -309,11 +319,18 @@ bool tud_init (void) _usbd_q = osal_queue_create(&_usbd_qdef); TU_ASSERT(_usbd_q != NULL); - // Init class drivers - for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) + // Get application driver if available + if ( usbd_app_driver_get_cb ) { - TU_LOG2("%s init\r\n", _usbd_driver[i].name); - _usbd_driver[i].init(); + _app_driver = usbd_app_driver_get_cb(&_app_driver_count); + } + + // Init class drivers + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) + { + usbd_class_driver_t const * driver = get_driver(i); + TU_LOG2("%s init\r\n", driver->name); + driver->init(); } // Init device controller driver @@ -333,9 +350,9 @@ static void usbd_reset(uint8_t rhport) usbd_control_reset(); - for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) + for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ ) { - if ( _usbd_driver[i].reset ) _usbd_driver[i].reset( rhport ); + get_driver(i)->reset(rhport); } } @@ -420,11 +437,11 @@ void tud_task (void) } else { - uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir]; - TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,); + usbd_class_driver_t const * driver = get_driver( _usbd_dev.ep2drv[epnum][ep_dir] ); + TU_ASSERT(driver, ); - TU_LOG2(" %s xfer callback\r\n", _usbd_driver[drv_id].name); - _usbd_driver[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len); + TU_LOG2(" %s xfer callback\r\n", driver->name); + driver->xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len); } } break; @@ -438,12 +455,10 @@ void tud_task (void) break; case DCD_EVENT_SOF: - for ( uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++ ) + for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ ) { - if ( _usbd_driver[i].sof ) - { - _usbd_driver[i].sof(event.rhport); - } + usbd_class_driver_t const * driver = get_driver(i); + if ( driver->sof ) driver->sof(event.rhport); } break; @@ -463,11 +478,11 @@ void tud_task (void) //--------------------------------------------------------------------+ // Helper to invoke class driver control request handler -static bool invoke_class_control(uint8_t rhport, uint8_t drvid, tusb_control_request_t const * request) +static bool invoke_class_control(uint8_t rhport, usbd_class_driver_t const * driver, tusb_control_request_t const * request) { - usbd_control_set_complete_callback(_usbd_driver[drvid].control_complete); - TU_LOG2(" %s control request\r\n", _usbd_driver[drvid].name); - return _usbd_driver[drvid].control_request(rhport, request); + usbd_control_set_complete_callback(driver->control_complete); + TU_LOG2(" %s control request\r\n", driver->name); + return driver->control_request(rhport, request); } // This handles the actual request and its response. @@ -580,12 +595,12 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const uint8_t const itf = tu_u16_low(p_request->wIndex); TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv)); - uint8_t const drvid = _usbd_dev.itf2drv[itf]; - TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT); + usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]); + TU_VERIFY(driver); // all requests to Interface (STD or Class) is forwarded to class driver. // notable requests are: GET HID REPORT DESCRIPTOR, SET_INTERFACE, GET_INTERFACE - if ( !invoke_class_control(rhport, drvid, p_request) ) + if ( !invoke_class_control(rhport, driver, p_request) ) { // For GET_INTERFACE, it is mandatory to respond even if the class // driver doesn't use alternate settings. @@ -607,8 +622,6 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) ); - uint8_t const drvid = _usbd_dev.ep2drv[ep_num][ep_dir]; - bool ret = false; // Handle STD request to endpoint @@ -647,16 +660,17 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const } } - if (drvid < 0xFF) { - TU_ASSERT(drvid < USBD_CLASS_DRIVER_COUNT); - + usbd_class_driver_t const * driver = get_driver(_usbd_dev.ep2drv[ep_num][ep_dir]); + + if (driver) + { // Some classes such as USBTMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request // We will forward all request targeted endpoint to class drivers after // - For class-type requests: driver is fully responsible to reply to host // - For std-type requests : driver init/re-init internal variable/buffer only, and // must not call tud_control_status(), driver's return value will have no effect. // EP state has already affected (stalled/cleared) - if ( invoke_class_control(rhport, drvid, p_request) ) ret = true; + if ( invoke_class_control(rhport, driver, p_request) ) ret = true; } if ( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type ) @@ -708,17 +722,17 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) uint8_t drv_id; uint16_t drv_len; - for (drv_id = 0; drv_id < USBD_CLASS_DRIVER_COUNT; drv_id++) + for (drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { - usbd_class_driver_t const *driver = &_usbd_driver[drv_id]; + usbd_class_driver_t const *driver = get_driver(drv_id); drv_len = 0; if ( driver->open(rhport, desc_itf, &drv_len) ) { // Interface number must not be used already - TU_ASSERT( DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] ); + TU_ASSERT(DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber]); - TU_LOG2(" %s opened\r\n", _usbd_driver[drv_id].name); + TU_LOG2(" %s opened\r\n", driver->name); _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id; // If IAD exist, assign all interfaces to the same driver @@ -741,7 +755,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) } // Assert if cannot find supported driver - TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT && drv_len >= sizeof(tusb_desc_interface_t) ); + TU_ASSERT( drv_id < TOTAL_DRIVER_COUNT && drv_len >= sizeof(tusb_desc_interface_t) ); mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, drv_len, drv_id); // TODO refactor diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 48188ec99..d6eb289c1 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -33,6 +33,29 @@ extern "C" { #endif +//--------------------------------------------------------------------+ +// Class Drivers +//--------------------------------------------------------------------+ + +typedef struct +{ +#if CFG_TUSB_DEBUG >= 2 + char const* name; +#endif + + void (* init ) (void); + void (* reset ) (uint8_t rhport); + bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); + bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); + bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request); + bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); + void (* sof ) (uint8_t rhport); /* optional */ +} usbd_class_driver_t; + +// Invoked when initializing device stack to get additional class drivers. +// Can optionally implemented by application to extend/overwrite class driver support. +usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK; + //--------------------------------------------------------------------+ // USBD Endpoint API //--------------------------------------------------------------------+ From 9538ca7d74fbf5644c1e3a9747f0ad0c3e1a28d2 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 May 2020 14:50:12 +0700 Subject: [PATCH 02/52] add uart for mcb1800 --- hw/bsp/ea4357/ea4357.c | 61 +++++++++++++++++++++++----------------- hw/bsp/mcb1800/mcb1800.c | 36 ++++++++++++++---------- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index b7e7f7a5c..30d899f69 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -30,8 +30,8 @@ #define UART_DEV LPC_USART0 #define UART_PORT 0x0f -#define UART_PIN_TX 10 // PF.10 : UART0_TXD -#define UART_PIN_RX 11 // PF.11 : UART0_RXD +#define UART_PIN_TX 10 +#define UART_PIN_RX 11 // P9_1 joystick down #define BUTTON_PORT 4 @@ -66,6 +66,10 @@ static const PINMUX_GRP_T pinmuxing[] = // Button {0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP)}, + // UART + {UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC1}, + {UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC1}, + // USB }; @@ -119,9 +123,6 @@ void board_init(void) Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN); //------------- UART -------------// - Chip_SCU_PinMuxSet(UART_PORT, UART_PIN_TX, (SCU_MODE_PULLDOWN | SCU_MODE_FUNC1)); - Chip_SCU_PinMuxSet(UART_PORT, UART_PIN_RX, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC1)); - Chip_UART_Init(UART_DEV); Chip_UART_SetBaud(UART_DEV, CFG_BOARD_UART_BAUDRATE); Chip_UART_ConfigData(UART_DEV, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS); @@ -139,15 +140,20 @@ void board_init(void) }; /* USB0 - * For USB Device operation; insert jumpers in position 1-2 in JP17/JP18/JP19. GPIO28 controls USB - * connect functionality and LED32 lights when the USB Device is connected. SJ4 has pads 1-2 shorted - * by default. LED33 is controlled by GPIO27 and signals USB-up state. GPIO54 is used for VBUS + * For USB0 Device operation: + * - insert jumpers in position 1-2 in JP17/JP18/JP19. + * - GPIO28 controls USB connect functionality + * - LED32 lights when the USB Device is connected. SJ4 has pads 1-2 shorted by default. + * - LED33 is controlled by GPIO27 and signals USB-up state. GPIO54 is used for VBUS * sensing. - * For USB Host operation; insert jumpers in position 2-3 in JP17/JP18/JP19. USB Host power is - * controlled via distribution switch U20 (found in schematic page 11). Signal GPIO26 is active low and - * enables +5V on VBUS2. LED35 light whenever +5V is present on VBUS2. GPIO55 is connected to - * status feedback from the distribution switch. GPIO54 is used for VBUS sensing. 15Kohm pull-down - * resistors are always active + * + * For USB0 Host operation: + * - insert jumpers in position 2-3 in JP17/JP18/JP19. + * - USB Host power is controlled via distribution switch U20 (found in schematic page 11). + * - Signal GPIO26 is active low and enables +5V on VBUS2. + * - LED35 light whenever +5V is present on VBUS2. + * - GPIO55 is connected to status feedback from the distribution switch. + * - GPIO54 is used for VBUS sensing. 15Kohm pull-down resistors are always active */ #if CFG_TUSB_RHPORT0_MODE Chip_USB0_Init(); @@ -168,22 +174,25 @@ void board_init(void) #endif /* USB1 - * When USB channel #1 is used as USB Host, 15Kohm pull-down resistors are needed on the USB data - * signals. These are activated inside the USB OTG chip (U31), and this has to be done via the I2C - * interface of GPIO52/GPIO53. - * J20 is the connector to use when USB Host is used. In order to provide +5V to the external USB - * device connected to this connector (J20), channel A of U20 must be enabled. It is enabled by default - * since SJ5 is normally connected between pin 1-2. LED34 lights green when +5V is available on J20. - * JP15 shall not be inserted. JP16 has no effect * - * When USB channel #1 is used as USB Device, a 1.5Kohm pull-up resistor is needed on the USB DP - * data signal. There are two methods to create this. JP15 is inserted and the pull-up resistor is always - * enabled. Alternatively, the pull-up resistor is activated inside the USB OTG chip (U31), and this has to - * be done via the I2C interface of GPIO52/GPIO53. In the latter case, JP15 shall not be inserted. - * J19 is the connector to use when USB Device is used. Normally it should be a USB-B connector for + * For USB Device: + * - a 1.5Kohm pull-up resistor is needed on the USB DP data signal. There are two methods to create this. + * JP15 is inserted and the pull-up resistor is always enabled. Alternatively, the pull-up resistor is activated + * inside the USB OTG chip (U31), and this has to be done via the I2C interface of GPIO52/GPIO53. In the latter case, + * JP15 shall not be inserted. + * - J19 is the connector to use when USB Device is used. Normally it should be a USB-B connector for * creating a USB Device interface, but the mini-AB connector can also be used in this case. The status * of VBUS can be read via U31. - * JP16 shall not be inserted. + * - JP16 shall not be inserted. + * + * For USB Host: + * - 15Kohm pull-down resistors are needed on the USB data signals. These are activated inside the USB OTG chip (U31), + * and this has to be done via the I2C interface of GPIO52/GPIO53. + * - J20 is the connector to use when USB Host is used. In order to provide +5V to the external USB + * device connected to this connector (J20), channel A of U20 must be enabled. It is enabled by default + * since SJ5 is normally connected between pin 1-2. + * - LED34 lights green when +5V is available on J20. + * - JP15 shall not be inserted. JP16 has no effect */ #if CFG_TUSB_RHPORT1_MODE Chip_USB1_Init(); diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index ae9f1b62f..075210c85 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -64,6 +64,11 @@ void USB1_IRQHandler(void) #define BUTTON_PORT 2 #define BUTTON_PIN 0 +#define UART_DEV LPC_USART3 +#define UART_PORT 0x02 +#define UART_PIN_TX 3 +#define UART_PIN_RX 4 + /* System configuration variables used by chip driver */ const uint32_t OscRateIn = 12000000; @@ -84,6 +89,10 @@ static const PINMUX_GRP_T pinmuxing[] = // Button {0x4, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP)}, + // UART + {UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC2 }, + {UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, + /* I2S */ {0x3, 0, (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, {0x6, 0, (SCU_PINIO_FAST | SCU_MODE_FUNC4)}, @@ -137,19 +146,11 @@ void board_init(void) // Button Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN); -#if 0 //------------- UART -------------// - scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_TX, MD_PDN, FUNC1); - scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_RX, MD_PLN | MD_EZI | MD_ZI, FUNC1); - - UART_CFG_Type UARTConfigStruct; - UART_ConfigStructInit(&UARTConfigStruct); - UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; - UARTConfigStruct.Clock_Speed = 0; - - UART_Init(BOARD_UART_PORT, &UARTConfigStruct); - UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit -#endif + Chip_UART_Init(UART_DEV); + Chip_UART_SetBaud(UART_DEV, CFG_BOARD_UART_BAUDRATE); + Chip_UART_ConfigData(UART_DEV, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS); + Chip_UART_TXEnable(UART_DEV); //------------- USB -------------// enum { @@ -223,9 +224,14 @@ int board_uart_read(uint8_t* buf, int len) int board_uart_write(void const * buf, int len) { - //UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING); - (void) buf; (void) len; - return 0; + uint8_t const* buf8 = (uint8_t const*) buf; + for(int i=0; i Date: Mon, 4 May 2020 00:29:52 +0700 Subject: [PATCH 03/52] clean up things, add makefile for host example --- examples/host/cdc_msc_hid/Makefile | 22 ++++++ .../cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject | 21 +++--- .../cdc_msc_hid/ses/lpc43xx/lpc43xx.emProject | 19 +++-- examples/host/cdc_msc_hid/src/main.c | 2 +- examples/host/cdc_msc_hid/src/tusb_config.h | 15 +--- hw/bsp/ea4357/ea4357.c | 15 ++-- hw/bsp/mcb1800/mcb1800.c | 71 +++++++++---------- src/class/cdc/cdc_host.c | 1 - src/host/ehci/ehci.c | 6 +- test/test/support/tusb_config.h | 10 +-- 10 files changed, 104 insertions(+), 78 deletions(-) create mode 100644 examples/host/cdc_msc_hid/Makefile diff --git a/examples/host/cdc_msc_hid/Makefile b/examples/host/cdc_msc_hid/Makefile new file mode 100644 index 000000000..20cd7ba69 --- /dev/null +++ b/examples/host/cdc_msc_hid/Makefile @@ -0,0 +1,22 @@ +include ../../../tools/top.mk +include ../../make.mk + +INC += \ + src \ + $(TOP)/hw \ + +# Example source +EXAMPLE_SOURCE += $(wildcard src/*.c) +SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) + + +# TinyUSB Host Stack source +SRC_C += \ + src/host/usbh.c \ + src/host/hub.c \ + src/host/ehci/ehci.c \ + src/class/cdc/cdc_host.c \ + src/host/ehci/ehci.c \ + src/portable/nxp/lpc18_43/hcd_lpc18_43.c + +include ../../rules.mk diff --git a/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject b/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject index 2dd7de552..2b68bfa23 100644 --- a/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject +++ b/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject @@ -17,8 +17,8 @@ arm_target_debug_interface_type="ADIv5" arm_target_device_name="LPC1857" arm_target_interface_type="SWD" - c_preprocessor_definitions="LPC18xx;__LPC1800_FAMILY;__LPC185x_SUBFAMILY;ARM_MATH_CM3;FLASH_PLACEMENT=1;CORE_M3;BOARD_MCB1800;CFG_TUSB_MCU=OPT_MCU_LPC18XX;CFG_TUSB_MEM_SECTION= __attribute__((section(".bss2")))" - c_user_include_directories="../../src;$(rootDir)/hw;$(rootDir)/src;$(lpcDir)//inc;$(lpcDir)//inc/config_18xx" + c_preprocessor_definitions="LPC18xx;__LPC1800_FAMILY;__LPC185x_SUBFAMILY;ARM_MATH_CM3;FLASH_PLACEMENT=1;CORE_M3;BOARD_MCB1800;CFG_TUSB_MCU=OPT_MCU_LPC18XX;CFG_TUSB_MEM_SECTION= __attribute__((section(".bss2")));CFG_TUSB_DEBUG=2" + c_user_include_directories="../../src;$(rootDir)/hw;$(rootDir)/src;$(lpcDir)//inc;$(lpcDir)//inc/config_18xx;$(rootDir)/lib/SEGGER_RTT/RTT" debug_register_definition_file="$(ProjectDir)/LPC18xx_Registers.xml" debug_target_connection="J-Link" gcc_entry_point="Reset_Handler" @@ -101,12 +101,6 @@ - + + + + + + + + + + + - + + + + + + + + + + + diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 0fbbd4534..6f5df1250 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -193,7 +193,7 @@ void print_greeting(void) printf("- issue at https://github.com/hathach/tinyusb\n"); printf("--------------------------------------------------------------------\n\n"); - printf("This Host demo is configured to support:"); + printf("This Host demo is configured to support:\r\n"); printf(" - RTOS = %s\n", rtos_name[CFG_TUSB_OS]); // if (CFG_TUH_CDC ) puts(" - Communication Device Class"); // if (CFG_TUH_MSC ) puts(" - Mass Storage"); diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index aa259dafd..235402180 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -75,23 +75,10 @@ #define CFG_TUH_HID_MOUSE 0 #define CFG_TUSB_HOST_HID_GENERIC 0 // (not yet supported) #define CFG_TUH_MSC 0 +#define CFG_TUH_VENDOR 0 #define CFG_TUSB_HOST_DEVICE_MAX (CFG_TUH_HUB ? 5 : 1) // normal hub has 4 ports -//------------- CLASS -------------// -#define CFG_TUD_CDC 0 -#define CFG_TUD_MSC 0 -#define CFG_TUD_HID 0 -#define CFG_TUD_VENDOR 0 - -// CDC FIFO size of TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE 64 -#define CFG_TUD_CDC_TX_BUFSIZE 64 - -// MSC Buffer size of Device Mass storage -#define CFG_TUD_MSC_BUFSIZE 512 - - #ifdef __cplusplus } #endif diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index 30d899f69..193e19831 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -63,7 +63,7 @@ const uint32_t ExtRateIn = 0; static const PINMUX_GRP_T pinmuxing[] = { - // Button + // Button ( Joystick down ) {0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP)}, // UART @@ -139,21 +139,26 @@ void board_init(void) USBMODE_VBUS_HIGH = 1 }; - /* USB0 - * For USB0 Device operation: - * - insert jumpers in position 1-2 in JP17/JP18/JP19. + /* From EA4357 user manual + * + * USB0 Device operation: + * - Insert jumpers in position 1-2 in JP17/JP18/JP19. * - GPIO28 controls USB connect functionality * - LED32 lights when the USB Device is connected. SJ4 has pads 1-2 shorted by default. * - LED33 is controlled by GPIO27 and signals USB-up state. GPIO54 is used for VBUS * sensing. * - * For USB0 Host operation: + * USB0 Host operation: * - insert jumpers in position 2-3 in JP17/JP18/JP19. * - USB Host power is controlled via distribution switch U20 (found in schematic page 11). * - Signal GPIO26 is active low and enables +5V on VBUS2. * - LED35 light whenever +5V is present on VBUS2. * - GPIO55 is connected to status feedback from the distribution switch. * - GPIO54 is used for VBUS sensing. 15Kohm pull-down resistors are always active + * + * Note: + * - Insert jumpers in position 2-3 in JP17/JP18/JP19 + * - Insert jumpers in JP31 (OTG) */ #if CFG_TUSB_RHPORT0_MODE Chip_USB0_Init(); diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index 075210c85..fdbe389a3 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -76,30 +76,27 @@ const uint32_t ExtRateIn = 0; static const PINMUX_GRP_T pinmuxing[] = { - // LEDs - {0xD, 10, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4)}, - {0xD, 11, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN)}, - {0xD, 12, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN)}, - {0xD, 13, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN)}, - {0xD, 14, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN)}, - {0x9, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN)}, - {0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN)}, - {0x9, 2, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN)}, + // LEDs + { 0xD, 10, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4) }, + { 0xD, 11, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, + { 0xD, 12, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, + { 0xD, 13, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, + { 0xD, 14, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, + { 0x9, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, + { 0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, + { 0x9, 2, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, - // Button - {0x4, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP)}, + // Button + { 0x4, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP) }, - // UART - {UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC2 }, - {UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, + // UART + { UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC2 }, + { UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, - /* I2S */ - {0x3, 0, (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, - {0x6, 0, (SCU_PINIO_FAST | SCU_MODE_FUNC4)}, - {0x7, 2, (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, - {0x6, 2, (SCU_PINIO_FAST | SCU_MODE_FUNC3)}, - {0x7, 1, (SCU_PINIO_FAST | SCU_MODE_FUNC2)}, - {0x6, 1, (SCU_PINIO_FAST | SCU_MODE_FUNC3)}, + // USB + { 0x9, 5, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC2 }, // P9_5 USB1_VBUS_EN, USB1 VBus function + { 0x2, 5, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, // P2_5 USB1_VBUS, MUST CONFIGURE THIS SIGNAL FOR USB1 NORMAL OPERATION */ + { 0x6, 3, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC1 } // P6_3 USB0_PWR_EN, USB0 VBus function }; /* Pin clock mux values, re-used structure, value in first index is meaningless */ @@ -147,10 +144,10 @@ void board_init(void) Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN); //------------- UART -------------// - Chip_UART_Init(UART_DEV); - Chip_UART_SetBaud(UART_DEV, CFG_BOARD_UART_BAUDRATE); - Chip_UART_ConfigData(UART_DEV, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS); - Chip_UART_TXEnable(UART_DEV); + Chip_UART_Init(UART_DEV); + Chip_UART_SetBaud(UART_DEV, CFG_BOARD_UART_BAUDRATE); + Chip_UART_ConfigData(UART_DEV, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS); + Chip_UART_TXEnable(UART_DEV); //------------- USB -------------// enum { @@ -167,17 +164,19 @@ void board_init(void) #if CFG_TUSB_RHPORT0_MODE Chip_USB0_Init(); -// // Reset controller -// LPC_USB0->USBCMD_D |= 0x02; -// while( LPC_USB0->USBCMD_D & 0x02 ) {} -// -// // Set mode -// #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST -// LPC_USB0->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); -// #else // TODO OTG -// LPC_USB0->USBMODE_D = USBMODE_DEVICE; -// LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; -// #endif + // Host/Device mode can only be set right after controller reset + LPC_USB0->USBCMD_D |= 0x02; + while( LPC_USB0->USBCMD_D & 0x02 ) {} + + // Set mode + #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST + LPC_USB0->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); + + LPC_USB0->PORTSC1_D |= (1<<24); // FIXME force full speed for debugging + #else // TODO OTG + LPC_USB0->USBMODE_D = USBMODE_DEVICE; + LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; + #endif #endif // USB1 diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index 5e45e8f6b..6b36ea94d 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -86,7 +86,6 @@ bool tuh_cdc_serial_is_mounted(uint8_t dev_addr) { // TODO consider all AT Command as serial candidate return tuh_cdc_mounted(dev_addr) && - (CDC_COMM_PROTOCOL_NONE <= cdch_data[dev_addr-1].itf_protocol) && (cdch_data[dev_addr-1].itf_protocol <= CDC_COMM_PROTOCOL_ATCOMMAND_CDMA); } diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index 9ddffaf2c..bbad072bf 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -358,7 +358,7 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const if ( dev_addr == 0 ) return true; // Insert to list - ehci_link_t * list_head; + ehci_link_t * list_head = NULL; switch (ep_desc->bmAttributes.xfer) { @@ -378,8 +378,10 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const default: break; } + TU_ASSERT(list_head); + // TODO might need to disable async/period list - list_insert( list_head, (ehci_link_t*) p_qhd, EHCI_QTYPE_QHD); + list_insert(list_head, (ehci_link_t*) p_qhd, EHCI_QTYPE_QHD); return true; } diff --git a/test/test/support/tusb_config.h b/test/test/support/tusb_config.h index 5258513ec..578c4126f 100644 --- a/test/test/support/tusb_config.h +++ b/test/test/support/tusb_config.h @@ -43,10 +43,12 @@ #define CFG_TUSB_MCU OPT_MCU_NRF5X #endif -#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) -#else -#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#ifndef CFG_TUSB_RHPORT0_MODE + #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX + #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) + #else + #define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE + #endif #endif #define CFG_TUSB_OS OPT_OS_NONE From 905a80d1b2de5e8ad67b3687ffd0543d90ac904e Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 4 May 2020 14:11:58 +0700 Subject: [PATCH 04/52] temporarily remove osal_task_delay() from osal - add hcd_uframe_number() API, update EHCI to return uframe number - get host running on ea4357 --- examples/host/cdc_msc_hid/src/tusb_config.h | 2 +- hw/bsp/ea4357/board.mk | 4 ++-- hw/bsp/ea4357/ea4357.c | 26 ++++++++++----------- hw/bsp/mcb1800/mcb1800.c | 25 ++++++++++---------- src/host/ehci/ehci.c | 13 ++++++++++- src/host/ehci/ehci.h | 6 +++-- src/host/hcd.h | 9 +++++++ src/host/usbh.c | 9 +++++++ src/osal/osal.h | 2 +- src/osal/osal_none.h | 16 ++++++------- 10 files changed, 72 insertions(+), 40 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 235402180..a929e36d8 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -69,7 +69,7 @@ // CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUH_HUB 1 +#define CFG_TUH_HUB 0 #define CFG_TUH_CDC 1 #define CFG_TUH_HID_KEYBOARD 0 #define CFG_TUH_HID_MOUSE 0 diff --git a/hw/bsp/ea4357/board.mk b/hw/bsp/ea4357/board.mk index dc002215b..dd987f476 100644 --- a/hw/bsp/ea4357/board.mk +++ b/hw/bsp/ea4357/board.mk @@ -6,9 +6,9 @@ CFLAGS += \ -mfloat-abi=hard \ -mfpu=fpv4-sp-d16 \ -nostdlib \ + -D__USE_LPCOPEN \ -DCORE_M4 \ - -DCFG_TUSB_MCU=OPT_MCU_LPC43XX \ - -D__USE_LPCOPEN + -DCFG_TUSB_MCU=OPT_MCU_LPC43XX # mcu driver cause following warnings CFLAGS += -Wno-error=unused-parameter -Wno-error=strict-prototypes diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index 193e19831..daa3a6bd0 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -163,19 +163,19 @@ void board_init(void) #if CFG_TUSB_RHPORT0_MODE Chip_USB0_Init(); -// // Reset controller -// LPC_USB0->USBCMD_D |= 0x02; -// while( LPC_USB0->USBCMD_D & 0x02 ) {} -// -// // Set mode -// #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST -// LPC_USB0->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); -// -// LPC_USB0->PORTSC1_D |= (1<<24); // FIXME force full speed for debugging -// #else // TODO OTG -// LPC_USB0->USBMODE_D = USBMODE_DEVICE; -// LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; -// #endif + // Reset controller + LPC_USB0->USBCMD_D |= 0x02; + while( LPC_USB0->USBCMD_D & 0x02 ) {} + + // Set mode + #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST + LPC_USB0->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); + + LPC_USB0->PORTSC1_D |= (1<<24); // FIXME force full speed for debugging + #else // TODO OTG + LPC_USB0->USBMODE_D = USBMODE_DEVICE; + LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; + #endif #endif /* USB1 diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index fdbe389a3..bfeb126fc 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -82,9 +82,9 @@ static const PINMUX_GRP_T pinmuxing[] = { 0xD, 12, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, { 0xD, 13, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, { 0xD, 14, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC4 | SCU_MODE_PULLDOWN) }, - { 0x9, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, - { 0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, - { 0x9, 2, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, + { 0x9, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, + { 0x9, 1, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, + { 0x9, 2, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLDOWN) }, // Button { 0x4, 0, (SCU_MODE_INBUFF_EN | SCU_MODE_INACT | SCU_MODE_FUNC0 | SCU_MODE_PULLUP) }, @@ -93,19 +93,20 @@ static const PINMUX_GRP_T pinmuxing[] = { UART_PORT, UART_PIN_TX, SCU_MODE_PULLDOWN | SCU_MODE_FUNC2 }, { UART_PORT, UART_PIN_RX, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, - // USB - { 0x9, 5, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC2 }, // P9_5 USB1_VBUS_EN, USB1 VBus function - { 0x2, 5, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, // P2_5 USB1_VBUS, MUST CONFIGURE THIS SIGNAL FOR USB1 NORMAL OPERATION */ - { 0x6, 3, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC1 } // P6_3 USB0_PWR_EN, USB0 VBus function + // USB0 + { 0x6, 3, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC1 }, // P6_3 USB0_PWR_EN, USB0 VBus function + + { 0x9, 5, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC2 }, // P9_5 USB1_VBUS_EN, USB1 VBus function + { 0x2, 5, SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_FUNC2 }, // P2_5 USB1_VBUS, MUST CONFIGURE THIS SIGNAL FOR USB1 NORMAL OPERATION }; /* Pin clock mux values, re-used structure, value in first index is meaningless */ static const PINMUX_GRP_T pinclockmuxing[] = { - {0, 0, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, - {0, 1, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, - {0, 2, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, - {0, 3, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, + { 0, 0, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, + { 0, 1, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, + { 0, 2, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, + { 0, 3, (SCU_MODE_INACT | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS | SCU_MODE_HIGHSPEEDSLEW_EN | SCU_MODE_FUNC0)}, }; // Invoked by startup code @@ -172,7 +173,7 @@ void board_init(void) #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST LPC_USB0->USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); - LPC_USB0->PORTSC1_D |= (1<<24); // FIXME force full speed for debugging +// LPC_USB0->PORTSC1_D |= (1<<24); // FIXME force full speed for debugging #else // TODO OTG LPC_USB0->USBMODE_D = USBMODE_DEVICE; LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index bbad072bf..048affb1e 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -108,6 +108,12 @@ bool hcd_init(void) return ehci_init(TUH_OPT_RHPORT); } +uint32_t hcd_uframe_number(uint8_t rhport) +{ + (void) rhport; + return ehci_data.uframe_number + ehci_data.regs->frame_index; +} + void hcd_port_reset(uint8_t rhport) { (void) rhport; @@ -192,7 +198,7 @@ static bool ehci_init(uint8_t rhport) regs->status = EHCI_INT_MASK_ALL; // 2. clear all status regs->inten = EHCI_INT_MASK_ERROR | EHCI_INT_MASK_PORT_CHANGE | EHCI_INT_MASK_ASYNC_ADVANCE | - EHCI_INT_MASK_NXP_PERIODIC | EHCI_INT_MASK_NXP_ASYNC ; + EHCI_INT_MASK_NXP_PERIODIC | EHCI_INT_MASK_NXP_ASYNC | EHCI_INT_MASK_FRAMELIST_ROLLOVER; //------------- Asynchronous List -------------// ehci_qhd_t * const async_head = qhd_async_head(rhport); @@ -636,6 +642,11 @@ void hcd_isr(uint8_t rhport) if (int_status == 0) return; + if (int_status & EHCI_INT_MASK_FRAMELIST_ROLLOVER) + { + ehci_data.uframe_number += (EHCI_FRAMELIST_SIZE << 3); + } + if (int_status & EHCI_INT_MASK_PORT_CHANGE) { uint32_t port_status = regs->portsc & EHCI_PORTSC_MASK_ALL; diff --git a/src/host/ehci/ehci.h b/src/host/ehci/ehci.h index ce2f56771..f11c4536a 100644 --- a/src/host/ehci/ehci.h +++ b/src/host/ehci/ehci.h @@ -54,8 +54,8 @@ //--------------------------------------------------------------------+ // EHCI CONFIGURATION & CONSTANTS //--------------------------------------------------------------------+ -#define EHCI_CFG_FRAMELIST_SIZE_BITS 7 /// Framelist Size (NXP specific) (0:1024) - (1:512) - (2:256) - (3:128) - (4:64) - (5:32) - (6:16) - (7:8) -#define EHCI_FRAMELIST_SIZE (1024 >> EHCI_CFG_FRAMELIST_SIZE_BITS) +#define EHCI_CFG_FRAMELIST_SIZE_BITS 7 /// Framelist Size (NXP specific) (0:1024) - (1:512) - (2:256) - (3:128) - (4:64) - (5:32) - (6:16) - (7:8) +#define EHCI_FRAMELIST_SIZE (1024 >> EHCI_CFG_FRAMELIST_SIZE_BITS) // TODO merge OHCI with EHCI enum { @@ -445,6 +445,8 @@ typedef struct ehci_qtd_t qtd_pool[HCD_MAX_XFER] TU_ATTR_ALIGNED(32); ehci_registers_t* regs; + + volatile uint32_t uframe_number; }ehci_data_t; #ifdef __cplusplus diff --git a/src/host/hcd.h b/src/host/hcd.h index a39e7fe16..d9307ca09 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -91,6 +91,15 @@ void hcd_isr(uint8_t hostid); void hcd_int_enable (uint8_t rhport); void hcd_int_disable(uint8_t rhport); +// Get micro frame number (125 us) +uint32_t hcd_uframe_number(uint8_t rhport); + +// Get frame number (1ms) +static inline uint32_t hcd_frame_number(uint8_t rhport) +{ + return hcd_uframe_number(rhport) >> 3; +} + // PORT API /// return the current connect status of roothub port bool hcd_port_connect_status(uint8_t hostid); diff --git a/src/host/usbh.c b/src/host/usbh.c index f1b88259c..7171f6bab 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -127,6 +127,15 @@ tusb_device_state_t tuh_device_get_state (uint8_t const dev_addr) return (tusb_device_state_t) _usbh_devices[dev_addr].state; } + +static inline void osal_task_delay(uint32_t msec) +{ + (void) msec; + + uint32_t start = hcd_frame_number(TUH_OPT_RHPORT); + while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {} +} + //--------------------------------------------------------------------+ // CLASS-USBD API (don't require to verify parameters) //--------------------------------------------------------------------+ diff --git a/src/osal/osal.h b/src/osal/osal.h index 0421b3294..02dcf5367 100644 --- a/src/osal/osal.h +++ b/src/osal/osal.h @@ -62,7 +62,7 @@ typedef void (*osal_task_func_t)( void * ); //--------------------------------------------------------------------+ // OSAL Porting API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec); +//static inline void osal_task_delay(uint32_t msec); //------------- Semaphore -------------// static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef); diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index b27e628a9..e7ce18079 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -34,14 +34,14 @@ //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec) -{ - (void) msec; - // TODO only used by Host stack, will implement using SOF - -// uint32_t start = tusb_hal_millis(); -// while ( ( tusb_hal_millis() - start ) < msec ) {} -} +//static inline void osal_task_delay(uint32_t msec) +//{ +// (void) msec; +// // TODO only used by Host stack, will implement using SOF +// +//// uint32_t start = tusb_hal_millis(); +//// while ( ( tusb_hal_millis() - start ) < msec ) {} +//} //--------------------------------------------------------------------+ // Binary Semaphore API From 81b1f97ef7510a12a2d2ee3e8c806d4824e8a720 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 May 2020 13:23:40 +0700 Subject: [PATCH 05/52] suppress cast-align warnings for net device driver --- examples/device/net_lwip_webserver/Makefile | 3 --- lib/networking/rndis_reports.c | 10 +++++----- src/class/net/net_device.c | 6 +++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/examples/device/net_lwip_webserver/Makefile b/examples/device/net_lwip_webserver/Makefile index 5279c49e2..fa93cf87f 100644 --- a/examples/device/net_lwip_webserver/Makefile +++ b/examples/device/net_lwip_webserver/Makefile @@ -6,9 +6,6 @@ CFLAGS += \ -DTCP_WND=2*TCP_MSS \ -DHTTPD_USE_CUSTOM_FSDATA=0 -# TODO rndis_reports.c and net_device cause cast algin warnings -CFLAGS += -Wno-error=cast-align - INC += \ src \ $(TOP)/hw \ diff --git a/lib/networking/rndis_reports.c b/lib/networking/rndis_reports.c index 6e24c57e9..ee611c883 100644 --- a/lib/networking/rndis_reports.c +++ b/lib/networking/rndis_reports.c @@ -74,7 +74,7 @@ static const uint32_t OIDSupportedList[] = #define OID_LIST_LENGTH TU_ARRAY_SIZE(OIDSupportedList) #define ENC_BUF_SIZE (OID_LIST_LENGTH * 4 + 32) -static uint8_t *encapsulated_buffer; +static void *encapsulated_buffer; static void rndis_report(void) { @@ -152,7 +152,7 @@ static void rndis_query(void) } } -#define INFBUF ((uint32_t *)((uint8_t *)&(m->RequestId) + m->InformationBufferOffset)) +#define INFBUF ((uint8_t *)&(m->RequestId) + m->InformationBufferOffset) static void rndis_handle_config_parm(const char *data, int keyoffset, int valoffset, int keylen, int vallen) { @@ -191,14 +191,14 @@ static void rndis_handle_set_msg(void) char *ptr = (char *)m; ptr += sizeof(rndis_generic_msg_t); ptr += m->InformationBufferOffset; - p = (rndis_config_parameter_t *)ptr; + p = (rndis_config_parameter_t *) ((void*) ptr); rndis_handle_config_parm(ptr, p->ParameterNameOffset, p->ParameterValueOffset, p->ParameterNameLength, p->ParameterValueLength); } break; /* Mandatory general OIDs */ case OID_GEN_CURRENT_PACKET_FILTER: - oid_packet_filter = *INFBUF; + memcpy(&oid_packet_filter, INFBUF, 4); if (oid_packet_filter) { rndis_packetFilter(oid_packet_filter); @@ -239,7 +239,7 @@ void rndis_class_set_handler(uint8_t *data, int size) encapsulated_buffer = data; (void)size; - switch (((rndis_generic_msg_t *)data)->MessageType) + switch (((rndis_generic_msg_t *)encapsulated_buffer)->MessageType) { case REMOTE_NDIS_INITIALIZE_MSG: { diff --git a/src/class/net/net_device.c b/src/class/net/net_device.c index fedd4db99..cbd662b10 100644 --- a/src/class/net/net_device.c +++ b/src/class/net/net_device.c @@ -326,7 +326,7 @@ bool netd_control_request(uint8_t rhport, tusb_control_request_t const * request { if (request->bmRequestType_bit.direction == TUSB_DIR_IN) { - rndis_generic_msg_t *rndis_msg = (rndis_generic_msg_t *)notify.rndis_buf; + rndis_generic_msg_t *rndis_msg = (rndis_generic_msg_t *) ((void*) notify.rndis_buf); uint32_t msglen = tu_le32toh(rndis_msg->MessageLength); TU_ASSERT(msglen <= sizeof(notify.rndis_buf)); tud_control_xfer(rhport, request, notify.rndis_buf, msglen); @@ -356,7 +356,7 @@ static void handle_incoming_packet(uint32_t len) } else { - rndis_data_packet_t *r = (rndis_data_packet_t *)pnt; + rndis_data_packet_t *r = (rndis_data_packet_t *) ((void*) pnt); if (len >= sizeof(rndis_data_packet_t)) if ( (r->MessageType == REMOTE_NDIS_PACKET_MSG) && (r->MessageLength <= len)) if ( (r->DataOffset + offsetof(rndis_data_packet_t, DataOffset) + r->DataLength) <= len) @@ -450,7 +450,7 @@ void tud_network_xmit(struct pbuf *p) if (!_netd_itf.ecm_mode) { - rndis_data_packet_t *hdr = (rndis_data_packet_t *)transmitted; + rndis_data_packet_t *hdr = (rndis_data_packet_t *) ((void*) transmitted); memset(hdr, 0, sizeof(rndis_data_packet_t)); hdr->MessageType = REMOTE_NDIS_PACKET_MSG; hdr->MessageLength = len; From 58cedf4c061fbf9f95cd88cafb6e581fc04549ff Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 19 May 2020 00:55:43 +0700 Subject: [PATCH 06/52] usb0 host on mcb1800 work with fullspeed mode. use usbh_edpt_open() to correctly map ep2drv[] --- examples/host/cdc_msc_hid/Makefile | 3 +- .../cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject | 4 ++ examples/host/cdc_msc_hid/src/main.c | 15 ++-- examples/host/cdc_msc_hid/src/msc_app.c | 36 +++++----- examples/host/cdc_msc_hid/src/tusb_config.h | 6 +- hw/bsp/ea4357/board.mk | 2 +- hw/bsp/mcb1800/board.mk | 4 +- hw/bsp/mcb1800/mcb1800.c | 3 +- src/class/cdc/cdc_host.c | 4 +- src/class/hid/hid_host.c | 2 +- src/class/msc/msc_host.c | 2 +- src/class/vendor/vendor_host.c | 2 +- src/host/ehci/ehci.c | 21 +++++- src/host/ehci/ehci.h | 4 ++ src/host/hcd.h | 1 + src/host/hub.c | 2 +- src/host/usbh.c | 68 +++++++++++-------- src/host/usbh.h | 2 + 18 files changed, 115 insertions(+), 66 deletions(-) diff --git a/examples/host/cdc_msc_hid/Makefile b/examples/host/cdc_msc_hid/Makefile index 20cd7ba69..373dbbf9e 100644 --- a/examples/host/cdc_msc_hid/Makefile +++ b/examples/host/cdc_msc_hid/Makefile @@ -15,8 +15,9 @@ SRC_C += \ src/host/usbh.c \ src/host/hub.c \ src/host/ehci/ehci.c \ - src/class/cdc/cdc_host.c \ src/host/ehci/ehci.c \ + src/class/cdc/cdc_host.c \ + src/class/msc/msc_host.c \ src/portable/nxp/lpc18_43/hcd_lpc18_43.c include ../../rules.mk diff --git a/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject b/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject index 2b68bfa23..e0a2f6f97 100644 --- a/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject +++ b/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject @@ -124,6 +124,10 @@ + USBMODE_H = USBMODE_HOST | (USBMODE_VBUS_HIGH << 5); - -// LPC_USB0->PORTSC1_D |= (1<<24); // FIXME force full speed for debugging + LPC_USB0->PORTSC1_H |= (1<<24); // FIXME force full speed for debugging #else // TODO OTG LPC_USB0->USBMODE_D = USBMODE_DEVICE; LPC_USB0->OTGSC = (1<<3) | (1<<0) /*| (1<<16)| (1<<24)| (1<<25)| (1<<26)| (1<<27)| (1<<28)| (1<<29)| (1<<30)*/; diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index 6b36ea94d..e62f47b72 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -158,7 +158,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it // notification endpoint tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT( hcd_edpt_open(rhport, dev_addr, ep_desc) ); + TU_ASSERT( usbh_edpt_open(rhport, dev_addr, ep_desc) ); p_cdc->ep_notif = ep_desc->bEndpointAddress; (*p_length) += p_desc[DESC_OFFSET_LEN]; @@ -179,7 +179,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType); TU_ASSERT(TUSB_XFER_BULK == ep_desc->bmAttributes.xfer); - TU_ASSERT(hcd_edpt_open(rhport, dev_addr, ep_desc)); + TU_ASSERT(usbh_edpt_open(rhport, dev_addr, ep_desc)); if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index 69c49b012..8abb3cd22 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -40,7 +40,7 @@ //--------------------------------------------------------------------+ static inline bool hidh_interface_open(uint8_t dev_addr, uint8_t interface_number, tusb_desc_endpoint_t const *p_endpoint_desc, hidh_interface_info_t *p_hid) { - p_hid->pipe_hdl = hcd_edpt_open(dev_addr, p_endpoint_desc, TUSB_CLASS_HID); + p_hid->pipe_hdl = usbh_edpt_open(dev_addr, p_endpoint_desc, TUSB_CLASS_HID); p_hid->report_size = p_endpoint_desc->wMaxPacketSize.size; // TODO get size from report descriptor p_hid->interface_number = interface_number; diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index e20969f57..98c71eb70 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -293,7 +293,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType); TU_ASSERT(TUSB_XFER_BULK == ep_desc->bmAttributes.xfer); - TU_ASSERT(hcd_edpt_open(rhport, dev_addr, ep_desc)); + TU_ASSERT(usbh_edpt_open(rhport, dev_addr, ep_desc)); if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { diff --git a/src/class/vendor/vendor_host.c b/src/class/vendor/vendor_host.c index 3e8dac0f6..0524cb981 100644 --- a/src/class/vendor/vendor_host.c +++ b/src/class/vendor/vendor_host.c @@ -107,7 +107,7 @@ tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_ pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? &custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out; - *p_pipe_hdl = hcd_edpt_open(dev_addr, p_endpoint, TUSB_CLASS_VENDOR_SPECIFIC); + *p_pipe_hdl = usbh_edpt_open(dev_addr, p_endpoint, TUSB_CLASS_VENDOR_SPECIFIC); TU_ASSERT ( pipehandle_is_valid(*p_pipe_hdl), TUSB_ERROR_HCD_OPEN_PIPE_FAILED ); p_desc = tu_desc_next(p_desc); diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index 048affb1e..e2ead7b08 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -120,10 +120,27 @@ void hcd_port_reset(uint8_t rhport) ehci_registers_t* regs = ehci_data.regs; - regs->portsc_bm.port_enabled = 0; // disable port before reset - regs->portsc_bm.port_reset = 1; +// regs->portsc_bm.port_enabled = 0; // disable port before reset +// regs->portsc_bm.port_reset = 1; + + uint32_t portsc = regs->portsc; + + portsc &= ~(EHCI_PORTSC_MASK_PORT_EANBLED); + portsc |= EHCI_PORTSC_MASK_PORT_RESET; + + regs->portsc = portsc; } +#if 0 +void hcd_port_reset_end(uint8_t rhport) +{ + (void) rhport; + + ehci_registers_t* regs = ehci_data.regs; + regs->portsc_bm.port_reset = 0; +} +#endif + bool hcd_port_connect_status(uint8_t rhport) { (void) rhport; diff --git a/src/host/ehci/ehci.h b/src/host/ehci/ehci.h index f11c4536a..a6342b2d2 100644 --- a/src/host/ehci/ehci.h +++ b/src/host/ehci/ehci.h @@ -311,10 +311,14 @@ enum ehci_usbcmd_pos_ { }; enum ehci_portsc_change_mask_{ + EHCI_PORTSC_MASK_CURRENT_CONNECT_STATUS = TU_BIT(0), EHCI_PORTSC_MASK_CONNECT_STATUS_CHANGE = TU_BIT(1), + EHCI_PORTSC_MASK_PORT_EANBLED = TU_BIT(2), EHCI_PORTSC_MASK_PORT_ENABLE_CHAGNE = TU_BIT(3), EHCI_PORTSC_MASK_OVER_CURRENT_CHANGE = TU_BIT(5), + EHCI_PORTSC_MASK_PORT_RESET = TU_BIT(8), + EHCI_PORTSC_MASK_ALL = EHCI_PORTSC_MASK_CONNECT_STATUS_CHANGE | EHCI_PORTSC_MASK_PORT_ENABLE_CHAGNE | diff --git a/src/host/hcd.h b/src/host/hcd.h index d9307ca09..f2ca0a1d6 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -104,6 +104,7 @@ static inline uint32_t hcd_frame_number(uint8_t rhport) /// return the current connect status of roothub port bool hcd_port_connect_status(uint8_t hostid); void hcd_port_reset(uint8_t hostid); +void hcd_port_reset_end(uint8_t rhport); tusb_speed_t hcd_port_speed_get(uint8_t hostid); // HCD closes all opened endpoints belong to this device diff --git a/src/host/hub.c b/src/host/hub.c index 00450b704..eb85dfa42 100644 --- a/src/host/hub.c +++ b/src/host/hub.c @@ -154,7 +154,7 @@ bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType); TU_ASSERT(TUSB_XFER_INTERRUPT == ep_desc->bmAttributes.xfer); - TU_ASSERT(hcd_edpt_open(rhport, dev_addr, ep_desc)); + TU_ASSERT(usbh_edpt_open(rhport, dev_addr, ep_desc)); hub_data[dev_addr-1].itf_num = itf_desc->bInterfaceNumber; hub_data[dev_addr-1].ep_status = ep_desc->bEndpointAddress; diff --git a/src/host/usbh.c b/src/host/usbh.c index 7171f6bab..8536cec0a 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -116,7 +116,6 @@ CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(4) static uint8_t _usbh_ctrl_buf[CFG_TUSB_H //------------- Helper Function Prototypes -------------// static inline uint8_t get_new_address(void); static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_desc); -static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); //--------------------------------------------------------------------+ // PUBLIC API (Parameter Verification is required) @@ -225,6 +224,30 @@ tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) return TUSB_ERROR_NONE; } +bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc) +{ + bool ret = hcd_edpt_open(rhport, dev_addr, ep_desc); + + if (ret) + { + usbh_device_t* dev = &_usbh_devices[dev_addr]; + + // new endpoints belongs to latest interface (last valid value) + uint8_t drvid = 0xff; + for(uint8_t i=0; i < sizeof(dev->itf2drv); i++) + { + if ( dev->itf2drv[i] == 0xff ) break; + drvid = dev->itf2drv[i]; + } + TU_ASSERT(drvid < USBH_CLASS_DRIVER_COUNT); + + uint8_t const ep_addr = ep_desc->bEndpointAddress; + dev->ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = drvid; + } + + return ret; +} + //--------------------------------------------------------------------+ // USBH-HCD ISR/Callback API //--------------------------------------------------------------------+ @@ -358,6 +381,8 @@ bool enum_task(hcd_event_t* event) { if( hcd_port_connect_status(dev0->rhport) ) { + TU_LOG2("Connect \r\n"); + // connection event osal_task_delay(POWER_STABLE_DELAY); // wait until device is stable. Increase this if the first 8 bytes is failed to get @@ -371,6 +396,8 @@ bool enum_task(hcd_event_t* event) } else { + TU_LOG2("Disconnect \r\n"); + // disconnection event usbh_device_unplugged(dev0->rhport, 0, 0); return true; // restart task @@ -424,6 +451,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT_ERR( usbh_pipe_control_open(0, 8) ); //------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------// + TU_LOG2("Get 8 byte of Device Descriptor \r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN }, .bRequest = TUSB_REQ_GET_DESCRIPTOR, @@ -434,12 +462,16 @@ bool enum_task(hcd_event_t* event) bool is_ok = usbh_control_xfer(0, &request, _usbh_ctrl_buf); //------------- Reset device again before Set Address -------------// + TU_LOG2("Port reset \r\n"); + if (dev0->hub_addr == 0) { // connected directly to roothub TU_ASSERT(is_ok); // TODO some slow device is observed to fail the very fist controller xfer, can try more times hcd_port_reset( dev0->rhport ); // reset port after 8 byte descriptor osal_task_delay(RESET_DELAY); +// hcd_port_reset_end(dev0->rhport); +// osal_task_delay(RESET_DELAY); } #if CFG_TUH_HUB else @@ -458,6 +490,7 @@ bool enum_task(hcd_event_t* event) #endif //------------- Set new address -------------// + TU_LOG2("Set Address \r\n"); uint8_t const new_addr = get_new_address(); TU_ASSERT(new_addr <= CFG_TUSB_HOST_DEVICE_MAX); // TODO notify application we reach max devices @@ -484,6 +517,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT_ERR ( usbh_pipe_control_open(new_addr, ((tusb_desc_device_t*) _usbh_ctrl_buf)->bMaxPacketSize0 ) ); //------------- Get full device descriptor -------------// + TU_LOG2("Get Device Descriptor \r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN }, .bRequest = TUSB_REQ_GET_DESCRIPTOR, @@ -502,6 +536,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT(configure_selected <= new_dev->configure_count); // TODO notify application when invalid configuration //------------- Get 9 bytes of configuration descriptor -------------// + TU_LOG2("Get 9 bytes of Configuration Descriptor \r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN }, .bRequest = TUSB_REQ_GET_DESCRIPTOR, @@ -515,6 +550,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT( CFG_TUSB_HOST_ENUM_BUFFER_SIZE >= ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength ); //------------- Get full configuration descriptor -------------// + TU_LOG2("Get full Configuration Descriptor \r\n"); request.wLength = ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength; // full length TU_ASSERT( usbh_control_xfer( new_addr, &request, _usbh_ctrl_buf ) ); @@ -522,6 +558,7 @@ bool enum_task(hcd_event_t* event) new_dev->interface_count = ((tusb_desc_configuration_t*) _usbh_ctrl_buf)->bNumInterfaces; //------------- Set Configure -------------// + TU_LOG2("Set Configuration Descriptor \r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_OUT }, .bRequest = TUSB_REQ_SET_CONFIGURATION, @@ -577,11 +614,7 @@ bool enum_task(hcd_event_t* event) { uint16_t itf_len = 0; - if ( usbh_class_drivers[drv_id].open(new_dev->rhport, new_addr, desc_itf, &itf_len) ) - { - mark_interface_endpoint(new_dev->ep2drv, p_desc, itf_len, drv_id); - } - + TU_ASSERT( usbh_class_drivers[drv_id].open(new_dev->rhport, new_addr, desc_itf, &itf_len) ); TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); p_desc += itf_len; } @@ -597,7 +630,7 @@ bool enum_task(hcd_event_t* event) /* USB Host Driver task * This top level thread manages all host controller event and delegates events to class-specific drivers. * This should be called periodically within the mainloop or rtos thread. - * + *_usbh_devices[dev_addr]. @code int main(void) { @@ -661,25 +694,4 @@ static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_de return config_num; } -// Helper marking endpoint of interface belongs to class driver -// TODO merge with usbd -static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id) -{ - uint16_t len = 0; - - while( len < desc_len ) - { - if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) ) - { - uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; - - ep2drv[ tu_edpt_number(ep_addr) ][ tu_edpt_dir(ep_addr) ] = driver_id; - } - - len += tu_desc_len(p_desc); - p_desc = tu_desc_next(p_desc); - } -} - - #endif diff --git a/src/host/usbh.h b/src/host/usbh.h index 42a2bd095..bc0abcccc 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -94,6 +94,8 @@ TU_ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr); bool usbh_init(void); bool usbh_control_xfer (uint8_t dev_addr, tusb_control_request_t* request, uint8_t* data); +bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); + #ifdef __cplusplus } #endif From 4c01099a3d6e92404c645e5c906968955a6c9b1a Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 22 May 2020 20:57:52 +0700 Subject: [PATCH 07/52] update makefile to build with ohci host update ses project for lpc1769 with rtt --- examples/host/cdc_msc_hid/Makefile | 6 +++-- .../ses/lpc175x_6x/lpc175x_6x.emProject | 23 ++++++++++++------- hw/bsp/board.c | 3 +++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/examples/host/cdc_msc_hid/Makefile b/examples/host/cdc_msc_hid/Makefile index 373dbbf9e..29a323957 100644 --- a/examples/host/cdc_msc_hid/Makefile +++ b/examples/host/cdc_msc_hid/Makefile @@ -9,15 +9,17 @@ INC += \ EXAMPLE_SOURCE += $(wildcard src/*.c) SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) +CFLAGS += -Wno-error=cast-align # TinyUSB Host Stack source SRC_C += \ src/host/usbh.c \ src/host/hub.c \ src/host/ehci/ehci.c \ - src/host/ehci/ehci.c \ + src/host/ohci/ohci.c \ src/class/cdc/cdc_host.c \ src/class/msc/msc_host.c \ - src/portable/nxp/lpc18_43/hcd_lpc18_43.c + src/portable/nxp/lpc18_43/hcd_lpc18_43.c \ + src/portable/nxp/lpc17_40/hcd_lpc17_40.c include ../../rules.mk diff --git a/examples/host/cdc_msc_hid/ses/lpc175x_6x/lpc175x_6x.emProject b/examples/host/cdc_msc_hid/ses/lpc175x_6x/lpc175x_6x.emProject index ba5a9cea7..8a2c36390 100644 --- a/examples/host/cdc_msc_hid/ses/lpc175x_6x/lpc175x_6x.emProject +++ b/examples/host/cdc_msc_hid/ses/lpc175x_6x/lpc175x_6x.emProject @@ -19,8 +19,8 @@ arm_target_device_name="LPC1769" arm_target_interface_type="SWD" build_treat_warnings_as_errors="Yes" - c_preprocessor_definitions="LPC175x_6x;__LPC1700_FAMILY;__LPC176x_SUBFAMILY;ARM_MATH_CM3;FLASH_PLACEMENT=1;CORE_M3;BOARD_LPCXPRESSO1769;CFG_TUSB_MCU=OPT_MCU_LPC175X_6X" - c_user_include_directories="../../src;$(rootDir)/hw;$(rootDir)/src;$(lpcDir)/inc" + c_preprocessor_definitions="LPC175x_6x;__LPC1700_FAMILY;__LPC176x_SUBFAMILY;ARM_MATH_CM3;FLASH_PLACEMENT=1;CORE_M3;BOARD_LPCXPRESSO1769;CFG_TUSB_MCU=OPT_MCU_LPC175X_6X;CFG_TUSB_DEBUG=2;LOGGER_RTT" + c_user_include_directories="../../src;$(rootDir)/hw;$(rootDir)/src;$(lpcDir)/inc;$(rootDir)/lib/SEGGER_RTT/RTT" debug_register_definition_file="LPC176x5x_Registers.xml" debug_target_connection="J-Link" gcc_enable_all_warnings="Yes" @@ -101,12 +101,19 @@ - + + + + + + + + + + + + + diff --git a/hw/bsp/board.c b/hw/bsp/board.c index 56f407326..383a02ef4 100644 --- a/hw/bsp/board.c +++ b/hw/bsp/board.c @@ -40,6 +40,8 @@ #if defined(LOGGER_RTT) // Logging with RTT +// If using SES IDE, use the Syscalls/SEGGER_RTT_Syscalls_SES.c instead +#if !(defined __SES_ARM) && !(defined __SES_RISCV) && !(defined __CROSSWORKS_ARM) #include "SEGGER_RTT.h" TU_ATTR_USED int sys_write (int fhdl, const void *buf, size_t count) @@ -54,6 +56,7 @@ TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count) (void) fhdl; return SEGGER_RTT_Read(0, buf, count); } +#endif #elif defined(LOGGER_SWO) // Logging with SWO for ARM Cortex From d108ea4326d824da73da0a4f9632c5da6aa2e3ec Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 22 May 2020 21:45:34 +0700 Subject: [PATCH 08/52] implement hcd_uframe_number for ohci able to get 8 byte descriptors using LPC1769 + base, but failed to reset and set address. --- .../cdc_msc_hid/ses/lpc40xx/lpc40xx.emProject | 24 ++++++++++++------- src/host/ohci/ohci.c | 15 +++++++++++- src/host/ohci/ohci.h | 2 ++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/examples/host/cdc_msc_hid/ses/lpc40xx/lpc40xx.emProject b/examples/host/cdc_msc_hid/ses/lpc40xx/lpc40xx.emProject index bfb7922a3..68995f1a5 100644 --- a/examples/host/cdc_msc_hid/ses/lpc40xx/lpc40xx.emProject +++ b/examples/host/cdc_msc_hid/ses/lpc40xx/lpc40xx.emProject @@ -18,8 +18,8 @@ arm_target_debug_interface_type="ADIv5" arm_target_device_name="LPC4088" arm_target_interface_type="SWD" - c_preprocessor_definitions="CORE_M4;__LPC4000_FAMILY;__LPC408x_SUBFAMILY;ARM_MATH_CM4;FLASH_PLACEMENT=1;BOARD_EA4088QS;CFG_TUSB_MCU=OPT_MCU_LPC40XX;CFG_TUSB_MEM_SECTION= __attribute__((section(".bss2")))" - c_user_include_directories="../../src;$(rootDir)/hw;$(rootDir)/src;$(lpcDir)/inc" + c_preprocessor_definitions="CORE_M4;__LPC4000_FAMILY;__LPC408x_SUBFAMILY;ARM_MATH_CM4;FLASH_PLACEMENT=1;BOARD_EA4088QS;CFG_TUSB_MCU=OPT_MCU_LPC40XX;CFG_TUSB_MEM_SECTION= __attribute__((section(".bss2")));CFG_TUSB_DEBUG=2;LOGGER_RTT" + c_user_include_directories="../../src;$(rootDir)/hw;$(rootDir)/src;$(lpcDir)/inc;$(rootDir)/lib/SEGGER_RTT/RTT" debug_register_definition_file="$(ProjectDir)/LPC408x_7x_Registers.xml" debug_target_connection="J-Link" gcc_enable_all_warnings="Yes" @@ -55,6 +55,7 @@ + @@ -91,12 +92,6 @@ - + + + + + + + + + + + + + interrupt_disable = OHCI_REG->interrupt_enable; // disable all interrupts OHCI_REG->interrupt_status = OHCI_REG->interrupt_status; // clear current set bits OHCI_REG->interrupt_enable = OHCI_INT_WRITEBACK_DONEHEAD_MASK | OHCI_INT_RESUME_DETECTED_MASK | - OHCI_INT_UNRECOVERABLE_ERROR_MASK | /*OHCI_INT_FRAME_OVERFLOW_MASK |*/ OHCI_INT_RHPORT_STATUS_CHANGE_MASK | + OHCI_INT_UNRECOVERABLE_ERROR_MASK | OHCI_INT_FRAME_OVERFLOW_MASK | OHCI_INT_RHPORT_STATUS_CHANGE_MASK | OHCI_INT_MASTER_ENABLE_MASK; OHCI_REG->control |= OHCI_CONTROL_CONTROL_BULK_RATIO | OHCI_CONTROL_LIST_CONTROL_ENABLE_MASK | @@ -181,6 +181,13 @@ bool hcd_init(void) return true; } +uint32_t hcd_uframe_number(uint8_t rhport) +{ + (void) rhport; + return (ohci_data.frame_number_hi << 16 | OHCI_REG->frame_number) << 3; +} + + //--------------------------------------------------------------------+ // PORT API //--------------------------------------------------------------------+ @@ -606,6 +613,12 @@ void hcd_isr(uint8_t hostid) if (int_status == 0) return; + // Frame number overflow + if ( int_status & OHCI_INT_FRAME_OVERFLOW_MASK ) + { + ohci_data.frame_number_hi++; + } + //------------- RootHub status -------------// if ( int_status & OHCI_INT_RHPORT_STATUS_CHANGE_MASK ) { diff --git a/src/host/ohci/ohci.h b/src/host/ohci/ohci.h index 6f6ef9676..bfcdaf9a3 100644 --- a/src/host/ohci/ohci.h +++ b/src/host/ohci/ohci.h @@ -180,6 +180,8 @@ typedef struct TU_ATTR_ALIGNED(256) ohci_ed_t ed_pool[HCD_MAX_ENDPOINT]; ohci_gtd_t gtd_pool[HCD_MAX_XFER]; + volatile uint16_t frame_number_hi; + } ohci_data_t; //--------------------------------------------------------------------+ From 8fe887198b63520c23af9f4c27d9733f6bb18e90 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 18 Jun 2020 01:13:44 -0700 Subject: [PATCH 09/52] Add tx callback to cdc device Useful for continuous transmission of data, which is difficult currently because there is no notification of tx completion. --- src/class/cdc/cdc_device.c | 3 +++ src/class/cdc/cdc_device.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 9180065c4..b979b11f1 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,6 +416,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { + // invoke transmit callback to possibly refill tx fifo + if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); + if ( 0 == tud_cdc_n_write_flush(itf) ) { // There is no data left, a ZLP should be sent if diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index a2523d8d0..13b59416b 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -128,6 +128,9 @@ TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); // Invoked when received `wanted_char` TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); +// Invoked when space becomes available in TX buffer +TU_ATTR_WEAK void tud_cdc_tx_cb(uint8_t itf); + // Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); From 7ae47a93979071ec3c7724c35f1ef55572ad1a1f Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 20 Jun 2020 22:12:10 -0700 Subject: [PATCH 10/52] Call tud_cdc_tx_cb right after flush to keep tx fifo full --- src/class/cdc/cdc_device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index b979b11f1..d8be48688 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,9 +416,6 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { - // invoke transmit callback to possibly refill tx fifo - if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); - if ( 0 == tud_cdc_n_write_flush(itf) ) { // There is no data left, a ZLP should be sent if @@ -428,6 +425,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, NULL, 0); } } + + // invoke transmit callback to possibly refill tx fifo + if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); } // nothing to do with notif endpoint for now From 6178f8de2f0a0a766d7e4ff202c2528c1ecce7c6 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Wed, 1 Jul 2020 13:38:59 +0300 Subject: [PATCH 11/52] ESP32-S2: Handle the fact that available EP IN FIFOs are less than the number of available EP INs ESP32-S2 has only 5 available endpoint-in FIFOs (including EP0) but 7 available EP IN numbers. This change decouples the fifo number from the endpoint number, providing FIFO numbers until they reach the limit, at which point it will return false and assert an error that too many endpoints were allocated. --- src/portable/espressif/esp32s2/dcd_esp32s2.c | 31 +++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/portable/espressif/esp32s2/dcd_esp32s2.c b/src/portable/espressif/esp32s2/dcd_esp32s2.c index d49b69a10..21bad327a 100644 --- a/src/portable/espressif/esp32s2/dcd_esp32s2.c +++ b/src/portable/espressif/esp32s2/dcd_esp32s2.c @@ -71,6 +71,20 @@ static uint32_t _setup_packet[2]; #define XFER_CTL_BASE(_ep, _dir) &xfer_status[_ep][_dir] static xfer_ctl_t xfer_status[EP_MAX][2]; +// Max number of IN EP FIFOs +#define USB_FIFO_NUM 5 + +// Keep count of how many FIFOs are in use +static uint8_t dcd_allocated_fifos = 1; //FIFO0 is always in use + +// Will either return an unused FIFO number, or 0 if all are used. +static uint8_t dcd_get_free_fifo(){ + if (dcd_allocated_fifos < USB_FIFO_NUM) { + return dcd_allocated_fifos++; + } + return 0; +} + // Setup the control endpoint 0. static void bus_reset(void) { @@ -271,8 +285,12 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_edpt) // - Offset: GRXFSIZ + 16 + Size*(epnum-1) // - IN EP 1 gets FIFO 1, IN EP "n" gets FIFO "n". + uint8_t fifo_num = dcd_get_free_fifo(); + TU_ASSERT(fifo_num != 0); + + in_ep[epnum].diepctl &= ~(USB_D_TXFNUM1_M | USB_D_EPTYPE1_M | USB_DI_SETD0PID1 | USB_D_MPS1_M); in_ep[epnum].diepctl |= USB_D_USBACTEP1_M | - epnum << USB_D_TXFNUM1_S | + fifo_num << USB_D_TXFNUM1_S | desc_edpt->bmAttributes.xfer << USB_D_EPTYPE1_S | (desc_edpt->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS ? (1 << USB_DI_SETD0PID1_S) : 0) | desc_edpt->wMaxPacketSize.size << 0; @@ -282,8 +300,8 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_edpt) // Both TXFD and TXSA are in unit of 32-bit words. // IN FIFO 0 was configured during enumeration, hence the "+ 16". uint16_t const allocated_size = (USB0.grxfsiz & 0x0000ffff) + 16; - uint16_t const fifo_size = (EP_FIFO_SIZE/4 - allocated_size) / (EP_MAX-1); - uint32_t const fifo_offset = allocated_size + fifo_size*(epnum-1); + uint16_t const fifo_size = (EP_FIFO_SIZE/4 - allocated_size) / (USB_FIFO_NUM-1); + uint32_t const fifo_offset = allocated_size + fifo_size*(fifo_num-1); // DIEPTXF starts at FIFO #1. USB0.dieptxf[epnum - 1] = (fifo_size << USB_NPTXFDEP_S) | fifo_offset; @@ -361,7 +379,8 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) } // Flush the FIFO, and wait until we have confirmed it cleared. - USB0.grstctl |= ((epnum - 1) << USB_TXFNUM_S); + uint8_t const fifo_num = ((in_ep[epnum].diepctl >> USB_D_TXFNUM1_S) & USB_D_TXFNUM1_V); + USB0.grstctl |= (fifo_num << USB_TXFNUM_S); USB0.grstctl |= USB_TXFFLSH_M; while ((USB0.grstctl & USB_TXFFLSH_M) != 0) ; } else { @@ -653,6 +672,8 @@ static void _dcd_int_handler(void* arg) // start of reset ESP_EARLY_LOGV(TAG, "dcd_int_handler - reset"); USB0.gintsts = USB_USBRST_M; + // FIFOs will be reassigned when the endpoints are reopen + dcd_allocated_fifos = 1; bus_reset(); } @@ -681,6 +702,8 @@ static void _dcd_int_handler(void* arg) if (otg_int & USB_SESENDDET_M) { dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true); + // FIFOs will be reassigned when the endpoints are reopen + dcd_allocated_fifos = 1; } USB0.gotgint = otg_int; From 6976e642176cf76be9cc1bd2ef1a137a0e71e078 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 28 Jul 2020 14:36:15 +0700 Subject: [PATCH 12/52] fix msp430 gcc 9.2.0 warning in #465 --- .github/workflows/build.yml | 8 +++++--- src/portable/ti/msp430x5xx/dcd_msp430x5xx.c | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 51ec0e606..2ad274928 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,10 +41,12 @@ jobs: uses: actions/cache@v1 with: path: /tmp/dl/ - # Increment serial number at end when updating downloads - key: msp430-${{ runner.os }}-0 + # Increment gcc version number at end when updating downloads + key: msp430-${{ runner.os }}-9.2.0.50 - name: Install Toolchains + env: + MSP430GCC_URL: http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSPGCC/9_2_0_0/export/msp430-gcc-9.2.0.50_linux64.tar.bz2 run: | # ARM & RISC-V GCC from xpack npm install --global xpm @@ -55,7 +57,7 @@ jobs: # TI MSP430 GCC mkdir -p /tmp/dl/ - [ -f "/tmp/dl/msp430-gcc.tar.bz2" ] || wget --progress=dot:mega http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSPGCC/8_3_0_0/exports/msp430-gcc-8.3.0.16_linux64.tar.bz2 -O /tmp/dl/msp430-gcc.tar.bz2 + [ -f "/tmp/dl/msp430-gcc.tar.bz2" ] || wget --progress=dot:mega $MSP430GCC_URL -O /tmp/dl/msp430-gcc.tar.bz2 tar -C $HOME -xaf /tmp/dl/msp430-gcc.tar.bz2 echo "::add-path::`echo $HOME/msp430-gcc-*_linux64/bin`" diff --git a/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c b/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c index 8672050f0..93ba99543 100644 --- a/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c +++ b/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c @@ -70,8 +70,7 @@ typedef enum SIZXY = 7 } ep_regs_index_t; -#define EP_REGS(epnum, dir) &USBOEPCNF_1 + 64*dir + 8*(epnum - 1) - +#define EP_REGS(epnum, dir) ((ep_regs_t) ((uintptr_t)&USBOEPCNF_1 + 64*dir + 8*(epnum - 1))) static void bus_reset(void) { From a1a390a7883b35bbc00a85e52d1e2f76a18ebcbd Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Tue, 28 Jul 2020 10:54:23 +0300 Subject: [PATCH 13/52] Update dcd_esp32s2.c --- src/portable/espressif/esp32s2/dcd_esp32s2.c | 22 ++++++++------------ 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/portable/espressif/esp32s2/dcd_esp32s2.c b/src/portable/espressif/esp32s2/dcd_esp32s2.c index 21bad327a..5be1c82e5 100644 --- a/src/portable/espressif/esp32s2/dcd_esp32s2.c +++ b/src/portable/espressif/esp32s2/dcd_esp32s2.c @@ -54,6 +54,9 @@ // FIFO size in bytes #define EP_FIFO_SIZE 1024 +// Max number of IN EP FIFOs +#define EP_FIFO_NUM 5 + typedef struct { uint8_t *buffer; uint16_t total_len; @@ -71,17 +74,12 @@ static uint32_t _setup_packet[2]; #define XFER_CTL_BASE(_ep, _dir) &xfer_status[_ep][_dir] static xfer_ctl_t xfer_status[EP_MAX][2]; -// Max number of IN EP FIFOs -#define USB_FIFO_NUM 5 - // Keep count of how many FIFOs are in use -static uint8_t dcd_allocated_fifos = 1; //FIFO0 is always in use +static uint8_t _allocated_fifos = 1; //FIFO0 is always in use // Will either return an unused FIFO number, or 0 if all are used. -static uint8_t dcd_get_free_fifo(){ - if (dcd_allocated_fifos < USB_FIFO_NUM) { - return dcd_allocated_fifos++; - } +static uint8_t get_free_fifo(){ + if (_allocated_fifos < EP_FIFO_NUM) return _allocated_fifos++; return 0; } @@ -285,7 +283,7 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_edpt) // - Offset: GRXFSIZ + 16 + Size*(epnum-1) // - IN EP 1 gets FIFO 1, IN EP "n" gets FIFO "n". - uint8_t fifo_num = dcd_get_free_fifo(); + uint8_t fifo_num = get_free_fifo(); TU_ASSERT(fifo_num != 0); in_ep[epnum].diepctl &= ~(USB_D_TXFNUM1_M | USB_D_EPTYPE1_M | USB_DI_SETD0PID1 | USB_D_MPS1_M); @@ -300,7 +298,7 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_edpt) // Both TXFD and TXSA are in unit of 32-bit words. // IN FIFO 0 was configured during enumeration, hence the "+ 16". uint16_t const allocated_size = (USB0.grxfsiz & 0x0000ffff) + 16; - uint16_t const fifo_size = (EP_FIFO_SIZE/4 - allocated_size) / (USB_FIFO_NUM-1); + uint16_t const fifo_size = (EP_FIFO_SIZE/4 - allocated_size) / (EP_FIFO_NUM-1); uint32_t const fifo_offset = allocated_size + fifo_size*(fifo_num-1); // DIEPTXF starts at FIFO #1. @@ -673,7 +671,7 @@ static void _dcd_int_handler(void* arg) ESP_EARLY_LOGV(TAG, "dcd_int_handler - reset"); USB0.gintsts = USB_USBRST_M; // FIFOs will be reassigned when the endpoints are reopen - dcd_allocated_fifos = 1; + _allocated_fifos = 1; bus_reset(); } @@ -702,8 +700,6 @@ static void _dcd_int_handler(void* arg) if (otg_int & USB_SESENDDET_M) { dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true); - // FIFOs will be reassigned when the endpoints are reopen - dcd_allocated_fifos = 1; } USB0.gotgint = otg_int; From 5af08e2ffc47c6a0c90f678edd04ca57480a1747 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 29 Jul 2020 16:59:07 +0700 Subject: [PATCH 14/52] fix strict prototype --- src/portable/espressif/esp32s2/dcd_esp32s2.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/portable/espressif/esp32s2/dcd_esp32s2.c b/src/portable/espressif/esp32s2/dcd_esp32s2.c index b4d869517..755e06701 100644 --- a/src/portable/espressif/esp32s2/dcd_esp32s2.c +++ b/src/portable/espressif/esp32s2/dcd_esp32s2.c @@ -78,7 +78,8 @@ static xfer_ctl_t xfer_status[EP_MAX][2]; static uint8_t _allocated_fifos = 1; //FIFO0 is always in use // Will either return an unused FIFO number, or 0 if all are used. -static uint8_t get_free_fifo(){ +static uint8_t get_free_fifo(void) +{ if (_allocated_fifos < EP_FIFO_NUM) return _allocated_fifos++; return 0; } From 10a8ef7614b1f154576c6b3cdd14f15d1b18a443 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 29 Jul 2020 17:04:47 +0700 Subject: [PATCH 15/52] fix nested extern declaration of 'SystemCoreClock' [-Werror=nested-externs] --- src/portable/st/synopsys/dcd_synopsys.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c index 7eb63e52f..03c33c598 100644 --- a/src/portable/st/synopsys/dcd_synopsys.c +++ b/src/portable/st/synopsys/dcd_synopsys.c @@ -231,6 +231,7 @@ static void bus_reset(uint8_t rhport) } // Set turn-around timeout according to link speed +extern uint32_t SystemCoreClock; static void set_turnaround(USB_OTG_GlobalTypeDef * usb_otg, tusb_speed_t speed) { usb_otg->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT; @@ -243,7 +244,6 @@ static void set_turnaround(USB_OTG_GlobalTypeDef * usb_otg, tusb_speed_t speed) else { // Turnaround timeout depends on the MCU clock - extern uint32_t SystemCoreClock; uint32_t turnaround; if ( SystemCoreClock >= 32000000U ) From c3b0389f10188e016c0cd0bb74638ecfb0eae8ea Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Fri, 31 Jul 2020 15:48:28 +0200 Subject: [PATCH 16/52] Fix synopsys size check for ISO endpoint Constraint was incorrect for ISO endpoint as stated in TODO. --- src/portable/st/synopsys/dcd_synopsys.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c index 03c33c598..f4430f582 100644 --- a/src/portable/st/synopsys/dcd_synopsys.c +++ b/src/portable/st/synopsys/dcd_synopsys.c @@ -514,8 +514,14 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) TU_ASSERT(epnum < EP_MAX); - // TODO ISO endpoint can be up to 1024 bytes - TU_ASSERT(desc_edpt->wMaxPacketSize.size <= (get_speed(rhport) == TUSB_SPEED_HIGH ? 512 : 64)); + if (desc_edpt->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS) + { + TU_ASSERT(desc_edpt->wMaxPacketSize.size <= (get_speed(rhport) == TUSB_SPEED_HIGH ? 1024 : 1023)); + } + else + { + TU_ASSERT(desc_edpt->wMaxPacketSize.size <= (get_speed(rhport) == TUSB_SPEED_HIGH ? 512 : 64)); + } xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir); xfer->max_size = desc_edpt->wMaxPacketSize.size; From 9bf2b3336610044dfb0187311797c5324eeec0ea Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 1 Aug 2020 12:02:59 +0700 Subject: [PATCH 17/52] correct isr context for nrf DCD_EVENT_UNPLUGGED also rename debug lookup to prevent conflict --- src/class/msc/msc_device.c | 6 +++--- src/common/tusb_common.h | 10 +++++----- src/portable/nordic/nrf5x/dcd_nrf5x.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 692fd2441..9b79b68a7 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -105,7 +105,7 @@ static inline uint16_t rdwr10_get_blockcount(uint8_t const command[]) //--------------------------------------------------------------------+ #if CFG_TUSB_DEBUG >= 2 -static lookup_entry_t const _msc_scsi_cmd_lookup[] = +static tu_lookup_entry_t const _msc_scsi_cmd_lookup[] = { { .key = SCSI_CMD_TEST_UNIT_READY , .data = "Test Unit Ready" }, { .key = SCSI_CMD_INQUIRY , .data = "Inquiry" }, @@ -120,7 +120,7 @@ static lookup_entry_t const _msc_scsi_cmd_lookup[] = { .key = SCSI_CMD_WRITE_10 , .data = "Write10" } }; -static lookup_table_t const _msc_scsi_cmd_table = +static tu_lookup_table_t const _msc_scsi_cmd_table = { .count = TU_ARRAY_SIZE(_msc_scsi_cmd_lookup), .items = _msc_scsi_cmd_lookup @@ -418,7 +418,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t TU_ASSERT( event == XFER_RESULT_SUCCESS && xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE ); - TU_LOG2(" SCSI Command: %s\r\n", lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0])); + TU_LOG2(" SCSI Command: %s\r\n", tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0])); // TU_LOG2_MEM(p_cbw, xferred_bytes, 2); p_csw->signature = MSC_CSW_SIGNATURE; diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 1dba6c914..d95c0ffc4 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -251,16 +251,16 @@ void tu_print_var(uint8_t const* buf, uint32_t bufsize) typedef struct { uint32_t key; - char const * data; -}lookup_entry_t; + const char* data; +} tu_lookup_entry_t; typedef struct { uint16_t count; - lookup_entry_t const* items; -} lookup_table_t; + tu_lookup_entry_t const* items; +} tu_lookup_table_t; -static inline char const* lookup_find(lookup_table_t const* p_table, uint32_t key) +static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key) { for(uint16_t i=0; icount; i++) { diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 7ca32c4f6..fdf56b6ca 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -829,7 +829,7 @@ void tusb_hal_nrf_power_event (uint32_t event) hfclk_disable(); - dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true); + dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) ? true : false); } break; From acde49ccc97466f603dc7face7ab70ea10d38ee3 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 1 Aug 2020 20:14:58 +0700 Subject: [PATCH 18/52] enable pull-up in dcd_init() instead of usbd --- docs/porting.md | 2 +- src/device/usbd.c | 1 - src/portable/dialog/da146xx/dcd_da146xx.c | 4 ++-- src/portable/espressif/esp32s2/dcd_esp32s2.c | 4 ++-- src/portable/microchip/samg/dcd_samg.c | 3 +-- src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 2 ++ src/portable/nxp/transdimension/dcd_transdimension.c | 3 ++- src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c | 4 ++-- src/portable/st/synopsys/dcd_synopsys.c | 2 ++ src/portable/ti/msp430x5xx/dcd_msp430x5xx.c | 3 +++ test/test/device/msc/test_msc_device.c | 1 - test/test/device/usbd/test_usbd.c | 1 - 12 files changed, 17 insertions(+), 13 deletions(-) diff --git a/docs/porting.md b/docs/porting.md index 473768450..d3c688a81 100644 --- a/docs/porting.md +++ b/docs/porting.md @@ -62,7 +62,7 @@ All of the code for the low-level device API is in `src/portable//USB_MCTRL_REG = USB_USB_MCTRL_REG_USBEN_Msk; tusb_vbus_changed((CRG_TOP->ANA_STATUS_REG & CRG_TOP_ANA_STATUS_REG_VBUS_AVAILABLE_Msk) != 0); + + dcd_connect(rhport); } void dcd_int_enable(uint8_t rhport) diff --git a/src/portable/espressif/esp32s2/dcd_esp32s2.c b/src/portable/espressif/esp32s2/dcd_esp32s2.c index 755e06701..58d92e728 100644 --- a/src/portable/espressif/esp32s2/dcd_esp32s2.c +++ b/src/portable/espressif/esp32s2/dcd_esp32s2.c @@ -165,8 +165,6 @@ static void enum_done_processing(void) *------------------------------------------------------------------*/ void dcd_init(uint8_t rhport) { - (void)rhport; - ESP_LOGV(TAG, "DCD init - Start"); // A. Disconnect @@ -204,6 +202,8 @@ void dcd_init(uint8_t rhport) USB_ENUMDONEMSK_M | USB_RESETDETMSK_M | USB_DISCONNINTMSK_M; // host most only + + dcd_connect(rhport); } void dcd_set_address(uint8_t rhport, uint8_t dev_addr) diff --git a/src/portable/microchip/samg/dcd_samg.c b/src/portable/microchip/samg/dcd_samg.c index 4a8ed53f3..2b64df50b 100644 --- a/src/portable/microchip/samg/dcd_samg.c +++ b/src/portable/microchip/samg/dcd_samg.c @@ -154,9 +154,8 @@ static void bus_reset(void) // Initialize controller to device mode void dcd_init (uint8_t rhport) { - (void) rhport; - tu_memclr(_dcd_xfer, sizeof(_dcd_xfer)); + dcd_connect(rhport); } // Enable device interrupt diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index f7a59a808..39c5e6638 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -181,6 +181,8 @@ void dcd_init(uint8_t rhport) LPC_USB->UDCAH = (uint32_t) _dcd.udca; LPC_USB->DMAIntEn = (DMA_INT_END_OF_XFER_MASK /*| DMA_INT_NEW_DD_REQUEST_MASK*/ | DMA_INT_ERROR_MASK); + dcd_connect(rhport); + // Clear pending IRQ NVIC_ClearPendingIRQ(USB_IRQn); } diff --git a/src/portable/nxp/transdimension/dcd_transdimension.c b/src/portable/nxp/transdimension/dcd_transdimension.c index 46751cf5d..755e7635e 100644 --- a/src/portable/nxp/transdimension/dcd_transdimension.c +++ b/src/portable/nxp/transdimension/dcd_transdimension.c @@ -345,7 +345,8 @@ void dcd_init(uint8_t rhport) dcd_reg->USBSTS = dcd_reg->USBSTS; dcd_reg->USBINTR = INTR_USB | INTR_ERROR | INTR_PORT_CHANGE | INTR_RESET | INTR_SUSPEND /*| INTR_SOF*/; - dcd_reg->USBCMD &= ~0x00FF0000; // Interrupt Threshold Interval = 0 + dcd_reg->USBCMD &= ~0x00FF0000; // Interrupt Threshold Interval = 0 + dcd_reg->USBCMD |= USBCMD_RUN_STOP; // Connect } void dcd_int_enable(uint8_t rhport) diff --git a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c index da184abe7..32179874e 100644 --- a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c +++ b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c @@ -205,7 +205,6 @@ static inline void reg16_clear_bits(__IO uint16_t *reg, uint16_t mask) { void dcd_init (uint8_t rhport) { - (void)rhport; /* Clocks should already be enabled */ /* Use __HAL_RCC_USB_CLK_ENABLE(); to enable the clocks before calling this function */ @@ -244,7 +243,8 @@ void dcd_init (uint8_t rhport) USB->CNTR |= USB_CNTR_RESETM | (USE_SOF ? USB_CNTR_SOFM : 0) | USB_CNTR_ESOFM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM; dcd_handle_bus_reset(); - // Data-line pull-up is left disconnected. + // Enable pull-up if supported + if ( dcd_connect ) dcd_connect(rhport); } // Define only on MCU with internal pull-up. BSP can define on MCU without internal PU. diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c index f4430f582..a284c0384 100644 --- a/src/portable/st/synopsys/dcd_synopsys.c +++ b/src/portable/st/synopsys/dcd_synopsys.c @@ -453,6 +453,8 @@ void dcd_init (uint8_t rhport) // Enable global interrupt usb_otg->GAHBCFG |= USB_OTG_GAHBCFG_GINT; + + dcd_connect(rhport); } void dcd_int_enable (uint8_t rhport) diff --git a/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c b/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c index 8672050f0..b6951efef 100644 --- a/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c +++ b/src/portable/ti/msp430x5xx/dcd_msp430x5xx.c @@ -134,6 +134,9 @@ void dcd_init (uint8_t rhport) // Enable reset and wait for it before continuing. USBIE |= RSTRIE; + // Enable pullup. + USBCNF |= PUR_EN; + USBKEYPID = 0; } diff --git a/test/test/device/msc/test_msc_device.c b/test/test/device/msc/test_msc_device.c index 0382fb327..a01ef153a 100644 --- a/test/test/device/msc/test_msc_device.c +++ b/test/test/device/msc/test_msc_device.c @@ -199,7 +199,6 @@ void setUp(void) if ( !tusb_inited() ) { dcd_init_Expect(rhport); - dcd_connect_Expect(rhport); tusb_init(); } diff --git a/test/test/device/usbd/test_usbd.c b/test/test/device/usbd/test_usbd.c index 1bb32c1e5..06372b2e4 100644 --- a/test/test/device/usbd/test_usbd.c +++ b/test/test/device/usbd/test_usbd.c @@ -127,7 +127,6 @@ void setUp(void) { mscd_init_Expect(); dcd_init_Expect(rhport); - dcd_connect_Expect(rhport); tusb_init(); } } From 7d9efd06979f6cdde2a4093f0c26e8100312c92c Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 4 Aug 2020 14:18:12 +0700 Subject: [PATCH 19/52] manually submit unplugged event for nrf dcd_disconnect() --- src/portable/nordic/nrf5x/dcd_nrf5x.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index fdf56b6ca..cd35f2e5b 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -271,6 +271,10 @@ void dcd_disconnect(uint8_t rhport) { (void) rhport; NRF_USBD->USBPULLUP = 0; + + // Disable Pull-up does not trigger Power USB Removed, in fact it have no + // impact on the USB Power status at all -> need to submit unplugged event to the stack. + dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, false); } // connect by enabling internal pull-up resistor on D+/D- @@ -693,6 +697,8 @@ void tusb_hal_nrf_power_event (uint32_t event) switch ( event ) { case USB_EVT_DETECTED: + TU_LOG2("Power USB Detect\r\n"); + if ( !NRF_USBD->ENABLE ) { /* Prepare for READY event receiving */ @@ -743,6 +749,12 @@ void tusb_hal_nrf_power_event (uint32_t event) break; case USB_EVT_READY: + TU_LOG2("Power USB Ready\r\n"); + + // Skip if pull-up is enabled and HCLK is already running. + // Application probably call this more than necessary. + if ( NRF_USBD->USBPULLUP && hfclk_running() ) break; + /* Waiting for USBD peripheral enabled */ while ( !(USBD_EVENTCAUSE_READY_Msk & NRF_USBD->EVENTCAUSE) ) { } @@ -810,6 +822,7 @@ void tusb_hal_nrf_power_event (uint32_t event) break; case USB_EVT_REMOVED: + TU_LOG2("Power USB Removed\r\n"); if ( NRF_USBD->ENABLE ) { // Abort all transfers From e9aa36a6e8991335d72666058f9c135c3331d238 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Fri, 31 Jul 2020 16:04:10 +0200 Subject: [PATCH 20/52] Fix synopsys odd/even frame bit for IN ISO endpoints For ISO endpoint driver has to specify when data is to be transmitted (odd or even frame). Currently code was not updating this bit resulting in data being sent every other frame. If interval was 1ms full data packed was sent every 2ms, and ZLP was sent in between. --- src/portable/st/synopsys/dcd_synopsys.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c index 03c33c598..283fefd01 100644 --- a/src/portable/st/synopsys/dcd_synopsys.c +++ b/src/portable/st/synopsys/dcd_synopsys.c @@ -365,6 +365,13 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c ((total_bytes << USB_OTG_DIEPTSIZ_XFRSIZ_Pos) & USB_OTG_DIEPTSIZ_XFRSIZ_Msk); in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_EPENA | USB_OTG_DIEPCTL_CNAK; + // For ISO endpoint set correct odd/even bit for next frame. + if ((in_ep[epnum].DIEPCTL & USB_OTG_DIEPCTL_EPTYP) == USB_OTG_DIEPCTL_EPTYP_0) + { + // Take odd/even bit from frame counter. + uint32_t const odd_frame_now = (dev->DSTS & (1u << USB_OTG_DSTS_FNSOF_Pos)); + in_ep[epnum].DIEPCTL |= (odd_frame_now ? USB_OTG_DIEPCTL_SD0PID_SEVNFRM_Msk : USB_OTG_DIEPCTL_SODDFRM_Msk); + } // Enable fifo empty interrupt only if there are something to put in the fifo. if(total_bytes != 0) { dev->DIEPEMPMSK |= (1 << epnum); From 946d4b735aa669b2f255399f5cc1f535c9b6bf60 Mon Sep 17 00:00:00 2001 From: Peter Lawrence <12226419+majbthrd@users.noreply.github.com> Date: Wed, 5 Aug 2020 20:20:12 -0500 Subject: [PATCH 21/52] update lib/lwip to STABLE-2_1_2_RELEASE --- lib/lwip | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lwip b/lib/lwip index 0192fe773..159e31b68 160000 --- a/lib/lwip +++ b/lib/lwip @@ -1 +1 @@ -Subproject commit 0192fe773ec28e11f66ec76f4e827fbb58b7e257 +Subproject commit 159e31b689577dbf69cf0683bbaffbd71fa5ee10 From 01b9b77d3b94ed83931d6a51c1b357f03f188e76 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 7 Aug 2020 14:47:32 +0700 Subject: [PATCH 22/52] allow application driver to overwrite built-in one - position application driver before built-in - remove dcd.h from public include. --- src/class/msc/msc_device.c | 1 + src/class/usbtmc/usbtmc_device.c | 1 - src/device/usbd.c | 47 ++++++++++++++++++-------------- src/device/usbd.h | 16 ++--------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 9b79b68a7..f3f2e536e 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -31,6 +31,7 @@ #include "common/tusb_common.h" #include "msc_device.h" #include "device/usbd_pvt.h" +#include "device/dcd.h" // for faking dcd_event_xfer_complete //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF diff --git a/src/class/usbtmc/usbtmc_device.c b/src/class/usbtmc/usbtmc_device.c index a3ff76079..bc5f23f42 100644 --- a/src/class/usbtmc/usbtmc_device.c +++ b/src/class/usbtmc/usbtmc_device.c @@ -80,7 +80,6 @@ #include #include "usbtmc.h" #include "usbtmc_device.h" -#include "device/dcd.h" #include "device/usbd.h" #include "osal/osal.h" diff --git a/src/device/usbd.c b/src/device/usbd.c index 7fc7189a5..6b91048b6 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -203,30 +203,30 @@ static usbd_class_driver_t const _usbd_driver[] = #endif }; -enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) }; +enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) }; // Additional class drivers implemented by application static usbd_class_driver_t const * _app_driver = NULL; static uint8_t _app_driver_count = 0; -// virtually joins built-in and application drivers together -// All drivers = built-in + application +// virtually joins built-in and application drivers together. +// Application is positioned first to allow overwriting built-in ones. static inline usbd_class_driver_t const * get_driver(uint8_t drvid) { - // Built-in drivers - if (drvid < USBD_CLASS_DRIVER_COUNT) return &_usbd_driver[drvid]; - - // App drivers + // Application drivers if ( usbd_app_driver_get_cb ) { - drvid -= USBD_CLASS_DRIVER_COUNT; if ( drvid < _app_driver_count ) return &_app_driver[drvid]; + drvid -= _app_driver_count; } + // Built-in drivers + if (drvid < BUILTIN_DRIVER_COUNT) return &_usbd_driver[drvid]; + return NULL; } -#define TOTAL_DRIVER_COUNT (USBD_CLASS_DRIVER_COUNT+_app_driver_count) +#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT) //--------------------------------------------------------------------+ // DCD Event @@ -245,6 +245,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const static bool process_set_config(uint8_t rhport, uint8_t cfg_num); static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request); +// from usbd_control.c void usbd_control_reset(void); void usbd_control_set_request(tusb_control_request_t const *request); void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) ); @@ -252,7 +253,7 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, //--------------------------------------------------------------------+ -// Debugging +// Debug //--------------------------------------------------------------------+ #if CFG_TUSB_DEBUG >= 2 static char const* const _usbd_event_str[DCD_EVENT_COUNT] = @@ -327,6 +328,20 @@ bool tud_remote_wakeup(void) return true; } +bool tud_disconnect(void) +{ + TU_VERIFY(dcd_disconnect); + dcd_disconnect(TUD_OPT_RHPORT); + return true; +} + +bool tud_connect(void) +{ + TU_VERIFY(dcd_connect); + dcd_connect(TUD_OPT_RHPORT); + return true; +} + //--------------------------------------------------------------------+ // USBD Task //--------------------------------------------------------------------+ @@ -593,7 +608,6 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const uint8_t const cfg_num = (uint8_t) p_request->wValue; if ( !_usbd_dev.configured && cfg_num ) TU_ASSERT( process_set_config(rhport, cfg_num) ); - _usbd_dev.configured = cfg_num ? 1 : 0; tud_control_status(rhport, p_request); @@ -688,18 +702,12 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const break; case TUSB_REQ_CLEAR_FEATURE: - if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue ) - { - usbd_edpt_clear_stall(rhport, ep_addr); - } + if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue ) usbd_edpt_clear_stall(rhport, ep_addr); tud_control_status(rhport, p_request); break; case TUSB_REQ_SET_FEATURE: - if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue ) - { - usbd_edpt_stall(rhport, ep_addr); - } + if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue ) usbd_edpt_stall(rhport, ep_addr); tud_control_status(rhport, p_request); break; @@ -773,7 +781,6 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) for (drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { usbd_class_driver_t const *driver = get_driver(drv_id); - uint16_t const drv_len = driver->open(rhport, desc_itf, remaining_len); if ( drv_len > 0 ) diff --git a/src/device/usbd.h b/src/device/usbd.h index 098e94075..5338be157 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -35,7 +35,6 @@ #endif #include "common/tusb_common.h" -#include "dcd.h" //--------------------------------------------------------------------+ // Application API @@ -53,6 +52,7 @@ void tud_task (void); bool tud_task_event_ready(void); // Interrupt handler, name alias to DCD +extern void dcd_int_handler(uint8_t rhport); #define tud_int_handler dcd_int_handler // Get current bus speed @@ -75,21 +75,11 @@ bool tud_remote_wakeup(void); // Enable pull-up resistor on D+ D- // Return false on unsupported MCUs -static inline bool tud_disconnect(void) -{ - TU_VERIFY(dcd_disconnect); - dcd_disconnect(TUD_OPT_RHPORT); - return true; -} +bool tud_disconnect(void); // Disable pull-up resistor on D+ D- // Return false on unsupported MCUs -static inline bool tud_connect(void) -{ - TU_VERIFY(dcd_connect); - dcd_connect(TUD_OPT_RHPORT); - return true; -} +bool tud_connect(void); // Carry out Data and Status stage of control transfer // - If len = 0, it is equivalent to sending status only From a9d362185b5985062ce3f1a618a67938d9c9d5f7 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 7 Aug 2020 15:06:19 +0700 Subject: [PATCH 23/52] update note for app driver list mustbe accesible at all time. --- src/device/usbd_pvt.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index e430a81d9..a5d223329 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -54,6 +54,7 @@ typedef struct // Invoked when initializing device stack to get additional class drivers. // Can optionally implemented by application to extend/overwrite class driver support. +// Note: The drivers array must be accessible at all time when stack is active usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK; //--------------------------------------------------------------------+ From 61e96e97cb078a8ae52a2bef107ebefd540603a4 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 11 Aug 2020 22:09:16 +0700 Subject: [PATCH 24/52] use usbd_edpt_open in bth driver --- src/class/bth/bth_device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class/bth/bth_device.c b/src/class/bth/bth_device.c index 04011bcdb..252e20e37 100755 --- a/src/class/bth/bth_device.c +++ b/src/class/bth/bth_device.c @@ -121,7 +121,7 @@ uint16_t btd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_ desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0); - TU_ASSERT(dcd_edpt_open(rhport, desc_ep), 0); + TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0); _btd_itf.ep_ev = desc_ep->bEndpointAddress; // Open endpoint pair From 88c5e2a37ffee3d82107076642b64227cd66dc17 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Wed, 12 Aug 2020 10:18:11 +0200 Subject: [PATCH 25/52] Fix synopsys fifo flush during stall Wrong FIFO was flushed in dcd_edpt_stall(). (epnum - 1) should only be used when accessing DIEPTXF registers. For DIEPCTL and GRSTCTL epnum is correct index. --- src/portable/st/synopsys/dcd_synopsys.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c index 3c349582f..aaf586ed0 100644 --- a/src/portable/st/synopsys/dcd_synopsys.c +++ b/src/portable/st/synopsys/dcd_synopsys.c @@ -667,7 +667,7 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) } // Flush the FIFO, and wait until we have confirmed it cleared. - usb_otg->GRSTCTL |= ((epnum - 1) << USB_OTG_GRSTCTL_TXFNUM_Pos); + usb_otg->GRSTCTL |= (epnum << USB_OTG_GRSTCTL_TXFNUM_Pos); usb_otg->GRSTCTL |= USB_OTG_GRSTCTL_TXFFLSH; while((usb_otg->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH_Msk) != 0); } else { From e8d50a3c57c0665cb254d3a88e3e18f9c31ad045 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Fri, 14 Aug 2020 14:29:35 +0200 Subject: [PATCH 26/52] Add dcd_edpt_close() to synopsys Endpoint close was implemented only in one driver so far. This function is needed for interfaces with several alternate settings. The way FIFO is allocated in dcd_edpt_open() allows to correctly close only one IN endpoint (the one that was opened last). --- src/portable/st/synopsys/dcd_synopsys.c | 38 ++++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/portable/st/synopsys/dcd_synopsys.c b/src/portable/st/synopsys/dcd_synopsys.c index aaf586ed0..cc7181c3d 100644 --- a/src/portable/st/synopsys/dcd_synopsys.c +++ b/src/portable/st/synopsys/dcd_synopsys.c @@ -637,9 +637,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t return true; } -// TODO: The logic for STALLing and disabling an endpoint is very similar -// (send STALL versus NAK handshakes back). Refactor into resuable function. -void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) +static void dcd_edpt_disable (uint8_t rhport, uint8_t ep_addr, bool stall) { (void) rhport; @@ -654,14 +652,14 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) if(dir == TUSB_DIR_IN) { // Only disable currently enabled non-control endpoint if ( (epnum == 0) || !(in_ep[epnum].DIEPCTL & USB_OTG_DIEPCTL_EPENA) ){ - in_ep[epnum].DIEPCTL |= (USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_STALL); + in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_SNAK | (stall ? USB_OTG_DIEPCTL_STALL : 0); } else { // Stop transmitting packets and NAK IN xfers. in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_SNAK; while((in_ep[epnum].DIEPINT & USB_OTG_DIEPINT_INEPNE) == 0); // Disable the endpoint. - in_ep[epnum].DIEPCTL |= (USB_OTG_DIEPCTL_STALL | USB_OTG_DIEPCTL_EPDIS); + in_ep[epnum].DIEPCTL |= USB_OTG_DIEPCTL_EPDIS | (stall ? USB_OTG_DIEPCTL_STALL : 0); while((in_ep[epnum].DIEPINT & USB_OTG_DIEPINT_EPDISD_Msk) == 0); in_ep[epnum].DIEPINT = USB_OTG_DIEPINT_EPDISD; } @@ -673,7 +671,7 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) } else { // Only disable currently enabled non-control endpoint if ( (epnum == 0) || !(out_ep[epnum].DOEPCTL & USB_OTG_DOEPCTL_EPENA) ){ - out_ep[epnum].DOEPCTL |= USB_OTG_DOEPCTL_STALL; + out_ep[epnum].DOEPCTL |= stall ? USB_OTG_DOEPCTL_STALL : 0; } else { // Asserting GONAK is required to STALL an OUT endpoint. // Simpler to use polling here, we don't use the "B"OUTNAKEFF interrupt @@ -683,7 +681,7 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) while((usb_otg->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF_Msk) == 0); // Ditto here- disable the endpoint. - out_ep[epnum].DOEPCTL |= (USB_OTG_DOEPCTL_STALL | USB_OTG_DOEPCTL_EPDIS); + out_ep[epnum].DOEPCTL |= USB_OTG_DOEPCTL_EPDIS | (stall ? USB_OTG_DOEPCTL_STALL : 0); while((out_ep[epnum].DOEPINT & USB_OTG_DOEPINT_EPDISD_Msk) == 0); out_ep[epnum].DOEPINT = USB_OTG_DOEPINT_EPDISD; @@ -693,6 +691,32 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) } } +/** + * Close an endpoint. + */ +void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) +{ + USB_OTG_GlobalTypeDef * usb_otg = GLOBAL_BASE(rhport); + + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); + + dcd_edpt_disable(rhport, ep_addr, false); + if (dir == TUSB_DIR_IN) + { + uint16_t const fifo_size = (usb_otg->DIEPTXF[epnum - 1] & USB_OTG_DIEPTXF_INEPTXFD_Msk) >> USB_OTG_DIEPTXF_INEPTXFD_Pos; + uint16_t const fifo_start = (usb_otg->DIEPTXF[epnum - 1] & USB_OTG_DIEPTXF_INEPTXSA_Msk) >> USB_OTG_DIEPTXF_INEPTXSA_Pos; + // For now only endpoint that has FIFO at the end of FIFO memory can be closed without fuss. + TU_ASSERT(fifo_start + fifo_size == _allocated_fifo_words,); + _allocated_fifo_words -= fifo_size; + } +} + +void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) +{ + dcd_edpt_disable(rhport, ep_addr, true); +} + void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; From 15b063beb287785b2bf84d00361000036afaba2c Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 20 Aug 2020 02:20:01 -0700 Subject: [PATCH 27/52] Smarter CDC TX refill logic --- src/class/cdc/cdc_device.c | 10 ++++++---- src/class/cdc/cdc_device.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index d8be48688..da9e94133 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,7 +416,12 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { - if ( 0 == tud_cdc_n_write_flush(itf) ) + uint32_t flushed = tud_cdc_n_write_flush(itf); + + // invoke transmit callback to possibly refill tx fifo + if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf); + + if ( 0 == flushed && tu_fifo_empty(&p_cdc->tx_ff) ) { // There is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero @@ -425,9 +430,6 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, NULL, 0); } } - - // invoke transmit callback to possibly refill tx fifo - if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); } // nothing to do with notif endpoint for now diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 13b59416b..7e9e8ca21 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -129,7 +129,7 @@ TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); // Invoked when space becomes available in TX buffer -TU_ATTR_WEAK void tud_cdc_tx_cb(uint8_t itf); +TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf); // Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); From 72183c7bb44ff117d9abac7b6e9c373c0945fb0b Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 20 Aug 2020 09:59:23 -0700 Subject: [PATCH 28/52] Slight optimization for cdc tx refill --- src/class/cdc/cdc_device.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index da9e94133..513fe203b 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,12 +416,10 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { - uint32_t flushed = tud_cdc_n_write_flush(itf); - // invoke transmit callback to possibly refill tx fifo if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf); - if ( 0 == flushed && tu_fifo_empty(&p_cdc->tx_ff) ) + if ( 0 == tud_cdc_n_write_flush(itf) ) { // There is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero From 76fe8ac61222856efd791fe5d20e89b3ffced71b Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 21 Aug 2020 12:19:38 +0700 Subject: [PATCH 29/52] fix samg55 ci build --- hw/bsp/samg55xplained/board.mk | 4 ++-- hw/mcu/microchip | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hw/bsp/samg55xplained/board.mk b/hw/bsp/samg55xplained/board.mk index e0c24ed9c..b53757dff 100644 --- a/hw/bsp/samg55xplained/board.mk +++ b/hw/bsp/samg55xplained/board.mk @@ -18,8 +18,8 @@ ASF_DIR = hw/mcu/microchip/samg55 LD_FILE = hw/bsp/$(BOARD)/samg55j19_flash.ld SRC_C += \ - $(ASF_DIR)/samg55/gcc/gcc/startup_samg55j19.c \ - $(ASF_DIR)/samg55/gcc/system_samg55j19.c \ + $(ASF_DIR)/samg55/gcc/gcc/startup_samg55.c \ + $(ASF_DIR)/samg55/gcc/system_samg55.c \ $(ASF_DIR)/hpl/core/hpl_init.c \ $(ASF_DIR)/hpl/usart/hpl_usart.c \ $(ASF_DIR)/hpl/pmc/hpl_pmc.c \ diff --git a/hw/mcu/microchip b/hw/mcu/microchip index 66b5a1199..168322aa7 160000 --- a/hw/mcu/microchip +++ b/hw/mcu/microchip @@ -1 +1 @@ -Subproject commit 66b5a11995025426224e0ba6f377322e6e8893b6 +Subproject commit 168322aa7100f60d6837505f5705fd61454dac27 From 91f7ce9769f9cacf4f7e86dfd72b2a93ad2f4bfb Mon Sep 17 00:00:00 2001 From: Katherine Temkin Date: Thu, 20 Aug 2020 21:49:35 -0600 Subject: [PATCH 30/52] add support for SAMD11 devices / add samd11_xplained board --- README.md | 2 +- docs/boards.md | 1 + hw/bsp/samd11_xplained/board.mk | 53 +++++++ hw/bsp/samd11_xplained/samd11_xplained.c | 152 ++++++++++++++++++++ hw/bsp/samd11_xplained/samd11d14am_flash.ld | 143 ++++++++++++++++++ 5 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 hw/bsp/samd11_xplained/board.mk create mode 100644 hw/bsp/samd11_xplained/samd11_xplained.c create mode 100644 hw/bsp/samd11_xplained/samd11d14am_flash.ld diff --git a/README.md b/README.md index 260248eda..6ca41e0f0 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The stack supports the following MCUs: - **Espressif:** ESP32-S2 - **Dialog:** DA1469x -- **MicroChip:** SAMD21, SAMD51, SAME5x (device only) +- **MicroChip:** SAMD11, SAMD21, SAMD51, SAME5x (device only) - **NordicSemi:** nRF52833, nRF52840 - **Nuvoton:** NUC120, NUC121/NUC125, NUC126, NUC505 - **NXP:** diff --git a/docs/boards.md b/docs/boards.md index 53dc95397..381d124cc 100644 --- a/docs/boards.md +++ b/docs/boards.md @@ -28,6 +28,7 @@ This code base already had supported for a handful of following boards (sorted a - [Adafruit ItsyBitsy M4 Express](https://www.adafruit.com/product/3800) - [Adafruit Metro M0 Express](https://www.adafruit.com/product/3505) - [Adafruit Metro M4 Express](https://www.adafruit.com/product/3382) +- [Microchip SAMD11 Xplained](https://www.microchip.com/developmenttools/ProductDetails/atsamd11-xpro) - [Seeeduino Xiao](https://www.seeedstudio.com/Seeeduino-XIAO-Arduino-Microcontroller-SAMD21-Cortex-M0+-p-4426.html) - [Great Scott Gadgets LUNA](https://greatscottgadgets.com/luna/) diff --git a/hw/bsp/samd11_xplained/board.mk b/hw/bsp/samd11_xplained/board.mk new file mode 100644 index 000000000..db0c62cf1 --- /dev/null +++ b/hw/bsp/samd11_xplained/board.mk @@ -0,0 +1,53 @@ +CFLAGS += \ + -ffunction-sections \ + -fdata-sections \ + -Wl,--gc-sections \ + -mthumb \ + -mabi=aapcs-linux \ + -mcpu=cortex-m0plus \ + -nostdlib -nostartfiles \ + -D__SAMD11D14AM__ \ + -DCONF_DFLL_OVERWRITE_CALIBRATION=0 \ + -DOSC32K_OVERWRITE_CALIBRATION=0 \ + -DCFG_TUSB_MCU=OPT_MCU_SAMD21 \ + -fshort-enums \ + -Os + +# All source paths should be relative to the top level. +LD_FILE = hw/bsp/$(BOARD)/samd11d14am_flash.ld + +SRC_C += \ + hw/mcu/microchip/samd11/gcc/gcc/startup_samd11.c \ + hw/mcu/microchip/samd11/gcc/system_samd11.c \ + hw/mcu/microchip/samd11/hpl/gclk/hpl_gclk.c \ + hw/mcu/microchip/samd11/hpl/pm/hpl_pm.c \ + hw/mcu/microchip/samd11/hpl/sysctrl/hpl_sysctrl.c \ + hw/mcu/microchip/samd11/hal/src/hal_atomic.c + +INC += \ + $(TOP)/hw/mcu/microchip/samd11/ \ + $(TOP)/hw/mcu/microchip/samd11/config \ + $(TOP)/hw/mcu/microchip/samd11/include \ + $(TOP)/hw/mcu/microchip/samd11/hal/include \ + $(TOP)/hw/mcu/microchip/samd11/hal/utils/include \ + $(TOP)/hw/mcu/microchip/samd11/hpl/pm/ \ + $(TOP)/hw/mcu/microchip/samd11/hpl/port \ + $(TOP)/hw/mcu/microchip/samd11/hri \ + $(TOP)/hw/mcu/microchip/samd11/CMSIS/Include \ + $(TOP)/hw/mcu/microchip/samd11/CMSIS/Core/Include + +# For TinyUSB port source +VENDOR = microchip +CHIP_FAMILY = samd + +# For freeRTOS port source +FREERTOS_PORT = ARM_CM0 + +# For flash-jlink target +JLINK_DEVICE = ATSAMD11D14 +JLINK_IF = swd + +# flash using edbg +flash: $(BUILD)/$(BOARD)-firmware.bin + edbg -b -t samd11 -e -pv -f $< + diff --git a/hw/bsp/samd11_xplained/samd11_xplained.c b/hw/bsp/samd11_xplained/samd11_xplained.c new file mode 100644 index 000000000..874e36d7c --- /dev/null +++ b/hw/bsp/samd11_xplained/samd11_xplained.c @@ -0,0 +1,152 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#include "bsp/board.h" + +#include "sam.h" +#include "hal/include/hal_gpio.h" +#include "hal/include/hal_init.h" +#include "hri/hri_nvmctrl_d11.h" + +#include "hpl/gclk/hpl_gclk_base.h" +#include "hpl_pm_config.h" +#include "hpl/pm/hpl_pm_base.h" + +//--------------------------------------------------------------------+ +// Forward USB interrupt events to TinyUSB IRQ Handler +//--------------------------------------------------------------------+ +void USB_Handler(void) +{ + tud_int_handler(0); +} + + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ +#define LED_PIN PIN_PA16 // pin PA22 +#define BUTTON_PIN PIN_PA14 // pin PB22 + +/* Referenced GCLKs, should be initialized firstly */ +#define _GCLK_INIT_1ST (1 << 0 | 1 << 1) + +/* Not referenced GCLKs, initialized last */ +#define _GCLK_INIT_LAST (~_GCLK_INIT_1ST) + +void board_init(void) +{ + // Clock init ( follow hpl_init.c ) + hri_nvmctrl_set_CTRLB_RWS_bf(NVMCTRL, 2); + + _pm_init(); + _sysctrl_init_sources(); +#if _GCLK_INIT_1ST + _gclk_init_generators_by_fref(_GCLK_INIT_1ST); +#endif + _sysctrl_init_referenced_generators(); + _gclk_init_generators_by_fref(_GCLK_INIT_LAST); + + // 1ms tick timer (samd SystemCoreClock may not correct) + SystemCoreClock = CONF_CPU_FREQUENCY; + SysTick_Config(CONF_CPU_FREQUENCY / 1000); + + // Led init + gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT); + gpio_set_pin_level(LED_PIN, 0); + + // Button init + gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN); + gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP); + + /* USB Clock init + * The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock + * for low speed and full speed operation. */ + _pm_enable_bus_clock(PM_BUS_APBB, USB); + _pm_enable_bus_clock(PM_BUS_AHB, USB); + _gclk_enable_channel(USB_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val); + + // USB Pin Init + gpio_set_pin_direction(PIN_PA24, GPIO_DIRECTION_OUT); + gpio_set_pin_level(PIN_PA24, false); + gpio_set_pin_pull_mode(PIN_PA24, GPIO_PULL_OFF); + gpio_set_pin_direction(PIN_PA25, GPIO_DIRECTION_OUT); + gpio_set_pin_level(PIN_PA25, false); + gpio_set_pin_pull_mode(PIN_PA25, GPIO_PULL_OFF); + + gpio_set_pin_function(PIN_PA24, PINMUX_PA24G_USB_DM); + gpio_set_pin_function(PIN_PA25, PINMUX_PA25G_USB_DP); +} + +//--------------------------------------------------------------------+ +// Board porting API +//--------------------------------------------------------------------+ + +void board_led_write(bool state) +{ + gpio_set_pin_level(LED_PIN, state); +} + +uint32_t board_button_read(void) +{ + // button is active low + return gpio_get_pin_level(BUTTON_PIN) ? 0 : 1; +} + +int board_uart_read(uint8_t* buf, int len) +{ + (void) buf; (void) len; + return 0; +} + +int board_uart_write(void const * buf, int len) +{ + (void) buf; (void) len; + return 0; +} + +#if CFG_TUSB_OS == OPT_OS_NONE + +volatile uint32_t system_ticks = 0; + +void SysTick_Handler (void) +{ + system_ticks++; +} + +uint32_t board_millis(void) +{ + return system_ticks; +} + +void _init(void) +{ + // This _init() standin makes certain GCC environments happier. + // They expect the main binary to have a constructor called _init; but don't provide a weak default. + // Providing an empty constructor satisfies this odd case, and doesn't harm anything. +} + + +#endif diff --git a/hw/bsp/samd11_xplained/samd11d14am_flash.ld b/hw/bsp/samd11_xplained/samd11d14am_flash.ld new file mode 100644 index 000000000..2153f5f50 --- /dev/null +++ b/hw/bsp/samd11_xplained/samd11d14am_flash.ld @@ -0,0 +1,143 @@ +/** + * \file + * + * \brief Linker script for running in internal FLASH on the SAMD11D14AM + * + * Copyright (c) 2018 Microchip Technology Inc. + * + * \asf_license_start + * + * \page License + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the Licence at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * \asf_license_stop + * + */ + + +OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") +OUTPUT_ARCH(arm) +SEARCH_DIR(.) + +/* Memory Spaces Definitions */ +MEMORY +{ + rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00004000 + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00001000 +} + +/* The stack size used by the application. NOTE: you need to adjust according to your application. */ +STACK_SIZE = DEFINED(STACK_SIZE) ? STACK_SIZE : DEFINED(__stack_size__) ? __stack_size__ : 0x400; + +/* Section Definitions */ +SECTIONS +{ + .text : + { + . = ALIGN(4); + _sfixed = .; + KEEP(*(.vectors .vectors.*)) + *(.text .text.* .gnu.linkonce.t.*) + *(.glue_7t) *(.glue_7) + *(.rodata .rodata* .gnu.linkonce.r.*) + *(.ARM.extab* .gnu.linkonce.armextab.*) + + /* Support C constructors, and C destructors in both user code + and the C library. This also provides support for C++ code. */ + . = ALIGN(4); + KEEP(*(.init)) + . = ALIGN(4); + __preinit_array_start = .; + KEEP (*(.preinit_array)) + __preinit_array_end = .; + + . = ALIGN(4); + __init_array_start = .; + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + __init_array_end = .; + + . = ALIGN(4); + KEEP (*crtbegin.o(.ctors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*crtend.o(.ctors)) + + . = ALIGN(4); + KEEP(*(.fini)) + + . = ALIGN(4); + __fini_array_start = .; + KEEP (*(.fini_array)) + KEEP (*(SORT(.fini_array.*))) + __fini_array_end = .; + + KEEP (*crtbegin.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*crtend.o(.dtors)) + + . = ALIGN(4); + _efixed = .; /* End of text section */ + } > rom + + /* .ARM.exidx is sorted, so has to go in its own output section. */ + PROVIDE_HIDDEN (__exidx_start = .); + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + } > rom + PROVIDE_HIDDEN (__exidx_end = .); + + . = ALIGN(4); + _etext = .; + + .relocate : AT (_etext) + { + . = ALIGN(4); + _srelocate = .; + *(.ramfunc .ramfunc.*); + *(.data .data.*); + . = ALIGN(4); + _erelocate = .; + } > ram + + /* .bss section which is used for uninitialized data */ + .bss (NOLOAD) : + { + . = ALIGN(4); + _sbss = . ; + _szero = .; + *(.bss .bss.*) + *(COMMON) + . = ALIGN(4); + _ebss = . ; + _ezero = .; + } > ram + + /* stack section */ + .stack (NOLOAD): + { + . = ALIGN(8); + _sstack = .; + . = . + STACK_SIZE; + . = ALIGN(8); + _estack = .; + } > ram + + . = ALIGN(4); + _end = . ; +} From 9d3a9cf546580907d0e72fd6e36e23050bcab850 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 22 Aug 2020 18:46:19 +0700 Subject: [PATCH 31/52] add OPT_MCU_SAMD11 skip ci build for example that need more ROM/RAM could fit into SAMD11 --- examples/device/cdc_msc/.skip.MCU_SAMD11 | 0 examples/device/cdc_msc_freertos/.skip.MCU_SAMD11 | 0 examples/device/dynamic_configuration/.skip.MCU_SAMD11 | 0 examples/device/hid_composite_freertos/.skip.MCU_SAMD11 | 0 examples/device/msc_dual_lun/.skip.MCU_SAMD11 | 0 examples/device/net_lwip_webserver/.skip.MCU_SAMD11 | 0 hw/bsp/board_mcu.h | 2 +- hw/bsp/samd11_xplained/board.mk | 2 +- src/portable/microchip/samd/dcd_samd.c | 5 +++-- src/tusb_option.h | 3 ++- 10 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 examples/device/cdc_msc/.skip.MCU_SAMD11 create mode 100644 examples/device/cdc_msc_freertos/.skip.MCU_SAMD11 create mode 100644 examples/device/dynamic_configuration/.skip.MCU_SAMD11 create mode 100644 examples/device/hid_composite_freertos/.skip.MCU_SAMD11 create mode 100644 examples/device/msc_dual_lun/.skip.MCU_SAMD11 create mode 100644 examples/device/net_lwip_webserver/.skip.MCU_SAMD11 diff --git a/examples/device/cdc_msc/.skip.MCU_SAMD11 b/examples/device/cdc_msc/.skip.MCU_SAMD11 new file mode 100644 index 000000000..e69de29bb diff --git a/examples/device/cdc_msc_freertos/.skip.MCU_SAMD11 b/examples/device/cdc_msc_freertos/.skip.MCU_SAMD11 new file mode 100644 index 000000000..e69de29bb diff --git a/examples/device/dynamic_configuration/.skip.MCU_SAMD11 b/examples/device/dynamic_configuration/.skip.MCU_SAMD11 new file mode 100644 index 000000000..e69de29bb diff --git a/examples/device/hid_composite_freertos/.skip.MCU_SAMD11 b/examples/device/hid_composite_freertos/.skip.MCU_SAMD11 new file mode 100644 index 000000000..e69de29bb diff --git a/examples/device/msc_dual_lun/.skip.MCU_SAMD11 b/examples/device/msc_dual_lun/.skip.MCU_SAMD11 new file mode 100644 index 000000000..e69de29bb diff --git a/examples/device/net_lwip_webserver/.skip.MCU_SAMD11 b/examples/device/net_lwip_webserver/.skip.MCU_SAMD11 new file mode 100644 index 000000000..e69de29bb diff --git a/hw/bsp/board_mcu.h b/hw/bsp/board_mcu.h index 15b31edfd..3382d465d 100644 --- a/hw/bsp/board_mcu.h +++ b/hw/bsp/board_mcu.h @@ -52,7 +52,7 @@ #elif CFG_TUSB_MCU == OPT_MCU_NRF5X #include "nrf.h" -#elif CFG_TUSB_MCU == OPT_MCU_SAMD21 || CFG_TUSB_MCU == OPT_MCU_SAMD51 +#elif CFG_TUSB_MCU == OPT_MCU_SAMD11 || CFG_TUSB_MCU == OPT_MCU_SAMD21 || CFG_TUSB_MCU == OPT_MCU_SAMD51 #include "sam.h" #elif CFG_TUSB_MCU == OPT_MCU_SAMG diff --git a/hw/bsp/samd11_xplained/board.mk b/hw/bsp/samd11_xplained/board.mk index db0c62cf1..5b1a42157 100644 --- a/hw/bsp/samd11_xplained/board.mk +++ b/hw/bsp/samd11_xplained/board.mk @@ -9,7 +9,7 @@ CFLAGS += \ -D__SAMD11D14AM__ \ -DCONF_DFLL_OVERWRITE_CALIBRATION=0 \ -DOSC32K_OVERWRITE_CALIBRATION=0 \ - -DCFG_TUSB_MCU=OPT_MCU_SAMD21 \ + -DCFG_TUSB_MCU=OPT_MCU_SAMD11 \ -fshort-enums \ -Os diff --git a/src/portable/microchip/samd/dcd_samd.c b/src/portable/microchip/samd/dcd_samd.c index d0cc8560a..f1dca88a5 100644 --- a/src/portable/microchip/samd/dcd_samd.c +++ b/src/portable/microchip/samd/dcd_samd.c @@ -27,7 +27,8 @@ #include "tusb_option.h" #if TUSB_OPT_DEVICE_ENABLED && \ - (CFG_TUSB_MCU == OPT_MCU_SAMD21 || CFG_TUSB_MCU == OPT_MCU_SAMD51 || CFG_TUSB_MCU == OPT_MCU_SAME5X) + (CFG_TUSB_MCU == OPT_MCU_SAMD11 || CFG_TUSB_MCU == OPT_MCU_SAMD21 || \ + CFG_TUSB_MCU == OPT_MCU_SAMD51 || CFG_TUSB_MCU == OPT_MCU_SAME5X) #include "sam.h" #include "device/dcd.h" @@ -113,7 +114,7 @@ void dcd_int_disable(uint8_t rhport) NVIC_DisableIRQ(USB_0_IRQn); } -#elif CFG_TUSB_MCU == OPT_MCU_SAMD21 +#elif CFG_TUSB_MCU == OPT_MCU_SAMD11 || CFG_TUSB_MCU == OPT_MCU_SAMD21 void dcd_int_enable(uint8_t rhport) { diff --git a/src/tusb_option.h b/src/tusb_option.h index abf6450ca..7e486bff0 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -55,10 +55,11 @@ #define OPT_MCU_NRF5X 100 ///< Nordic nRF5x series // SAM +#define OPT_MCU_SAMD11 204 ///< MicroChip SAMD11 #define OPT_MCU_SAMD21 200 ///< MicroChip SAMD21 #define OPT_MCU_SAMD51 201 ///< MicroChip SAMD51 -#define OPT_MCU_SAMG 202 ///< MicroChip SAMDG series #define OPT_MCU_SAME5X 203 ///< MicroChip SAM E5x +#define OPT_MCU_SAMG 202 ///< MicroChip SAMDG series // STM32 #define OPT_MCU_STM32F0 300 ///< ST STM32F0 From 2b1adbb7e581af46626017de38efcec3b3747da9 Mon Sep 17 00:00:00 2001 From: Ha Thach Date: Sat, 22 Aug 2020 19:13:43 +0700 Subject: [PATCH 32/52] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ca41e0f0..05f3521c4 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The stack supports the following MCUs: - **Espressif:** ESP32-S2 - **Dialog:** DA1469x -- **MicroChip:** SAMD11, SAMD21, SAMD51, SAME5x (device only) +- **MicroChip:** SAMD11, SAMD21, SAMD51, SAME5x, SAMG (device only) - **NordicSemi:** nRF52833, nRF52840 - **Nuvoton:** NUC120, NUC121/NUC125, NUC126, NUC505 - **NXP:** From a1b7e767af7c4916e9b004120715988268079d28 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 24 Aug 2020 14:31:46 +0700 Subject: [PATCH 33/52] improve midi - fix #436 tud_midi_rx_cb() not invoked - fix xfer_cb() not handle ep in - add ZLP if needed --- src/class/cdc/cdc_device.c | 7 ++--- src/class/cdc/cdc_device.h | 1 - src/class/midi/midi_device.c | 51 +++++++++++++++++++++--------------- src/class/midi/midi_device.h | 2 +- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 94473d036..b4ebfc92e 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -164,7 +164,7 @@ uint32_t tud_cdc_n_write_flush (uint8_t itf) // skip if previous transfer not complete yet TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in), 0 ); - uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf)); + uint16_t count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf)); if ( count ) { TU_VERIFY( tud_cdc_n_connected(itf), 0 ); // fifo is empty if not connected @@ -376,7 +376,6 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { - (void) rhport; (void) result; uint8_t itf; @@ -393,6 +392,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Received new data if ( ep_addr == p_cdc->ep_out ) { + // TODO search for wanted char first for better performance for(uint32_t i=0; irx_ff, &p_cdc->epout_buf[i]); @@ -423,9 +423,10 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ { // There is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero + // FIXME CFG_TUD_CDC_EP_BUFSIZE is not Endpoint packet size if ( xferred_bytes && (0 == (xferred_bytes % CFG_TUD_CDC_EP_BUFSIZE)) ) { - usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, NULL, 0); + usbd_edpt_xfer(rhport, p_cdc->ep_in, NULL, 0); } } } diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index f9692e20f..145381c42 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -57,7 +57,6 @@ // CFG_TUD_CDC > 1 //--------------------------------------------------------------------+ - // Check if terminal is connected to this port bool tud_cdc_n_connected (uint8_t itf); diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index d4ed21c78..71a24cbcd 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -101,8 +101,7 @@ uint32_t tud_midi_n_read(uint8_t itf, uint8_t jack_id, void* buffer, uint32_t bu // Fill empty buffer if (midi->read_buffer_length == 0) { - if (!tud_midi_n_receive(itf, midi->read_buffer)) - return 0; + if (!tud_midi_n_receive(itf, midi->read_buffer)) return 0; uint8_t code_index = midi->read_buffer[0] & 0x0f; // We always copy over the first byte. @@ -119,8 +118,7 @@ uint32_t tud_midi_n_read(uint8_t itf, uint8_t jack_id, void* buffer, uint32_t bu } uint32_t n = midi->read_buffer_length - midi->read_target_length; - if (bufsize < n) - n = bufsize; + if (bufsize < n) n = bufsize; // Skip the header in the buffer memcpy(buffer, midi->read_buffer + 1 + midi->read_target_length, n); @@ -153,10 +151,8 @@ void midi_rx_done_cb(midid_interface_t* midi, uint8_t const* buffer, uint32_t bu // WRITE API //--------------------------------------------------------------------+ -static bool maybe_transmit(midid_interface_t* midi, uint8_t itf_index) +static uint32_t write_flush(midid_interface_t* midi) { - (void) itf_index; - // skip if previous transfer not complete TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, midi->ep_in) ); @@ -165,7 +161,7 @@ static bool maybe_transmit(midid_interface_t* midi, uint8_t itf_index) { TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, midi->ep_in, midi->epin_buf, count) ); } - return true; + return count; } uint32_t tud_midi_n_write(uint8_t itf, uint8_t jack_id, uint8_t const* buffer, uint32_t bufsize) @@ -234,7 +230,8 @@ uint32_t tud_midi_n_write(uint8_t itf, uint8_t jack_id, uint8_t const* buffer, u } i++; } - maybe_transmit(midi, itf); + + write_flush(midi); return i; } @@ -250,7 +247,7 @@ bool tud_midi_n_send (uint8_t itf, uint8_t const packet[4]) return false; tu_fifo_write_n(&midi->tx_ff, packet, 4); - maybe_transmit(midi, itf); + write_flush(midi); return true; } @@ -389,27 +386,39 @@ bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32 (void) result; uint8_t itf = 0; - midid_interface_t* p_midi = _midid_itf; + midid_interface_t* p_midi; - for ( ; ; itf++, p_midi++) + // Identify which interface to use + for (itf = 0; itf < CFG_TUD_MIDI; itf++) { - if (itf >= TU_ARRAY_SIZE(_midid_itf)) return false; - - if ( ep_addr == p_midi->ep_out ) break; + p_midi = &_midid_itf[itf]; + if ( ( ep_addr == p_midi->ep_out ) || ( ep_addr == p_midi->ep_in ) ) break; } + TU_ASSERT(itf < CFG_TUD_CDC); // receive new data if ( ep_addr == p_midi->ep_out ) { - midi_rx_done_cb(p_midi, p_midi->epout_buf, xferred_bytes); + tu_fifo_write_n(&p_midi->rx_ff, p_midi->epout_buf, xferred_bytes); + + // invoke receive callback if available + if (tud_midi_rx_cb) tud_midi_rx_cb(itf); // prepare for next - TU_ASSERT( usbd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, CFG_TUD_MIDI_EP_BUFSIZE), false ); - } else if ( ep_addr == p_midi->ep_in ) { - maybe_transmit(p_midi, itf); + TU_ASSERT(usbd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, CFG_TUD_MIDI_EP_BUFSIZE), false); + } + else if ( ep_addr == p_midi->ep_in ) + { + if (0 == write_flush(p_midi)) + { + // There is no data left, a ZLP should be sent if + // xferred_bytes is multiple of EP size and not zero + if ( xferred_bytes && (0 == (xferred_bytes % CFG_TUD_MIDI_EP_BUFSIZE)) ) + { + usbd_edpt_xfer(rhport, p_midi->ep_in, NULL, 0); + } + } } - - // nothing to do with in and notif endpoint return true; } diff --git a/src/class/midi/midi_device.h b/src/class/midi/midi_device.h index 1828a21a3..b8fb55cc2 100644 --- a/src/class/midi/midi_device.h +++ b/src/class/midi/midi_device.h @@ -72,7 +72,7 @@ bool tud_midi_n_receive (uint8_t itf, uint8_t packet[4]); bool tud_midi_n_send (uint8_t itf, uint8_t const packet[4]); //--------------------------------------------------------------------+ -// Application API (Interface0) +// Application API (Single Interface) //--------------------------------------------------------------------+ static inline bool tud_midi_mounted (void); static inline uint32_t tud_midi_available (void); From 2d8787cdeb649f6b4e3c34ea2bd7015f0f120861 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 24 Aug 2020 15:29:34 +0700 Subject: [PATCH 34/52] fix typo --- src/class/midi/midi_device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index 71a24cbcd..376b3670a 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -385,7 +385,7 @@ bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32 { (void) result; - uint8_t itf = 0; + uint8_t itf; midid_interface_t* p_midi; // Identify which interface to use @@ -394,7 +394,7 @@ bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32 p_midi = &_midid_itf[itf]; if ( ( ep_addr == p_midi->ep_out ) || ( ep_addr == p_midi->ep_in ) ) break; } - TU_ASSERT(itf < CFG_TUD_CDC); + TU_ASSERT(itf < CFG_TUD_MIDI); // receive new data if ( ep_addr == p_midi->ep_out ) From 7a443d6beafd0b5f3a9ef8cfdcdfb68bfece1170 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 27 Aug 2020 16:40:24 +0700 Subject: [PATCH 35/52] add itsybitsy nrf52840 board support --- docs/boards.md | 1 + hw/bsp/itsybitsy_nrf52840/board.mk | 64 +++++ .../itsybitsy_nrf52840/itsybitsy_nrf52840.c | 225 ++++++++++++++++++ hw/bsp/itsybitsy_nrf52840/nrf52840_s140_v6.ld | 38 +++ 4 files changed, 328 insertions(+) create mode 100644 hw/bsp/itsybitsy_nrf52840/board.mk create mode 100644 hw/bsp/itsybitsy_nrf52840/itsybitsy_nrf52840.c create mode 100644 hw/bsp/itsybitsy_nrf52840/nrf52840_s140_v6.ld diff --git a/docs/boards.md b/docs/boards.md index 381d124cc..1b3de29e9 100644 --- a/docs/boards.md +++ b/docs/boards.md @@ -38,6 +38,7 @@ This code base already had supported for a handful of following boards (sorted a - [Adafruit CLUE](https://www.adafruit.com/product/4500) - [Adafruit Feather nRF52840 Express](https://www.adafruit.com/product/4062) - [Adafruit Feather nRF52840 Sense](https://www.adafruit.com/product/4516) +- [Adafruit ItsyBitsy nRF52840 Express](https://www.adafruit.com/product/4481) - [Arduino Nano 33 BLE](https://store.arduino.cc/usa/nano-33-ble) - [Arduino Nano 33 BLE Sense](https://store.arduino.cc/usa/nano-33-ble-sense) - [Maker Diary nRF52840 MDK Dongle](https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle) diff --git a/hw/bsp/itsybitsy_nrf52840/board.mk b/hw/bsp/itsybitsy_nrf52840/board.mk new file mode 100644 index 000000000..7b2625cd4 --- /dev/null +++ b/hw/bsp/itsybitsy_nrf52840/board.mk @@ -0,0 +1,64 @@ +CFLAGS += \ + -flto \ + -mthumb \ + -mabi=aapcs \ + -mcpu=cortex-m4 \ + -mfloat-abi=hard \ + -mfpu=fpv4-sp-d16 \ + -DCFG_TUSB_MCU=OPT_MCU_NRF5X \ + -DNRF52840_XXAA \ + -DCONFIG_GPIO_AS_PINRESET + +# suppress warning caused by vendor mcu driver +CFLAGS += -Wno-error=undef -Wno-error=unused-parameter -Wno-error=cast-align + +# due to tusb_hal_nrf_power_event +GCCVERSION = $(firstword $(subst ., ,$(shell arm-none-eabi-gcc -dumpversion))) +ifeq ($(shell expr $(GCCVERSION) \>= 8), 1) +CFLAGS += -Wno-error=cast-function-type +endif + +# All source paths should be relative to the top level. +LD_FILE = hw/bsp/$(BOARD)/nrf52840_s140_v6.ld + +LDFLAGS += -L$(TOP)/hw/mcu/nordic/nrfx/mdk + +SRC_C += \ + hw/mcu/nordic/nrfx/drivers/src/nrfx_power.c \ + hw/mcu/nordic/nrfx/drivers/src/nrfx_uarte.c \ + hw/mcu/nordic/nrfx/mdk/system_nrf52840.c + +INC += \ + $(TOP)/lib/CMSIS_4/CMSIS/Include \ + $(TOP)/hw/mcu/nordic \ + $(TOP)/hw/mcu/nordic/nrfx \ + $(TOP)/hw/mcu/nordic/nrfx/mdk \ + $(TOP)/hw/mcu/nordic/nrfx/hal \ + $(TOP)/hw/mcu/nordic/nrfx/drivers/include \ + $(TOP)/hw/mcu/nordic/nrfx/drivers/src \ + +SRC_S += hw/mcu/nordic/nrfx/mdk/gcc_startup_nrf52840.S + +ASFLAGS += -D__HEAP_SIZE=0 + +# For TinyUSB port source +VENDOR = nordic +CHIP_FAMILY = nrf5x + +# For freeRTOS port source +FREERTOS_PORT = ARM_CM4F + +# For flash-jlink target +JLINK_DEVICE = nRF52840_xxAA +JLINK_IF = swd + +# For uf2 conversion +UF2_FAMILY = 0xADA52840 + +$(BUILD)/$(BOARD)-firmware.zip: $(BUILD)/$(BOARD)-firmware.hex + adafruit-nrfutil dfu genpkg --dev-type 0x0052 --sd-req 0xFFFE --application $^ $@ + +# flash using adafruit-nrfutil dfu +flash: $(BUILD)/$(BOARD)-firmware.zip + @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0) + adafruit-nrfutil --verbose dfu serial --package $^ -p $(SERIAL) -b 115200 --singlebank --touch 1200 diff --git a/hw/bsp/itsybitsy_nrf52840/itsybitsy_nrf52840.c b/hw/bsp/itsybitsy_nrf52840/itsybitsy_nrf52840.c new file mode 100644 index 000000000..e9e5a3876 --- /dev/null +++ b/hw/bsp/itsybitsy_nrf52840/itsybitsy_nrf52840.c @@ -0,0 +1,225 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#include "bsp/board.h" + +#include "nrfx.h" +#include "nrfx/hal/nrf_gpio.h" +#include "nrfx/drivers/include/nrfx_power.h" +#include "nrfx/drivers/include/nrfx_uarte.h" + +#ifdef SOFTDEVICE_PRESENT +#include "nrf_sdm.h" +#include "nrf_soc.h" +#endif + +//--------------------------------------------------------------------+ +// Forward USB interrupt events to TinyUSB IRQ Handler +//--------------------------------------------------------------------+ +void USBD_IRQHandler(void) +{ + tud_int_handler(0); +} + +/*------------------------------------------------------------------*/ +/* MACRO TYPEDEF CONSTANT ENUM + *------------------------------------------------------------------*/ +#define _PINNUM(port, pin) ((port)*32 + (pin)) + +#define LED_PIN _PINNUM(0, 6) +#define LED_STATE_ON 1 + +#define BUTTON_PIN _PINNUM(0, 29) + +#define UART_RX_PIN 25 +#define UART_TX_PIN 24 + +static nrfx_uarte_t _uart_id = NRFX_UARTE_INSTANCE(0); + +// tinyusb function that handles power event (detected, ready, removed) +// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. +extern void tusb_hal_nrf_power_event(uint32_t event); + +void board_init(void) +{ + // stop LF clock just in case we jump from application without reset + NRF_CLOCK->TASKS_LFCLKSTOP = 1UL; + + // Config clock source: XTAL or RC in sdk_config.h + NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); + NRF_CLOCK->TASKS_LFCLKSTART = 1UL; + + // LED + nrf_gpio_cfg_output(LED_PIN); + board_led_write(false); + + // Button + nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP); + + // 1ms tick timer + SysTick_Config(SystemCoreClock/1000); + + // UART + nrfx_uarte_config_t uart_cfg = + { + .pseltxd = UART_TX_PIN, + .pselrxd = UART_RX_PIN, + .pselcts = NRF_UARTE_PSEL_DISCONNECTED, + .pselrts = NRF_UARTE_PSEL_DISCONNECTED, + .p_context = NULL, + .baudrate = NRF_UARTE_BAUDRATE_115200, // CFG_BOARD_UART_BAUDRATE + .interrupt_priority = 7, + .hal_cfg = { + .hwfc = NRF_UARTE_HWFC_DISABLED, + .parity = NRF_UARTE_PARITY_EXCLUDED, + } + }; + + nrfx_uarte_init(&_uart_id, &uart_cfg, NULL); //uart_handler); + + //------------- USB -------------// +#if TUSB_OPT_DEVICE_ENABLED + // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice + // 2 is highest for application + NVIC_SetPriority(USBD_IRQn, 2); + + // USB power may already be ready at this time -> no event generated + // We need to invoke the handler based on the status initially + uint32_t usb_reg; + +#ifdef SOFTDEVICE_PRESENT + uint8_t sd_en = false; + sd_softdevice_is_enabled(&sd_en); + + if ( sd_en ) { + sd_power_usbdetected_enable(true); + sd_power_usbpwrrdy_enable(true); + sd_power_usbremoved_enable(true); + + sd_power_usbregstatus_get(&usb_reg); + }else +#endif + { + // Power module init + const nrfx_power_config_t pwr_cfg = { 0 }; + nrfx_power_init(&pwr_cfg); + + // Register tusb function as USB power handler + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; + nrfx_power_usbevt_init(&config); + + nrfx_power_usbevt_enable(); + + usb_reg = NRF_POWER->USBREGSTATUS; + } + + if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); + if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); +#endif +} + +//--------------------------------------------------------------------+ +// Board porting API +//--------------------------------------------------------------------+ + +void board_led_write(bool state) +{ + nrf_gpio_pin_write(LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); +} + +uint32_t board_button_read(void) +{ + // button is active LOW + return (nrf_gpio_pin_read(BUTTON_PIN) ? 0 : 1); +} + +int board_uart_read(uint8_t* buf, int len) +{ + (void) buf; (void) len; + return 0; +// return NRFX_SUCCESS == nrfx_uart_rx(&_uart_id, buf, (size_t) len) ? len : 0; +} + +int board_uart_write(void const * buf, int len) +{ + return (NRFX_SUCCESS == nrfx_uarte_tx(&_uart_id, (uint8_t const*) buf, (size_t) len)) ? len : 0; +} + +#if CFG_TUSB_OS == OPT_OS_NONE +volatile uint32_t system_ticks = 0; +void SysTick_Handler (void) +{ + system_ticks++; +} + +uint32_t board_millis(void) +{ + return system_ticks; +} +#endif + +#ifdef SOFTDEVICE_PRESENT +// process SOC event from SD +uint32_t proc_soc(void) +{ + uint32_t soc_evt; + uint32_t err = sd_evt_get(&soc_evt); + + if (NRF_SUCCESS == err) + { + /*------------- usb power event handler -------------*/ + int32_t usbevt = (soc_evt == NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED: + (soc_evt == NRF_EVT_POWER_USB_POWER_READY) ? NRFX_POWER_USB_EVT_READY : + (soc_evt == NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : -1; + + if ( usbevt >= 0) tusb_hal_nrf_power_event(usbevt); + } + + return err; +} + +uint32_t proc_ble(void) +{ + // do nothing with ble + return NRF_ERROR_NOT_FOUND; +} + +void SD_EVT_IRQHandler(void) +{ + // process BLE and SOC until there is no more events + while( (NRF_ERROR_NOT_FOUND != proc_ble()) || (NRF_ERROR_NOT_FOUND != proc_soc()) ) + { + + } +} + +void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info) +{ + (void) id; + (void) pc; + (void) info; +} +#endif diff --git a/hw/bsp/itsybitsy_nrf52840/nrf52840_s140_v6.ld b/hw/bsp/itsybitsy_nrf52840/nrf52840_s140_v6.ld new file mode 100644 index 000000000..5314a4e93 --- /dev/null +++ b/hw/bsp/itsybitsy_nrf52840/nrf52840_s140_v6.ld @@ -0,0 +1,38 @@ +/* Linker script to configure memory regions. */ + +SEARCH_DIR(.) +GROUP(-lgcc -lc -lnosys) + +MEMORY +{ + FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xED000 - 0x26000 + + /* SRAM required by S132 depend on + * - Attribute Table Size + * - Vendor UUID count + * - Max ATT MTU + * - Concurrent connection peripheral + central + secure links + * - Event Len, HVN queue, Write CMD queue + */ + RAM (rwx) : ORIGIN = 0x20003400, LENGTH = 0x20040000 - 0x20003400 +} + +SECTIONS +{ + . = ALIGN(4); + .svc_data : + { + PROVIDE(__start_svc_data = .); + KEEP(*(.svc_data)) + PROVIDE(__stop_svc_data = .); + } > RAM + + .fs_data : + { + PROVIDE(__start_fs_data = .); + KEEP(*(.fs_data)) + PROVIDE(__stop_fs_data = .); + } > RAM +} INSERT AFTER .data; + +INCLUDE "nrf52_common.ld" From f4e3c6fd8ee125123e2cfa704d61dab34be576c5 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 1 Sep 2020 17:12:31 +0700 Subject: [PATCH 36/52] clean up --- examples/host/cdc_msc_hid/src/main.c | 16 ++++++++-------- hw/bsp/mcb1800/board.mk | 4 ++-- test/test/support/tusb_config.h | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 273c95c77..9ec213090 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -75,7 +75,7 @@ CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 }; void tuh_mount_cb(uint8_t dev_addr) { // application set-up - printf("\na device with address %d is mounted\n", dev_addr); + printf("A device with address %d is mounted\r\n", dev_addr); tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer } @@ -83,7 +83,7 @@ void tuh_mount_cb(uint8_t dev_addr) void tuh_umount_cb(uint8_t dev_addr) { // application tear-down - printf("\na device with address %d is unmounted \n", dev_addr); + printf("A device with address %d is unmounted \r\n", dev_addr); } // invoked ISR context @@ -187,14 +187,14 @@ void print_greeting(void) [OPT_OS_FREERTOS] = "FreeRTOS", }; - printf("\n--------------------------------------------------------------------\n"); - printf("- Host example\n"); - printf("- if you find any bugs or get any questions, feel free to file an\n"); - printf("- issue at https://github.com/hathach/tinyusb\n"); - printf("--------------------------------------------------------------------\n\n"); + printf("--------------------------------------------------------------------\r\n"); + printf("- Host example\r\n"); + printf("- if you find any bugs or get any questions, feel free to file an\r\n"); + printf("- issue at https://github.com/hathach/tinyusb\r\n"); + printf("--------------------------------------------------------------------\r\n\r\n"); printf("This Host demo is configured to support:\r\n"); - printf(" - RTOS = %s\n", rtos_name[CFG_TUSB_OS]); + printf(" - RTOS = %s\r\n", rtos_name[CFG_TUSB_OS]); #if CFG_TUH_CDC printf(" - Communication Device Class\r\n"); diff --git a/hw/bsp/mcb1800/board.mk b/hw/bsp/mcb1800/board.mk index 6037310a6..7ff17dae3 100644 --- a/hw/bsp/mcb1800/board.mk +++ b/hw/bsp/mcb1800/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_LPCOPEN + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC18XX \ # mcu driver cause following warnings @@ -37,7 +37,7 @@ FREERTOS_PORT = ARM_CM3 # For flash-jlink target JLINK_DEVICE = LPC1857 -JLINK_IF = jtag +JLINK_IF = swd # flash using jlink flash: flash-jlink diff --git a/test/test/support/tusb_config.h b/test/test/support/tusb_config.h index 80ea0a1ea..a18d86598 100644 --- a/test/test/support/tusb_config.h +++ b/test/test/support/tusb_config.h @@ -43,7 +43,7 @@ #define CFG_TUSB_MCU OPT_MCU_NRF5X #endif -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) #define CFG_TUSB_OS OPT_OS_NONE // CFG_TUSB_DEBUG is defined by compiler in DEBUG build From 84425c50b3bb5c892bfab9d094cc3da6a9c04da5 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 1 Sep 2020 19:16:50 +0700 Subject: [PATCH 37/52] add more logging to host stack tested host with lpc4357, don't use fpu with lpc m4 since it seems to cause hardfault (stack does not make use of fpu anyway). --- hw/bsp/ea4357/board.mk | 6 ++---- hw/bsp/mcb1800/board.mk | 2 +- hw/bsp/ngx4330/board.mk | 2 -- src/class/msc/msc_host.c | 1 + src/host/usbh.c | 44 +++++++++++++++++++++++++++++++--------- src/host/usbh.h | 4 ++++ src/osal/osal.h | 4 ++-- 7 files changed, 44 insertions(+), 19 deletions(-) diff --git a/hw/bsp/ea4357/board.mk b/hw/bsp/ea4357/board.mk index 2ee6b695c..c77ee4536 100644 --- a/hw/bsp/ea4357/board.mk +++ b/hw/bsp/ea4357/board.mk @@ -3,8 +3,6 @@ CFLAGS += \ -mthumb \ -mabi=aapcs \ -mcpu=cortex-m4 \ - -mfloat-abi=hard \ - -mfpu=fpv4-sp-d16 \ -nostdlib \ -D__USE_LPCOPEN \ -DCORE_M4 \ @@ -40,8 +38,8 @@ CHIP_FAMILY = transdimension FREERTOS_PORT = ARM_CM4F # For flash-jlink target -JLINK_DEVICE = LPC4357 -JLINK_IF = jtag +JLINK_DEVICE = LPC4357_M4 +JLINK_IF = swd # flash using jlink flash: flash-jlink diff --git a/hw/bsp/mcb1800/board.mk b/hw/bsp/mcb1800/board.mk index 7ff17dae3..e3f07d0d0 100644 --- a/hw/bsp/mcb1800/board.mk +++ b/hw/bsp/mcb1800/board.mk @@ -6,7 +6,7 @@ CFLAGS += \ -nostdlib \ -DCORE_M3 \ -D__USE_LPCOPEN \ - -DCFG_TUSB_MCU=OPT_MCU_LPC18XX \ + -DCFG_TUSB_MCU=OPT_MCU_LPC18XX # mcu driver cause following warnings CFLAGS += -Wno-error=unused-parameter -Wno-error=strict-prototypes diff --git a/hw/bsp/ngx4330/board.mk b/hw/bsp/ngx4330/board.mk index 97c24b40e..df9df63a1 100644 --- a/hw/bsp/ngx4330/board.mk +++ b/hw/bsp/ngx4330/board.mk @@ -3,8 +3,6 @@ CFLAGS += \ -mthumb \ -mabi=aapcs \ -mcpu=cortex-m4 \ - -mfloat-abi=hard \ - -mfpu=fpv4-sp-d16 \ -nostdlib \ -DCORE_M4 \ -DCFG_TUSB_MCU=OPT_MCU_LPC43XX \ diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index 98c71eb70..3aa697b17 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -310,6 +310,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it (*p_length) += sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); //------------- Get Max Lun -------------// + TU_LOG2("MSC Get Max Lun\r\n"); tusb_control_request_t request = { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_IN }, .bRequest = MSC_REQ_GET_MAX_LUN, diff --git a/src/host/usbh.c b/src/host/usbh.c index 8536cec0a..c5f28cf0e 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -42,10 +42,17 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ +#if CFG_TUSB_DEBUG >= 2 + #define DRIVER_NAME(_name) .name = _name, +#else + #define DRIVER_NAME(_name) +#endif + static host_class_driver_t const usbh_class_drivers[] = { #if CFG_TUH_CDC { + DRIVER_NAME("CDC") .class_code = TUSB_CLASS_CDC, .init = cdch_init, .open = cdch_open, @@ -56,6 +63,7 @@ static host_class_driver_t const usbh_class_drivers[] = #if CFG_TUH_MSC { + DRIVER_NAME("MSC") .class_code = TUSB_CLASS_MSC, .init = msch_init, .open = msch_open, @@ -66,6 +74,7 @@ static host_class_driver_t const usbh_class_drivers[] = #if HOST_CLASS_HID { + DRIVER_NAME("HID") .class_code = TUSB_CLASS_HID, .init = hidh_init, .open = hidh_open_subtask, @@ -76,6 +85,7 @@ static host_class_driver_t const usbh_class_drivers[] = #if CFG_TUH_HUB { + DRIVER_NAME("HUB") .class_code = TUSB_CLASS_HUB, .init = hub_init, .open = hub_open, @@ -86,6 +96,7 @@ static host_class_driver_t const usbh_class_drivers[] = #if CFG_TUH_VENDOR { + DRIVER_NAME("VENDOR") .class_code = TUSB_CLASS_VENDOR_SPECIFIC, .init = cush_init, .open = cush_open_subtask, @@ -131,7 +142,7 @@ static inline void osal_task_delay(uint32_t msec) { (void) msec; - uint32_t start = hcd_frame_number(TUH_OPT_RHPORT); + const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT); while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {} } @@ -162,7 +173,11 @@ bool usbh_init(void) } // Class drivers init - for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) usbh_class_drivers[drv_id].init(); + for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) + { + TU_LOG2("%s init\r\n", usbh_class_drivers[drv_id].name); + usbh_class_drivers[drv_id].init(); + } TU_ASSERT(hcd_init()); hcd_int_enable(TUH_OPT_RHPORT); @@ -269,6 +284,7 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t ev if (usbh_class_drivers[drv_id].isr) { + TU_LOG2("%s isr\r\n", usbh_class_drivers[drv_id].name); usbh_class_drivers[drv_id].isr(dev_addr, ep_addr, event, xferred_bytes); } else @@ -336,7 +352,11 @@ static void usbh_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_ if (tuh_umount_cb) tuh_umount_cb(dev_addr); // Close class driver - for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) usbh_class_drivers[drv_id].close(dev_addr); + for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) + { + TU_LOG2("%s close\r\n", usbh_class_drivers[drv_id].name); + usbh_class_drivers[drv_id].close(dev_addr); + } memset(dev->itf2drv, 0xff, sizeof(dev->itf2drv)); // invalid mapping memset(dev->ep2drv , 0xff, sizeof(dev->ep2drv )); // invalid mapping @@ -381,7 +401,7 @@ bool enum_task(hcd_event_t* event) { if( hcd_port_connect_status(dev0->rhport) ) { - TU_LOG2("Connect \r\n"); + TU_LOG2("Device connect \r\n"); // connection event osal_task_delay(POWER_STABLE_DELAY); // wait until device is stable. Increase this if the first 8 bytes is failed to get @@ -396,7 +416,7 @@ bool enum_task(hcd_event_t* event) } else { - TU_LOG2("Disconnect \r\n"); + TU_LOG2("Device disconnect \r\n"); // disconnection event usbh_device_unplugged(dev0->rhport, 0, 0); @@ -451,7 +471,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT_ERR( usbh_pipe_control_open(0, 8) ); //------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------// - TU_LOG2("Get 8 byte of Device Descriptor \r\n"); + TU_LOG2("Get 8 byte of Device Descriptor\r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN }, .bRequest = TUSB_REQ_GET_DESCRIPTOR, @@ -536,7 +556,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT(configure_selected <= new_dev->configure_count); // TODO notify application when invalid configuration //------------- Get 9 bytes of configuration descriptor -------------// - TU_LOG2("Get 9 bytes of Configuration Descriptor \r\n"); + TU_LOG2("Get 9 bytes of Configuration Descriptor\r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN }, .bRequest = TUSB_REQ_GET_DESCRIPTOR, @@ -550,7 +570,7 @@ bool enum_task(hcd_event_t* event) TU_ASSERT( CFG_TUSB_HOST_ENUM_BUFFER_SIZE >= ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength ); //------------- Get full configuration descriptor -------------// - TU_LOG2("Get full Configuration Descriptor \r\n"); + TU_LOG2("Get full Configuration Descriptor\r\n"); request.wLength = ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength; // full length TU_ASSERT( usbh_control_xfer( new_addr, &request, _usbh_ctrl_buf ) ); @@ -558,7 +578,7 @@ bool enum_task(hcd_event_t* event) new_dev->interface_count = ((tusb_desc_configuration_t*) _usbh_ctrl_buf)->bNumInterfaces; //------------- Set Configure -------------// - TU_LOG2("Set Configuration Descriptor \r\n"); + TU_LOG2("Set Configuration Descriptor\r\n"); request = (tusb_control_request_t ) { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_OUT }, .bRequest = TUSB_REQ_SET_CONFIGURATION, @@ -568,6 +588,7 @@ bool enum_task(hcd_event_t* event) }; TU_ASSERT(usbh_control_xfer( new_addr, &request, NULL )); + TU_LOG2("Device configured\r\n"); new_dev->state = TUSB_DEVICE_STATE_CONFIGURED; //------------- TODO Get String Descriptors -------------// @@ -575,6 +596,8 @@ bool enum_task(hcd_event_t* event) //------------- parse configuration & install drivers -------------// uint8_t const* p_desc = _usbh_ctrl_buf + sizeof(tusb_desc_configuration_t); + TU_LOG2_MEM(_usbh_ctrl_buf, ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength, 0); + // parse each interfaces while( p_desc < _usbh_ctrl_buf + ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength ) { @@ -584,7 +607,7 @@ bool enum_task(hcd_event_t* event) p_desc = tu_desc_next(p_desc); // skip the descriptor, increase by the descriptor's length }else { - tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc; + tusb_desc_interface_t const* desc_itf = (tusb_desc_interface_t const*) p_desc; // Check if class is supported uint8_t drv_id; @@ -614,6 +637,7 @@ bool enum_task(hcd_event_t* event) { uint16_t itf_len = 0; + TU_LOG2("%s open\r\n", usbh_class_drivers[drv_id].name); TU_ASSERT( usbh_class_drivers[drv_id].open(new_dev->rhport, new_addr, desc_itf, &itf_len) ); TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); p_desc += itf_len; diff --git a/src/host/usbh.h b/src/host/usbh.h index cc112d756..39e7a4e0d 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -52,6 +52,10 @@ typedef enum tusb_interface_status_{ } tusb_interface_status_t; typedef struct { + #if CFG_TUSB_DEBUG >= 2 + char const* name; + #endif + uint8_t class_code; void (* const init) (void); diff --git a/src/osal/osal.h b/src/osal/osal.h index c61f0c912..b5057ff4c 100644 --- a/src/osal/osal.h +++ b/src/osal/osal.h @@ -37,9 +37,9 @@ #include "common/tusb_common.h" // Return immediately -#define OSAL_TIMEOUT_NOTIMEOUT (0) +#define OSAL_TIMEOUT_NOTIMEOUT (0) // Default timeout -#define OSAL_TIMEOUT_NORMAL (10) +#define OSAL_TIMEOUT_NORMAL (10) // Wait forever #define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX) From f17d6f15e0a63c74376e029822193cbac3e45d7e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 1 Sep 2020 23:38:46 +0700 Subject: [PATCH 38/52] fix unit test --- test/test/support/tusb_config.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test/support/tusb_config.h b/test/test/support/tusb_config.h index a18d86598..8cd99eaae 100644 --- a/test/test/support/tusb_config.h +++ b/test/test/support/tusb_config.h @@ -43,7 +43,10 @@ #define CFG_TUSB_MCU OPT_MCU_NRF5X #endif -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) +#ifndef CFG_TUSB_RHPORT0_MODE +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) +#endif + #define CFG_TUSB_OS OPT_OS_NONE // CFG_TUSB_DEBUG is defined by compiler in DEBUG build From c492aef4c7b4dd6ee7b24c30176a570ae6eae043 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Sep 2020 00:46:08 +0700 Subject: [PATCH 39/52] revert to use float-abi define __USE_CMSIS instead of __USE_LPCOPEN will have startup enable FPU on startup properly. Although it is only relevant to lpx43/40 series, change all to __USE_CMSIS for consistency --- hw/bsp/ea4088qs/board.mk | 4 ++-- hw/bsp/ea4357/board.mk | 4 +++- hw/bsp/lpcxpresso11u37/board.mk | 2 +- hw/bsp/lpcxpresso11u68/board.mk | 2 +- hw/bsp/lpcxpresso1347/board.mk | 2 +- hw/bsp/lpcxpresso1549/board.mk | 2 +- hw/bsp/lpcxpresso1769/board.mk | 2 +- hw/bsp/mbed1768/board.mk | 2 +- hw/bsp/mcb1800/board.mk | 2 +- hw/bsp/ngx4330/board.mk | 6 ++++-- src/host/usbh.c | 2 +- 11 files changed, 17 insertions(+), 13 deletions(-) diff --git a/hw/bsp/ea4088qs/board.mk b/hw/bsp/ea4088qs/board.mk index 950aa484c..2a63d8af2 100644 --- a/hw/bsp/ea4088qs/board.mk +++ b/hw/bsp/ea4088qs/board.mk @@ -5,9 +5,9 @@ CFLAGS += \ -mcpu=cortex-m4 \ -nostdlib \ -DCORE_M4 \ - -DCFG_TUSB_MCU=OPT_MCU_LPC40XX \ + -D__USE_CMSIS \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM2")))' \ - -D__USE_LPCOPEN + -DCFG_TUSB_MCU=OPT_MCU_LPC40XX # mcu driver cause following warnings CFLAGS += -Wno-error=strict-prototypes -Wno-error=unused-parameter diff --git a/hw/bsp/ea4357/board.mk b/hw/bsp/ea4357/board.mk index c77ee4536..44481118d 100644 --- a/hw/bsp/ea4357/board.mk +++ b/hw/bsp/ea4357/board.mk @@ -3,9 +3,11 @@ CFLAGS += \ -mthumb \ -mabi=aapcs \ -mcpu=cortex-m4 \ + -mfloat-abi=hard \ + -mfpu=fpv4-sp-d16 \ -nostdlib \ - -D__USE_LPCOPEN \ -DCORE_M4 \ + -D__USE_CMSIS \ -DCFG_TUSB_MCU=OPT_MCU_LPC43XX # mcu driver cause following warnings diff --git a/hw/bsp/lpcxpresso11u37/board.mk b/hw/bsp/lpcxpresso11u37/board.mk index d1c48a2e5..27019ff8b 100644 --- a/hw/bsp/lpcxpresso11u37/board.mk +++ b/hw/bsp/lpcxpresso11u37/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m0 \ -nostdlib \ -DCORE_M0 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_EXAMPLE_MSC_READONLY \ -DCFG_TUSB_MCU=OPT_MCU_LPC11UXX \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM2")))' \ diff --git a/hw/bsp/lpcxpresso11u68/board.mk b/hw/bsp/lpcxpresso11u68/board.mk index d9017ae1a..1c1660367 100644 --- a/hw/bsp/lpcxpresso11u68/board.mk +++ b/hw/bsp/lpcxpresso11u68/board.mk @@ -6,7 +6,7 @@ CFLAGS += \ -nostdlib \ -DCORE_M0PLUS \ -D__VTOR_PRESENT=0 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_TUSB_MCU=OPT_MCU_LPC11UXX \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM3")))' \ -DCFG_TUSB_MEM_ALIGN='__attribute__((aligned(64)))' diff --git a/hw/bsp/lpcxpresso1347/board.mk b/hw/bsp/lpcxpresso1347/board.mk index 6dc3937f6..5befc3aee 100644 --- a/hw/bsp/lpcxpresso1347/board.mk +++ b/hw/bsp/lpcxpresso1347/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_EXAMPLE_MSC_READONLY \ -DCFG_TUSB_MCU=OPT_MCU_LPC13XX \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM2")))' \ diff --git a/hw/bsp/lpcxpresso1549/board.mk b/hw/bsp/lpcxpresso1549/board.mk index 18325c5f7..c603e4b30 100644 --- a/hw/bsp/lpcxpresso1549/board.mk +++ b/hw/bsp/lpcxpresso1549/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_EXAMPLE_MSC_READONLY \ -DCFG_TUSB_MCU=OPT_MCU_LPC15XX \ -DCFG_TUSB_MEM_ALIGN='__attribute__((aligned(64)))' diff --git a/hw/bsp/lpcxpresso1769/board.mk b/hw/bsp/lpcxpresso1769/board.mk index a345e5a27..6886ef749 100644 --- a/hw/bsp/lpcxpresso1769/board.mk +++ b/hw/bsp/lpcxpresso1769/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_TUSB_MCU=OPT_MCU_LPC175X_6X \ -DRTC_EV_SUPPORT=0 diff --git a/hw/bsp/mbed1768/board.mk b/hw/bsp/mbed1768/board.mk index 3bdc8a27a..5b23900ff 100644 --- a/hw/bsp/mbed1768/board.mk +++ b/hw/bsp/mbed1768/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_TUSB_MCU=OPT_MCU_LPC175X_6X \ -DRTC_EV_SUPPORT=0 diff --git a/hw/bsp/mcb1800/board.mk b/hw/bsp/mcb1800/board.mk index e3f07d0d0..3d3bb2371 100644 --- a/hw/bsp/mcb1800/board.mk +++ b/hw/bsp/mcb1800/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_LPCOPEN \ + -D__USE_CMSIS \ -DCFG_TUSB_MCU=OPT_MCU_LPC18XX # mcu driver cause following warnings diff --git a/hw/bsp/ngx4330/board.mk b/hw/bsp/ngx4330/board.mk index df9df63a1..08a877229 100644 --- a/hw/bsp/ngx4330/board.mk +++ b/hw/bsp/ngx4330/board.mk @@ -3,10 +3,12 @@ CFLAGS += \ -mthumb \ -mabi=aapcs \ -mcpu=cortex-m4 \ + -mfloat-abi=hard \ + -mfpu=fpv4-sp-d16 \ -nostdlib \ -DCORE_M4 \ - -DCFG_TUSB_MCU=OPT_MCU_LPC43XX \ - -D__USE_LPCOPEN + -D__USE_CMSIS \ + -DCFG_TUSB_MCU=OPT_MCU_LPC43XX # mcu driver cause following warnings CFLAGS += -Wno-error=strict-prototypes -Wno-error=unused-parameter diff --git a/src/host/usbh.c b/src/host/usbh.c index c5f28cf0e..352450588 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -596,7 +596,7 @@ bool enum_task(hcd_event_t* event) //------------- parse configuration & install drivers -------------// uint8_t const* p_desc = _usbh_ctrl_buf + sizeof(tusb_desc_configuration_t); - TU_LOG2_MEM(_usbh_ctrl_buf, ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength, 0); + // TU_LOG2_MEM(_usbh_ctrl_buf, ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength, 0); // parse each interfaces while( p_desc < _usbh_ctrl_buf + ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength ) From 52b5748e8d9dc8b3e32108f6fdae431ca1dbc71d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Sep 2020 00:56:43 +0700 Subject: [PATCH 40/52] fix ci build --- hw/bsp/ngx4330/board.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/bsp/ngx4330/board.mk b/hw/bsp/ngx4330/board.mk index 08a877229..23d53bd18 100644 --- a/hw/bsp/ngx4330/board.mk +++ b/hw/bsp/ngx4330/board.mk @@ -4,7 +4,7 @@ CFLAGS += \ -mabi=aapcs \ -mcpu=cortex-m4 \ -mfloat-abi=hard \ - -mfpu=fpv4-sp-d16 \ + -mfpu=fpv4-sp-d16 \ -nostdlib \ -DCORE_M4 \ -D__USE_CMSIS \ From c7d2d0ae294ca2c93920c86d6cd10a3e74e2b010 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Sep 2020 01:40:04 +0700 Subject: [PATCH 41/52] ohci work, tested with lpc1769 --- hw/bsp/lpcxpresso1769/lpcxpresso1769.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c index cc6fd95ae..f002e8cdc 100644 --- a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c +++ b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c @@ -78,21 +78,26 @@ static const PINMUX_GRP_T pin_usb_mux[] = { {0, 29, IOCON_MODE_INACT | IOCON_FUNC1}, // D+ {0, 30, IOCON_MODE_INACT | IOCON_FUNC1}, // D- - {2, 9, IOCON_MODE_INACT | IOCON_FUNC1}, // Connect + {2, 9, IOCON_MODE_INACT | IOCON_FUNC1}, // Soft Connect - {1, 19, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PPWR - {1, 22, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PWRD + {1, 19, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PPWR (Host mode) - /* VBUS is not connected on this board, so leave the pin at default setting. */ - /*Chip_IOCON_PinMux(LPC_IOCON, 1, 30, IOCON_MODE_INACT, IOCON_FUNC2);*/ /* USB VBUS */ + // VBUS is not connected on this board, so leave the pin at default setting. + /// Chip_IOCON_PinMux(LPC_IOCON, 1, 30, IOCON_MODE_INACT, IOCON_FUNC2); // USB VBUS }; // Invoked by startup code void SystemInit(void) { + extern void (* const g_pfnVectors[])(void); + unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; + *pSCB_VTOR = (unsigned int) &g_pfnVectors; + Chip_IOCON_Init(LPC_IOCON); Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); Chip_SetupXtalClocking(); + + Chip_SYSCTL_SetFLASHAccess(FLASHTIM_100MHZ_CPU); } void board_init(void) From 1cee2da7b920288202f651a4b970e50e05b8786f Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Sep 2020 12:58:09 +0700 Subject: [PATCH 42/52] revert to use __USE_LPCOPEN properly init fpu on LPC M4 --- hw/bsp/ea4088qs/board.mk | 9 ++++++--- hw/bsp/ea4088qs/ea4088qs.c | 10 ++++++++++ hw/bsp/ea4357/board.mk | 5 +++-- hw/bsp/ea4357/ea4357.c | 10 ++++++++++ hw/bsp/lpcxpresso11u37/board.mk | 2 +- hw/bsp/lpcxpresso11u68/board.mk | 2 +- hw/bsp/lpcxpresso1347/board.mk | 2 +- hw/bsp/lpcxpresso1549/board.mk | 2 +- hw/bsp/lpcxpresso1769/board.mk | 2 +- hw/bsp/lpcxpresso1769/lpcxpresso1769.c | 2 ++ hw/bsp/mbed1768/board.mk | 2 +- hw/bsp/mbed1768/mbed1768.c | 6 ++++++ hw/bsp/mcb1800/board.mk | 2 +- hw/bsp/mcb1800/mcb1800.c | 6 ++++++ hw/bsp/ngx4330/board.mk | 5 +++-- hw/bsp/ngx4330/ngx4330.c | 12 +++++++++--- 16 files changed, 62 insertions(+), 17 deletions(-) diff --git a/hw/bsp/ea4088qs/board.mk b/hw/bsp/ea4088qs/board.mk index 2a63d8af2..51a6009a0 100644 --- a/hw/bsp/ea4088qs/board.mk +++ b/hw/bsp/ea4088qs/board.mk @@ -3,9 +3,11 @@ CFLAGS += \ -mthumb \ -mabi=aapcs \ -mcpu=cortex-m4 \ + -mfloat-abi=hard \ + -mfpu=fpv4-sp-d16 \ -nostdlib \ -DCORE_M4 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM2")))' \ -DCFG_TUSB_MCU=OPT_MCU_LPC40XX @@ -25,7 +27,8 @@ SRC_C += \ $(MCU_DIR)/src/iocon_17xx_40xx.c \ $(MCU_DIR)/src/sysctl_17xx_40xx.c \ $(MCU_DIR)/src/sysinit_17xx_40xx.c \ - $(MCU_DIR)/src/uart_17xx_40xx.c + $(MCU_DIR)/src/uart_17xx_40xx.c \ + $(MCU_DIR)/src/fpu_init.c INC += \ $(TOP)/$(MCU_DIR)/inc @@ -35,7 +38,7 @@ VENDOR = nxp CHIP_FAMILY = lpc17_40 # For freeRTOS port source -FREERTOS_PORT = ARM_CM3 +FREERTOS_PORT = ARM_CM4F # For flash-jlink target JLINK_DEVICE = LPC4088 diff --git a/hw/bsp/ea4088qs/ea4088qs.c b/hw/bsp/ea4088qs/ea4088qs.c index 7fc047490..00db1a299 100644 --- a/hw/bsp/ea4088qs/ea4088qs.c +++ b/hw/bsp/ea4088qs/ea4088qs.c @@ -86,6 +86,16 @@ static const PINMUX_GRP_T pin_usb_mux[] = // Invoked by startup code void SystemInit(void) { +#ifdef __USE_LPCOPEN + extern void (* const g_pfnVectors[])(void); + unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; + *pSCB_VTOR = (unsigned int) &g_pfnVectors; + +#if __FPU_USED == 1 + fpuInit(); +#endif +#endif // __USE_LPCOPEN + Chip_IOCON_Init(LPC_IOCON); Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); Chip_SetupXtalClocking(); diff --git a/hw/bsp/ea4357/board.mk b/hw/bsp/ea4357/board.mk index 44481118d..3ebe107e2 100644 --- a/hw/bsp/ea4357/board.mk +++ b/hw/bsp/ea4357/board.mk @@ -7,7 +7,7 @@ CFLAGS += \ -mfpu=fpv4-sp-d16 \ -nostdlib \ -DCORE_M4 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC43XX # mcu driver cause following warnings @@ -26,7 +26,8 @@ SRC_C += \ $(MCU_DIR)/src/sysinit_18xx_43xx.c \ $(MCU_DIR)/src/i2c_18xx_43xx.c \ $(MCU_DIR)/src/i2cm_18xx_43xx.c \ - $(MCU_DIR)/src/uart_18xx_43xx.c + $(MCU_DIR)/src/uart_18xx_43xx.c \ + $(MCU_DIR)/src/fpu_init.c INC += \ $(TOP)/$(MCU_DIR)/inc \ diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index daa3a6bd0..f41ba5ab9 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -85,6 +85,16 @@ static const PINMUX_GRP_T pinclockmuxing[] = // Invoked by startup code void SystemInit(void) { +#ifdef __USE_LPCOPEN + extern void (* const g_pfnVectors[])(void); + unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; + *pSCB_VTOR = (unsigned int) &g_pfnVectors; + +#if __FPU_USED == 1 + fpuInit(); +#endif +#endif // __USE_LPCOPEN + /* Setup system level pin muxing */ Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); diff --git a/hw/bsp/lpcxpresso11u37/board.mk b/hw/bsp/lpcxpresso11u37/board.mk index 27019ff8b..d1c48a2e5 100644 --- a/hw/bsp/lpcxpresso11u37/board.mk +++ b/hw/bsp/lpcxpresso11u37/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m0 \ -nostdlib \ -DCORE_M0 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_EXAMPLE_MSC_READONLY \ -DCFG_TUSB_MCU=OPT_MCU_LPC11UXX \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM2")))' \ diff --git a/hw/bsp/lpcxpresso11u68/board.mk b/hw/bsp/lpcxpresso11u68/board.mk index 1c1660367..d9017ae1a 100644 --- a/hw/bsp/lpcxpresso11u68/board.mk +++ b/hw/bsp/lpcxpresso11u68/board.mk @@ -6,7 +6,7 @@ CFLAGS += \ -nostdlib \ -DCORE_M0PLUS \ -D__VTOR_PRESENT=0 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC11UXX \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM3")))' \ -DCFG_TUSB_MEM_ALIGN='__attribute__((aligned(64)))' diff --git a/hw/bsp/lpcxpresso1347/board.mk b/hw/bsp/lpcxpresso1347/board.mk index 5befc3aee..6dc3937f6 100644 --- a/hw/bsp/lpcxpresso1347/board.mk +++ b/hw/bsp/lpcxpresso1347/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_EXAMPLE_MSC_READONLY \ -DCFG_TUSB_MCU=OPT_MCU_LPC13XX \ -DCFG_TUSB_MEM_SECTION='__attribute__((section(".data.$$RAM2")))' \ diff --git a/hw/bsp/lpcxpresso1549/board.mk b/hw/bsp/lpcxpresso1549/board.mk index c603e4b30..18325c5f7 100644 --- a/hw/bsp/lpcxpresso1549/board.mk +++ b/hw/bsp/lpcxpresso1549/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_EXAMPLE_MSC_READONLY \ -DCFG_TUSB_MCU=OPT_MCU_LPC15XX \ -DCFG_TUSB_MEM_ALIGN='__attribute__((aligned(64)))' diff --git a/hw/bsp/lpcxpresso1769/board.mk b/hw/bsp/lpcxpresso1769/board.mk index 6886ef749..a345e5a27 100644 --- a/hw/bsp/lpcxpresso1769/board.mk +++ b/hw/bsp/lpcxpresso1769/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC175X_6X \ -DRTC_EV_SUPPORT=0 diff --git a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c index f002e8cdc..5ec4fac23 100644 --- a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c +++ b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c @@ -89,9 +89,11 @@ static const PINMUX_GRP_T pin_usb_mux[] = // Invoked by startup code void SystemInit(void) { +#ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; *pSCB_VTOR = (unsigned int) &g_pfnVectors; +#endif Chip_IOCON_Init(LPC_IOCON); Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); diff --git a/hw/bsp/mbed1768/board.mk b/hw/bsp/mbed1768/board.mk index 5b23900ff..3bdc8a27a 100644 --- a/hw/bsp/mbed1768/board.mk +++ b/hw/bsp/mbed1768/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC175X_6X \ -DRTC_EV_SUPPORT=0 diff --git a/hw/bsp/mbed1768/mbed1768.c b/hw/bsp/mbed1768/mbed1768.c index 5963ad8ec..0dfd05c81 100644 --- a/hw/bsp/mbed1768/mbed1768.c +++ b/hw/bsp/mbed1768/mbed1768.c @@ -65,6 +65,12 @@ static const PINMUX_GRP_T pin_usb_mux[] = // Invoked by startup code void SystemInit(void) { +#ifdef __USE_LPCOPEN + extern void (* const g_pfnVectors[])(void); + unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; + *pSCB_VTOR = (unsigned int) &g_pfnVectors; +#endif + Chip_IOCON_Init(LPC_IOCON); Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); Chip_SetupXtalClocking(); diff --git a/hw/bsp/mcb1800/board.mk b/hw/bsp/mcb1800/board.mk index 3d3bb2371..e3f07d0d0 100644 --- a/hw/bsp/mcb1800/board.mk +++ b/hw/bsp/mcb1800/board.mk @@ -5,7 +5,7 @@ CFLAGS += \ -mcpu=cortex-m3 \ -nostdlib \ -DCORE_M3 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC18XX # mcu driver cause following warnings diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index 1d7ffb35f..b57385985 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -112,6 +112,12 @@ static const PINMUX_GRP_T pinclockmuxing[] = // Invoked by startup code void SystemInit(void) { +#ifdef __USE_LPCOPEN + extern void (* const g_pfnVectors[])(void); + unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; + *pSCB_VTOR = (unsigned int) &g_pfnVectors; +#endif + /* Setup system level pin muxing */ Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); diff --git a/hw/bsp/ngx4330/board.mk b/hw/bsp/ngx4330/board.mk index 23d53bd18..fe66fceb5 100644 --- a/hw/bsp/ngx4330/board.mk +++ b/hw/bsp/ngx4330/board.mk @@ -7,7 +7,7 @@ CFLAGS += \ -mfpu=fpv4-sp-d16 \ -nostdlib \ -DCORE_M4 \ - -D__USE_CMSIS \ + -D__USE_LPCOPEN \ -DCFG_TUSB_MCU=OPT_MCU_LPC43XX # mcu driver cause following warnings @@ -24,7 +24,8 @@ SRC_C += \ $(MCU_DIR)/src/clock_18xx_43xx.c \ $(MCU_DIR)/src/gpio_18xx_43xx.c \ $(MCU_DIR)/src/sysinit_18xx_43xx.c \ - $(MCU_DIR)/src/uart_18xx_43xx.c + $(MCU_DIR)/src/uart_18xx_43xx.c \ + $(MCU_DIR)/src/fpu_init.c INC += \ $(TOP)/$(MCU_DIR)/inc \ diff --git a/hw/bsp/ngx4330/ngx4330.c b/hw/bsp/ngx4330/ngx4330.c index 5296ddcb3..b1908fc38 100644 --- a/hw/bsp/ngx4330/ngx4330.c +++ b/hw/bsp/ngx4330/ngx4330.c @@ -71,11 +71,17 @@ static const PINMUX_GRP_T pinmuxing[] = }; // Invoked by startup code -extern void (* const g_pfnVectors[])(void); void SystemInit(void) { - // Remap isr vector - *((uint32_t *) 0xE000ED08) = (uint32_t) &g_pfnVectors; +#ifdef __USE_LPCOPEN + extern void (* const g_pfnVectors[])(void); + unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; + *pSCB_VTOR = (unsigned int) &g_pfnVectors; + +#if __FPU_USED == 1 + fpuInit(); +#endif +#endif // __USE_LPCOPEN // Set up pinmux Chip_SCU_SetPinMuxing(pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); From 1d83ad0ebb6c263736b0dadec80c9406563a5199 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Sep 2020 13:12:09 +0700 Subject: [PATCH 43/52] rename tuh_isr/hcd_isr to tuh_int_handler/hcd_int_handler --- hw/bsp/ea4088qs/ea4088qs.c | 2 +- hw/bsp/ea4357/ea4357.c | 4 ++-- hw/bsp/lpcxpresso1769/lpcxpresso1769.c | 2 +- hw/bsp/mbed1768/mbed1768.c | 2 +- hw/bsp/mcb1800/mcb1800.c | 4 ++-- hw/bsp/mimxrt1010_evk/mimxrt1010_evk.c | 2 +- hw/bsp/mimxrt1015_evk/mimxrt1015_evk.c | 2 +- hw/bsp/mimxrt1020_evk/mimxrt1020_evk.c | 2 +- hw/bsp/mimxrt1050_evkb/mimxrt1050_evkb.c | 4 ++-- hw/bsp/mimxrt1060_evk/mimxrt1060_evk.c | 4 ++-- hw/bsp/mimxrt1064_evk/mimxrt1064_evk.c | 4 ++-- hw/bsp/ngx4330/ngx4330.c | 4 ++-- hw/bsp/teensy_40/teensy40.c | 4 ++-- src/device/dcd.h | 6 +++--- src/host/ehci/ehci.c | 2 +- src/host/hcd.h | 9 +++------ src/host/ohci/ohci.c | 2 +- src/host/usbh.h | 3 ++- 18 files changed, 30 insertions(+), 32 deletions(-) diff --git a/hw/bsp/ea4088qs/ea4088qs.c b/hw/bsp/ea4088qs/ea4088qs.c index 00db1a299..df76bb92a 100644 --- a/hw/bsp/ea4088qs/ea4088qs.c +++ b/hw/bsp/ea4088qs/ea4088qs.c @@ -33,7 +33,7 @@ void USB_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index f41ba5ab9..69d2c3cc1 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -246,7 +246,7 @@ void board_init(void) void USB0_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -257,7 +257,7 @@ void USB0_IRQHandler(void) void USB1_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c index 5ec4fac23..0d5e98e9e 100644 --- a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c +++ b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c @@ -33,7 +33,7 @@ void USB_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mbed1768/mbed1768.c b/hw/bsp/mbed1768/mbed1768.c index 0dfd05c81..17004cfe0 100644 --- a/hw/bsp/mbed1768/mbed1768.c +++ b/hw/bsp/mbed1768/mbed1768.c @@ -147,7 +147,7 @@ void board_init(void) void USB_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index b57385985..a17ac5c3b 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -33,7 +33,7 @@ void USB0_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -44,7 +44,7 @@ void USB0_IRQHandler(void) void USB1_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mimxrt1010_evk/mimxrt1010_evk.c b/hw/bsp/mimxrt1010_evk/mimxrt1010_evk.c index c682ace8d..1c4b1798c 100644 --- a/hw/bsp/mimxrt1010_evk/mimxrt1010_evk.c +++ b/hw/bsp/mimxrt1010_evk/mimxrt1010_evk.c @@ -121,7 +121,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mimxrt1015_evk/mimxrt1015_evk.c b/hw/bsp/mimxrt1015_evk/mimxrt1015_evk.c index 214077a4a..2b1f3cc80 100644 --- a/hw/bsp/mimxrt1015_evk/mimxrt1015_evk.c +++ b/hw/bsp/mimxrt1015_evk/mimxrt1015_evk.c @@ -121,7 +121,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mimxrt1020_evk/mimxrt1020_evk.c b/hw/bsp/mimxrt1020_evk/mimxrt1020_evk.c index f32ec5401..273f3a0c9 100644 --- a/hw/bsp/mimxrt1020_evk/mimxrt1020_evk.c +++ b/hw/bsp/mimxrt1020_evk/mimxrt1020_evk.c @@ -120,7 +120,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mimxrt1050_evkb/mimxrt1050_evkb.c b/hw/bsp/mimxrt1050_evkb/mimxrt1050_evkb.c index be0ae3ea8..1042bcfe7 100644 --- a/hw/bsp/mimxrt1050_evkb/mimxrt1050_evkb.c +++ b/hw/bsp/mimxrt1050_evkb/mimxrt1050_evkb.c @@ -124,7 +124,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -135,7 +135,7 @@ void USB_OTG1_IRQHandler(void) void USB_OTG2_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mimxrt1060_evk/mimxrt1060_evk.c b/hw/bsp/mimxrt1060_evk/mimxrt1060_evk.c index be0ae3ea8..1042bcfe7 100644 --- a/hw/bsp/mimxrt1060_evk/mimxrt1060_evk.c +++ b/hw/bsp/mimxrt1060_evk/mimxrt1060_evk.c @@ -124,7 +124,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -135,7 +135,7 @@ void USB_OTG1_IRQHandler(void) void USB_OTG2_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/mimxrt1064_evk/mimxrt1064_evk.c b/hw/bsp/mimxrt1064_evk/mimxrt1064_evk.c index be0ae3ea8..1042bcfe7 100644 --- a/hw/bsp/mimxrt1064_evk/mimxrt1064_evk.c +++ b/hw/bsp/mimxrt1064_evk/mimxrt1064_evk.c @@ -124,7 +124,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -135,7 +135,7 @@ void USB_OTG1_IRQHandler(void) void USB_OTG2_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/ngx4330/ngx4330.c b/hw/bsp/ngx4330/ngx4330.c index b1908fc38..dda3bc092 100644 --- a/hw/bsp/ngx4330/ngx4330.c +++ b/hw/bsp/ngx4330/ngx4330.c @@ -231,7 +231,7 @@ void board_init(void) void USB0_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -242,7 +242,7 @@ void USB0_IRQHandler(void) void USB1_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/hw/bsp/teensy_40/teensy40.c b/hw/bsp/teensy_40/teensy40.c index f45a98ef3..30fdedd27 100644 --- a/hw/bsp/teensy_40/teensy40.c +++ b/hw/bsp/teensy_40/teensy40.c @@ -125,7 +125,7 @@ void board_init(void) void USB_OTG1_IRQHandler(void) { #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST - tuh_isr(0); + tuh_int_handler(0); #endif #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE @@ -136,7 +136,7 @@ void USB_OTG1_IRQHandler(void) void USB_OTG2_IRQHandler(void) { #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST - tuh_isr(1); + tuh_int_handler(1); #endif #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE diff --git a/src/device/dcd.h b/src/device/dcd.h index a4635346b..776f782b8 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -87,9 +87,9 @@ typedef struct TU_ATTR_ALIGNED(4) //TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct"); -/*------------------------------------------------------------------*/ -/* Device API - *------------------------------------------------------------------*/ +//--------------------------------------------------------------------+ +// Controller API +//--------------------------------------------------------------------+ // Initialize controller to device mode void dcd_init (uint8_t rhport); diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index e2ead7b08..295da4566 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -648,7 +648,7 @@ static void xfer_error_isr(uint8_t hostid) } //------------- Host Controller Driver's Interrupt Handler -------------// -void hcd_isr(uint8_t rhport) +void hcd_int_handler(uint8_t rhport) { ehci_registers_t* regs = ehci_data.regs; diff --git a/src/host/hcd.h b/src/host/hcd.h index f2ca0a1d6..24cc62501 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -84,10 +84,10 @@ enum { #endif //--------------------------------------------------------------------+ -// HCD API +// Controller & Port API //--------------------------------------------------------------------+ bool hcd_init(void); -void hcd_isr(uint8_t hostid); +void hcd_int_handler(uint8_t rhport); void hcd_int_enable (uint8_t rhport); void hcd_int_disable(uint8_t rhport); @@ -100,7 +100,6 @@ static inline uint32_t hcd_frame_number(uint8_t rhport) return hcd_uframe_number(rhport) >> 3; } -// PORT API /// return the current connect status of roothub port bool hcd_port_connect_status(uint8_t hostid); void hcd_port_reset(uint8_t hostid); @@ -144,9 +143,7 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * bool hcd_pipe_queue_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes); // only queue, not transferring yet bool hcd_pipe_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete); -#if 0 -tusb_error_t hcd_pipe_cancel(); -#endif +// tusb_error_t hcd_pipe_cancel(); #ifdef __cplusplus } diff --git a/src/host/ohci/ohci.c b/src/host/ohci/ohci.c index 5ee5fe8c4..114f52e7d 100644 --- a/src/host/ohci/ohci.c +++ b/src/host/ohci/ohci.c @@ -606,7 +606,7 @@ static void done_queue_isr(uint8_t hostid) } } -void hcd_isr(uint8_t hostid) +void hcd_int_handler(uint8_t hostid) { uint32_t const int_en = OHCI_REG->interrupt_enable; uint32_t const int_status = OHCI_REG->interrupt_status & int_en; diff --git a/src/host/usbh.h b/src/host/usbh.h index 39e7a4e0d..27193d378 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -73,7 +73,8 @@ typedef struct { void tuh_task(void); // Interrupt handler, name alias to HCD -#define tuh_isr hcd_isr +extern void hcd_int_handler(uint8_t rhport); +#define tuh_int_handler hcd_int_handler tusb_device_state_t tuh_device_get_state (uint8_t dev_addr); static inline bool tuh_device_is_configured(uint8_t dev_addr) From ef651e073409bac1a9793d97bf5a25220913f32e Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Sep 2020 17:07:29 +0700 Subject: [PATCH 44/52] fix #449 remove obsolete pipehandle from hid host --- examples/host/cdc_msc_hid/Makefile | 5 ++- src/class/hid/hid_host.c | 66 ++++++++++++++++-------------- src/class/hid/hid_host.h | 10 +---- src/class/msc/msc_host.c | 6 +-- src/class/msc/msc_host.h | 2 +- 5 files changed, 45 insertions(+), 44 deletions(-) diff --git a/examples/host/cdc_msc_hid/Makefile b/examples/host/cdc_msc_hid/Makefile index 29a323957..35de0a9a0 100644 --- a/examples/host/cdc_msc_hid/Makefile +++ b/examples/host/cdc_msc_hid/Makefile @@ -13,12 +13,13 @@ CFLAGS += -Wno-error=cast-align # TinyUSB Host Stack source SRC_C += \ + src/class/cdc/cdc_host.c \ + src/class/hid/hid_host.c \ + src/class/msc/msc_host.c \ src/host/usbh.c \ src/host/hub.c \ src/host/ehci/ehci.c \ src/host/ohci/ohci.c \ - src/class/cdc/cdc_host.c \ - src/class/msc/msc_host.c \ src/portable/nxp/lpc18_43/hcd_lpc18_43.c \ src/portable/nxp/lpc17_40/hcd_lpc17_40.c diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index 8abb3cd22..ec5333917 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -35,35 +35,43 @@ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ +typedef struct { + uint8_t itf_numr; + uint8_t ep_in; + uint8_t ep_out; + + uint16_t report_size; +}hidh_interface_t; + //--------------------------------------------------------------------+ // HID Interface common functions //--------------------------------------------------------------------+ -static inline bool hidh_interface_open(uint8_t dev_addr, uint8_t interface_number, tusb_desc_endpoint_t const *p_endpoint_desc, hidh_interface_info_t *p_hid) +static inline bool hidh_interface_open(uint8_t rhport, uint8_t dev_addr, uint8_t interface_number, tusb_desc_endpoint_t const *p_endpoint_desc, hidh_interface_t *p_hid) { - p_hid->pipe_hdl = usbh_edpt_open(dev_addr, p_endpoint_desc, TUSB_CLASS_HID); - p_hid->report_size = p_endpoint_desc->wMaxPacketSize.size; // TODO get size from report descriptor - p_hid->interface_number = interface_number; + TU_ASSERT( usbh_edpt_open(rhport, dev_addr, p_endpoint_desc) ); - TU_ASSERT (pipehandle_is_valid(p_hid->pipe_hdl)); + p_hid->ep_in = p_endpoint_desc->bEndpointAddress; + p_hid->report_size = p_endpoint_desc->wMaxPacketSize.size; // TODO get size from report descriptor + p_hid->itf_numr = interface_number; return true; } -static inline void hidh_interface_close(hidh_interface_info_t *p_hid) +static inline void hidh_interface_close(hidh_interface_t *p_hid) { - tu_memclr(p_hid, sizeof(hidh_interface_info_t)); + tu_memclr(p_hid, sizeof(hidh_interface_t)); } // called from public API need to validate parameters -tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_interface_info_t *p_hid) +tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_interface_t *p_hid) { //------------- parameters validation -------------// // TODO change to use is configured function TU_ASSERT (TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); TU_VERIFY (report, TUSB_ERROR_INVALID_PARA); - TU_ASSERT (!hcd_edpt_busy(p_hid->pipe_hdl), TUSB_ERROR_INTERFACE_IS_BUSY); + TU_ASSERT (!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY); - TU_ASSERT_ERR( hcd_pipe_xfer(p_hid->pipe_hdl, report, p_hid->report_size, true) ) ; + TU_ASSERT_ERR( hcd_pipe_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size, true) ) ; return TUSB_ERROR_NONE; } @@ -85,12 +93,12 @@ uint8_t const hid_keycode_to_ascii_tbl[2][128] = }; #endif -static hidh_interface_info_t keyboardh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1 +static hidh_interface_t keyboardh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1 //------------- KEYBOARD PUBLIC API (parameter validation required) -------------// bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr) { - return tuh_device_is_configured(dev_addr) && pipehandle_is_valid(keyboardh_data[dev_addr-1].pipe_hdl); + return tuh_device_is_configured(dev_addr) && (keyboardh_data[dev_addr-1].ep_in != 0); } tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void* p_report) @@ -100,8 +108,7 @@ tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void* p_report) bool tuh_hid_keyboard_is_busy(uint8_t dev_addr) { - return tuh_hid_keyboard_is_mounted(dev_addr) && - hcd_edpt_busy( keyboardh_data[dev_addr-1].pipe_hdl ); + return tuh_hid_keyboard_is_mounted(dev_addr) && hcd_edpt_busy(dev_addr, keyboardh_data[dev_addr-1].ep_in); } #endif @@ -111,18 +118,17 @@ bool tuh_hid_keyboard_is_busy(uint8_t dev_addr) //--------------------------------------------------------------------+ #if CFG_TUH_HID_MOUSE -static hidh_interface_info_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1 +static hidh_interface_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1 //------------- Public API -------------// bool tuh_hid_mouse_is_mounted(uint8_t dev_addr) { - return tuh_device_is_configured(dev_addr) && pipehandle_is_valid(mouseh_data[dev_addr-1].pipe_hdl); + return tuh_device_is_configured(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0); } bool tuh_hid_mouse_is_busy(uint8_t dev_addr) { - return tuh_hid_mouse_is_mounted(dev_addr) && - hcd_edpt_busy( mouseh_data[dev_addr-1].pipe_hdl ); + return tuh_hid_mouse_is_mounted(dev_addr) && hcd_edpt_busy(dev_addr, mouseh_data[dev_addr-1].ep_in); } tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void * report) @@ -149,11 +155,11 @@ tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void * report) void hidh_init(void) { #if CFG_TUH_HID_KEYBOARD - tu_memclr(&keyboardh_data, sizeof(hidh_interface_info_t)*CFG_TUSB_HOST_DEVICE_MAX); + tu_memclr(&keyboardh_data, sizeof(hidh_interface_t)*CFG_TUSB_HOST_DEVICE_MAX); #endif #if CFG_TUH_HID_MOUSE - tu_memclr(&mouseh_data, sizeof(hidh_interface_info_t)*CFG_TUSB_HOST_DEVICE_MAX); + tu_memclr(&mouseh_data, sizeof(hidh_interface_t)*CFG_TUSB_HOST_DEVICE_MAX); #endif #if CFG_TUSB_HOST_HID_GENERIC @@ -165,7 +171,7 @@ void hidh_init(void) CFG_TUSB_MEM_SECTION uint8_t report_descriptor[256]; #endif -bool hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length) +bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length) { uint8_t const *p_desc = (uint8_t const *) p_interface_desc; @@ -208,7 +214,7 @@ bool hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interfac #if CFG_TUH_HID_KEYBOARD if ( HID_PROTOCOL_KEYBOARD == p_interface_desc->bInterfaceProtocol) { - TU_ASSERT( hidh_interface_open(dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &keyboardh_data[dev_addr-1]) ); + TU_ASSERT( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &keyboardh_data[dev_addr-1]) ); tuh_hid_keyboard_mounted_cb(dev_addr); } else #endif @@ -216,7 +222,7 @@ bool hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interfac #if CFG_TUH_HID_MOUSE if ( HID_PROTOCOL_MOUSE == p_interface_desc->bInterfaceProtocol) { - TU_ASSERT ( hidh_interface_open(dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &mouseh_data[dev_addr-1]) ); + TU_ASSERT ( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &mouseh_data[dev_addr-1]) ); tuh_hid_mouse_mounted_cb(dev_addr); } else #endif @@ -236,22 +242,22 @@ bool hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interfac return true; } -void hidh_isr(pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes) +void hidh_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { (void) xferred_bytes; // TODO may need to use this para later #if CFG_TUH_HID_KEYBOARD - if ( pipehandle_is_equal(pipe_hdl, keyboardh_data[pipe_hdl.dev_addr-1].pipe_hdl) ) + if ( ep_addr == keyboardh_data[dev_addr-1].ep_in ) { - tuh_hid_keyboard_isr(pipe_hdl.dev_addr, event); + tuh_hid_keyboard_isr(dev_addr, event); return; } #endif #if CFG_TUH_HID_MOUSE - if ( pipehandle_is_equal(pipe_hdl, mouseh_data[pipe_hdl.dev_addr-1].pipe_hdl) ) + if ( ep_addr == mouseh_data[dev_addr-1].ep_in ) { - tuh_hid_mouse_isr(pipe_hdl.dev_addr, event); + tuh_hid_mouse_isr(dev_addr, event); return; } #endif @@ -264,7 +270,7 @@ void hidh_isr(pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_byte void hidh_close(uint8_t dev_addr) { #if CFG_TUH_HID_KEYBOARD - if ( pipehandle_is_valid( keyboardh_data[dev_addr-1].pipe_hdl ) ) + if ( keyboardh_data[dev_addr-1].ep_in != 0 ) { hidh_interface_close(&keyboardh_data[dev_addr-1]); tuh_hid_keyboard_unmounted_cb(dev_addr); @@ -272,7 +278,7 @@ void hidh_close(uint8_t dev_addr) #endif #if CFG_TUH_HID_MOUSE - if( pipehandle_is_valid( mouseh_data[dev_addr-1].pipe_hdl ) ) + if( mouseh_data[dev_addr-1].ep_in != 0 ) { hidh_interface_close(&mouseh_data[dev_addr-1]); tuh_hid_mouse_unmounted_cb( dev_addr ); diff --git a/src/class/hid/hid_host.h b/src/class/hid/hid_host.h index a3b614cd7..2e7f52674 100644 --- a/src/class/hid/hid_host.h +++ b/src/class/hid/hid_host.h @@ -195,15 +195,9 @@ void tuh_hid_generic_isr(uint8_t dev_addr, xfer_result_t event); //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -typedef struct { - pipe_handle_t pipe_hdl; - uint16_t report_size; - uint8_t interface_number; -}hidh_interface_info_t; - void hidh_init(void); -bool hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length); -void hidh_isr(pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes); +bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length); +void hidh_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void hidh_close(uint8_t dev_addr); #ifdef __cplusplus diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index 3aa697b17..539e98376 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -306,7 +306,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc); } - p_msc->itf_numr = itf_desc->bInterfaceNumber; + p_msc->itf_num = itf_desc->bInterfaceNumber; (*p_length) += sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); //------------- Get Max Lun -------------// @@ -315,7 +315,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_IN }, .bRequest = MSC_REQ_GET_MAX_LUN, .wValue = 0, - .wIndex = p_msc->itf_numr, + .wIndex = p_msc->itf_num, .wLength = 1 }; // TODO STALL means zero @@ -328,7 +328,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT }, .bRequest = MSC_REQ_RESET, .wValue = 0, - .wIndex = p_msc->itf_numr, + .wIndex = p_msc->itf_num, .wLength = 0 }; TU_ASSERT( usbh_control_xfer( dev_addr, &request, NULL ) ); diff --git a/src/class/msc/msc_host.h b/src/class/msc/msc_host.h index 76cf86853..959294adf 100644 --- a/src/class/msc/msc_host.h +++ b/src/class/msc/msc_host.h @@ -175,7 +175,7 @@ void tuh_msc_isr(uint8_t dev_addr, xfer_result_t event, uint32_t xferred_bytes); //--------------------------------------------------------------------+ typedef struct { - uint8_t itf_numr; + uint8_t itf_num; uint8_t ep_in; uint8_t ep_out; From 4ecedc70c8cbec366c7eb6dc90c99c7cce27407e Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Sep 2020 23:48:56 +0700 Subject: [PATCH 45/52] fix vector assign --- hw/bsp/ea4088qs/ea4088qs.c | 2 +- hw/bsp/ea4357/ea4357.c | 2 +- hw/bsp/lpcxpresso1769/lpcxpresso1769.c | 2 +- hw/bsp/mbed1768/mbed1768.c | 2 +- hw/bsp/mcb1800/mcb1800.c | 2 +- hw/bsp/ngx4330/ngx4330.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hw/bsp/ea4088qs/ea4088qs.c b/hw/bsp/ea4088qs/ea4088qs.c index df76bb92a..f164526d7 100644 --- a/hw/bsp/ea4088qs/ea4088qs.c +++ b/hw/bsp/ea4088qs/ea4088qs.c @@ -89,7 +89,7 @@ void SystemInit(void) #ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; - *pSCB_VTOR = (unsigned int) &g_pfnVectors; + *pSCB_VTOR = (unsigned int) g_pfnVectors; #if __FPU_USED == 1 fpuInit(); diff --git a/hw/bsp/ea4357/ea4357.c b/hw/bsp/ea4357/ea4357.c index 69d2c3cc1..56653bd1b 100644 --- a/hw/bsp/ea4357/ea4357.c +++ b/hw/bsp/ea4357/ea4357.c @@ -88,7 +88,7 @@ void SystemInit(void) #ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; - *pSCB_VTOR = (unsigned int) &g_pfnVectors; + *pSCB_VTOR = (unsigned int) g_pfnVectors; #if __FPU_USED == 1 fpuInit(); diff --git a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c index 0d5e98e9e..101cc8a4c 100644 --- a/hw/bsp/lpcxpresso1769/lpcxpresso1769.c +++ b/hw/bsp/lpcxpresso1769/lpcxpresso1769.c @@ -92,7 +92,7 @@ void SystemInit(void) #ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; - *pSCB_VTOR = (unsigned int) &g_pfnVectors; + *pSCB_VTOR = (unsigned int) g_pfnVectors; #endif Chip_IOCON_Init(LPC_IOCON); diff --git a/hw/bsp/mbed1768/mbed1768.c b/hw/bsp/mbed1768/mbed1768.c index 17004cfe0..5495ed166 100644 --- a/hw/bsp/mbed1768/mbed1768.c +++ b/hw/bsp/mbed1768/mbed1768.c @@ -68,7 +68,7 @@ void SystemInit(void) #ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; - *pSCB_VTOR = (unsigned int) &g_pfnVectors; + *pSCB_VTOR = (unsigned int) g_pfnVectors; #endif Chip_IOCON_Init(LPC_IOCON); diff --git a/hw/bsp/mcb1800/mcb1800.c b/hw/bsp/mcb1800/mcb1800.c index a17ac5c3b..569d33834 100644 --- a/hw/bsp/mcb1800/mcb1800.c +++ b/hw/bsp/mcb1800/mcb1800.c @@ -115,7 +115,7 @@ void SystemInit(void) #ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; - *pSCB_VTOR = (unsigned int) &g_pfnVectors; + *pSCB_VTOR = (unsigned int) g_pfnVectors; #endif /* Setup system level pin muxing */ diff --git a/hw/bsp/ngx4330/ngx4330.c b/hw/bsp/ngx4330/ngx4330.c index dda3bc092..2e4ff9af9 100644 --- a/hw/bsp/ngx4330/ngx4330.c +++ b/hw/bsp/ngx4330/ngx4330.c @@ -76,7 +76,7 @@ void SystemInit(void) #ifdef __USE_LPCOPEN extern void (* const g_pfnVectors[])(void); unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; - *pSCB_VTOR = (unsigned int) &g_pfnVectors; + *pSCB_VTOR = (unsigned int) g_pfnVectors; #if __FPU_USED == 1 fpuInit(); From 35aee4a6af40c7c8befb5dfe81bbf7e26dc01ca8 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Sep 2020 23:57:51 +0700 Subject: [PATCH 46/52] more hid host work --- examples/host/cdc_msc_hid/src/main.c | 63 +++++++++++++++++++++++++--- src/class/hid/hid_host.c | 22 +++------- src/host/usbh.c | 2 +- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 9ec213090..8f61310d5 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -110,21 +110,74 @@ void cdc_task(void) // USB HID //--------------------------------------------------------------------+ #if CFG_TUH_HID_KEYBOARD + +uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; + +// look up new key in previous keys +static inline bool find_key_in_report(hid_keyboard_report_t const *p_report, uint8_t keycode) +{ + for(uint8_t i=0; i<6; i++) + { + if (p_report->keycode[i] == keycode) return true; + } + + return false; +} + +static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report) +{ + static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released + + //------------- example code ignore control (non-printable) key affects -------------// + for(uint8_t i=0; i<6; i++) + { + if ( p_new_report->keycode[i] ) + { + if ( find_key_in_report(&prev_report, p_new_report->keycode[i]) ) + { + // exist in previous report means the current key is holding + }else + { + // not existed in previous report means the current key is pressed + bool const is_shift = p_new_report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT); + uint8_t ch = keycode2ascii[p_new_report->keycode[i]][is_shift ? 1 : 0]; + putchar(ch); + if ( ch == '\r' ) putchar('\n'); // added new line for enter key + } + } + // TODO example skips key released + } + + prev_report = *p_new_report; +} + +CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report; + void hid_task(void) { - + uint8_t const addr = 1; + if ( tuh_hid_keyboard_is_mounted(addr) ) + { + if ( !tuh_hid_keyboard_is_busy(addr) ) + { + process_kbd_report(&usb_keyboard_report); + tuh_hid_keyboard_get_report(addr, &usb_keyboard_report); + } + } } void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr) { // application set-up - printf("\na Keyboard device (address %d) is mounted\n", dev_addr); + printf("A Keyboard device (address %d) is mounted\r\n", dev_addr); + + tuh_hid_keyboard_get_report(dev_addr, &usb_keyboard_report); } void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr) { // application tear-down - printf("\na Keyboard device (address %d) is unmounted\n", dev_addr); + printf("A Keyboard device (address %d) is unmounted\r\n", dev_addr); } // invoked ISR context @@ -139,13 +192,13 @@ void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event) void tuh_hid_mouse_mounted_cb(uint8_t dev_addr) { // application set-up - printf("\na Mouse device (address %d) is mounted\n", dev_addr); + printf("A Mouse device (address %d) is mounted\r\n", dev_addr); } void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr) { // application tear-down - printf("\na Mouse device (address %d) is unmounted\n", dev_addr); + printf("A Mouse device (address %d) is unmounted\r\n", dev_addr); } // invoked ISR context diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index ec5333917..d2d1c0397 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -67,11 +67,11 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int { //------------- parameters validation -------------// // TODO change to use is configured function - TU_ASSERT (TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); - TU_VERIFY (report, TUSB_ERROR_INVALID_PARA); - TU_ASSERT (!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY); + TU_ASSERT(TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); + TU_VERIFY(report, TUSB_ERROR_INVALID_PARA); + TU_ASSERT(!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY); - TU_ASSERT_ERR( hcd_pipe_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size, true) ) ; + TU_ASSERT( hcd_pipe_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size, true) ) ; return TUSB_ERROR_NONE; } @@ -81,18 +81,6 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int //--------------------------------------------------------------------+ #if CFG_TUH_HID_KEYBOARD -#if 0 -#define EXPAND_KEYCODE_TO_ASCII(keycode, ascii, shift_modified) \ - [0][keycode] = ascii,\ - [1][keycode] = shift_modified,\ - -// TODO size of table should be a macro for application to check boundary -uint8_t const hid_keycode_to_ascii_tbl[2][128] = -{ - HID_KEYCODE_TABLE(EXPAND_KEYCODE_TO_ASCII) -}; -#endif - static hidh_interface_t keyboardh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1 //------------- KEYBOARD PUBLIC API (parameter validation required) -------------// @@ -215,6 +203,7 @@ bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c if ( HID_PROTOCOL_KEYBOARD == p_interface_desc->bInterfaceProtocol) { TU_ASSERT( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &keyboardh_data[dev_addr-1]) ); + TU_LOG2_HEX(keyboardh_data[dev_addr-1].ep_in); tuh_hid_keyboard_mounted_cb(dev_addr); } else #endif @@ -223,6 +212,7 @@ bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c if ( HID_PROTOCOL_MOUSE == p_interface_desc->bInterfaceProtocol) { TU_ASSERT ( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &mouseh_data[dev_addr-1]) ); + TU_LOG2_HEX(mouseh_data[dev_addr-1].ep_in); tuh_hid_mouse_mounted_cb(dev_addr); } else #endif diff --git a/src/host/usbh.c b/src/host/usbh.c index 352450588..58550c93b 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -284,7 +284,7 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t ev if (usbh_class_drivers[drv_id].isr) { - TU_LOG2("%s isr\r\n", usbh_class_drivers[drv_id].name); + //TU_LOG2("%s isr\r\n", usbh_class_drivers[drv_id].name); usbh_class_drivers[drv_id].isr(dev_addr, ep_addr, event, xferred_bytes); } else From 7828c396dbc287f9d9a0aa6af63a32108f139b94 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Sep 2020 00:23:57 +0700 Subject: [PATCH 47/52] keyboard host work as proof of concept --- examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject | 1 + examples/host/cdc_msc_hid/src/main.c | 2 +- examples/host/cdc_msc_hid/src/tusb_config.h | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject b/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject index e0a2f6f97..2b5538957 100644 --- a/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject +++ b/examples/host/cdc_msc_hid/ses/lpc18xx/lpc18xx.emProject @@ -53,6 +53,7 @@ + diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 8f61310d5..209379b37 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -58,7 +58,7 @@ int main(void) cdc_task(); #endif -#if CFG_TUD_HID +#if CFG_TUH_HID_KEYBOARD || CFG_TUH_HID_MOUSE hid_task(); #endif } diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index dd50c7050..6604623b1 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -71,8 +71,8 @@ #define CFG_TUH_HUB 0 #define CFG_TUH_CDC 1 -#define CFG_TUH_HID_KEYBOARD 0 -#define CFG_TUH_HID_MOUSE 0 +#define CFG_TUH_HID_KEYBOARD 1 +#define CFG_TUH_HID_MOUSE 1 #define CFG_TUSB_HOST_HID_GENERIC 0 // (not yet supported) #define CFG_TUH_MSC 1 #define CFG_TUH_VENDOR 0 From 865ebf7c5db98ccc3f8f5f4fead9d8bba0e57c18 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Sep 2020 00:50:10 +0700 Subject: [PATCH 48/52] fflush(stdout) for keyboard host example --- examples/host/cdc_msc_hid/src/main.c | 2 ++ examples/host/cdc_msc_hid/src/msc_app.c | 4 ++-- examples/make.mk | 1 - 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 209379b37..34503499f 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -143,6 +143,8 @@ static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report) uint8_t ch = keycode2ascii[p_new_report->keycode[i]][is_shift ? 1 : 0]; putchar(ch); if ( ch == '\r' ) putchar('\n'); // added new line for enter key + + fflush(stdout); // flush right away, else nanolib will wait for newline } } // TODO example skips key released diff --git a/examples/host/cdc_msc_hid/src/msc_app.c b/examples/host/cdc_msc_hid/src/msc_app.c index 93dc6d3fe..62bd961c1 100644 --- a/examples/host/cdc_msc_hid/src/msc_app.c +++ b/examples/host/cdc_msc_hid/src/msc_app.c @@ -35,7 +35,7 @@ //------------- IMPLEMENTATION -------------// void tuh_msc_mounted_cb(uint8_t dev_addr) { - printf("a MassStorage device is mounted\r\n"); + printf("A MassStorage device is mounted\r\n"); //------------- Disk Information -------------// // SCSI VendorID[8] & ProductID[16] from Inquiry Command @@ -83,7 +83,7 @@ void tuh_msc_mounted_cb(uint8_t dev_addr) void tuh_msc_unmounted_cb(uint8_t dev_addr) { (void) dev_addr; - printf("a MassStorage device is unmounted\r\n"); + printf("A MassStorage device is unmounted\r\n"); // uint8_t phy_disk = dev_addr-1; // diff --git a/examples/make.mk b/examples/make.mk index b7917987e..6743044fd 100644 --- a/examples/make.mk +++ b/examples/make.mk @@ -84,7 +84,6 @@ ifeq ($(LOGGER),rtt) RTT_SRC = lib/SEGGER_RTT CFLAGS += -DLOGGER_RTT -DSEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL INC += $(TOP)/$(RTT_SRC)/RTT - SRC_C += $(RTT_SRC)/RTT/SEGGER_RTT_printf.c SRC_C += $(RTT_SRC)/RTT/SEGGER_RTT.c else ifeq ($(LOGGER),swo) CFLAGS += -DLOGGER_SWO From 5fb3d439b3e415bae5edbc556ea20f7211ac4bb5 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Sep 2020 01:32:30 +0700 Subject: [PATCH 49/52] update ci to also build host example --- .github/workflows/build.yml | 19 +++++++-- .../host/cdc_msc_hid/.only.MCU_LPC175X_6X | 0 .../host/cdc_msc_hid/.only.MCU_LPC177X_8X | 0 examples/host/cdc_msc_hid/.only.MCU_LPC18XX | 0 examples/host/cdc_msc_hid/.only.MCU_LPC40XX | 0 examples/host/cdc_msc_hid/.only.MCU_LPC43XX | 0 examples/host/cdc_msc_hid/src/main.c | 5 ++- tools/build_all.py | 41 +++++++++++++------ 8 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 examples/host/cdc_msc_hid/.only.MCU_LPC175X_6X create mode 100644 examples/host/cdc_msc_hid/.only.MCU_LPC177X_8X create mode 100644 examples/host/cdc_msc_hid/.only.MCU_LPC18XX create mode 100644 examples/host/cdc_msc_hid/.only.MCU_LPC40XX create mode 100644 examples/host/cdc_msc_hid/.only.MCU_LPC43XX diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2ad274928..da1f9e5e0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,9 +26,22 @@ jobs: strategy: fail-fast: false matrix: - example: ['board_test', 'cdc_dual_ports', 'cdc_msc', 'cdc_msc_freertos', 'dfu_rt', - 'hid_composite', 'hid_composite_freertos', 'hid_generic_inout', 'midi_test', 'msc_dual_lun', 'net_lwip_webserver', - 'usbtmc', 'webusb_serial'] + example: + - 'device/board_test' + - 'device/cdc_dual_ports' + - 'device/cdc_msc' + - 'device/cdc_msc_freertos' + - 'device/dfu_rt' + - 'device/hid_composite' + - 'device/hid_composite_freertos' + - 'device/hid_generic_inout' + - 'device/midi_test' + - 'device/msc_dual_lun' + - 'device/net_lwip_webserver' + - 'device/usbtmc' + - 'device/webusb_serial' + - 'host/cdc_msc_hid' + steps: - name: Setup Python uses: actions/setup-python@v1 diff --git a/examples/host/cdc_msc_hid/.only.MCU_LPC175X_6X b/examples/host/cdc_msc_hid/.only.MCU_LPC175X_6X new file mode 100644 index 000000000..e69de29bb diff --git a/examples/host/cdc_msc_hid/.only.MCU_LPC177X_8X b/examples/host/cdc_msc_hid/.only.MCU_LPC177X_8X new file mode 100644 index 000000000..e69de29bb diff --git a/examples/host/cdc_msc_hid/.only.MCU_LPC18XX b/examples/host/cdc_msc_hid/.only.MCU_LPC18XX new file mode 100644 index 000000000..e69de29bb diff --git a/examples/host/cdc_msc_hid/.only.MCU_LPC40XX b/examples/host/cdc_msc_hid/.only.MCU_LPC40XX new file mode 100644 index 000000000..e69de29bb diff --git a/examples/host/cdc_msc_hid/.only.MCU_LPC43XX b/examples/host/cdc_msc_hid/.only.MCU_LPC43XX new file mode 100644 index 000000000..e69de29bb diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 34503499f..4200cc5e0 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -185,7 +185,8 @@ void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr) // invoked ISR context void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event) { - + (void) dev_addr; + (void) event; } #endif @@ -206,6 +207,8 @@ void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr) // invoked ISR context void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event) { + (void) dev_addr; + (void) event; } #endif diff --git a/tools/build_all.py b/tools/build_all.py index 9581104e1..0dc555d2f 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -4,6 +4,10 @@ import sys import subprocess import time +SUCCEEDED = "\033[32msucceeded\033[0m" +FAILED = "\033[31mfailed\033[0m" +SKIPPED = "\033[33mskipped\033[0m" + success_count = 0 fail_count = 0 skip_count = 0 @@ -11,15 +15,19 @@ exit_status = 0 total_time = time.monotonic() -build_format = '| {:23} | {:30} | {:9} | {:7} | {:6} | {:6} |' -build_separator = '-' * 100 +build_format = '| {:29} | {:30} | {:18} | {:7} | {:6} | {:6} |' +build_separator = '-' * 106 # If examples are not specified in arguments, build all all_examples = [] for entry in os.scandir("examples/device"): if entry.is_dir(): - all_examples.append(entry.name) + all_examples.append("device/" + entry.name) + +for entry in os.scandir("examples/host"): + if entry.is_dir(): + all_examples.append("host/" + entry.name) if len(sys.argv) > 1: input_examples = list(set(all_examples).intersection(sys.argv)) @@ -43,14 +51,14 @@ if len(sys.argv) > 1: all_boards.sort() def build_example(example, board): - subprocess.run("make -C examples/device/{} BOARD={} clean".format(example, board), shell=True, + subprocess.run("make -C examples/{} BOARD={} clean".format(example, board), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - return subprocess.run("make -j -C examples/device/{} BOARD={} all".format(example, board), shell=True, + return subprocess.run("make -j -C examples/{} BOARD={} all".format(example, board), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) def build_size(example, board): #elf_file = 'examples/device/{}/_build/build-{}/{}-firmware.elf'.format(example, board, board) - elf_file = 'examples/device/{}/_build/build-{}/*.elf'.format(example, board) + elf_file = 'examples/{}/_build/build-{}/*.elf'.format(example, board) size_output = subprocess.run('size {}'.format(elf_file), shell=True, stdout=subprocess.PIPE).stdout.decode("utf-8") size_list = size_output.split('\n')[1].split('\t') flash_size = int(size_list[0]) @@ -58,7 +66,7 @@ def build_size(example, board): return (flash_size, sram_size) def skip_example(example, board): - ex_dir = 'examples/device/' + example + ex_dir = 'examples/' + example board_mk = 'hw/bsp/{}/board.mk'.format(board) with open(board_mk) as mk: @@ -74,10 +82,19 @@ def skip_example(example, board): if mcu_cflag in mk_contents: return 1 + # Build only list, if exists only these MCU are built + only_list = list(glob.iglob(ex_dir + '/.only.MCU_*')) + if len(only_list) > 0: + for only_file in only_list: + mcu_cflag = '-DCFG_TUSB_MCU=OPT_' + os.path.basename(only_file).split('.')[2] + if mcu_cflag in mk_contents: + return 0 + return 1 + return 0 print(build_separator) -print(build_format.format('Example', 'Board', 'Result', 'Time', 'Flash', 'SRAM')) +print(build_format.format('Example', 'Board', '\033[39mResult\033[0m', 'Time', 'Flash', 'SRAM')) for example in all_examples: print(build_separator) @@ -89,19 +106,19 @@ for example in all_examples: # Check if board is skipped if skip_example(example, board): - success = "\033[33mskipped\033[0m " + success = SKIPPED skip_count += 1 print(build_format.format(example, board, success, '-', flash_size, sram_size)) else: build_result = build_example(example, board) if build_result.returncode == 0: - success = "\033[32msucceeded\033[0m" + success = SUCCEEDED success_count += 1 (flash_size, sram_size) = build_size(example, board) else: exit_status = build_result.returncode - success = "\033[31mfailed\033[0m " + success = FAILED fail_count += 1 build_duration = time.monotonic() - start_time @@ -114,7 +131,7 @@ for example in all_examples: total_time = time.monotonic() - total_time print(build_separator) -print("Build Sumamary: {} \033[32msucceeded\033[0m, {} \033[31mfailed\033[0m, {} \033[33mskipped\033[0m and took {:.2f}s".format(success_count, fail_count, skip_count, total_time)) +print("Build Summary: {} {}, {} {}, {} {} and took {:.2f}s".format(success_count, SUCCEEDED, fail_count, FAILED, skip_count, SKIPPED, total_time)) print(build_separator) sys.exit(exit_status) From a8e538efe7d11291e32e016fad5910ebaf9eac43 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Sep 2020 01:35:32 +0700 Subject: [PATCH 50/52] clean up --- src/class/hid/hid_host.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index d2d1c0397..378278cc3 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -36,7 +36,7 @@ //--------------------------------------------------------------------+ typedef struct { - uint8_t itf_numr; + uint8_t itf_num; uint8_t ep_in; uint8_t ep_out; @@ -52,7 +52,7 @@ static inline bool hidh_interface_open(uint8_t rhport, uint8_t dev_addr, uint8_t p_hid->ep_in = p_endpoint_desc->bEndpointAddress; p_hid->report_size = p_endpoint_desc->wMaxPacketSize.size; // TODO get size from report descriptor - p_hid->itf_numr = interface_number; + p_hid->itf_num = interface_number; return true; } From 10d5dac9137adfd6c14d96e1079f08447eacd9d8 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Sep 2020 11:20:09 +0700 Subject: [PATCH 51/52] update doc --- docs/boards.md | 7 ++++--- hw/bsp/samg55xplained/board.mk | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/boards.md b/docs/boards.md index 1b3de29e9..309f5e5d9 100644 --- a/docs/boards.md +++ b/docs/boards.md @@ -19,7 +19,7 @@ This code base already had supported for a handful of following boards (sorted a - [DA14695 Development Kit – USB](https://www.dialog-semiconductor.com/products/da14695-development-kit-usb) - [DA1469x Development Kit – Pro](https://www.dialog-semiconductor.com/products/da14695-development-kit-pro) -### MicroChip SAMD +### MicroChip SAM - [Adafruit Circuit Playground Express](https://www.adafruit.com/product/3333) - [Adafruit Feather M0 Express](https://www.adafruit.com/product/3403) @@ -28,9 +28,10 @@ This code base already had supported for a handful of following boards (sorted a - [Adafruit ItsyBitsy M4 Express](https://www.adafruit.com/product/3800) - [Adafruit Metro M0 Express](https://www.adafruit.com/product/3505) - [Adafruit Metro M4 Express](https://www.adafruit.com/product/3382) -- [Microchip SAMD11 Xplained](https://www.microchip.com/developmenttools/ProductDetails/atsamd11-xpro) -- [Seeeduino Xiao](https://www.seeedstudio.com/Seeeduino-XIAO-Arduino-Microcontroller-SAMD21-Cortex-M0+-p-4426.html) - [Great Scott Gadgets LUNA](https://greatscottgadgets.com/luna/) +- [Microchip SAMD11 Xplained](https://www.microchip.com/developmenttools/ProductDetails/atsamd11-xpro) +- [Microchip SAMG55 Xplained Pro Evaluation Kit](https://www.microchip.com/DevelopmentTools/ProductDetails/PartNO/ATSAMG55-XPRO) +- [Seeeduino Xiao](https://www.seeedstudio.com/Seeeduino-XIAO-Arduino-Microcontroller-SAMD21-Cortex-M0+-p-4426.html) ### Nordic nRF5x diff --git a/hw/bsp/samg55xplained/board.mk b/hw/bsp/samg55xplained/board.mk index b53757dff..a58a7b41d 100644 --- a/hw/bsp/samg55xplained/board.mk +++ b/hw/bsp/samg55xplained/board.mk @@ -46,7 +46,7 @@ CHIP_FAMILY = samg FREERTOS_PORT = ARM_CM4F # For flash-jlink target -JLINK_DEVICE = ATSAMD51J19 +JLINK_DEVICE = ATSAMG55J19 JLINK_IF = swd # flash using edbg from https://github.com/ataradov/edbg From 25bb8830c5db7985906d2540da2a35e49edddb75 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 12 Sep 2020 09:26:41 +0700 Subject: [PATCH 52/52] doc: merge example/readme.md into docs/getting_started.md --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- docs/getting_started.md | 114 ++++++++++++++++++++++++--- examples/readme.md | 104 ------------------------ 3 files changed, 103 insertions(+), 117 deletions(-) delete mode 100644 examples/readme.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 5160e9f84..9f956f9d8 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -26,4 +26,4 @@ Steps to reproduce the behavior: If applicable, add screenshots, bus capture to help explain your problem. **Log** -Please provide the stack's log (uart/rtt/swo) where the issue occurred, best with comments to explain the actual events. To enable logging, add `LOG=2` to to the make command if building with stock examples or set `CFG_TUSB_DEBUG=2` in your tusb_config.h. More information can be found at [example's readme](/examples/readme.md) +Please provide the stack's log (uart/rtt/swo) where the issue occurred, best with comments to explain the actual events. To enable logging, add `LOG=2` to to the make command if building with stock examples or set `CFG_TUSB_DEBUG=2` in your tusb_config.h. More information can be found at [example's readme](/docs/getting_started.md) diff --git a/docs/getting_started.md b/docs/getting_started.md index e282b4171..8d22a3127 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -1,14 +1,6 @@ # Getting Started # -## Get - -``` -git clone git@github.com:hathach/tinyusb.git tinyusb -``` - -*examples* is the folder where all the application & project files are located. There are demos for both device and hosts. For each, there are different projects for each of supported RTOS. Click to have more information on how to [build](../examples/readme.md) and run [device](../examples/device/readme.md) demos. - -## Add tinyusb to your project +## Add TinyUSB to your project It is relatively simple to incorporate tinyusb to your (existing) project @@ -37,6 +29,104 @@ int main(void) } ~~~ -[//]: # "\subpage md_boards_readme" -[//]: # "\subpage md_doxygen_started_demo" -[//]: # "\subpage md_tools_readme" +## Examples + +For your convenience, TinyUSB contains a handful of examples for both host and device with/without RTOS to quickly test the functionality as well as demonstrate how API() should be used. Most examples will work on most of [the supported Boards](boards.md). Firstly we need to `git clone` if not already + +``` +$ git clone https://github.com/hathach/tinyusb tinyusb +$ cd tinyusb +``` + +TinyUSB examples includes external repos aka submodules to provide low-level MCU peripheral's driver as well as external libraries such as FreeRTOS to compile with. Therefore we will firstly fetch those mcu driver repo by running this command in the top folder repo + +``` +$ git submodule update --init --recursive +``` + +It will takes a bit of time due to the number of supported MCUs, luckily we only need to do this once. Or if you only want to test with a specific mcu, you could only fetch its driver submodule. + +### Build + +To build example, first change directory to an example folder. + +``` +$ cd examples/device/cdc_msc +``` + +Then compile with `make BOARD=[board_name] all`, for example + +``` +$ make BOARD=feather_nrf52840_express all +``` + +#### Port Selection + +If a board has several ports, one port is chosen by default in the individual board.mk file. Use option `PORT=x` To choose another port. For example to select the HS port of a STM32F746Disco board, use: + +``` +$ make BOARD=stm32f746disco PORT=1 all +``` + +#### Port Speed + +A MCU can support multiple operational speed. By default, the example build system will use the fastest supported on the board. Use option `SPEED=full/high` e.g To force F723 operate at full instead of default high speed + +``` +$ make BOARD=stm32f746disco SPEED=full all +``` + +### Debug + +To compile for debugging add `DEBUG=1`, for example + +``` +$ make BOARD=feather_nrf52840_express DEBUG=1 all +``` + +#### Log + +Should you have an issue running example and/or submitting an bug report. You could enable TinyUSB built-in debug logging with optional `LOG=`. LOG=1 will only print out error message, LOG=2 print more information with on-going events. LOG=3 or higher is not used yet. + +``` +$ make BOARD=feather_nrf52840_express LOG=2 all +``` + +#### Logger + +By default log message is printed via on-board UART which is slow and take lots of CPU time comparing to USB speed. If your board support on-board/external debugger, it would be more efficient to use it for logging. There are 2 protocols: + +- `LOGGER=rtt`: use [Segger RTT protocol](https://www.segger.com/products/debug-probes/j-link/technology/about-real-time-transfer/) + - Cons: requires jlink as the debugger. + - Pros: work with most if not all MCUs + - Software viewer is JLink RTT Viewer/Client/Logger which is bundled with JLink driver package. +- `LOGGER=swo`: Use dedicated SWO pin of ARM Cortex SWD debug header. + - Cons: only work with ARM Cortex MCUs minus M0 + - Pros: should be compatible with more debugger that support SWO. + - Software viewer should be provided along with your debugger driver. + +``` +$ make BOARD=feather_nrf52840_express LOG=2 LOGGER=rtt all +$ make BOARD=feather_nrf52840_express LOG=2 LOGGER=swo all +``` + +### Flash + +`flash` target will use the default on-board debugger (jlink/cmsisdap/stlink/dfu) to flash the binary, please install those support software in advance. Some board use bootloader/DFU via serial which is required to pass to make command + +``` +$ make BOARD=feather_nrf52840_express flash +$ make SERIAL=/dev/ttyACM0 BOARD=feather_nrf52840_express flash +``` + +Since jlink can be used with most of the boards, there is also `flash-jlink` target for your convenience. + +``` +$ make BOARD=feather_nrf52840_express flash-jlink +``` + +Some board use uf2 bootloader for drag & drop in to mass storage device, uf2 can be generated with `uf2` target + +``` +$ make BOARD=feather_nrf52840_express all uf2 +``` diff --git a/examples/readme.md b/examples/readme.md deleted file mode 100644 index f6cd380b6..000000000 --- a/examples/readme.md +++ /dev/null @@ -1,104 +0,0 @@ -# Examples - -## Clone this repo - -``` -$ git clone https://github.com/hathach/tinyusb tinyusb -$ cd tinyusb -``` - -## Fetch submodule MCUs drivers - -TinyUSB examples includes external repos aka submodules to provide low-level MCU peripheral's driver to compile with. Therefore we will firstly fetch those mcu driver repo by running this command in the top folder repo - -``` -$ git submodule update --init --recursive -``` - -It will takes a bit of time due to the number of supported MCUs, luckily we only need to do this once. Or if you only want to test with a specific mcu, you could only update its driver submodule - -## Build - -[Here is the list of supported Boards](docs/boards.md) that should work out of the box with provided examples (hopefully). -To build example, first change directory to example folder. - -``` -$ cd examples/device/cdc_msc -``` - -Then compile with `make BOARD=[your_board] all`, for example - -``` -$ make BOARD=feather_nrf52840_express all -``` - -**Port Selection** - -If a board has several ports, one port is chosen by default in the individual board.mk file. Use option `PORT=x` To choose another port. For example to select the HS port of a STM32F746Disco board, use: - -``` -$ make BOARD=stm32f746disco PORT=1 all -``` - -**Port Speed** - -A MCU can support multiple operational speed. By default, the example build system will use the fastest supported on the board. Use option `SPEED=full/high` e.g To force F723 operate at full instead of default high speed - -``` -$ make BOARD=stm32f746disco SPEED=full all -``` - -### Debug - -To compile for debugging with debug symbols add `DEBUG=1`, for example - -``` -$ make BOARD=feather_nrf52840_express DEBUG=1 all -``` - -#### Log - -Should you have an issue running example and/or submitting an bug report. You could enable TinyUSB built-in debug logging with optional `LOG=`. LOG=1 will only print out error message, LOG=2 print more information with on-going events. LOG=3 or higher is not used yet. - -``` -$ make BOARD=feather_nrf52840_express LOG=2 all -``` - -#### Logger - -By default log message is printed via on-board UART which is slow and take lots of CPU time comparing to USB speed. If your board support on-board/external debugger, it would be more efficient to use it for logging. There are 2 protocols: - -- `LOGGER=rtt`: use [Segger RTT protocol](https://www.segger.com/products/debug-probes/j-link/technology/about-real-time-transfer/) - - Cons: requires jlink as the debugger. - - Pros: work with most if not all MCUs - - Software viewer is JLink RTT Viewer/Client/Logger which is bundled with JLink driver package. -- `LOGGER=swo`: Use dedicated SWO pin of ARM Cortex SWD debug header. - - Cons: only work with ARM Cortex MCUs minus M0 - - Pros: should be compatible with more debugger that support SWO. - - Software viewer should be provided along with your debugger driver. - -``` -$ make BOARD=feather_nrf52840_express LOG=2 LOGGER=rtt all -$ make BOARD=feather_nrf52840_express LOG=2 LOGGER=swo all -``` - -## Flash - -`flash` target will use the default on-board debugger (jlink/cmsisdap/stlink/dfu) to flash the binary, please install those support software in advance. Some board use bootloader/DFU via serial which is required to pass to make command - -``` -$ make BOARD=feather_nrf52840_express flash -$ make SERIAL=/dev/ttyACM0 BOARD=feather_nrf52840_express flash -``` - -Since jlink can be used with most of the boards, there is also `flash-jlink` target for your convenience. - -``` -$ make BOARD=feather_nrf52840_express flash-jlink -``` - -Some board use uf2 bootloader for drag & drop in to mass storage device, uf2 can be generated with `uf2` target - -``` -$ make BOARD=feather_nrf52840_express all uf2 -```