From 6804fee0fa571907f9311cce4f5e65aaa569dd1a Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 24 Apr 2014 23:40:28 +0700 Subject: correct dcd_pipe_is_busy to use list_qtd_idx[] instead of qtd_overlay flush usbd_queue_hdl when bus_reset add assert check for osal_queue_send increase ENUM_QUEUE_DEPTH for usbh change osal_freeRTOS.h implementation to - correctly waiting forever for semaphore wait, queue & mutex - not use ISR safe version since it is not as generic as we want --- tinyusb/class/cdc_device.c | 1 - tinyusb/class/msc_device.c | 2 +- tinyusb/device/dcd_lpc43xx.c | 27 +++++++++------------------ tinyusb/device/usbd.c | 22 +++++++++++----------- tinyusb/host/usbh.c | 2 +- tinyusb/osal/osal_freeRTOS.h | 23 +++++++++++------------ 6 files changed, 33 insertions(+), 44 deletions(-) (limited to 'tinyusb') diff --git a/tinyusb/class/cdc_device.c b/tinyusb/class/cdc_device.c index 2c90bfc4b..5cee0f898 100644 --- a/tinyusb/class/cdc_device.c +++ b/tinyusb/class/cdc_device.c @@ -204,7 +204,6 @@ tusb_error_t cdcd_control_request_subtask(uint8_t coreid, tusb_control_request_t case CDC_REQUEST_SET_CONTROL_LINE_STATE: // TODO extract DTE present break; - default: return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; } diff --git a/tinyusb/class/msc_device.c b/tinyusb/class/msc_device.c index 091faabb6..742fc8524 100644 --- a/tinyusb/class/msc_device.c +++ b/tinyusb/class/msc_device.c @@ -158,7 +158,7 @@ tusb_error_t mscd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32 //------------- new CBW received -------------// if ( !is_waiting_read10_write10 ) { - if ( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) ) return TUSB_ERROR_NONE; // bulk in interrupt for dcd to clean up +// if ( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) ) return TUSB_ERROR_NONE; // bulk in interrupt for dcd to clean up ASSERT( endpointhandle_is_equal(p_msc->edpt_out, edpt_hdl) && xferred_bytes == sizeof(msc_cmd_block_wrapper_t) && diff --git a/tinyusb/device/dcd_lpc43xx.c b/tinyusb/device/dcd_lpc43xx.c index ea020a4ef..60d8e2fc7 100644 --- a/tinyusb/device/dcd_lpc43xx.c +++ b/tinyusb/device/dcd_lpc43xx.c @@ -151,7 +151,7 @@ typedef struct ATTR_ALIGNED(64) { /// thus there are 16 bytes padding free that we can make use of. //--------------------------------------------------------------------+ uint8_t class_code; // Class code that endpoint belongs to - uint8_t list_qtd_idx[DCD_QTD_PER_QHD_MAX]; + volatile uint8_t list_qtd_idx[DCD_QTD_PER_QHD_MAX]; uint8_t reserved[15-DCD_QTD_PER_QHD_MAX]; } dcd_qhd_t; @@ -167,6 +167,9 @@ typedef struct { }dcd_data_t; +extern ATTR_WEAK dcd_data_t dcd_data0; +extern ATTR_WEAK dcd_data_t dcd_data1; + #if (TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE) TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(2048) STATIC_VAR dcd_data_t dcd_data0; #endif @@ -176,20 +179,7 @@ TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(2048) STATIC_VAR dcd_data_t dcd_data1; #endif static LPC_USB0_Type * const LPC_USB[2] = { LPC_USB0, ((LPC_USB0_Type*) LPC_USB1_BASE) }; -static dcd_data_t* const dcd_data_ptr[2] = -{ - #if (TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE) - &dcd_data0, - #else - NULL, - #endif - - #if (TUSB_CFG_CONTROLLER_1_MODE & TUSB_MODE_DEVICE) - &dcd_data1 - #else - NULL - #endif -}; +static dcd_data_t* const dcd_data_ptr[2] = { &dcd_data0, &dcd_data1 }; //--------------------------------------------------------------------+ // CONTROLLER API @@ -364,6 +354,7 @@ tusb_error_t dcd_pipe_control_xfer(uint8_t coreid, tusb_direction_t dir, uint8_t uint8_t const ep_status = 1 - ep_data; while(lpc_usb->ENDPTSETUPSTAT & BIT_(0)) {} // wait until ENDPTSETUPSTAT before priming data/status in response +// while(p_dcd->qhd[0].qtd_overlay.active || p_dcd->qhd[1].qtd_overlay.active) {}; // wait until previous device request is completed ASSERT_FALSE(p_dcd->qhd[0].qtd_overlay.active || p_dcd->qhd[1].qtd_overlay.active, TUSB_ERROR_FAILED); //------------- Data Phase -------------// @@ -454,10 +445,10 @@ endpoint_handle_t dcd_pipe_open(uint8_t coreid, tusb_descriptor_endpoint_t const bool dcd_pipe_is_busy(endpoint_handle_t edpt_hdl) { - dcd_qhd_t* p_qhd = &dcd_data_ptr[edpt_hdl.coreid]->qhd[edpt_hdl.index]; + dcd_qhd_t const * p_qhd = &dcd_data_ptr[edpt_hdl.coreid]->qhd[edpt_hdl.index]; - // LPC_USB0->ENDPTSTAT & endpoint_phy2pos(edpt_hdl.index) - return !p_qhd->qtd_overlay.halted && p_qhd->qtd_overlay.active; + return p_qhd->list_qtd_idx[0] != 0; // qtd list is not empty +// return !p_qhd->qtd_overlay.halted && p_qhd->qtd_overlay.active; } // add only, controller virtually cannot know diff --git a/tinyusb/device/usbd.c b/tinyusb/device/usbd.c index 258a8ef9f..6200113b6 100644 --- a/tinyusb/device/usbd.c +++ b/tinyusb/device/usbd.c @@ -112,7 +112,7 @@ bool tusbd_is_configured(uint8_t coreid) //--------------------------------------------------------------------+ //------------- OSAL Task -------------// -enum { USBD_TASK_QUEUE_DEPTH = 8 }; +enum { USBD_TASK_QUEUE_DEPTH = 16 }; typedef enum { USBD_EVENTID_SETUP_RECEIVED = 1, @@ -200,24 +200,23 @@ static tusb_error_t usbd_body_subtask(void) tusb_error_t error; error = TUSB_ERROR_NONE; + memclr_(&event, sizeof(usbd_task_event_t)); osal_queue_receive(usbd_queue_hdl, &event, OSAL_TIMEOUT_WAIT_FOREVER, &error); SUBTASK_ASSERT_STATUS(error); if ( USBD_EVENTID_SETUP_RECEIVED == event.event_id ) { OSAL_SUBTASK_INVOKED_AND_WAIT( usbd_control_request_subtask(event.coreid, &event.setup_received), error ); - }else + }else if (USBD_EVENTID_XFER_DONE == event.event_id) { uint8_t class_index; class_index = std_class_code_to_index( event.xfer_done.edpt_hdl.class_code ); - if (usbd_class_drivers[class_index].xfer_cb) - { - usbd_class_drivers[class_index].xfer_cb( event.xfer_done.edpt_hdl, (tusb_event_t) event.sub_event_id, event.xfer_done.xferred_byte); - }else - { - hal_debugger_breakpoint(); // something wrong, no one claims the isr's source - } + SUBTASK_ASSERT(usbd_class_drivers[class_index].xfer_cb != NULL); + usbd_class_drivers[class_index].xfer_cb( event.xfer_done.edpt_hdl, (tusb_event_t) event.sub_event_id, event.xfer_done.xferred_byte); + }else + { + SUBTASK_ASSERT(false); } OSAL_SUBTASK_END @@ -400,6 +399,7 @@ void usbd_dcd_bus_event_isr(uint8_t coreid, usbd_bus_event_type_t bus_event) case USBD_BUS_EVENT_RESET : case USBD_BUS_EVENT_UNPLUGGED : memclr_(&usbd_devices[coreid], sizeof(usbd_device_info_t)); + osal_queue_flush(usbd_queue_hdl); osal_semaphore_reset(usbd_control_xfer_sem_hdl); for (uint8_t class_code = TUSB_CLASS_AUDIO; class_code < USBD_CLASS_DRIVER_COUNT; class_code++) { @@ -424,7 +424,7 @@ void usbd_setup_received_isr(uint8_t coreid, tusb_control_request_t * p_request) }; task_event.setup_received = (*p_request); - osal_queue_send(usbd_queue_hdl, &task_event); + ASSERT( TUSB_ERROR_NONE == osal_queue_send(usbd_queue_hdl, &task_event), VOID_RETURN); } void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes) @@ -444,7 +444,7 @@ void usbd_xfer_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xfer task_event.xfer_done.xferred_byte = xferred_bytes; task_event.xfer_done.edpt_hdl = edpt_hdl; - osal_queue_send(usbd_queue_hdl, &task_event); + ASSERT( TUSB_ERROR_NONE == osal_queue_send(usbd_queue_hdl, &task_event), VOID_RETURN); } } diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 542f6d5bf..b36fa7cd1 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -108,7 +108,7 @@ enum { USBH_CLASS_DRIVER_COUNT = sizeof(usbh_class_drivers) / sizeof(host_class_ TUSB_CFG_ATTR_USBRAM usbh_device_info_t usbh_devices[TUSB_CFG_HOST_DEVICE_MAX+1]; // including zero-address //------------- Enumeration Task Data -------------/ -enum { ENUM_QUEUE_DEPTH = 8 }; +enum { ENUM_QUEUE_DEPTH = 16 }; OSAL_TASK_DEF(usbh_enumeration_task, 200, TUSB_CFG_OS_TASK_PRIO); OSAL_QUEUE_DEF(enum_queue_def, ENUM_QUEUE_DEPTH, uint32_t); diff --git a/tinyusb/osal/osal_freeRTOS.h b/tinyusb/osal/osal_freeRTOS.h index 4d35c3c0f..934b9bbad 100644 --- a/tinyusb/osal/osal_freeRTOS.h +++ b/tinyusb/osal/osal_freeRTOS.h @@ -103,28 +103,26 @@ static inline void osal_task_delay(uint32_t msec) typedef xSemaphoreHandle osal_semaphore_handle_t; // create FreeRTOS binary semaphore with zero as init value TODO: omit semaphore take from vSemaphoreCreateBinary API, should double checks this -#define osal_semaphore_create(x) \ - xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ) +#define osal_semaphore_create(x) xSemaphoreCreateBinary() // TODO add timeout (with instant return from ISR option) for semaphore post & queue send static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE; static inline tusb_error_t osal_semaphore_post(osal_semaphore_handle_t const sem_hdl) { - portBASE_TYPE task_waken; - return (xSemaphoreGiveFromISR(sem_hdl, &task_waken) == pdTRUE) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_SEMAPHORE_FAILED; + return (xSemaphoreGive(sem_hdl) == pdTRUE) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_SEMAPHORE_FAILED; } static inline void osal_semaphore_wait(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE; static inline void osal_semaphore_wait(osal_semaphore_handle_t const sem_hdl, uint32_t msec, tusb_error_t *p_error) { - (*p_error) = ( xSemaphoreTake(sem_hdl, osal_tick_from_msec(msec)) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; + uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec); + (*p_error) = ( xSemaphoreTake(sem_hdl, ticks) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; } static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl) ATTR_ALWAYS_INLINE; static inline void osal_semaphore_reset(osal_semaphore_handle_t const sem_hdl) { - portBASE_TYPE task_waken; - xSemaphoreTakeFromISR(sem_hdl, &task_waken); + (void) xSemaphoreTake(sem_hdl, 0); } //--------------------------------------------------------------------+ @@ -144,7 +142,8 @@ static inline tusb_error_t osal_mutex_release(osal_mutex_handle_t const mutex_h static inline void osal_mutex_wait(osal_mutex_handle_t const mutex_hdl, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE; static inline void osal_mutex_wait(osal_mutex_handle_t const mutex_hdl, uint32_t msec, tusb_error_t *p_error) { - (*p_error) = ( xSemaphoreTake(mutex_hdl, osal_tick_from_msec(msec)) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; + uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec); + (*p_error) = ( xSemaphoreTake(mutex_hdl, ticks) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; } static inline void osal_mutex_reset(osal_mutex_handle_t const mutex_hdl) ATTR_ALWAYS_INLINE; @@ -176,14 +175,14 @@ typedef xQueueHandle osal_queue_handle_t; static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) ATTR_ALWAYS_INLINE; static inline void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error) { - (*p_error) = ( xQueueReceive(queue_hdl, p_data, osal_tick_from_msec(msec)) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; + uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : osal_tick_from_msec(msec); + (*p_error) = ( xQueueReceive(queue_hdl, p_data, ticks) == pdPASS ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_TIMEOUT; } -static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_ALWAYS_INLINE; +static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE; static inline tusb_error_t osal_queue_send(osal_queue_handle_t const queue_hdl, void const * data) { - portBASE_TYPE taskWaken; - return ( xQueueSendFromISR(queue_hdl, data, &taskWaken) == pdTRUE ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_QUEUE_FAILED; + return ( xQueueSend(queue_hdl, data, 0) == pdTRUE ) ? TUSB_ERROR_NONE : TUSB_ERROR_OSAL_QUEUE_FAILED; } static inline void osal_queue_flush(osal_queue_handle_t const queue_hdl) ATTR_ALWAYS_INLINE; -- cgit v1.3.1