Skip to content

Commit f428e4f

Browse files
added QSPIStorage library
1 parent bac60c2 commit f428e4f

30 files changed

+3985
-520
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2024 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ARDUINO_STORAGE_H
8+
#define ARDUINO_STORAGE_H
9+
10+
/**
11+
* @brief Arduino Storage Library
12+
*
13+
* This library provides a unified interface for all Arduino storage implementations.
14+
* It defines the base classes and error handling that all storage types (QSPI, SD, Flash, etc.)
15+
* conform to.
16+
*
17+
* Include this header to get access to:
18+
* - StorageError: Error handling class
19+
* - File: Abstract base class for file operations
20+
* - Folder: Abstract base class for folder operations
21+
* - FileMode: Enum for file opening modes
22+
* - FilesystemType: Enum for filesystem types
23+
*
24+
* For specific storage implementations, include their respective headers:
25+
* - QSPIStorage.h: QSPI flash storage
26+
* - SDStorage.h: SD card storage
27+
* - FlashStorage.h: Internal flash storage
28+
*/
29+
30+
#include "StorageCommon.h"
31+
#include "StorageError.h"
32+
#include "StorageFile.h"
33+
#include "StorageFolder.h"
34+
35+
#endif // ARDUINO_STORAGE_H
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
zephyr_include_directories(.)
3+
4+
if(NOT DEFINED ARDUINO_BUILD_PATH)
5+
zephyr_sources_ifdef(CONFIG_FILE_SYSTEM
6+
StorageError.cpp
7+
StorageFile.cpp
8+
StorageFolder.cpp
9+
)
10+
endif()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2024 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ARDUINO_STORAGE_COMMON_H
8+
#define ARDUINO_STORAGE_COMMON_H
9+
10+
#include <Arduino.h>
11+
12+
// File opening modes
13+
enum class FileMode {
14+
READ, // Open for reading, file must exist
15+
WRITE, // Open for writing, creates if doesn't exist, truncates if exists
16+
APPEND, // Open for writing at end, creates if doesn't exist
17+
READ_WRITE, // Open for reading and writing, file must exist
18+
READ_WRITE_CREATE // Open for reading and writing, creates if doesn't exist
19+
};
20+
21+
// Supported filesystem types
22+
enum class FilesystemType {
23+
LITTLEFS, // LittleFS - recommended for flash storage
24+
FAT, // FAT32 - better compatibility, larger overhead
25+
EXT2, // Extended 2 - Linux-style filesystem
26+
AUTO // Auto-detect or use default
27+
};
28+
29+
// Storage information structure
30+
struct StorageInfo {
31+
char mountPoint[64];
32+
FilesystemType fsType;
33+
size_t totalBytes;
34+
size_t usedBytes;
35+
size_t availableBytes;
36+
size_t blockSize;
37+
size_t totalBlocks;
38+
size_t usedBlocks;
39+
bool readOnly;
40+
bool mounted;
41+
};
42+
43+
// Storage health structure
44+
struct StorageHealth {
45+
bool healthy; // Overall health status
46+
uint32_t errorCount; // Number of errors encountered
47+
uint32_t badBlocks; // Number of bad blocks (flash)
48+
uint32_t writeCount; // Total write operations
49+
uint32_t eraseCount; // Total erase operations
50+
float fragmentationPercent; // File system fragmentation
51+
char statusMessage[128]; // Human-readable status
52+
};
53+
54+
// Partition information
55+
struct PartitionInfo {
56+
const char* label; // Partition name/label
57+
size_t offset; // Start offset in bytes
58+
size_t size; // Size in bytes
59+
FilesystemType fsType; // File system type for this partition
60+
};
61+
62+
// Maximum path length
63+
constexpr size_t STORAGE_MAX_PATH_LENGTH = 256;
64+
65+
#endif // ARDUINO_STORAGE_COMMON_H
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2024 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "StorageError.h"
8+
#include <cstring>
9+
10+
StorageError::StorageError() : code_(StorageErrorCode::NONE) {
11+
message_[0] = '\0';
12+
}
13+
14+
StorageErrorCode StorageError::getCode() const {
15+
return code_;
16+
}
17+
18+
const char* StorageError::getMessage() const {
19+
if (message_[0] != '\0') {
20+
return message_;
21+
}
22+
23+
// Return default message based on error code
24+
switch (code_) {
25+
case StorageErrorCode::NONE:
26+
return "No error";
27+
case StorageErrorCode::FILE_NOT_FOUND:
28+
return "File not found";
29+
case StorageErrorCode::FOLDER_NOT_FOUND:
30+
return "Folder not found";
31+
case StorageErrorCode::ALREADY_EXISTS:
32+
return "Already exists";
33+
case StorageErrorCode::INVALID_PATH:
34+
return "Invalid path";
35+
case StorageErrorCode::PERMISSION_DENIED:
36+
return "Permission denied";
37+
case StorageErrorCode::READ_ERROR:
38+
return "Read error";
39+
case StorageErrorCode::WRITE_ERROR:
40+
return "Write error";
41+
case StorageErrorCode::SEEK_ERROR:
42+
return "Seek error";
43+
case StorageErrorCode::OPEN_ERROR:
44+
return "Open error";
45+
case StorageErrorCode::CLOSE_ERROR:
46+
return "Close error";
47+
case StorageErrorCode::STORAGE_FULL:
48+
return "Storage full";
49+
case StorageErrorCode::STORAGE_NOT_MOUNTED:
50+
return "Storage not mounted";
51+
case StorageErrorCode::STORAGE_CORRUPTED:
52+
return "Storage corrupted";
53+
case StorageErrorCode::STORAGE_NOT_FORMATTED:
54+
return "Storage not formatted";
55+
case StorageErrorCode::INVALID_OPERATION:
56+
return "Invalid operation";
57+
case StorageErrorCode::INVALID_MODE:
58+
return "Invalid mode";
59+
case StorageErrorCode::BUFFER_OVERFLOW:
60+
return "Buffer overflow";
61+
case StorageErrorCode::OUT_OF_MEMORY:
62+
return "Out of memory";
63+
case StorageErrorCode::TIMEOUT:
64+
return "Timeout";
65+
case StorageErrorCode::HARDWARE_ERROR:
66+
return "Hardware error";
67+
case StorageErrorCode::NOT_INITIALIZED:
68+
return "Not initialized";
69+
case StorageErrorCode::UNKNOWN_ERROR:
70+
default:
71+
return "Unknown error";
72+
}
73+
}
74+
75+
bool StorageError::hasError() const {
76+
return code_ != StorageErrorCode::NONE;
77+
}
78+
79+
void StorageError::setError(StorageErrorCode code, const char* message) {
80+
code_ = code;
81+
if (message != nullptr) {
82+
strncpy(message_, message, sizeof(message_) - 1);
83+
message_[sizeof(message_) - 1] = '\0';
84+
} else {
85+
message_[0] = '\0';
86+
}
87+
}
88+
89+
void StorageError::clear() {
90+
code_ = StorageErrorCode::NONE;
91+
message_[0] = '\0';
92+
}
93+
94+
StorageError::operator bool() const {
95+
return hasError();
96+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2024 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ARDUINO_STORAGE_ERROR_H
8+
#define ARDUINO_STORAGE_ERROR_H
9+
10+
#include <Arduino.h>
11+
12+
enum class StorageErrorCode {
13+
NONE = 0,
14+
15+
// File/Folder errors
16+
FILE_NOT_FOUND,
17+
FOLDER_NOT_FOUND,
18+
ALREADY_EXISTS,
19+
INVALID_PATH,
20+
PERMISSION_DENIED,
21+
22+
// I/O errors
23+
READ_ERROR,
24+
WRITE_ERROR,
25+
SEEK_ERROR,
26+
OPEN_ERROR,
27+
CLOSE_ERROR,
28+
29+
// Storage errors
30+
STORAGE_FULL,
31+
STORAGE_NOT_MOUNTED,
32+
STORAGE_CORRUPTED,
33+
STORAGE_NOT_FORMATTED,
34+
35+
// Operation errors
36+
INVALID_OPERATION,
37+
INVALID_MODE,
38+
BUFFER_OVERFLOW,
39+
OUT_OF_MEMORY,
40+
TIMEOUT,
41+
42+
// Hardware errors
43+
HARDWARE_ERROR,
44+
NOT_INITIALIZED,
45+
46+
// Generic
47+
UNKNOWN_ERROR
48+
};
49+
50+
class StorageError {
51+
public:
52+
StorageError();
53+
54+
// Error state
55+
StorageErrorCode getCode() const;
56+
const char* getMessage() const;
57+
bool hasError() const;
58+
59+
// Error setting (for implementations)
60+
void setError(StorageErrorCode code, const char* message = nullptr);
61+
void clear();
62+
63+
// Convenience operators
64+
operator bool() const; // Returns true if error exists
65+
66+
private:
67+
StorageErrorCode code_;
68+
char message_[128];
69+
};
70+
71+
#endif // ARDUINO_STORAGE_ERROR_H
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2024 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "StorageFile.h"
8+
#include <cstring>
9+
10+
File::File() {
11+
path_[0] = '\0';
12+
}
13+
14+
File::File(const char* path) {
15+
if (path != nullptr) {
16+
strncpy(path_, path, sizeof(path_) - 1);
17+
path_[sizeof(path_) - 1] = '\0';
18+
} else {
19+
path_[0] = '\0';
20+
}
21+
}
22+
23+
File::File(const String& path) {
24+
strncpy(path_, path.c_str(), sizeof(path_) - 1);
25+
path_[sizeof(path_) - 1] = '\0';
26+
}
27+
28+
File::~File() {
29+
}
30+
31+
const char* File::getPath() const {
32+
return path_;
33+
}
34+
35+
String File::getPathAsString() const {
36+
return String(path_);
37+
}
38+
39+
String File::getFilename() const {
40+
const char* lastSep = strrchr(path_, '/');
41+
if (lastSep != nullptr) {
42+
return String(lastSep + 1);
43+
}
44+
return String(path_);
45+
}
46+
47+
void File::setErrorIfNotNull(StorageError* error, StorageErrorCode code, const char* message) {
48+
if (error != nullptr) {
49+
error->setError(code, message);
50+
}
51+
}

0 commit comments

Comments
 (0)