Skip to content

Commit cc53b80

Browse files
committed
added example
1 parent 30bd0e1 commit cc53b80

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
/*
2+
Portenta H33 - USB HOST MSD
3+
4+
The sketch shows how to mount a Mass Storage Device using portenta as Host Device
5+
6+
The circuit:
7+
- Portenta H33 + Portenta Breakout
8+
9+
created February 7th, 2023
10+
by Daniele Aimo ([email protected])
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <vector>
16+
#include <string>
17+
#include "UsbHostMsd.h"
18+
#include "FATFileSystem.h"
19+
20+
#define TEST_FS_NAME "usb"
21+
#define TEST_FOLDER_NAME "TEST_FOLDER"
22+
#define TEST_FILE "test.txt"
23+
#define DELETE_FILE_DIMENSION 150
24+
25+
26+
USBHostMSD block_device;
27+
FATFileSystem fs(TEST_FS_NAME);
28+
29+
extern "C" void log_init();
30+
extern "C" int mylogadd(const char *fmt, ...) ;
31+
extern "C" void empty_log();
32+
33+
std::string root_folder = std::string("/") + std::string(TEST_FS_NAME);
34+
std::string folder_test_name = root_folder + std::string("/") + std::string(TEST_FOLDER_NAME);
35+
std::string file_test_name = folder_test_name + std::string("/") + std::string(TEST_FILE);
36+
37+
void setup() {
38+
log_init();
39+
/*
40+
* SERIAL INITIALIZATION
41+
*/
42+
Serial.begin(9600);
43+
while(!Serial) {
44+
45+
}
46+
47+
Serial.println("*** USB HOST Mass Storage Device example ***");
48+
49+
/* list to store all directory in the root */
50+
std::vector<std::string> dir_list;
51+
52+
/*
53+
* Check for device to be connected
54+
*/
55+
56+
int count = 0;
57+
while (!block_device.connect()) {
58+
if(count == 0) {
59+
Serial.println("Waiting for Mass Storage Device");
60+
}
61+
else {
62+
Serial.print(".");
63+
if(count % 30 == 0) {
64+
Serial.println();
65+
empty_log();
66+
}
67+
}
68+
count++;
69+
delay(1000);
70+
}
71+
72+
Serial.println("Mass Storage Device connected.");
73+
74+
/*
75+
* MOUNTIN SDCARD AS FATFS filesystem
76+
*/
77+
78+
Serial.println("Mounting Mass Storage Device...");
79+
int err = fs.mount(&block_device);
80+
if (err) {
81+
// Reformat if we can't mount the filesystem
82+
// this should only happen on the first boot
83+
Serial.println("No filesystem found, formatting... ");
84+
err = fs.reformat(&block_device);
85+
}
86+
87+
if (err) {
88+
Serial.println("Error formatting USB Mass Storage Device");
89+
while(1);
90+
}
91+
92+
/*
93+
* READING root folder
94+
*/
95+
96+
DIR *dir;
97+
struct dirent *ent;
98+
int dirIndex = 0;
99+
100+
Serial.println("*** List USB Mass Storage Device content: ");
101+
if ((dir = opendir(root_folder.c_str())) != NULL) {
102+
while ((ent = readdir (dir)) != NULL) {
103+
if(ent->d_type == DT_REG) {
104+
Serial.print("- [File]: ");
105+
}
106+
else if(ent->d_type == DT_DIR) {
107+
Serial.print("- [Fold]: ");
108+
dir_list.push_back(ent->d_name);
109+
}
110+
Serial.println(ent->d_name);
111+
dirIndex++;
112+
}
113+
closedir (dir);
114+
}
115+
else {
116+
// Could not open directory
117+
Serial.println("Error opening USB Mass Storage Device\n");
118+
while(1);
119+
}
120+
121+
bool found_test_folder = false;
122+
123+
/*
124+
* LISTING CONTENT of the first level folders (the one immediately present in root folder)
125+
*/
126+
127+
if(dir_list.size()) {
128+
Serial.println();
129+
Serial.println("Listing content of folders in root: ");
130+
}
131+
for(unsigned int i = 0; i < dir_list.size(); i++) {
132+
if(dir_list[i] == TEST_FOLDER_NAME) {
133+
found_test_folder = true;
134+
}
135+
Serial.print("- ");
136+
Serial.print(dir_list[i].c_str());
137+
Serial.println(":");
138+
139+
std::string d = root_folder + std::string("/") + dir_list[i];
140+
if ((dir = opendir(d.c_str())) != NULL) {
141+
while ((ent = readdir (dir)) != NULL) {
142+
if(ent->d_type == DT_REG) {
143+
Serial.print(" - [File]: ");
144+
}
145+
else if(ent->d_type == DT_DIR) {
146+
Serial.print(" - [Fold]: ");
147+
dir_list.push_back(ent->d_name);
148+
}
149+
Serial.println(ent->d_name);
150+
}
151+
closedir (dir);
152+
}
153+
else {
154+
Serial.print("ERROR OPENING SUB-FOLDER ");
155+
Serial.println(d.c_str());
156+
}
157+
}
158+
159+
/*
160+
* CREATING TEST FOLDER (if does not exist already)
161+
*/
162+
163+
err = 0;
164+
if(!found_test_folder) {
165+
Serial.println("TEST FOLDER NOT FOUND... creating folder test");
166+
err = mkdir(folder_test_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
167+
if(err != 0) {
168+
Serial.print("FAILED folder creation with error ");
169+
Serial.println(err);
170+
}
171+
}
172+
173+
/*
174+
* READING TEST FILE CONTENT
175+
*/
176+
177+
if(err == 0) {
178+
int file_dimension = 0;
179+
FILE* fp = fopen(file_test_name.c_str(), "r");
180+
if(fp != NULL) {
181+
Serial.print("Opened file: ");
182+
Serial.print(file_test_name.c_str());
183+
Serial.println(" for reading");
184+
185+
fseek(fp, 0L, SEEK_END);
186+
int numbytes = ftell(fp);
187+
fseek(fp, 0L, SEEK_SET);
188+
189+
Serial.print("Bytes in the file: ");
190+
Serial.println(numbytes);
191+
file_dimension = numbytes;
192+
193+
if(numbytes > 0) {
194+
Serial.println();
195+
Serial.println("-------------------- START FILE CONTENT --------------------");
196+
}
197+
198+
for(int i = 0; i < numbytes; i++) {
199+
char ch;
200+
fread(&ch, sizeof(char), 1, fp);
201+
Serial.print(ch);
202+
}
203+
204+
if(numbytes > 0) {
205+
Serial.println("--------------------- END FILE CONTENT ---------------------");
206+
Serial.println();
207+
}
208+
else {
209+
Serial.println("File is EMPTY!");
210+
Serial.println();
211+
}
212+
213+
fclose(fp);
214+
}
215+
else {
216+
Serial.print("FAILED open file ");
217+
Serial.println(file_test_name.c_str());
218+
}
219+
220+
/*
221+
* DELETE FILE IF THE File dimension is greater than 150 bytes
222+
*/
223+
224+
if(file_dimension > DELETE_FILE_DIMENSION) {
225+
Serial.println("Test file reached the delete dimension... deleting it!");
226+
if(remove(file_test_name.c_str()) == 0) {
227+
Serial.println("TEST FILE HAS BEEN DELETED!");
228+
}
229+
}
230+
231+
/*
232+
* APPENDING SOMETHING TO FILE
233+
*/
234+
235+
fp = fopen(file_test_name.c_str(), "a");
236+
if(fp != NULL) {
237+
Serial.print("Opened file: ");
238+
Serial.print(file_test_name.c_str());
239+
Serial.println(" for writing (append)");
240+
char text[] = "This line has been appended to file!\n";
241+
fwrite(text, sizeof(char), strlen(text), fp);
242+
fclose(fp);
243+
}
244+
else {
245+
Serial.print("FAILED open file for appending ");
246+
Serial.println(file_test_name.c_str());
247+
}
248+
249+
/*
250+
* READING AGAIN FILE CONTENT
251+
*/
252+
253+
fp = fopen(file_test_name.c_str(), "r");
254+
if(fp != NULL) {
255+
Serial.print("Opened file: ");
256+
Serial.print(file_test_name.c_str());
257+
Serial.println(" for reading");
258+
259+
fseek(fp, 0L, SEEK_END);
260+
int numbytes = ftell(fp);
261+
fseek(fp, 0L, SEEK_SET);
262+
263+
Serial.print("Bytes in the file: ");
264+
Serial.println(numbytes);
265+
266+
if(numbytes > 0) {
267+
Serial.println();
268+
Serial.println("-------------------- START FILE CONTENT --------------------");
269+
}
270+
271+
for(int i = 0; i < numbytes; i++) {
272+
char ch;
273+
fread(&ch, sizeof(char), 1, fp);
274+
Serial.print(ch);
275+
}
276+
277+
if(numbytes > 0) {
278+
Serial.println("--------------------- END FILE CONTENT ---------------------");
279+
Serial.println();
280+
}
281+
else {
282+
Serial.println("File is EMPTY!");
283+
Serial.println();
284+
}
285+
286+
fclose(fp);
287+
288+
}
289+
else {
290+
Serial.print("FAILED open file for appending ");
291+
Serial.println(file_test_name.c_str());
292+
}
293+
}
294+
if(dirIndex == 0) {
295+
Serial.println("Empty SDCARD");
296+
}
297+
}
298+
299+
void loop() {
300+
// Empty
301+
}

0 commit comments

Comments
 (0)