From 064eec5dd8e27b1f8e88b24a5d5071bbd8458a29 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 26 Nov 2018 12:25:28 +0700 Subject: clean up warnings --- src/device/usbd.c | 14 +++++++------- src/device/usbd_control.c | 6 +++++- 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src/device') diff --git a/src/device/usbd.c b/src/device/usbd.c index 8c7e588e0..81fcfe959 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -170,7 +170,7 @@ static osal_queue_t _usbd_q; //--------------------------------------------------------------------+ static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request); -static bool process_set_config(uint8_t rhport, uint8_t config_number); +static bool process_set_config(uint8_t rhport); static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len); void usbd_control_reset (uint8_t rhport); @@ -356,7 +356,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const dcd_set_config(rhport, config); _usbd_dev.config_num = config; - TU_ASSERT( TUSB_ERROR_NONE == process_set_config(rhport, config) ); + TU_ASSERT( TUSB_ERROR_NONE == process_set_config(rhport) ); } break; @@ -427,7 +427,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // Process Set Configure Request // This function parse configuration descriptor & open drivers accordingly -static bool process_set_config(uint8_t rhport, uint8_t config_number) +static bool process_set_config(uint8_t rhport) { uint8_t const * desc_cfg = (uint8_t const *) usbd_desc_set->config; TU_ASSERT(desc_cfg != NULL); @@ -591,23 +591,23 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) // helper to send bus signal event void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr) { - dcd_event_t event = { .rhport = 0, .event_id = eid, }; + dcd_event_t event = { .rhport = rhport, .event_id = eid, }; dcd_event_handler(&event, in_isr); } // helper to send setup received void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr) { - dcd_event_t event = { .rhport = 0, .event_id = DCD_EVENT_SETUP_RECEIVED }; + dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED }; memcpy(&event.setup_received, setup, 8); - dcd_event_handler(&event, true); + dcd_event_handler(&event, in_isr); } // helper to send transfer complete event void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr) { - dcd_event_t event = { .rhport = 0, .event_id = DCD_EVENT_XFER_COMPLETE }; + dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE }; event.xfer_complete.ep_addr = ep_addr; event.xfer_complete.len = xferred_bytes; diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index 31d74edf0..ebeaaaa4d 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -68,6 +68,7 @@ CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN uint8_t _usbd_ctrl_buf[CFG_TUD_ENDOINT0_ void usbd_control_reset (uint8_t rhport) { + (void) rhport; tu_varclr(&_control_state); } @@ -126,8 +127,11 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v } // callback when a transaction complete on DATA stage of control endpoint -bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { + (void) result; + (void) ep_addr; + if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_OUT ) { memcpy(_control_state.buffer, _usbd_ctrl_buf, xferred_bytes); -- cgit v1.3.1 From 8b03b6d3b45167b8db805ca95aafce92a1a2e4b0 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 28 Nov 2018 16:10:31 +0700 Subject: add Auto descriptor endpoint num config to support lpc17xx CFG_TUD_DESC_*_EPNUM --- examples/device/cdc_msc_hid/src/tusb_config.h | 15 +++++-- src/class/msc/msc_device.h | 10 ----- src/device/usbd_auto_desc.c | 63 +++++++++++++++++++-------- 3 files changed, 57 insertions(+), 31 deletions(-) (limited to 'src/device') diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h index 0634c9576..a9a356f20 100644 --- a/examples/device/cdc_msc_hid/src/tusb_config.h +++ b/examples/device/cdc_msc_hid/src/tusb_config.h @@ -52,7 +52,7 @@ // defined by compiler flags for flexibility #ifndef CFG_TUSB_MCU - #error CFG_TUSB_MCU should be defined using compiler flags + #error CFG_TUSB_MCU must be defined #endif #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX @@ -93,12 +93,22 @@ */ #define CFG_TUD_DESC_AUTO 1 -/* USB VID/PID if not defined, tinyusb to use default value +/* If USB VID/PID is not defined, tinyusb will use default value * Note: different class combination e.g CDC and (CDC + MSC) should have different * PID since Host OS will "remembered" device driver after the first plug */ // #define CFG_TUD_DESC_VID 0xCAFE // #define CFG_TUD_DESC_PID 0x0001 +// LPC175x_6x's endpoint type (bulk/interrupt/iso) are fixed by its number +// Therefor we need to force endpoint number to correct type on lpc17xx +#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X +#define CFG_TUD_DESC_CDC_EPNUM_NOTIF 1 +#define CFG_TUD_DESC_CDC_EPNUM 2 +#define CFG_TUD_DESC_MSC_EPNUM 5 +#define CFG_TUD_DESC_HID_KEYBOARD_EPNUM 4 +#define CFG_TUD_DESC_HID_MOUSE_EPNUM 7 +#endif + //------------- CLASS -------------// #define CFG_TUD_CDC 1 #define CFG_TUD_MSC 1 @@ -114,7 +124,6 @@ #define CFG_TUD_HID_KEYBOARD_BOOT 1 #define CFG_TUD_HID_MOUSE_BOOT 1 - //-------------------------------------------------------------------- // CDC //-------------------------------------------------------------------- diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index b268830ca..4053679b8 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -71,16 +71,6 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct"); #error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined #endif -// TODO highspeed device is 512 -#ifndef CFG_TUD_MSC_EPSIZE -#if TUD_OPT_HIGH_SPEED -#define CFG_TUD_MSC_EPSIZE 512 -#else -#define CFG_TUD_MSC_EPSIZE 64 -#endif -#endif - - #ifdef __cplusplus extern "C" { #endif diff --git a/src/device/usbd_auto_desc.c b/src/device/usbd_auto_desc.c index 2d54a934d..028d1b433 100644 --- a/src/device/usbd_auto_desc.c +++ b/src/device/usbd_auto_desc.c @@ -101,18 +101,19 @@ enum ITF_NUM_TOTAL }; -enum { - ITF_STR_LANGUAGE = 0 , - ITF_STR_MANUFACTURER , - ITF_STR_PRODUCT , - ITF_STR_SERIAL , +enum +{ + ITF_STR_LANGUAGE = 0 , + ITF_STR_MANUFACTURER , + ITF_STR_PRODUCT , + ITF_STR_SERIAL , #if CFG_TUD_CDC - ITF_STR_CDC , + ITF_STR_CDC , #endif #if CFG_TUD_MSC - ITF_STR_MSC , + ITF_STR_MSC , #endif #if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT @@ -133,23 +134,51 @@ enum { #define _EP_OUT(x) (x) // CDC -#define EP_CDC_NOTIF _EP_IN ( ITF_NUM_CDC+1 ) +#ifdef CFG_TUD_DESC_CDC_EPNUM_NOTIF + #define EP_CDC_NOTIF _EP_IN (CFG_TUD_DESC_CDC_EPNUM_NOTIF) +#else + #define EP_CDC_NOTIF _EP_IN ( ITF_NUM_CDC+1 ) +#endif #define EP_CDC_NOTIF_SIZE 8 -#define EP_CDC_OUT _EP_OUT( ITF_NUM_CDC+2 ) -#define EP_CDC_IN _EP_IN ( ITF_NUM_CDC+2 ) +#ifdef CFG_TUD_DESC_CDC_EPNUM + #define EP_CDC_OUT _EP_OUT( CFG_TUD_DESC_CDC_EPNUM ) + #define EP_CDC_IN _EP_IN ( CFG_TUD_DESC_CDC_EPNUM ) +#else + #define EP_CDC_OUT _EP_OUT( ITF_NUM_CDC+2 ) + #define EP_CDC_IN _EP_IN ( ITF_NUM_CDC+2 ) +#endif // Mass Storage -#define EP_MSC_OUT _EP_OUT( ITF_NUM_MSC+1 ) -#define EP_MSC_IN _EP_IN ( ITF_NUM_MSC+1 ) +#ifdef CFG_TUD_DESC_MSC_EPNUM + #define EP_MSC_OUT _EP_OUT( CFG_TUD_DESC_MSC_EPNUM ) + #define EP_MSC_IN _EP_IN ( CFG_TUD_DESC_MSC_EPNUM ) +#else + #define EP_MSC_OUT _EP_OUT( ITF_NUM_MSC+1 ) + #define EP_MSC_IN _EP_IN ( ITF_NUM_MSC+1 ) +#endif + +#if TUD_OPT_HIGH_SPEED +#define EP_MSC_SIZE 512 +#else +#define EP_MSC_SIZE 64 +#endif // HID Keyboard with boot protocol -#define EP_HID_KBD_BOOT _EP_IN ( ITF_NUM_HID_BOOT_KBD+1 ) +#ifdef CFG_TUD_DESC_HID_KEYBOARD_EPNUM + #define EP_HID_KBD_BOOT _EP_IN ( CFG_TUD_DESC_HID_KEYBOARD_EPNUM ) +#else + #define EP_HID_KBD_BOOT _EP_IN ( ITF_NUM_HID_BOOT_KBD+1 ) +#endif #define EP_HID_KBD_BOOT_SZ 8 // HID Mouse with boot protocol -#define EP_HID_MSE_BOOT _EP_IN ( ITF_NUM_HID_BOOT_MSE+1 ) +#ifdef CFG_TUD_DESC_HID_MOUSE_EPNUM + #define EP_HID_MSE_BOOT _EP_IN ( CFG_TUD_DESC_HID_MOUSE_EPNUM ) +#else + #define EP_HID_MSE_BOOT _EP_IN ( ITF_NUM_HID_BOOT_MSE+1 ) +#endif #define EP_HID_MSE_BOOT_SZ 8 // HID composite = keyboard + mouse + gamepad + etc ... @@ -466,7 +495,7 @@ desc_auto_cfg_t const _desc_auto_config_struct = .bDescriptorType = TUSB_DESC_ENDPOINT, .bEndpointAddress = EP_MSC_OUT, .bmAttributes = { .xfer = TUSB_XFER_BULK }, - .wMaxPacketSize = { .size = CFG_TUD_MSC_EPSIZE}, + .wMaxPacketSize = { .size = EP_MSC_SIZE}, .bInterval = 1 }, @@ -476,7 +505,7 @@ desc_auto_cfg_t const _desc_auto_config_struct = .bDescriptorType = TUSB_DESC_ENDPOINT, .bEndpointAddress = EP_MSC_IN, .bmAttributes = { .xfer = TUSB_XFER_BULK }, - .wMaxPacketSize = { .size = CFG_TUD_MSC_EPSIZE}, + .wMaxPacketSize = { .size = EP_MSC_SIZE}, .bInterval = 1 } }, @@ -563,7 +592,6 @@ desc_auto_cfg_t const _desc_auto_config_struct = #endif // boot mouse #if AUTO_DESC_HID_GENERIC - //------------- HID Generic Multiple report -------------// .hid_generic = { @@ -601,7 +629,6 @@ desc_auto_cfg_t const _desc_auto_config_struct = .bInterval = 0x0A } } - #endif // hid generic }; -- cgit v1.3.1 From 6048a3bff48dc588849f83f4088367d20b6bc651 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Dec 2018 12:22:33 +0700 Subject: remove dcd_connect/disconnect since there is no usage now --- doxygen/porting.md | 4 +++- src/device/dcd.h | 12 ++++++------ src/portable/microchip/samd21/dcd_samd21.c | 9 --------- src/portable/microchip/samd51/dcd_samd51.c | 9 --------- src/portable/nordic/nrf5x/dcd_nrf5x.c | 9 --------- src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c | 6 ------ src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 6 ------ src/portable/nxp/lpc18_43/dcd_lpc18_43.c | 5 ----- 8 files changed, 9 insertions(+), 51 deletions(-) (limited to 'src/device') diff --git a/doxygen/porting.md b/doxygen/porting.md index be917d6f7..00aad6a08 100644 --- a/doxygen/porting.md +++ b/doxygen/porting.md @@ -79,7 +79,6 @@ After the USB device is setup, the USB device code works by processing events on All of the code for the low-level device API is in `src/portable///dcd_.c`. #### Device Setup -`dcd_connect`, `dcd_disconnect` and `dcd_set_config` are not currently used and can be left empty. ##### dcd_init Initializes the USB peripheral for device mode and enables it. @@ -89,6 +88,9 @@ Called when the device is given a new bus address. If your peripheral automatically changes address during enumeration (like the nrf52) you may leave this empty and also no queue an event for the corresponding SETUP packet. +##### dcd_set_config +Called when the device received SET_CONFIG request, you can leave this empty if your peripheral does not require any specific action. + #### Special events You must let TinyUSB know when certain events occur so that it can continue its work. There are a few methods you can call to queue events for TinyUSB to process. diff --git a/src/device/dcd.h b/src/device/dcd.h index db84df839..6b1356cbf 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -90,14 +90,14 @@ typedef struct ATTR_ALIGNED(4) TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct"); /*------------------------------------------------------------------*/ -/* Device API (Weak is optional) +/* Device API *------------------------------------------------------------------*/ -bool dcd_init (uint8_t rhport); -void dcd_set_address (uint8_t rhport, uint8_t dev_addr); -void dcd_set_config (uint8_t rhport, uint8_t config_num); +bool dcd_init (uint8_t rhport); +void dcd_int_enable (uint8_t rhport); +void dcd_int_disable(uint8_t rhport); -void dcd_connect (uint8_t rhport) ATTR_WEAK; -void dcd_disconnect (uint8_t rhport) ATTR_WEAK; +void dcd_set_address(uint8_t rhport, uint8_t dev_addr); +void dcd_set_config (uint8_t rhport, uint8_t config_num); /*------------------------------------------------------------------*/ /* Event Function diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index dda75cee8..5436259dd 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -82,15 +82,6 @@ bool dcd_init (uint8_t rhport) return true; } -void dcd_connect (uint8_t rhport) -{ - -} -void dcd_disconnect (uint8_t rhport) -{ - -} - void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index d0ad17f92..0e8b23d83 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -82,15 +82,6 @@ bool dcd_init (uint8_t rhport) return true; } -void dcd_connect (uint8_t rhport) -{ - (void) rhport; -} -void dcd_disconnect (uint8_t rhport) -{ - (void) rhport; -} - void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index e1e53a83e..2118a4bdc 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -195,15 +195,6 @@ bool dcd_init (uint8_t rhport) return true; } -void dcd_connect (uint8_t rhport) -{ - (void) rhport; -} -void dcd_disconnect (uint8_t rhport) -{ - (void) rhport; -} - void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c index 9c5991cef..26674d9e9 100644 --- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c +++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c @@ -157,12 +157,6 @@ bool tusb_hal_init(void) return true; } -void dcd_connect(uint8_t rhport) -{ - (void) rhport; - LPC_USB->DEVCMDSTAT |= CMDSTAT_DEVICE_CONNECT_MASK; -} - void dcd_set_config(uint8_t rhport, uint8_t config_num) { diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index 8504cbf51..8763d1e18 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -202,12 +202,6 @@ bool dcd_init(uint8_t rhport) return TUSB_ERROR_NONE; } -void dcd_connect(uint8_t rhport) -{ - (void) rhport; - sie_write(SIE_CMDCODE_DEVICE_STATUS, 1, 1); -} - void dcd_set_address(uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c index 8f5e3b457..9423b64a9 100644 --- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c @@ -92,11 +92,6 @@ static dcd_data_t* const dcd_data_ptr[2] = //--------------------------------------------------------------------+ // CONTROLLER API //--------------------------------------------------------------------+ -void dcd_connect(uint8_t rhport) -{ - LPC_USB[rhport]->USBCMD_D |= BIT_(0); -} - void dcd_set_address(uint8_t rhport, uint8_t dev_addr) { LPC_USB[rhport]->DEVICEADDR = (dev_addr << 25) | BIT_(24); -- cgit v1.3.1 From a73017fdc23f7bedf38e754cba3a4ffbcf8d4c27 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Dec 2018 13:20:25 +0700 Subject: hal clean up - replace tusb_hal_int_enable/disable to dcd_int_enable/disable, hcd_int_enable/disable - remove tusb_hal_init(), this will be part of dcd_init/hcd_init, anything beyond dcd/hcd should be inited by bsp --- doxygen/porting.md | 17 ++--- hw/bsp/ea4357/board_ea4357.c | 2 + hw/bsp/mcb1800/board_mcb1800.c | 2 + src/device/usbd.c | 21 ++++--- src/portable/microchip/samd21/dcd_samd21.c | 26 ++++++++ src/portable/microchip/samd21/hal_samd21.c | 81 ------------------------ src/portable/microchip/samd51/dcd_samd51.c | 31 ++++++++++ src/portable/microchip/samd51/hal_samd51.c | 86 -------------------------- src/portable/nordic/nrf5x/dcd_nrf5x.c | 12 ++++ src/portable/nordic/nrf5x/hal_nrf5x.c | 21 ------- src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c | 14 ++--- src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 12 ++++ src/portable/nxp/lpc17_40/hal_lpc17_40.c | 20 ------ src/portable/nxp/lpc18_43/dcd_lpc18_43.c | 11 +++- src/portable/nxp/lpc18_43/hal_lpc18_43.c | 15 ----- src/tusb.c | 3 - src/tusb_hal.h | 37 +++-------- 17 files changed, 123 insertions(+), 288 deletions(-) delete mode 100644 src/portable/microchip/samd21/hal_samd21.c delete mode 100644 src/portable/microchip/samd51/hal_samd51.c (limited to 'src/device') diff --git a/doxygen/porting.md b/doxygen/porting.md index 00aad6a08..5a464fa6b 100644 --- a/doxygen/porting.md +++ b/doxygen/porting.md @@ -59,19 +59,6 @@ The OPT_OS_NONE option is the only option which requires an MCU specific functio `tusb_hal_millis` is also provided in `hw/bsp//board_.c` because it may vary with MCU use. -### Hardware Abstraction Layer (HAL) -The hardware abstraction layer is a minimal set of abstractions used in both Device and Host USB modes. - -The HAL implementations are located in `src/portable///hal_.c`. - -#### tusb_hal_init - -The HAL init is responsible for configuring common settings of USB peripheral such as pad calibration. - -#### tusb_hal_int_enable / tusb_hal_int_disable - -Enables or disables the USB interrupt(s). May be used to prevent concurrency issues when mutating data structures shared between main code and the interrupt handler. - ### Device API After the USB device is setup, the USB device code works by processing events on the main thread (by calling `tusb_task`). These events are queued by the USB interrupt handler. So, there are three parts to the device low-level API: device setup, endpoint setup and interrupt processing. @@ -83,6 +70,10 @@ All of the code for the low-level device API is in `src/portable//USBCMD_D |= 0x02; + while( LPC_USB0->USBCMD_D & 0x02 ) {} // Set mode #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST @@ -204,6 +205,7 @@ void board_init(void) // Reset controller LPC_USB1->USBCMD_D |= 0x02; + while( LPC_USB1->USBCMD_D & 0x02 ) {} // Set mode #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST diff --git a/hw/bsp/mcb1800/board_mcb1800.c b/hw/bsp/mcb1800/board_mcb1800.c index 5b7a846ef..8474fbff3 100644 --- a/hw/bsp/mcb1800/board_mcb1800.c +++ b/hw/bsp/mcb1800/board_mcb1800.c @@ -159,6 +159,7 @@ void board_init(void) // Reset controller LPC_USB0->USBCMD_D |= 0x02; + while( LPC_USB0->USBCMD_D & 0x02 ) {} // Set mode #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST @@ -175,6 +176,7 @@ void board_init(void) // Reset controller LPC_USB1->USBCMD_D |= 0x02; + while( LPC_USB1->USBCMD_D & 0x02 ) {} // Set mode #if CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST diff --git a/src/device/usbd.c b/src/device/usbd.c index 81fcfe959..ddb63cebb 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -190,23 +190,26 @@ bool tud_mounted(void) //--------------------------------------------------------------------+ tusb_error_t usbd_init (void) { + // Init device queue & task + _usbd_q = osal_queue_create(&_usbd_qdef); + TU_VERIFY(_usbd_q, TUSB_ERROR_OSAL_QUEUE_FAILED); + + osal_task_create(&_usbd_task_def); + + // Init class drivers + for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init(); + + // Init device controller driver #if (CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE) dcd_init(0); + dcd_int_enable(0); #endif #if (CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE) dcd_init(1); + dcd_int_enable(1); #endif - //------------- Task init -------------// - _usbd_q = osal_queue_create(&_usbd_qdef); - TU_VERIFY(_usbd_q, TUSB_ERROR_OSAL_QUEUE_FAILED); - - osal_task_create(&_usbd_task_def); - - //------------- class init -------------// - for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init(); - return TUSB_ERROR_NONE; } diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index 5436259dd..57c4c4d9d 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -72,6 +72,20 @@ static void bus_reset(void) { bool dcd_init (uint8_t rhport) { (void) rhport; + + // Reset to get in a clean state. + USB->DEVICE.CTRLA.bit.SWRST = true; + while (USB->DEVICE.SYNCBUSY.bit.SWRST == 0) {} + while (USB->DEVICE.SYNCBUSY.bit.SWRST == 1) {} + + USB->DEVICE.PADCAL.bit.TRANSP = (*((uint32_t*) USB_FUSES_TRANSP_ADDR) & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos; + USB->DEVICE.PADCAL.bit.TRANSN = (*((uint32_t*) USB_FUSES_TRANSN_ADDR) & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos; + USB->DEVICE.PADCAL.bit.TRIM = (*((uint32_t*) USB_FUSES_TRIM_ADDR) & USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos; + + USB->DEVICE.QOSCTRL.bit.CQOS = USB_QOSCTRL_CQOS_HIGH_Val; + USB->DEVICE.QOSCTRL.bit.DQOS = USB_QOSCTRL_DQOS_HIGH_Val; + + // Configure registers USB->DEVICE.DESCADD.reg = (uint32_t) &sram_registers; USB->DEVICE.CTRLB.reg = USB_DEVICE_CTRLB_SPDCONF_FS; USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY; @@ -82,6 +96,18 @@ bool dcd_init (uint8_t rhport) return true; } +void dcd_int_enable(uint8_t rhport) +{ + (void) rhport; + NVIC_EnableIRQ(USB_IRQn); +} + +void dcd_int_disable(uint8_t rhport) +{ + (void) rhport; + NVIC_DisableIRQ(USB_IRQn); +} + void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/microchip/samd21/hal_samd21.c b/src/portable/microchip/samd21/hal_samd21.c deleted file mode 100644 index 9f04630ea..000000000 --- a/src/portable/microchip/samd21/hal_samd21.c +++ /dev/null @@ -1,81 +0,0 @@ -/**************************************************************************/ -/*! - @file hal_nrf5x.c - @author hathach - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2018, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "tusb_option.h" - -#if TUSB_OPT_DEVICE_ENABLED && CFG_TUSB_MCU == OPT_MCU_SAMD21 - -#include "sam.h" - -#include "tusb_hal.h" - - -/*------------------------------------------------------------------*/ -/* TUSB HAL - *------------------------------------------------------------------*/ -bool tusb_hal_init(void) -{ - // Reset to get in a clean state. - USB->DEVICE.CTRLA.bit.SWRST = true; - while (USB->DEVICE.SYNCBUSY.bit.SWRST == 0) {} - while (USB->DEVICE.SYNCBUSY.bit.SWRST == 1) {} - - USB->DEVICE.PADCAL.bit.TRANSP = (*((uint32_t*) USB_FUSES_TRANSP_ADDR) & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos; - USB->DEVICE.PADCAL.bit.TRANSN = (*((uint32_t*) USB_FUSES_TRANSN_ADDR) & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos; - USB->DEVICE.PADCAL.bit.TRIM = (*((uint32_t*) USB_FUSES_TRIM_ADDR) & USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos; - - USB->DEVICE.QOSCTRL.bit.CQOS = USB_QOSCTRL_CQOS_HIGH_Val; - USB->DEVICE.QOSCTRL.bit.DQOS = USB_QOSCTRL_DQOS_HIGH_Val; - - tusb_hal_int_enable(0); - return true; -} - -void tusb_hal_int_enable(uint8_t rhport) -{ - (void) rhport; - NVIC_EnableIRQ(USB_IRQn); -} - -void tusb_hal_int_disable(uint8_t rhport) -{ - (void) rhport; - NVIC_DisableIRQ(USB_IRQn); -} - -#endif diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index 0e8b23d83..ba53d5598 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -73,6 +73,19 @@ bool dcd_init (uint8_t rhport) { (void) rhport; + // Reset to get in a clean state. + USB->DEVICE.CTRLA.bit.SWRST = true; + while (USB->DEVICE.SYNCBUSY.bit.SWRST == 0) {} + while (USB->DEVICE.SYNCBUSY.bit.SWRST == 1) {} + + USB->DEVICE.PADCAL.bit.TRANSP = (*((uint32_t*) USB_FUSES_TRANSP_ADDR) & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos; + USB->DEVICE.PADCAL.bit.TRANSN = (*((uint32_t*) USB_FUSES_TRANSN_ADDR) & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos; + USB->DEVICE.PADCAL.bit.TRIM = (*((uint32_t*) USB_FUSES_TRIM_ADDR) & USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos; + + USB->DEVICE.QOSCTRL.bit.CQOS = 3; + USB->DEVICE.QOSCTRL.bit.DQOS = 3; + + // Configure registers USB->DEVICE.DESCADD.reg = (uint32_t) &sram_registers; USB->DEVICE.CTRLB.reg = USB_DEVICE_CTRLB_SPDCONF_FS; USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY; @@ -82,6 +95,24 @@ bool dcd_init (uint8_t rhport) return true; } +void dcd_int_enable(uint8_t rhport) +{ + (void) rhport; + NVIC_EnableIRQ(USB_0_IRQn); + NVIC_EnableIRQ(USB_1_IRQn); + NVIC_EnableIRQ(USB_2_IRQn); + NVIC_EnableIRQ(USB_3_IRQn); +} + +void dcd_int_disable(uint8_t rhport) +{ + (void) rhport; + NVIC_DisableIRQ(USB_3_IRQn); + NVIC_DisableIRQ(USB_2_IRQn); + NVIC_DisableIRQ(USB_1_IRQn); + NVIC_DisableIRQ(USB_0_IRQn); +} + void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/microchip/samd51/hal_samd51.c b/src/portable/microchip/samd51/hal_samd51.c deleted file mode 100644 index 49212b74c..000000000 --- a/src/portable/microchip/samd51/hal_samd51.c +++ /dev/null @@ -1,86 +0,0 @@ -/**************************************************************************/ -/*! - @file hal_nrf5x.c - @author hathach - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2018, hathach (tinyusb.org) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - This file is part of the tinyusb stack. -*/ -/**************************************************************************/ - -#include "tusb_option.h" - -#if TUSB_OPT_DEVICE_ENABLED && CFG_TUSB_MCU == OPT_MCU_SAMD51 - -#include "sam.h" - -#include "tusb_hal.h" - -/*------------------------------------------------------------------*/ -/* TUSB HAL - *------------------------------------------------------------------*/ -bool tusb_hal_init(void) -{ - // Reset to get in a clean state. - USB->DEVICE.CTRLA.bit.SWRST = true; - while (USB->DEVICE.SYNCBUSY.bit.SWRST == 0) {} - while (USB->DEVICE.SYNCBUSY.bit.SWRST == 1) {} - - USB->DEVICE.PADCAL.bit.TRANSP = (*((uint32_t*) USB_FUSES_TRANSP_ADDR) & USB_FUSES_TRANSP_Msk) >> USB_FUSES_TRANSP_Pos; - USB->DEVICE.PADCAL.bit.TRANSN = (*((uint32_t*) USB_FUSES_TRANSN_ADDR) & USB_FUSES_TRANSN_Msk) >> USB_FUSES_TRANSN_Pos; - USB->DEVICE.PADCAL.bit.TRIM = (*((uint32_t*) USB_FUSES_TRIM_ADDR) & USB_FUSES_TRIM_Msk) >> USB_FUSES_TRIM_Pos; - - USB->DEVICE.QOSCTRL.bit.CQOS = 3; - USB->DEVICE.QOSCTRL.bit.DQOS = 3; - - tusb_hal_int_enable(0); - return true; -} - -void tusb_hal_int_enable(uint8_t rhport) -{ - (void) rhport; - NVIC_EnableIRQ(USB_0_IRQn); - NVIC_EnableIRQ(USB_1_IRQn); - NVIC_EnableIRQ(USB_2_IRQn); - NVIC_EnableIRQ(USB_3_IRQn); -} - -void tusb_hal_int_disable(uint8_t rhport) -{ - (void) rhport; - NVIC_DisableIRQ(USB_3_IRQn); - NVIC_DisableIRQ(USB_2_IRQn); - NVIC_DisableIRQ(USB_1_IRQn); - NVIC_DisableIRQ(USB_0_IRQn); -} - -#endif diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 2118a4bdc..c7a4f413f 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -195,6 +195,18 @@ bool dcd_init (uint8_t rhport) return true; } +void dcd_int_enable(uint8_t rhport) +{ + (void) rhport; + NVIC_EnableIRQ(USBD_IRQn); +} + +void dcd_int_disable(uint8_t rhport) +{ + (void) rhport; + NVIC_DisableIRQ(USBD_IRQn); +} + void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/nordic/nrf5x/hal_nrf5x.c b/src/portable/nordic/nrf5x/hal_nrf5x.c index f40e904d7..4cbcd1788 100644 --- a/src/portable/nordic/nrf5x/hal_nrf5x.c +++ b/src/portable/nordic/nrf5x/hal_nrf5x.c @@ -128,27 +128,6 @@ static void hfclk_disable(void) nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP); } - -/*------------------------------------------------------------------*/ -/* TUSB HAL - *------------------------------------------------------------------*/ -bool tusb_hal_init(void) -{ - return true; -} - -void tusb_hal_int_enable(uint8_t rhport) -{ - (void) rhport; - NVIC_EnableIRQ(USBD_IRQn); -} - -void tusb_hal_int_disable(uint8_t rhport) -{ - (void) rhport; - NVIC_DisableIRQ(USBD_IRQn); -} - /*------------------------------------------------------------------*/ /* Controller Start up Sequence (USBD 51.4 specs) *------------------------------------------------------------------*/ diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c index 26674d9e9..69c2759de 100644 --- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c +++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c @@ -139,24 +139,18 @@ static inline uint8_t ep_addr2id(uint8_t endpoint_addr) //--------------------------------------------------------------------+ // CONTROLLER API //--------------------------------------------------------------------+ -void tusb_hal_int_enable(uint8_t rhport) +void dcd_int_enable(uint8_t rhport) { - (void) rhport; // discard compiler's warning + (void) rhport; NVIC_EnableIRQ(USB0_IRQn); } -void tusb_hal_int_disable(uint8_t rhport) +void dcd_int_disable(uint8_t rhport) { - (void) rhport; // discard compiler's warning + (void) rhport; NVIC_DisableIRQ(USB0_IRQn); } -bool tusb_hal_init(void) -{ - // TODO remove - return true; -} - void dcd_set_config(uint8_t rhport, uint8_t config_num) { diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index 8763d1e18..84eb0c121 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -202,6 +202,18 @@ bool dcd_init(uint8_t rhport) return TUSB_ERROR_NONE; } +void dcd_int_enable(uint8_t rhport) +{ + (void) rhport; + NVIC_EnableIRQ(USB_IRQn); +} + +void dcd_int_disable(uint8_t rhport) +{ + (void) rhport; + NVIC_DisableIRQ(USB_IRQn); +} + void dcd_set_address(uint8_t rhport, uint8_t dev_addr) { (void) rhport; diff --git a/src/portable/nxp/lpc17_40/hal_lpc17_40.c b/src/portable/nxp/lpc17_40/hal_lpc17_40.c index 70df70510..7509dac58 100644 --- a/src/portable/nxp/lpc17_40/hal_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/hal_lpc17_40.c @@ -42,26 +42,6 @@ #include "chip.h" -void tusb_hal_int_enable(uint8_t rhport) -{ - (void) rhport; - NVIC_EnableIRQ(USB_IRQn); -} - -void tusb_hal_int_disable(uint8_t rhport) -{ - (void) rhport; - NVIC_DisableIRQ(USB_IRQn); -} - -//--------------------------------------------------------------------+ -// IMPLEMENTATION -//--------------------------------------------------------------------+ -bool tusb_hal_init(void) -{ - return true; -} - void USB_IRQHandler(void) { extern void hal_dcd_isr(uint8_t rhport); diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c index 9423b64a9..7f6442f8c 100644 --- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c @@ -158,10 +158,17 @@ bool dcd_init(uint8_t rhport) lpc_usb->USBCMD_D &= ~0x00FF0000; // Interrupt Threshold Interval = 0 lpc_usb->USBCMD_D |= BIT_(0); // connect - // enable interrupt + return true; +} + +void dcd_int_enable(uint8_t rhport) +{ NVIC_EnableIRQ(rhport ? USB1_IRQn : USB0_IRQn); +} - return true; +void dcd_int_disable(uint8_t rhport) +{ + NVIC_DisableIRQ(rhport ? USB1_IRQn : USB0_IRQn); } //--------------------------------------------------------------------+ diff --git a/src/portable/nxp/lpc18_43/hal_lpc18_43.c b/src/portable/nxp/lpc18_43/hal_lpc18_43.c index 152072cab..2978bebf6 100644 --- a/src/portable/nxp/lpc18_43/hal_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/hal_lpc18_43.c @@ -42,21 +42,6 @@ #include "chip.h" -void tusb_hal_int_enable(uint8_t rhport) -{ - NVIC_EnableIRQ(rhport ? USB1_IRQn : USB0_IRQn); -} - -void tusb_hal_int_disable(uint8_t rhport) -{ - NVIC_DisableIRQ(rhport ? USB1_IRQn : USB0_IRQn); -} - -bool tusb_hal_init(void) -{ - return true; -} - void hal_dcd_isr(uint8_t rhport); #if CFG_TUSB_RHPORT0_MODE diff --git a/src/tusb.c b/src/tusb.c index e9db184f2..81c4907fb 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -47,14 +47,11 @@ static bool _initialized = false; - tusb_error_t tusb_init(void) { // skip if already initialized if (_initialized) return TUSB_ERROR_NONE; - TU_VERIFY( tusb_hal_init(), TUSB_ERROR_FAILED ) ; // hardware init - #if MODE_HOST_SUPPORTED TU_ASSERT_ERR( usbh_init() ); // host stack init #endif diff --git a/src/tusb_hal.h b/src/tusb_hal.h index 85a2a8709..301e8c2f2 100644 --- a/src/tusb_hal.h +++ b/src/tusb_hal.h @@ -51,55 +51,37 @@ extern "C" { //--------------------------------------------------------------------+ // HAL API //--------------------------------------------------------------------+ -/** \ingroup group_mcu - * \defgroup group_hal Hardware Abtract Layer (HAL) - * Hardware Abstraction Layer (HAL) is an abstraction layer, between the physical hardware and the tinyusb stack. - * Its function is to hide differences in hardware from most of MCUs, so that most of the stack code does not need to be changed to - * run on systems with a different MCU. - * HAL are sets of routines that emulate some platform-specific details, giving programs direct access to the hardware resources. - * @{ */ - -/** \brief Initialize USB controller hardware - * \returns true if succeeded - * \note This function is invoked by \ref tusb_init as part of the initialization. - */ -bool tusb_hal_init(void); - -/** \brief Enable USB Interrupt on a specific USB Controller - * \param[in] rhport is a zero-based index to identify USB controller's ID - */ -void tusb_hal_int_enable(uint8_t rhport); - -/** \brief Disable USB Interrupt on a specific USB Controller - * \param[in] rhport is a zero-based index to identify USB controller's ID - */ -void tusb_hal_int_disable(uint8_t rhport); // Only required to implement if using No RTOS (osal_none) uint32_t tusb_hal_millis(void); +// TODO remove +extern void dcd_int_enable (uint8_t rhport); +extern void dcd_int_disable(uint8_t rhport); // Enable all ports' interrupt +// TODO remove static inline void tusb_hal_int_enable_all(void) { #ifdef CFG_TUSB_RHPORT0_MODE - tusb_hal_int_enable(0); + dcd_int_enable(0); #endif #ifdef CFG_TUSB_RHPORT0_MODE - tusb_hal_int_enable(1); + dcd_int_enable(1); #endif } // Disable all ports' interrupt +// TODO remove static inline void tusb_hal_int_disable_all(void) { #ifdef CFG_TUSB_RHPORT0_MODE - tusb_hal_int_disable(0); + dcd_int_disable(0); #endif #ifdef CFG_TUSB_RHPORT0_MODE - tusb_hal_int_disable(1); + dcd_int_disable(1); #endif } @@ -111,4 +93,3 @@ static inline void tusb_hal_int_disable_all(void) #endif /* _TUSB_HAL_H_ */ -/** @} */ -- cgit v1.3.1 From 6f3898572d6f8eb04587f057b74f4f86e8a18856 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Dec 2018 17:01:19 +0700 Subject: add role to OSAL_QUEUE_DEF() to disable correct dcd/hcd isr --- .../device/cdc_msc_hid/ses/samd21/samd21.emProject | 1 + src/common/tusb_fifo.h | 3 +- src/device/usbd.c | 32 ++++---- src/osal/osal_freertos.h | 4 +- src/osal/osal_mynewt.h | 4 +- src/osal/osal_none.h | 89 +++++++++++++++++----- src/tusb_hal.h | 32 -------- src/tusb_option.h | 1 - 8 files changed, 94 insertions(+), 72 deletions(-) (limited to 'src/device') diff --git a/examples/device/cdc_msc_hid/ses/samd21/samd21.emProject b/examples/device/cdc_msc_hid/ses/samd21/samd21.emProject index d61b82cfa..fbb8841b7 100644 --- a/examples/device/cdc_msc_hid/ses/samd21/samd21.emProject +++ b/examples/device/cdc_msc_hid/ses/samd21/samd21.emProject @@ -22,6 +22,7 @@ c_user_include_directories="../../src;$(rootDir)/hw/cmsis/Include;$(rootDir)/hw;$(rootDir)/src;$(asf4Dir);$(asf4Dir)/include;$(asf4Dir)/config;$(asf4Dir)/hri;$(asf4Dir)/hal/include;$(asf4Dir)/hal/utils/include;$(asf4Dir)/hpl/port;$(asf4Dir)/hpl/gclk;$(asf4Dir)/hpl/pm" debug_register_definition_file="ATSAMD21G18A_Registers.xml" debug_target_connection="J-Link" + gcc_enable_all_warnings="Yes" gcc_entry_point="Reset_Handler" link_use_linker_script_file="No" linker_memory_map_file="$(ProjectDir)/ATSAMD21G18A_MemoryMap.xml" diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index 61d92b602..e88c2ac6d 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -67,13 +67,12 @@ typedef struct uint8_t* buffer ; ///< buffer pointer uint16_t depth ; ///< max items uint16_t item_size ; ///< size of each item + bool overwritable ; volatile uint16_t count ; ///< number of items in queue volatile uint16_t wr_idx ; ///< write pointer volatile uint16_t rd_idx ; ///< read pointer - bool overwritable ; - #if CFG_FIFO_MUTEX tu_fifo_mutex_t mutex; #endif diff --git a/src/device/usbd.c b/src/device/usbd.c index ddb63cebb..206b663c0 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -161,8 +161,9 @@ enum { USBD_CLASS_DRIVER_COUNT = sizeof(usbd_class_drivers) / sizeof(usbd_class_ //--------------------------------------------------------------------+ OSAL_TASK_DEF(_usbd_task_def, "usbd", usbd_task, CFG_TUD_TASK_PRIO, CFG_TUD_TASK_STACK_SZ); -/*------------- event queue -------------*/ -OSAL_QUEUE_DEF(_usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t); +// Event queue +// role device/host is used by OS NONE for mutex (disable usb isr) only +OSAL_QUEUE_DEF(OPT_MODE_DEVICE, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t); static osal_queue_t _usbd_q; //--------------------------------------------------------------------+ @@ -200,15 +201,8 @@ tusb_error_t usbd_init (void) for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init(); // Init device controller driver - #if (CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE) - dcd_init(0); - dcd_int_enable(0); - #endif - - #if (CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE) - dcd_init(1); - dcd_int_enable(1); - #endif + dcd_init(TUD_OPT_RHPORT); + dcd_int_enable(TUD_OPT_RHPORT); return TUSB_ERROR_NONE; } @@ -268,14 +262,14 @@ static void usbd_task_body(void) break; case DCD_EVENT_BUS_RESET: - // note: if task is too slow, we could clear the event of the new attached usbd_reset(event.rhport); + // TODO remove since if task is too slow, we could clear the event of the new attached osal_queue_reset(_usbd_q); break; case DCD_EVENT_UNPLUGGED: - // note: if task is too slow, we could clear the event of the new attached usbd_reset(event.rhport); + // TODO remove since if task is too slow, we could clear the event of the new attached osal_queue_reset(_usbd_q); tud_umount_cb(); // invoke callback @@ -587,6 +581,11 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) TU_ASSERT(event->xfer_complete.result == XFER_RESULT_SUCCESS,); break; + // Not an DCD event, just a convenient way to defer ISR function should we need + case USBD_EVT_FUNC_CALL: + osal_queue_send(_usbd_q, event, in_isr); + break; + default: break; } } @@ -622,6 +621,8 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ //--------------------------------------------------------------------+ // Helper //--------------------------------------------------------------------+ + +// Helper to parse an pair of endpoint descriptors (IN & OUT) tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_desc_ep, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) { for(int i=0; i<2; i++) @@ -645,7 +646,8 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_d return TUSB_ERROR_NONE; } -void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr ) +// Helper to defer an isr function +void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr) { dcd_event_t event = { @@ -656,7 +658,7 @@ void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr ) event.func_call.func = func; event.func_call.param = param; - osal_queue_send(_usbd_q, &event, in_isr); + dcd_event_handler(&event, in_isr); } #endif diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 52cadd798..704aafd64 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -139,7 +139,9 @@ static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) //--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ -#define OSAL_QUEUE_DEF(_name, _depth, _type) \ + +// role device/host is used by OS NONE for mutex (disable usb isr) only +#define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \ static _type _name##_##buf[_depth];\ osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf }; diff --git a/src/osal/osal_mynewt.h b/src/osal/osal_mynewt.h index cb09680e0..62b5b45e8 100644 --- a/src/osal/osal_mynewt.h +++ b/src/osal/osal_mynewt.h @@ -75,7 +75,9 @@ static inline void osal_task_delay(uint32_t msec) //--------------------------------------------------------------------+ // QUEUE API //--------------------------------------------------------------------+ -#define OSAL_QUEUE_DEF(_name, _depth, _type) \ + +// role device/host is used by OS NONE for mutex (disable usb isr) only +#define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \ static _type _name##_##buf[_depth];\ static struct os_event* _name##_##evbuf[_depth];\ osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, .evbuf = _name##_##evbuf};\ diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index 40ecb9c37..115c50bdb 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -93,12 +93,11 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) static inline tusb_error_t osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { (void) msec; while (true) { - while (sem_hdl->count == 0) { - } - if (sem_hdl->count == 0) { - sem_hdl->count--; - break; - } + while (sem_hdl->count == 0) { } + if (sem_hdl->count == 0) { + sem_hdl->count--; + break; + } } return TUSB_ERROR_NONE; } @@ -124,40 +123,90 @@ static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) //--------------------------------------------------------------------+ #include "common/tusb_fifo.h" -#define OSAL_QUEUE_DEF(_name, _depth, _type) TU_FIFO_DEF(_name, _depth, _type, false) +typedef struct +{ + uint8_t role; // device or host + tu_fifo_t ff; +}osal_queue_def_t; + +typedef osal_queue_def_t* osal_queue_t; + +// role device/host is used by OS NONE for mutex (disable usb isr) only +#define OSAL_QUEUE_DEF(_role, _name, _depth, _type) \ + uint8_t _name##_buf[_depth*sizeof(_type)]; \ + osal_queue_def_t _name = { \ + .role = _role, \ + .ff = { \ + .buffer = _name##_buf, \ + .depth = _depth, \ + .item_size = sizeof(_type), \ + .overwritable = false, \ + }\ + } + +// lock queue by disable usb isr +static inline void _osal_q_lock(osal_queue_t qhdl) +{ +#if TUSB_OPT_DEVICE_ENABLED + extern void dcd_int_disable(uint8_t rhport); + if (qhdl->role == OPT_MODE_DEVICE) dcd_int_disable(TUD_OPT_RHPORT); +#endif -typedef tu_fifo_t osal_queue_def_t; -typedef tu_fifo_t* osal_queue_t; +#if MODE_HOST_SUPPORTED + extern void hcd_int_disable(uint8_t rhport); + if (qhdl->role == OPT_MODE_HOST) hcd_int_disable(TUH_OPT_RHPORT); +#endif +} + +// unlock queue +static inline void _osal_q_unlock(osal_queue_t qhdl) +{ +#if TUSB_OPT_DEVICE_ENABLED + extern void dcd_int_enable(uint8_t rhport); + if (qhdl->role == OPT_MODE_DEVICE) dcd_int_enable(TUD_OPT_RHPORT); +#endif + +#if MODE_HOST_SUPPORTED + extern void hcd_int_enable(uint8_t rhport); + if (qhdl->role == OPT_MODE_HOST) hcd_int_enable(TUH_OPT_RHPORT); +#endif +} static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { - tu_fifo_clear(qdef); + tu_fifo_clear(&qdef->ff); return (osal_queue_t) qdef; } -static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr) +static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr) { if (!in_isr) { - tusb_hal_int_disable_all(); + _osal_q_lock(qhdl); } - bool success = tu_fifo_write( (tu_fifo_t*) queue_hdl, data); + + bool success = tu_fifo_write(&qhdl->ff, data); + if (!in_isr) { - tusb_hal_int_enable_all(); + _osal_q_unlock(qhdl); } + return success; } -static inline void osal_queue_reset(osal_queue_t const queue_hdl) +static inline void osal_queue_reset(osal_queue_t const qhdl) { // tusb_hal_int_disable_all(); - tu_fifo_clear( (tu_fifo_t*) queue_hdl); + tu_fifo_clear(&qhdl->ff); // tusb_hal_int_enable_all(); } -static inline bool osal_queue_receive(osal_queue_t const queue_hdl, void* data) { - tusb_hal_int_disable_all(); - bool success = tu_fifo_read(queue_hdl, data); - tusb_hal_int_enable_all(); +// non blocking +static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) +{ + _osal_q_lock(qhdl); + bool success = tu_fifo_read(&qhdl->ff, data); + _osal_q_unlock(qhdl); + return success; } diff --git a/src/tusb_hal.h b/src/tusb_hal.h index 301e8c2f2..804196ad2 100644 --- a/src/tusb_hal.h +++ b/src/tusb_hal.h @@ -55,38 +55,6 @@ extern "C" { // Only required to implement if using No RTOS (osal_none) uint32_t tusb_hal_millis(void); -// TODO remove -extern void dcd_int_enable (uint8_t rhport); -extern void dcd_int_disable(uint8_t rhport); - -// Enable all ports' interrupt -// TODO remove -static inline void tusb_hal_int_enable_all(void) -{ -#ifdef CFG_TUSB_RHPORT0_MODE - dcd_int_enable(0); -#endif - -#ifdef CFG_TUSB_RHPORT0_MODE - dcd_int_enable(1); -#endif -} - -// Disable all ports' interrupt -// TODO remove -static inline void tusb_hal_int_disable_all(void) -{ -#ifdef CFG_TUSB_RHPORT0_MODE - dcd_int_disable(0); -#endif - -#ifdef CFG_TUSB_RHPORT0_MODE - dcd_int_disable(1); -#endif -} - - - #ifdef __cplusplus } #endif diff --git a/src/tusb_option.h b/src/tusb_option.h index c558b30ff..6b84db21c 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -123,7 +123,6 @@ // Which roothub port is configured as device #define TUD_OPT_RHPORT ( (CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE) ? 0 : ((CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE) ? 1 : -1) ) - #if TUD_OPT_RHPORT == 0 #define TUD_OPT_HIGH_SPEED ( CFG_TUSB_RHPORT0_MODE & OPT_MODE_HIGH_SPEED ) #else -- cgit v1.3.1 From 3dc0653d702e5e6b50e6a256d613ab7323561d94 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Dec 2018 17:09:30 +0700 Subject: clean up --- src/device/usbd_control.c | 1 - src/tusb_hal.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'src/device') diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index ebeaaaa4d..ae7e894b7 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -83,7 +83,6 @@ bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request) return dcd_edpt_xfer(rhport, request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN, NULL, 0); } - // Each transaction is up to endpoint0's max packet size static bool start_control_data_xact(uint8_t rhport) { diff --git a/src/tusb_hal.h b/src/tusb_hal.h index 804196ad2..e02ff480d 100644 --- a/src/tusb_hal.h +++ b/src/tusb_hal.h @@ -53,6 +53,7 @@ extern "C" { //--------------------------------------------------------------------+ // Only required to implement if using No RTOS (osal_none) +// TODO could be remove uint32_t tusb_hal_millis(void); #ifdef __cplusplus -- cgit v1.3.1 From d887829b4c088e14d8e528b3afb6f5fc894c49d9 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Dec 2018 17:30:04 +0700 Subject: change usbd_init() return to bool for simplicity --- src/device/usbd.c | 8 ++++---- src/device/usbd_pvt.h | 4 ++-- src/host/usbh.h | 3 --- src/tusb.c | 8 ++++---- src/tusb.h | 10 ++++------ 5 files changed, 14 insertions(+), 19 deletions(-) (limited to 'src/device') diff --git a/src/device/usbd.c b/src/device/usbd.c index 206b663c0..78eebf76b 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -189,11 +189,11 @@ bool tud_mounted(void) //--------------------------------------------------------------------+ // USBD Task //--------------------------------------------------------------------+ -tusb_error_t usbd_init (void) +bool usbd_init (void) { // Init device queue & task _usbd_q = osal_queue_create(&_usbd_qdef); - TU_VERIFY(_usbd_q, TUSB_ERROR_OSAL_QUEUE_FAILED); + TU_ASSERT(_usbd_q != NULL); osal_task_create(&_usbd_task_def); @@ -201,10 +201,10 @@ tusb_error_t usbd_init (void) for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init(); // Init device controller driver - dcd_init(TUD_OPT_RHPORT); + TU_ASSERT(dcd_init(TUD_OPT_RHPORT)); dcd_int_enable(TUD_OPT_RHPORT); - return TUSB_ERROR_NONE; + return true; } static void usbd_reset(uint8_t rhport) diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 30f010887..cbe5017bb 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -51,8 +51,8 @@ extern tud_desc_set_t const* usbd_desc_set; //--------------------------------------------------------------------+ // INTERNAL API for stack management //--------------------------------------------------------------------+ -tusb_error_t usbd_init (void); -void usbd_task (void* param); +bool usbd_init (void); +void usbd_task (void* param); // Carry out Data and Status stage of control transfer diff --git a/src/host/usbh.h b/src/host/usbh.h index 22fdef162..56efb6ff7 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -97,14 +97,11 @@ ATTR_WEAK void tuh_device_mount_failed_cb(tusb_error_t error, tusb_desc_devic //--------------------------------------------------------------------+ #ifdef _TINY_USB_SOURCE_FILE_ - void usbh_enumeration_task(void* param); tusb_error_t usbh_init(void); tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t* data); - - #endif #ifdef __cplusplus diff --git a/src/tusb.c b/src/tusb.c index 81c4907fb..fe4dbb2c1 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -47,17 +47,17 @@ static bool _initialized = false; -tusb_error_t tusb_init(void) +bool tusb_init(void) { // skip if already initialized - if (_initialized) return TUSB_ERROR_NONE; + if (_initialized) return true; #if MODE_HOST_SUPPORTED - TU_ASSERT_ERR( usbh_init() ); // host stack init + TU_VERIFY( usbh_init() == TUSB_ERROR_NONE ); // init host stack #endif #if TUSB_OPT_DEVICE_ENABLED - TU_ASSERT_ERR ( usbd_init() ); // device stack init + TU_VERIFY ( usbd_init() ); // init device stack #endif _initialized = true; diff --git a/src/tusb.h b/src/tusb.h index c0246ccc8..4f8918ce5 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -101,16 +101,14 @@ /** \ingroup group_application_api * @{ */ -/** \brief Initialize the usb stack - * \return Error Code of the \ref TUSB_ERROR enum - * \note Function will initialize the stack according to configuration in the configure file (tusb_config.h) - */ -tusb_error_t tusb_init(void); +// Initialize device/host stack according to tusb_config.h +// return true if success +bool tusb_init(void); #if CFG_TUSB_OS == OPT_OS_NONE /** \brief Run all tinyusb's internal tasks (e.g host task, device task). * \note This function is only required when using no RTOS (\ref CFG_TUSB_OS == OPT_OS_NONE). All the stack functions - * & callback are invoked within this function, so it should be called periodically within the mainloop + * & callback are invoked within this function. This should be called periodically within the mainloop * @code int main(void) -- cgit v1.3.1 From 4537ba66e536eb1d40c73f80ab150d92c2393397 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 5 Dec 2018 18:58:30 +0700 Subject: fixing build error with host stack --- examples/host/cdc_msc_hid/src/tusb_config.h | 33 ++------ src/device/usbd.c | 2 +- src/host/ehci/ehci.c | 1 - src/host/usbh.c | 117 +++++++++++++++------------- src/host/usbh.h | 2 +- src/host/usbh_hcd.h | 3 + src/osal/osal_freertos.h | 4 +- src/tusb.c | 9 ++- 8 files changed, 82 insertions(+), 89 deletions(-) (limited to 'src/device') diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 22e67cbda..9fef03af8 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -56,7 +56,7 @@ #endif #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_NONE | OPT_MODE_HIGH_SPEED) +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) #else #define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE #endif @@ -82,32 +82,15 @@ //-------------------------------------------------------------------- // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 -/*------------- Descriptors -------------*/ +#define CFG_TUSB_HOST_HUB 0 +#define CFG_TUSB_HOST_HID_KEYBOARD 0 +#define CFG_TUSB_HOST_HID_MOUSE 0 +#define CFG_TUSB_HOST_HID_GENERIC 0 // (not yet supported) +#define CFG_TUSB_HOST_MSC 0 +#define CFG_TUSB_HOST_CDC 0 -/* Enable auto generated descriptor, tinyusb will try its best to create - * descriptor ( device, configuration, hid ) that matches enabled CFG_* in this file - * - * Note: All CFG_TUD_DESC_* are relevant only if CFG_TUD_DESC_AUTO is enabled - */ -#define CFG_TUD_DESC_AUTO 1 - -/* If USB VID/PID is not defined, tinyusb will use default value - * Note: different class combination e.g CDC and (CDC + MSC) should have different - * PID since Host OS will "remembered" device driver after the first plug */ -// #define CFG_TUD_DESC_VID 0xCAFE -// #define CFG_TUD_DESC_PID 0x0001 - -// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number -// Therefor we need to force endpoint number to correct type on lpc17xx -#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX -#define CFG_TUD_DESC_CDC_EPNUM_NOTIF 1 -#define CFG_TUD_DESC_CDC_EPNUM 2 -#define CFG_TUD_DESC_MSC_EPNUM 5 -#define CFG_TUD_DESC_HID_KEYBOARD_EPNUM 4 -#define CFG_TUD_DESC_HID_MOUSE_EPNUM 7 -#endif +#define CFG_TUSB_HOST_DEVICE_MAX (CFG_TUSB_HOST_HUB ? 5 : 1) // normal hub has 4 ports //------------- CLASS -------------// #define CFG_TUD_CDC 0 diff --git a/src/device/usbd.c b/src/device/usbd.c index 78eebf76b..121ee3da9 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -298,7 +298,7 @@ static void usbd_task_body(void) /* USB device task * Thread that handles all device events. With an real RTOS, the task must be a forever loop and never return. - * For codign convenience with no RTOS, we use wrapped sub-function for processing to easily return at any time. + * For coding convenience with no RTOS, we use wrapped sub-function for processing to easily return at any time. */ void usbd_task( void* param) { diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index 27808d59b..491651f5c 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -42,7 +42,6 @@ //--------------------------------------------------------------------+ // INCLUDE //--------------------------------------------------------------------+ -#include "hal/hal.h" #include "osal/osal.h" #include "../hcd.h" diff --git a/src/host/usbh.c b/src/host/usbh.c index a0aab3b96..3bb90e71d 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -42,10 +42,19 @@ #define _TINY_USB_SOURCE_FILE_ -#ifndef CFG_TUD_TASK_PRIO -#define CFG_TUD_TASK_PRIO 0 +#ifndef CFG_TUH_TASK_QUEUE_SZ +#define CFG_TUH_TASK_QUEUE_SZ 16 #endif +#ifndef CFG_TUH_TASK_STACK_SZ +#define CFG_TUH_TASK_STACK_SZ 200 +#endif + +#ifndef CFG_TUH_TASK_PRIO +#define CFG_TUH_TASK_PRIO 0 +#endif + + //--------------------------------------------------------------------+ // INCLUDE //--------------------------------------------------------------------+ @@ -111,10 +120,13 @@ enum { USBH_CLASS_DRIVER_COUNT = sizeof(usbh_class_drivers) / sizeof(host_class_ //--------------------------------------------------------------------+ CFG_TUSB_MEM_SECTION usbh_device_info_t usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // including zero-address -//------------- Enumeration Task Data -------------/ -enum { ENUM_QUEUE_DEPTH = 16 }; +OSAL_TASK_DEF(_usbh_task_def, "usbh", usbh_task, CFG_TUH_TASK_PRIO, CFG_TUH_TASK_STACK_SZ); + +// Event queue +// role device/host is used by OS NONE for mutex (disable usb isr) only +OSAL_QUEUE_DEF(OPT_MODE_HOST, _usbh_qdef, CFG_TUH_TASK_QUEUE_SZ, uint32_t); +static osal_queue_t _usbh_q; -STATIC_VAR osal_queue_t enum_queue_hdl; CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4) STATIC_VAR uint8_t enum_data_buffer[CFG_TUSB_HOST_ENUM_BUFFER_SIZE]; //------------- Reporter Task Data -------------// @@ -144,23 +156,21 @@ tusb_error_t usbh_init(void) { tu_memclr(usbh_devices, sizeof(usbh_device_info_t)*(CFG_TUSB_HOST_DEVICE_MAX+1)); - TU_ASSERT_ERR( hcd_init() ); - //------------- Enumeration & Reporter Task init -------------// - enum_queue_hdl = osal_queue_create( ENUM_QUEUE_DEPTH, sizeof(uint32_t) ); - TU_ASSERT(enum_queue_hdl, TUSB_ERROR_OSAL_QUEUE_FAILED); + _usbh_q = osal_queue_create( &_usbh_qdef ); + TU_ASSERT(_usbh_q, TUSB_ERROR_OSAL_QUEUE_FAILED); - osal_task_create(usbh_enumeration_task, "usbh", 200, NULL, CFG_TUD_TASK_PRIO); + osal_task_create(&_usbh_task_def); //------------- Semaphore, Mutex for Control Pipe -------------// for(uint8_t i=0; icontrol.sem_hdl = osal_semaphore_create(1, 0); + p_device->control.sem_hdl = osal_semaphore_create(&p_device->control.sem_def); TU_ASSERT(p_device->control.sem_hdl, TUSB_ERROR_OSAL_SEMAPHORE_FAILED); - p_device->control.mutex_hdl = osal_mutex_create(); + p_device->control.mutex_hdl = osal_mutex_create(&p_device->control.mutex_def); TU_ASSERT(p_device->control.mutex_hdl, TUSB_ERROR_OSAL_MUTEX_FAILED); } @@ -173,6 +183,8 @@ tusb_error_t usbh_init(void) } } + TU_ASSERT_ERR( hcd_init() ); + return TUSB_ERROR_NONE; } @@ -181,12 +193,13 @@ tusb_error_t usbh_init(void) tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t* data) { - static tusb_error_t error; // FIXME [CMSIS-RTX] use svc for OS API, error value changed after mutex release at the end of function + // FIXME [CMSIS-RTX] use svc for OS API, error value changed after mutex release at the end of function + static tusb_error_t error; - OSAL_SUBTASK_BEGIN +// OSAL_SUBTASK_BEGIN - osal_mutex_wait(usbh_devices[dev_addr].control.mutex_hdl, OSAL_TIMEOUT_NORMAL, &error); - STASK_ASSERT_ERR_HDLR(error, osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl)); + error = osal_mutex_lock(usbh_devices[dev_addr].control.mutex_hdl, OSAL_TIMEOUT_NORMAL); + STASK_ASSERT_ERR_HDLR(error, osal_mutex_unlock(usbh_devices[dev_addr].control.mutex_hdl)); usbh_devices[dev_addr].control.request = (tusb_control_request_t) { {.bmRequestType = bmRequestType}, @@ -195,16 +208,11 @@ tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, .wIndex = wIndex, .wLength = wLength }; - -#ifndef _TEST_ usbh_devices[dev_addr].control.pipe_status = 0; -#else - usbh_devices[dev_addr].control.pipe_status = XFER_RESULT_SUCCESS; // in Test project, mark as complete immediately -#endif error = hcd_pipe_control_xfer(dev_addr, &usbh_devices[dev_addr].control.request, data); - if ( TUSB_ERROR_NONE == error ) osal_semaphore_wait(usbh_devices[dev_addr].control.sem_hdl, OSAL_TIMEOUT_NORMAL, &error); - osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl); + if ( TUSB_ERROR_NONE == error ) error = osal_semaphore_wait(usbh_devices[dev_addr].control.sem_hdl, OSAL_TIMEOUT_NORMAL); + osal_mutex_unlock(usbh_devices[dev_addr].control.mutex_hdl); STASK_ASSERT_ERR(error); if (XFER_RESULT_STALLED == usbh_devices[dev_addr].control.pipe_status) STASK_RETURN(TUSB_ERROR_USBH_XFER_STALLED); @@ -214,10 +222,9 @@ tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, // XFER_RESULT_SUCCESS == usbh_devices[dev_addr].control.pipe_status, // tuh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) ); - OSAL_SUBTASK_END +// OSAL_SUBTASK_END } -tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) ATTR_ALWAYS_INLINE; tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) { osal_semaphore_reset( usbh_devices[dev_addr].control.sem_hdl ); @@ -228,7 +235,6 @@ tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) return TUSB_ERROR_NONE; } -static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr) ATTR_ALWAYS_INLINE; static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr) { TU_ASSERT_ERR( hcd_pipe_control_close(dev_addr) ); @@ -263,13 +269,13 @@ void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, xfer_result_t eve { usbh_devices[ pipe_hdl.dev_addr ].control.pipe_status = event; // usbh_devices[ pipe_hdl.dev_addr ].control.xferred_bytes = xferred_bytes; not yet neccessary - osal_semaphore_post( usbh_devices[ pipe_hdl.dev_addr ].control.sem_hdl ); + osal_semaphore_post( usbh_devices[ pipe_hdl.dev_addr ].control.sem_hdl, true ); }else if (usbh_class_drivers[class_index].isr) { usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes); }else { - TU_ASSERT(false); // something wrong, no one claims the isr's source + TU_ASSERT(false, ); // something wrong, no one claims the isr's source } } @@ -282,7 +288,7 @@ void usbh_hub_port_plugged_isr(uint8_t hub_addr, uint8_t hub_port) .hub_port = hub_port }; - osal_queue_send(enum_queue_hdl, &enum_entry); + osal_queue_send(_usbh_q, &enum_entry, true); } void usbh_hcd_rhport_plugged_isr(uint8_t hostid) @@ -294,7 +300,7 @@ void usbh_hcd_rhport_plugged_isr(uint8_t hostid) .hub_port = 0 }; - osal_queue_send(enum_queue_hdl, &enum_entry); + osal_queue_send(_usbh_q, &enum_entry, true); } // a device unplugged on hostid, hub_addr, hub_port @@ -346,33 +352,13 @@ void usbh_hcd_rhport_unplugged_isr(uint8_t hostid) .hub_port = 0 }; - osal_queue_send(enum_queue_hdl, &enum_entry); + osal_queue_send(_usbh_q, &enum_entry, true); } //--------------------------------------------------------------------+ // ENUMERATION TASK //--------------------------------------------------------------------+ -static tusb_error_t enumeration_body_subtask(void); - -// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper -// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with -// forever loop cannot have any return at all. -void usbh_enumeration_task(void* param) -{ - (void) param; - -#if CFG_TUSB_OS != OPT_OS_NONE - while (1) { -#endif - - enumeration_body_subtask(); - -#if CFG_TUSB_OS != OPT_OS_NONE - } -#endif -} - -tusb_error_t enumeration_body_subtask(void) +tusb_error_t usbh_task_body(void) { enum { POWER_STABLE_DELAY = 500, @@ -387,10 +373,9 @@ tusb_error_t enumeration_body_subtask(void) static uint8_t configure_selected = 1; // TODO move static uint8_t *p_desc = NULL; // TODO move - OSAL_SUBTASK_BEGIN +// OSAL_SUBTASK_BEGIN - osal_queue_receive(enum_queue_hdl, &enum_entry, OSAL_TIMEOUT_WAIT_FOREVER, &error); - STASK_ASSERT_ERR(error); + if ( !osal_queue_receive(_usbh_q, &enum_entry) ) return; usbh_devices[0].core_id = enum_entry.core_id; // TODO refractor integrate to device_pool usbh_devices[0].hub_addr = enum_entry.hub_addr; @@ -617,7 +602,27 @@ tusb_error_t enumeration_body_subtask(void) tuh_device_mount_succeed_cb(new_addr); - OSAL_SUBTASK_END +// OSAL_SUBTASK_END +} + + +/* USB Host task + * Thread that handles all device events. With an real RTOS, the task must be a forever loop and never return. + * For coding convenience with no RTOS, we use wrapped sub-function for processing to easily return at any time. + */ +void usbh_task(void* param) +{ + (void) param; + +#if CFG_TUSB_OS != OPT_OS_NONE + while (1) { +#endif + + usbh_task_body(); + +#if CFG_TUSB_OS != OPT_OS_NONE + } +#endif } //--------------------------------------------------------------------+ diff --git a/src/host/usbh.h b/src/host/usbh.h index 56efb6ff7..22c85a5d0 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -97,7 +97,7 @@ ATTR_WEAK void tuh_device_mount_failed_cb(tusb_error_t error, tusb_desc_devic //--------------------------------------------------------------------+ #ifdef _TINY_USB_SOURCE_FILE_ -void usbh_enumeration_task(void* param); +void usbh_task(void* param); tusb_error_t usbh_init(void); tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, uint8_t bRequest, diff --git a/src/host/usbh_hcd.h b/src/host/usbh_hcd.h index f6b72273a..cd2a712e4 100644 --- a/src/host/usbh_hcd.h +++ b/src/host/usbh_hcd.h @@ -91,7 +91,10 @@ typedef struct { // uint8_t xferred_bytes; TODO not yet necessary tusb_control_request_t request; + osal_semaphore_def_t sem_def; osal_semaphore_t sem_hdl; // used to synchronize with HCD when control xfer complete + + osal_mutex_def_t mutex_def; osal_mutex_t mutex_hdl; // used to exclusively occupy control pipe } control; } usbh_device_info_t; diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 704aafd64..458e54b5c 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -107,10 +107,10 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) return in_isr ? xSemaphoreGiveFromISR(sem_hdl, NULL) : xSemaphoreGive(sem_hdl); } -static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, uint32_t *err) +static inline tusb_error_t osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec); - (*err) = (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT); + return (xSemaphoreTake(sem_hdl, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT); } static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) diff --git a/src/tusb.c b/src/tusb.c index fe4dbb2c1..6d2b92dc8 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -39,14 +39,17 @@ #include "tusb_option.h" #if TUSB_OPT_HOST_ENABLED || TUSB_OPT_DEVICE_ENABLED - #define _TINY_USB_SOURCE_FILE_ #include "tusb.h" -#include "device/usbd_pvt.h" static bool _initialized = false; +// TODO clean up +#if TUSB_OPT_DEVICE_ENABLED +#include "device/usbd_pvt.h" +#endif + bool tusb_init(void) { // skip if already initialized @@ -69,7 +72,7 @@ bool tusb_init(void) void tusb_task(void) { #if MODE_HOST_SUPPORTED - usbh_enumeration_task(NULL); + usbh_task(NULL); #endif #if TUSB_OPT_DEVICE_ENABLED -- cgit v1.3.1 From 6a6e7d0ecb6690c5bc833f6d543c5303b6b52155 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 Dec 2018 05:07:22 +0700 Subject: refactor usbh class driver --- src/device/usbd.c | 12 +-- src/host/ehci/ehci.c | 1 + src/host/hcd.h | 2 +- src/host/usbh.c | 227 ++++++++++++++++++++++++++++----------------------- src/host/usbh.h | 3 +- src/host/usbh_hcd.h | 3 + 6 files changed, 138 insertions(+), 110 deletions(-) (limited to 'src/device') diff --git a/src/device/usbd.c b/src/device/usbd.c index 121ee3da9..44bd641dc 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -450,19 +450,19 @@ static bool process_set_config(uint8_t rhport) { if ( usbd_class_drivers[drv_id].class_code == desc_itf->bInterfaceClass ) break; } - TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT ); // unsupported class + TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT ); // Interface number must not be used already TODO alternate interface TU_ASSERT( 0xff == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] ); _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id; - uint16_t len=0; - TU_ASSERT_ERR( usbd_class_drivers[drv_id].open( rhport, desc_itf, &len ), false ); - TU_ASSERT( len >= sizeof(tusb_desc_interface_t) ); + uint16_t itf_len=0; + TU_ASSERT_ERR( usbd_class_drivers[drv_id].open( rhport, desc_itf, &itf_len ), false ); + TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); - mark_interface_endpoint(p_desc, len, drv_id); + mark_interface_endpoint(p_desc, itf_len, drv_id); - p_desc += len; // next interface + p_desc += itf_len; // next interface } } diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index dadd377bc..49f43a9ad 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -951,6 +951,7 @@ static inline pipe_handle_t qhd_create_pipe_handle(ehci_qhd_t const * p_qhd, tus if (TUSB_XFER_CONTROL != xfer_type) // qhd index for control is meaningless { pipe_hdl.index = qhd_get_index(p_qhd); + pipe_hdl.ep_addr = edpt_addr(p_qhd->endpoint_number, p_qhd->pid_non_control == EHCI_PID_IN ? 1 : 0); } return pipe_hdl; diff --git a/src/host/hcd.h b/src/host/hcd.h index 6c7a3e98c..8ee06b2be 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -96,7 +96,7 @@ typedef struct { uint8_t dev_addr; uint8_t xfer_type; uint8_t index; - uint8_t reserved; + uint8_t ep_addr; } pipe_handle_t; static inline bool pipehandle_is_valid(pipe_handle_t pipe_hdl) diff --git a/src/host/usbh.c b/src/host/usbh.c index 4e3643aac..73bcc2e96 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -67,44 +67,49 @@ //--------------------------------------------------------------------+ static host_class_driver_t const usbh_class_drivers[] = { - #if HOST_CLASS_HID - [TUSB_CLASS_HID] = { - .init = hidh_init, - .open_subtask = hidh_open_subtask, - .isr = hidh_isr, - .close = hidh_close + #if CFG_TUH_CDC + { + .class_code = TUSB_CLASS_CDC, + .init = cdch_init, + .open_subtask = cdch_open_subtask, + .isr = cdch_isr, + .close = cdch_close }, #endif - #if CFG_TUH_CDC - [TUSB_CLASS_CDC] = { - .init = cdch_init, - .open_subtask = cdch_open_subtask, - .isr = cdch_isr, - .close = cdch_close + #if CFG_TUH_MSC + { + .class_code = TUSB_CLASS_MSC, + .init = msch_init, + .open_subtask = msch_open_subtask, + .isr = msch_isr, + .close = msch_close }, #endif - #if CFG_TUH_MSC - [TUSB_CLASS_MSC] = { - .init = msch_init, - .open_subtask = msch_open_subtask, - .isr = msch_isr, - .close = msch_close + #if HOST_CLASS_HID + { + .class_code = TUSB_CLASS_HID, + .init = hidh_init, + .open_subtask = hidh_open_subtask, + .isr = hidh_isr, + .close = hidh_close }, #endif #if CFG_TUH_HUB - [TUSB_CLASS_HUB] = { - .init = hub_init, - .open_subtask = hub_open_subtask, - .isr = hub_isr, - .close = hub_close + { + .class_code = TUSB_CLASS_HUB, + .init = hub_init, + .open_subtask = hub_open_subtask, + .isr = hub_isr, + .close = hub_close }, #endif #if CFG_TUSB_HOST_CUSTOM_CLASS - [TUSB_CLASS_MAPPED_INDEX_END-1] = { + { + .class_code = TUSB_CLASS_VENDOR_SPECIFIC, .init = cush_init, .open_subtask = cush_open_subtask, .isr = cush_isr, @@ -134,6 +139,7 @@ CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4) static uint8_t _usbh_ctrl_buf[CFG_TUSB_HOST //------------- Helper Function Prototypes -------------// static inline uint8_t get_new_address(void) ATTR_ALWAYS_INLINE; static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_desc) ATTR_ALWAYS_INLINE; +static void mark_interface_endpoint(int8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); //--------------------------------------------------------------------+ // PUBLIC API (Parameter Verification is required) @@ -144,11 +150,6 @@ tusb_device_state_t tuh_device_get_state (uint8_t const dev_addr) return (tusb_device_state_t) _usbh_devices[dev_addr].state; } -uint32_t tuh_device_get_mounted_class_flag(uint8_t dev_addr) -{ - return tuh_device_is_configured(dev_addr) ? _usbh_devices[dev_addr].flag_supported_class : 0; -} - //--------------------------------------------------------------------+ // CLASS-USBD API (don't require to verify parameters) //--------------------------------------------------------------------+ @@ -165,24 +166,21 @@ bool usbh_init(void) //------------- Semaphore, Mutex for Control Pipe -------------// for(uint8_t i=0; icontrol.sem_hdl = osal_semaphore_create(&p_device->control.sem_def); - TU_ASSERT(p_device->control.sem_hdl != NULL); + dev->control.sem_hdl = osal_semaphore_create(&dev->control.sem_def); + TU_ASSERT(dev->control.sem_hdl != NULL); - p_device->control.mutex_hdl = osal_mutex_create(&p_device->control.mutex_def); - TU_ASSERT(p_device->control.mutex_hdl != NULL); - } + dev->control.mutex_hdl = osal_mutex_create(&dev->control.mutex_def); + TU_ASSERT(dev->control.mutex_hdl != NULL); - //------------- class init -------------// - for (uint8_t class_index = 1; class_index < USBH_CLASS_DRIVER_COUNT; class_index++) - { - if (usbh_class_drivers[class_index].init) - { - usbh_class_drivers[class_index].init(); - } + memset(dev->itf2drv, -1, sizeof(dev->itf2drv)); // invalid mapping + memset(dev->ep2drv , -1, sizeof(dev->ep2drv )); // invalid mapping } + // Class drivers init + for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) usbh_class_drivers[drv_id].init(); + TU_ASSERT(hcd_init()); hcd_int_enable(TUH_OPT_RHPORT); @@ -254,40 +252,33 @@ static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr) return TUSB_ERROR_NONE; } -// TODO [USBH] unify pipe status get -//tusb_interface_status_t usbh_pipe_status_get(pipe_handle_t pipe_hdl) -//{ -// return TUSB_INTERFACE_STATUS_BUSY; -//} - -static inline uint8_t std_class_code_to_index(uint8_t std_class_code) -{ - return (std_class_code <= TUSB_CLASS_AUDIO_VIDEO ) ? std_class_code : - (std_class_code == TUSB_CLASS_DIAGNOSTIC ) ? TUSB_CLASS_MAPPED_INDEX_START : - (std_class_code == TUSB_CLASS_WIRELESS_CONTROLLER ) ? TUSB_CLASS_MAPPED_INDEX_START + 1 : - (std_class_code == TUSB_CLASS_MISC ) ? TUSB_CLASS_MAPPED_INDEX_START + 2 : - (std_class_code == TUSB_CLASS_APPLICATION_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 3 : - (std_class_code == TUSB_CLASS_VENDOR_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 4 : 0; -} - //--------------------------------------------------------------------+ // USBH-HCD ISR/Callback API //--------------------------------------------------------------------+ // interrupt caused by a TD (with IOC=1) in pipe of class class_code void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, xfer_result_t event, uint32_t xferred_bytes) { - uint8_t class_index = std_class_code_to_index(class_code); + usbh_device_t* dev = &_usbh_devices[ pipe_hdl.dev_addr ]; + if (TUSB_XFER_CONTROL == pipe_hdl.xfer_type) { - _usbh_devices[ pipe_hdl.dev_addr ].control.pipe_status = event; + dev->control.pipe_status = event; // usbh_devices[ pipe_hdl.dev_addr ].control.xferred_bytes = xferred_bytes; not yet neccessary - osal_semaphore_post( _usbh_devices[ pipe_hdl.dev_addr ].control.sem_hdl, true ); - }else if (usbh_class_drivers[class_index].isr) - { - usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes); - }else + osal_semaphore_post( dev->control.sem_hdl, true ); + } + else { - TU_ASSERT(false, ); // something wrong, no one claims the isr's source + int8_t drv_id = dev->ep2drv[edpt_number(pipe_hdl.ep_addr)][edpt_dir(pipe_hdl.ep_addr)]; + TU_ASSERT(drv_id >= 0, ); + + if (usbh_class_drivers[drv_id].isr) + { + usbh_class_drivers[drv_id].isr(pipe_hdl, event, xferred_bytes); + } + else + { + TU_ASSERT(false, ); // something wrong, no one claims the isr's source + } } } @@ -338,26 +329,24 @@ static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_ //------------- find the all devices (star-network) under port that is unplugged -------------// for (uint8_t dev_addr = 0; dev_addr <= CFG_TUSB_HOST_DEVICE_MAX; dev_addr ++) { - if (_usbh_devices[dev_addr].rhport == hostid && - (hub_addr == 0 || _usbh_devices[dev_addr].hub_addr == hub_addr) && // hub_addr == 0 & hub_port == 0 means roothub - (hub_port == 0 || _usbh_devices[dev_addr].hub_port == hub_port) && - _usbh_devices[dev_addr].state != TUSB_DEVICE_STATE_UNPLUG) + usbh_device_t* dev = &_usbh_devices[dev_addr]; + + if (dev->rhport == hostid && + (hub_addr == 0 || dev->hub_addr == hub_addr) && // hub_addr == 0 & hub_port == 0 means roothub + (hub_port == 0 || dev->hub_port == hub_port) && + dev->state != TUSB_DEVICE_STATE_UNPLUG) { // TODO Hub multiple level - for (uint8_t class_index = 1; class_index < USBH_CLASS_DRIVER_COUNT; class_index++) - { - if ((_usbh_devices[dev_addr].flag_supported_class & BIT_(class_index)) && - usbh_class_drivers[class_index].close) - { - usbh_class_drivers[class_index].close(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); // TODO refractor // set to REMOVING to allow HCD to clean up its cached data for this device // HCD must set this device's state to TUSB_DEVICE_STATE_UNPLUG when done - _usbh_devices[dev_addr].state = TUSB_DEVICE_STATE_REMOVING; - _usbh_devices[dev_addr].flag_supported_class = 0; + dev->state = TUSB_DEVICE_STATE_REMOVING; + + memset(dev->itf2drv, -1, sizeof(dev->itf2drv)); // invalid mapping + memset(dev->ep2drv , -1, sizeof(dev->ep2drv )); // invalid mapping usbh_pipe_control_close(dev_addr); @@ -388,7 +377,6 @@ bool enum_task(hcd_event_t* event) // for OSAL_NONE local variable won't retain value after blocking service sem_wait/queue_recv static uint8_t configure_selected = 1; // TODO move - static uint8_t *p_desc = NULL; // TODO move usbh_device_t* dev0 = &_usbh_devices[0]; tusb_control_request_t request; @@ -583,41 +571,55 @@ bool enum_task(hcd_event_t* event) //------------- TODO Get String Descriptors -------------// //------------- parse configuration & install drivers -------------// - p_desc = _usbh_ctrl_buf + sizeof(tusb_desc_configuration_t); + uint8_t const* p_desc = _usbh_ctrl_buf + sizeof(tusb_desc_configuration_t); // parse each interfaces while( p_desc < _usbh_ctrl_buf + ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength ) { // skip until we see interface descriptor - if ( TUSB_DESC_INTERFACE != p_desc[DESC_OFFSET_TYPE] ) + if ( TUSB_DESC_INTERFACE != descriptor_type(p_desc) ) { - p_desc += p_desc[DESC_OFFSET_LEN]; // skip the descriptor, increase by the descriptor's length + p_desc = descriptor_next(p_desc); // skip the descriptor, increase by the descriptor's length }else { - static uint8_t class_index; // has to be static as it is used to call class's open_subtask - - class_index = std_class_code_to_index( ((tusb_desc_interface_t*) p_desc)->bInterfaceClass ); - TU_ASSERT( class_index != 0 ); // class_index == 0 means corrupted data, abort enumeration + tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc; - if (usbh_class_drivers[class_index].open_subtask && - !(class_index == TUSB_CLASS_HUB && new_dev->hub_addr != 0)) + // Check if class is supported + uint8_t drv_id; + for (drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) + { + if ( usbh_class_drivers[drv_id].class_code == desc_itf->bInterfaceClass ) break; + } + + if( drv_id >= USBH_CLASS_DRIVER_COUNT ) + { + // skip unsupported class + p_desc = descriptor_next(p_desc); + } + else { - // supported class, TODO Hub disable multiple level - static uint16_t length; - length = 0; + // Interface number must not be used already TODO alternate interface + TU_ASSERT( new_dev->itf2drv[desc_itf->bInterfaceNumber] < 0 ); + new_dev->itf2drv[desc_itf->bInterfaceNumber] = drv_id; - if ( usbh_class_drivers[class_index].open_subtask(new_dev->rhport, new_addr, (tusb_desc_interface_t*) p_desc, &length) ) + if (desc_itf->bInterfaceClass == TUSB_CLASS_HUB && new_dev->hub_addr != 0) { - TU_ASSERT( length >= sizeof(tusb_desc_interface_t) ); - new_dev->flag_supported_class |= BIT_(class_index); - p_desc += length; - }else // Interface open failed, for example a subclass is not supported + // TODO Attach hub to Hub is not currently supported + // skip this interface + p_desc = descriptor_next(p_desc); + } + else { - p_desc += p_desc[DESC_OFFSET_LEN]; // skip this interface, the rest will be skipped by the above loop + uint16_t itf_len = 0; + + if ( usbh_class_drivers[drv_id].open_subtask(new_dev->rhport, new_addr, desc_itf, &itf_len) ) + { + mark_interface_endpoint(new_dev->ep2drv, p_desc, itf_len, drv_id); + } + + TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); + p_desc += itf_len; } - } else // unsupported class (not enable or yet implemented) - { - p_desc += p_desc[DESC_OFFSET_LEN]; // skip this interface, the rest will be skipped by the above loop } } } @@ -692,4 +694,25 @@ 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(int8_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 == descriptor_type(p_desc) ) + { + uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; + + ep2drv[ edpt_number(ep_addr) ][ edpt_dir(ep_addr) ] = driver_id; + } + + len += descriptor_len(p_desc); + p_desc = descriptor_next(p_desc); + } +} + + #endif diff --git a/src/host/usbh.h b/src/host/usbh.h index 584d8cdbd..b04328e40 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -64,6 +64,8 @@ typedef enum tusb_interface_status_{ } tusb_interface_status_t; typedef struct { + uint8_t class_code; + void (* const init) (void); bool (* const open_subtask)(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *, uint16_t* outlen); void (* const isr) (pipe_handle_t, xfer_result_t, uint32_t); @@ -83,7 +85,6 @@ static inline bool tuh_device_is_configured(uint8_t dev_addr) { return tuh_device_get_state(dev_addr) == TUSB_DEVICE_STATE_CONFIGURED; } -uint32_t tuh_device_get_mounted_class_flag(uint8_t dev_addr); //--------------------------------------------------------------------+ // APPLICATION CALLBACK diff --git a/src/host/usbh_hcd.h b/src/host/usbh_hcd.h index 7ccbe3a25..398c61112 100644 --- a/src/host/usbh_hcd.h +++ b/src/host/usbh_hcd.h @@ -86,6 +86,9 @@ typedef struct { osal_mutex_def_t mutex_def; osal_mutex_t mutex_hdl; // used to exclusively occupy control pipe } control; + + int8_t itf2drv[16]; // map interface number to driver (negative is invalid) + int8_t ep2drv[8][2]; // map endpoint to driver ( negative is invalid ) } usbh_device_t; extern usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // including zero-address -- cgit v1.3.1 From ac67e0ea3fdb7e3121b82335268a00133bd4b660 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 Dec 2018 05:15:49 +0700 Subject: clean up --- src/device/usbd.c | 14 +++++++------- src/host/usbh.c | 18 +++++++++--------- src/host/usbh_hcd.h | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/device') diff --git a/src/device/usbd.c b/src/device/usbd.c index 44bd641dc..5262d6c73 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -68,7 +68,7 @@ typedef struct { uint8_t config_num; uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid) - uint8_t ep2drv[2][8]; // map endpoint to driver ( 0xff is invalid ) + uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid ) }usbd_device_t; @@ -169,7 +169,7 @@ static osal_queue_t _usbd_q; //--------------------------------------------------------------------+ // Prototypes //--------------------------------------------------------------------+ -static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); +static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request); static bool process_set_config(uint8_t rhport); static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len); @@ -253,7 +253,7 @@ static void usbd_task_body(void) } else { - uint8_t const drv_id = _usbd_dev.ep2drv[edpt_dir(ep_addr)][edpt_number(ep_addr)]; + uint8_t const drv_id = _usbd_dev.ep2drv[edpt_number(ep_addr)][edpt_dir(ep_addr)]; TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,); usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len); @@ -375,7 +375,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const uint8_t const itf = tu_u16_low(p_request->wIndex); uint8_t const drvid = _usbd_dev.itf2drv[ itf ]; - TU_VERIFY (drvid < USBD_CLASS_DRIVER_COUNT ); + TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT); usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_request_complete ); @@ -460,7 +460,7 @@ static bool process_set_config(uint8_t rhport) TU_ASSERT_ERR( usbd_class_drivers[drv_id].open( rhport, desc_itf, &itf_len ), false ); TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); - mark_interface_endpoint(p_desc, itf_len, drv_id); + mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, itf_len, drv_id); p_desc += itf_len; // next interface } @@ -473,7 +473,7 @@ static bool process_set_config(uint8_t rhport) } // Helper marking endpoint of interface belongs to class driver -static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id) +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; @@ -483,7 +483,7 @@ static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, ui { uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; - _usbd_dev.ep2drv[ edpt_dir(ep_addr) ][ edpt_number(ep_addr) ] = driver_id; + ep2drv[edpt_number(ep_addr)][edpt_dir(ep_addr)] = driver_id; } len += descriptor_len(p_desc); diff --git a/src/host/usbh.c b/src/host/usbh.c index 73bcc2e96..7be0376c0 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -139,7 +139,7 @@ CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4) static uint8_t _usbh_ctrl_buf[CFG_TUSB_HOST //------------- Helper Function Prototypes -------------// static inline uint8_t get_new_address(void) ATTR_ALWAYS_INLINE; static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_desc) ATTR_ALWAYS_INLINE; -static void mark_interface_endpoint(int8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id); +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) @@ -174,8 +174,8 @@ bool usbh_init(void) dev->control.mutex_hdl = osal_mutex_create(&dev->control.mutex_def); TU_ASSERT(dev->control.mutex_hdl != NULL); - memset(dev->itf2drv, -1, sizeof(dev->itf2drv)); // invalid mapping - memset(dev->ep2drv , -1, sizeof(dev->ep2drv )); // invalid mapping + memset(dev->itf2drv, 0xff, sizeof(dev->itf2drv)); // invalid mapping + memset(dev->ep2drv , 0xff, sizeof(dev->ep2drv )); // invalid mapping } // Class drivers init @@ -268,8 +268,8 @@ void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, xfer_result_t eve } else { - int8_t drv_id = dev->ep2drv[edpt_number(pipe_hdl.ep_addr)][edpt_dir(pipe_hdl.ep_addr)]; - TU_ASSERT(drv_id >= 0, ); + uint8_t drv_id = dev->ep2drv[edpt_number(pipe_hdl.ep_addr)][edpt_dir(pipe_hdl.ep_addr)]; + TU_ASSERT(drv_id < USBH_CLASS_DRIVER_COUNT, ); if (usbh_class_drivers[drv_id].isr) { @@ -345,8 +345,8 @@ static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_ // HCD must set this device's state to TUSB_DEVICE_STATE_UNPLUG when done dev->state = TUSB_DEVICE_STATE_REMOVING; - memset(dev->itf2drv, -1, sizeof(dev->itf2drv)); // invalid mapping - memset(dev->ep2drv , -1, sizeof(dev->ep2drv )); // invalid mapping + memset(dev->itf2drv, 0xff, sizeof(dev->itf2drv)); // invalid mapping + memset(dev->ep2drv , 0xff, sizeof(dev->ep2drv )); // invalid mapping usbh_pipe_control_close(dev_addr); @@ -599,7 +599,7 @@ bool enum_task(hcd_event_t* event) else { // Interface number must not be used already TODO alternate interface - TU_ASSERT( new_dev->itf2drv[desc_itf->bInterfaceNumber] < 0 ); + TU_ASSERT( new_dev->itf2drv[desc_itf->bInterfaceNumber] == 0xff ); new_dev->itf2drv[desc_itf->bInterfaceNumber] = drv_id; if (desc_itf->bInterfaceClass == TUSB_CLASS_HUB && new_dev->hub_addr != 0) @@ -696,7 +696,7 @@ static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_de // Helper marking endpoint of interface belongs to class driver // TODO merge with usbd -static void mark_interface_endpoint(int8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id) +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; diff --git a/src/host/usbh_hcd.h b/src/host/usbh_hcd.h index 398c61112..8af454b1a 100644 --- a/src/host/usbh_hcd.h +++ b/src/host/usbh_hcd.h @@ -87,8 +87,8 @@ typedef struct { osal_mutex_t mutex_hdl; // used to exclusively occupy control pipe } control; - int8_t itf2drv[16]; // map interface number to driver (negative is invalid) - int8_t ep2drv[8][2]; // map endpoint to driver ( negative is invalid ) + uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid) + uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid ) } usbh_device_t; extern usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // including zero-address -- cgit v1.3.1 From 9c4c7975024e9b971a43a866c1feffb419dd8f5b Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 Dec 2018 19:01:28 +0700 Subject: add ep addr to host cdc --- src/class/cdc/cdc_device.c | 7 +- src/class/cdc/cdc_host.c | 83 +++++++++++++-------- src/class/cdc/cdc_host.h | 12 +-- src/common/tusb_common.h | 61 ++++----------- src/device/usbd.c | 16 ++-- src/host/ehci/ehci.c | 86 +++++----------------- src/host/ehci/ehci.h | 8 +- .../test/host/ehci/test_ehci_structure.c | 2 +- 8 files changed, 104 insertions(+), 171 deletions(-) (limited to 'src/device') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 040c8d4f4..d477a0d23 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -41,9 +41,7 @@ #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC) #define _TINY_USB_SOURCE_FILE_ -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ + #include "cdc_device.h" #include "device/usbd_pvt.h" @@ -236,8 +234,7 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != p_interface_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS; // Only support AT commands, no protocol and vendor specific commands. - if ( !(tu_within(CDC_COMM_PROTOCOL_ATCOMMAND, p_interface_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || - p_interface_desc->bInterfaceProtocol == CDC_COMM_PROTOCOL_NONE || + if ( !(tu_within(CDC_COMM_PROTOCOL_NONE, p_interface_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || p_interface_desc->bInterfaceProtocol == 0xff ) ) { return TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL; diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index 290cbfb44..0d0378920 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -42,24 +42,35 @@ #define _TINY_USB_SOURCE_FILE_ -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ #include "common/tusb_common.h" #include "cdc_host.h" //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ +typedef struct { + uint8_t itf_num; + uint8_t itf_protocol; + + cdc_acm_capability_t acm_capability; + + pipe_handle_t pipe_notification, pipe_out, pipe_in; + + uint8_t ep_notif; + uint8_t ep_in; + uint8_t ep_out; + +} cdch_data_t; //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -static cdch_data_t cdch_data[CFG_TUSB_HOST_DEVICE_MAX]; // TODO to be static +static cdch_data_t cdch_data[CFG_TUSB_HOST_DEVICE_MAX]; -static inline bool tuh_cdc_mounted(uint8_t dev_addr) +bool tuh_cdc_mounted(uint8_t dev_addr) { - return pipehandle_is_valid(cdch_data[dev_addr-1].pipe_in) && pipehandle_is_valid(cdch_data[dev_addr-1].pipe_out); + cdch_data_t* cdc = &cdch_data[dev_addr-1]; + return cdc->ep_in && cdc->ep_out; } bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid) @@ -91,8 +102,8 @@ 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_ATCOMMAND <= cdch_data[dev_addr-1].interface_protocol) && - (cdch_data[dev_addr-1].interface_protocol <= CDC_COMM_PROTOCOL_ATCOMMAND_CDMA); + (CDC_COMM_PROTOCOL_ATCOMMAND <= cdch_data[dev_addr-1].itf_protocol) && + (cdch_data[dev_addr-1].itf_protocol <= CDC_COMM_PROTOCOL_ATCOMMAND_CDMA); } tusb_error_t tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool is_notify) @@ -125,33 +136,33 @@ void cdch_init(void) tu_memclr(cdch_data, sizeof(cdch_data_t)*CFG_TUSB_HOST_DEVICE_MAX); } -bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length) +bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length) { - // TODO change following assert to subtask_assert - if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != p_interface_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS; + // Only support ACM + TU_VERIFY( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass); - if ( !(tu_within(CDC_COMM_PROTOCOL_ATCOMMAND, p_interface_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || - 0xff == p_interface_desc->bInterfaceProtocol) ) - { - return TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL; - } + // Only support AT commands, no protocol and vendor specific commands. + TU_VERIFY(tu_within(CDC_COMM_PROTOCOL_NONE, itf_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || + 0xff == itf_desc->bInterfaceProtocol); uint8_t const * p_desc; cdch_data_t * p_cdc; - p_desc = descriptor_next ( (uint8_t const *) p_interface_desc ); - p_cdc = &cdch_data[dev_addr-1]; // non-static variable cannot be used after OS service call + p_desc = descriptor_next ( (uint8_t const *) itf_desc ); + p_cdc = &cdch_data[dev_addr-1]; - p_cdc->interface_number = p_interface_desc->bInterfaceNumber; - p_cdc->interface_protocol = p_interface_desc->bInterfaceProtocol; // TODO 0xff is consider as rndis candidate, other is virtual Com + p_cdc->itf_num = itf_desc->bInterfaceNumber; + p_cdc->itf_protocol = itf_desc->bInterfaceProtocol; // TODO 0xff is consider as rndis candidate, other is virtual Com //------------- Communication Interface -------------// (*p_length) = sizeof(tusb_desc_interface_t); + // Communication Functional Descriptors while( TUSB_DESC_CLASS_SPECIFIC == p_desc[DESC_OFFSET_TYPE] ) - { // Communication Functional Descriptors + { if ( CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT == cdc_functional_desc_typeof(p_desc) ) - { // save ACM bmCapabilities + { + // save ACM bmCapabilities p_cdc->acm_capability = ((cdc_desc_func_acm_t const *) p_desc)->bmCapabilities; } @@ -160,8 +171,12 @@ bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c } if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE]) - { // notification endpoint if any - p_cdc->pipe_notification = hcd_pipe_open(rhport, dev_addr, (tusb_desc_endpoint_t const *) p_desc, TUSB_CLASS_CDC); + { + // notification endpoint if any + tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) p_desc; + p_cdc->pipe_notification = hcd_pipe_open(rhport, dev_addr, ep_desc, TUSB_CLASS_CDC); + + p_cdc->ep_notif = ep_desc->bEndpointAddress; (*p_length) += p_desc[DESC_OFFSET_LEN]; p_desc = descriptor_next(p_desc); @@ -179,16 +194,24 @@ bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c // data endpoints expected to be in pairs for(uint32_t i=0; i<2; i++) { - tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType); - TU_ASSERT(TUSB_XFER_BULK == p_endpoint->bmAttributes.xfer); + tusb_desc_endpoint_t const *ep_desc = (tusb_desc_endpoint_t const *) p_desc; + TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType); + TU_ASSERT(TUSB_XFER_BULK == ep_desc->bmAttributes.xfer); - pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? + pipe_handle_t * p_pipe_hdl = ( ep_desc->bEndpointAddress & TUSB_DIR_IN_MASK ) ? &p_cdc->pipe_in : &p_cdc->pipe_out; - (*p_pipe_hdl) = hcd_pipe_open(rhport, dev_addr, p_endpoint, TUSB_CLASS_CDC); + (*p_pipe_hdl) = hcd_pipe_open(rhport, dev_addr, ep_desc, TUSB_CLASS_CDC); TU_ASSERT ( pipehandle_is_valid(*p_pipe_hdl) ); + if ( edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) + { + p_cdc->ep_in = ep_desc->bEndpointAddress; + }else + { + p_cdc->ep_out = ep_desc->bEndpointAddress; + } + (*p_length) += p_desc[DESC_OFFSET_LEN]; p_desc = descriptor_next( p_desc ); } @@ -203,7 +226,7 @@ bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT }, .bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE, .wValue = 0x03, // dtr on, cst on - .wIndex = p_cdc->interface_number, + .wIndex = p_cdc->itf_num, .wLength = 0 }; diff --git a/src/class/cdc/cdc_host.h b/src/class/cdc/cdc_host.h index 2fbd40f40..eceec3ee3 100644 --- a/src/class/cdc/cdc_host.h +++ b/src/class/cdc/cdc_host.h @@ -137,18 +137,8 @@ void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_i //--------------------------------------------------------------------+ #ifdef _TINY_USB_SOURCE_FILE_ -typedef struct { - uint8_t interface_number; - uint8_t interface_protocol; - - cdc_acm_capability_t acm_capability; - - pipe_handle_t pipe_notification, pipe_out, pipe_in; - -} cdch_data_t; - void cdch_init(void); -bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length) ATTR_WARN_UNUSED_RESULT; +bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length); void cdch_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void cdch_close(uint8_t dev_addr); diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 3c5402e59..f4fac96b8 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -73,6 +73,8 @@ // MACROS //--------------------------------------------------------------------+ #define TU_ARRAY_SZIE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) ) +#define TU_MIN(_x, _y) ( (_x) < (_y) ) ? (_x) : (_y) ) +#define TU_MAX(_x, _y) ( (_x) > (_y) ) ? (_x) : (_y) ) #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff)) #define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff)) @@ -167,53 +169,23 @@ static inline uint16_t tu_u16_le2be(uint16_t u16) return ((uint16_t)(tu_u16_low(u16) << 8)) | tu_u16_high(u16); } -//------------- Min -------------// -static inline uint8_t tu_min8(uint8_t x, uint8_t y) -{ - return (x < y) ? x : y; -} - -static inline uint16_t tu_min16(uint16_t x, uint16_t y) -{ - return (x < y) ? x : y; -} - -static inline uint32_t tu_min32(uint32_t x, uint32_t y) -{ - return (x < y) ? x : y; -} - -//------------- Max -------------// -static inline uint32_t tu_max32(uint32_t x, uint32_t y) -{ - return (x > y) ? x : y; -} - -//------------- Align -------------// -static inline uint32_t tu_align32 (uint32_t value) -{ - return (value & 0xFFFFFFE0UL); -} - -static inline uint32_t tu_align16 (uint32_t value) -{ - return (value & 0xFFFFFFF0UL); -} +// Min +static inline uint8_t tu_min8(uint8_t x, uint8_t y) { return (x < y) ? x : y; } +static inline uint16_t tu_min16(uint16_t x, uint16_t y) { return (x < y) ? x : y; } +static inline uint32_t tu_min32(uint32_t x, uint32_t y) { return (x < y) ? x : y; } -static inline uint32_t tu_align_n (uint32_t alignment, uint32_t value) -{ - return value & ((uint32_t) ~(alignment-1)); -} +// Max +static inline uint8_t tu_max8(uint8_t x, uint8_t y) { return (x > y) ? x : y; } +static inline uint16_t tu_max16(uint16_t x, uint16_t y) { return (x > y) ? x : y; } +static inline uint32_t tu_max32(uint32_t x, uint32_t y) { return (x > y) ? x : y; } -static inline uint32_t tu_align4k (uint32_t value) -{ - return (value & 0xFFFFF000UL); -} +// Align +static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); } +static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); } +static inline uint32_t tu_align_n (uint32_t alignment, uint32_t value) { return value & ((uint32_t) ~(alignment-1)); } +static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); } -static inline uint32_t tu_offset4k(uint32_t value) -{ - return (value & 0xFFFUL); -} +static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); } //------------- Mathematics -------------// static inline uint32_t tu_abs(int32_t value) @@ -221,7 +193,6 @@ static inline uint32_t tu_abs(int32_t value) return (value < 0) ? (-value) : value; } - /// inclusive range checking static inline bool tu_within(uint32_t lower, uint32_t value, uint32_t upper) { diff --git a/src/device/usbd.c b/src/device/usbd.c index 5262d6c73..d1b708a0d 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -623,24 +623,24 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ //--------------------------------------------------------------------+ // Helper to parse an pair of endpoint descriptors (IN & OUT) -tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_desc_ep, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) +tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_desc, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) { for(int i=0; i<2; i++) { - TU_ASSERT(TUSB_DESC_ENDPOINT == p_desc_ep->bDescriptorType && - xfer_type == p_desc_ep->bmAttributes.xfer, TUSB_ERROR_DESCRIPTOR_CORRUPTED); + TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType && + xfer_type == ep_desc->bmAttributes.xfer, TUSB_ERROR_DESCRIPTOR_CORRUPTED); - TU_ASSERT( dcd_edpt_open(rhport, p_desc_ep), TUSB_ERROR_DCD_OPEN_PIPE_FAILED ); + TU_ASSERT( dcd_edpt_open(rhport, ep_desc), TUSB_ERROR_DCD_OPEN_PIPE_FAILED ); - if ( edpt_dir(p_desc_ep->bEndpointAddress) == TUSB_DIR_IN ) + if ( edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { - (*ep_in) = p_desc_ep->bEndpointAddress; + (*ep_in) = ep_desc->bEndpointAddress; }else { - (*ep_out) = p_desc_ep->bEndpointAddress; + (*ep_out) = ep_desc->bEndpointAddress; } - p_desc_ep = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) p_desc_ep ); + ep_desc = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) ep_desc ); } return TUSB_ERROR_NONE; diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index 4989c74a0..2e9090db2 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -58,26 +58,8 @@ //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -CFG_TUSB_MEM_SECTION static ehci_data_t ehci_data; - -#if EHCI_PERIODIC_LIST - - #if (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST) - CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4096) static ehci_link_t period_frame_list0[EHCI_FRAMELIST_SIZE]; - - #ifndef __ICCARM__ // IAR cannot able to determine the alignment with datalignment pragma - TU_VERIFY_STATIC( ALIGN_OF(period_frame_list0) == 4096, "Period Framelist must be 4k alginment"); // validation - #endif - #endif - - #if (CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST) - CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4096) STATIC_VAR ehci_link_t period_frame_list1[EHCI_FRAMELIST_SIZE]; - - #ifndef __ICCARM__ // IAR cannot able to determine the alignment with datalignment pragma - TU_VERIFY_STATIC( ALIGN_OF(period_frame_list1) == 4096, "Period Framelist must be 4k alginment"); // validation - #endif - #endif -#endif +// Periodic frame list must be 4K alignment +CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4096) static ehci_data_t ehci_data; //------------- Validation -------------// // TODO static assert for memory placement on some known MCU such as lpc43xx @@ -86,7 +68,6 @@ CFG_TUSB_MEM_SECTION static ehci_data_t ehci_data; // PROTOTYPE //--------------------------------------------------------------------+ static inline ehci_registers_t* get_operational_register(uint8_t hostid) ATTR_PURE ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT; -static inline ehci_link_t* get_period_frame_list(uint8_t hostid) ATTR_PURE ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT; static inline ehci_qhd_t* get_async_head(uint8_t hostid) ATTR_ALWAYS_INLINE ATTR_PURE ATTR_WARN_UNUSED_RESULT; static inline ehci_link_t* get_period_head(uint8_t hostid, uint8_t interval_ms) ATTR_ALWAYS_INLINE ATTR_PURE ATTR_WARN_UNUSED_RESULT; @@ -112,9 +93,9 @@ static void qhd_init(ehci_qhd_t *p_qhd, uint8_t dev_addr, tusb_desc_endpoint_t c static inline ehci_qtd_t* qtd_find_free(uint8_t dev_addr) ATTR_PURE ATTR_ALWAYS_INLINE; -static inline ehci_qtd_t* qtd_next(ehci_qtd_t const * p_qtd ) ATTR_PURE ATTR_ALWAYS_INLINE; -static inline void qtd_insert_to_qhd(ehci_qhd_t *p_qhd, ehci_qtd_t *p_qtd_new) ATTR_ALWAYS_INLINE; -static inline void qtd_remove_1st_from_qhd(ehci_qhd_t *p_qhd) ATTR_ALWAYS_INLINE; +static inline ehci_qtd_t* qtd_next(ehci_qtd_t const * p_qtd ) ATTR_PURE ATTR_ALWAYS_INLINE; +static inline void qtd_insert_to_qhd(ehci_qhd_t *p_qhd, ehci_qtd_t *p_qtd_new) ATTR_ALWAYS_INLINE; +static inline void qtd_remove_1st_from_qhd(ehci_qhd_t *p_qhd) ATTR_ALWAYS_INLINE; static void qtd_init(ehci_qtd_t* p_qtd, uint32_t data_ptr, uint16_t total_bytes); static inline void list_insert(ehci_link_t *current, ehci_link_t *new, uint8_t new_type) ATTR_ALWAYS_INLINE; @@ -129,18 +110,8 @@ static bool ehci_init(uint8_t hostid); //--------------------------------------------------------------------+ bool hcd_init(void) { - //------------- Data Structure init -------------// tu_memclr(&ehci_data, sizeof(ehci_data_t)); - - #if (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST) - TU_VERIFY(ehci_init(0)); - #endif - - #if (CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST) - TU_VERIFY(ehci_init(1)); - #endif - - return true; + return ehci_init(TUH_OPT_RHPORT); } //--------------------------------------------------------------------+ @@ -148,7 +119,7 @@ bool hcd_init(void) //--------------------------------------------------------------------+ void hcd_port_reset(uint8_t hostid) { - ehci_registers_t* const regs = get_operational_register(hostid); + ehci_registers_t* regs = ehci_data.regs; regs->portsc_bit.port_enable = 0; // disable port before reset regs->portsc_bit.port_reset = 1; @@ -156,19 +127,18 @@ void hcd_port_reset(uint8_t hostid) bool hcd_port_connect_status(uint8_t hostid) { - return get_operational_register(hostid)->portsc_bit.current_connect_status; + return ehci_data.regs->portsc_bit.current_connect_status; } tusb_speed_t hcd_port_speed_get(uint8_t hostid) { - return (tusb_speed_t) get_operational_register(hostid)->portsc_bit.nxp_port_speed; // NXP specific port speed + return (tusb_speed_t) ehci_data.regs->portsc_bit.nxp_port_speed; // NXP specific port speed } // TODO refractor abtract later void hcd_port_unplug(uint8_t hostid) { - ehci_registers_t* const regs = get_operational_register(hostid); - regs->usb_cmd_bit.advacne_async = 1; // Async doorbell check EHCI 4.8.2 for operational details + ehci_data.regs->usb_cmd_bit.advance_async = 1; // Async doorbell check EHCI 4.8.2 for operational details } //--------------------------------------------------------------------+ @@ -176,7 +146,9 @@ void hcd_port_unplug(uint8_t hostid) //--------------------------------------------------------------------+ static bool ehci_init(uint8_t hostid) { - ehci_registers_t* const regs = get_operational_register(hostid); + ehci_data.regs = get_operational_register(hostid); + + ehci_registers_t* regs = ehci_data.regs; //------------- CTRLDSSEGMENT Register (skip) -------------// //------------- USB INT Register -------------// @@ -211,7 +183,7 @@ static bool ehci_init(uint8_t hostid) ehci_data.period_head_arr[i].qtd_overlay.halted = 1; // dummy node, always inactive } - ehci_link_t * const framelist = get_period_frame_list(hostid); + ehci_link_t * const framelist = ehci_data.period_framelist; ehci_link_t * const period_1ms = get_period_head(hostid, 1); // all links --> period_head_arr[0] (1ms) // 0, 2, 4, 6 etc --> period_head_arr[1] (2ms) @@ -264,11 +236,11 @@ static bool ehci_init(uint8_t hostid) static tusb_error_t hcd_controller_stop(uint8_t hostid) { - ehci_registers_t* const regs = get_operational_register(hostid); - tu_timeout_t timeout; + ehci_registers_t* regs = ehci_data.regs; regs->usb_cmd_bit.run_stop = 0; + tu_timeout_t timeout; tu_timeout_set(&timeout, 2); // USB Spec: controller has to stop within 16 uframe = 2 frames while( regs->usb_sts_bit.hc_halted == 0 && !tu_timeout_expired(&timeout)) {} @@ -613,10 +585,8 @@ static void async_advance_isr(ehci_qhd_t * const async_head) static void port_connect_status_change_isr(uint8_t hostid) { - ehci_registers_t* const regs = get_operational_register(hostid); - // NOTE There is an sequence plug->unplug->…..-> plug if device is powering with pre-plugged device - if (regs->portsc_bit.current_connect_status) + if (ehci_data.regs->portsc_bit.current_connect_status) { hcd_port_reset(hostid); hcd_event_device_attach(hostid); @@ -797,7 +767,7 @@ static void xfer_error_isr(uint8_t hostid) //------------- Host Controller Driver's Interrupt Handler -------------// void hal_hcd_isr(uint8_t hostid) { - ehci_registers_t* const regs = get_operational_register(hostid); + ehci_registers_t* regs = ehci_data.regs; uint32_t int_status = regs->usb_sts; int_status &= regs->usb_int_enable; @@ -854,26 +824,6 @@ static inline ehci_registers_t* get_operational_register(uint8_t hostid) return (ehci_registers_t*) (hostid ? (&LPC_USB1->USBCMD_H) : (&LPC_USB0->USBCMD_H) ); } -#if EHCI_PERIODIC_LIST // TODO refractor/group this together -static inline ehci_link_t* get_period_frame_list(uint8_t hostid) -{ - switch(hostid) - { -#if (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST) - case 0: - return period_frame_list0; -#endif - -#if (CFG_TUSB_RHPORT1_MODE & OPT_MODE_HOST) - case 1: - return period_frame_list1; -#endif - - default: return NULL; - } -} -#endif - //------------- queue head helper -------------// static inline ehci_qhd_t* get_async_head(uint8_t hostid) { diff --git a/src/host/ehci/ehci.h b/src/host/ehci/ehci.h index 48e0c34e5..4534c4cd3 100644 --- a/src/host/ehci/ehci.h +++ b/src/host/ehci/ehci.h @@ -352,7 +352,7 @@ typedef volatile struct { uint32_t framelist_size : 2 ; ///< This field is R/W only if Programmable Frame List Flagin the HCCPARAMS registers is set to a one. This field specifies the size of the frame list.00b 1024 elements (4096 bytes) Default value 01b 512 elements (2048 bytes) 10b 256 elements (1024 bytes) uint32_t periodic_enable : 1 ; ///< This bit controls whether the host controller skips processing the Periodic Schedule. Values mean: 0b Do not process the Periodic Schedule 1b Use the PERIODICLISTBASE register to access the Periodic Schedule. uint32_t async_enable : 1 ; ///< This bit controls whether the host controller skips processing the Asynchronous Schedule. Values mean: 0b Do not process the Asynchronous Schedule 1b Use the ASYNCLISTADDR register to access the Asynchronous Schedule. - uint32_t advacne_async : 1 ; ///< This bit is used as a doorbell by software to tell the host controller to issue an interrupt the next time it advances asynchronous schedule. Software must write a 1 to this bit to ringthe doorbell. When the host controller has evicted all appropriate cached schedule state, it sets the Interrupt on Async Advancestatus bit in the USBSTS register. If the Interrupt on Async Advance Enablebit in the USBINTR register is a one then the host controller will assert an interrupt at the next interrupt threshold. See Section 4.8.2 for operational details. The host controller sets this bit to a zero after it has set the Interrupt on Async Advance status bit in the USBSTS register to a one. Software should not write a one to this bit when the asynchronous schedule is disabled. Doing so will yield undefined results. + uint32_t advance_async : 1 ; ///< This bit is used as a doorbell by software to tell the host controller to issue an interrupt the next time it advances asynchronous schedule. Software must write a 1 to this bit to ringthe doorbell. When the host controller has evicted all appropriate cached schedule state, it sets the Interrupt on Async Advancestatus bit in the USBSTS register. If the Interrupt on Async Advance Enablebit in the USBINTR register is a one then the host controller will assert an interrupt at the next interrupt threshold. See Section 4.8.2 for operational details. The host controller sets this bit to a zero after it has set the Interrupt on Async Advance status bit in the USBSTS register to a one. Software should not write a one to this bit when the asynchronous schedule is disabled. Doing so will yield undefined results. uint32_t light_reset : 1 ; ///< This control bit is not required. If implemented, it allows the driver to reset the EHCI controller without affecting the state of the ports or the relationship to the companion host controllers. For example, the PORSTC registers should not be reset to their default values and the CF bit setting should not go to zero (retaining port ownership relationships). A host software read of this bit as zero indicates the Light Host Controller Reset has completed and it is safe for host software to re-initialize the host controller. A host software read of this bit as a one indicates the Light Host Controller Reset has not yet completed. uint32_t async_park : 2 ; ///< It contains a count of the number of successive transactions the host controller is allowed to execute from a high-speed queue head on the Asynchronous schedule before continuing traversal of the Asynchronous schedule. See Section 4.10.3.2 for full operational details. Valid values are 1h to 3h. Software must not write a zero to this bit when Park Mode Enableis a one as this will result in undefined behavior. uint32_t : 1 ; ///< reserved @@ -450,11 +450,11 @@ typedef volatile struct { //--------------------------------------------------------------------+ typedef struct { -#if EHCI_PERIODIC_LIST + ehci_link_t period_framelist[EHCI_FRAMELIST_SIZE]; + // for NXP ECHI, only implement 1 ms & 2 ms & 4 ms, 8 ms (framelist) // [0] : 1ms, [1] : 2ms, [2] : 4ms, [3] : 8 ms ehci_qhd_t period_head_arr[4]; -#endif struct { ehci_qhd_t qhd; // also used as head of async list (each for 1 controller), always exists @@ -472,6 +472,8 @@ typedef struct // ehci_itd_t itd[EHCI_MAX_ITD] ; ///< Iso Transfer Pool // ehci_sitd_t sitd[EHCI_MAX_SITD] ; ///< Split (FS) Isochronous Transfer Pool }device[CFG_TUSB_HOST_DEVICE_MAX]; + + ehci_registers_t* regs; }ehci_data_t; #ifdef __cplusplus diff --git a/tests/lpc18xx_43xx/test/host/ehci/test_ehci_structure.c b/tests/lpc18xx_43xx/test/host/ehci/test_ehci_structure.c index 7eaabcd51..39e15b227 100644 --- a/tests/lpc18xx_43xx/test/host/ehci/test_ehci_structure.c +++ b/tests/lpc18xx_43xx/test/host/ehci/test_ehci_structure.c @@ -225,7 +225,7 @@ void test_register_usbcmd(void) TEST_ASSERT_EQUAL( 2 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, framelist_size) ); TEST_ASSERT_EQUAL( 4 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, periodic_enable) ); TEST_ASSERT_EQUAL( 5 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, async_enable) ); - TEST_ASSERT_EQUAL( 6 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, advacne_async) ); + TEST_ASSERT_EQUAL( 6 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, advance_async) ); TEST_ASSERT_EQUAL( 7 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, light_reset) ); TEST_ASSERT_EQUAL( 8 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, async_park) ); TEST_ASSERT_EQUAL( 11 , BITFIELD_OFFSET_OF_MEMBER(ehci_registers_t, usb_cmd_bit, async_park_enable) ); -- cgit v1.3.1 From 4e7596ca9cdde3bd1197cf1853c370d8da48d7a2 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 Dec 2018 19:25:57 +0700 Subject: add tuh_mount_cb/tuh_umount_cb --- examples/host/cdc_msc_hid/src/main.c | 4 ++-- src/class/cdc/cdc_host.c | 8 +------- src/class/cdc/cdc_host.h | 13 +------------ src/device/usbd.c | 5 +++-- src/host/usbh.c | 11 +++++------ src/host/usbh.h | 8 ++++++-- tests/lpc18xx_43xx/test/host/cdc/test_cdc_host.c | 16 ++++++++-------- 7 files changed, 26 insertions(+), 39 deletions(-) (limited to 'src/device') diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 4d197a16a..ebb540782 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -84,7 +84,7 @@ int main(void) #if CFG_TUH_CDC CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 }; -void tuh_cdc_mounted_cb(uint8_t dev_addr) +void tuh_mount_cb(uint8_t dev_addr) { // application set-up printf("\na CDC device (address %d) is mounted\n", dev_addr); @@ -92,7 +92,7 @@ void tuh_cdc_mounted_cb(uint8_t dev_addr) tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer } -void tuh_cdc_unmounted_cb(uint8_t dev_addr) +void tuh_umount_cb(uint8_t dev_addr) { // application tear-down printf("\na CDC device (address %d) is unmounted \n", dev_addr); diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index 0d0378920..9e1ca0e7a 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -136,7 +136,7 @@ void cdch_init(void) tu_memclr(cdch_data, sizeof(cdch_data_t)*CFG_TUSB_HOST_DEVICE_MAX); } -bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length) +bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length) { // Only support ACM TU_VERIFY( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass); @@ -217,9 +217,6 @@ bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c } } - // FIXME mounted class flag is not set yet - tuh_cdc_mounted_cb(dev_addr); - // FIXME move to seperate API : connect tusb_control_request_t request = { @@ -249,9 +246,6 @@ void cdch_close(uint8_t dev_addr) hcd_pipe_close(TUH_OPT_RHPORT, dev_addr, p_cdc->pipe_out); tu_memclr(p_cdc, sizeof(cdch_data_t)); - - tuh_cdc_unmounted_cb(dev_addr); - } #endif diff --git a/src/class/cdc/cdc_host.h b/src/class/cdc/cdc_host.h index eceec3ee3..8d0b1d28d 100644 --- a/src/class/cdc/cdc_host.h +++ b/src/class/cdc/cdc_host.h @@ -104,17 +104,6 @@ tusb_error_t tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, //--------------------------------------------------------------------+ // CDC APPLICATION CALLBACKS //--------------------------------------------------------------------+ -/** \brief Callback function that will be invoked when a device with CDC Abstract Control Model interface is mounted - * \param[in] dev_addr Address of newly mounted device - * \note This callback should be used by Application to set-up interface-related data - */ -void tuh_cdc_mounted_cb(uint8_t dev_addr); - -/** \brief Callback function that will be invoked when a device with CDC Abstract Control Model interface is unmounted - * \param[in] dev_addr Address of newly unmounted device - * \note This callback should be used by Application to tear-down interface-related data - */ -void tuh_cdc_unmounted_cb(uint8_t dev_addr); /** \brief Callback function that is invoked when an transferring event occurred * \param[in] dev_addr Address of device @@ -138,7 +127,7 @@ void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_i #ifdef _TINY_USB_SOURCE_FILE_ void cdch_init(void); -bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length); +bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length); void cdch_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void cdch_close(uint8_t dev_addr); diff --git a/src/device/usbd.c b/src/device/usbd.c index d1b708a0d..ad40e3c9f 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -272,7 +272,8 @@ static void usbd_task_body(void) // TODO remove since if task is too slow, we could clear the event of the new attached osal_queue_reset(_usbd_q); - tud_umount_cb(); // invoke callback + // invoke callback + if (tud_umount_cb) tud_umount_cb(); break; case DCD_EVENT_SOF: @@ -467,7 +468,7 @@ static bool process_set_config(uint8_t rhport) } // invoke callback - tud_mount_cb(); + if (tud_mount_cb) tud_mount_cb(); return TUSB_ERROR_NONE; } diff --git a/src/host/usbh.c b/src/host/usbh.c index c345ddf0d..2c2286698 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -71,7 +71,7 @@ static host_class_driver_t const usbh_class_drivers[] = { .class_code = TUSB_CLASS_CDC, .init = cdch_init, - .open_subtask = cdch_open_subtask, + .open_subtask = cdch_open, .isr = cdch_isr, .close = cdch_close }, @@ -218,10 +218,6 @@ bool usbh_control_xfer (uint8_t dev_addr, tusb_control_request_t* request, uint8 if ( XFER_RESULT_STALLED == dev->control.pipe_status ) return false; if ( XFER_RESULT_FAILED == dev->control.pipe_status ) return false; -// STASK_ASSERT_HDLR(TUSB_ERROR_NONE == error && -// XFER_RESULT_SUCCESS == dev->control.pipe_status, -// tuh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) ); - return true; } @@ -336,6 +332,9 @@ static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_ (hub_port == 0 || dev->hub_port == hub_port) && dev->state != TUSB_DEVICE_STATE_UNPLUG) { + // Invoke callback before close driver + if (tuh_umount_cb) tuh_umount_cb(dev_addr); + // TODO Hub multiple level // 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); @@ -624,7 +623,7 @@ bool enum_task(hcd_event_t* event) } } - tuh_device_mount_succeed_cb(new_addr); + if (tuh_mount_cb) tuh_mount_cb(new_addr); return true; } diff --git a/src/host/usbh.h b/src/host/usbh.h index 390b04053..a2e196c9a 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -90,8 +90,12 @@ static inline bool tuh_device_is_configured(uint8_t dev_addr) // APPLICATION CALLBACK //--------------------------------------------------------------------+ ATTR_WEAK uint8_t tuh_device_attached_cb (tusb_desc_device_t const *p_desc_device) ATTR_WARN_UNUSED_RESULT; -ATTR_WEAK void tuh_device_mount_succeed_cb (uint8_t dev_addr); -ATTR_WEAK void tuh_device_mount_failed_cb(tusb_error_t error, tusb_desc_device_t const *p_desc_device); // TODO refractor remove desc_device + +/** Callback invoked when device is mounted (configured) */ +ATTR_WEAK void tuh_mount_cb (uint8_t dev_addr); + +/** Callback invoked when device is unmounted (bus reset/unplugged) */ +ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr); //--------------------------------------------------------------------+ // CLASS-USBH & INTERNAL API diff --git a/tests/lpc18xx_43xx/test/host/cdc/test_cdc_host.c b/tests/lpc18xx_43xx/test/host/cdc/test_cdc_host.c index fe7d05516..1b73e649f 100644 --- a/tests/lpc18xx_43xx/test/host/cdc/test_cdc_host.c +++ b/tests/lpc18xx_43xx/test/host/cdc/test_cdc_host.c @@ -89,7 +89,7 @@ void test_cdch_open_failed_to_open_notification_endpoint(void) hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_notification, TUSB_CLASS_CDC, null_hdl); //------------- CUT -------------// - TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open_subtask(dev_addr, p_comm_interface, &length)); + TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open(dev_addr, p_comm_interface, &length)); } @@ -102,7 +102,7 @@ void test_cdch_open_failed_to_open_data_endpoint_out(void) hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_out, TUSB_CLASS_CDC, null_hdl); //------------- CUT -------------// - TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open_subtask(dev_addr, p_comm_interface, &length)); + TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open(dev_addr, p_comm_interface, &length)); } @@ -116,7 +116,7 @@ void test_cdch_open_failed_to_open_data_endpoint_in(void) hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_in, TUSB_CLASS_CDC, null_hdl); //------------- CUT -------------// - TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open_subtask(dev_addr, p_comm_interface, &length)); + TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open(dev_addr, p_comm_interface, &length)); } @@ -135,7 +135,7 @@ void test_cdch_open_length_check(void) tusbh_cdc_mounted_cb_Expect(dev_addr); //------------- CUT -------------// - TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) ); + TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) ); TEST_ASSERT_EQUAL(expected_length, length); } @@ -147,7 +147,7 @@ void test_cdch_open_interface_number_check(void) tusbh_cdc_mounted_cb_Expect(dev_addr); //------------- CUT -------------// - TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) ); + TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) ); TEST_ASSERT_EQUAL(1, p_cdc->interface_number); @@ -160,7 +160,7 @@ void test_cdch_open_protocol_check(void) tusbh_cdc_mounted_cb_Expect(dev_addr); //------------- CUT -------------// - TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) ); + TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) ); TEST_ASSERT_EQUAL(p_comm_interface->bInterfaceProtocol, p_cdc->interface_protocol); @@ -173,7 +173,7 @@ void test_cdch_open_acm_capacity_check(void) tusbh_cdc_mounted_cb_Expect(dev_addr); //------------- CUT -------------// - TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) ); + TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) ); TEST_ASSERT_EQUAL_MEMORY(&cdc_config_descriptor.cdc_acm.bmCapabilities, &p_cdc->acm_capability, 1); } @@ -192,7 +192,7 @@ void test_cdch_close_device(void) hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_in, TUSB_CLASS_CDC, pipe_int); tusbh_cdc_mounted_cb_Expect(dev_addr); - TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) ); + TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) ); hcd_pipe_close_ExpectAndReturn(pipe_notification , TUSB_ERROR_NONE); hcd_pipe_close_ExpectAndReturn(pipe_int , TUSB_ERROR_NONE); -- cgit v1.3.1 From 6d86db3977aa60963d78d1bceaa7e3def60f1957 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Dec 2018 11:51:31 +0700 Subject: rename edpt_dir/number/addr to tu_edpt_* --- docs/porting.md | 6 +++--- examples/host/cdc_msc_hid/src/tusb_config.h | 2 +- src/class/cdc/cdc_device.h | 2 +- src/class/cdc/cdc_host.c | 2 +- src/class/custom/custom_device.c | 2 +- src/class/custom/custom_device.h | 2 +- src/class/hid/hid_device.c | 2 +- src/class/hid/hid_device.h | 2 +- src/class/msc/msc_device.h | 2 +- src/class/msc/msc_host.c | 2 +- src/common/tusb_types.h | 6 +++--- src/device/usbd.c | 10 ++++----- src/host/ehci/ehci.c | 30 +++++++++++--------------- src/host/ohci/ohci.c | 10 ++++----- src/host/usbh.c | 10 ++++----- src/portable/microchip/samd21/dcd_samd21.c | 24 ++++++++++----------- src/portable/microchip/samd51/dcd_samd51.c | 24 ++++++++++----------- src/portable/nordic/nrf5x/dcd_nrf5x.c | 22 +++++++++---------- src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c | 6 +++--- src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 8 +++---- src/portable/nxp/lpc18_43/dcd_lpc18_43.c | 24 ++++++++++----------- src/portable/nxp/lpc18_43/hcd_lpc18_43.c | 7 ++++++ tests/support/tusb_config.h | 2 +- 23 files changed, 104 insertions(+), 103 deletions(-) (limited to 'src/device') diff --git a/docs/porting.md b/docs/porting.md index 5a464fa6b..040112d9c 100644 --- a/docs/porting.md +++ b/docs/porting.md @@ -113,10 +113,10 @@ As before with `dcd_event_bus_signal` the first argument is the USB peripheral n Endpoints are the core of the USB data transfer process. They come in a few forms such as control, isochronous, bulk, and interrupt. We won't cover the details here except with some caveats in open below. In general, data is transferred by setting up a buffer of a given length to be transferred on a given endpoint address and then waiting for an interrupt to signal that the transfer is finished. Further details below. -Endpoints within USB have an address which encodes both the number and direction of an endpoint. TinyUSB provides `edpt_number` and `edpt_dir` to unpack this data from the address. Here is a snippet that does it. +Endpoints within USB have an address which encodes both the number and direction of an endpoint. TinyUSB provides `tu_edpt_number` and `tu_edpt_dir` to unpack this data from the address. Here is a snippet that does it. - uint8_t epnum = edpt_number(ep_addr); - uint8_t dir = edpt_dir(ep_addr); + uint8_t epnum = tu_edpt_number(ep_addr); + uint8_t dir = tu_edpt_dir(ep_addr); ##### dcd_edpt_open diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 07764fa90..cc06ae93d 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -84,7 +84,7 @@ #define CFG_TUH_CDC 1 #define CFG_TUH_HID_KEYBOARD 0 #define CFG_TUH_HID_MOUSE 0 -#define CFG_TUSB_HOST_HID_GENERIC 0 // (not yet supported) +#define CFG_TUSB_HOST_HID_GENERIC 0 // (not yet supported) #define CFG_TUH_MSC 0 #define CFG_TUSB_HOST_DEVICE_MAX (CFG_TUH_HUB ? 5 : 1) // normal hub has 4 ports diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 0a17aa916..9dd837d7d 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -116,7 +116,7 @@ void cdcd_init (void); tusb_error_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t cdcd_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes); +tusb_error_t cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); void cdcd_reset (uint8_t rhport); #endif diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index 159a13990..d660cc39f 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -196,7 +196,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it TU_ASSERT(hcd_edpt_open(rhport, dev_addr, ep_desc)); - if ( edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) + if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { p_cdc->ep_in = ep_desc->bEndpointAddress; }else diff --git a/src/class/custom/custom_device.c b/src/class/custom/custom_device.c index 194963c17..5abffacf6 100644 --- a/src/class/custom/custom_device.c +++ b/src/class/custom/custom_device.c @@ -94,7 +94,7 @@ bool cusd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque return false; } -tusb_error_t cusd_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t event, uint32_t xferred_bytes) +tusb_error_t cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { return TUSB_ERROR_NONE; } diff --git a/src/class/custom/custom_device.h b/src/class/custom/custom_device.h index 0a8f05e7d..f223a4d3c 100644 --- a/src/class/custom/custom_device.h +++ b/src/class/custom/custom_device.h @@ -66,7 +66,7 @@ void cusd_init(void); tusb_error_t cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); bool cusd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request); bool cusd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t cusd_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t event, uint32_t xferred_bytes); +tusb_error_t cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void cusd_reset(uint8_t rhport); #endif diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 6e2ea27db..fa76ee015 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -510,7 +510,7 @@ bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const return true; } -tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t event, uint32_t xferred_bytes) +tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { // nothing to do return TUSB_ERROR_NONE; diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 7aff7f34d..42c61a47b 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -380,7 +380,7 @@ void hidd_init(void); tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); bool hidd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t event, uint32_t xferred_bytes); +tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void hidd_reset(uint8_t rhport); #endif diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 4053679b8..55bfba83c 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -174,7 +174,7 @@ void mscd_init(void); tusb_error_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); bool mscd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t event, uint32_t xferred_bytes); +tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void mscd_reset(uint8_t rhport); #endif diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index 2c9b2dec7..dd771440d 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -309,7 +309,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it TU_ASSERT(hcd_edpt_open(rhport, dev_addr, ep_desc)); - if ( edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) + if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { p_msc->ep_in = ep_desc->bEndpointAddress; }else diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index d0a10acef..cbddd4156 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -376,18 +376,18 @@ static inline uint8_t bm_request_type(uint8_t direction, uint8_t type, uint8_t r //--------------------------------------------------------------------+ // Get direction from Endpoint address -static inline tusb_dir_t edpt_dir(uint8_t addr) +static inline tusb_dir_t tu_edpt_dir(uint8_t addr) { return (addr & TUSB_DIR_IN_MASK) ? TUSB_DIR_IN : TUSB_DIR_OUT; } // Get Endpoint number from address -static inline uint8_t edpt_number(uint8_t addr) +static inline uint8_t tu_edpt_number(uint8_t addr) { return addr & (~TUSB_DIR_IN_MASK); } -static inline uint8_t edpt_addr(uint8_t num, uint8_t dir) +static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir) { return num | (dir ? TUSB_DIR_IN_MASK : 0); } diff --git a/src/device/usbd.c b/src/device/usbd.c index ad40e3c9f..b63dcb131 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -246,14 +246,14 @@ static void usbd_task_body(void) // Invoke the class callback associated with the endpoint address uint8_t const ep_addr = event.xfer_complete.ep_addr; - if ( 0 == edpt_number(ep_addr) ) + if ( 0 == tu_edpt_number(ep_addr) ) { // control transfer DATA stage callback usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len); } else { - uint8_t const drv_id = _usbd_dev.ep2drv[edpt_number(ep_addr)][edpt_dir(ep_addr)]; + uint8_t const drv_id = _usbd_dev.ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)]; TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,); usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len); @@ -484,7 +484,7 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, { uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; - ep2drv[edpt_number(ep_addr)][edpt_dir(ep_addr)] = driver_id; + ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = driver_id; } len += descriptor_len(p_desc); @@ -576,7 +576,7 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) case DCD_EVENT_XFER_COMPLETE: // skip zero-length control status complete event, should dcd notifies us. - if ( 0 == edpt_number(event->xfer_complete.ep_addr) && event->xfer_complete.len == 0) break; + if ( 0 == tu_edpt_number(event->xfer_complete.ep_addr) && event->xfer_complete.len == 0) break; osal_queue_send(_usbd_q, event, in_isr); TU_ASSERT(event->xfer_complete.result == XFER_RESULT_SUCCESS,); @@ -633,7 +633,7 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_ TU_ASSERT( dcd_edpt_open(rhport, ep_desc), TUSB_ERROR_DCD_OPEN_PIPE_FAILED ); - if ( edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) + if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { (*ep_in) = ep_desc->bEndpointAddress; }else diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index ef5510a6d..278765585 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -48,9 +48,6 @@ #include "../usbh_hcd.h" #include "ehci.h" -// TODO remove -#include "chip.h" - //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ @@ -61,13 +58,8 @@ // Periodic frame list must be 4K alignment CFG_TUSB_MEM_SECTION ATTR_ALIGNED(4096) static ehci_data_t ehci_data; -//------------- Validation -------------// -// TODO static assert for memory placement on some known MCU such as lpc43xx - -uint32_t hcd_ehci_register_addr(uint8_t rhport) -{ - return (uint32_t) (rhport ? &LPC_USB1->USBCMD_H : &LPC_USB0->USBCMD_H ); -} +// EHCI portable +uint32_t hcd_ehci_register_addr(uint8_t rhport); //--------------------------------------------------------------------+ // PROTOTYPE @@ -279,7 +271,8 @@ static bool ehci_init(uint8_t hostid) return true; } -static void hcd_controller_stop(uint8_t rhport) +#if 0 +static void ehci_stop(uint8_t rhport) { (void) rhport; @@ -290,6 +283,7 @@ static void hcd_controller_stop(uint8_t rhport) // USB Spec: controller has to stop within 16 uframe = 2 frames while( regs->status_bm.hc_halted == 0 ) {} } +#endif //--------------------------------------------------------------------+ // CONTROL PIPE API @@ -298,8 +292,8 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); // FIXME control only for now if ( epnum == 0 ) @@ -505,7 +499,7 @@ static void qhd_xfer_complete_isr(ehci_qhd_t * p_qhd) { // end of request // call USBH callback - hcd_event_xfer_complete(p_qhd->dev_addr, edpt_addr(p_qhd->ep_number, p_qhd->pid == EHCI_PID_IN ? 1 : 0), XFER_RESULT_SUCCESS, p_qhd->total_xferred_bytes); + hcd_event_xfer_complete(p_qhd->dev_addr, tu_edpt_addr(p_qhd->ep_number, p_qhd->pid == EHCI_PID_IN ? 1 : 0), XFER_RESULT_SUCCESS, p_qhd->total_xferred_bytes); p_qhd->total_xferred_bytes = 0; } } @@ -592,7 +586,7 @@ static void qhd_xfer_error_isr(ehci_qhd_t * p_qhd) } // call USBH callback - hcd_event_xfer_complete(p_qhd->dev_addr, edpt_addr(p_qhd->ep_number, p_qhd->pid == EHCI_PID_IN ? 1 : 0), error_event, p_qhd->total_xferred_bytes); + hcd_event_xfer_complete(p_qhd->dev_addr, tu_edpt_addr(p_qhd->ep_number, p_qhd->pid == EHCI_PID_IN ? 1 : 0), error_event, p_qhd->total_xferred_bytes); p_qhd->total_xferred_bytes = 0; } @@ -718,7 +712,7 @@ static inline ehci_qhd_t* qhd_get_from_addr(uint8_t dev_addr, uint8_t ep_addr) for(uint32_t i=0; idev_addr = dev_addr; p_qhd->fl_inactive_next_xact = 0; - p_qhd->ep_number = edpt_number(ep_desc->bEndpointAddress); + p_qhd->ep_number = tu_edpt_number(ep_desc->bEndpointAddress); p_qhd->ep_speed = _usbh_devices[dev_addr].speed; p_qhd->data_toggle_control= (xfer_type == TUSB_XFER_CONTROL) ? 1 : 0; p_qhd->head_list_flag = (dev_addr == 0) ? 1 : 0; // addr0's endpoint is the static asyn list head @@ -826,7 +820,7 @@ static void qhd_init(ehci_qhd_t *p_qhd, uint8_t dev_addr, tusb_desc_endpoint_t c p_qhd->removing = 0; p_qhd->p_qtd_list_head = NULL; p_qhd->p_qtd_list_tail = NULL; - p_qhd->pid = edpt_dir(ep_desc->bEndpointAddress) ? EHCI_PID_IN : EHCI_PID_OUT; // PID for TD under this endpoint + p_qhd->pid = tu_edpt_dir(ep_desc->bEndpointAddress) ? EHCI_PID_IN : EHCI_PID_OUT; // PID for TD under this endpoint //------------- active, but no TD list -------------// p_qhd->qtd_overlay.halted = 0; diff --git a/src/host/ohci/ohci.c b/src/host/ohci/ohci.c index c35eaa77b..bebb8a2ef 100644 --- a/src/host/ohci/ohci.c +++ b/src/host/ohci/ohci.c @@ -315,8 +315,8 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); // FIXME control only for now if ( epnum == 0 ) @@ -344,14 +344,14 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * //--------------------------------------------------------------------+ static inline ohci_ed_t * ed_from_addr(uint8_t dev_addr, uint8_t ep_addr) { - if ( edpt_number(ep_addr) == 0 ) return &ohci_data.control[dev_addr].ed; + if ( tu_edpt_number(ep_addr) == 0 ) return &ohci_data.control[dev_addr].ed; ohci_ed_t* ed_pool = ohci_data.ed_pool; for(uint32_t i=0; idev_addr, - edpt_addr(p_ed->ep_number, p_ed->pid == OHCI_PID_IN), + tu_edpt_addr(p_ed->ep_number, p_ed->pid == OHCI_PID_IN), event, xferred_bytes); } diff --git a/src/host/usbh.c b/src/host/usbh.c index 962dc04dc..369ddd2a5 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -205,12 +205,12 @@ bool usbh_control_xfer (uint8_t dev_addr, tusb_control_request_t* request, uint8 // Data stage : first data toggle is always 1 if ( request->wLength ) { - hcd_edpt_xfer(rhport, dev_addr, edpt_addr(0, request->bmRequestType_bit.direction), data, request->wLength); + hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, request->bmRequestType_bit.direction), data, request->wLength); TU_VERIFY(osal_semaphore_wait(dev->control.sem_hdl, OSAL_TIMEOUT_NORMAL)); } // Status : data toggle is always 1 - hcd_edpt_xfer(rhport, dev_addr, edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0); + hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0); TU_VERIFY(osal_semaphore_wait(dev->control.sem_hdl, OSAL_TIMEOUT_NORMAL)); osal_mutex_unlock(dev->control.mutex_hdl); @@ -249,7 +249,7 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t ev { usbh_device_t* dev = &_usbh_devices[ dev_addr ]; - if (0 == edpt_number(ep_addr)) + if (0 == tu_edpt_number(ep_addr)) { dev->control.pipe_status = event; // usbh_devices[ pipe_hdl.dev_addr ].control.xferred_bytes = xferred_bytes; not yet neccessary @@ -257,7 +257,7 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t ev } else { - uint8_t drv_id = dev->ep2drv[edpt_number(ep_addr)][edpt_dir(ep_addr)]; + uint8_t drv_id = dev->ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)]; TU_ASSERT(drv_id < USBH_CLASS_DRIVER_COUNT, ); if (usbh_class_drivers[drv_id].isr) @@ -689,7 +689,7 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, { uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; - ep2drv[ edpt_number(ep_addr) ][ edpt_dir(ep_addr) ] = driver_id; + ep2drv[ tu_edpt_number(ep_addr) ][ tu_edpt_dir(ep_addr) ] = driver_id; } len += descriptor_len(p_desc); diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index 57c4c4d9d..af0721ec3 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -132,8 +132,8 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) { (void) rhport; - uint8_t const epnum = edpt_number(desc_edpt->bEndpointAddress); - uint8_t const dir = edpt_dir(desc_edpt->bEndpointAddress); + uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress); + uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress); UsbDeviceDescBank* bank = &sram_registers[epnum][dir]; uint32_t size_value = 0; @@ -168,8 +168,8 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); UsbDeviceDescBank* bank = &sram_registers[epnum][dir]; UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; @@ -208,19 +208,19 @@ bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr) return false; } - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - return (edpt_dir(ep_addr) == TUSB_DIR_IN ) ? ep->EPINTFLAG.bit.STALL1 : ep->EPINTFLAG.bit.STALL0; + return (tu_edpt_dir(ep_addr) == TUSB_DIR_IN ) ? ep->EPINTFLAG.bit.STALL1 : ep->EPINTFLAG.bit.STALL0; } void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - if (edpt_dir(ep_addr) == TUSB_DIR_IN) { + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; } else { ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0; @@ -236,10 +236,10 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - if (edpt_dir(ep_addr) == TUSB_DIR_IN) { + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { ep->EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_STALLRQ1; } else { ep->EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_STALLRQ0; @@ -253,10 +253,10 @@ bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr) // USBD shouldn't check control endpoint state if ( 0 == ep_addr ) return false; - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - if (edpt_dir(ep_addr) == TUSB_DIR_IN) { + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { return ep->EPINTFLAG.bit.TRCPT1 == 0 && ep->EPSTATUS.bit.BK1RDY == 1; } return ep->EPINTFLAG.bit.TRCPT0 == 0 && ep->EPSTATUS.bit.BK0RDY == 1; diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index ba53d5598..ce66b8245 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -137,8 +137,8 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) { (void) rhport; - uint8_t const epnum = edpt_number(desc_edpt->bEndpointAddress); - uint8_t const dir = edpt_dir(desc_edpt->bEndpointAddress); + uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress); + uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress); UsbDeviceDescBank* bank = &sram_registers[epnum][dir]; uint32_t size_value = 0; @@ -173,8 +173,8 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); UsbDeviceDescBank* bank = &sram_registers[epnum][dir]; UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; @@ -212,19 +212,19 @@ bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr) return false; } - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - return (edpt_dir(ep_addr) == TUSB_DIR_IN ) ? ep->EPINTFLAG.bit.STALL1 : ep->EPINTFLAG.bit.STALL0; + return (tu_edpt_dir(ep_addr) == TUSB_DIR_IN ) ? ep->EPINTFLAG.bit.STALL1 : ep->EPINTFLAG.bit.STALL0; } void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - if (edpt_dir(ep_addr) == TUSB_DIR_IN) { + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; } else { ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0; @@ -240,10 +240,10 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - if (edpt_dir(ep_addr) == TUSB_DIR_IN) { + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { ep->EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_STALLRQ1; } else { ep->EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_STALLRQ0; @@ -257,10 +257,10 @@ bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr) // USBD shouldn't check control endpoint state if ( 0 == ep_addr ) return false; - uint8_t const epnum = edpt_number(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; - if (edpt_dir(ep_addr) == TUSB_DIR_IN) { + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { return ep->EPINTFLAG.bit.TRCPT1 == 0 && ep->EPSTATUS.bit.BK1RDY == 1; } return ep->EPINTFLAG.bit.TRCPT0 == 0 && ep->EPSTATUS.bit.BK0RDY == 1; diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index c7a4f413f..ad5090eb0 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -225,8 +225,8 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) { (void) rhport; - uint8_t const epnum = edpt_number(desc_edpt->bEndpointAddress); - uint8_t const dir = edpt_dir(desc_edpt->bEndpointAddress); + uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress); + uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress); _dcd.xfer[epnum][dir].mps = desc_edpt->wMaxPacketSize.size; @@ -248,8 +248,8 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t { (void) rhport; - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); xfer_td_t* xfer = get_td(epnum, dir); @@ -295,15 +295,15 @@ bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr) // control is never got halted if ( ep_addr == 0 ) return false; - uint8_t const epnum = edpt_number(ep_addr); - return (edpt_dir(ep_addr) == TUSB_DIR_IN ) ? NRF_USBD->HALTED.EPIN[epnum] : NRF_USBD->HALTED.EPOUT[epnum]; + uint8_t const epnum = tu_edpt_number(ep_addr); + return (tu_edpt_dir(ep_addr) == TUSB_DIR_IN ) ? NRF_USBD->HALTED.EPIN[epnum] : NRF_USBD->HALTED.EPOUT[epnum]; } void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - if ( edpt_number(ep_addr) == 0 ) + if ( tu_edpt_number(ep_addr) == 0 ) { NRF_USBD->TASKS_EP0STALL = 1; }else @@ -318,7 +318,7 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - if ( edpt_number(ep_addr) ) + if ( tu_edpt_number(ep_addr) ) { NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep_addr; __ISB(); __DSB(); @@ -330,10 +330,10 @@ bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr) (void) rhport; // USBD shouldn't check control endpoint state - if ( 0 == edpt_number(ep_addr) ) return false; + if ( 0 == tu_edpt_number(ep_addr) ) return false; - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); xfer_td_t* xfer = get_td(epnum, dir); diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c index aa7f5e7e6..c910bf1ec 100644 --- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c +++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c @@ -189,7 +189,7 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) { (void) rhport; - if ( edpt_number(ep_addr) == 0 ) + if ( tu_edpt_number(ep_addr) == 0 ) { // TODO cannot able to STALL Control OUT endpoint !!!!! FIXME try some walk-around _dcd.ep[0][0].stall = _dcd.ep[1][0].stall = 1; @@ -209,11 +209,11 @@ bool dcd_edpt_stalled(uint8_t rhport, uint8_t ep_addr) return _dcd.ep[ep_id][0].stall; } -void dcd_edpt_clear_stall(uint8_t rhport, uint8_t edpt_addr) +void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) { (void) rhport; - uint8_t const ep_id = ep_addr2id(edpt_addr); + uint8_t const ep_id = ep_addr2id(ep_addr); _dcd.ep[ep_id][0].stall = 0; _dcd.ep[ep_id][0].toggle_reset = 1; diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index 84eb0c121..82d3c3cac 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -285,7 +285,7 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc) { (void) rhport; - uint8_t const epnum = edpt_number(p_endpoint_desc->bEndpointAddress); + uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress); uint8_t const ep_id = ep_addr2idx(p_endpoint_desc->bEndpointAddress); // Endpoint type is fixed to endpoint number @@ -336,7 +336,7 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) { (void) rhport; - if ( edpt_number(ep_addr) == 0 ) + if ( tu_edpt_number(ep_addr) == 0 ) { sie_write(SIE_CMDCODE_ENDPOINT_SET_STATUS+0, 1, SIE_SET_ENDPOINT_STALLED_MASK | SIE_SET_ENDPOINT_CONDITION_STALLED_MASK); }else @@ -394,9 +394,9 @@ static bool control_xact(uint8_t rhport, uint8_t dir, uint8_t * buffer, uint8_t bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { // Control transfer is not DMA support, and must be done in slave mode - if ( edpt_number(ep_addr) == 0 ) + if ( tu_edpt_number(ep_addr) == 0 ) { - return control_xact(rhport, edpt_dir(ep_addr), buffer, (uint8_t) total_bytes); + return control_xact(rhport, tu_edpt_dir(ep_addr), buffer, (uint8_t) total_bytes); } else { diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c index d524d9dea..70b34d848 100644 --- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c @@ -205,8 +205,8 @@ static void qtd_init(dcd_qtd_t* p_qtd, void * data_ptr, uint16_t total_bytes) //--------------------------------------------------------------------+ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) { - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); if ( epnum == 0) { @@ -220,16 +220,16 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr) { - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); return LPC_USB[rhport]->ENDPTCTRL[epnum] & (ENDPTCTRL_MASK_STALL << (dir ? 16 : 0)); } void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) { - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); // data toggle also need to be reset LPC_USB[rhport]->ENDPTCTRL[epnum] |= ENDPTCTRL_MASK_TOGGLE_RESET << ( dir ? 16 : 0 ); @@ -241,8 +241,8 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc) // TODO not support ISO yet TU_VERIFY ( p_endpoint_desc->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS); - uint8_t const epnum = edpt_number(p_endpoint_desc->bEndpointAddress); - uint8_t const dir = edpt_dir(p_endpoint_desc->bEndpointAddress); + uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress); + uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress); uint8_t const ep_idx = 2*epnum + dir; // USB0 has 5, USB1 has 3 non-control endpoints @@ -264,8 +264,8 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc) bool dcd_edpt_busy(uint8_t rhport, uint8_t ep_addr) { - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const ep_idx = 2*epnum + dir; dcd_qtd_t * p_qtd = &dcd_data_ptr[rhport]->qtd[ep_idx]; @@ -276,8 +276,8 @@ bool dcd_edpt_busy(uint8_t rhport, uint8_t ep_addr) bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { - uint8_t const epnum = edpt_number(ep_addr); - uint8_t const dir = edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const ep_idx = 2*epnum + dir; if ( epnum == 0 ) diff --git a/src/portable/nxp/lpc18_43/hcd_lpc18_43.c b/src/portable/nxp/lpc18_43/hcd_lpc18_43.c index efdf8fea4..092030056 100644 --- a/src/portable/nxp/lpc18_43/hcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/hcd_lpc18_43.c @@ -42,6 +42,8 @@ #include "chip.h" +// LPC18xx and 43xx use EHCI driver + void hcd_int_enable(uint8_t rhport) { NVIC_EnableIRQ(rhport ? USB1_IRQn : USB0_IRQn); @@ -52,4 +54,9 @@ void hcd_int_disable(uint8_t rhport) NVIC_DisableIRQ(rhport ? USB1_IRQn : USB0_IRQn); } +uint32_t hcd_ehci_register_addr(uint8_t rhport) +{ + return (uint32_t) (rhport ? &LPC_USB1->USBCMD_H : &LPC_USB0->USBCMD_H ); +} + #endif diff --git a/tests/support/tusb_config.h b/tests/support/tusb_config.h index 4ff3965e1..fe3d59ef1 100644 --- a/tests/support/tusb_config.h +++ b/tests/support/tusb_config.h @@ -55,7 +55,7 @@ #define CFG_TUSB_HOST_DEVICE_MAX 5 // TODO be a part of HUB config //------------- CLASS -------------// -#define CFG_TUH_HUB 0 +#define CFG_TUH_HUB 1 #define CFG_TUH_HID_KEYBOARD 1 #define CFG_TUH_HID_MOUSE 1 #define CFG_TUH_MSC 1 -- cgit v1.3.1 From 6c0b0917e15ca0acf7ce9bed0f3735a794abb807 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Dec 2018 12:01:15 +0700 Subject: rename descriptor_* helper to tu_desc_* --- src/class/cdc/cdc_device.c | 18 +++++++++--------- src/class/cdc/cdc_host.c | 10 +++++----- src/class/custom/custom_device.c | 2 +- src/class/custom/custom_host.c | 4 ++-- src/class/msc/msc_device.c | 10 +++++----- src/class/msc/msc_host.c | 4 ++-- src/common/tusb_types.h | 13 +++++++------ src/device/usbd.c | 14 +++++++------- src/host/hub.c | 8 ++++---- src/host/usbh.c | 14 +++++++------- 10 files changed, 49 insertions(+), 48 deletions(-) (limited to 'src/device') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index d477a0d23..a865ba3c5 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -229,13 +229,13 @@ void cdcd_reset(uint8_t rhport) } } -tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length) +tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length) { - if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != p_interface_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS; + if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != itf_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS; // Only support AT commands, no protocol and vendor specific commands. - if ( !(tu_within(CDC_COMM_PROTOCOL_NONE, p_interface_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || - p_interface_desc->bInterfaceProtocol == 0xff ) ) + if ( !(tu_within(CDC_COMM_PROTOCOL_NONE, itf_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || + itf_desc->bInterfaceProtocol == 0xff ) ) { return TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL; } @@ -252,16 +252,16 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface } //------------- Control Interface -------------// - p_cdc->itf_num = p_interface_desc->bInterfaceNumber; + p_cdc->itf_num = itf_desc->bInterfaceNumber; - uint8_t const * p_desc = descriptor_next ( (uint8_t const *) p_interface_desc ); + uint8_t const * p_desc = tu_desc_next( itf_desc ); (*p_length) = sizeof(tusb_desc_interface_t); // Communication Functional Descriptors while( TUSB_DESC_CLASS_SPECIFIC == p_desc[DESC_OFFSET_TYPE] ) { (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE]) @@ -271,7 +271,7 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface p_cdc->ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress; (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } //------------- Data Interface (if any) -------------// @@ -280,7 +280,7 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface { // next to endpoint descritpor (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); // Open endpoint pair with usbd helper tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) p_desc; diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index d660cc39f..d6767d0a7 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -146,7 +146,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it uint8_t const * p_desc; cdch_data_t * p_cdc; - p_desc = descriptor_next ( (uint8_t const *) itf_desc ); + p_desc = tu_desc_next(itf_desc); p_cdc = &cdch_data[dev_addr-1]; p_cdc->itf_num = itf_desc->bInterfaceNumber; @@ -165,7 +165,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it } (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE]) @@ -177,7 +177,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it p_cdc->ep_notif = ep_desc->bEndpointAddress; (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } //------------- Data Interface (if any) -------------// @@ -185,7 +185,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) ) { (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); // data endpoints expected to be in pairs for(uint32_t i=0; i<2; i++) @@ -205,7 +205,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it } (*p_length) += p_desc[DESC_OFFSET_LEN]; - p_desc = descriptor_next( p_desc ); + p_desc = tu_desc_next( p_desc ); } } diff --git a/src/class/custom/custom_device.c b/src/class/custom/custom_device.c index 5abffacf6..bec6f42d1 100644 --- a/src/class/custom/custom_device.c +++ b/src/class/custom/custom_device.c @@ -76,7 +76,7 @@ tusb_error_t cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, cusd_interface_t* p_itf = &_cusd_itf; // Open endpoint pair with usbd helper - tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) p_desc_itf ); + tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next( (uint8_t const*) p_desc_itf ); TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_itf->ep_out, &p_itf->ep_in) ); p_itf->itf_num = p_desc_itf->bInterfaceNumber; diff --git a/src/class/custom/custom_host.c b/src/class/custom/custom_host.c index e840a2829..894e641e8 100644 --- a/src/class/custom/custom_host.c +++ b/src/class/custom/custom_host.c @@ -111,7 +111,7 @@ tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_ { // FIXME quick hack to test lpc1k custom class with 2 bulk endpoints uint8_t const *p_desc = (uint8_t const *) p_interface_desc; - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); //------------- Bulk Endpoints Descriptor -------------// for(uint32_t i=0; i<2; i++) @@ -124,7 +124,7 @@ tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_ *p_pipe_hdl = hcd_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 = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } (*p_length) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 83c9990d8..48f23688d 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -150,19 +150,19 @@ void mscd_reset(uint8_t rhport) tu_memclr(&_mscd_itf, sizeof(mscd_interface_t)); } -tusb_error_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_t *p_len) +tusb_error_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_len) { // only support SCSI's BOT protocol - TU_VERIFY( ( MSC_SUBCLASS_SCSI == p_desc_itf->bInterfaceSubClass && - MSC_PROTOCOL_BOT == p_desc_itf->bInterfaceProtocol ), TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL ); + TU_VERIFY( ( MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass && + MSC_PROTOCOL_BOT == itf_desc->bInterfaceProtocol ), TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL ); mscd_interface_t * p_msc = &_mscd_itf; // Open endpoint pair with usbd helper - tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) p_desc_itf ); + tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next( itf_desc ); TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in) ); - p_msc->itf_num = p_desc_itf->bInterfaceNumber; + p_msc->itf_num = itf_desc->bInterfaceNumber; (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); //------------- Queue Endpoint OUT for Command Block Wrapper -------------// diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c index dd771440d..86669b279 100644 --- a/src/class/msc/msc_host.c +++ b/src/class/msc/msc_host.c @@ -300,7 +300,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it msch_interface_t* p_msc = &msch_data[dev_addr-1]; //------------- Open Data Pipe -------------// - tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) itf_desc ); + tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); for(uint32_t i=0; i<2; i++) { @@ -317,7 +317,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it p_msc->ep_out = ep_desc->bEndpointAddress; } - ep_desc = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) ep_desc ); + ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc); } p_msc->itf_numr = itf_desc->bInterfaceNumber; diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index cbddd4156..cf02952ab 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -395,19 +395,20 @@ static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir) //--------------------------------------------------------------------+ // Descriptor helper //--------------------------------------------------------------------+ -static inline uint8_t const * descriptor_next(uint8_t const p_desc[]) +static inline uint8_t const * tu_desc_next(void const* desc) { - return p_desc + p_desc[DESC_OFFSET_LEN]; + uint8_t const* desc8 = (uint8_t const*) desc; + return desc8 + desc8[DESC_OFFSET_LEN]; } -static inline uint8_t descriptor_type(uint8_t const p_desc[]) +static inline uint8_t tu_desc_type(void const* desc) { - return p_desc[DESC_OFFSET_TYPE]; + return ((uint8_t const*) desc)[DESC_OFFSET_TYPE]; } -static inline uint8_t descriptor_len(uint8_t const p_desc[]) +static inline uint8_t tu_desc_len(void const* desc) { - return p_desc[DESC_OFFSET_LEN]; + return ((uint8_t const*) desc)[DESC_OFFSET_LEN]; } // Length of the string descriptors in bytes with slen characters diff --git a/src/device/usbd.c b/src/device/usbd.c index b63dcb131..096437747 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -436,12 +436,12 @@ static bool process_set_config(uint8_t rhport) while( p_desc < desc_cfg + cfg_len ) { // Each interface always starts with Interface or Association descriptor - if ( TUSB_DESC_INTERFACE_ASSOCIATION == descriptor_type(p_desc) ) + if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) ) { - p_desc = descriptor_next(p_desc); // ignore Interface Association + p_desc = tu_desc_next(p_desc); // ignore Interface Association }else { - TU_ASSERT( TUSB_DESC_INTERFACE == descriptor_type(p_desc) ); + TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) ); tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc; @@ -480,15 +480,15 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, while( len < desc_len ) { - if ( TUSB_DESC_ENDPOINT == descriptor_type(p_desc) ) + 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 += descriptor_len(p_desc); - p_desc = descriptor_next(p_desc); + len += tu_desc_len(p_desc); + p_desc = tu_desc_next(p_desc); } } @@ -641,7 +641,7 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_ (*ep_out) = ep_desc->bEndpointAddress; } - ep_desc = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) ep_desc ); + ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc); } return TUSB_ERROR_NONE; diff --git a/src/host/hub.c b/src/host/hub.c index e2e91d73d..e9f9dc7de 100644 --- a/src/host/hub.c +++ b/src/host/hub.c @@ -156,21 +156,21 @@ void hub_init(void) // hub_enum_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(hub_enum_semaphore) ); } -bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length) +bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length) { // not support multiple TT yet - if ( p_interface_desc->bInterfaceProtocol > 1 ) return false; + if ( itf_desc->bInterfaceProtocol > 1 ) return false; //------------- Open Interrupt Status Pipe -------------// tusb_desc_endpoint_t const *ep_desc; - ep_desc = (tusb_desc_endpoint_t const *) descriptor_next( (uint8_t const*) p_interface_desc ); + ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); 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)); - hub_data[dev_addr-1].itf_num = p_interface_desc->bInterfaceNumber; + hub_data[dev_addr-1].itf_num = itf_desc->bInterfaceNumber; hub_data[dev_addr-1].ep_status = ep_desc->bEndpointAddress; (*p_length) = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t); diff --git a/src/host/usbh.c b/src/host/usbh.c index 369ddd2a5..bbe60362a 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -560,9 +560,9 @@ bool enum_task(hcd_event_t* event) while( p_desc < _usbh_ctrl_buf + ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength ) { // skip until we see interface descriptor - if ( TUSB_DESC_INTERFACE != descriptor_type(p_desc) ) + if ( TUSB_DESC_INTERFACE != tu_desc_type(p_desc) ) { - p_desc = descriptor_next(p_desc); // skip the descriptor, increase by the descriptor's length + 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; @@ -577,7 +577,7 @@ bool enum_task(hcd_event_t* event) if( drv_id >= USBH_CLASS_DRIVER_COUNT ) { // skip unsupported class - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } else { @@ -589,7 +589,7 @@ bool enum_task(hcd_event_t* event) { // TODO Attach hub to Hub is not currently supported // skip this interface - p_desc = descriptor_next(p_desc); + p_desc = tu_desc_next(p_desc); } else { @@ -685,15 +685,15 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, while( len < desc_len ) { - if ( TUSB_DESC_ENDPOINT == descriptor_type(p_desc) ) + 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 += descriptor_len(p_desc); - p_desc = descriptor_next(p_desc); + len += tu_desc_len(p_desc); + p_desc = tu_desc_next(p_desc); } } -- cgit v1.3.1 From b6cb4757d27fa35564686baab82f7a86094787a7 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Dec 2018 13:00:59 +0700 Subject: change class driver open return type to bool --- src/class/cdc/cdc_device.c | 36 ++++++++++++++++++------------------ src/class/cdc/cdc_device.h | 6 +++--- src/class/hid/hid_device.c | 24 +++++++++++------------- src/class/hid/hid_device.h | 2 +- src/class/msc/msc_device.c | 14 +++++++------- src/class/msc/msc_device.h | 2 +- src/device/usbd.c | 20 ++++++++++---------- src/device/usbd_pvt.h | 2 +- 8 files changed, 52 insertions(+), 54 deletions(-) (limited to 'src/device') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index a865ba3c5..fc124627d 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -229,16 +229,14 @@ void cdcd_reset(uint8_t rhport) } } -tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length) +bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length) { - if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != itf_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS; + // Only support ACM subclass + TU_ASSERT ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass); // Only support AT commands, no protocol and vendor specific commands. - if ( !(tu_within(CDC_COMM_PROTOCOL_NONE, itf_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || - itf_desc->bInterfaceProtocol == 0xff ) ) - { - return TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL; - } + TU_ASSERT(tu_within(CDC_COMM_PROTOCOL_NONE, itf_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) || + itf_desc->bInterfaceProtocol == 0xff); // Find available interface cdcd_interface_t * p_cdc = NULL; @@ -250,23 +248,25 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, u break; } } + TU_ASSERT(p_cdc); //------------- Control Interface -------------// - p_cdc->itf_num = itf_desc->bInterfaceNumber; + p_cdc->itf_num = itf_desc->bInterfaceNumber; uint8_t const * p_desc = tu_desc_next( itf_desc ); (*p_length) = sizeof(tusb_desc_interface_t); // Communication Functional Descriptors - while( TUSB_DESC_CLASS_SPECIFIC == p_desc[DESC_OFFSET_TYPE] ) + while ( TUSB_DESC_CLASS_SPECIFIC == tu_desc_type(p_desc) ) { - (*p_length) += p_desc[DESC_OFFSET_LEN]; + (*p_length) += tu_desc_len(p_desc); p_desc = tu_desc_next(p_desc); } - if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE]) - { // notification endpoint if any - TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), TUSB_ERROR_DCD_OPEN_PIPE_FAILED); + if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) ) + { + // notification endpoint if any + TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc) ); p_cdc->ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress; @@ -278,21 +278,21 @@ tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, u if ( (TUSB_DESC_INTERFACE == p_desc[DESC_OFFSET_TYPE]) && (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) ) { - // next to endpoint descritpor - (*p_length) += p_desc[DESC_OFFSET_LEN]; + // next to endpoint descriptor + (*p_length) += tu_desc_len(p_desc); p_desc = tu_desc_next(p_desc); // Open endpoint pair with usbd helper tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) ); + TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) ); (*p_length) += 2*sizeof(tusb_desc_endpoint_t); } // Prepare for incoming data - TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE), TUSB_ERROR_DCD_EDPT_XFER); + TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE) ); - return TUSB_ERROR_NONE; + return true; } // Invoked when class request DATA stage is finished. diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 9dd837d7d..03aa2485e 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -112,12 +112,12 @@ ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_li //--------------------------------------------------------------------+ #ifdef _TINY_USB_SOURCE_FILE_ -void cdcd_init (void); -tusb_error_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); +void cdcd_init (void); +bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); tusb_error_t cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void cdcd_reset (uint8_t rhport); +void cdcd_reset (uint8_t rhport); #endif diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index fa76ee015..cfb1a698d 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -318,22 +318,22 @@ void hidd_reset(uint8_t rhport) #endif } -tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t *p_len) +bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t *p_len) { uint8_t const *p_desc = (uint8_t const *) desc_itf; - // TODO not support HID OUT Endpoint - TU_ASSERT(desc_itf->bNumEndpoints == 1, ERR_TUD_INVALID_DESCRIPTOR); + // TODO support HID OUT Endpoint + TU_ASSERT(desc_itf->bNumEndpoints == 1); //------------- HID descriptor -------------// - p_desc += p_desc[DESC_OFFSET_LEN]; + p_desc = tu_desc_next(p_desc); tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; - TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType, ERR_TUD_INVALID_DESCRIPTOR); + TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType); //------------- Endpoint Descriptor -------------// - p_desc += p_desc[DESC_OFFSET_LEN]; + p_desc = tu_desc_next(p_desc); tusb_desc_endpoint_t const *desc_edpt = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT(TUSB_DESC_ENDPOINT == desc_edpt->bDescriptorType, ERR_TUD_INVALID_DESCRIPTOR); + TU_ASSERT(TUSB_DESC_ENDPOINT == desc_edpt->bDescriptorType); hidd_interface_t * p_hid = NULL; @@ -374,7 +374,7 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, u } #endif - TU_ASSERT(p_hid, ERR_TUD_INVALID_DESCRIPTOR); + TU_ASSERT(p_hid); p_hid->boot_protocol = true; // default mode is BOOT } /*------------- Generic (multiple report) -------------*/ @@ -386,12 +386,10 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, u p_hid->desc_report = usbd_desc_set->hid_report.generic; p_hid->get_report_cb = tud_hid_generic_get_report_cb; p_hid->set_report_cb = tud_hid_generic_set_report_cb; - - TU_ASSERT(p_hid, ERR_TUD_INVALID_DESCRIPTOR); } - TU_VERIFY(p_hid->desc_report, ERR_TUD_INVALID_DESCRIPTOR); - TU_ASSERT( dcd_edpt_open(rhport, desc_edpt), ERR_TUD_EDPT_OPEN_FAILED ); + TU_ASSERT(p_hid->desc_report); + TU_ASSERT(dcd_edpt_open(rhport, desc_edpt)); p_hid->itf_num = desc_itf->bInterfaceNumber; p_hid->ep_in = desc_edpt->bEndpointAddress; @@ -399,7 +397,7 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, u *p_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t); - return TUSB_ERROR_NONE; + return true; } // Handle class control request diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 42c61a47b..2f0047d81 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -377,7 +377,7 @@ ATTR_WEAK void tud_hid_mouse_set_report_cb(uint8_t report_id, hid_report_type_t #ifdef _TINY_USB_SOURCE_FILE_ void hidd_init(void); -tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); +bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); bool hidd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 48f23688d..14a7e593a 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -150,25 +150,25 @@ void mscd_reset(uint8_t rhport) tu_memclr(&_mscd_itf, sizeof(mscd_interface_t)); } -tusb_error_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_len) +bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_len) { // only support SCSI's BOT protocol - TU_VERIFY( ( MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass && - MSC_PROTOCOL_BOT == itf_desc->bInterfaceProtocol ), TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL ); + TU_ASSERT(MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass && + MSC_PROTOCOL_BOT == itf_desc->bInterfaceProtocol); mscd_interface_t * p_msc = &_mscd_itf; // Open endpoint pair with usbd helper tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next( itf_desc ); - TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in) ); + TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in) ); p_msc->itf_num = itf_desc->bInterfaceNumber; (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); - //------------- Queue Endpoint OUT for Command Block Wrapper -------------// - TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)), TUSB_ERROR_DCD_EDPT_XFER ); + // Prepare for Command Block Wrapper + TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) ); - return TUSB_ERROR_NONE; + return true; } // Handle class control request diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 55bfba83c..80e47d84b 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -171,7 +171,7 @@ ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); #ifdef _TINY_USB_SOURCE_FILE_ void mscd_init(void); -tusb_error_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); +bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); bool mscd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); diff --git a/src/device/usbd.c b/src/device/usbd.c index 096437747..bf7fd950a 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -88,13 +88,13 @@ tud_desc_set_t const* usbd_desc_set = &tud_desc_set; typedef struct { uint8_t class_code; - void (* init ) (void); - tusb_error_t (* 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); + void (* init ) (void); + 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_request_complete ) (uint8_t rhport, tusb_control_request_t const * request); tusb_error_t (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); - void (* sof ) (uint8_t rhport); - void (* reset ) (uint8_t); + void (* sof ) (uint8_t rhport); + void (* reset ) (uint8_t); } usbd_class_driver_t; static usbd_class_driver_t const usbd_class_drivers[] = @@ -458,7 +458,7 @@ static bool process_set_config(uint8_t rhport) _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id; uint16_t itf_len=0; - TU_ASSERT_ERR( usbd_class_drivers[drv_id].open( rhport, desc_itf, &itf_len ), false ); + TU_ASSERT( usbd_class_drivers[drv_id].open( rhport, desc_itf, &itf_len ) ); TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, itf_len, drv_id); @@ -624,14 +624,14 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ //--------------------------------------------------------------------+ // Helper to parse an pair of endpoint descriptors (IN & OUT) -tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_desc, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) +bool usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_desc, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in) { for(int i=0; i<2; i++) { TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType && - xfer_type == ep_desc->bmAttributes.xfer, TUSB_ERROR_DESCRIPTOR_CORRUPTED); + xfer_type == ep_desc->bmAttributes.xfer ); - TU_ASSERT( dcd_edpt_open(rhport, ep_desc), TUSB_ERROR_DCD_OPEN_PIPE_FAILED ); + TU_ASSERT(dcd_edpt_open(rhport, ep_desc)); if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN ) { @@ -644,7 +644,7 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_ ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc); } - return TUSB_ERROR_NONE; + return true; } // Helper to defer an isr function diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index cbe5017bb..798a871b1 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -70,7 +70,7 @@ void usbd_control_stall(uint8_t rhport); /* Helper *------------------------------------------------------------------*/ // helper to parse an pair of In and Out endpoint descriptors. They must be consecutive -tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_desc_ep, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in); +bool usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_desc_ep, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in); void usbd_defer_func( osal_task_func_t func, void* param, bool in_isr ); -- cgit v1.3.1 From c1c501e0c2f3ad2c910b11bb358396c2e57ec624 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Dec 2018 13:12:06 +0700 Subject: change usbd xfer_cb return type to bool --- src/class/cdc/cdc_device.c | 8 ++++---- src/class/cdc/cdc_device.h | 2 +- src/class/custom/custom_device.c | 14 +++++++------- src/class/custom/custom_device.h | 4 ++-- src/class/hid/hid_device.c | 4 ++-- src/class/hid/hid_device.h | 2 +- src/class/msc/msc_device.c | 16 ++++++++-------- src/class/msc/msc_device.h | 2 +- src/device/usbd.c | 2 +- 9 files changed, 27 insertions(+), 27 deletions(-) (limited to 'src/device') diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index fc124627d..7d9e196aa 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -357,7 +357,7 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request return true; } -tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) +bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { (void) result; @@ -382,13 +382,13 @@ tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, // invoke receive callback (if there is still data) if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf); - // prepare for next - TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE), TUSB_ERROR_DCD_EDPT_XFER ); + // prepare for incoming data + TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE) ); } // nothing to do with in and notif endpoint - return TUSB_ERROR_NONE; + return true; } #endif diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 03aa2485e..5ee649c4b 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -116,7 +116,7 @@ void cdcd_init (void); bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); +bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); void cdcd_reset (uint8_t rhport); #endif diff --git a/src/class/custom/custom_device.c b/src/class/custom/custom_device.c index bec6f42d1..129d405a4 100644 --- a/src/class/custom/custom_device.c +++ b/src/class/custom/custom_device.c @@ -71,22 +71,22 @@ void cusd_init(void) tu_varclr(&_cusd_itf); } -tusb_error_t cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_t *p_len) +bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_t *p_len) { cusd_interface_t* p_itf = &_cusd_itf; // Open endpoint pair with usbd helper - tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next( (uint8_t const*) p_desc_itf ); - TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_itf->ep_out, &p_itf->ep_in) ); + tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(p_desc_itf); + TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_itf->ep_out, &p_itf->ep_in) ); p_itf->itf_num = p_desc_itf->bInterfaceNumber; (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); // TODO Prepare for incoming data -// TU_ASSERT( dcd_edpt_xfer(rhport, p_itf->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)), TUSB_ERROR_DCD_EDPT_XFER ); +// TU_ASSERT( dcd_edpt_xfer(rhport, p_itf->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) ); - return TUSB_ERROR_NONE; + return true; } bool cusd_control_request(uint8_t rhport, tusb_control_request_t const * p_request) @@ -94,9 +94,9 @@ bool cusd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque return false; } -tusb_error_t cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { - return TUSB_ERROR_NONE; + return true; } void cusd_reset(uint8_t rhport) diff --git a/src/class/custom/custom_device.h b/src/class/custom/custom_device.h index f223a4d3c..aae06d503 100644 --- a/src/class/custom/custom_device.h +++ b/src/class/custom/custom_device.h @@ -63,10 +63,10 @@ #ifdef _TINY_USB_SOURCE_FILE_ void cusd_init(void); -tusb_error_t cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); +bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool cusd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request); bool cusd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); +bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void cusd_reset(uint8_t rhport); #endif diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index cfb1a698d..bd49c157a 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -508,10 +508,10 @@ bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const return true; } -tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { // nothing to do - return TUSB_ERROR_NONE; + return true; } diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 2f0047d81..885f33443 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -380,7 +380,7 @@ void hidd_init(void); bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); bool hidd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); +bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void hidd_reset(uint8_t rhport); #endif diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 14a7e593a..d4d115bce 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -329,7 +329,7 @@ int32_t proc_builtin_scsi(msc_cbw_t const * p_cbw, uint8_t* buffer, uint32_t buf return ret; } -tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { mscd_interface_t* p_msc = &_mscd_itf; msc_cbw_t const * p_cbw = &p_msc->cbw; @@ -340,10 +340,10 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, case MSC_STAGE_CMD: //------------- new CBW received -------------// // Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it - if(ep_addr != p_msc->ep_out) return TUSB_ERROR_NONE; + if(ep_addr != p_msc->ep_out) return true; TU_ASSERT( event == XFER_RESULT_SUCCESS && - xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE, TUSB_ERROR_INVALID_PARA ); + xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE ); p_csw->signature = MSC_CSW_SIGNATURE; p_csw->tag = p_cbw->tag; @@ -388,7 +388,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, else if ( !BIT_TEST_(p_cbw->dir, 7) ) { // OUT transfer - TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len), TUSB_ERROR_DCD_EDPT_XFER ); + TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) ); } else { @@ -409,8 +409,8 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, p_msc->total_len = (uint32_t) cb_result; p_csw->status = MSC_CSW_STATUS_PASSED; - TU_ASSERT( p_cbw->total_bytes >= p_msc->total_len, TUSB_ERROR_INVALID_PARA ); // cannot return more than host expect - TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len), TUSB_ERROR_DCD_EDPT_XFER ); + TU_ASSERT( p_cbw->total_bytes >= p_msc->total_len ); // cannot return more than host expect + TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len) ); }else { p_msc->total_len = 0; @@ -474,7 +474,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, // simulate an transfer complete with adjusted parameters --> this driver callback will fired again dcd_event_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, XFER_RESULT_SUCCESS, false); - return TUSB_ERROR_NONE; // skip the rest + return true; // skip the rest } else { @@ -549,7 +549,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, } } - return TUSB_ERROR_NONE; + return true; } /*------------------------------------------------------------------*/ diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 80e47d84b..c9e4139e9 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -174,7 +174,7 @@ void mscd_init(void); bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); bool mscd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); +bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void mscd_reset(uint8_t rhport); #endif diff --git a/src/device/usbd.c b/src/device/usbd.c index bf7fd950a..8c685d5b3 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -92,7 +92,7 @@ typedef struct { 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_request_complete ) (uint8_t rhport, tusb_control_request_t const * request); - tusb_error_t (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); + bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); void (* sof ) (uint8_t rhport); void (* reset ) (uint8_t); } usbd_class_driver_t; -- cgit v1.3.1 From bc46dc6edf3ff4bb72fb36e74b8a7d3da447d2d4 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 13 Dec 2018 13:49:09 +0700 Subject: osal clean up remove OSAL_TASK_DEF, osal_task_create. Applicaton should create a task and call tinyusb_task(). This make API consistent with NO OS. --- docs/configuration.txt | 1 - docs/getting_started.md | 2 +- examples/device/cdc_msc_hid/src/main.c | 39 ++----------- .../cdc_msc_hid_freertos/ses/nrf5x/nrf5x.emProject | 3 +- examples/device/cdc_msc_hid_freertos/src/main.c | 65 +++++++++++----------- .../device/cdc_msc_hid_freertos/src/tusb_config.h | 8 +-- examples/host/cdc_msc_hid/src/tusb_config.h | 16 +++--- examples/obsolete/device/src/tusb_config.h | 3 - lib/FreeRTOS/freertos_hook.c | 11 ++-- src/common/tusb_fifo.c | 4 +- src/device/usbd.c | 46 +++------------ src/host/usbh.c | 44 ++++----------- src/osal/osal.h | 2 - src/osal/osal_freertos.h | 21 ------- src/osal/osal_mynewt.h | 21 ------- src/osal/osal_none.h | 10 ---- src/tusb.c | 3 - src/tusb.h | 11 ++-- src/tusb_option.h | 5 -- 19 files changed, 76 insertions(+), 239 deletions(-) (limited to 'src/device') diff --git a/docs/configuration.txt b/docs/configuration.txt index 49061a4a0..7850827f6 100644 --- a/docs/configuration.txt +++ b/docs/configuration.txt @@ -20,7 +20,6 @@ #define CFG_TUSB_MCU ///< Select one of the supported MCU, the value must be from \ref group_mcu #define CFG_TUSB_OS ///< Select one of the supported RTOS, the value must be from \ref group_supported_os. -#define CFG_TUD_TASK_PRIO ///< If \ref CFG_TUSB_OS is configured to use a real RTOS (other than OPT_OS_NONE). This determines the priority of the usb stack task. //--------------------------------------------------------------------+ // HOST CONFIGURATION diff --git a/docs/getting_started.md b/docs/getting_started.md index 6d6091c03..268811ff4 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -37,7 +37,7 @@ It is relatively simple to incorporate tinyusb to your (existing) project 1. Copy or `git submodule` this repo into your project in a subfolder. Let's say it is *your_project/tinyusb* 2. Add all the .c in the src folder to your project settings (uvproj, ewp, makefile) 3. Add *your_project/tinysb* to your include path. Also make sure your current include path also contains the configuration file tusb_config.h. Or you could simply put the tusb_config.h into the tinyusb folder as well. -4. Make sure all required macros are all defined properly in tusb_config.h (configure file in demo application is sufficient, but you need to add a few more such as CFG_TUSB_MCU, CFG_TUSB_OS, CFG_TUD_TASK_PRIO since they are passed by IDE/compiler to maintain a unique configure for all demo projects). +4. Make sure all required macros are all defined properly in tusb_config.h (configure file in demo application is sufficient, but you need to add a few more such as CFG_TUSB_MCU, CFG_TUSB_OS since they are passed by IDE/compiler to maintain a unique configure for all demo projects). 5. If you use the device stack, make sure you have created/modified usb descriptors for your own need. Ultimately you need to fill out required pointers in tusbd_descriptor_pointers for that stack to work. 6. Add tusb_init() call to your reset initialization code. 7. Implement all enabled classes's callbacks. diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index bc260fdfa..178609e1e 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -46,7 +46,6 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ -void print_greeting(void); void led_blinking_task(void); extern void virtual_com_task(void); @@ -56,7 +55,6 @@ extern void usb_hid_task(void); int main(void) { board_init(); - print_greeting(); tusb_init(); @@ -84,9 +82,9 @@ int main(void) #if CFG_TUD_CDC void virtual_com_task(void) { - // connected and there are data available if ( tud_cdc_connected() ) { + // connected and there are data available if ( tud_cdc_available() ) { uint8_t buf[64]; @@ -98,11 +96,7 @@ void virtual_com_task(void) { tud_cdc_write_char(buf[i]); - if ( buf[i] == '\r' ) - { - tud_cdc_write_char('\n'); - tud_cdc_write_str("tinyusb cdc: "); - } + if ( buf[i] == '\r' ) tud_cdc_write_char('\n'); } tud_cdc_write_flush(); @@ -117,8 +111,8 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) // connected if ( dtr && rts ) { - // print greeting - tud_cdc_write_str("tinyusb cdc: "); + // print initial message when connected + tud_cdc_write_str("\r\nTinyUSB CDC MSC HID device example\r\n"); } } #endif @@ -213,28 +207,3 @@ void led_blinking_task(void) board_led_control(led_state); led_state = 1 - led_state; // toggle } - -//--------------------------------------------------------------------+ -// HELPER FUNCTION -//--------------------------------------------------------------------+ -void print_greeting(void) -{ - char const * const rtos_name[] = - { - [OPT_OS_NONE] = "None", - [OPT_OS_FREERTOS] = "FreeRTOS", - }; - - printf("\n--------------------------------------------------------------------\n"); - printf("- Device Demo (a tinyusb 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("This DEVICE demo is configured to support:"); - printf(" - RTOS = %s\n", rtos_name[CFG_TUSB_OS]); - if (CFG_TUD_CDC ) puts(" - Communication Device Class"); - if (CFG_TUD_MSC ) puts(" - Mass Storage"); - if (CFG_TUD_HID_KEYBOARD ) puts(" - HID Keyboard"); - if (CFG_TUD_HID_MOUSE ) puts(" - HID Mouse"); -} diff --git a/examples/device/cdc_msc_hid_freertos/ses/nrf5x/nrf5x.emProject b/examples/device/cdc_msc_hid_freertos/ses/nrf5x/nrf5x.emProject index 76615bf5b..ea4950682 100644 --- a/examples/device/cdc_msc_hid_freertos/ses/nrf5x/nrf5x.emProject +++ b/examples/device/cdc_msc_hid_freertos/ses/nrf5x/nrf5x.emProject @@ -18,11 +18,12 @@ arm_target_debug_interface_type="ADIv5" arm_target_device_name="nRF52840_xxAA" arm_target_interface_type="SWD" - build_treat_warnings_as_errors="Yes" + build_treat_warnings_as_errors="No" c_preprocessor_definitions="NRF52840_XXAA;__nRF_FAMILY;ARM_MATH_CM4;FLASH_PLACEMENT=1;BOARD_PCA10056;CFG_TUSB_MCU=OPT_MCU_NRF5X" c_user_include_directories="./;../../src;$(rootDir)/hw/cmsis/Include;$(rootDir)/hw;$(rootDir)/src;$(nrfxDir)/..;$(nrfxDir);$(nrfxDir)/mdk;$(nrfxDir)/hal;$(nrfxDir)/drivers/include;$(freertosDir)/Source/include;$(freertosDir)/Source/portable/GCC/ARM_CM4F" debug_register_definition_file="nrf52840_Registers.xml" debug_target_connection="J-Link" + gcc_enable_all_warnings="Yes" gcc_entry_point="Reset_Handler" link_use_linker_script_file="No" linker_memory_map_file="nRF52840_xxAA_MemoryMap.xml" diff --git a/examples/device/cdc_msc_hid_freertos/src/main.c b/examples/device/cdc_msc_hid_freertos/src/main.c index fbe1ec93e..843b2b077 100644 --- a/examples/device/cdc_msc_hid_freertos/src/main.c +++ b/examples/device/cdc_msc_hid_freertos/src/main.c @@ -59,14 +59,13 @@ //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -void print_greeting(void); void led_blinky_cb(TimerHandle_t xTimer); +void usb_device_task(void* param); /*------------- MAIN -------------*/ int main(void) { board_init(); - print_greeting(); // soft timer for blinky TimerHandle_t tm_hdl = xTimerCreate(NULL, pdMS_TO_TICKS(1000), true, NULL, led_blinky_cb); @@ -74,10 +73,13 @@ int main(void) tusb_init(); + // Create a task for tinyusb device stack + xTaskCreate( usb_device_task, "usbd", 150, NULL, configMAX_PRIORITIES-1, NULL); + // Create task #if CFG_TUD_CDC extern void cdc_task(void* params); - xTaskCreate( cdc_task, "cdc", 256, NULL, 2, NULL); + xTaskCreate( cdc_task, "cdc", 256, NULL, configMAX_PRIORITIES-2, NULL); #endif #if CFG_TUD_HID @@ -90,6 +92,19 @@ int main(void) return 0; } +// USB Device Driver task +// This top level thread process all usb events and invoke callbacks +void usb_device_task(void* param) +{ + (void) param; + + // RTOS forever loop + while (1) + { + tusb_task(); + } +} + //--------------------------------------------------------------------+ // USB CDC //--------------------------------------------------------------------+ @@ -98,11 +113,12 @@ void cdc_task(void* params) { (void) params; + // RTOS forever loop while ( 1 ) { - // connected and there are data available if ( tud_cdc_connected() ) { + // connected and there are data available if ( tud_cdc_available() ) { uint8_t buf[64]; @@ -110,10 +126,15 @@ void cdc_task(void* params) // read and echo back uint32_t count = tud_cdc_read(buf, sizeof(buf)); - tud_cdc_write(buf, count); - } + for(uint32_t i=0; imutex) { - uint32_t err; - (void) err; - osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER, &err); + osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER); } } diff --git a/src/device/usbd.c b/src/device/usbd.c index 8c685d5b3..a0ff96e8e 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -36,8 +36,6 @@ */ /**************************************************************************/ -// This top level class manages the bus state and delegates events to class-specific drivers. - #include "tusb_option.h" #if TUSB_OPT_DEVICE_ENABLED @@ -52,15 +50,6 @@ #define CFG_TUD_TASK_QUEUE_SZ 16 #endif -#ifndef CFG_TUD_TASK_STACK_SZ -#define CFG_TUD_TASK_STACK_SZ 150 -#endif - -#ifndef CFG_TUD_TASK_PRIO -#define CFG_TUD_TASK_PRIO 0 -#endif - - //--------------------------------------------------------------------+ // Device Data //--------------------------------------------------------------------+ @@ -153,16 +142,14 @@ static usbd_class_driver_t const usbd_class_drivers[] = #endif }; -enum { USBD_CLASS_DRIVER_COUNT = sizeof(usbd_class_drivers) / sizeof(usbd_class_driver_t) }; - +enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SZIE(usbd_class_drivers) }; //--------------------------------------------------------------------+ // DCD Event //--------------------------------------------------------------------+ -OSAL_TASK_DEF(_usbd_task_def, "usbd", usbd_task, CFG_TUD_TASK_PRIO, CFG_TUD_TASK_STACK_SZ); // Event queue -// role device/host is used by OS NONE for mutex (disable usb isr) only +// OPT_MODE_DEVICE is used by OS NONE for mutex (disable usb isr) OSAL_QUEUE_DEF(OPT_MODE_DEVICE, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t); static osal_queue_t _usbd_q; @@ -195,8 +182,6 @@ bool usbd_init (void) _usbd_q = osal_queue_create(&_usbd_qdef); TU_ASSERT(_usbd_q != NULL); - osal_task_create(&_usbd_task_def); - // Init class drivers for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init(); @@ -221,9 +206,13 @@ static void usbd_reset(uint8_t rhport) } } -// Main device task implementation -static void usbd_task_body(void) +/* USB Device Driver task + * This top level thread manages all device controller event and delegates events to class-specific drivers. + */ +void usbd_task( void* param) { + (void) param; + // Loop until there is no more events in the queue while (1) { @@ -297,25 +286,6 @@ static void usbd_task_body(void) } } -/* USB device task - * Thread that handles all device events. With an real RTOS, the task must be a forever loop and never return. - * For coding convenience with no RTOS, we use wrapped sub-function for processing to easily return at any time. - */ -void usbd_task( void* param) -{ - (void) param; - -#if CFG_TUSB_OS != OPT_OS_NONE - while (1) { -#endif - - usbd_task_body(); - -#if CFG_TUSB_OS != OPT_OS_NONE - } -#endif -} - //--------------------------------------------------------------------+ // Control Request Parser & Handling //--------------------------------------------------------------------+ diff --git a/src/host/usbh.c b/src/host/usbh.c index 39de4f7e7..7bcf7aaaf 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -46,15 +46,6 @@ #define CFG_TUH_TASK_QUEUE_SZ 16 #endif -#ifndef CFG_TUH_TASK_STACK_SZ -#define CFG_TUH_TASK_STACK_SZ 200 -#endif - -#ifndef CFG_TUH_TASK_PRIO -#define CFG_TUH_TASK_PRIO 0 -#endif - - //--------------------------------------------------------------------+ // INCLUDE //--------------------------------------------------------------------+ @@ -123,9 +114,9 @@ enum { USBH_CLASS_DRIVER_COUNT = TU_ARRAY_SZIE(usbh_class_drivers) }; //--------------------------------------------------------------------+ // INTERNAL OBJECT & FUNCTION DECLARATION //--------------------------------------------------------------------+ -CFG_TUSB_MEM_SECTION usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // including zero-address -OSAL_TASK_DEF(_usbh_task_def, "usbh", usbh_task, CFG_TUH_TASK_PRIO, CFG_TUH_TASK_STACK_SZ); +// including zero-address +CFG_TUSB_MEM_SECTION usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // Event queue // role device/host is used by OS NONE for mutex (disable usb isr) only @@ -161,8 +152,6 @@ bool usbh_init(void) _usbh_q = osal_queue_create( &_usbh_qdef ); TU_ASSERT(_usbh_q != NULL); - osal_task_create(&_usbh_task_def); - //------------- Semaphore, Mutex for Control Pipe -------------// for(uint8_t i=0; ifunc, taskdef->strname, taskdef->stack_sz, NULL, taskdef->prio, (StackType_t*) taskdef->buf, &taskdef->stask); -} - static inline void osal_task_delay(uint32_t msec) { vTaskDelay( pdMS_TO_TICKS(msec) ); diff --git a/src/osal/osal_mynewt.h b/src/osal/osal_mynewt.h index 62b5b45e8..d63ea731d 100644 --- a/src/osal/osal_mynewt.h +++ b/src/osal/osal_mynewt.h @@ -46,27 +46,6 @@ //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -#define OSAL_TASK_DEF(_name, _str, _func, _prio, _stack_sz) \ - static os_stack_t _name##_##buf[_stack_sz]; \ - osal_task_def_t _name = { .func = _func, .prio = _prio, .stack_sz = _stack_sz, .buf = _name##_##buf, .strname = _str }; - -typedef struct -{ - struct os_task mynewt_task; - osal_task_func_t func; - - uint16_t prio; - uint16_t stack_sz; - void* buf; - const char* strname; -}osal_task_def_t; - -static inline bool osal_task_create(osal_task_def_t* taskdef) -{ - return OS_OK == os_task_init(&taskdef->mynewt_task, taskdef->strname, taskdef->func, NULL, taskdef->prio, OS_WAIT_FOREVER, - (os_stack_t*) taskdef->buf, taskdef->stack_sz); -} - static inline void osal_task_delay(uint32_t msec) { os_time_delay( os_time_ms_to_ticks32(msec) ); diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index ff37113c6..55db674ce 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -51,17 +51,7 @@ //--------------------------------------------------------------------+ // TASK API -// Virtually do nothing in osal none //--------------------------------------------------------------------+ -#define OSAL_TASK_DEF(_name, _str, _func, _prio, _stack_sz) osal_task_def_t _name; -typedef uint8_t osal_task_def_t; - -static inline bool osal_task_create(osal_task_def_t* taskdef) -{ - (void) taskdef; - return true; -} - static inline void osal_task_delay(uint32_t msec) { uint32_t start = tusb_hal_millis(); diff --git a/src/tusb.c b/src/tusb.c index 4f7996664..fd390722b 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -68,7 +68,6 @@ bool tusb_init(void) return TUSB_ERROR_NONE; } -#if CFG_TUSB_OS == OPT_OS_NONE void tusb_task(void) { #if TUSB_OPT_HOST_ENABLED @@ -79,8 +78,6 @@ void tusb_task(void) usbd_task(NULL); #endif } -#endif - /*------------------------------------------------------------------*/ /* Debug diff --git a/src/tusb.h b/src/tusb.h index 4d65c80e1..e4393d067 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -105,11 +105,9 @@ // return true if success bool tusb_init(void); -#if CFG_TUSB_OS == OPT_OS_NONE -/** \brief Run all tinyusb's internal tasks (e.g host task, device task). - * \note This function is only required when using no RTOS (\ref CFG_TUSB_OS == OPT_OS_NONE). All the stack functions - * & callback are invoked within this function. This should be called periodically within the mainloop - * +/** Run all tinyusb's internal tasks (e.g host task, device task) and invoke callback + * This should be called periodically within the mainloop. + @code int main(void) { @@ -126,10 +124,9 @@ bool tusb_init(void); } } @endcode - * + */ void tusb_task(void); -#endif /** @} */ diff --git a/src/tusb_option.h b/src/tusb_option.h index 8b2e59b17..07d8387e3 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -224,11 +224,6 @@ //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ - -#if (CFG_TUSB_OS != OPT_OS_NONE) && !defined (CFG_TUD_TASK_PRIO) - #error CFG_TUD_TASK_PRIO need to be defined (hint: use the highest if possible) -#endif - #if CFG_TUD_ENDOINT0_SIZE > 64 #error Control Endpoint Max Packet Size cannot be larger than 64 #endif -- cgit v1.3.1 From af1ffe4675ca9eeeebd47a82f3508eecdc1486c5 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 13 Dec 2018 13:53:43 +0700 Subject: remove task void* param --- src/device/usbd.c | 4 +--- src/device/usbd_pvt.h | 2 +- src/host/usbh.c | 4 +--- src/host/usbh.h | 2 +- src/tusb.c | 4 ++-- 5 files changed, 6 insertions(+), 10 deletions(-) (limited to 'src/device') diff --git a/src/device/usbd.c b/src/device/usbd.c index a0ff96e8e..aa81cb118 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -209,10 +209,8 @@ static void usbd_reset(uint8_t rhport) /* USB Device Driver task * This top level thread manages all device controller event and delegates events to class-specific drivers. */ -void usbd_task( void* param) +void usbd_task (void) { - (void) param; - // Loop until there is no more events in the queue while (1) { diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 798a871b1..d1fccd9e0 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -52,7 +52,7 @@ extern tud_desc_set_t const* usbd_desc_set; // INTERNAL API for stack management //--------------------------------------------------------------------+ bool usbd_init (void); -void usbd_task (void* param); +void usbd_task (void); // Carry out Data and Status stage of control transfer diff --git a/src/host/usbh.c b/src/host/usbh.c index 7bcf7aaaf..27d32330a 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -602,10 +602,8 @@ 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. */ -void usbh_task(void* param) +void usbh_task(void) { - (void) param; - // Loop until there is no more events in the queue while (1) { diff --git a/src/host/usbh.h b/src/host/usbh.h index 3656b9a0d..8009c5080 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -103,7 +103,7 @@ ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr); #ifdef _TINY_USB_SOURCE_FILE_ bool usbh_init(void); -void usbh_task(void* param); +void usbh_task(void); bool usbh_control_xfer (uint8_t dev_addr, tusb_control_request_t* request, uint8_t* data); diff --git a/src/tusb.c b/src/tusb.c index fd390722b..253adc59c 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -71,11 +71,11 @@ bool tusb_init(void) void tusb_task(void) { #if TUSB_OPT_HOST_ENABLED - usbh_task(NULL); + usbh_task(); #endif #if TUSB_OPT_DEVICE_ENABLED - usbd_task(NULL); + usbd_task(); #endif } -- cgit v1.3.1 From 1c49c479cabf1be9b1c22017c42a8480b26cebcb Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 13 Dec 2018 14:51:37 +0700 Subject: seperate tusb_task() to tud_task() and tuh_task() tusb_task() still exists for backward compatible --- docs/getting_started.md | 5 ++-- docs/porting.md | 4 +-- examples/device/cdc_msc_hid/src/main.c | 3 ++- examples/device/cdc_msc_hid_freertos/src/main.c | 3 ++- examples/host/cdc_msc_hid/src/main.c | 3 ++- src/class/hid/hid_device.c | 2 +- src/device/usbd.c | 18 ++++++++++++- src/device/usbd.h | 1 + src/device/usbd_pvt.h | 2 -- src/host/usbh.c | 18 ++++++++++++- src/host/usbh.h | 9 +++---- src/tusb.c | 11 -------- src/tusb.h | 34 ++++++++++--------------- 13 files changed, 64 insertions(+), 49 deletions(-) (limited to 'src/device') diff --git a/docs/getting_started.md b/docs/getting_started.md index 268811ff4..723e6d862 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -41,7 +41,7 @@ It is relatively simple to incorporate tinyusb to your (existing) project 5. If you use the device stack, make sure you have created/modified usb descriptors for your own need. Ultimately you need to fill out required pointers in tusbd_descriptor_pointers for that stack to work. 6. Add tusb_init() call to your reset initialization code. 7. Implement all enabled classes's callbacks. -8. If you don't use any RTOSes at all, you need to continuously and/or periodically call tusb_task() function. Most of the callbacks and functionality are handled and invoke within the call of that task runner. +8. If you don't use any RTOSes at all, you need to continuously and/or periodically call tud_task()/tuh_task() function. Most of the callbacks and functionality are handled and invoke within the call of that task runner. ~~~{.c} int main(void) @@ -53,7 +53,8 @@ int main(void) { your_application_code(); - tusb_task(); // handle tinyusb event, task etc ... + tud_task(); // tinyusb device task + tuh_task(); // tinyusb host task } } ~~~ diff --git a/docs/porting.md b/docs/porting.md index 040112d9c..f5af82800 100644 --- a/docs/porting.md +++ b/docs/porting.md @@ -61,7 +61,7 @@ The OPT_OS_NONE option is the only option which requires an MCU specific functio ### Device API -After the USB device is setup, the USB device code works by processing events on the main thread (by calling `tusb_task`). These events are queued by the USB interrupt handler. So, there are three parts to the device low-level API: device setup, endpoint setup and interrupt processing. +After the USB device is setup, the USB device code works by processing events on the main thread (by calling `tud_task`). These events are queued by the USB interrupt handler. So, there are three parts to the device low-level API: device setup, endpoint setup and interrupt processing. All of the code for the low-level device API is in `src/portable///dcd_.c`. @@ -164,4 +164,4 @@ At this point you should have everything working! ;-) Of course, you may not wri Use [WireShark](https://www.wireshark.org/) or [a Beagle](https://www.totalphase.com/protocols/usb/) to sniff the USB traffic. When things aren't working its likely very early in the USB enumeration process. Figuring out where can help clue in where the issue is. For example: * If the host sends a SETUP packet and its not ACKed then your USB peripheral probably isn't started correctly. * If the peripheral is started correctly but it still didn't work, then verify your usb clock is correct. (You did output a PWM based on it right? ;-) ) -* If the SETUP packet is ACKed but nothing is sent back then you interrupt handler isn't queueing the setup packet correctly. (Also, if you are using your own code instead of an example `tusb_task` may not be called.) If thats OK, the `dcd_xfer_complete` may not be setting up the next transaction correctly. +* If the SETUP packet is ACKed but nothing is sent back then you interrupt handler isn't queueing the setup packet correctly. (Also, if you are using your own code instead of an example `tud_task` may not be called.) If thats OK, the `dcd_xfer_complete` may not be setting up the next transaction correctly. diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index 178609e1e..a9d1f1618 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -60,7 +60,8 @@ int main(void) while (1) { - tusb_task(); + // tinyusb device task + tud_task(); led_blinking_task(); diff --git a/examples/device/cdc_msc_hid_freertos/src/main.c b/examples/device/cdc_msc_hid_freertos/src/main.c index 843b2b077..946972c16 100644 --- a/examples/device/cdc_msc_hid_freertos/src/main.c +++ b/examples/device/cdc_msc_hid_freertos/src/main.c @@ -101,7 +101,8 @@ void usb_device_task(void* param) // RTOS forever loop while (1) { - tusb_task(); + // tinyusb device task + tud_task(); } } diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index ebb540782..f1139974d 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -62,7 +62,8 @@ int main(void) while (1) { - tusb_task(); + // tinyusb host task + tuh_task(); led_blinking_task(); diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index bd49c157a..70aa7370d 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -340,7 +340,7 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t /*------------- Boot protocol only keyboard & mouse -------------*/ if (desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT) { - TU_ASSERT(desc_itf->bInterfaceProtocol == HID_PROTOCOL_KEYBOARD || desc_itf->bInterfaceProtocol == HID_PROTOCOL_MOUSE, ERR_TUD_INVALID_DESCRIPTOR); + TU_ASSERT(desc_itf->bInterfaceProtocol == HID_PROTOCOL_KEYBOARD || desc_itf->bInterfaceProtocol == HID_PROTOCOL_MOUSE); #if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT if (desc_itf->bInterfaceProtocol == HID_PROTOCOL_KEYBOARD) diff --git a/src/device/usbd.c b/src/device/usbd.c index aa81cb118..a3fbfdff9 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -208,8 +208,24 @@ static void usbd_reset(uint8_t rhport) /* USB Device Driver task * This top level thread manages all device controller event and delegates events to class-specific drivers. + * This should be called periodically within the mainloop or rtos thread. + * + @code + int main(void) + { + application_init(); + tusb_init(); + + while(1) // the mainloop + { + application_code(); + + tud_task(); // tinyusb device task + } + } + @endcode */ -void usbd_task (void) +void tud_task (void) { // Loop until there is no more events in the queue while (1) diff --git a/src/device/usbd.h b/src/device/usbd.h index f9e0d8f4d..f9e7368ce 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -81,6 +81,7 @@ extern tud_desc_set_t tud_desc_set; // APPLICATION API //--------------------------------------------------------------------+ bool tud_mounted(void); +void tud_task (void); //--------------------------------------------------------------------+ // APPLICATION CALLBACK (WEAK is optional) diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index d1fccd9e0..06951673f 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -52,8 +52,6 @@ extern tud_desc_set_t const* usbd_desc_set; // INTERNAL API for stack management //--------------------------------------------------------------------+ bool usbd_init (void); -void usbd_task (void); - // Carry out Data and Status stage of control transfer // - If len = 0, it is equivalent to sending status only diff --git a/src/host/usbh.c b/src/host/usbh.c index 27d32330a..d0b5edb63 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -601,8 +601,24 @@ 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. + * + @code + int main(void) + { + application_init(); + tusb_init(); + + while(1) // the mainloop + { + application_code(); + + tuh_task(); // tinyusb host task + } + } + @endcode */ -void usbh_task(void) +void tuh_task(void) { // Loop until there is no more events in the queue while (1) diff --git a/src/host/usbh.h b/src/host/usbh.h index 8009c5080..fee1235f4 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -78,9 +78,9 @@ typedef struct { //--------------------------------------------------------------------+ // APPLICATION API //--------------------------------------------------------------------+ -//tusb_error_t tusbh_configuration_set (uint8_t dev_addr, uint8_t configure_number) ATTR_WARN_UNUSED_RESULT; -tusb_device_state_t tuh_device_get_state (uint8_t dev_addr) ATTR_WARN_UNUSED_RESULT ATTR_PURE; -static inline bool tuh_device_is_configured(uint8_t dev_addr) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT ATTR_PURE; +void tuh_task(void); + +tusb_device_state_t tuh_device_get_state (uint8_t dev_addr); static inline bool tuh_device_is_configured(uint8_t dev_addr) { return tuh_device_get_state(dev_addr) == TUSB_DEVICE_STATE_CONFIGURED; @@ -89,7 +89,7 @@ static inline bool tuh_device_is_configured(uint8_t dev_addr) //--------------------------------------------------------------------+ // APPLICATION CALLBACK //--------------------------------------------------------------------+ -ATTR_WEAK uint8_t tuh_device_attached_cb (tusb_desc_device_t const *p_desc_device) ATTR_WARN_UNUSED_RESULT; +ATTR_WEAK uint8_t tuh_device_attached_cb (tusb_desc_device_t const *p_desc_device); /** Callback invoked when device is mounted (configured) */ ATTR_WEAK void tuh_mount_cb (uint8_t dev_addr); @@ -103,7 +103,6 @@ ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr); #ifdef _TINY_USB_SOURCE_FILE_ bool usbh_init(void); -void usbh_task(void); bool usbh_control_xfer (uint8_t dev_addr, tusb_control_request_t* request, uint8_t* data); diff --git a/src/tusb.c b/src/tusb.c index 253adc59c..ad2dcbfd3 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -68,17 +68,6 @@ bool tusb_init(void) return TUSB_ERROR_NONE; } -void tusb_task(void) -{ - #if TUSB_OPT_HOST_ENABLED - usbh_task(); - #endif - - #if TUSB_OPT_DEVICE_ENABLED - usbd_task(); - #endif -} - /*------------------------------------------------------------------*/ /* Debug *------------------------------------------------------------------*/ diff --git a/src/tusb.h b/src/tusb.h index e4393d067..3a13bbecc 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -101,32 +101,24 @@ /** \ingroup group_application_api * @{ */ -// Initialize device/host stack according to tusb_config.h -// return true if success +// Initialize device/host stack bool tusb_init(void); -/** Run all tinyusb's internal tasks (e.g host task, device task) and invoke callback - * This should be called periodically within the mainloop. +// TODO +// bool tusb_teardown(void); - @code - int main(void) - { - your_init_code(); - tusb_init(); - // other config code - - while(1) // the mainloop - { - your_application_code(); - - tusb_task(); // handle tinyusb event, task etc ... - } - } - @endcode +// backward compatible only. TODO remove later +static inline void tusb_task(void) +{ + #if TUSB_OPT_HOST_ENABLED + tuh_task(); + #endif - */ -void tusb_task(void); + #if TUSB_OPT_DEVICE_ENABLED + tud_task(); + #endif +} /** @} */ -- cgit v1.3.1 From 902697ca079a179221da92f8cb9f873768a5d0d7 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 17 Dec 2018 12:14:11 +0700 Subject: add dcd_get_microframe() --- src/class/hid/hid_device.c | 2 ++ src/class/hid/hid_device.h | 1 - src/common/tusb_timeout.h | 10 --------- src/device/dcd.h | 4 ++++ src/portable/microchip/samd21/dcd_samd21.c | 6 ++++++ src/portable/microchip/samd51/dcd_samd51.c | 6 ++++++ src/portable/nordic/nrf5x/dcd_nrf5x.c | 11 +++++++++- src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c | 7 +++++++ src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 6 ++++++ src/portable/nxp/lpc18_43/dcd_lpc18_43.c | 28 ++++++++++++++++---------- 10 files changed, 58 insertions(+), 23 deletions(-) (limited to 'src/device') diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 70aa7370d..3b882f705 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -207,6 +207,7 @@ bool tud_hid_keyboard_key_press(char ch) return tud_hid_keyboard_keycode(modifier, keycode); } +#if 0 // should be at application bool tud_hid_keyboard_key_sequence(const char* str, uint32_t interval_ms) { // Send each key in string @@ -231,6 +232,7 @@ bool tud_hid_keyboard_key_sequence(const char* str, uint32_t interval_ms) return true; } +#endif #endif // CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 885f33443..f3d5087a2 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -95,7 +95,6 @@ static inline bool tud_hid_keyboard_key_release(void) { return tud_hid_keyboard_ #if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP bool tud_hid_keyboard_key_press(char ch); -bool tud_hid_keyboard_key_sequence(const char* str, uint32_t interval_ms); typedef struct{ uint8_t shift; diff --git a/src/common/tusb_timeout.h b/src/common/tusb_timeout.h index 9d80f5fe7..06d5ff5bf 100644 --- a/src/common/tusb_timeout.h +++ b/src/common/tusb_timeout.h @@ -79,16 +79,6 @@ static inline void tu_timeout_restart(tu_timeout_t* tt) tt->start = tusb_hal_millis(); } - -static inline void tu_timeout_wait(uint32_t msec) -{ - tu_timeout_t tt; - tu_timeout_set(&tt, msec); - - // blocking delay - while ( !tu_timeout_expired(&tt) ) { } -} - #ifdef __cplusplus } #endif diff --git a/src/device/dcd.h b/src/device/dcd.h index 6b1356cbf..408922740 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -93,12 +93,16 @@ TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct"); /* Device API *------------------------------------------------------------------*/ bool dcd_init (uint8_t rhport); + void dcd_int_enable (uint8_t rhport); void dcd_int_disable(uint8_t rhport); void dcd_set_address(uint8_t rhport, uint8_t dev_addr); void dcd_set_config (uint8_t rhport, uint8_t config_num); +// Get current micro-frame number +uint32_t dcd_get_microframe(uint8_t rhport); + /*------------------------------------------------------------------*/ /* Event Function * Called by DCD to notify USBD diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index af0721ec3..0865f2bda 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -124,6 +124,12 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) // Nothing to do } +uint32_t dcd_get_microframe(uint8_t rhport) +{ + (void) rhport; + return USB->DEVICE.FNUM & (TU_BIT(14) - 1); +} + /*------------------------------------------------------------------*/ /* DCD Endpoint port *------------------------------------------------------------------*/ diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index ce66b8245..fc3d31dea 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -129,6 +129,12 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) // Nothing to do } +uint32_t dcd_get_microframe(uint8_t rhport) +{ + (void) rhport; + return USB->DEVICE.FNUM & (TU_BIT(14) - 1); +} + /*------------------------------------------------------------------*/ /* DCD Endpoint port *------------------------------------------------------------------*/ diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 7d13ef8c3..1e0c34743 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -187,7 +187,7 @@ static void xact_in_prepare(uint8_t epnum) } //--------------------------------------------------------------------+ -// Tinyusb DCD API +// Controller API //--------------------------------------------------------------------+ bool dcd_init (uint8_t rhport) { @@ -221,6 +221,15 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) // Nothing to do } +uint32_t dcd_get_microframe(uint8_t rhport) +{ + (void) rhport; + return NRF_USBD->FRAMECNTR << 3; +} + +//--------------------------------------------------------------------+ +// Endpoint API +//--------------------------------------------------------------------+ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) { (void) rhport; diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c index 26ecaf8be..23522b940 100644 --- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c +++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c @@ -165,6 +165,13 @@ void dcd_set_address(uint8_t rhport, uint8_t dev_addr) LPC_USB->DEVCMDSTAT |= dev_addr; } +uint32_t dcd_get_microframe(uint8_t rhport) +{ + (void) rhport; + + return (LPC_USB->INFO & (TU_BIT(11) - 1)) << 3; +} + bool dcd_init(uint8_t rhport) { (void) rhport; diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index fca2c355d..7406d1770 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -227,6 +227,12 @@ void dcd_set_config(uint8_t rhport, uint8_t config_num) sie_write(SIE_CMDCODE_CONFIGURE_DEVICE, 1, 1); } +uint32_t dcd_get_microframe(uint8_t rhport) +{ + (void) rhport; + return ((uint32_t) sie_read(SIE_CMDCODE_READ_FRAME_NUMBER)) << 3; +} + //--------------------------------------------------------------------+ // CONTROL HELPER //--------------------------------------------------------------------+ diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c index 7761d547f..796c8c4bb 100644 --- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c @@ -92,17 +92,6 @@ static dcd_data_t* const dcd_data_ptr[2] = //--------------------------------------------------------------------+ // CONTROLLER API //--------------------------------------------------------------------+ -void dcd_set_address(uint8_t rhport, uint8_t dev_addr) -{ - LPC_USB[rhport]->DEVICEADDR = (dev_addr << 25) | TU_BIT(24); -} - -void dcd_set_config(uint8_t rhport, uint8_t config_num) -{ - (void) rhport; - (void) config_num; - // nothing to do -} /// follows LPC43xx User Manual 23.10.3 static void bus_reset(uint8_t rhport) @@ -173,6 +162,23 @@ void dcd_int_disable(uint8_t rhport) NVIC_DisableIRQ(rhport ? USB1_IRQn : USB0_IRQn); } +void dcd_set_address(uint8_t rhport, uint8_t dev_addr) +{ + LPC_USB[rhport]->DEVICEADDR = (dev_addr << 25) | TU_BIT(24); +} + +void dcd_set_config(uint8_t rhport, uint8_t config_num) +{ + (void) rhport; + (void) config_num; + // nothing to do +} + +uint32_t dcd_get_microframe(uint8_t rhport) +{ + return LPC_USB[rhport]->FRINDEX_D; +} + //--------------------------------------------------------------------+ // HELPER //--------------------------------------------------------------------+ -- cgit v1.3.1 From 6f17b4e01973185beb2876f827a76e4200ee0ef3 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 21 Dec 2018 12:46:20 +0700 Subject: change to dcd_get_frame_number --- src/device/dcd.h | 4 ++-- src/portable/microchip/samd21/dcd_samd21.c | 4 ++-- src/portable/microchip/samd51/dcd_samd51.c | 4 ++-- src/portable/nordic/nrf5x/dcd_nrf5x.c | 4 ++-- src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c | 4 ++-- src/portable/nxp/lpc17_40/dcd_lpc17_40.c | 4 ++-- src/portable/nxp/lpc18_43/dcd_lpc18_43.c | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/device') diff --git a/src/device/dcd.h b/src/device/dcd.h index 408922740..abd85a70b 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -100,8 +100,8 @@ void dcd_int_disable(uint8_t rhport); void dcd_set_address(uint8_t rhport, uint8_t dev_addr); void dcd_set_config (uint8_t rhport, uint8_t config_num); -// Get current micro-frame number -uint32_t dcd_get_microframe(uint8_t rhport); +// Get current frame number +uint32_t dcd_get_frame_number(uint8_t rhport); /*------------------------------------------------------------------*/ /* Event Function diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index be1f4495c..7b2a57eeb 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -124,10 +124,10 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) // Nothing to do } -uint32_t dcd_get_microframe(uint8_t rhport) +uint32_t dcd_get_frame_number(uint8_t rhport) { (void) rhport; - return USB->DEVICE.FNUM.reg & (TU_BIT(14) - 1); + return USB->DEVICE.FNUM.bit.FNUM; } /*------------------------------------------------------------------*/ diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index 3dd6c88d0..1de3fadb2 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -129,10 +129,10 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) // Nothing to do } -uint32_t dcd_get_microframe(uint8_t rhport) +uint32_t dcd_get_frame_number(uint8_t rhport) { (void) rhport; - return USB->DEVICE.FNUM.reg & (TU_BIT(14) - 1); + return USB->DEVICE.FNUM.bit.FNUM; } /*------------------------------------------------------------------*/ diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 1e0c34743..49984eae8 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -221,10 +221,10 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) // Nothing to do } -uint32_t dcd_get_microframe(uint8_t rhport) +uint32_t dcd_get_frame_number(uint8_t rhport) { (void) rhport; - return NRF_USBD->FRAMECNTR << 3; + return NRF_USBD->FRAMECNTR; } //--------------------------------------------------------------------+ diff --git a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c index 23522b940..a9ba5e336 100644 --- a/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c +++ b/src/portable/nxp/lpc11_13_15/dcd_lpc11_13_15.c @@ -165,11 +165,11 @@ void dcd_set_address(uint8_t rhport, uint8_t dev_addr) LPC_USB->DEVCMDSTAT |= dev_addr; } -uint32_t dcd_get_microframe(uint8_t rhport) +uint32_t dcd_get_frame_number(uint8_t rhport) { (void) rhport; - return (LPC_USB->INFO & (TU_BIT(11) - 1)) << 3; + return LPC_USB->INFO & (TU_BIT(11) - 1); } bool dcd_init(uint8_t rhport) diff --git a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c index 7406d1770..e3286b1cb 100644 --- a/src/portable/nxp/lpc17_40/dcd_lpc17_40.c +++ b/src/portable/nxp/lpc17_40/dcd_lpc17_40.c @@ -227,10 +227,10 @@ void dcd_set_config(uint8_t rhport, uint8_t config_num) sie_write(SIE_CMDCODE_CONFIGURE_DEVICE, 1, 1); } -uint32_t dcd_get_microframe(uint8_t rhport) +uint32_t dcd_get_frame_number(uint8_t rhport) { (void) rhport; - return ((uint32_t) sie_read(SIE_CMDCODE_READ_FRAME_NUMBER)) << 3; + return (uint32_t) sie_read(SIE_CMDCODE_READ_FRAME_NUMBER); } //--------------------------------------------------------------------+ diff --git a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c index 796c8c4bb..65b0728eb 100644 --- a/src/portable/nxp/lpc18_43/dcd_lpc18_43.c +++ b/src/portable/nxp/lpc18_43/dcd_lpc18_43.c @@ -174,9 +174,9 @@ void dcd_set_config(uint8_t rhport, uint8_t config_num) // nothing to do } -uint32_t dcd_get_microframe(uint8_t rhport) +uint32_t dcd_get_frame_number(uint8_t rhport) { - return LPC_USB[rhport]->FRINDEX_D; + return LPC_USB[rhport]->FRINDEX_D >> 3; } //--------------------------------------------------------------------+ -- cgit v1.3.1 From a3173b8de48b37e802d4de45821d2efbf54b1652 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 16 Nov 2018 17:03:33 -0800 Subject: Add USB Midi support. It also introduces a txbuffer which copies data into it but passes the buffer straight to the USB rather than another copy. --- examples/device/cdc_msc_hid/src/tusb_config.h | 1 + src/class/audio/audio.h | 78 +++++ src/class/midi/midi.h | 84 ++++++ src/class/midi/midi_device.c | 412 ++++++++++++++++++++++++++ src/class/midi/midi_device.h | 120 ++++++++ src/device/usbd.c | 13 + src/tusb.h | 5 +- 7 files changed, 712 insertions(+), 1 deletion(-) create mode 100644 src/class/audio/audio.h create mode 100644 src/class/midi/midi.h create mode 100644 src/class/midi/midi_device.c create mode 100644 src/class/midi/midi_device.h (limited to 'src/device') diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h index 2ff49aaf7..3f748c3f0 100644 --- a/examples/device/cdc_msc_hid/src/tusb_config.h +++ b/examples/device/cdc_msc_hid/src/tusb_config.h @@ -109,6 +109,7 @@ //------------- CLASS -------------// #define CFG_TUD_CDC 1 #define CFG_TUD_MSC 1 +#define CFG_TUD_MIDI 0 #define CFG_TUD_CUSTOM_CLASS 0 #define CFG_TUD_HID 0 diff --git a/src/class/audio/audio.h b/src/class/audio/audio.h new file mode 100644 index 000000000..1ea3f1166 --- /dev/null +++ b/src/class/audio/audio.h @@ -0,0 +1,78 @@ +/**************************************************************************/ +/*! + @file cdc.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \ingroup group_class + * \defgroup ClassDriver_Audio Audio + * Currently only MIDI subclass is supported + * @{ */ + +#ifndef _TUSB_CDC_H__ +#define _TUSB_CDC_H__ + +#include "common/tusb_common.h" + +#ifdef __cplusplus + extern "C" { +#endif + +/// Audio Interface Subclass Codes +typedef enum +{ + AUDIO_SUBCLASS_AUDIO_CONTROL = 0x01 , ///< Audio Control + AUDIO_SUBCLASS_AUDIO_STREAMING , ///< Audio Streaming + AUDIO_SUBCLASS_MIDI_STREAMING , ///< MIDI Streaming +} audio_subclass_type_t; + +/// Audio Protocol Codes +typedef enum +{ + AUDIO_PROTOCOL_V1 = 0x00, ///< Version 1.0 + AUDIO_PROTOCOL_V2 = 0x20, ///< Version 2.0 + AUDIO_PROTOCOL_V3 = 0x30, ///< Version 3.0 +} audio_protocol_type_t; + + +/** @} */ + +#ifdef __cplusplus + } +#endif + +#endif + +/** @} */ diff --git a/src/class/midi/midi.h b/src/class/midi/midi.h new file mode 100644 index 000000000..21c43be20 --- /dev/null +++ b/src/class/midi/midi.h @@ -0,0 +1,84 @@ +/**************************************************************************/ +/*! + @file cdc.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \ingroup group_class + * \defgroup ClassDriver_CDC Communication Device Class (CDC) + * Currently only Abstract Control Model subclass is supported + * @{ */ + +#ifndef _TUSB_MIDI_H__ +#define _TUSB_MIDI_H__ + +#include "common/tusb_common.h" + +#ifdef __cplusplus + extern "C" { +#endif + +//--------------------------------------------------------------------+ +// FUNCTIONAL DESCRIPTOR (COMMUNICATION INTERFACE) +//--------------------------------------------------------------------+ +/// Header Functional Descriptor (Communication Interface) +typedef struct ATTR_PACKED +{ + uint8_t bLength ; ///< Size of this descriptor in bytes. + uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific + uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above MIDI_FUCN_DESC_ + uint16_t bcdMSC ; ///< MidiStreaming SubClass release number in Binary-Coded Decimal + uint16_t wTotalLength ; +}midi_desc_func_header_t; + +/// Union Functional Descriptor (Communication Interface) +typedef struct ATTR_PACKED +{ + uint8_t bLength ; ///< Size of this descriptor in bytes. + uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific + uint8_t bDescriptorSubType ; ///< Descriptor SubType one of above CDC_FUCN_DESC_ + uint8_t bControlInterface ; ///< Interface number of Communication Interface + uint8_t bSubordinateInterface ; ///< Array of Interface number of Data Interface +}cdc_desc_func_union_t; + +/** @} */ + +#ifdef __cplusplus + } +#endif + +#endif + +/** @} */ diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c new file mode 100644 index 000000000..5acc0b0f1 --- /dev/null +++ b/src/class/midi/midi_device.c @@ -0,0 +1,412 @@ +/**************************************************************************/ +/*! + @file cdc_device.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#include "tusb_option.h" + +#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_MIDI) + +#define _TINY_USB_SOURCE_FILE_ +//--------------------------------------------------------------------+ +// INCLUDE +//--------------------------------------------------------------------+ +#include "midi_device.h" +#include "class/audio/audio.h" +#include "device/usbd_pvt.h" + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF +//--------------------------------------------------------------------+ +typedef struct +{ + uint8_t itf_num; + uint8_t ep_in; + uint8_t ep_out; + + // FIFO + tu_fifo_t rx_ff; + uint8_t rx_ff_buf[CFG_TUD_MIDI_RX_BUFSIZE]; + osal_mutex_def_t rx_ff_mutex; + + #if CFG_TUD_MIDI_TX_BUFSIZE % CFG_TUD_MIDI_EPSIZE != 0 + #error "TX buffer size must be multiple of endpoint size." + #endif + + // This is a ring buffer that aligns to word boundaries so that it can be transferred directly to + // the USB peripheral. There are three states to the data: free, transmitting and pending. + CFG_TUSB_MEM_ALIGN uint8_t tx_buf[CFG_TUD_MIDI_TX_BUFSIZE]; + uint16_t tx_buf_len; + uint16_t first_free; + uint16_t pending_count; + uint16_t transmitting_count; + + // We need to pack messages into words before queueing their transmission so buffer across write + // calls. + uint8_t message_buffer[4]; + uint8_t message_buffer_length; + uint8_t message_target_length; + + // Endpoint Transfer buffer + CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_MIDI_EPSIZE]; + +} midid_interface_t; + +#define ITF_MEM_RESET_SIZE offsetof(midid_interface_t, rx_ff) + +//--------------------------------------------------------------------+ +// INTERNAL OBJECT & FUNCTION DECLARATION +//--------------------------------------------------------------------+ +CFG_TUSB_ATTR_USBRAM midid_interface_t _midid_itf[CFG_TUD_MIDI]; + +bool tud_midi_n_connected(uint8_t itf) { + midid_interface_t* midi = &_midid_itf[itf]; + return midi->itf_num != 0; +} + +//--------------------------------------------------------------------+ +// READ API +//--------------------------------------------------------------------+ +uint32_t tud_midi_n_available(uint8_t itf, uint8_t jack_id) +{ + return tu_fifo_count(&_midid_itf[itf].rx_ff); +} + +char tud_midi_n_read_char(uint8_t itf, uint8_t jack_id) +{ + char ch; + return tu_fifo_read(&_midid_itf[itf].rx_ff, &ch) ? ch : (-1); +} + +uint32_t tud_midi_n_read(uint8_t itf, uint8_t jack_id, void* buffer, uint32_t bufsize) +{ + return tu_fifo_read_n(&_midid_itf[itf].rx_ff, buffer, bufsize); +} + +void tud_midi_n_read_flush (uint8_t itf, uint8_t jack_id) +{ + tu_fifo_clear(&_midid_itf[itf].rx_ff); +} + + +void midi_rx_done_cb(midid_interface_t* midi, uint8_t const* buffer, uint32_t bufsize) { + if (bufsize % 4 != 0) { + return; + } + + for(uint32_t i=0; i> 4; + uint8_t code_index = header & 0x0f; + // We always copy over the first byte. + uint8_t count = 1; + // Ignore subsequent bytes based on the code. + if (code_index != 0x5 && code_index != 0xf) { + count = 2; + if (code_index != 0x2 && code_index != 0x6 && code_index != 0xc && code_index != 0xd) { + count = 3; + } + } + tu_fifo_write_n(&midi->rx_ff, &buffer[i + 1], count); + } +} + + +//--------------------------------------------------------------------+ +// WRITE API +//--------------------------------------------------------------------+ + +uint16_t maybe_transmit(midid_interface_t* midi) { + if (midi->transmitting_count > 0 || midi->pending_count == 0) { + return 0; + } + midi->transmitting_count = midi->pending_count; + uint16_t transmit_start_index; + // The pending zone wraps back to the end so we must do two transfers. + if (midi->pending_count > midi->first_free) { + midi->transmitting_count -= midi->first_free; + transmit_start_index = midi->tx_buf_len - midi->transmitting_count; + } else { + transmit_start_index = midi->first_free - midi->transmitting_count; + + // We are transmitting up to first free so ensure it's word aligned for the next transmit. + uint8_t over_aligned = midi->first_free % sizeof(size_t); + if (over_aligned != 0) { + midi->first_free = (midi->first_free + (sizeof(size_t) - midi->first_free)) % midi->tx_buf_len; + } + } + midi->pending_count -= midi->transmitting_count; + + uint8_t* tx_start = midi->tx_buf + transmit_start_index; + if (!dcd_edpt_xfer(0, midi->ep_in, tx_start, midi->transmitting_count)) { + return 0; + } + return midi->transmitting_count; +} + +uint32_t tud_tx_buf_write_done_cb(midid_interface_t* midi, uint32_t bufsize) { + midi->transmitting_count -= bufsize; + + uint16_t len = maybe_transmit(midi); + return len; +} + +uint32_t tud_tx_buf_write(midid_interface_t* midi, uint8_t const* buffer, uint32_t bufsize) { + uint32_t len; + int32_t last_free = midi->first_free - midi->pending_count - midi->transmitting_count; + if (last_free < 0) { + len = (last_free + midi->tx_buf_len) - midi->first_free; + } else { + len = midi->tx_buf_len - midi->first_free; + } + if (bufsize < len) { + len = bufsize; + } + memcpy(midi->tx_buf + midi->first_free, buffer, len); + // uint32_t remaining_bytes + // if (last_free > 0 && len < bufsize) { + // if () + // + // } + midi->first_free = (midi->first_free + len) % midi->tx_buf_len; + midi->pending_count += len; + + maybe_transmit(midi); + return len; +} + +uint32_t tud_midi_n_write(uint8_t itf, uint8_t jack_id, uint8_t const* buffer, uint32_t bufsize) +{ + midid_interface_t* midi = &_midid_itf[itf]; + if (midi->itf_num == 0) { + return 0; + } + + uint32_t i = 0; + while (i < bufsize) { + uint8_t data = buffer[i]; + if (midi->message_buffer_length == 0) { + uint8_t msg = data >> 4; + midi->message_buffer[1] = data; + midi->message_buffer_length = 2; + // Check to see if we're still in a SysEx transmit. + if (midi->message_buffer[0] == 0x4) { + if (data == 0xf7) { + midi->message_buffer[0] = 0x5; + } else { + midi->message_buffer_length = 4; + } + } else if ((msg >= 0x8 && msg <= 0xB) || msg == 0xE) { + midi->message_buffer[0] = jack_id << 4 | msg; + midi->message_target_length = 4; + } else if (msg == 0xf) { + if (data == 0xf0) { + midi->message_buffer[0] = 0x4; + midi->message_target_length = 4; + } else if (data == 0xf1 || data == 0xf3) { + midi->message_buffer[0] = 0x2; + midi->message_target_length = 3; + } else if (data == 0xf2) { + midi->message_buffer[0] = 0x3; + midi->message_target_length = 4; + } else { + midi->message_buffer[0] = 0x5; + midi->message_target_length = 2; + } + } else { + // Pack individual bytes if we don't support packing them into words. + midi->message_buffer[0] = jack_id << 4 | 0xf; + midi->message_buffer[2] = 0; + midi->message_buffer[3] = 0; + midi->message_buffer_length = 2; + midi->message_target_length = 2; + } + } else { + midi->message_buffer[midi->message_buffer_length] = data; + midi->message_buffer_length += 1; + // See if this byte ends a SysEx. + if (midi->message_buffer[0] == 0x4 && data == 0xf7) { + midi->message_buffer[0] = 0x4 + (midi->message_buffer_length - 1); + midi->message_target_length = midi->message_buffer_length; + } + } + + if (midi->message_buffer_length == midi->message_target_length) { + tud_tx_buf_write(midi, midi->message_buffer, 4); + midi->message_buffer_length = 0; + } + i++; + } + + return i; +} + +//--------------------------------------------------------------------+ +// USBD Driver API +//--------------------------------------------------------------------+ +void midid_init(void) +{ + tu_memclr(_midid_itf, sizeof(_midid_itf)); + + for(uint8_t i=0; irx_ff, midi->rx_ff_buf, CFG_TUD_MIDI_RX_BUFSIZE, 1, true); + #if CFG_FIFO_MUTEX + tu_fifo_config_mutex(&midi->rx_ff, osal_mutex_create(&midi->rx_ff_mutex)); + #endif + + midi->tx_buf_len = CFG_TUD_MIDI_TX_BUFSIZE; + midi->first_free = 0; + midi->pending_count = 0; + midi->transmitting_count = 0; + } +} + +void midid_reset(uint8_t rhport) +{ + (void) rhport; + + for(uint8_t i=0; irx_ff); + midi->tx_buf_len = CFG_TUD_MIDI_TX_BUFSIZE; + midi->first_free = 0; + midi->pending_count = 0; + midi->transmitting_count = 0; + } +} + +bool midid_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length) +{ + // For now handle the audio control interface as well. + if ( AUDIO_SUBCLASS_AUDIO_CONTROL == p_interface_desc->bInterfaceSubClass) { + uint8_t const * p_desc = tu_desc_next ( (uint8_t const *) p_interface_desc ); + (*p_length) = sizeof(tusb_desc_interface_t); + + // Skip over the class specific descriptor. + (*p_length) += p_desc[DESC_OFFSET_LEN]; + p_desc = tu_desc_next(p_desc); + return true; + } + + if ( AUDIO_SUBCLASS_MIDI_STREAMING != p_interface_desc->bInterfaceSubClass || + p_interface_desc->bInterfaceProtocol != AUDIO_PROTOCOL_V1 ) { + return false; + } + + // Find available interface + midid_interface_t * p_midi = NULL; + for(uint8_t i=0; iitf_num = p_interface_desc->bInterfaceNumber; + + uint8_t const * p_desc = tu_desc_next( (uint8_t const *) p_interface_desc ); + (*p_length) = sizeof(tusb_desc_interface_t); + + uint8_t found_endpoints = 0; + while (found_endpoints < p_interface_desc->bNumEndpoints) { + if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE]) + { + TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), false); + uint8_t ep_addr = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress; + if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) { + p_midi->ep_in = ep_addr; + } else { + p_midi->ep_out = ep_addr; + } + + (*p_length) += p_desc[DESC_OFFSET_LEN]; + p_desc = tu_desc_next(p_desc); + found_endpoints += 1; + } + (*p_length) += p_desc[DESC_OFFSET_LEN]; + p_desc = tu_desc_next(p_desc); + } + + // Prepare for incoming data + TU_ASSERT( dcd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, CFG_TUD_MIDI_EPSIZE), false); + + return true; +} + +bool midid_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request) +{ + return false; +} + +bool midid_control_request(uint8_t rhport, tusb_control_request_t const * p_request) +{ + //------------- Class Specific Request -------------// + if (p_request->bmRequestType_bit.type != TUSB_REQ_TYPE_CLASS) return false; + + return false; +} + +bool midid_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes) +{ + // TODO Support multiple interfaces + uint8_t const itf = 0; + midid_interface_t* p_midi = &_midid_itf[itf]; + + // receive new data + if ( edpt_addr == p_midi->ep_out ) + { + midi_rx_done_cb(p_midi, p_midi->epout_buf, xferred_bytes); + + // prepare for next + TU_ASSERT( dcd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, CFG_TUD_MIDI_EPSIZE), false ); + } else if ( edpt_addr == p_midi->ep_in ) { + tud_tx_buf_write_done_cb(p_midi, xferred_bytes); + } + + // nothing to do with in and notif endpoint + + return TUSB_ERROR_NONE; +} + +#endif diff --git a/src/class/midi/midi_device.h b/src/class/midi/midi_device.h new file mode 100644 index 000000000..70030f2ce --- /dev/null +++ b/src/class/midi/midi_device.h @@ -0,0 +1,120 @@ +/**************************************************************************/ +/*! + @file midi_device.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +#ifndef _TUSB_MIDI_DEVICE_H_ +#define _TUSB_MIDI_DEVICE_H_ + +#include "common/tusb_common.h" +#include "device/usbd.h" +#include "class/audio/audio.h" + +//--------------------------------------------------------------------+ +// Class Driver Configuration +//--------------------------------------------------------------------+ +#ifndef CFG_TUD_MIDI_EPSIZE +#define CFG_TUD_MIDI_EPSIZE 64 +#endif + + +#ifdef __cplusplus + extern "C" { +#endif + +/** \addtogroup MIDI_Serial Serial + * @{ + * \defgroup MIDI_Serial_Device Device + * @{ */ + +//--------------------------------------------------------------------+ +// APPLICATION API (Multiple Interfaces) +// CFG_TUD_MIDI > 1 +//--------------------------------------------------------------------+ +bool tud_midi_n_connected (uint8_t itf); + +uint32_t tud_midi_n_available (uint8_t itf, uint8_t jack_id); +char tud_midi_n_read_char (uint8_t itf, uint8_t jack_id); +uint32_t tud_midi_n_read (uint8_t itf, uint8_t jack_id, void* buffer, uint32_t bufsize); +void tud_midi_n_read_flush (uint8_t itf, uint8_t jack_id); +char tud_midi_n_peek (uint8_t itf, uint8_t jack_id, int pos); + +uint32_t tud_midi_n_write_char (uint8_t itf, char ch); +uint32_t tud_midi_n_write (uint8_t itf, uint8_t jack_id, uint8_t const* buffer, uint32_t bufsize); +bool tud_midi_n_write_flush (uint8_t itf); + +//--------------------------------------------------------------------+ +// APPLICATION API (Interface0) +//--------------------------------------------------------------------+ +static inline bool tud_midi_connected (void) { return tud_midi_n_connected(0); } + +static inline uint32_t tud_midi_available (void) { return tud_midi_n_available(0, 0); } +static inline char tud_midi_read_char (void) { return tud_midi_n_read_char(0, 0); } +static inline uint32_t tud_midi_read (void* buffer, uint32_t bufsize) { return tud_midi_n_read(0, 0, buffer, bufsize); } +static inline void tud_midi_read_flush (void) { tud_midi_n_read_flush(0, 0); } +static inline char tud_midi_peek (int pos) { return tud_midi_n_peek(0, 0, pos); } + +static inline uint32_t tud_midi_write_char (char ch) { return tud_midi_n_write_char(0, ch); } +static inline uint32_t tud_midi_write (uint8_t jack_id, void const* buffer, uint32_t bufsize) { return tud_midi_n_write(0, jack_id, buffer, bufsize); } +static inline bool tud_midi_write_flush (void) { return tud_midi_n_write_flush(0); } + +//--------------------------------------------------------------------+ +// APPLICATION CALLBACK API (WEAK is optional) +//--------------------------------------------------------------------+ +ATTR_WEAK void tud_midi_rx_cb(uint8_t itf); + +//--------------------------------------------------------------------+ +// USBD-CLASS DRIVER API +//--------------------------------------------------------------------+ +#ifdef _TINY_USB_SOURCE_FILE_ + +void midid_init (void); +bool midid_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); +bool midid_control_request (uint8_t rhport, tusb_control_request_t const * p_request); +bool midid_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); +bool midid_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes); +void midid_reset (uint8_t rhport); + +#endif + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_MIDI_DEVICE_H_ */ + +/** @} */ +/** @} */ diff --git a/src/device/usbd.c b/src/device/usbd.c index a3fbfdff9..9c08a7932 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -128,6 +128,19 @@ static usbd_class_driver_t const usbd_class_drivers[] = }, #endif + #if CFG_TUD_MIDI + { + .class_code = TUSB_CLASS_AUDIO, + .init = midid_init, + .open = midid_open, + .control_request = midid_control_request, + .control_request_complete = midid_control_request_complete, + .xfer_cb = midid_xfer_cb, + .sof = NULL, + .reset = midid_reset + }, + #endif + #if CFG_TUD_CUSTOM_CLASS { .class_code = TUSB_CLASS_VENDOR_SPECIFIC, diff --git a/src/tusb.h b/src/tusb.h index 389bf54d4..fb20c5ff7 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -89,6 +89,10 @@ #include "class/msc/msc_device.h" #endif + #if CFG_TUD_MIDI + #include "class/midi/midi_device.h" + #endif + #if CFG_TUD_CUSTOM_CLASS #include "class/custom/custom_device.h" #endif @@ -128,4 +132,3 @@ static inline void tusb_task(void) #endif #endif /* _TUSB_H_ */ - -- cgit v1.3.1