From 5abacd9dd9ecf4ece2676f0e91e1cab3e3369dcf Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Oct 2018 16:54:56 +0700 Subject: [PATCH] sync with tinyusb repo --- .../usb/tinyusb/src/class/cdc/cdc_device.c | 4 +- .../tinyusb/src/class/custom/custom_device.c | 2 +- .../usb/tinyusb/src/class/hid/hid_device.c | 8 +- cores/nRF5/usb/tinyusb/src/class/msc/msc.h | 14 +- .../usb/tinyusb/src/class/msc/msc_device.c | 79 +++++---- .../nRF5/usb/tinyusb/src/common/tusb_common.h | 34 +--- .../nRF5/usb/tinyusb/src/common/tusb_types.h | 3 - .../nRF5/usb/tinyusb/src/common/tusb_verify.h | 16 +- cores/nRF5/usb/tinyusb/src/device/dcd.h | 69 ++++++-- cores/nRF5/usb/tinyusb/src/device/usbd.c | 153 ++++++------------ cores/nRF5/usb/tinyusb/src/device/usbd_pvt.h | 2 +- cores/nRF5/usb/tinyusb/src/osal/osal.h | 6 +- .../nRF5/usb/tinyusb/src/osal/osal_freertos.h | 18 +-- cores/nRF5/usb/tinyusb/src/osal/osal_none.h | 18 +-- .../src/portable/nordic/nrf5x/dcd_nrf5x.c | 22 ++- 15 files changed, 199 insertions(+), 249 deletions(-) diff --git a/cores/nRF5/usb/tinyusb/src/class/cdc/cdc_device.c b/cores/nRF5/usb/tinyusb/src/class/cdc/cdc_device.c index 70d51c25..06530bc7 100644 --- a/cores/nRF5/usb/tinyusb/src/class/cdc/cdc_device.c +++ b/cores/nRF5/usb/tinyusb/src/class/cdc/cdc_device.c @@ -185,7 +185,7 @@ bool tud_cdc_n_write_flush (uint8_t itf) //--------------------------------------------------------------------+ void cdcd_init(void) { - arrclr_( _cdcd_itf ); + tu_memclr(_cdcd_itf, sizeof(_cdcd_itf)); for(uint8_t i=0; iep_out) return TUSB_ERROR_NONE; - TU_ASSERT( event == TUSB_EVENT_XFER_COMPLETE && + TU_ASSERT( event == DCD_XFER_SUCCESS && xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE, TUSB_ERROR_INVALID_PARA ); p_csw->signature = MSC_CSW_SIGNATURE; @@ -314,8 +312,8 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u p_csw->data_residue = 0; /*------------- Parse command and prepare DATA -------------*/ - p_msc->stage = MSC_STAGE_DATA; - p_msc->data_len = p_cbw->xfer_bytes; + p_msc->stage = MSC_STAGE_DATA; + p_msc->total_len = p_cbw->total_bytes; p_msc->xferred_len = 0; if (SCSI_CMD_READ_10 == p_cbw->command[0]) @@ -332,13 +330,12 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u // 1. Zero : Invoke app callback, skip DATA and move to STATUS stage // 2. OUT : queue transfer (invoke app callback after done) // 3. IN : invoke app callback to get response - - if ( p_cbw->xfer_bytes == 0) + if ( p_cbw->total_bytes == 0) { int32_t const cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, NULL, 0); - p_msc->data_len = 0; - p_msc->stage = MSC_STAGE_STATUS; + p_msc->total_len = 0; + p_msc->stage = MSC_STAGE_STATUS; if ( cb_result < 0 ) { @@ -353,7 +350,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u else if ( !BIT_TEST_(p_cbw->dir, 7) ) { // OUT transfer - TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->data_len), TUSB_ERROR_DCD_EDPT_XFER ); + TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len), TUSB_ERROR_DCD_EDPT_XFER ); } else { @@ -366,21 +363,21 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u // Not an built-in command, invoke user callback if ( cb_result < 0 ) { - cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->data_len); + cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len); } if ( cb_result > 0 ) { - p_msc->data_len = (uint32_t) cb_result; - p_csw->status = MSC_CSW_STATUS_PASSED; + p_msc->total_len = (uint32_t) cb_result; + p_csw->status = MSC_CSW_STATUS_PASSED; - TU_ASSERT( p_cbw->xfer_bytes >= p_msc->data_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->data_len), TUSB_ERROR_DCD_EDPT_XFER ); + 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 ); }else { - p_msc->data_len = 0; - p_csw->status = MSC_CSW_STATUS_FAILED; - p_msc->stage = MSC_STAGE_STATUS; + p_msc->total_len = 0; + p_csw->status = MSC_CSW_STATUS_FAILED; + p_msc->stage = MSC_STAGE_STATUS; tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation dcd_edpt_stall(rhport, p_msc->ep_in); @@ -395,7 +392,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u { if ( SCSI_CMD_WRITE_10 != p_cbw->command[0] ) { - int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->data_len); + int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len); if ( cb_result < 0 ) { @@ -408,7 +405,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u } else { - uint16_t const block_sz = p_cbw->xfer_bytes / rdwr10_get_blockcount(p_cbw->command); + uint16_t const block_sz = p_cbw->total_bytes / rdwr10_get_blockcount(p_cbw->command); // Adjust lba with transferred bytes uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz); @@ -419,7 +416,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u if ( nbytes < 0 ) { // negative means error -> skip to status phase, status in CSW set to failed - p_csw->data_residue = p_cbw->xfer_bytes - p_msc->xferred_len; + p_csw->data_residue = p_cbw->total_bytes - p_msc->xferred_len; p_csw->status = MSC_CSW_STATUS_FAILED; p_msc->stage = MSC_STAGE_STATUS; @@ -437,7 +434,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u } // simulate an transfer complete with adjusted parameters --> this driver callback will fired again - dcd_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, true); + dcd_event_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, DCD_XFER_SUCCESS, false); return TUSB_ERROR_NONE; // skip the rest } @@ -452,7 +449,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u // Accumulate data so far p_msc->xferred_len += xferred_bytes; - if ( p_msc->xferred_len >= p_msc->data_len ) + if ( p_msc->xferred_len >= p_msc->total_len ) { // Data Stage is complete p_msc->stage = MSC_STAGE_STATUS; @@ -471,7 +468,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u }else { // No other command take more than one transfer yet -> unlikely error - verify_breakpoint(); + TU_BREAKPOINT(); } } break; @@ -486,7 +483,7 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u if ( dcd_edpt_stalled(rhport, p_msc->ep_in) || dcd_edpt_stalled(rhport, p_msc->ep_out) ) { // simulate an transfer complete with adjusted parameters --> this driver callback will fired again - dcd_xfer_complete(rhport, p_msc->ep_out, 0, true); + dcd_event_xfer_complete(rhport, p_msc->ep_out, 0, DCD_XFER_SUCCESS, false); } else { @@ -525,13 +522,13 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc) msc_cbw_t const * p_cbw = &p_msc->cbw; msc_csw_t * p_csw = &p_msc->csw; - uint16_t const block_sz = p_cbw->xfer_bytes / rdwr10_get_blockcount(p_cbw->command); + uint16_t const block_sz = p_cbw->total_bytes / rdwr10_get_blockcount(p_cbw->command); // Adjust lba with transferred bytes uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz); // remaining bytes capped at class buffer - int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->xfer_bytes-p_msc->xferred_len); + int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len); // Application can consume smaller bytes nbytes = tud_msc_read10_cb(p_cbw->lun, lba, p_msc->xferred_len % block_sz, _mscd_buf, (uint32_t) nbytes); @@ -539,7 +536,7 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc) if ( nbytes < 0 ) { // negative means error -> pipe is stalled & status in CSW set to failed - p_csw->data_residue = p_cbw->xfer_bytes - p_msc->xferred_len; + p_csw->data_residue = p_cbw->total_bytes - p_msc->xferred_len; p_csw->status = MSC_CSW_STATUS_FAILED; tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation @@ -548,7 +545,7 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc) else if ( nbytes == 0 ) { // zero means not ready -> simulate an transfer complete so that this driver callback will fired again - dcd_xfer_complete(rhport, p_msc->ep_in, 0, true); + dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, DCD_XFER_SUCCESS, false); } else { @@ -561,7 +558,7 @@ static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc) msc_cbw_t const * p_cbw = &p_msc->cbw; // remaining bytes capped at class buffer - int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->xfer_bytes-p_msc->xferred_len); + int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len); // Write10 callback will be called later when usb transfer complete TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), ); diff --git a/cores/nRF5/usb/tinyusb/src/common/tusb_common.h b/cores/nRF5/usb/tinyusb/src/common/tusb_common.h index 778e7067..85bd15d4 100644 --- a/cores/nRF5/usb/tinyusb/src/common/tusb_common.h +++ b/cores/nRF5/usb/tinyusb/src/common/tusb_common.h @@ -73,8 +73,7 @@ //--------------------------------------------------------------------+ // MACROS //--------------------------------------------------------------------+ -#define MAX_OF(a, b) ( (a) > (b) ? (a) : (b) ) -#define MIN_OF(a, b) ( (a) < (b) ? (a) : (b) ) +#define TU_ARRAY_SZIE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) ) #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff)) #define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff)) @@ -137,12 +136,10 @@ //--------------------------------------------------------------------+ // INLINE FUNCTION //--------------------------------------------------------------------+ -#define memclr_(buffer, size) memset((buffer), 0, (size)) -#define varclr_(_var) memclr_(_var, sizeof(*(_var))) -#define arrclr_(_arr) memclr_(_arr, sizeof(_arr)) -#define arrcount_(_arr) ( sizeof(_arr) / sizeof(_arr[0]) ) +#define tu_memclr(buffer, size) memset((buffer), 0, (size)) +#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var))) -static inline bool mem_all_zero(void const* buffer, uint32_t size) +static inline bool tu_mem_test_zero (void const* buffer, uint32_t size) { uint8_t const* p_mem = (uint8_t const*) buffer; for(uint32_t i=0; i y) ? x : y; } -static inline uint16_t tu_max16(uint16_t x, uint16_t y) -{ - return (x > y) ? x : y; -} - //------------- Align -------------// static inline uint32_t tu_align32 (uint32_t value) { @@ -249,24 +241,6 @@ static inline uint8_t tu_log2(uint32_t value) return result; } -// return the number of set bits in value -static inline uint8_t tu_cardof(uint32_t value) -{ - // Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only - // the high bit set, then it will only go once through the loop - // Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) - // mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method - // "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and - // published in 1964 in a book edited by Beckenbach.)" - uint8_t count; - for (count = 0; value; count++) - { - value &= value - 1; // clear the least significant bit set - } - - return count; -} - #ifdef __cplusplus } #endif diff --git a/cores/nRF5/usb/tinyusb/src/common/tusb_types.h b/cores/nRF5/usb/tinyusb/src/common/tusb_types.h index ee9e849b..8ce334e5 100644 --- a/cores/nRF5/usb/tinyusb/src/common/tusb_types.h +++ b/cores/nRF5/usb/tinyusb/src/common/tusb_types.h @@ -197,9 +197,6 @@ typedef enum TUSB_EVENT_XFER_COMPLETE, TUSB_EVENT_XFER_ERROR, TUSB_EVENT_XFER_STALLED, - - TUSB_EVENT_BUS_RESET, // TODO refractor - TUSB_EVENT_SETUP_RECEIVED, }tusb_event_t; enum { diff --git a/cores/nRF5/usb/tinyusb/src/common/tusb_verify.h b/cores/nRF5/usb/tinyusb/src/common/tusb_verify.h index 6588ff5b..c22c53db 100644 --- a/cores/nRF5/usb/tinyusb/src/common/tusb_verify.h +++ b/cores/nRF5/usb/tinyusb/src/common/tusb_verify.h @@ -74,14 +74,14 @@ // Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7 #if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) -#define verify_breakpoint() \ +#define TU_BREAKPOINT() \ do {\ volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \ if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */\ } while(0) #else -#define verify_breakpoint() +#define TU_BREAKPOINT() #endif /*------------------------------------------------------------------*/ @@ -157,21 +157,21 @@ /*------------------------------------------------------------------*/ /* ASSERT - * basically TU_VERIFY with verify_breakpoint() as handler + * basically TU_VERIFY with TU_BREAKPOINT() as handler * - 1 arg : return false if failed * - 2 arg : return error if failed *------------------------------------------------------------------*/ -#define ASSERT_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); verify_breakpoint(), false) -#define ASSERT_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); verify_breakpoint(), _ret) +#define ASSERT_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), false) +#define ASSERT_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), _ret) #define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__) /*------------------------------------------------------------------*/ /* ASSERT Error - * basically TU_VERIFY Error with verify_breakpoint() as handler + * basically TU_VERIFY Error with TU_BREAKPOINT() as handler *------------------------------------------------------------------*/ -#define ASERT_ERR_1ARGS(_error) TU_VERIFY_ERR_DEF2(_error, verify_breakpoint()) -#define ASERT_ERR_2ARGS(_error, _ret) TU_VERIFY_ERR_DEF3(_error, verify_breakpoint(), _ret) +#define ASERT_ERR_1ARGS(_error) TU_VERIFY_ERR_DEF2(_error, TU_BREAKPOINT()) +#define ASERT_ERR_2ARGS(_error, _ret) TU_VERIFY_ERR_DEF3(_error, TU_BREAKPOINT(), _ret) #define TU_ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__) diff --git a/cores/nRF5/usb/tinyusb/src/device/dcd.h b/cores/nRF5/usb/tinyusb/src/device/dcd.h index e246f052..66249670 100644 --- a/cores/nRF5/usb/tinyusb/src/device/dcd.h +++ b/cores/nRF5/usb/tinyusb/src/device/dcd.h @@ -49,14 +49,52 @@ extern "C" { #endif +enum +{ + DCD_XFER_SUCCESS = 0, + DCD_XFER_FAILED, + DCD_XFER_STALLED +}; + typedef enum { - USBD_BUS_EVENT_RESET = 1, - USBD_BUS_EVENT_UNPLUGGED, - USBD_BUS_EVENT_SOF, - USBD_BUS_EVENT_SUSPENDED, - USBD_BUS_EVENT_RESUME -}usbd_bus_event_type_t; + DCD_EVENT_BUS_RESET = 1, + DCD_EVENT_UNPLUGGED, + DCD_EVENT_SOF, + DCD_EVENT_SUSPENDED, + DCD_EVENT_RESUME, + + DCD_EVENT_SETUP_RECEIVED, + DCD_EVENT_XFER_COMPLETE, + + USBD_EVT_FUNC_CALL +}usbd_eventid_t; + +typedef struct ATTR_ALIGNED(4) +{ + uint8_t rhport; + uint8_t event_id; + + union { + // USBD_EVT_SETUP_RECEIVED + tusb_control_request_t setup_received; + + // USBD_EVT_XFER_COMPLETE + struct { + uint8_t ep_addr; + uint8_t result; + uint32_t len; + }xfer_complete; + + // USBD_EVT_FUNC_CALL + struct { + void (*func) (void*); + void* param; + }func_call; + }; +} dcd_event_t; + +TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct"); /*------------------------------------------------------------------*/ /* Device API (Weak is optional) @@ -72,14 +110,21 @@ void dcd_disconnect (uint8_t rhport) ATTR_WEAK; /* Event Function * Called by DCD to notify USBD *------------------------------------------------------------------*/ -void dcd_bus_event (uint8_t rhport, usbd_bus_event_type_t bus_event); -void dcd_setup_received (uint8_t rhport, uint8_t const* p_request); -void dcd_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, bool succeeded); +void dcd_event_handler(dcd_event_t const * event, bool in_isr); -static inline void dcd_control_complete(uint8_t rhport, uint32_t xferred_bytes) +static inline void dcd_event_xfer_complete(uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr) { - // all control complete is successful !! - dcd_xfer_complete(rhport, 0, xferred_bytes, true); + dcd_event_t event = + { + .rhport = 0, + .event_id = DCD_EVENT_XFER_COMPLETE, + }; + + event.xfer_complete.ep_addr = ep_addr; + event.xfer_complete.len = xferred_bytes; + event.xfer_complete.result = result; + + dcd_event_handler(&event, true); } /*------------------------------------------------------------------*/ diff --git a/cores/nRF5/usb/tinyusb/src/device/usbd.c b/cores/nRF5/usb/tinyusb/src/device/usbd.c index 89dd4b72..81594845 100644 --- a/cores/nRF5/usb/tinyusb/src/device/usbd.c +++ b/cores/nRF5/usb/tinyusb/src/device/usbd.c @@ -156,50 +156,15 @@ enum { USBD_CLASS_DRIVER_COUNT = sizeof(usbd_class_drivers) / sizeof(usbd_class_ //--------------------------------------------------------------------+ // DCD Event //--------------------------------------------------------------------+ -typedef enum -{ - USBD_EVT_SETUP_RECEIVED = 1, - USBD_EVT_XFER_DONE, - USBD_EVT_SOF, - - USBD_EVT_FUNC_CALL -}usbd_eventid_t; - -typedef struct ATTR_ALIGNED(4) -{ - uint8_t rhport; - uint8_t event_id; - - union { - // USBD_EVT_SETUP_RECEIVED - tusb_control_request_t setup_received; - - // USBD_EVT_XFER_DONE - struct { - uint8_t ep_addr; - uint8_t result; - uint32_t xferred_byte; - }xfer_done; - - // USBD_EVT_FUNC_CALL - struct { - osal_task_func_t func; - void* param; - }func_call; - }; -} usbd_task_event_t; - -TU_VERIFY_STATIC(sizeof(usbd_task_event_t) <= 12, "size is not correct"); - 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, usbd_task_event_t); +OSAL_QUEUE_DEF(_usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t); static osal_queue_t _usbd_q; /*------------- control transfer semaphore -------------*/ static osal_semaphore_def_t _usbd_sem_def; -/*static*/ osal_semaphore_t _usbd_ctrl_sem; +osal_semaphore_t _usbd_ctrl_sem; //--------------------------------------------------------------------+ // INTERNAL FUNCTION @@ -264,7 +229,7 @@ void usbd_task( void* param) static tusb_error_t usbd_main_st(void) { - static usbd_task_event_t event; + static dcd_event_t event; OSAL_SUBTASK_BEGIN @@ -274,26 +239,26 @@ static tusb_error_t usbd_main_st(void) tusb_error_t err; err = TUSB_ERROR_NONE; - memclr_(&event, sizeof(usbd_task_event_t)); + tu_memclr(&event, sizeof(dcd_event_t)); osal_queue_receive(_usbd_q, &event, OSAL_TIMEOUT_WAIT_FOREVER, &err); - if ( USBD_EVT_SETUP_RECEIVED == event.event_id ) + if ( DCD_EVENT_SETUP_RECEIVED == event.event_id ) { STASK_INVOKE( proc_control_request_st(event.rhport, &event.setup_received), err ); } - else if (USBD_EVT_XFER_DONE == event.event_id) + else if (DCD_EVENT_XFER_COMPLETE == event.event_id) { // Invoke the class callback associated with the endpoint address - uint8_t const ep_addr = event.xfer_done.ep_addr; + uint8_t const ep_addr = event.xfer_complete.ep_addr; uint8_t const drv_id = _usbd_dev.ep2drv[ edpt_dir(ep_addr) ][ edpt_number(ep_addr) ]; if (drv_id < USBD_CLASS_DRIVER_COUNT) { - usbd_class_drivers[drv_id].xfer_cb( event.rhport, ep_addr, (tusb_event_t) event.xfer_done.result, event.xfer_done.xferred_byte); + usbd_class_drivers[drv_id].xfer_cb( event.rhport, ep_addr, (tusb_event_t) event.xfer_complete.result, event.xfer_complete.len); } } - else if (USBD_EVT_SOF == event.event_id) + else if (DCD_EVENT_SOF == event.event_id) { for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) { @@ -309,7 +274,7 @@ static tusb_error_t usbd_main_st(void) } else { - verify_breakpoint(); + TU_BREAKPOINT(); } } @@ -318,7 +283,7 @@ static tusb_error_t usbd_main_st(void) static void usbd_reset(uint8_t rhport) { - varclr_(&_usbd_dev); + tu_varclr(&_usbd_dev); memset(_usbd_dev.itf2drv, 0xff, sizeof(_usbd_dev.itf2drv)); // invalid mapping memset(_usbd_dev.ep2drv , 0xff, sizeof(_usbd_dev.ep2drv )); // invalid mapping @@ -567,83 +532,69 @@ static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, ui //--------------------------------------------------------------------+ // USBD-DCD Callback API //--------------------------------------------------------------------+ -void dcd_bus_event(uint8_t rhport, usbd_bus_event_type_t bus_event) +void dcd_event_handler(dcd_event_t const * event, bool in_isr) { - switch(bus_event) + uint8_t const rhport = event->rhport; + + switch (event->event_id) { - case USBD_BUS_EVENT_RESET: + case DCD_EVENT_BUS_RESET: usbd_reset(rhport); osal_queue_flush(_usbd_q); osal_semaphore_reset_isr(_usbd_ctrl_sem); break; - case USBD_BUS_EVENT_SOF: + case DCD_EVENT_SOF: { #if 0 - usbd_task_event_t task_event = + dcd_event_t task_event = { .rhport = rhport, - .event_id = USBD_EVT_SOF, + .event_id = DCD_EVENT_SOF, }; - osal_queue_send_isr(_usbd_q, &task_event); + osal_queue_send(_usbd_q, &task_event, in_isr); #endif } break; - case USBD_BUS_EVENT_UNPLUGGED: + case DCD_EVENT_UNPLUGGED: usbd_reset(rhport); tud_umount_cb(); // invoke callback break; - case USBD_BUS_EVENT_SUSPENDED: + case DCD_EVENT_SUSPENDED: // TODO support suspended break; + case DCD_EVENT_RESUME: + // TODO support resume + break; + + case DCD_EVENT_SETUP_RECEIVED: + osal_queue_send(_usbd_q, event, in_isr); + break; + + case DCD_EVENT_XFER_COMPLETE: + if (event->xfer_complete.ep_addr == 0) + { + // only signal data stage, skip status (zero byte) + if (event->xfer_complete.len) + { + (void) event->xfer_complete.result; // TODO handle control error/stalled + osal_semaphore_post( _usbd_ctrl_sem, in_isr); + } + }else + { + osal_queue_send(_usbd_q, event, in_isr); + } + TU_ASSERT(event->xfer_complete.result == DCD_XFER_SUCCESS,); + break; + default: break; } } -void dcd_setup_received(uint8_t rhport, uint8_t const* p_request) -{ - usbd_task_event_t task_event = - { - .rhport = rhport, - .event_id = USBD_EVT_SETUP_RECEIVED, - }; - - memcpy(&task_event.setup_received, p_request, sizeof(tusb_control_request_t)); - osal_queue_send_isr(_usbd_q, &task_event); -} - -void dcd_xfer_complete(uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, bool succeeded) -{ - if (ep_addr == 0 ) - { - // Control Transfer - (void) rhport; - (void) succeeded; - - // only signal data stage, skip status (zero byte) - if (xferred_bytes) osal_semaphore_post_isr( _usbd_ctrl_sem ); - }else - { - usbd_task_event_t event = - { - .rhport = rhport, - .event_id = USBD_EVT_XFER_DONE, - }; - - event.xfer_done.ep_addr = ep_addr; - event.xfer_done.xferred_byte = xferred_bytes; - event.xfer_done.result = succeeded ? TUSB_EVENT_XFER_COMPLETE : TUSB_EVENT_XFER_ERROR; - - osal_queue_send_isr(_usbd_q, &event); - } - - TU_ASSERT(succeeded, ); -} - //--------------------------------------------------------------------+ // Helper //--------------------------------------------------------------------+ @@ -670,9 +621,9 @@ 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 isr ) +void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr ) { - usbd_task_event_t event = + dcd_event_t event = { .rhport = 0, .event_id = USBD_EVT_FUNC_CALL, @@ -681,13 +632,7 @@ void usbd_defer_func(osal_task_func_t func, void* param, bool isr ) event.func_call.func = func; event.func_call.param = param; - if ( isr ) - { - osal_queue_send_isr(_usbd_q, &event); - }else - { - osal_queue_send(_usbd_q, &event); - } + osal_queue_send(_usbd_q, &event, in_isr); } #endif diff --git a/cores/nRF5/usb/tinyusb/src/device/usbd_pvt.h b/cores/nRF5/usb/tinyusb/src/device/usbd_pvt.h index 0bc288d7..59887c9e 100644 --- a/cores/nRF5/usb/tinyusb/src/device/usbd_pvt.h +++ b/cores/nRF5/usb/tinyusb/src/device/usbd_pvt.h @@ -81,7 +81,7 @@ tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_d /*------------------------------------------------------------------*/ /* Other Helpers *------------------------------------------------------------------*/ -void usbd_defer_func( osal_task_func_t func, void* param, bool isr ); +void usbd_defer_func( osal_task_func_t func, void* param, bool in_isr ); #ifdef __cplusplus diff --git a/cores/nRF5/usb/tinyusb/src/osal/osal.h b/cores/nRF5/usb/tinyusb/src/osal/osal.h index d6483720..93ac48b1 100644 --- a/cores/nRF5/usb/tinyusb/src/osal/osal.h +++ b/cores/nRF5/usb/tinyusb/src/osal/osal.h @@ -80,15 +80,13 @@ typedef void (*osal_task_func_t)( void * ); * osal_queue_def_t, osal_queue_t * osal_queue_t osal_queue_create(osal_queue_def_t* qdef) * osal_queue_receive (osal_queue_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) - * bool osal_queue_send_isr(osal_queue_t const queue_hdl, void const * data) - * bool osal_queue_send(osal_queue_t const queue_hdl, void const * data) + * bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr) * osal_queue_flush() TODO remove * * Semaphore * osal_semaphore_def_t, osal_semaphore_t * osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) - * bool osal_semaphore_post_isr(osal_semaphore_t sem_hdl) - * bool osal_semaphore_post(osal_semaphore_t sem_hdl) + * bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) * void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error) * void osal_semaphore_reset_isr(osal_semaphore_t const sem_hdl) * diff --git a/cores/nRF5/usb/tinyusb/src/osal/osal_freertos.h b/cores/nRF5/usb/tinyusb/src/osal/osal_freertos.h index ff39bab3..0308b051 100644 --- a/cores/nRF5/usb/tinyusb/src/osal/osal_freertos.h +++ b/cores/nRF5/usb/tinyusb/src/osal/osal_freertos.h @@ -120,14 +120,9 @@ static inline void osal_queue_receive (osal_queue_t const queue_hdl, void *p_dat (*p_error) = ( xQueueReceive(queue_hdl, p_data, ticks) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT); } -static inline bool osal_queue_send_isr(osal_queue_t const queue_hdl, void const * data) +static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr) { - return xQueueSendToBackFromISR(queue_hdl, data, NULL); -} - -static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data) -{ - return xQueueSendToBack(queue_hdl, data, OSAL_TIMEOUT_WAIT_FOREVER) == pdTRUE; + return in_isr ? xQueueSendToBackFromISR(queue_hdl, data, NULL) : xQueueSendToBack(queue_hdl, data, OSAL_TIMEOUT_WAIT_FOREVER); } static inline void osal_queue_flush(osal_queue_t const queue_hdl) @@ -147,14 +142,9 @@ static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semde return xSemaphoreCreateBinaryStatic(semdef); } -static inline bool osal_semaphore_post_isr(osal_semaphore_t sem_hdl) +static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { - return xSemaphoreGiveFromISR(sem_hdl, NULL) == pdTRUE; -} - -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl) -{ - return xSemaphoreGive(sem_hdl) == pdTRUE; + return in_isr ? xSemaphoreGiveFromISR(sem_hdl, NULL) : xSemaphoreGive(sem_hdl); } static inline void osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec, tusb_error_t *p_error) diff --git a/cores/nRF5/usb/tinyusb/src/osal/osal_none.h b/cores/nRF5/usb/tinyusb/src/osal/osal_none.h index 22c89868..783e4fd2 100644 --- a/cores/nRF5/usb/tinyusb/src/osal/osal_none.h +++ b/cores/nRF5/usb/tinyusb/src/osal/osal_none.h @@ -113,11 +113,11 @@ static inline bool osal_task_create(osal_task_def_t* taskdef) //------------- Sub Task Assert -------------// #define STASK_RETURN(error) do { TASK_RESTART; return error; } while(0) -#define STASK_ASSERT_ERR(_err) TU_VERIFY_ERR_HDLR(_err, verify_breakpoint(); TASK_RESTART, TUSB_ERROR_FAILED) -#define STASK_ASSERT_ERR_HDLR(_err, _func) TU_VERIFY_ERR_HDLR(_err, verify_breakpoint(); _func; TASK_RESTART, TUSB_ERROR_FAILED ) +#define STASK_ASSERT_ERR(_err) TU_VERIFY_ERR_HDLR(_err, TU_BREAKPOINT(); TASK_RESTART, TUSB_ERROR_FAILED) +#define STASK_ASSERT_ERR_HDLR(_err, _func) TU_VERIFY_ERR_HDLR(_err, TU_BREAKPOINT(); _func; TASK_RESTART, TUSB_ERROR_FAILED ) -#define STASK_ASSERT(_cond) TU_VERIFY_HDLR(_cond, verify_breakpoint(); TASK_RESTART, TUSB_ERROR_FAILED) -#define STASK_ASSERT_HDLR(_cond, _func) TU_VERIFY_HDLR(_cond, verify_breakpoint(); _func; TASK_RESTART, TUSB_ERROR_FAILED) +#define STASK_ASSERT(_cond) TU_VERIFY_HDLR(_cond, TU_BREAKPOINT(); TASK_RESTART, TUSB_ERROR_FAILED) +#define STASK_ASSERT_HDLR(_cond, _func) TU_VERIFY_HDLR(_cond, TU_BREAKPOINT(); _func; TASK_RESTART, TUSB_ERROR_FAILED) //--------------------------------------------------------------------+ // QUEUE API @@ -133,13 +133,12 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) return (osal_queue_t) qdef; } -static inline bool osal_queue_send_isr(osal_queue_t const queue_hdl, void const * data) +static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr) { + (void) in_isr; return tu_fifo_write( (tu_fifo_t*) queue_hdl, data); } -#define osal_queue_send osal_queue_send_isr - static inline void osal_queue_flush(osal_queue_t const queue_hdl) { queue_hdl->count = queue_hdl->rd_idx = queue_hdl->wr_idx = 0; @@ -182,10 +181,9 @@ static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semde return semdef; } -#define osal_semaphore_post_isr osal_semaphore_post - -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl) +static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { + (void) in_isr; if (sem_hdl->count < sem_hdl->max_count ) sem_hdl->count++; return true; } diff --git a/cores/nRF5/usb/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c b/cores/nRF5/usb/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c index c33aea3b..1dc8bc3e 100644 --- a/cores/nRF5/usb/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/cores/nRF5/usb/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -108,7 +108,7 @@ void bus_reset(void) NRF_USBD->TASKS_STARTISOIN = 0; NRF_USBD->TASKS_STARTISOOUT = 0; - varclr_(&_dcd); + tu_varclr(&_dcd); } /*------------------------------------------------------------------*/ @@ -418,7 +418,9 @@ void USBD_IRQHandler(void) if ( int_status & USBD_INTEN_USBRESET_Msk ) { bus_reset(); - dcd_bus_event(0, USBD_BUS_EVENT_RESET); + + dcd_event_t event = { .rhport = 0, .event_id = DCD_EVENT_BUS_RESET }; + dcd_event_handler(&event, true); } if ( int_status & EDPT_END_ALL_MASK ) @@ -435,7 +437,10 @@ void USBD_IRQHandler(void) NRF_USBD->WINDEXL , NRF_USBD->WINDEXH , NRF_USBD->WLENGTHL, NRF_USBD->WLENGTHH }; - dcd_setup_received(0, setup); + dcd_event_t event = { .rhport = 0, .event_id = DCD_EVENT_SETUP_RECEIVED }; + memcpy(&event.setup_received, setup, 8); + + dcd_event_handler(&event, true); } if ( int_status & USBD_INTEN_EP0DATADONE_Msk ) @@ -454,7 +459,7 @@ void USBD_IRQHandler(void) }else { // Control IN complete - dcd_control_complete(0, _dcd.control.actual_len); + dcd_event_xfer_complete(0, 0, _dcd.control.actual_len, DCD_XFER_SUCCESS, true); } } } @@ -468,7 +473,7 @@ void USBD_IRQHandler(void) }else { // Control OUT complete - dcd_control_complete(0, _dcd.control.actual_len); + dcd_event_xfer_complete(0, 0, _dcd.control.actual_len, DCD_XFER_SUCCESS, true); } } @@ -499,7 +504,7 @@ void USBD_IRQHandler(void) xfer->total_len = xfer->actual_len; // BULK/INT OUT complete - dcd_xfer_complete(0, epnum, xfer->actual_len, true); + dcd_event_xfer_complete(0, epnum, xfer->actual_len, DCD_XFER_SUCCESS, true); } } @@ -528,7 +533,7 @@ void USBD_IRQHandler(void) } else { // Bulk/Int IN complete - dcd_xfer_complete(0, epnum | TUSB_DIR_IN_MASK, xfer->actual_len, true); + dcd_event_xfer_complete(0, epnum | TUSB_DIR_IN_MASK, xfer->actual_len, DCD_XFER_SUCCESS, true); } } } @@ -556,7 +561,8 @@ void USBD_IRQHandler(void) // SOF interrupt if ( int_status & USBD_INTEN_SOF_Msk ) { - dcd_bus_event(0, USBD_BUS_EVENT_SOF); + dcd_event_t event = { .rhport = 0, .event_id = DCD_EVENT_SOF }; + dcd_event_handler(&event, true); } }