From 177adf4bfa77f48d0266f08f32449a37a9dc70bd Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 23 Oct 2018 16:07:48 +0700 Subject: [PATCH] replace dcd_bus_event() and dcd_setup_received() by dcd_event_handler() --- src/device/usbd.c | 69 +++---------------- src/device/usbd_pvt.h | 2 +- src/portable/nordic/nrf5x/dcd_nrf5x.c | 14 +++- .../nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c | 20 ++++-- src/portable/nxp/lpc17xx/dcd_lpc175x_6x.c | 29 +++++--- .../nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c | 20 ++++-- 6 files changed, 71 insertions(+), 83 deletions(-) diff --git a/src/device/usbd.c b/src/device/usbd.c index a5f30aab3..58581d609 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -532,55 +532,6 @@ 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) -{ - switch(bus_event) - { - case USBD_BUS_EVENT_RESET: - usbd_reset(rhport); - - osal_queue_flush(_usbd_q); - osal_semaphore_reset_isr(_usbd_ctrl_sem); - break; - - case USBD_BUS_EVENT_SOF: - { - #if 0 - dcd_event_t task_event = - { - .rhport = rhport, - .event_id = DCD_EVENT_SOF, - }; - osal_queue_send_isr(_usbd_q, &task_event); - #endif - } - break; - - case USBD_BUS_EVENT_UNPLUGGED: - usbd_reset(rhport); - tud_umount_cb(); // invoke callback - break; - - case USBD_BUS_EVENT_SUSPENDED: - // TODO support suspended - break; - - default: break; - } -} - -void dcd_setup_received(uint8_t rhport, uint8_t const* p_request) -{ - dcd_event_t task_event = - { - .rhport = rhport, - .event_id = DCD_EVENT_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 ) @@ -590,7 +541,7 @@ void dcd_xfer_complete(uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, (void) succeeded; // only signal data stage, skip status (zero byte) - if (xferred_bytes) osal_semaphore_post_isr( _usbd_ctrl_sem ); + if (xferred_bytes) osal_semaphore_post( _usbd_ctrl_sem, true); }else { dcd_event_t event = @@ -603,7 +554,7 @@ void dcd_xfer_complete(uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, event.xfer_complete.len = xferred_bytes; event.xfer_complete.result = succeeded ? TUSB_EVENT_XFER_COMPLETE : TUSB_EVENT_XFER_ERROR; - osal_queue_send_isr(_usbd_q, &event); + osal_queue_send(_usbd_q, &event, true); } TU_ASSERT(succeeded, ); @@ -630,7 +581,7 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) .rhport = rhport, .event_id = DCD_EVENT_SOF, }; - osal_queue_send_isr(_usbd_q, &task_event); + osal_queue_send(_usbd_q, &task_event, true); #endif } break; @@ -648,7 +599,11 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) // TODO support resume break; + case DCD_EVENT_SETUP_RECEIVED: + osal_queue_send(_usbd_q, event, in_isr); + break; + default: break; } } @@ -678,7 +633,7 @@ 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 ) { dcd_event_t event = { @@ -689,13 +644,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/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 55d1867d7..d7e014cb1 100644 --- a/src/device/usbd_pvt.h +++ b/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/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c index 371c291a3..f04acb6e3 100644 --- a/src/portable/nordic/nrf5x/dcd_nrf5x.c +++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c @@ -414,11 +414,15 @@ void USBD_IRQHandler(void) } } + dcd_event_t event = { .rhport = 0 }; + /*------------- Interrupt Processing -------------*/ if ( int_status & USBD_INTEN_USBRESET_Msk ) { bus_reset(); - dcd_bus_event(0, USBD_BUS_EVENT_RESET); + + event.event_id = DCD_EVENT_BUS_RESET; + dcd_event_handler(&event, true); } if ( int_status & EDPT_END_ALL_MASK ) @@ -435,7 +439,10 @@ void USBD_IRQHandler(void) NRF_USBD->WINDEXL , NRF_USBD->WINDEXH , NRF_USBD->WLENGTHL, NRF_USBD->WLENGTHH }; - dcd_setup_received(0, setup); + event.event_id = DCD_EVENT_SETUP_RECEIVED; + memcpy(&event.setup_received, setup, 8); + + dcd_event_handler(&event, true); } if ( int_status & USBD_INTEN_EP0DATADONE_Msk ) @@ -556,7 +563,8 @@ void USBD_IRQHandler(void) // SOF interrupt if ( int_status & USBD_INTEN_SOF_Msk ) { - dcd_bus_event(0, USBD_BUS_EVENT_SOF); + event.event_id = DCD_EVENT_SOF; + dcd_event_handler(&event, true); } } diff --git a/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c b/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c index 8e5a4d1aa..5c64cbb6b 100644 --- a/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c +++ b/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c @@ -309,6 +309,8 @@ void hal_dcd_isr(uint8_t rhport) uint32_t const dev_cmd_stat = LPC_USB->DEVCMDSTAT; + dcd_event_t event = { .rhport = rhport }; + //------------- Device Status -------------// if ( int_status & INT_MASK_DEVICE_STATUS ) { @@ -316,14 +318,17 @@ void hal_dcd_isr(uint8_t rhport) if ( dev_cmd_stat & CMDSTAT_RESET_CHANGE_MASK) // bus reset { bus_reset(); - dcd_bus_event(0, USBD_BUS_EVENT_RESET); + + event.event_id = DCD_EVENT_BUS_RESET; + dcd_event_handler(&event, true); } if (dev_cmd_stat & CMDSTAT_CONNECT_CHANGE_MASK) { // device disconnect if (dev_cmd_stat & CMDSTAT_DEVICE_ADDR_MASK) { // debouncing as this can be set when device is powering - dcd_bus_event(0, USBD_BUS_EVENT_UNPLUGGED); + event.event_id = DCD_EVENT_UNPLUGGED; + dcd_event_handler(&event, true); } } @@ -335,13 +340,15 @@ void hal_dcd_isr(uint8_t rhport) // Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration. if (dev_cmd_stat & CMDSTAT_DEVICE_ADDR_MASK) { - dcd_bus_event(0, USBD_BUS_EVENT_SUSPENDED); + event.event_id = DCD_EVENT_SUSPENDED; + dcd_event_handler(&event, true); } } } // else // { // resume signal -// dcd_bus_event(0, USBD_BUS_EVENT_RESUME); +// event.event_id = DCD_EVENT_RESUME; +// dcd_event_handler(&event, true); // } // } } @@ -350,7 +357,10 @@ void hal_dcd_isr(uint8_t rhport) if ( BIT_TEST_(int_status, 0) && (dev_cmd_stat & CMDSTAT_SETUP_RECEIVED_MASK) ) { // received control request from host // copy setup request & acknowledge so that the next setup can be received by hw - dcd_setup_received(rhport, (uint8_t*)&dcd_data.setup_request); + event.event_id = DCD_EVENT_SETUP_RECEIVED; + event.setup_received = dcd_data.setup_request; + + dcd_event_handler(&event, true); // NXP control flowchart clear Active & Stall on both Control IN/OUT endpoints dcd_data.qhd[0][0].stall = dcd_data.qhd[1][0].stall = 0; diff --git a/src/portable/nxp/lpc17xx/dcd_lpc175x_6x.c b/src/portable/nxp/lpc17xx/dcd_lpc175x_6x.c index 189521d25..850e257c6 100644 --- a/src/portable/nxp/lpc17xx/dcd_lpc175x_6x.c +++ b/src/portable/nxp/lpc17xx/dcd_lpc175x_6x.c @@ -182,15 +182,18 @@ static void endpoint_control_isr(void) uint32_t const endpoint_int_status = LPC_USB->USBEpIntSt & interrupt_enable; // LPC_USB->USBEpIntClr = endpoint_int_status; // acknowledge interrupt TODO cannot immediately acknowledge setup packet + dcd_event_t event = { .rhport = 0 }; + //------------- Setup Recieved-------------// if ( (endpoint_int_status & BIT_(0)) && (sie_read(SIE_CMDCODE_ENDPOINT_SELECT+0, 1) & SIE_SELECT_ENDPOINT_SETUP_RECEIVED_MASK) ) { (void) sie_read(SIE_CMDCODE_ENDPOINT_SELECT_CLEAR_INTERRUPT+0, 1); // clear setup bit - tusb_control_request_t control_request; - pipe_control_read(&control_request, 8); // TODO read before clear setup above - dcd_setup_received(0, (uint8_t*) &control_request); + event.event_id = DCD_EVENT_SETUP_RECEIVED; + pipe_control_read(&event.setup_received, 8); // TODO read before clear setup above + + dcd_event_handler(&event, true); } else if (endpoint_int_status & 0x03) { @@ -225,6 +228,8 @@ void hal_dcd_isr(uint8_t rhport) uint32_t const device_int_status = LPC_USB->USBDevIntSt & device_int_enable; LPC_USB->USBDevIntClr = device_int_status;// Acknowledge handled interrupt + dcd_event_t event = { .rhport = rhport }; + //------------- usb bus event -------------// if (device_int_status & DEV_INT_DEVICE_STATUS_MASK) { @@ -232,24 +237,30 @@ void hal_dcd_isr(uint8_t rhport) if (dev_status_reg & SIE_DEV_STATUS_RESET_MASK) { bus_reset(); - dcd_bus_event(0, USBD_BUS_EVENT_RESET); + + event.event_id = DCD_EVENT_BUS_RESET; + dcd_event_handler(&event, true); } if (dev_status_reg & SIE_DEV_STATUS_CONNECT_CHANGE_MASK) { // device is disconnected, require using VBUS (P1_30) - dcd_bus_event(0, USBD_BUS_EVENT_UNPLUGGED); + event.event_id = DCD_EVENT_UNPLUGGED; + dcd_event_handler(&event, true); } if (dev_status_reg & SIE_DEV_STATUS_SUSPEND_CHANGE_MASK) { if (dev_status_reg & SIE_DEV_STATUS_SUSPEND_MASK) { - dcd_bus_event(0, USBD_BUS_EVENT_SUSPENDED); + event.event_id = DCD_EVENT_SUSPENDED; + dcd_event_handler(&event, true); } -// else -// { -// dcd_bus_event(0, USBD_BUS_EVENT_RESUME); +// else +// { // resume signal +// event.event_id = DCD_EVENT_RESUME; +// dcd_event_handler(&event, true); // } +// } } } diff --git a/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c b/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c index 5a7d61004..6e1d96168 100644 --- a/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c +++ b/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c @@ -418,10 +418,14 @@ void hal_dcd_isr(uint8_t rhport) if (int_status == 0) return;// disabled interrupt sources + dcd_event_t event = { .rhport = rhport }; + if (int_status & INT_MASK_RESET) { bus_reset(rhport); - dcd_bus_event(rhport, USBD_BUS_EVENT_RESET); + + event.event_id = DCD_EVENT_BUS_RESET; + dcd_event_handler(&event, true); } if (int_status & INT_MASK_SUSPEND) @@ -430,7 +434,8 @@ void hal_dcd_isr(uint8_t rhport) { // Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration. if ((lpc_usb->DEVICEADDR >> 25) & 0x0f) { - dcd_bus_event(0, USBD_BUS_EVENT_SUSPENDED); + event.event_id = DCD_EVENT_SUSPENDED; + dcd_event_handler(&event, true); } } } @@ -440,7 +445,8 @@ void hal_dcd_isr(uint8_t rhport) // { // if ( !(lpc_usb->PORTSC1_D & PORTSC_CURRENT_CONNECT_STATUS_MASK) ) // { -// dcd_bus_event(0, USBD_BUS_EVENT_UNPLUGGED); +// event.event_id = DCD_EVENT_UNPLUGGED; +// dcd_event_handler(&event, true); // } // } @@ -457,7 +463,10 @@ void hal_dcd_isr(uint8_t rhport) // 23.10.10.2 Operational model for setup transfers lpc_usb->ENDPTSETUPSTAT = lpc_usb->ENDPTSETUPSTAT;// acknowledge - dcd_setup_received(rhport, (uint8_t*) &p_dcd->qhd[0].setup_request); + event.event_id = DCD_EVENT_SETUP_RECEIVED; + event.setup_received = p_dcd->qhd[0].setup_request; + + dcd_event_handler(&event, true); } //------------- Control Request Completed -------------// @@ -487,7 +496,8 @@ void hal_dcd_isr(uint8_t rhport) if (int_status & INT_MASK_SOF) { - dcd_bus_event(rhport, USBD_BUS_EVENT_SOF); + event.event_id = DCD_EVENT_SOF; + dcd_event_handler(&event, true); } if (int_status & INT_MASK_NAK) {}