-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstorage.cc
230 lines (187 loc) · 6.75 KB
/
storage.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "storage.h"
#include <stdint.h>
#include <memory>
#include "FreeRTOS.h"
#include "config.h"
#include "hardware/flash.h"
#include "hardware/sync.h"
#include "pico/multicore.h"
#include "pico/platform.h"
#include "semphr.h"
#include "sync.h"
extern "C" {
#include "littlefs/lfs.h"
static SemaphoreHandle_t __not_in_flash("storage") semaphore;
static lfs_t __not_in_flash("storage") lfs;
#define FS_OFFSET (PICO_FLASH_SIZE_BYTES - CONFIG_FLASH_FILESYSTEM_SIZE)
static int read(const struct lfs_config* c, lfs_block_t block, lfs_off_t off,
void* buffer, lfs_size_t size);
static int prog(const struct lfs_config* c, lfs_block_t block, lfs_off_t off,
const void* buffer, lfs_size_t size);
static int erase(const struct lfs_config* c, lfs_block_t block);
static int sync(const struct lfs_config* c);
static int lock(const struct lfs_config* c);
static int unlock(const struct lfs_config* c);
void failure(const char*) {}
// Compile time validation
constexpr struct lfs_config CreateLFSConfig() {
if ((CONFIG_FLASH_FILESYSTEM_SIZE) % (FLASH_SECTOR_SIZE) != 0) {
failure("Filesystem size has to be a multiple of FLASH_SECTOR_SIZE");
}
if ((CONFIG_FLASH_FILESYSTEM_SIZE) > PICO_FLASH_SIZE_BYTES) {
failure("Filesystem is larger than PICO_FLASH_SIZE_BYTES");
}
const struct lfs_config cfg = {
// block device operations
.read = read,
.prog = prog,
.erase = erase,
.sync = sync,
// block device configuration
.read_size = 64,
.prog_size = FLASH_PAGE_SIZE,
.block_size = FLASH_SECTOR_SIZE,
.block_count = CONFIG_FLASH_FILESYSTEM_SIZE / FLASH_SECTOR_SIZE,
.block_cycles = 500,
.cache_size = FLASH_SECTOR_SIZE / 4,
.lookahead_size = 32,
};
return cfg;
}
constexpr struct lfs_config __not_in_flash("storage1")
kLFSConfig = CreateLFSConfig();
static int read(const struct lfs_config* c, lfs_block_t block, lfs_off_t off,
void* buffer, lfs_size_t size) {
memcpy(buffer,
(uint8_t*)(XIP_NOCACHE_NOALLOC_BASE) + FS_OFFSET +
(block * c->block_size) + off,
size);
return LFS_ERR_OK;
}
static int __no_inline_not_in_flash_func(prog)(const struct lfs_config* c,
lfs_block_t block, lfs_off_t off,
const void* buffer,
lfs_size_t size) {
const uint32_t irq = save_and_disable_interrupts();
volatile uint32_t offset = FS_OFFSET + (block * c->block_size) + off;
flash_range_program(FS_OFFSET + (block * c->block_size) + off,
(const uint8_t*)buffer, size);
restore_interrupts(irq);
return LFS_ERR_OK;
}
static int __no_inline_not_in_flash_func(erase)(const struct lfs_config* c,
lfs_block_t block) {
const uint32_t irq = save_and_disable_interrupts();
flash_range_erase(FS_OFFSET + (block * c->block_size), c->block_size);
restore_interrupts(irq);
return LFS_ERR_OK;
}
static int sync(const struct lfs_config* c) { return LFS_ERR_OK; }
// TODO implement the USB mass storage class
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
void* buffer, uint32_t bufsize) {
return 0;
}
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
uint8_t* buffer, uint32_t bufsize) {
return 0;
}
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
uint8_t product_id[16], uint8_t product_rev[4]) {}
bool tud_msc_test_unit_ready_cb(uint8_t lun) { return true; }
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count,
uint16_t* block_size) {}
int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
uint16_t bufsize) {
return 0;
}
}
Status InitializeStorage() {
semaphore = xSemaphoreCreateBinary();
xSemaphoreGive(semaphore);
// Mount the filesystem or format it
int err = lfs_mount(&lfs, &kLFSConfig);
if (err) {
lfs_format(&lfs, &kLFSConfig);
lfs_mount(&lfs, &kLFSConfig);
}
return StartSyncTasks();
}
Status WriteStringToFile(const std::string& content, const std::string& name) {
LockSemaphore lock(semaphore);
// Block the other core to avoid executing flash code when writing to flash
std::unique_ptr<CoreBlockerSection> blocker;
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
blocker = std::make_unique<CoreBlockerSection>();
}
lfs_file_t file;
if (lfs_file_open(&lfs, &file, name.c_str(), LFS_O_RDWR | LFS_O_CREAT) < 0) {
return ERROR;
}
const lfs_ssize_t written =
lfs_file_write(&lfs, &file, content.c_str(), content.size());
if (written < 0 || written != content.size()) {
return ERROR;
}
if (lfs_file_close(&lfs, &file) < 0) {
return ERROR;
}
return OK;
}
Status ReadFileContent(const std::string& name, std::string* output) {
LockSemaphore lock(semaphore);
// Block the other core to avoid executing flash code when writing to flash
std::unique_ptr<CoreBlockerSection> blocker;
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
blocker = std::make_unique<CoreBlockerSection>();
}
lfs_file_t file;
if (lfs_file_open(&lfs, &file, name.c_str(), LFS_O_RDONLY) < 0) {
return ERROR;
}
const lfs_soff_t file_size = lfs_file_size(&lfs, &file);
if (file_size < 0) {
return ERROR;
}
std::unique_ptr<uint8_t[]> read_buffer(new uint8_t[file_size]);
const lfs_ssize_t read_bytes =
lfs_file_read(&lfs, &file, read_buffer.get(), file_size);
if (read_bytes < 0 || read_bytes != file_size) {
return ERROR;
}
*output = std::string((char*)read_buffer.get(), (uint32_t)file_size);
if (lfs_file_close(&lfs, &file) < 0) {
return ERROR;
}
return OK;
}
Status GetFileSize(const std::string& name, size_t* output) {
LockSemaphore lock(semaphore);
// Block the other core to avoid executing flash code when writing to flash
std::unique_ptr<CoreBlockerSection> blocker;
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
blocker = std::make_unique<CoreBlockerSection>();
}
lfs_file_t file;
if (lfs_file_open(&lfs, &file, name.c_str(), LFS_O_RDONLY) < 0) {
return ERROR;
}
const lfs_soff_t file_size = lfs_file_size(&lfs, &file);
if (file_size < 0) {
return ERROR;
}
*output = file_size;
return OK;
}
Status RemoveFile(const std::string& name) {
LockSemaphore lock(semaphore);
// Block the other core to avoid executing flash code when writing to flash
std::unique_ptr<CoreBlockerSection> blocker;
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
blocker = std::make_unique<CoreBlockerSection>();
}
if (lfs_remove(&lfs, name.c_str()) < 0) {
return ERROR;
}
return OK;
}