Skip to content

Commit 3ce9ec3

Browse files
committed
add msc data logger example
1 parent 0386ab6 commit 3ce9ec3

File tree

3 files changed

+182
-1
lines changed

3 files changed

+182
-1
lines changed

examples/DualRole/msc_data_logger/.feather_rp2040_tinyusb.test.only

Whitespace-only changes.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
* Example will log CPU temperature periodically (ms,value) to USB thumbdrive
18+
*
19+
* Requirements:
20+
* - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library
21+
* - 2 consecutive GPIOs: D+ is defined by HOST_PIN_DP (gpio20), D- = D+ +1 (gpio21)
22+
* - Provide VBus (5v) and GND for peripheral
23+
* - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed"
24+
*/
25+
26+
// pio-usb is required for rp2040 host
27+
#include "pio_usb.h"
28+
29+
// SdFat is required for using Adafruit_USBH_MSC_SdFatDevice
30+
#include "SdFat.h"
31+
32+
// TinyUSB lib
33+
#include "Adafruit_TinyUSB.h"
34+
35+
// Pin D+ for host, D- = D+ + 1
36+
#define HOST_PIN_DP 20
37+
38+
// Pin for enabling Host VBUS. comment out if not used
39+
#define HOST_PIN_VBUS_EN 22
40+
#define HOST_PIN_VBUS_EN_STATE 1
41+
42+
43+
#define LOG_FILE "cpu_temp.txt"
44+
#define LOG_INTERVAL 5000
45+
46+
// USB Host object
47+
Adafruit_USBH_Host USBHost;
48+
49+
// USB Host MSC Block Device object which implemented API for use with SdFat
50+
Adafruit_USBH_MSC_BlockDevice msc_block_dev;
51+
52+
// file system object from SdFat
53+
FatVolume fatfs;
54+
File32 f_log;
55+
56+
// if file system is successfully mounted on usb block device
57+
volatile bool is_mounted = false;
58+
59+
//--------------------------------------------------------------------+
60+
// Setup and Loop on Core0
61+
//--------------------------------------------------------------------+
62+
63+
void setup()
64+
{
65+
Serial1.begin(115200);
66+
67+
Serial.begin(115200);
68+
//while ( !Serial ) delay(10); // wait for native usb
69+
70+
Serial.println("TinyUSB Host MassStorage Data Logger Example");
71+
}
72+
73+
void loop()
74+
{
75+
// nothing to do
76+
if (!is_mounted) return;
77+
78+
f_log = fatfs.open(LOG_FILE, O_WRITE | O_APPEND | O_CREAT);
79+
80+
if (!f_log) {
81+
Serial.println("Cannot create file: " LOG_FILE);
82+
}else {
83+
float cpu_temp = analogReadTemp();
84+
85+
f_log.printf("%u\t%.02f\r\n", millis(), cpu_temp);
86+
87+
f_log.close();
88+
}
89+
90+
delay(LOG_INTERVAL);
91+
}
92+
93+
//--------------------------------------------------------------------+
94+
// Setup and Loop on Core1
95+
//--------------------------------------------------------------------+
96+
97+
void setup1() {
98+
//while ( !Serial ) delay(10); // wait for native usb
99+
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
100+
101+
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
102+
uint32_t cpu_hz = clock_get_hz(clk_sys);
103+
if ( cpu_hz != 120000000UL && cpu_hz != 240000000UL ) {
104+
while ( !Serial ) {
105+
delay(10); // wait for native usb
106+
}
107+
Serial.printf("Error: CPU Clock = %u, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz);
108+
Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n", cpu_hz);
109+
while(1) {
110+
delay(1);
111+
}
112+
}
113+
114+
#ifdef HOST_PIN_VBUS_EN
115+
pinMode(HOST_PIN_VBUS_EN, OUTPUT);
116+
digitalWrite(HOST_PIN_VBUS_EN, HOST_PIN_VBUS_EN_STATE);
117+
#endif
118+
119+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
120+
pio_cfg.pin_dp = HOST_PIN_DP;
121+
USBHost.configure_pio_usb(1, &pio_cfg);
122+
123+
// run host stack on controller (rhport) 1
124+
// Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the
125+
// host bit-banging processing works done in core1 to free up core0 for other works
126+
USBHost.begin(1);
127+
}
128+
129+
void loop1()
130+
{
131+
USBHost.task();
132+
Serial.flush();
133+
}
134+
135+
//--------------------------------------------------------------------+
136+
// TinyUSB Host callbacks
137+
//--------------------------------------------------------------------+
138+
139+
// Invoked when device is mounted (configured)
140+
void tuh_mount_cb (uint8_t daddr)
141+
{
142+
(void) daddr;
143+
}
144+
145+
/// Invoked when device is unmounted (bus reset/unplugged)
146+
void tuh_umount_cb(uint8_t daddr)
147+
{
148+
(void) daddr;
149+
}
150+
151+
// Invoked when a device with MassStorage interface is mounted
152+
void tuh_msc_mount_cb(uint8_t dev_addr)
153+
{
154+
// Initialize block device with MSC device address
155+
msc_block_dev.begin(dev_addr);
156+
157+
// For simplicity this example only support LUN 0
158+
msc_block_dev.setActiveLUN(0);
159+
160+
is_mounted = fatfs.begin(&msc_block_dev);
161+
162+
if (is_mounted) {
163+
fatfs.ls(&Serial, LS_SIZE);
164+
}else {
165+
Serial.println("Failed to mount mass storage device. Make sure it is formatted as FAT");
166+
}
167+
}
168+
169+
// Invoked when a device with MassStorage interface is unmounted
170+
void tuh_msc_umount_cb(uint8_t dev_addr)
171+
{
172+
(void) dev_addr;
173+
174+
// unmount file system
175+
is_mounted = false;
176+
fatfs.end();
177+
178+
// end block device
179+
msc_block_dev.end();
180+
}
181+

examples/DualRole/msc_file_explorer/msc_file_explorer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void setup()
6060
Serial.begin(115200);
6161
//while ( !Serial ) delay(10); // wait for native usb
6262

63-
Serial.println("TinyUSB Dual Device Info Example");
63+
Serial.println("TinyUSB Host Mass Storage File Explorer Example");
6464
}
6565

6666
void loop()

0 commit comments

Comments
 (0)