seperate tusb_task() to tud_task() and tuh_task()

tusb_task() still exists for backward compatible
This commit is contained in:
hathach 2018-12-13 14:51:37 +07:00
parent af1ffe4675
commit 1c49c479ca
13 changed files with 64 additions and 49 deletions

View File

@ -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
}
}
~~~

View File

@ -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/<vendor>/<chip family>/dcd_<chip family>.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.

View File

@ -60,7 +60,8 @@ int main(void)
while (1)
{
tusb_task();
// tinyusb device task
tud_task();
led_blinking_task();

View File

@ -101,7 +101,8 @@ void usb_device_task(void* param)
// RTOS forever loop
while (1)
{
tusb_task();
// tinyusb device task
tud_task();
}
}

View File

@ -62,7 +62,8 @@ int main(void)
while (1)
{
tusb_task();
// tinyusb host task
tuh_task();
led_blinking_task();

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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);

View File

@ -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
*------------------------------------------------------------------*/

View File

@ -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
}
/** @} */