Skip to content

Commit 7099920

Browse files
committed
Libraries: modernize USBMSD library
1 parent 29d6290 commit 7099920

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
QSPI as USB Mass Storage
3+
This example shows how to expose a QSPIF BlockDevice (16MB external flash on the Portenta H7)
4+
as an USB stick. It can be adapted to any kind of BlockDevice (FlashIAP or either RAM via HeapBlockDevice)
5+
Before loading this example, make sure you execute PortentWiFiFirmwareUpdater sketch
6+
to create and format the proper partitions.
7+
*/
8+
9+
#include "PluggableUSBMSD.h"
10+
#include "QSPIFBlockDevice.h"
11+
#include "MBRBlockDevice.h"
12+
#include "FATFileSystem.h"
13+
14+
static QSPIFBlockDevice root;
15+
mbed::MBRBlockDevice wifi_data(&root, 1);
16+
mbed::MBRBlockDevice ota_data(&root, 2);
17+
static mbed::FATFileSystem wifi("wifi");
18+
static mbed::FATFileSystem ota("ota");
19+
20+
void USBMSD::begin()
21+
{
22+
int err = wifi.mount(&wifi_data);
23+
if (err) {
24+
while (!Serial);
25+
Serial.println("Please run PortentWiFiFirmwareUpdater before");
26+
return;
27+
}
28+
ota.mount(&ota_data);
29+
}
30+
31+
32+
USBMSD MassStorage(&root);
33+
34+
void setup() {
35+
Serial.begin(115200);
36+
MassStorage.begin();
37+
}
38+
39+
void printDirectory(char* name) {
40+
DIR *d;
41+
struct dirent *p;
42+
43+
d = opendir(name);
44+
if (d != NULL) {
45+
while ((p = readdir(d)) != NULL) {
46+
Serial.println(p->d_name);
47+
}
48+
}
49+
closedir(d);
50+
}
51+
52+
void loop() {
53+
if (MassStorage.media_removed()) {
54+
// list the content of the partitions
55+
// you may need to restart the board for the list to update if you copied new files
56+
Serial.println("Content of WiFi partition:");
57+
printDirectory("/wifi");
58+
Serial.println("Content of OTA partition:");
59+
printDirectory("/ota");
60+
}
61+
delay(1000);
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
This example exposes the second half (512KB) of Nano 33 BLE flash
3+
as a USB disk.
4+
The user can interact with this disk as a bidirectional communication with the board
5+
For example, the board could save data in a file to be retrieved later with a drag and drop
6+
*/
7+
8+
#include "PluggableUSBMSD.h"
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
MassStorage.begin();
13+
14+
// write a file with some data
15+
// a+ means append, so every time the board is rebooted the file will grow by a new line
16+
FILE *f = fopen("/fs/data.txt", "a+");
17+
String hello = "Hello from Nano33BLE Filesystem\n";
18+
fwrite(hello.c_str(), hello.length(), 1, f);
19+
fclose(f);
20+
}
21+
22+
void loop() {
23+
delay(1000);
24+
}

libraries/USBMSD/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=USB Mass Storage
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Allow exposing block devices as USB disk when connected to a PC
6+
paragraph=
7+
category=Data Storage
8+
url=https://github.com/arduino/ArduinoCore-mbed/tree/master/libraries/USBMSD
9+
architectures=mbed,mbed_portenta,mbed_nano,mbed_edge,mbed_rp2040
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)