From 1c49c479cabf1be9b1c22017c42a8480b26cebcb Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 13 Dec 2018 14:51:37 +0700 Subject: [PATCH] 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 +- .../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(-) diff --git a/docs/getting_started.md b/docs/getting_started.md index 268811ff..723e6d86 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 040112d9..f5af8280 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 178609e1..a9d1f161 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 843b2b07..946972c1 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 ebb54078..f1139974 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 bd49c157..70aa7370 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 aa81cb11..a3fbfdff 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 f9e0d8f4..f9e7368c 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 d1fccd9e..06951673 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 27d32330..d0b5edb6 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 8009c508..fee1235f 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 253adc59..ad2dcbfd 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 e4393d06..3a13bbec 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 +// backward compatible only. TODO remove later +static inline void tusb_task(void) +{ + #if TUSB_OPT_HOST_ENABLED + tuh_task(); + #endif - while(1) // the mainloop - { - your_application_code(); - - tusb_task(); // handle tinyusb event, task etc ... - } - } - @endcode - - */ -void tusb_task(void); + #if TUSB_OPT_DEVICE_ENABLED + tud_task(); + #endif +} /** @} */