Skip to content

Commit 722bf6e

Browse files
fixed bug on first-time internal storage usage
1 parent 2ea7e0c commit 722bf6e

File tree

7 files changed

+261
-210
lines changed

7 files changed

+261
-210
lines changed

examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
*/
2525

26-
#define UNIFIED_STORAGE_DEBUG
26+
#define ARDUINO_UNIFIED_STORAGE_DEBUG
2727
#include "Arduino_UnifiedStorage.h"
2828

2929
// Two instances are made for the USB and internal storage respectively

examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@
2727
2828
*/
2929

30+
3031
#include "Arduino_UnifiedStorage.h"
3132

33+
// Redirect Serial.print*() output to GDB instead of SerialUSB where it would conflict with ThreadDebug.
34+
// NOTE: Calls to Serial.print*() will block waiting for GDB to be connected so only useful to use this redefine
35+
// when actively debugging the program.
36+
37+
38+
3239
void printFolderContents(Folder dir, int indentation = 0) {
3340
std::vector<Folder> directories = dir.getFolders();
3441
std::vector<UFile> files = dir.getFiles();
@@ -57,15 +64,23 @@ void printFolderContents(Folder dir, int indentation = 0) {
5764
// Uncomment one of the three lines below to select between SD card, USB or internal storage
5865
//SDStorage unifiedStorage = SDStorage(); // Create an instance for interacting with SD card storage
5966
//USBStorage unifiedStorage = USBStorage() // Create an instance for interacting with USB storage
60-
InternalStorage internalStorage = InternalStorage();// Create an instance for interacting with internal Flash storage (default)
67+
InternalStorage internalStorage = InternalStorage();
6168

6269
void setup() {
70+
71+
6372
Serial.begin(115200);
6473
while (!Serial);
6574

75+
76+
77+
internalStorage = InternalStorage();
78+
6679
if(!internalStorage.begin()){
6780
Serial.println("Error mounting storage device.");
6881
}
82+
83+
6984

7085
// Create a root directory in storage device
7186
Folder root = internalStorage.getRootFolder();

extras/tests/TestFolderOperations/TestFolderOperations.ino

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

2-
3-
#include <Arduino_UnifiedStorage.h>
4-
52
#define ARDUINO_UNIFIED_STORAGE_DEBUG
3+
#include <Arduino_UnifiedStorage.h>
64

75
#if defined(HAS_USB)
86
USBStorage usb = USBStorage();

src/InternalStorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ InternalStorage::InternalStorage(){
44
std::vector<Partition> partitionsAvailable = Partitioning::readPartitions(QSPIFBlockDeviceType::get_default_instance());
55
if(partitionsAvailable.size() == 0){
66

7-
debugPrint("[InternalStorage][INFO] No partitions found, restoring default partitions");
7+
//debugPrint("[InternalStorage][INFO] No partitions found, restoring default partitions");
88
restoreDefaultPartitions();
99
} else {
1010
int lastPartitionNumber = partitionsAvailable.size();
1111
FileSystems lastPartitionFileSystem = partitionsAvailable.back().fileSystemType;
12-
debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage");
12+
//debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage");
1313

1414
this -> partitionNumber = lastPartitionNumber;
1515
this -> fileSystemType = lastPartitionFileSystem;

src/Partitioning.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool Partitioning::isPartitionSchemeValid(BlockDeviceType * blockDevice, std::ve
2626
for (size_t i = 1; i < partitions.size() + 1; ++i) {
2727
Partition thisPartition = partitions[i - 1];
2828

29-
if(thisPartition.size % 64 == 0){
29+
if(thisPartition.size < 0 && (thisPartition.size & (thisPartition.size - 1)) != 0){
3030
return false;
3131
}
3232
totalSize += thisPartition.size;
@@ -96,17 +96,21 @@ bool Partitioning::partitionDrive(BlockDeviceType * blockDevice, std::vector<Par
9696
blockDevice -> init();
9797

9898
if(!isPartitionSchemeValid(blockDevice, partitions)){
99+
blockDevice -> deinit();
99100
return false;
100101
}
101102

102103
if(!eraseMBRSector(blockDevice)){
104+
blockDevice -> deinit();
103105
return false;
104106
}
105107

106108
if(!createAndFormatPartitions(blockDevice, partitions)){
109+
blockDevice -> deinit();
107110
return false;
108111
}
109112

113+
blockDevice -> deinit();
110114
return true;
111115
}
112116

0 commit comments

Comments
 (0)