From 57c5c37f3d09c5fe71abe6fc23dd6621b1d157a2 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 11 Oct 2024 12:58:18 +0700 Subject: [PATCH] change tusb_init(), tusb_rhport_init() to use init struct for expandability --- .../device/audio_4_channel_mic/src/main.c | 7 +++- .../audio_4_channel_mic_freertos/src/main.c | 7 +++- examples/device/audio_test/src/main.c | 7 +++- .../device/audio_test_freertos/src/main.c | 7 +++- .../device/audio_test_multi_rate/src/main.c | 7 +++- examples/device/cdc_dual_ports/src/main.c | 7 +++- examples/device/cdc_msc/src/main.c | 7 +++- examples/device/cdc_msc_freertos/src/main.c | 7 +++- examples/device/cdc_uac2/src/main.c | 7 +++- examples/device/dfu/src/main.c | 7 +++- examples/device/dfu_runtime/src/main.c | 7 +++- .../device/dynamic_configuration/src/main.c | 7 +++- examples/device/hid_boot_interface/src/main.c | 7 +++- examples/device/hid_composite/src/main.c | 7 +++- .../device/hid_composite_freertos/src/main.c | 7 +++- examples/device/hid_generic_inout/src/main.c | 7 +++- .../device/hid_multiple_interface/src/main.c | 7 +++- examples/device/midi_test/src/main.c | 7 +++- examples/device/msc_dual_lun/src/main.c | 7 +++- examples/device/net_lwip_webserver/src/main.c | 7 +++- examples/device/uac2_headset/src/main.c | 7 +++- examples/device/uac2_speaker_fb/src/main.c | 7 +++- examples/device/usbtmc/src/main.c | 7 +++- examples/device/video_capture/src/main.c | 14 ++++++-- examples/device/video_capture_2ch/src/main.c | 14 ++++++-- examples/device/webusb_serial/src/main.c | 7 +++- examples/dual/host_hid_to_device_cdc/only.txt | 1 + .../dual/host_hid_to_device_cdc/src/main.c | 15 ++++++-- .../dual/host_info_to_device_cdc/only.txt | 1 + .../dual/host_info_to_device_cdc/src/main.c | 15 ++++++-- examples/host/bare_api/src/main.c | 7 +++- examples/host/cdc_msc_hid/src/main.c | 7 +++- examples/host/cdc_msc_hid_freertos/src/main.c | 8 ++++- examples/host/device_info/src/main.c | 7 +++- examples/host/hid_controller/src/main.c | 7 +++- examples/host/msc_file_explorer/src/main.c | 7 +++- src/common/tusb_common.h | 2 +- src/common/tusb_compiler.h | 8 +++++ src/common/tusb_types.h | 10 ++++++ src/common/tusb_verify.h | 3 -- src/device/usbd.c | 17 ++++++---- src/device/usbd.h | 15 +++++++- src/host/usbh.c | 16 +++++---- src/host/usbh.h | 22 +++++++++--- src/tusb.c | 34 ++++++++++++------- src/tusb.h | 16 ++++++--- .../test/device/msc/test_msc_device.c | 8 ++++- test/unit-test/test/device/usbd/test_usbd.c | 8 ++++- 48 files changed, 350 insertions(+), 80 deletions(-) diff --git a/examples/device/audio_4_channel_mic/src/main.c b/examples/device/audio_4_channel_mic/src/main.c index 6c75d95bfe..ca0e67faea 100644 --- a/examples/device/audio_4_channel_mic/src/main.c +++ b/examples/device/audio_4_channel_mic/src/main.c @@ -86,7 +86,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/audio_4_channel_mic_freertos/src/main.c b/examples/device/audio_4_channel_mic_freertos/src/main.c index d24113df60..c1ebc48993 100644 --- a/examples/device/audio_4_channel_mic_freertos/src/main.c +++ b/examples/device/audio_4_channel_mic_freertos/src/main.c @@ -209,7 +209,12 @@ void usb_device_task(void* param) // init device stack on configured roothub port // This should be called after scheduler/kernel is started. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/audio_test/src/main.c b/examples/device/audio_test/src/main.c index 9702060e0a..f4c1bf95fc 100644 --- a/examples/device/audio_test/src/main.c +++ b/examples/device/audio_test/src/main.c @@ -79,7 +79,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/audio_test_freertos/src/main.c b/examples/device/audio_test_freertos/src/main.c index 3d9166263e..b7d7dc0426 100644 --- a/examples/device/audio_test_freertos/src/main.c +++ b/examples/device/audio_test_freertos/src/main.c @@ -156,7 +156,12 @@ void usb_device_task(void* param) // init device stack on configured roothub port // This should be called after scheduler/kernel is started. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/audio_test_multi_rate/src/main.c b/examples/device/audio_test_multi_rate/src/main.c index 99b81f74df..a1c4d32dbe 100644 --- a/examples/device/audio_test_multi_rate/src/main.c +++ b/examples/device/audio_test_multi_rate/src/main.c @@ -97,7 +97,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index 07f41371a2..b446bca0b3 100644 --- a/examples/device/cdc_dual_ports/src/main.c +++ b/examples/device/cdc_dual_ports/src/main.c @@ -52,7 +52,12 @@ int main(void) { board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index a1f178e2a4..47ba5a8878 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -51,7 +51,12 @@ int main(void) { board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index b84b33a8cb..a08e063ac1 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -132,7 +132,12 @@ static void usb_device_task(void *param) { // init device stack on configured roothub port // This should be called after scheduler/kernel is started. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/cdc_uac2/src/main.c b/examples/device/cdc_uac2/src/main.c index a75ec14270..384d910ec0 100644 --- a/examples/device/cdc_uac2/src/main.c +++ b/examples/device/cdc_uac2/src/main.c @@ -46,7 +46,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); #if (CFG_TUSB_MCU == OPT_MCU_RP2040) stdio_init_all(); diff --git a/examples/device/dfu/src/main.c b/examples/device/dfu/src/main.c index 5efec9d322..f2aea7cfc7 100644 --- a/examples/device/dfu/src/main.c +++ b/examples/device/dfu/src/main.c @@ -75,7 +75,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/dfu_runtime/src/main.c b/examples/device/dfu_runtime/src/main.c index c21034fc1f..5d6b907b7a 100644 --- a/examples/device/dfu_runtime/src/main.c +++ b/examples/device/dfu_runtime/src/main.c @@ -70,7 +70,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/dynamic_configuration/src/main.c b/examples/device/dynamic_configuration/src/main.c index bc9ab0b2ba..143570c5c0 100644 --- a/examples/device/dynamic_configuration/src/main.c +++ b/examples/device/dynamic_configuration/src/main.c @@ -57,7 +57,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/hid_boot_interface/src/main.c b/examples/device/hid_boot_interface/src/main.c index 351a60854b..dfee7421d4 100644 --- a/examples/device/hid_boot_interface/src/main.c +++ b/examples/device/hid_boot_interface/src/main.c @@ -57,7 +57,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c index 8f7873ee30..56c485982a 100644 --- a/examples/device/hid_composite/src/main.c +++ b/examples/device/hid_composite/src/main.c @@ -58,7 +58,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/hid_composite_freertos/src/main.c b/examples/device/hid_composite_freertos/src/main.c index d1842368ba..fc07a1a39f 100644 --- a/examples/device/hid_composite_freertos/src/main.c +++ b/examples/device/hid_composite_freertos/src/main.c @@ -136,7 +136,12 @@ void usb_device_task(void* param) // init device stack on configured roothub port // This should be called after scheduler/kernel is started. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/hid_generic_inout/src/main.c b/examples/device/hid_generic_inout/src/main.c index 03de8bfee2..1bbcbf7a46 100644 --- a/examples/device/hid_generic_inout/src/main.c +++ b/examples/device/hid_generic_inout/src/main.c @@ -81,7 +81,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/hid_multiple_interface/src/main.c b/examples/device/hid_multiple_interface/src/main.c index 0b7544fe05..edcedd3caf 100644 --- a/examples/device/hid_multiple_interface/src/main.c +++ b/examples/device/hid_multiple_interface/src/main.c @@ -62,7 +62,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/midi_test/src/main.c b/examples/device/midi_test/src/main.c index 84ca1d9445..b05307d1b0 100644 --- a/examples/device/midi_test/src/main.c +++ b/examples/device/midi_test/src/main.c @@ -63,7 +63,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/msc_dual_lun/src/main.c b/examples/device/msc_dual_lun/src/main.c index 9b0930ce08..fe3896ca7b 100644 --- a/examples/device/msc_dual_lun/src/main.c +++ b/examples/device/msc_dual_lun/src/main.c @@ -54,7 +54,12 @@ int main(void) { board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/net_lwip_webserver/src/main.c b/examples/device/net_lwip_webserver/src/main.c index dbd7746b0b..6041b56e27 100644 --- a/examples/device/net_lwip_webserver/src/main.c +++ b/examples/device/net_lwip_webserver/src/main.c @@ -216,7 +216,12 @@ int main(void) { board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/uac2_headset/src/main.c b/examples/device/uac2_headset/src/main.c index 73616109fc..4d28f73a4c 100644 --- a/examples/device/uac2_headset/src/main.c +++ b/examples/device/uac2_headset/src/main.c @@ -100,7 +100,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/uac2_speaker_fb/src/main.c b/examples/device/uac2_speaker_fb/src/main.c index 21c2ac24ae..1250b041d1 100644 --- a/examples/device/uac2_speaker_fb/src/main.c +++ b/examples/device/uac2_speaker_fb/src/main.c @@ -106,7 +106,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/usbtmc/src/main.c b/examples/device/usbtmc/src/main.c index 8dbded6322..2af581bc18 100644 --- a/examples/device/usbtmc/src/main.c +++ b/examples/device/usbtmc/src/main.c @@ -55,7 +55,12 @@ int main(void) board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/video_capture/src/main.c b/examples/device/video_capture/src/main.c index 4b8877a2fe..c5d87c660a 100644 --- a/examples/device/video_capture/src/main.c +++ b/examples/device/video_capture/src/main.c @@ -68,7 +68,12 @@ int main(void) { freertos_init_task(); #else // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); @@ -319,7 +324,12 @@ void usb_device_task(void *param) { // init device stack on configured roothub port // This should be called after scheduler/kernel is started. // Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API. - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/video_capture_2ch/src/main.c b/examples/device/video_capture_2ch/src/main.c index 118ee669d9..a45e62ab0d 100644 --- a/examples/device/video_capture_2ch/src/main.c +++ b/examples/device/video_capture_2ch/src/main.c @@ -68,7 +68,12 @@ int main(void) { freertos_init_task(); #else // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); @@ -327,7 +332,12 @@ void usb_device_task(void *param) { // init device stack on configured roothub port // This should be called after scheduler/kernel is started. // Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API. - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c index e809c6319f..46215ec138 100644 --- a/examples/device/webusb_serial/src/main.c +++ b/examples/device/webusb_serial/src/main.c @@ -91,7 +91,12 @@ int main(void) { board_init(); // init device stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/dual/host_hid_to_device_cdc/only.txt b/examples/dual/host_hid_to_device_cdc/only.txt index cfc87eb4ec..149ece8565 100644 --- a/examples/dual/host_hid_to_device_cdc/only.txt +++ b/examples/dual/host_hid_to_device_cdc/only.txt @@ -4,3 +4,4 @@ board:mcb1800 mcu:RP2040 mcu:ra6m5 mcu:MAX3421 +mcu:STM32H7 diff --git a/examples/dual/host_hid_to_device_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c index ce586201c1..061958aae9 100644 --- a/examples/dual/host_hid_to_device_cdc/src/main.c +++ b/examples/dual/host_hid_to_device_cdc/src/main.c @@ -79,8 +79,19 @@ int main(void) { printf("TinyUSB Host HID <-> Device CDC Example\r\n"); // init device and host stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); + + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/dual/host_info_to_device_cdc/only.txt b/examples/dual/host_info_to_device_cdc/only.txt index cfc87eb4ec..149ece8565 100644 --- a/examples/dual/host_info_to_device_cdc/only.txt +++ b/examples/dual/host_info_to_device_cdc/only.txt @@ -4,3 +4,4 @@ board:mcb1800 mcu:RP2040 mcu:ra6m5 mcu:MAX3421 +mcu:STM32H7 diff --git a/examples/dual/host_info_to_device_cdc/src/main.c b/examples/dual/host_info_to_device_cdc/src/main.c index 7a862f5ada..e4d802a0cc 100644 --- a/examples/dual/host_info_to_device_cdc/src/main.c +++ b/examples/dual/host_info_to_device_cdc/src/main.c @@ -82,8 +82,19 @@ int main(void) { printf("TinyUSB Host Information -> Device CDC Example\r\n"); // init device and host stack on configured roothub port - tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE); - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t dev_init = { + .rhport = BOARD_TUD_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); + + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/host/bare_api/src/main.c b/examples/host/bare_api/src/main.c index f248dd28d4..e710e3caab 100644 --- a/examples/host/bare_api/src/main.c +++ b/examples/host/bare_api/src/main.c @@ -62,7 +62,12 @@ int main(void) printf("TinyUSB Bare API Example\r\n"); // init host stack on configured roothub port - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 65f7b5201d..3a6e762b7c 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -50,7 +50,12 @@ int main(void) { printf("TinyUSB Host CDC MSC HID Example\r\n"); // init host stack on configured roothub port - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/host/cdc_msc_hid_freertos/src/main.c b/examples/host/cdc_msc_hid_freertos/src/main.c index 2116e6dcf7..9d2f347c39 100644 --- a/examples/host/cdc_msc_hid_freertos/src/main.c +++ b/examples/host/cdc_msc_hid_freertos/src/main.c @@ -126,7 +126,13 @@ static void usb_host_task(void *param) { (void) param; // init host stack on configured roothub port - if (!tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST)) { + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + + if (!tusb_init(&host_init)) { printf("Failed to init USB Host Stack\r\n"); vTaskSuspend(NULL); } diff --git a/examples/host/device_info/src/main.c b/examples/host/device_info/src/main.c index 55d685a5d6..8ca3d8a3a5 100644 --- a/examples/host/device_info/src/main.c +++ b/examples/host/device_info/src/main.c @@ -66,7 +66,12 @@ int main(void) { printf("TinyUSB Device Info Example\r\n"); // init host stack on configured roothub port - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/host/hid_controller/src/main.c b/examples/host/hid_controller/src/main.c index 76b6866837..6db47792ca 100644 --- a/examples/host/hid_controller/src/main.c +++ b/examples/host/hid_controller/src/main.c @@ -52,7 +52,12 @@ int main(void) printf("Note: Events only displayed for explicit supported controllers\r\n"); // init host stack on configured roothub port - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/examples/host/msc_file_explorer/src/main.c b/examples/host/msc_file_explorer/src/main.c index 3af079b0ee..11512ab65e 100644 --- a/examples/host/msc_file_explorer/src/main.c +++ b/examples/host/msc_file_explorer/src/main.c @@ -78,7 +78,12 @@ int main(void) { printf("TinyUSB Host MassStorage Explorer Example\r\n"); // init host stack on configured roothub port - tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST); + tusb_rhport_init_t host_init = { + .rhport = BOARD_TUH_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&host_init); if (board_init_after_tusb) { board_init_after_tusb(); diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 0d4082c031..d3e0cf888b 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -79,7 +79,7 @@ //--------------------------------------------------------------------+ // Optional API implemented by application if needed -// TODO move to a more ovious place/file +// TODO move to a more obvious place/file //--------------------------------------------------------------------+ // flush data cache diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h index ce5566ffe3..646c22b295 100644 --- a/src/common/tusb_compiler.h +++ b/src/common/tusb_compiler.h @@ -118,6 +118,14 @@ #define _TU_ARGS_APPLY_7(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7) _X(_a1) _s _TU_ARGS_APPLY_6(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7) #define _TU_ARGS_APPLY_8(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) _X(_a1) _s _TU_ARGS_APPLY_7(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7, _a8) +//--------------------------------------------------------------------+ +// Macro for function default arguments +//--------------------------------------------------------------------+ +#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 + +// function expand with number of arguments +#define TU_FUNC_OPTIONAL_ARG(func, ...) TU_XSTRCAT(func##_arg, TU_ARGS_NUM(__VA_ARGS__))(__VA_ARGS__) + //--------------------------------------------------------------------+ // Compiler porting with Attribute and Endian //--------------------------------------------------------------------+ diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index adf4b57204..5dfe3ff98c 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -50,6 +50,7 @@ typedef enum { TUSB_SPEED_FULL = 0, TUSB_SPEED_LOW = 1, TUSB_SPEED_HIGH = 2, + TUSB_SPEED_AUTO = 0xaa, TUSB_SPEED_INVALID = 0xff, } tusb_speed_t; @@ -273,6 +274,15 @@ enum { TUSB_INDEX_INVALID_8 = 0xFFu }; +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ +typedef struct { + uint8_t rhport; + tusb_role_t role; + tusb_speed_t speed; +} tusb_rhport_init_t; + //--------------------------------------------------------------------+ // USB Descriptors //--------------------------------------------------------------------+ diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h index 8ad218b033..3e0f1f1068 100644 --- a/src/common/tusb_verify.h +++ b/src/common/tusb_verify.h @@ -93,9 +93,6 @@ #define TU_BREAKPOINT() do {} while (0) #endif -// Helper to implement optional parameter for TU_VERIFY Macro family -#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 - /*------------------------------------------------------------------*/ /* TU_VERIFY * - TU_VERIFY_1ARGS : return false if failed diff --git a/src/device/usbd.c b/src/device/usbd.c index 6fe7e416e7..7e35c976c0 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -448,11 +448,14 @@ bool tud_inited(void) { return _usbd_rhport != RHPORT_INVALID; } -bool tud_init(uint8_t rhport) { - // skip if already initialized - if (tud_inited()) return true; +bool tud_rhport_init(const tusb_rhport_init_t* rh_init) { + if (tud_inited()) { + return true; // skip if already initialized + } + TU_ASSERT(rh_init); - TU_LOG_USBD("USBD init on controller %u, Highspeed = %u\r\n", rhport, TUD_OPT_HIGH_SPEED); + TU_LOG_USBD("USBD init on controller %u, speed = %s\r\n", rh_init->rhport, + rh_init->speed == TUSB_SPEED_HIGH ? "High" : "Full"); TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(usbd_device_t)); TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(dcd_event_t)); TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(tu_fifo_t)); @@ -484,11 +487,11 @@ bool tud_init(uint8_t rhport) { driver->init(); } - _usbd_rhport = rhport; + _usbd_rhport = rh_init->rhport; // Init device controller driver - dcd_init(rhport); - dcd_int_enable(rhport); + dcd_init(rh_init->rhport); + dcd_int_enable(rh_init->rhport); return true; } diff --git a/src/device/usbd.h b/src/device/usbd.h index 7913096e39..05eb0173f6 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -37,8 +37,21 @@ extern "C" { // Application API //--------------------------------------------------------------------+ +// New API to replace tud_init() to init device stack on specific roothub port +bool tud_rhport_init(const tusb_rhport_init_t* rh_init); + // Init device stack on roothub port -bool tud_init (uint8_t rhport); +#if TUSB_VERSION_NUMBER > 2000 // 0.20.0 +TU_ATTR_DEPRECATED("Please use tusb_init(tusb_rhport_init_t*) instead") +#endif +TU_ATTR_ALWAYS_INLINE static inline bool tud_init (uint8_t rhport) { + const tusb_rhport_init_t rh_init = { + .rhport = rhport, + .role = TUSB_ROLE_DEVICE, + .speed = TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL + }; + return tud_rhport_init(&rh_init); +} // Deinit device stack on roothub port bool tud_deinit(uint8_t rhport); diff --git a/src/host/usbh.c b/src/host/usbh.c index ed253ffcc4..39bc8d63a7 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -352,11 +352,13 @@ bool tuh_inited(void) { return _usbh_controller != TUSB_INDEX_INVALID_8; } -bool tuh_init(uint8_t rhport) { - // skip if already initialized - if (tuh_rhport_is_active(rhport)) return true; +bool tuh_rhport_init(const tusb_rhport_init_t* rh_init) { + if (tuh_rhport_is_active(rh_init->rhport)) { + return true; // skip if already initialized + } - TU_LOG_USBH("USBH init on controller %u\r\n", rhport); + TU_LOG_USBH("USBH init on controller %u, speed = %s\r\n", rhport, + rh_init->speed == TUSB_SPEED_HIGH ? "High" : "Full"); // Init host stack if not already if (!tuh_inited()) { @@ -402,9 +404,9 @@ bool tuh_init(uint8_t rhport) { } // Init host controller - _usbh_controller = rhport;; - TU_ASSERT(hcd_init(rhport)); - hcd_int_enable(rhport); + _usbh_controller = rh_init->rhport; + TU_ASSERT(hcd_init(rh_init->rhport)); + hcd_int_enable(rh_init->rhport); return true; } diff --git a/src/host/usbh.h b/src/host/usbh.h index 217cb1a9c6..9b865ac1b5 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -120,8 +120,21 @@ void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr); // - cfg_param: configure data, structure depends on the ID bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param); +// New API to replace tuh_init() to init host stack on specific roothub port +bool tuh_rhport_init(const tusb_rhport_init_t* rh_init); + // Init host stack -bool tuh_init(uint8_t rhport); +#if TUSB_VERSION_NUMBER > 2000 // 0.20.0 +TU_ATTR_DEPRECATED("Please use tusb_init(tusb_rhport_init_t*) instead") +#endif +TU_ATTR_ALWAYS_INLINE static inline bool tuh_init(uint8_t rhport) { + const tusb_rhport_init_t rh_init = { + .rhport = rhport, + .role = TUSB_ROLE_HOST, + .speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL, + }; + return tuh_rhport_init(&rh_init); +} // Deinit host stack on rhport bool tuh_deinit(uint8_t rhport); @@ -149,9 +162,10 @@ extern void hcd_int_handler(uint8_t rhport, bool in_isr); #endif // Interrupt handler alias to HCD with in_isr as optional parameter -#define _tuh_int_handler_1arg(_rhport) hcd_int_handler(_rhport, true) -#define _tuh_int_hanlder_2arg(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr) -#define tuh_int_handler(...) TU_GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__) +#define _tuh_int_handler_arg0() TU_VERIFY_STATIC(false, "tuh_int_handler() must have 1 or 2 arguments") +#define _tuh_int_handler_arg1(_rhport) hcd_int_handler(_rhport, true) +#define _tuh_int_handler_arg2(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr) +#define tuh_int_handler(...) TU_FUNC_OPTIONAL_ARG(_tuh_int_handler, __VA_ARGS__) // Check if roothub port is initialized and active as a host bool tuh_rhport_is_active(uint8_t rhport); diff --git a/src/tusb.c b/src/tusb.c index 9f0e45fef9..3eb299dff4 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -47,19 +47,29 @@ static tusb_role_t _rhport_role[TUP_USBIP_CONTROLLER_NUM] = { 0 }; // Public API //--------------------------------------------------------------------+ -bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role) { +bool tusb_rhport_init(const tusb_rhport_init_t* rh_init) { // backward compatible called with tusb_init(void) #if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT) - if (rhport == 0xff || role == TUSB_ROLE_INVALID) { + if (rh_init == NULL) { #if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) // init device stack CFG_TUSB_RHPORTx_MODE must be defined - TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); + const tusb_rhport_init_t dev_init = { + .rhport = TUD_OPT_RHPORT, + .role = TUSB_ROLE_DEVICE, + .speed = TUD_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL + }; + TU_ASSERT ( tud_rhport_init(&dev_init) ); _rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_DEVICE; #endif #if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) // init host stack CFG_TUSB_RHPORTx_MODE must be defined - TU_ASSERT( tuh_init(TUH_OPT_RHPORT) ); + const tusb_rhport_init_t host_init = { + .rhport = TUH_OPT_RHPORT, + .role = TUSB_ROLE_HOST, + .speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL + }; + TU_ASSERT( tuh_rhport_init(&host_init) ); _rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_HOST; #endif @@ -68,21 +78,21 @@ bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role) { #endif // new API with explicit rhport and role - TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM && role != TUSB_ROLE_INVALID); + TU_ASSERT(rh_init->rhport < TUP_USBIP_CONTROLLER_NUM && rh_init->role != TUSB_ROLE_INVALID); #if CFG_TUD_ENABLED - if (role == TUSB_ROLE_DEVICE) { - TU_ASSERT( tud_init(rhport) ); + if (rh_init->role == TUSB_ROLE_DEVICE) { + TU_ASSERT( tud_rhport_init(rh_init) ); } #endif #if CFG_TUH_ENABLED - if (role == TUSB_ROLE_HOST) { - TU_ASSERT( tuh_init(rhport) ); + if (rh_init->role == TUSB_ROLE_HOST) { + TU_ASSERT( tuh_rhport_init(rh_init) ); } #endif - _rhport_role[rhport] = role; + _rhport_role[rh_init->rhport] = rh_init->role; return true; } @@ -106,13 +116,13 @@ void tusb_int_handler(uint8_t rhport, bool in_isr) { #if CFG_TUD_ENABLED if (_rhport_role[rhport] == TUSB_ROLE_DEVICE) { (void) in_isr; - tud_int_handler(rhport); + dcd_int_handler(rhport); } #endif #if CFG_TUH_ENABLED if (_rhport_role[rhport] == TUSB_ROLE_HOST) { - tuh_int_handler(rhport, in_isr); + hcd_int_handler(rhport, in_isr); } #endif } diff --git a/src/tusb.h b/src/tusb.h index 911d1bae02..c12abba8b8 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -129,19 +129,25 @@ //--------------------------------------------------------------------+ // APPLICATION API //--------------------------------------------------------------------+ + + #if CFG_TUH_ENABLED || CFG_TUD_ENABLED // Internal helper for backward compatible with tusb_init(void) -bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role); +bool tusb_rhport_init(const tusb_rhport_init_t* rh_init); // Initialize roothub port with device/host role // Note: when using with RTOS, this should be called after scheduler/kernel is started. // Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API. // Note2: defined as macro for backward compatible with tusb_init(void), can be changed to function in the future. -#define _tusb_init_0arg() _tusb_rhport_init(0xff, TUSB_ROLE_INVALID) -#define _tusb_init_1arg(_rhport) _tusb_rhport_init(_rhport, TUSB_ROLE_INVALID) -#define _tusb_init_2arg(_rhport, _role) _tusb_rhport_init(_rhport, _role) -#define tusb_init(...) TU_GET_3RD_ARG(__VA_ARGS__, _tusb_init_2arg, _tusb_init_1arg, _tusb_init_0arg)(__VA_ARGS__) +#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT) +#define _tusb_init_arg0() tusb_rhport_init(NULL) +#else +#define _tusb_init_arg0() TU_VERIFY_STATIC(false, "CFG_TUSB_RHPORT0_MODE/CFG_TUSB_RHPORT1_MODE must be defined") +#endif + +#define _tusb_init_arg1(_rh_init) tusb_rhport_init(_rh_init) +#define tusb_init(...) TU_FUNC_OPTIONAL_ARG(_tusb_init, __VA_ARGS__) // Check if stack is initialized bool tusb_inited(void); diff --git a/test/unit-test/test/device/msc/test_msc_device.c b/test/unit-test/test/device/msc/test_msc_device.c index 28216af107..3444d81c5b 100644 --- a/test/unit-test/test/device/msc/test_msc_device.c +++ b/test/unit-test/test/device/msc/test_msc_device.c @@ -200,7 +200,13 @@ void setUp(void) if ( !tud_inited() ) { dcd_init_Expect(rhport); - tusb_init(0, TUSB_ROLE_DEVICE); + + tusb_rhport_init_t dev_init = { + .rhport = 0, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); } dcd_event_bus_reset(rhport, TUSB_SPEED_HIGH, false); diff --git a/test/unit-test/test/device/usbd/test_usbd.c b/test/unit-test/test/device/usbd/test_usbd.c index 43722d1759..faa6b159ee 100644 --- a/test/unit-test/test/device/usbd/test_usbd.c +++ b/test/unit-test/test/device/usbd/test_usbd.c @@ -128,7 +128,13 @@ void setUp(void) { mscd_init_Expect(); dcd_init_Expect(rhport); - tusb_init(0, TUSB_ROLE_DEVICE); + + tusb_rhport_init_t dev_init = { + .rhport = 0, + .role = TUSB_ROLE_DEVICE, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(&dev_init); } }