Skip to content

Commit 2e3f3d6

Browse files
authored
Merge pull request #2 from manchoz/mnx_portenta_ota
Add Portenta OTA examples
2 parents 13c5129 + 8a01191 commit 2e3f3d6

File tree

8 files changed

+466
-1
lines changed

8 files changed

+466
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

examples/DirList/DirList.ino

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 <DigitalOut.h>
15+
#include <FATFileSystem.h>
16+
#include <USBHostMbed5.h>
17+
18+
USBHostMSD msd;
19+
mbed::FATFileSystem usb("usb");
20+
21+
mbed::DigitalOut pin5(PC_6, 0);
22+
23+
// If you are using a Portenta Machine Control uncomment the following line
24+
mbed::DigitalOut otg(PB_14, 0);
25+
26+
#define BUFFER_MAX_LEN 64
27+
void setup()
28+
{
29+
Serial.begin(115200);
30+
while (!Serial)
31+
;
32+
33+
delay(2500);
34+
Serial.println("Starting USB Dir List example...");
35+
36+
// if you are using a Max Carrier uncomment the following line
37+
//start_hub();
38+
39+
while (!msd.connect()) {
40+
//while (!port.connected()) {
41+
delay(1000);
42+
}
43+
44+
Serial.print("Mounting USB device... ");
45+
int err = usb.mount(&msd);
46+
if (err) {
47+
Serial.print("Error mounting USB device ");
48+
Serial.println(err);
49+
while (1)
50+
;
51+
}
52+
Serial.println("done.");
53+
54+
char buf[256] {};
55+
// Display the root directory
56+
Serial.print("Opening the root directory... ");
57+
DIR* d = opendir("/usb/");
58+
Serial.println(!d ? "Fail :(" : "Done");
59+
if (!d) {
60+
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
61+
Serial.print(buf);
62+
}
63+
Serial.println("done.");
64+
65+
Serial.println("Root directory:");
66+
unsigned int count { 0 };
67+
while (true) {
68+
struct dirent* e = readdir(d);
69+
if (!e) {
70+
break;
71+
}
72+
count++;
73+
snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
74+
Serial.print(buf);
75+
}
76+
Serial.print(count);
77+
Serial.println(" files found!");
78+
79+
snprintf(buf, sizeof(buf), "Closing the root directory... ");
80+
Serial.print(buf);
81+
fflush(stdout);
82+
err = closedir(d);
83+
snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
84+
Serial.print(buf);
85+
if (err < 0) {
86+
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
87+
Serial.print(buf);
88+
}
89+
}
90+
91+
void loop()
92+
{
93+
}

examples/FileRead/FileRead.ino

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 <DigitalOut.h>
16+
#include <FATFileSystem.h>
17+
18+
USBHostMSD msd;
19+
mbed::FATFileSystem usb("usb");
20+
21+
mbed::DigitalOut pin5(PC_6, 0);
22+
23+
// If you are using a Portenta Machine Control uncomment the following line
24+
mbed::DigitalOut otg(PB_14, 0);
25+
26+
#define BUFFER_MAX_LEN 64
27+
void setup() {
28+
Serial.begin(115200);
29+
while (!Serial);
30+
31+
delay(2500);
32+
Serial.println("Starting USB File Read example...");
33+
34+
// if you are using a Max Carrier uncomment the following line
35+
//start_hub();
36+
37+
while (!msd.connect()) {
38+
delay(1000);
39+
}
40+
41+
Serial.println("Mounting USB device...");
42+
int err = usb.mount(&msd);
43+
if (err) {
44+
Serial.print("Error mounting USB device ");
45+
Serial.println(err);
46+
while (1);
47+
}
48+
Serial.print("read done ");
49+
mbed::fs_file_t file;
50+
struct dirent *ent;
51+
int dirIndex = 0;
52+
int res = 0;
53+
Serial.println("Open file..");
54+
FILE *f = fopen("/usb/Arduino.txt", "r+");
55+
char buf[256];
56+
Serial.println("File content:");
57+
58+
while (fgets(buf, 256, f) != NULL) {
59+
Serial.print(buf);
60+
}
61+
62+
Serial.println("File closing");
63+
fflush(stdout);
64+
err = fclose(f);
65+
if (err < 0) {
66+
Serial.print("fclose error:");
67+
Serial.print(strerror(errno));
68+
Serial.print(" (");
69+
Serial.print(-errno);
70+
Serial.print(")");
71+
} else {
72+
Serial.println("File closed");
73+
}
74+
}
75+
76+
void loop() {
77+
78+
}

examples/FileWrite/FileWrite.ino

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

0 commit comments

Comments
 (0)