Skip to content

Commit a03a03d

Browse files
authored
Merge pull request #1495 from hathach/enhance-tusb-config
Better support multiple controllers configuration
2 parents afd9b18 + 7c82783 commit a03a03d

File tree

90 files changed

+1072
-841
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1072
-841
lines changed

examples/device/audio_4_channel_mic/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ int main(void)
8181
{
8282
board_init();
8383

84-
tusb_init();
84+
// init device stack on configured roothub port
85+
tud_init(BOARD_TUD_RHPORT);
8586

8687
// Init values
8788
sampFreq = AUDIO_SAMPLE_RATE;

examples/device/audio_4_channel_mic/src/tusb_config.h

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

33+
//--------------------------------------------------------------------+
34+
// Board Specific Configuration
35+
//--------------------------------------------------------------------+
36+
37+
// RHPort number used for device can be defined by board.mk, default to port 0
38+
#ifndef BOARD_TUD_RHPORT
39+
#define BOARD_TUD_RHPORT 0
40+
#endif
41+
42+
// RHPort max operational speed can defined by board.mk
43+
#ifndef BOARD_TUD_MAX_SPEED
44+
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
45+
#endif
46+
3347
//--------------------------------------------------------------------
3448
// COMMON CONFIGURATION
3549
//--------------------------------------------------------------------
@@ -39,18 +53,19 @@ extern "C" {
3953
#error CFG_TUSB_MCU must be defined
4054
#endif
4155

42-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
43-
4456
#ifndef CFG_TUSB_OS
45-
#define CFG_TUSB_OS OPT_OS_NONE
57+
#define CFG_TUSB_OS OPT_OS_NONE
4658
#endif
4759

4860
#ifndef CFG_TUSB_DEBUG
49-
#define CFG_TUSB_DEBUG 0
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 Device stack
65+
#define CFG_TUD_ENABLED 1
66+
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUD_MAX_SPEED BOARD_TUD_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
@@ -64,7 +79,7 @@ extern "C" {
6479
#endif
6580

6681
#ifndef CFG_TUSB_MEM_ALIGN
67-
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
82+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
6883
#endif
6984

7085
//--------------------------------------------------------------------
@@ -76,11 +91,11 @@ extern "C" {
7691
#endif
7792

7893
//------------- CLASS -------------//
94+
#define CFG_TUD_AUDIO 1
7995
#define CFG_TUD_CDC 0
8096
#define CFG_TUD_MSC 0
8197
#define CFG_TUD_HID 0
8298
#define CFG_TUD_MIDI 0
83-
#define CFG_TUD_AUDIO 1
8499
#define CFG_TUD_VENDOR 0
85100

86101
//--------------------------------------------------------------------
@@ -89,23 +104,23 @@ extern "C" {
89104

90105
// Have a look into audio_device.h for all configurations
91106

92-
#define CFG_TUD_AUDIO_FUNC_1_DESC_LEN TUD_AUDIO_MIC_FOUR_CH_DESC_LEN
107+
#define CFG_TUD_AUDIO_FUNC_1_DESC_LEN TUD_AUDIO_MIC_FOUR_CH_DESC_LEN
93108

94-
#define CFG_TUD_AUDIO_FUNC_1_N_AS_INT 1
95-
#define CFG_TUD_AUDIO_FUNC_1_CTRL_BUF_SZ 64
109+
#define CFG_TUD_AUDIO_FUNC_1_N_AS_INT 1
110+
#define CFG_TUD_AUDIO_FUNC_1_CTRL_BUF_SZ 64
96111

97-
#define CFG_TUD_AUDIO_ENABLE_EP_IN 1
98-
#define CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX 2 // This value is not required by the driver, it parses this information from the descriptor once the alternate interface is set by the host - we use it for the setup
99-
#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX 4 // This value is not required by the driver, it parses this information from the descriptor once the alternate interface is set by the host - we use it for the setup
100-
#define CFG_TUD_AUDIO_EP_SZ_IN (48 + 1) * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX // 48 Samples (48 kHz) x 2 Bytes/Sample x CFG_TUD_AUDIO_N_CHANNELS_TX Channels - the Windows driver always needs an extra sample per channel of space more, otherwise it complains... found by trial and error
101-
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX CFG_TUD_AUDIO_EP_SZ_IN
102-
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ CFG_TUD_AUDIO_EP_SZ_IN
112+
#define CFG_TUD_AUDIO_ENABLE_EP_IN 1
113+
#define CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX 2 // This value is not required by the driver, it parses this information from the descriptor once the alternate interface is set by the host - we use it for the setup
114+
#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX 4 // This value is not required by the driver, it parses this information from the descriptor once the alternate interface is set by the host - we use it for the setup
115+
#define CFG_TUD_AUDIO_EP_SZ_IN (48 + 1) * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX // 48 Samples (48 kHz) x 2 Bytes/Sample x CFG_TUD_AUDIO_N_CHANNELS_TX Channels - the Windows driver always needs an extra sample per channel of space more, otherwise it complains... found by trial and error
116+
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX CFG_TUD_AUDIO_EP_SZ_IN
117+
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ CFG_TUD_AUDIO_EP_SZ_IN
103118

104-
#define CFG_TUD_AUDIO_ENABLE_ENCODING 1
105-
#define CFG_TUD_AUDIO_ENABLE_TYPE_I_ENCODING 1
106-
#define CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX 2 // One I2S stream contains two channels, each stream is saved within one support FIFO - this value is currently fixed, the driver does not support a changing value
107-
#define CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO (CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX / CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX)
108-
#define CFG_TUD_AUDIO_FUNC_1_TX_SUPP_SW_FIFO_SZ (CFG_TUD_AUDIO_EP_SZ_IN / CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO)
119+
#define CFG_TUD_AUDIO_ENABLE_ENCODING 1
120+
#define CFG_TUD_AUDIO_ENABLE_TYPE_I_ENCODING 1
121+
#define CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX 2 // One I2S stream contains two channels, each stream is saved within one support FIFO - this value is currently fixed, the driver does not support a changing value
122+
#define CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO (CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX / CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX)
123+
#define CFG_TUD_AUDIO_FUNC_1_TX_SUPP_SW_FIFO_SZ (CFG_TUD_AUDIO_EP_SZ_IN / CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO)
109124

110125
#ifdef __cplusplus
111126
}

examples/device/audio_test/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ int main(void)
8282
{
8383
board_init();
8484

85-
tusb_init();
85+
// init device stack on configured roothub port
86+
tud_init(BOARD_TUD_RHPORT);
8687

8788
// Init values
8889
sampFreq = AUDIO_SAMPLE_RATE;

examples/device/audio_test/src/tusb_config.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@
3030
extern "C" {
3131
#endif
3232

33+
//--------------------------------------------------------------------+
34+
// Board Specific Configuration
35+
//--------------------------------------------------------------------+
36+
37+
// RHPort number used for device can be defined by board.mk, default to port 0
38+
#ifndef BOARD_TUD_RHPORT
39+
#define BOARD_TUD_RHPORT 0
40+
#endif
41+
42+
// RHPort max operational speed can defined by board.mk
43+
#ifndef BOARD_TUD_MAX_SPEED
44+
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
45+
#endif
46+
3347
//--------------------------------------------------------------------
3448
// COMMON CONFIGURATION
3549
//--------------------------------------------------------------------
@@ -39,8 +53,6 @@ extern "C" {
3953
#error CFG_TUSB_MCU must be defined
4054
#endif
4155

42-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
43-
4456
#ifndef CFG_TUSB_OS
4557
#define CFG_TUSB_OS OPT_OS_NONE
4658
#endif
@@ -49,6 +61,12 @@ extern "C" {
4961
#define CFG_TUSB_DEBUG 0
5062
#endif
5163

64+
// Enable Device stack
65+
#define CFG_TUD_ENABLED 1
66+
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
69+
5270
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
5371
// #define CFG_TUSB_DEBUG 0
5472

@@ -76,11 +94,11 @@ extern "C" {
7694
#endif
7795

7896
//------------- CLASS -------------//
97+
#define CFG_TUD_AUDIO 1
7998
#define CFG_TUD_CDC 0
8099
#define CFG_TUD_MSC 0
81100
#define CFG_TUD_HID 0
82101
#define CFG_TUD_MIDI 0
83-
#define CFG_TUD_AUDIO 1
84102
#define CFG_TUD_VENDOR 0
85103

86104
//--------------------------------------------------------------------

examples/device/board_test/src/tusb_config.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
#define CFG_TUSB_OS OPT_OS_NONE
4444
#endif
4545

46-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_NONE
46+
// This example only test LED & GPIO, disable both device and host stack
47+
#define CFG_TUD_ENABLED 0
48+
#define CFG_TUH_ENABLED 0
4749

4850
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
4951
// #define CFG_TUSB_DEBUG 0
@@ -63,21 +65,6 @@
6365
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
6466
#endif
6567

66-
//--------------------------------------------------------------------
67-
// DEVICE CONFIGURATION
68-
//--------------------------------------------------------------------
69-
70-
#ifndef CFG_TUD_ENDPOINT0_SIZE
71-
#define CFG_TUD_ENDPOINT0_SIZE 64
72-
#endif
73-
74-
//------------- CLASS -------------//
75-
#define CFG_TUD_CDC 0
76-
#define CFG_TUD_MSC 0
77-
#define CFG_TUD_HID 0
78-
#define CFG_TUD_MIDI 0
79-
#define CFG_TUD_VENDOR 0
80-
8168
#ifdef __cplusplus
8269
}
8370
#endif

examples/device/cdc_dual_ports/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ int main(void)
3939
{
4040
board_init();
4141

42-
tusb_init();
42+
// init device stack on configured roothub port
43+
tud_init(BOARD_TUD_RHPORT);
4344

4445
while (1)
4546
{

examples/device/cdc_dual_ports/src/tusb_config.h

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

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

3751
// defined by board.mk
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-
// RHPort number used for device can be defined by board.mk, default to port 0
43-
#ifndef BOARD_DEVICE_RHPORT_NUM
44-
#define BOARD_DEVICE_RHPORT_NUM 0
45-
#endif
46-
47-
// RHPort max operational speed can defined by board.mk
48-
// Default to max (auto) speed for MCU with internal HighSpeed PHY
49-
#ifndef BOARD_DEVICE_RHPORT_SPEED
50-
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_DEFAULT_SPEED
56+
#ifndef CFG_TUSB_OS
57+
#define CFG_TUSB_OS OPT_OS_NONE
5158
#endif
5259

53-
// Device mode with rhport and speed defined by board.mk
54-
#if BOARD_DEVICE_RHPORT_NUM == 0
55-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
56-
#elif BOARD_DEVICE_RHPORT_NUM == 1
57-
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
58-
#else
59-
#error "Incorrect RHPort configuration"
60+
#ifndef CFG_TUSB_DEBUG
61+
#define CFG_TUSB_DEBUG 0
6062
#endif
6163

62-
#ifndef CFG_TUSB_OS
63-
#define CFG_TUSB_OS OPT_OS_NONE
64-
#endif
64+
// Enable Device stack
65+
#define CFG_TUD_ENABLED 1
6566

66-
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
67-
// #define CFG_TUSB_DEBUG 0
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
6869

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

examples/device/cdc_msc/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ void cdc_task(void);
5454
int main(void)
5555
{
5656
board_init();
57-
tusb_init();
57+
58+
// init device stack on configured roothub port
59+
tud_init(BOARD_TUD_RHPORT);
5860

5961
while (1)
6062
{

examples/device/cdc_msc/src/tusb_config.h

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,42 @@
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
43-
#ifndef BOARD_DEVICE_RHPORT_NUM
44-
#define BOARD_DEVICE_RHPORT_NUM 0
38+
#ifndef BOARD_TUD_RHPORT
39+
#define BOARD_TUD_RHPORT 0
4540
#endif
4641

4742
// RHPort max operational speed can defined by board.mk
48-
// Default to max (auto) speed for MCU with internal HighSpeed PHY
49-
#ifndef BOARD_DEVICE_RHPORT_SPEED
50-
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_DEFAULT_SPEED
43+
#ifndef BOARD_TUD_MAX_SPEED
44+
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
5145
#endif
5246

53-
// Device mode with rhport and speed defined by board.mk
54-
#if BOARD_DEVICE_RHPORT_NUM == 0
55-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
56-
#elif BOARD_DEVICE_RHPORT_NUM == 1
57-
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
58-
#else
59-
#error "Incorrect RHPort configuration"
47+
//--------------------------------------------------------------------
48+
// Common Configuration
49+
//--------------------------------------------------------------------
50+
51+
// defined by compiler flags for flexibility
52+
#ifndef CFG_TUSB_MCU
53+
#error CFG_TUSB_MCU must be defined
6054
#endif
6155

62-
// This example doesn't use an RTOS
6356
#ifndef CFG_TUSB_OS
64-
#define CFG_TUSB_OS OPT_OS_NONE
57+
#define CFG_TUSB_OS OPT_OS_NONE
6558
#endif
6659

67-
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
68-
// #define CFG_TUSB_DEBUG 0
60+
#ifndef CFG_TUSB_DEBUG
61+
#define CFG_TUSB_DEBUG 0
62+
#endif
63+
64+
// Enable Device stack
65+
#define CFG_TUD_ENABLED 1
66+
67+
// Default is max speed that hardware controller could support with on-chip PHY
68+
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
6969

7070
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
7171
* Tinyusb use follows macros to declare transferring memory so that they can be put
@@ -79,7 +79,7 @@
7979
#endif
8080

8181
#ifndef CFG_TUSB_MEM_ALIGN
82-
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
82+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
8383
#endif
8484

8585
//--------------------------------------------------------------------

examples/device/cdc_msc_freertos/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ void usb_device_task(void* param)
123123
{
124124
(void) param;
125125

126+
// init device stack on configured roothub port
126127
// This should be called after scheduler/kernel is started.
127128
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
128-
tusb_init();
129+
tud_init(BOARD_TUD_RHPORT);
129130

130131
// RTOS forever loop
131132
while (1)

0 commit comments

Comments
 (0)