Skip to content

Commit 672af73

Browse files
author
Alrik Vidstrom
committed
Initial commit
0 parents  commit 672af73

File tree

11 files changed

+2190
-0
lines changed

11 files changed

+2190
-0
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
# macOS
35+
.DS_Store
36+

LICENSE

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Arduino POSIXStorage Library
2+
3+
The Arduino POSIXStorage Library complements the POSIX storage functions already included in the Renesas core and the Arduino_USBHostMbed5 library, and makes them available to sketches.
4+
5+
For more information about this library please read the documentation [here](./docs/).

docs/README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Arduino POSIXStorage Library
2+
3+
The Arduino POSIXStorage Library complements the POSIX storage functions already included in the Renesas core and the Arduino_USBHostMbed5 library. It also makes them available to use in sketches.
4+
5+
The library supports and is tested on:
6+
- Portenta C33 with Portenta Breakout Board (SD Card and USB Thumb Drive)
7+
- Portenta C33 with Portenta Vision Shield (SD Card)
8+
- Portenta H7 with Portenta Breakout Board (SD Card and USB Thumb Drive)
9+
- Portenta H7 with Portenta Vision Shield (SD Card)
10+
- Portenta Machine Control (USB Thumb Drive)
11+
12+
13+
## Usage
14+
15+
For detailed information on usage, see the API and Examples sections below. This is a very basic example of how to include and use the library:
16+
17+
```cpp
18+
#include "POSIXStorage.h"
19+
20+
void setup() {
21+
mount(DEV_SDCARD, FS_FAT, MNT_DEFAULT);
22+
// ...
23+
// Use various POSIX storage functions.
24+
// ...
25+
umount(DEV_SDCARD);
26+
}
27+
28+
void loop() {
29+
}
30+
```
31+
32+
It is necessary to install the Arduino_USBHostMbed5 library to use POSIXStorage on Portenta H7 and Portenta Machine Control. No additional library is needed on Portenta C33.
33+
34+
The library automatically detects different types of Portenta H7 / Portenta Machine Control boards. This detection should work in the absolute majority of cases, but if you have trouble with USB on the Portenta Machine control you can try to add #define AUTOMATIC_OVERRIDE_PORTENTA_MACHINE_CONTROL just before #include "POSIXStorage.h". The automatic detection should work even with custom boards, but if you have trouble with USB on a custom board, try to add #define AUTOMATIC_OVERRIDE_PORTENTA_H7 in a similar manner.
35+
36+
## API
37+
38+
The following POSIX functions are not a part of the library but are made available and work more or less according to the specification: close, closedir, fcntl, fsync, fstat, ftruncate, isatty, lseek, mkdir, open, opendir, poll, read, remove, rewinddir, seekdir, stat, statvfs, telldir, and write.
39+
40+
Many ISO C standard functions are also made available. For example: fopen, fprintf, and fclose.
41+
42+
Start pathnames on SD Cards with "/sdcard/" and pathnames on USB thumb drives with "/usb/". See the Examples section below for examples.
43+
44+
The following are additional functions provided by the library:
45+
46+
### `int` [`mount`](#)`(enum DeviceNames deviceName, enum FileSystems fileSystem, enum MountFlags mountFlags)`
47+
48+
Attach a file system to a device.
49+
50+
#### Parameters
51+
* `deviceName` The device to attach to: DEV_SDCARD or DEV_USB.
52+
* `fileSystem` The file system type to attach: FS_FAT or FS_LITTLEFS.
53+
* `mountFlags` The only valid flag at this time: MNT_DEFAULT (future platforms might also support MNT_RDONLY).
54+
55+
#### Returns
56+
On success: 0. On failure: -1 with an error code in the errno variable.
57+
58+
59+
### `int` [`umount`](#)`(enum DeviceNames deviceName)`
60+
61+
Remove the attached file system from a device.
62+
63+
#### Parameters
64+
* `deviceName` The device to remove from: DEV_SDCARD or DEV_USB.
65+
66+
#### Returns
67+
On success: 0. On failure: -1 with an error code in the errno variable.
68+
69+
70+
### `int` [`register_hotplug_callback`](#)`(enum DeviceNames deviceName, void (*callbackFunction)())`
71+
72+
Register a hotplug callback function. Currently only supported for DEV_USB on Portenta C33.
73+
74+
#### Parameters
75+
* `deviceName` The device to register for: DEV_SDCARD or DEV_USB.
76+
* `callbackFunction` A function pointer to the callback.
77+
78+
#### Returns
79+
On success: 0. On failure: -1 with an error code in the errno variable.
80+
81+
82+
### `int` [`deregister_hotplug_callback`](#)`(enum DeviceNames deviceName)`
83+
84+
Deregister a previously registered hotplug callback function. Not currently supported on any platform.
85+
86+
#### Parameters
87+
* `deviceName` The device to deregister for: DEV_SDCARD or DEV_USB.
88+
89+
#### Returns
90+
On success: 0. On failure: -1 with an error code in the errno variable.
91+
92+
93+
### `int` [`mkfs`](#)`(enum DeviceNames deviceName, enum FileSystems fileSystem)`
94+
95+
Format a device (make file system).
96+
97+
#### Parameters
98+
* `deviceName` The device to format: DEV_SDCARD or DEV_USB.
99+
* `fileSystem` The file system type to format: FS_FAT or FS_LITTLEFS. FS_FAT is probably the better choice for both SD Cards and USB thumb drives in most cases.
100+
101+
#### Returns
102+
On success: 0. On failure: -1 with an error code in the errno variable.
103+
104+
105+
## Examples
106+
107+
- **SD_Card_Example:** This example shows how to mount an SD Card and write to and read from a file.
108+
- **USB_No_Hotplug_Example:** This example shows how to mount a USB thumb drive, without hotplug registration, and write to and read from a file.
109+
- **USB_Hotplug_Example:** This example shows how to mount a USB thumb drive, with hotplug registration, and write to and read from a file.
110+
111+
## License
112+
113+
This library is released under the [LGPLv2.1 license](https://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html).
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This example shows how to mount an SD Card and write to and read from a file
3+
*
4+
* Original author: A. Vidstrom
5+
*/
6+
7+
#include "POSIXStorage.h"
8+
9+
void setup() {
10+
Serial.begin(9600);
11+
while (!Serial) ;
12+
13+
FILE *fp;
14+
if (0 == mount(DEV_SDCARD, FS_FAT, MNT_DEFAULT))
15+
{
16+
fp = fopen("/sdcard/testfile.txt", "w");
17+
if (nullptr != fp)
18+
{
19+
if (fprintf(fp, "Hello, SD Card world") < 0)
20+
{
21+
Serial.println("Error writing to file");
22+
Serial.println(errno);
23+
}
24+
}
25+
else
26+
{
27+
Serial.println("Error opening file for writing");
28+
Serial.println(errno);
29+
}
30+
fclose(fp);
31+
}
32+
else
33+
{
34+
Serial.println("Error mounting SD Card");
35+
Serial.println(errno);
36+
}
37+
umount(DEV_SDCARD);
38+
if (0 == mount(DEV_SDCARD, FS_FAT, MNT_DEFAULT))
39+
{
40+
fp = fopen("/sdcard/testfile.txt", "r");
41+
if (nullptr != fp)
42+
{
43+
constexpr int bufferSize = 25;
44+
char buffer[bufferSize];
45+
if (NULL != fgets(buffer, bufferSize, fp))
46+
{
47+
Serial.println(buffer);
48+
}
49+
else
50+
{
51+
Serial.println("Error reading from file");
52+
Serial.println(errno);
53+
}
54+
}
55+
else
56+
{
57+
Serial.println("Error opening file for reading");
58+
Serial.println(errno);
59+
}
60+
fclose(fp);
61+
}
62+
else
63+
{
64+
Serial.println("Error mounting SD Card");
65+
Serial.println(errno);
66+
}
67+
umount(DEV_SDCARD);
68+
}
69+
70+
void loop() {
71+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This example shows how to mount a USB thumb drive, with hotplug registration, and write to and read from a file
3+
*
4+
* Original author: A. Vidstrom
5+
*/
6+
7+
#include "POSIXStorage.h"
8+
9+
volatile bool usbAttached = false;
10+
11+
void usbCallback()
12+
{
13+
usbAttached = true;
14+
}
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
while (!Serial) ;
19+
20+
if (-1 == register_hotplug_callback(DEV_USB, usbCallback))
21+
{
22+
if (ENOTSUP == errno)
23+
{
24+
Serial.println("Hotplug registration isn't supported on this board");
25+
for ( ; ; ) ;
26+
}
27+
}
28+
Serial.println("Please insert a thumb drive");
29+
while (false == usbAttached) {
30+
delay(500);
31+
}
32+
Serial.println("Thank you!");
33+
34+
FILE *fp;
35+
if (0 == mount(DEV_USB, FS_FAT, MNT_DEFAULT))
36+
{
37+
fp = fopen("/usb/testfile.txt", "w");
38+
if (nullptr != fp)
39+
{
40+
if (fprintf(fp, "Hello, USB thumb drive world") < 0)
41+
{
42+
Serial.println("Error writing to file");
43+
Serial.println(errno);
44+
}
45+
}
46+
else
47+
{
48+
Serial.println("Error opening file for writing");
49+
Serial.println(errno);
50+
}
51+
fclose(fp);
52+
}
53+
else
54+
{
55+
Serial.println("Error mounting USB thumb drive");
56+
Serial.println(errno);
57+
}
58+
umount(DEV_USB);
59+
if (0 == mount(DEV_USB, FS_FAT, MNT_DEFAULT))
60+
{
61+
fp = fopen("/usb/testfile.txt", "r");
62+
if (nullptr != fp)
63+
{
64+
constexpr int bufferSize = 30;
65+
char buffer[bufferSize];
66+
if (NULL != fgets(buffer, bufferSize, fp))
67+
{
68+
Serial.println(buffer);
69+
}
70+
else
71+
{
72+
Serial.println("Error reading from file");
73+
Serial.println(errno);
74+
}
75+
}
76+
else
77+
{
78+
Serial.println("Error opening file for reading");
79+
Serial.println(errno);
80+
}
81+
fclose(fp);
82+
}
83+
else
84+
{
85+
Serial.println("Error mounting USB thumb drive");
86+
Serial.println(errno);
87+
}
88+
umount(DEV_USB);
89+
}
90+
91+
void loop() {
92+
}

0 commit comments

Comments
 (0)