Skip to content

Commit a883a0a

Browse files
committed
added examples to manage the file writing and readings
1 parent 7942f46 commit a883a0a

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

examples/FileRead/FileRead.ino

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Portenta - FileRead
3+
4+
The sketch shows how to mount an usb storage device and how to
5+
read from an existing file.
6+
to use this sketch create a .txt file named Arduino.txt,
7+
in your storage device and write some content inside.
8+
9+
The circuit:
10+
- Portenta H7
11+
12+
This example code is in the public domain.
13+
*/
14+
#include "USBHostMbed5.h"
15+
#include "USBHostMSD/USBHostMSD.h"
16+
#include "USBHostHub/USBHostHub.h"
17+
#include "USBHostSerial/USBHostSerial.h"
18+
#include "DigitalOut.h"
19+
#include "FATFileSystem.h"
20+
21+
USBHostMSD msd;
22+
mbed::FATFileSystem fs("fs");
23+
24+
mbed::DigitalOut pin5(PC_6, 0);
25+
26+
// If you are using a Portenta Machine Control uncomment the following line
27+
//mbed::DigitalOut otg(PB_8, 1);
28+
29+
#define BUFFER_MAX_LEN 64
30+
void setup() {
31+
Serial.begin(115200);
32+
while (!Serial);
33+
34+
// if you are using a Max Carrier uncomment the following line
35+
//start_hub();
36+
37+
while (!msd.connect()) {
38+
//while (!port.connected()) {
39+
delay(1000);
40+
}
41+
42+
Serial.println("Mounting USB device...");
43+
int err = fs.mount(&msd);
44+
if (err) {
45+
Serial.print("Error mounting USB device ");
46+
Serial.println(err);
47+
while (1);
48+
}
49+
Serial.print("read done ");
50+
mbed::fs_file_t file;
51+
struct dirent *ent;
52+
int dirIndex = 0;
53+
int res = 0;
54+
Serial.println("Open file..");
55+
FILE *f = fopen("/fs/Arduino.txt", "r+");
56+
char buf[2];
57+
Serial.println("File conntet:");
58+
59+
while (fgets(buf, 2, f) != NULL) {
60+
Serial.println(buf);
61+
}
62+
63+
Serial.println("File closing");
64+
fflush(stdout);
65+
err = fclose(f);
66+
if (err < 0) {
67+
Serial.print("fclose error:");
68+
Serial.print(strerror(errno));
69+
Serial.print(" (");
70+
Serial.print(-errno);
71+
Serial.print(")");
72+
} else {
73+
Serial.println("File closed");
74+
}
75+
}
76+
77+
void loop() {
78+
79+
}

examples/FileWrite/FileWrite.ino

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "USBHostMbed5.h"
2+
#include "USBHostMSD/USBHostMSD.h"
3+
#include "USBHostHub/USBHostHub.h"
4+
#include "USBHostSerial/USBHostSerial.h"
5+
#include "DigitalOut.h"
6+
#include "FATFileSystem.h"
7+
8+
USBHostMSD msd;
9+
mbed::FATFileSystem fs("fs");
10+
11+
mbed::DigitalOut pin5(PC_6, 0);
12+
mbed::DigitalOut otg(PB_8, 1);
13+
14+
void setup() {
15+
Serial.begin(115200);
16+
while (!Serial);
17+
msd.connect();
18+
19+
while (!msd.connected()) {
20+
//while (!port.connected()) {
21+
delay(1000);
22+
}
23+
24+
Serial.println("Mounting USB device...");
25+
int err = fs.mount(&msd);
26+
if (err) {
27+
Serial.print("Error mounting USB device ");
28+
Serial.println(err);
29+
while (1);
30+
}
31+
Serial.print("read done ");
32+
mbed::fs_file_t file;
33+
struct dirent *ent;
34+
int dirIndex = 0;
35+
int res = 0;
36+
Serial.println("Open /fs/numbers.txt");
37+
FILE *f = fopen("/fs/numbers.txt", "w+");
38+
for (int i = 0; i < 10; i++) {
39+
Serial.print("Writing numbers (");
40+
Serial.print(i);
41+
Serial.println("/10)");
42+
fflush(stdout);
43+
err = fprintf(f, "%d\n", i);
44+
if (err < 0) {
45+
Serial.println("Fail :(");
46+
error("error: %s (%d)\n", strerror(errno), -errno);
47+
}
48+
}
49+
50+
Serial.println("File closing");
51+
fflush(stdout);
52+
err = fclose(f);
53+
if (err < 0) {
54+
Serial.print("fclose error:");
55+
Serial.print(strerror(errno));
56+
Serial.print(" (");
57+
Serial.print(-errno);
58+
Serial.print(")");
59+
} else {
60+
Serial.println("File closed");
61+
}
62+
}
63+
64+
void loop() {
65+
66+
}

0 commit comments

Comments
 (0)