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 --- src/device/usbd.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'src/device') 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 -- 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