Skip to content

Commit f3f5d9f

Browse files
authored
update TinyUSB examples for latest TinyUSB (#325)
1 parent 9600dec commit f3f5d9f

File tree

11 files changed

+195
-89
lines changed

11 files changed

+195
-89
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ At the time of writing, there is only one host example available:
275275

276276
- tinyusb_host_cdc_msc_hid
277277

278+
### USB Dual Mode
279+
280+
USB Dual Mode uses PIO as a USB host controller and the RP2040 USB device controller as a device controller. All the USB dual examples come directly from the TinyUSB dual examples directory [here](https://github.com/hathach/tinyusb/tree/master/examples/dual).
281+
Those that are supported on RP2040 devices are automatically included as part of the pico-examples
282+
build as targets named `tinyusb_dual_<example_name>`, e.g. https://github.com/hathach/tinyusb/tree/master/examples/dual/host_hid_to_device_cdc
283+
is built as `tinyusb_dual_host_hid_to_device_cdc`.
284+
285+
At the time of writing, there is only one dual example available:
286+
287+
- tinyusb_dual_host_hid_to_device_cdc
288+
278289
### Watchdog
279290

280291
App|Description

usb/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ if (TARGET tinyusb_host)
77
add_subdirectory(host)
88
else ()
99
message("Skipping TinyUSB host examples as TinyUSB is unavailable")
10+
endif ()
11+
if (TARGET tinyusb_pico_pio_usb)
12+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.3)
13+
message("Skipping TinyUSB dual examples, as TinyUSB hw/mcu/raspberry_pi/Pico-PIO-USB does not currently compile on GCC 11.3 or greater")
14+
else()
15+
add_subdirectory(dual)
16+
endif()
17+
else ()
18+
message("Skipping TinyUSB dual examples, as TinyUSB hw/mcu/raspberry_pi/Pico-PIO-USB submodule unavailable")
1019
endif ()

usb/device/dev_hid_composite/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void hid_task(void)
223223
// Invoked when sent REPORT successfully to host
224224
// Application can use this to send the next report
225225
// Note: For composite reports, report[0] is report ID
226-
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len)
226+
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len)
227227
{
228228
(void) instance;
229229
(void) len;

usb/dual/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(FAMILY rp2040)
2+
set(BOARD pico_sdk)
3+
set(TINYUSB_FAMILY_PROJECT_NAME_PREFIX "tinyusb_dual_")
4+
add_subdirectory(${PICO_TINYUSB_PATH}/examples/dual tinyusb_dual_examples)

usb/host/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
set(FAMILY rp2040)
22
set(BOARD pico_sdk)
33
set(TINYUSB_FAMILY_PROJECT_NAME_PREFIX "tinyusb_host_")
4+
# Hack as some host examples use $TOP in their path
5+
set(TOP ${PICO_TINYUSB_PATH})
46
add_subdirectory(${PICO_TINYUSB_PATH}/examples/host tinyusb_host_examples)
57

68
add_subdirectory(host_cdc_msc_hid)

usb/host/host_cdc_msc_hid/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ target_sources(host_cdc_msc_hid PUBLIC
77
${CMAKE_CURRENT_LIST_DIR}/hid_app.c
88
${CMAKE_CURRENT_LIST_DIR}/main.c
99
${CMAKE_CURRENT_LIST_DIR}/msc_app.c
10+
${CMAKE_CURRENT_LIST_DIR}/cdc_app.c
1011
)
1112

1213
# Make sure TinyUSB can find tusb_config.h

usb/host/host_cdc_msc_hid/cdc_app.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022, Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#include "tusb.h"
28+
#include "bsp/board.h"
29+
30+
//--------------------------------------------------------------------+
31+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
32+
//--------------------------------------------------------------------+
33+
34+
35+
//------------- IMPLEMENTATION -------------//
36+
37+
size_t get_console_inputs(uint8_t* buf, size_t bufsize)
38+
{
39+
size_t count = 0;
40+
while (count < bufsize)
41+
{
42+
int ch = board_getchar();
43+
if ( ch <= 0 ) break;
44+
45+
buf[count] = (uint8_t) ch;
46+
count++;
47+
}
48+
49+
return count;
50+
}
51+
52+
void cdc_app_task(void)
53+
{
54+
uint8_t buf[64+1]; // +1 for extra null character
55+
uint32_t const bufsize = sizeof(buf)-1;
56+
57+
uint32_t count = get_console_inputs(buf, bufsize);
58+
buf[count] = 0;
59+
60+
// loop over all mounted interfaces
61+
for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++)
62+
{
63+
if ( tuh_cdc_mounted(idx) )
64+
{
65+
// console --> cdc interfaces
66+
if (count)
67+
{
68+
tuh_cdc_write(idx, buf, count);
69+
tuh_cdc_write_flush(idx);
70+
}
71+
}
72+
}
73+
}
74+
75+
// Invoked when received new data
76+
void tuh_cdc_rx_cb(uint8_t idx)
77+
{
78+
uint8_t buf[64+1]; // +1 for extra null character
79+
uint32_t const bufsize = sizeof(buf)-1;
80+
81+
// forward cdc interfaces -> console
82+
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
83+
buf[count] = 0;
84+
85+
printf((char*) buf);
86+
}
87+
88+
void tuh_cdc_mount_cb(uint8_t idx)
89+
{
90+
tuh_cdc_itf_info_t itf_info = { 0 };
91+
tuh_cdc_itf_get_info(idx, &itf_info);
92+
93+
printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
94+
95+
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
96+
// CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
97+
// otherwise you need to call tuh_cdc_set_line_coding() first
98+
cdc_line_coding_t line_coding = { 0 };
99+
if ( tuh_cdc_get_local_line_coding(idx, &line_coding) )
100+
{
101+
printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
102+
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity , line_coding.data_bits);
103+
}
104+
#endif
105+
}
106+
107+
void tuh_cdc_umount_cb(uint8_t idx)
108+
{
109+
tuh_cdc_itf_info_t itf_info = { 0 };
110+
tuh_cdc_itf_get_info(idx, &itf_info);
111+
112+
printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
113+
}

usb/host/host_cdc_msc_hid/hid_app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
247247
// Composite report, 1st byte is report ID, data starts from 2nd byte
248248
uint8_t const rpt_id = report[0];
249249

250-
// Find report id in the arrray
250+
// Find report id in the array
251251
for(uint8_t i=0; i<rpt_count; i++)
252252
{
253253
if (rpt_id == rpt_info_arr[i].report_id )

usb/host/host_cdc_msc_hid/main.c

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//--------------------------------------------------------------------+
3636
void led_blinking_task(void);
3737

38-
extern void cdc_task(void);
38+
extern void cdc_app_task(void);
3939
extern void hid_app_task(void);
4040

4141
/*------------- MAIN -------------*/
@@ -45,38 +45,30 @@ 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
{
5253
// tinyusb host task
5354
tuh_task();
54-
led_blinking_task();
55-
56-
#if CFG_TUH_CDC
57-
cdc_task();
58-
#endif
5955

60-
#if CFG_TUH_HID
56+
led_blinking_task();
57+
cdc_app_task();
6158
hid_app_task();
62-
#endif
6359
}
6460

6561
return 0;
6662
}
6763

6864
//--------------------------------------------------------------------+
69-
// USB CDC
65+
// TinyUSB Callbacks
7066
//--------------------------------------------------------------------+
71-
#if CFG_TUH_CDC
72-
CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
7367

7468
void tuh_mount_cb(uint8_t dev_addr)
7569
{
7670
// application set-up
7771
printf("A device with address %d is mounted\r\n", dev_addr);
78-
79-
tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer
8072
}
8173

8274
void tuh_umount_cb(uint8_t dev_addr)
@@ -85,29 +77,6 @@ void tuh_umount_cb(uint8_t dev_addr)
8577
printf("A device with address %d is unmounted \r\n", dev_addr);
8678
}
8779

88-
// invoked ISR context
89-
void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes)
90-
{
91-
(void) event;
92-
(void) pipe_id;
93-
(void) xferred_bytes;
94-
95-
printf(serial_in_buffer);
96-
tu_memclr(serial_in_buffer, sizeof(serial_in_buffer));
97-
98-
tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // waiting for next data
99-
}
100-
101-
void cdc_task(void)
102-
{
103-
104-
}
105-
106-
#endif
107-
108-
//--------------------------------------------------------------------+
109-
// TinyUSB Callbacks
110-
//--------------------------------------------------------------------+
11180

11281
//--------------------------------------------------------------------+
11382
// Blinking Task

usb/host/host_cdc_msc_hid/msc_app.c

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525

2626
#include "tusb.h"
2727

28-
#if CFG_TUH_MSC
29-
3028
//--------------------------------------------------------------------+
3129
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
3230
//--------------------------------------------------------------------+
3331
static scsi_inquiry_resp_t inquiry_resp;
3432

35-
bool inquiry_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
33+
bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const * cb_data)
3634
{
35+
msc_cbw_t const* cbw = cb_data->cbw;
36+
msc_csw_t const* csw = cb_data->csw;
37+
3738
if (csw->status != 0)
3839
{
3940
printf("Inquiry failed\r\n");
@@ -59,48 +60,12 @@ void tuh_msc_mount_cb(uint8_t dev_addr)
5960
printf("A MassStorage device is mounted\r\n");
6061

6162
uint8_t const lun = 0;
62-
tuh_msc_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb);
63-
//
64-
// //------------- file system (only 1 LUN support) -------------//
65-
// uint8_t phy_disk = dev_addr-1;
66-
// disk_initialize(phy_disk);
67-
//
68-
// if ( disk_is_ready(phy_disk) )
69-
// {
70-
// if ( f_mount(phy_disk, &fatfs[phy_disk]) != FR_OK )
71-
// {
72-
// puts("mount failed");
73-
// return;
74-
// }
75-
//
76-
// f_chdrive(phy_disk); // change to newly mounted drive
77-
// f_chdir("/"); // root as current dir
78-
//
79-
// cli_init();
80-
// }
63+
tuh_msc_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb, 0);
8164
}
8265

8366
void tuh_msc_umount_cb(uint8_t dev_addr)
8467
{
8568
(void) dev_addr;
8669
printf("A MassStorage device is unmounted\r\n");
87-
88-
// uint8_t phy_disk = dev_addr-1;
89-
//
90-
// f_mount(phy_disk, NULL); // unmount disk
91-
// disk_deinitialize(phy_disk);
92-
//
93-
// if ( phy_disk == f_get_current_drive() )
94-
// { // active drive is unplugged --> change to other drive
95-
// for(uint8_t i=0; i<CFG_TUH_DEVICE_MAX; i++)
96-
// {
97-
// if ( disk_is_ready(i) )
98-
// {
99-
// f_chdrive(i);
100-
// cli_init(); // refractor, rename
101-
// }
102-
// }
103-
// }
10470
}
10571

106-
#endif

0 commit comments

Comments
 (0)