Skip to content

Commit 7c82783

Browse files
committed
update all host examples
1 parent 9794a2b commit 7c82783

File tree

9 files changed

+129
-91
lines changed

9 files changed

+129
-91
lines changed

examples/dual/host_hid_to_device_cdc/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ int main(void)
7979

8080
printf("TinyUSB Host HID <-> Device CDC Example\r\n");
8181

82-
tusb_init();
82+
// init device and host stack on configured roothub port
83+
tud_init(BOARD_TUD_RHPORT);
84+
tuh_init(BOARD_TUH_RHPORT);
8385

8486
while (1)
8587
{

examples/dual/host_hid_to_device_cdc/src/tusb_config.h

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,75 +30,61 @@
3030
extern "C" {
3131
#endif
3232

33-
//--------------------------------------------------------------------
34-
// COMMON CONFIGURATION
35-
//--------------------------------------------------------------------
36-
37-
// defined by board.mk
38-
#ifndef CFG_TUSB_MCU
39-
#error CFG_TUSB_MCU must be defined
40-
#endif
33+
//--------------------------------------------------------------------+
34+
// Board Specific Configuration
35+
//--------------------------------------------------------------------+
4136

4237
// RHPort number used for device can be defined by board.mk, default to port 0
4338
#ifndef BOARD_TUD_RHPORT
44-
#define BOARD_TUD_RHPORT 0
39+
#define BOARD_TUD_RHPORT 0
4540
#endif
4641

47-
// RHPort number used for device can be defined by board.mk, default to port 1
48-
#ifndef BOARD_TUH_RHPORT
49-
#define BOARD_TUH_RHPORT 1
50-
#endif
51-
52-
// Use raspberry pio-usb for host
53-
#define CFG_TUH_RPI_PIO_USB 1
54-
5542
// RHPort max operational speed can defined by board.mk
56-
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
5743
#ifndef BOARD_TUD_MAX_SPEED
58-
#if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX, OPT_MCU_MIMXRT10XX, OPT_MCU_NUC505) ||\
59-
TU_CHECK_MCU(OPT_MCU_CXD56, OPT_MCU_SAMX7X, OPT_MCU_BCM2711) ||\
60-
TU_CHECK_MCU(OPT_MCU_FT90X, OPT_MCU_FT93X)
61-
#define BOARD_TUD_MAX_SPEED OPT_MODE_HIGH_SPEED
62-
#else
63-
#define BOARD_TUD_MAX_SPEED OPT_MODE_FULL_SPEED
64-
#endif
44+
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
45+
#endif
46+
47+
// RHPort number used for host can be defined by board.mk, default to port 1
48+
#ifndef BOARD_TUH_RHPORT
49+
#define BOARD_TUH_RHPORT 1
6550
#endif
6651

6752
// RHPort max operational speed can defined by board.mk
68-
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
6953
#ifndef BOARD_TUH_MAX_SPEED
70-
#if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX, OPT_MCU_MIMXRT10XX, OPT_MCU_NUC505) ||\
71-
TU_CHECK_MCU(OPT_MCU_CXD56, OPT_MCU_SAMX7X, OPT_MCU_BCM2711) ||\
72-
TU_CHECK_MCU(OPT_MCU_FT90X, OPT_MCU_FT93X)
73-
#define BOARD_TUH_MAX_SPEED OPT_MODE_HIGH_SPEED
74-
#else
75-
#define BOARD_TUH_MAX_SPEED OPT_MODE_FULL_SPEED
76-
#endif
54+
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
7755
#endif
7856

79-
// Device mode with rhport and speed defined by board.mk
80-
#if BOARD_TUD_RHPORT == 0
81-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_TUD_MAX_SPEED)
82-
#elif BOARD_TUD_RHPORT == 1
83-
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_TUD_MAX_SPEED)
84-
#else
85-
#error "Incorrect RHPort configuration"
86-
#endif
57+
//--------------------------------------------------------------------
58+
// COMMON CONFIGURATION
59+
//--------------------------------------------------------------------
8760

88-
// Device mode with rhport and speed defined by board.mk
89-
#if BOARD_TUH_RHPORT == 0
90-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | BOARD_TUH_MAX_SPEED)
91-
#elif BOARD_TUH_RHPORT == 1
92-
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_HOST | BOARD_TUH_MAX_SPEED)
93-
#else
94-
#error "Incorrect RHPort configuration"
61+
// defined by compiler flags for flexibility
62+
#ifndef CFG_TUSB_MCU
63+
#error CFG_TUSB_MCU must be defined
9564
#endif
9665

97-
// This example doesn't use an RTOS
9866
#ifndef CFG_TUSB_OS
99-
#define CFG_TUSB_OS OPT_OS_NONE
67+
#define CFG_TUSB_OS OPT_OS_NONE
68+
#endif
69+
70+
#ifndef CFG_TUSB_DEBUG
71+
#define CFG_TUSB_DEBUG 0
72+
#endif
73+
74+
// Enable Device stack, Default is max speed that hardware controller could support with on-chip PHY
75+
#define CFG_TUD_ENABLED 1
76+
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
77+
78+
// Enable Host stack, Default is max speed that hardware controller could support with on-chip PHY
79+
#define CFG_TUH_ENABLED 1
80+
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
81+
82+
#if CFG_TUSB_MCU == OPT_MCU_RP2040
83+
// Use pico-pio-usb as host controller for raspberry rp2040
84+
#define CFG_TUH_RPI_PIO_USB 1
10085
#endif
10186

87+
10288
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
10389
// #define CFG_TUSB_DEBUG 0
10490

@@ -114,7 +100,7 @@
114100
#endif
115101

116102
#ifndef CFG_TUSB_MEM_ALIGN
117-
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
103+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
118104
#endif
119105

120106
//--------------------------------------------------------------------

examples/host/bare_api/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ int main(void)
6262
{
6363
board_init();
6464

65-
printf("TinyUSB Host HID Controller Example\r\n");
66-
tusb_init();
65+
printf("TinyUSB Bare API Example\r\n");
66+
67+
// init host stack on configured roothub port
68+
tuh_init(BOARD_TUH_RHPORT);
6769

6870
while (1)
6971
{

examples/host/bare_api/src/tusb_config.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,42 @@
3030
extern "C" {
3131
#endif
3232

33+
//--------------------------------------------------------------------+
34+
// Board Specific Configuration
35+
//--------------------------------------------------------------------+
36+
37+
// RHPort number used for host can be defined by board.mk, default to port 0
38+
#ifndef BOARD_TUH_RHPORT
39+
#define BOARD_TUH_RHPORT 0
40+
#endif
41+
42+
// RHPort max operational speed can defined by board.mk
43+
#ifndef BOARD_TUH_MAX_SPEED
44+
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
45+
#endif
46+
3347
//--------------------------------------------------------------------
3448
// COMMON CONFIGURATION
3549
//--------------------------------------------------------------------
3650

3751
// defined by compiler flags for flexibility
3852
#ifndef CFG_TUSB_MCU
39-
#error CFG_TUSB_MCU must be defined
53+
#error CFG_TUSB_MCU must be defined
4054
#endif
4155

42-
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
43-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
44-
#else
45-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
56+
#ifndef CFG_TUSB_OS
57+
#define CFG_TUSB_OS OPT_OS_NONE
4658
#endif
4759

48-
#ifndef CFG_TUSB_OS
49-
#define CFG_TUSB_OS OPT_OS_NONE
60+
#ifndef CFG_TUSB_DEBUG
61+
#define CFG_TUSB_DEBUG 0
5062
#endif
5163

52-
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
53-
// #define CFG_TUSB_DEBUG 0
64+
// Enable Host stack
65+
#define CFG_TUH_ENABLED 1
66+
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
5469

5570
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
5671
* Tinyusb use follows macros to declare transferring memory so that they can be put

examples/host/cdc_msc_hid/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ int main(void)
4545

4646
printf("TinyUSB Host CDC MSC HID Example\r\n");
4747

48-
tusb_init();
48+
// init host stack on configured roothub port
49+
tuh_init(BOARD_TUH_RHPORT);
4950

5051
while (1)
5152
{

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,42 @@
3030
extern "C" {
3131
#endif
3232

33+
//--------------------------------------------------------------------+
34+
// Board Specific Configuration
35+
//--------------------------------------------------------------------+
36+
37+
// RHPort number used for host can be defined by board.mk, default to port 0
38+
#ifndef BOARD_TUH_RHPORT
39+
#define BOARD_TUH_RHPORT 0
40+
#endif
41+
42+
// RHPort max operational speed can defined by board.mk
43+
#ifndef BOARD_TUH_MAX_SPEED
44+
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
45+
#endif
46+
3347
//--------------------------------------------------------------------
3448
// COMMON CONFIGURATION
3549
//--------------------------------------------------------------------
3650

3751
// defined by compiler flags for flexibility
3852
#ifndef CFG_TUSB_MCU
39-
#error CFG_TUSB_MCU must be defined
53+
#error CFG_TUSB_MCU must be defined
4054
#endif
4155

42-
// Use raspberry pio-usb for host
43-
// #define CFG_TUH_RPI_PIO_USB 1
44-
// #define CFG_TUH_RPI_PIO_USB 1
45-
46-
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
47-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
48-
#elif defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB
49-
// rp2040: port0 is native, port 1 for PIO-USB
50-
#define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST
51-
#else
52-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
56+
#ifndef CFG_TUSB_OS
57+
#define CFG_TUSB_OS OPT_OS_NONE
5358
#endif
5459

55-
#ifndef CFG_TUSB_OS
56-
#define CFG_TUSB_OS OPT_OS_NONE
60+
#ifndef CFG_TUSB_DEBUG
61+
#define CFG_TUSB_DEBUG 0
5762
#endif
5863

59-
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
60-
// #define CFG_TUSB_DEBUG 0
64+
// Enable Host stack
65+
#define CFG_TUH_ENABLED 1
66+
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
6169

6270
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
6371
* Tinyusb use follows macros to declare transferring memory so that they can be put

examples/host/hid_controller/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ int main(void)
4949
board_init();
5050

5151
printf("TinyUSB Host HID Controller Example\r\n");
52-
printf("Note: Events only displayed for explictly supported controllers\r\n");
52+
printf("Note: Events only displayed for explicit supported controllers\r\n");
5353

54-
tusb_init();
54+
// init host stack on configured roothub port
55+
tuh_init(BOARD_TUH_RHPORT);
5556

5657
while (1)
5758
{

examples/host/hid_controller/src/tusb_config.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,42 @@
3030
extern "C" {
3131
#endif
3232

33+
//--------------------------------------------------------------------+
34+
// Board Specific Configuration
35+
//--------------------------------------------------------------------+
36+
37+
// RHPort number used for host can be defined by board.mk, default to port 0
38+
#ifndef BOARD_TUH_RHPORT
39+
#define BOARD_TUH_RHPORT 0
40+
#endif
41+
42+
// RHPort max operational speed can defined by board.mk
43+
#ifndef BOARD_TUH_MAX_SPEED
44+
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
45+
#endif
46+
3347
//--------------------------------------------------------------------
3448
// COMMON CONFIGURATION
3549
//--------------------------------------------------------------------
3650

3751
// defined by compiler flags for flexibility
3852
#ifndef CFG_TUSB_MCU
39-
#error CFG_TUSB_MCU must be defined
53+
#error CFG_TUSB_MCU must be defined
4054
#endif
4155

42-
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
43-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
44-
#else
45-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
56+
#ifndef CFG_TUSB_OS
57+
#define CFG_TUSB_OS OPT_OS_NONE
4658
#endif
4759

48-
#ifndef CFG_TUSB_OS
49-
#define CFG_TUSB_OS OPT_OS_NONE
60+
#ifndef CFG_TUSB_DEBUG
61+
#define CFG_TUSB_DEBUG 0
5062
#endif
5163

52-
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
53-
// #define CFG_TUSB_DEBUG 0
64+
// Enable Host stack
65+
#define CFG_TUH_ENABLED 1
66+
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
5469

5570
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
5671
* Tinyusb use follows macros to declare transferring memory so that they can be put

src/tusb_option.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,19 @@ typedef int make_iso_compilers_happy ;
238238
#define CFG_TUH_ENABLED (TUH_RHPORT_MODE & OPT_MODE_HOST)
239239
#endif
240240

241+
#ifndef CFG_TUH_MAX_SPEED
242+
// fallback to use CFG_TUSB_RHPORTx_MODE
243+
#define CFG_TUH_MAX_SPEED (TUH_RHPORT_MODE & OPT_MODE_SPEED_MASK)
244+
#endif
245+
241246
// For backward compatible
242247
#define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED
243248
#define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED
244249

250+
//--------------------------------------------------------------------+
245251
// TODO move later
252+
//--------------------------------------------------------------------+
253+
246254
// TUP_MCU_STRICT_ALIGN will overwrite TUP_ARCH_STRICT_ALIGN.
247255
// In case TUP_MCU_STRICT_ALIGN = 1 and TUP_ARCH_STRICT_ALIGN =0, we will not reply on compiler
248256
// to generate unaligned access code.

0 commit comments

Comments
 (0)