Skip to content

Commit 8b45a8b

Browse files
authored
Merge pull request #220 from adafruit/host-msc
Implement host msc
2 parents af1451b + bf5c566 commit 8b45a8b

File tree

11 files changed

+443
-48
lines changed

11 files changed

+443
-48
lines changed

examples/DualRole/HID_device_report/HID_device_report.ino

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,32 @@
1616
*
1717
* Requirements:
1818
* - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library
19-
* - 2 consecutive GPIOs: D+ is defined by HOST_PIN_DP (gpio2), D- = D+ +1 (gpio3)
19+
* - 2 consecutive GPIOs: D+ is defined by HOST_PIN_DP (gpio20), D- = D+ +1 (gpio21)
2020
* - Provide VBus (5v) and GND for peripheral
2121
* - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed"
22-
*
23-
* RP2040 host stack will get device descriptors of attached devices and print it out via
24-
* device cdc (Serial) as follows:
25-
* Device 1: ID 046d:c52f
26-
Device Descriptor:
27-
bLength 18
28-
bDescriptorType 1
29-
bcdUSB 0200
30-
bDeviceClass 0
31-
bDeviceSubClass 0
32-
bDeviceProtocol 0
33-
bMaxPacketSize0 8
34-
idVendor 0x046d
35-
idProduct 0xc52f
36-
bcdDevice 2200
37-
iManufacturer 1 Logitech
38-
iProduct 2 USB Receiver
39-
iSerialNumber 0
40-
bNumConfigurations 1
41-
*
4222
*/
4323

4424
// pio-usb is required for rp2040 host
4525
#include "pio_usb.h"
46-
#define HOST_PIN_DP 2 // Pin used as D+ for host, D- = D+ + 1
47-
4826
#include "Adafruit_TinyUSB.h"
4927

50-
#define LANGUAGE_ID 0x0409 // English
28+
// Pin D+ for host, D- = D+ + 1
29+
#define HOST_PIN_DP 20
30+
31+
// Pin for enabling Host VBUS. comment out if not used
32+
#define HOST_PIN_VBUS_EN 22
33+
#define HOST_PIN_VBUS_EN_STATE 1
34+
35+
// Language ID: English
36+
#define LANGUAGE_ID 0x0409
5137

5238
// USB Host object
5339
Adafruit_USBH_Host USBHost;
5440

55-
// holding device descriptor
56-
tusb_desc_device_t desc_device;
41+
//--------------------------------------------------------------------+
42+
// Setup and Loop on Core0
43+
//--------------------------------------------------------------------+
5744

58-
// the setup function runs once when you press reset or power the board
5945
void setup()
6046
{
6147
Serial1.begin(115200);
@@ -70,7 +56,10 @@ void loop()
7056
{
7157
}
7258

73-
// core1's setup
59+
//--------------------------------------------------------------------+
60+
// Setup and Loop on Core1
61+
//--------------------------------------------------------------------+
62+
7463
void setup1() {
7564
//while ( !Serial ) delay(10); // wait for native usb
7665
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
@@ -84,6 +73,11 @@ void setup1() {
8473
while(1) delay(1);
8574
}
8675

76+
#ifdef HOST_PIN_VBUS_EN
77+
pinMode(HOST_PIN_VBUS_EN, OUTPUT);
78+
digitalWrite(HOST_PIN_VBUS_EN, HOST_PIN_VBUS_EN_STATE);
79+
#endif
80+
8781
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
8882
pio_cfg.pin_dp = HOST_PIN_DP;
8983
USBHost.configure_pio_usb(1, &pio_cfg);
@@ -94,7 +88,6 @@ void setup1() {
9488
USBHost.begin(1);
9589
}
9690

97-
// core1's loop
9891
void loop1()
9992
{
10093
USBHost.task();

examples/DualRole/device_info_rp2040/device_info_rp2040.ino

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* Requirements:
1818
* - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library
19-
* - 2 consecutive GPIOs: D+ is defined by HOST_PIN_DP (gpio2), D- = D+ +1 (gpio3)
19+
* - 2 consecutive GPIOs: D+ is defined by HOST_PIN_DP (gpio20), D- = D+ +1 (gpio21)
2020
* - Provide VBus (5v) and GND for peripheral
2121
* - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed"
2222
*
@@ -43,18 +43,28 @@
4343

4444
// pio-usb is required for rp2040 host
4545
#include "pio_usb.h"
46-
#define HOST_PIN_DP 2 // Pin used as D+ for host, D- = D+ + 1
47-
4846
#include "Adafruit_TinyUSB.h"
4947

50-
#define LANGUAGE_ID 0x0409 // English
48+
// Pin D+ for host, D- = D+ + 1
49+
#define HOST_PIN_DP 20
50+
51+
// Pin for enabling Host VBUS. comment out if not used
52+
#define HOST_PIN_VBUS_EN 22
53+
#define HOST_PIN_VBUS_EN_STATE 1
54+
55+
// Language ID: English
56+
#define LANGUAGE_ID 0x0409
5157

5258
// USB Host object
5359
Adafruit_USBH_Host USBHost;
5460

5561
// holding device descriptor
5662
tusb_desc_device_t desc_device;
5763

64+
//--------------------------------------------------------------------+
65+
// Setup and Loop on Core0
66+
//--------------------------------------------------------------------+
67+
5868
// the setup function runs once when you press reset or power the board
5969
void setup()
6070
{
@@ -70,20 +80,32 @@ void loop()
7080
{
7181
}
7282

73-
// core1's setup
83+
//--------------------------------------------------------------------+
84+
// Setup and Loop on Core1
85+
//--------------------------------------------------------------------+
86+
7487
void setup1() {
7588
//while ( !Serial ) delay(10); // wait for native usb
7689
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
7790

7891
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
7992
uint32_t cpu_hz = clock_get_hz(clk_sys);
8093
if ( cpu_hz != 120000000UL && cpu_hz != 240000000UL ) {
81-
while ( !Serial ) delay(10); // wait for native usb
94+
while ( !Serial ) {
95+
delay(10); // wait for native usb
96+
}
8297
Serial.printf("Error: CPU Clock = %u, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz);
8398
Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n", cpu_hz);
84-
while(1) delay(1);
99+
while(1) {
100+
delay(1);
101+
}
85102
}
86103

104+
#ifdef HOST_PIN_VBUS_EN
105+
pinMode(HOST_PIN_VBUS_EN, OUTPUT);
106+
digitalWrite(HOST_PIN_VBUS_EN, HOST_PIN_VBUS_EN_STATE);
107+
#endif
108+
87109
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
88110
pio_cfg.pin_dp = HOST_PIN_DP;
89111
USBHost.configure_pio_usb(1, &pio_cfg);
@@ -94,7 +116,6 @@ void setup1() {
94116
USBHost.begin(1);
95117
}
96118

97-
// core1's loop
98119
void loop1()
99120
{
100121
USBHost.task();

examples/DualRole/msc_file_explorer/.feather_rp2040_tinyusb.test.only

Whitespace-only changes.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2019 Ha Thach for Adafruit Industries
8+
All text above, and the splash screen below must be included in
9+
any redistribution
10+
*********************************************************************/
11+
12+
13+
/* This example demonstrates use of both device and host, where
14+
* - Device run on native usb controller (controller0)
15+
* - Host run on bit-banging 2 GPIOs with the help of Pico-PIO-USB library (controller1)
16+
*
17+
* Requirements:
18+
* - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library
19+
* - 2 consecutive GPIOs: D+ is defined by HOST_PIN_DP (gpio20), D- = D+ +1 (gpio21)
20+
* - Provide VBus (5v) and GND for peripheral
21+
* - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed"
22+
*/
23+
24+
// pio-usb is required for rp2040 host
25+
#include "pio_usb.h"
26+
27+
// SdFat is required for using Adafruit_USBH_MSC_SdFatDevice
28+
#include "SdFat.h"
29+
30+
// TinyUSB lib
31+
#include "Adafruit_TinyUSB.h"
32+
33+
// Pin D+ for host, D- = D+ + 1
34+
#define HOST_PIN_DP 20
35+
36+
// Pin for enabling Host VBUS. comment out if not used
37+
#define HOST_PIN_VBUS_EN 22
38+
#define HOST_PIN_VBUS_EN_STATE 1
39+
40+
// USB Host object
41+
Adafruit_USBH_Host USBHost;
42+
43+
// USB Host MSC Block Device object which implemented API for use with SdFat
44+
Adafruit_USBH_MSC_BlockDevice msc_block_dev;
45+
46+
// file system object from SdFat
47+
FatVolume fatfs;
48+
49+
// if file system is succesfully mounted on usb block device
50+
bool is_mounted = false;
51+
52+
//--------------------------------------------------------------------+
53+
// Setup and Loop on Core0
54+
//--------------------------------------------------------------------+
55+
56+
void setup()
57+
{
58+
Serial1.begin(115200);
59+
60+
Serial.begin(115200);
61+
//while ( !Serial ) delay(10); // wait for native usb
62+
63+
Serial.println("TinyUSB Dual Device Info Example");
64+
}
65+
66+
void loop()
67+
{
68+
}
69+
70+
//--------------------------------------------------------------------+
71+
// Setup and Loop on Core1
72+
//--------------------------------------------------------------------+
73+
74+
void setup1() {
75+
//while ( !Serial ) delay(10); // wait for native usb
76+
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
77+
78+
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
79+
uint32_t cpu_hz = clock_get_hz(clk_sys);
80+
if ( cpu_hz != 120000000UL && cpu_hz != 240000000UL ) {
81+
while ( !Serial ) {
82+
delay(10); // wait for native usb
83+
}
84+
Serial.printf("Error: CPU Clock = %u, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz);
85+
Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n", cpu_hz);
86+
while(1) {
87+
delay(1);
88+
}
89+
}
90+
91+
#ifdef HOST_PIN_VBUS_EN
92+
pinMode(HOST_PIN_VBUS_EN, OUTPUT);
93+
digitalWrite(HOST_PIN_VBUS_EN, HOST_PIN_VBUS_EN_STATE);
94+
#endif
95+
96+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
97+
pio_cfg.pin_dp = HOST_PIN_DP;
98+
USBHost.configure_pio_usb(1, &pio_cfg);
99+
100+
// run host stack on controller (rhport) 1
101+
// Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the
102+
// host bit-banging processing works done in core1 to free up core0 for other works
103+
USBHost.begin(1);
104+
}
105+
106+
void loop1()
107+
{
108+
USBHost.task();
109+
}
110+
111+
//--------------------------------------------------------------------+
112+
// TinyUSB Host callbacks
113+
//--------------------------------------------------------------------+
114+
115+
// Invoked when device is mounted (configured)
116+
void tuh_mount_cb (uint8_t daddr)
117+
{
118+
(void) daddr;
119+
}
120+
121+
/// Invoked when device is unmounted (bus reset/unplugged)
122+
void tuh_umount_cb(uint8_t daddr)
123+
{
124+
(void) daddr;
125+
}
126+
127+
// Invoked when a device with MassStorage interface is mounted
128+
void tuh_msc_mount_cb(uint8_t dev_addr)
129+
{
130+
// Initialize block device with MSC device address
131+
msc_block_dev.begin(dev_addr);
132+
133+
// For simplicity this example only support LUN 0
134+
msc_block_dev.setActiveLUN(0);
135+
136+
is_mounted = fatfs.begin(&msc_block_dev);
137+
138+
if (is_mounted) {
139+
fatfs.ls(&Serial, LS_SIZE);
140+
}
141+
}
142+
143+
// Invoked when a device with MassStorage interface is unmounted
144+
void tuh_msc_umount_cb(uint8_t dev_addr)
145+
{
146+
(void) dev_addr;
147+
148+
// unmount file system
149+
is_mounted = false;
150+
fatfs.end();
151+
152+
// end block device
153+
msc_block_dev.end();
154+
}
155+

src/Adafruit_TinyUSB.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ void TinyUSB_Device_Init(uint8_t rhport);
6464
#if CFG_TUH_ENABLED
6565

6666
#include "arduino/Adafruit_USBH_Host.h"
67+
#if CFG_TUH_MSC
68+
#include "arduino/msc/Adafruit_USBH_MSC.h"
69+
#endif
6770

6871
#endif
6972

src/arduino/Adafruit_TinyUSB_API.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
// API Version, need to be updated when there is changes for
3232
// TinyUSB_API, USBD_CDC, USBD_Device, USBD_Interface,
33-
#define TINYUSB_API_VERSION 10700
33+
#define TINYUSB_API_VERSION 11500
3434

3535
//--------------------------------------------------------------------+
3636
// Core API

0 commit comments

Comments
 (0)