Skip to content

Commit 0fa201b

Browse files
committed
Added examples to MSD device library
Former-commit-id: 2b88a95
1 parent 94a523b commit 0fa201b

File tree

4 files changed

+414
-0
lines changed

4 files changed

+414
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Portenta H33 - USB MASS STORAGE DEVICE
3+
4+
The sketch shows how to format and mount an H33 as Mass Storage Device on QSPI Flash
5+
6+
The circuit:
7+
- Portenta H33
8+
9+
created February 7th, 2023
10+
by Daniele Aimo (d.aimo@arduino.cc)
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
/*
16+
* CONFIGURATION DEFINES
17+
*/
18+
19+
/* the name of the filesystem */
20+
#define TEST_FS_NAME "qspi"
21+
22+
#include "QSPIFlashBlockDevice.h"
23+
#include "MBRBlockDevice.h"
24+
#include "UsbMsd.h"
25+
#include "FATFileSystem.h"
26+
27+
QSPIFlashBlockDevice block_device(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
28+
MBRBlockDevice mbr(&block_device, 1);
29+
USBMSD msd(&block_device);
30+
FATFileSystem fs(TEST_FS_NAME);
31+
32+
std::string root_folder = std::string("/") + std::string(TEST_FS_NAME);
33+
34+
/* -------------------------------------------------------------------------- */
35+
void printDirectoryContent(const char* name) {
36+
/* -------------------------------------------------------------------------- */
37+
DIR *dir;
38+
struct dirent *ent;
39+
int num = 0;
40+
41+
if ((dir = opendir(name)) != NULL) {
42+
while ((ent = readdir (dir)) != NULL) {
43+
if(ent->d_type == DT_REG) {
44+
Serial.print(" - [File]: ");
45+
}
46+
else if(ent->d_type == DT_DIR) {
47+
Serial.print(" - [Fold]: ");
48+
}
49+
Serial.println(ent->d_name);
50+
num++;
51+
}
52+
closedir (dir);
53+
if(num == 0) {
54+
Serial.println(" ** EMPTY ** ");
55+
}
56+
}
57+
else {
58+
Serial.print("Failed to open folder ");
59+
Serial.println(name);
60+
}
61+
}
62+
63+
64+
/* -------------------------------------------------------------------------- */
65+
/* SETUP */
66+
/* -------------------------------------------------------------------------- */
67+
void setup() {
68+
69+
/* SERIAL INITIALIZATION */
70+
Serial.begin(9600);
71+
while(!Serial) {
72+
}
73+
74+
#ifdef ALLOW_BLINKING_ALIVE
75+
pinMode(LED_BUILTIN, OUTPUT);
76+
#endif
77+
78+
Serial.println("*** USB Mass Storage DEVICE on QSPI: format the device ***");
79+
80+
/* CREATE A PARTITION big as the whole device */
81+
MBRBlockDevice::partition(&block_device, 1, 0x0e, 0, 1024* 1024 * 16);
82+
/* Mount the partition */
83+
int err = fs.mount(&block_device);
84+
if (err) {
85+
Serial.println("No Filesystem found, formatting...");
86+
err = fs.reformat(&block_device);
87+
}
88+
89+
if (err) {
90+
Serial.println("[ERROR]: Something went wrong while formatting the new partition");
91+
}
92+
93+
}
94+
95+
/* -------------------------------------------------------------------------- */
96+
/* LOOP */
97+
/* -------------------------------------------------------------------------- */
98+
void loop() {
99+
printDirectoryContent(root_folder.c_str());
100+
delay(2000);
101+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Portenta H33 - USB MASS STORAGE DEVICE
3+
4+
The sketch shows how mount an H33 as Mass Storage Device on QSPI Flash
5+
6+
The circuit:
7+
- Portenta H33
8+
9+
created February 21th, 2023
10+
by Daniele Aimo (d.aimo@arduino.cc)
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
/*
16+
* CONFIGURATION DEFINES
17+
*/
18+
19+
/* the name of the filesystem */
20+
#define TEST_FS_NAME "qspi"
21+
22+
#include "QSPIFlashBlockDevice.h"
23+
#include "UsbMsd.h"
24+
#include "FATFileSystem.h"
25+
26+
QSPIFlashBlockDevice block_device(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
27+
USBMSD msd(&block_device);
28+
FATFileSystem fs(TEST_FS_NAME);
29+
30+
std::string root_folder = std::string("/") + std::string(TEST_FS_NAME);
31+
32+
/* -------------------------------------------------------------------------- */
33+
void printDirectoryContent(const char* name) {
34+
/* -------------------------------------------------------------------------- */
35+
DIR *dir;
36+
struct dirent *ent;
37+
int num = 0;
38+
39+
if ((dir = opendir(name)) != NULL) {
40+
while ((ent = readdir (dir)) != NULL) {
41+
if(ent->d_type == DT_REG) {
42+
Serial.print(" - [File]: ");
43+
}
44+
else if(ent->d_type == DT_DIR) {
45+
Serial.print(" - [Fold]: ");
46+
}
47+
Serial.println(ent->d_name);
48+
num++;
49+
}
50+
closedir (dir);
51+
if(num == 0) {
52+
Serial.println(" ** EMPTY ** ");
53+
}
54+
}
55+
else {
56+
Serial.print("Failed to open folder ");
57+
Serial.println(name);
58+
}
59+
}
60+
61+
/* -------------------------------------------------------------------------- */
62+
/* SETUP */
63+
/* -------------------------------------------------------------------------- */
64+
void setup() {
65+
66+
/* SERIAL INITIALIZATION */
67+
Serial.begin(9600);
68+
while(!Serial) {
69+
70+
}
71+
72+
Serial.println("*** USB Mass Storage DEVICE on QSPI Flash ***");
73+
74+
75+
/* Mount the partition */
76+
int err = fs.mount(&block_device);
77+
if (err) {
78+
Serial.println("Unable to mount filesystem");
79+
while(1) {
80+
81+
}
82+
}
83+
}
84+
85+
/* -------------------------------------------------------------------------- */
86+
/* LOOP */
87+
/* -------------------------------------------------------------------------- */
88+
void loop() {
89+
Serial.print("Content of the folder ");
90+
Serial.print(root_folder.c_str());
91+
Serial.println(":");
92+
printDirectoryContent(root_folder.c_str());
93+
delay(2000);
94+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
Portenta H33 - USB MASS STORAGE DEVICE
3+
4+
The sketch shows how mount an H33 as Mass Storage Device on QSPI Flash AND SD Card
5+
6+
The circuit:
7+
- Portenta H33 + Breakout board
8+
9+
created February 21th, 2023
10+
by Daniele Aimo (d.aimo@arduino.cc)
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
/*
16+
* CONFIGURATION DEFINES
17+
*/
18+
19+
/* the name of the filesystem */
20+
#define TEST_QSPI_FS_NAME "qspi"
21+
#define TEST_SD_FS_NAME "sd"
22+
23+
#include "SDCardBlockDevice.h"
24+
#include "QSPIFlashBlockDevice.h"
25+
#include "UsbMsd.h"
26+
#include "FATFileSystem.h"
27+
28+
QSPIFlashBlockDevice bd_qspi(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
29+
SDCardBlockDevice bd_sd(PIN_SDHI_CLK, PIN_SDHI_CMD, PIN_SDHI_D0, PIN_SDHI_D1, PIN_SDHI_D2, PIN_SDHI_D3, PIN_SDHI_CD, PIN_SDHI_WP);
30+
USBMSD msd{&bd_qspi,&bd_sd};
31+
FATFileSystem fs_qspi(TEST_QSPI_FS_NAME);
32+
FATFileSystem fs_sd(TEST_SD_FS_NAME);
33+
34+
std::string root_qspi_folder = std::string("/") + std::string(TEST_QSPI_FS_NAME);
35+
std::string root_sd_folder = std::string("/") + std::string(TEST_SD_FS_NAME);
36+
37+
/* -------------------------------------------------------------------------- */
38+
void printDirectoryContent(const char* name) {
39+
/* -------------------------------------------------------------------------- */
40+
DIR *dir;
41+
struct dirent *ent;
42+
int num = 0;
43+
44+
if ((dir = opendir(name)) != NULL) {
45+
while ((ent = readdir (dir)) != NULL) {
46+
if(ent->d_type == DT_REG) {
47+
Serial.print(" - [File]: ");
48+
}
49+
else if(ent->d_type == DT_DIR) {
50+
Serial.print(" - [Fold]: ");
51+
}
52+
Serial.println(ent->d_name);
53+
num++;
54+
}
55+
closedir (dir);
56+
if(num == 0) {
57+
Serial.println(" ** EMPTY ** ");
58+
}
59+
}
60+
else {
61+
Serial.print("Failed to open folder ");
62+
Serial.println(name);
63+
}
64+
}
65+
66+
int err_qspi = 0;
67+
int err_sd = 0;
68+
69+
/* -------------------------------------------------------------------------- */
70+
/* SETUP */
71+
/* -------------------------------------------------------------------------- */
72+
void setup() {
73+
74+
/* SERIAL INITIALIZATION */
75+
Serial.begin(9600);
76+
while(!Serial) {
77+
78+
}
79+
80+
Serial.println("*** USB Mass Storage DEVICE on QSPI Flash AND SD Card ***");
81+
82+
/* Mount the QSPI device */
83+
err_qspi = fs_qspi.mount(&bd_qspi);
84+
if (err_qspi) {
85+
Serial.println("Unable to mount filesystem on QSPI Flash");
86+
Serial.println("Probably the QSPI has not been formatted properly");
87+
Serial.println("Use the FormatQSPIForMSD sketch to format the QSPI flash");
88+
}
89+
90+
/* Mount the SD Card (if present) */
91+
err_sd = fs_sd.mount(&bd_sd);
92+
if (err_sd) {
93+
Serial.println("Unable to mount filesystem on SD Card");
94+
95+
}
96+
}
97+
98+
/* -------------------------------------------------------------------------- */
99+
/* LOOP */
100+
/* -------------------------------------------------------------------------- */
101+
void loop() {
102+
int flag = 0;
103+
if(err_qspi == 0) {
104+
Serial.print("Content of the folder ");
105+
Serial.print(root_qspi_folder.c_str());
106+
Serial.println(":");
107+
printDirectoryContent(root_qspi_folder.c_str());
108+
flag++;
109+
}
110+
111+
if(err_sd == 0) {
112+
Serial.print("Content of the folder ");
113+
Serial.print(root_sd_folder.c_str());
114+
Serial.println(":");
115+
printDirectoryContent(root_sd_folder.c_str());
116+
flag++;
117+
}
118+
119+
if(flag == 0) {
120+
Serial.println("Unable to mount BOTH filesystems");
121+
}
122+
123+
124+
delay(2000);
125+
}

0 commit comments

Comments
 (0)