Skip to content

Commit a27983a

Browse files
added BackupInternalPartitions example
1 parent f126620 commit a27983a

File tree

18 files changed

+206
-136
lines changed

18 files changed

+206
-136
lines changed

examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
USBStorage usbStorage = USBStorage();
3030
InternalStorage internalStorage = InternalStorage(2, "user", FS_FAT);
3131

32+
3233
// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
3334
void printFolderContents(Folder dir, int indentation = 0) {
3435
std::vector<Folder> directories = dir.getFolders();
@@ -61,16 +62,14 @@ void setup() {
6162
while (!Serial);
6263

6364
// Mount the USB storage
64-
if(usbStorage.begin()){
65+
if(usbStorage.begin(FS_FAT)){
6566
Serial.println("USB storage mounted.");
6667
} else {
6768
Serial.println(errno);
6869
}
6970

70-
// Mount the internal storage
71-
// Serial.println("Reformatting internal storage to make sure we have a clean FS");
72-
// internalStorage.format();
73-
if(internalStorage.begin()){
71+
72+
if(internalStorage.begin(FS_FAT)){
7473
Serial.println("Internal storage mounted.");
7574
} else {
7675
Serial.println(errno);
@@ -88,7 +87,7 @@ void setup() {
8887
file.close();
8988

9089
// Copy the file from internal storage to USB storage
91-
bool success = file.copyTo(usbStorage.getRootFolder());
90+
bool success = file.copyTo(usbStorage.getRootFolder(), true);
9291
if (success) {
9392
Serial.println("File copied successfully from internal storage to USB storage.");
9493
} else {
@@ -97,7 +96,7 @@ void setup() {
9796
}
9897

9998
// Move the subdirectory from internal storage to USB storage
100-
success = subdir.moveTo(usbStorage.getRootFolder());
99+
success = subdir.moveTo(usbStorage.getRootFolder(), true);
101100
if (success) {
102101
Serial.println("Subdirectory moved successfully from internal storage to USB storage.");
103102
} else {
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
3+
#include <Arduino_UnifiedStorage.h>
4+
5+
6+
#if defined(ARDUINO_PORTENTA_C33)
7+
InternalStorage ota = InternalStorage(1, "ota", FS_FAT);
8+
InternalStorage data = InternalStorage(2, "data", FS_FAT);
9+
10+
#elif defined(ARDUINO_PORTENTA_H7_M7)
11+
InternalStorage wifi = InternalStorage(1, "wifi", FS_FAT);
12+
InternalStorage ota = InternalStorage(2, "ota", FS_FAT);
13+
InternalStorage data = InternalStorage(3, "data", FS_FAT);
14+
#endif
15+
16+
USBStorage thumbDrive = USBStorage();
17+
18+
19+
void addSomeFakeFiles(Folder * folder){
20+
Serial.println("* adding some fake files to: " + String(folder -> getPathString()));
21+
22+
for (int i = 0; i < random(0, 9); i++){
23+
UFile thisFile = folder -> createFile("File_"+ String(random(999)), FileMode::WRITE);
24+
Serial.println("\t * " + thisFile.getPathString());
25+
thisFile.write("writing stuff to the file");
26+
thisFile.close();
27+
}
28+
29+
30+
Folder subfolder = folder -> createSubfolder("ChildFolder_"+ String(random(999)));
31+
for (int i = 0; i < random(0, 9); i++){
32+
UFile thisFile = subfolder.createFile("File_"+ String(random(999)), FileMode::WRITE);
33+
Serial.println("\t * " + thisFile.getPathString());
34+
thisFile.write("writing stuff to the file");
35+
thisFile.close();
36+
}
37+
}
38+
39+
void move(Folder * source, Folder * dest){
40+
for(Folder f: source -> getFolders()){
41+
Serial.println("* copying folder :" + String(f.getPathString()));
42+
f.moveTo(*dest);
43+
}
44+
45+
for(UFile f: source -> getFiles()){
46+
Serial.println("* copying file :" + String(f.getPathString()));
47+
f.moveTo(*dest);
48+
}
49+
}
50+
51+
52+
#if defined(ARDUINO_PORTENTA_C33)
53+
void backupPartitionsC33(Folder * backupFolder){
54+
55+
Serial.println("* backup location: " + String(backupFolder -> getPathString()));
56+
57+
int otaMounted = ota.begin(FS_FAT);
58+
Serial.println("* ota partition mount: " + String(otaMounted));
59+
60+
int dataMounted = data.begin(FS_FAT);
61+
Serial.println("* data partition mount: " + String(dataMounted));
62+
63+
64+
if(otaMounted == 1){
65+
Folder otaRoot = ota.getRootFolder();
66+
addSomeFakeFiles(&otaRoot);
67+
Folder otaFolder = backupFolder -> createSubfolder("ota");
68+
move(&otaRoot, &otaFolder);
69+
ota.unmount();
70+
} else {
71+
Serial.println("OTA partition not mounted, cannot proceed");
72+
}
73+
74+
75+
if(dataMounted == 1){
76+
Folder dataRoot = data.getRootFolder();
77+
addSomeFakeFiles(&dataRoot);
78+
Folder dataFolder = backupFolder -> createSubfolder("data");
79+
move(&dataRoot, &dataFolder);
80+
data.unmount();
81+
} else {
82+
Serial.println("Data partition not mounted, cannot proceed");
83+
}
84+
}
85+
#endif
86+
87+
88+
#if defined(ARDUINO_PORTENTA_H7_M7)
89+
void backupPartitionsH7(Folder * backupFolder){
90+
Serial.println("* backup location: " + String(backupFolder -> getPathString()));
91+
92+
int wifiMounted = wifi.begin(FS_FAT);
93+
Serial.println("* wifi partition mount: " + String(wifiMounted));
94+
95+
int otaMounted = ota.begin(FS_FAT);
96+
Serial.println("* ota partition mount: " + String(otaMounted));
97+
98+
int dataMounted = data.begin(FS_FAT);
99+
Serial.println("* data partition mounted: " + String(dataMounted));
100+
101+
if(wifiMounted == 1){
102+
Folder wifiRoot = wifi.getRootFolder();
103+
addSomeFakeFiles(&wifiRoot);
104+
Folder wifiFolder = backupFolder -> createSubfolder("wifi");
105+
move(&wifiRoot, &wifiFolder);
106+
wifi.unmount();
107+
} else {
108+
Serial.println("WiFi partition not mounted, cannot proceed");
109+
}
110+
111+
if(otaMounted == 1){
112+
Folder otaRoot = ota.getRootFolder();
113+
addSomeFakeFiles(&otaRoot);
114+
Folder otaFolder = backupFolder -> createSubfolder("ota");
115+
move(&otaRoot, &otaFolder);
116+
ota.unmount();
117+
} else {
118+
Serial.println("OTA partition not mounted, cannot proceed");
119+
}
120+
121+
122+
if(dataMounted == 1){
123+
Folder dataRoot = data.getRootFolder();
124+
addSomeFakeFiles(&dataRoot);
125+
Folder dataFolder = backupFolder -> createSubfolder("data");
126+
move(&dataRoot, &dataFolder);
127+
data.unmount();
128+
} else {
129+
Serial.println("Data partition not mounted, cannot proceed");
130+
}
131+
}
132+
#endif
133+
134+
void setup(){
135+
randomSeed(analogRead(A0));
136+
137+
138+
Serial.begin(115200);
139+
while(!Serial);
140+
141+
int thumbMounted = thumbDrive.begin(FS_FAT);
142+
Serial.println("* usb drive mounted:" + String(thumbMounted));
143+
144+
145+
Folder thumbRoot = thumbDrive.getRootFolder();
146+
String folderName = "InternalBackup_" + String(millis());
147+
Folder backupFolder = thumbRoot.createSubfolder(folderName);
148+
149+
150+
#if defined(ARDUINO_PORTENTA_H7_M7)
151+
backupPartitionsH7(&backupFolder);
152+
#elif defined(ARDUINO_PORTENTA_C33)
153+
backupPartitionsC33(&backupFolder);
154+
#endif
155+
156+
thumbDrive.unmount();
157+
158+
Serial.println("DONE, you can restart the board now");
159+
160+
}
161+
162+
163+
void loop(){
164+
165+
}

examples/PoftentaH7Logger/PortentaH7Logger.ino

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,7 @@ void setup() {
157157
while (!Serial);
158158
pinMode(USB_MOUNTED_LED, OUTPUT);
159159
Serial.println("Formatting internal storage...");
160-
internalStorage.begin();
161-
internalStorage.unmount();
162-
int formatted = internalStorage.format();
163-
internalStorage.begin();
164-
160+
int formatted = internalStorage.formatFAT();
165161
Serial.print("QSPI Format status: "); Serial.println(formatted);
166162

167163
//configureRS485(baudrate);

examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void setup() {
5757
Serial.begin(115200);
5858
while (!Serial);
5959

60-
if(!internalStorage.begin()){
60+
if(!internalStorage.begin(FS_FAT)){
6161
Serial.println("Error mounting storage device.");
6262
}
6363

examples/advanced/advanced.ino

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/Arduino_UnifiedStorage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Arduino_UnifiedStorage {
2929
public:
3030
virtual int begin() = 0;
3131

32+
virtual int begin(FileSystems fs) = 0;
33+
3234
virtual int unmount() = 0;
3335

3436
virtual Folder getRootFolder() = 0;

src/InternalStorage.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ InternalStorage::InternalStorage(){
66
this -> setQSPIPartitionName("user");
77
}
88

9-
InternalStorage::InternalStorage(int partition, const char * name, uint8_t fs){
9+
InternalStorage::InternalStorage(int partition, const char * name, FileSystems fs){
1010
this -> setQSPIPartition(partition);
1111
this -> setQSPIPartitionName(name);
1212
this -> fs = fs;
1313
}
1414

15+
int InternalStorage::begin(FileSystems fs){
16+
this -> fs = fs;
17+
this -> begin();
18+
}
1519

1620
int InternalStorage::begin(){
1721
#if defined(ARDUINO_PORTENTA_C33)

src/InternalStorage.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class InternalStorage : public Arduino_UnifiedStorage {
1414
public:
1515
InternalStorage();
1616
// Override begin() method for SD card initialization
17-
InternalStorage(int partition, const char * name, uint8_t fs);
17+
InternalStorage(int partition, const char * name, FileSystems fs);
1818

1919
int begin() override;
2020

21+
int begin(FileSystems fs) override;
22+
2123
int unmount() override;
2224

2325
Folder getRootFolder() override;
@@ -51,7 +53,7 @@ class InternalStorage : public Arduino_UnifiedStorage {
5153

5254
int partitionNumber = 2;
5355
char * partitionName = "user";
54-
int fs = FS_FAT;
56+
FileSystems fs = FS_FAT;
5557
};
5658

5759
#endif

0 commit comments

Comments
 (0)