30
30
31
31
using namespace Adafruit_LittleFS_Namespace ;
32
32
33
- #ifdef NRF52840_XXAA
34
- #define LFS_FLASH_ADDR 0xED000
35
- #else
36
- #define LFS_FLASH_ADDR 0x6D000
37
- #endif
38
-
39
- #define LFS_FLASH_TOTAL_SIZE (7 *FLASH_NRF52_PAGE_SIZE)
40
33
#define LFS_BLOCK_SIZE 128
41
34
42
35
// --------------------------------------------------------------------+
43
36
// LFS Disk IO
44
37
// --------------------------------------------------------------------+
45
38
46
- #if CFG_DEBUG
47
-
48
- #define VERIFY_LFS (...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_lfs)
49
- #define PRINT_LFS_ERR (_err ) VERIFY_MESS(_err, dbg_strerr_lfs)
50
-
51
- const char * dbg_strerr_lfs (int32_t err)
52
- {
53
- switch ( err )
54
- {
55
- case LFS_ERR_OK : return " LFS_ERR_OK" ;
56
- case LFS_ERR_IO : return " LFS_ERR_IO" ;
57
- case LFS_ERR_CORRUPT : return " LFS_ERR_CORRUPT" ;
58
- case LFS_ERR_NOENT : return " LFS_ERR_NOENT" ;
59
- case LFS_ERR_EXIST : return " LFS_ERR_EXIST" ;
60
- case LFS_ERR_NOTDIR : return " LFS_ERR_NOTDIR" ;
61
- case LFS_ERR_ISDIR : return " LFS_ERR_ISDIR" ;
62
- case LFS_ERR_NOTEMPTY : return " LFS_ERR_NOTEMPTY" ;
63
- case LFS_ERR_BADF : return " LFS_ERR_BADF" ;
64
- case LFS_ERR_INVAL : return " LFS_ERR_INVAL" ;
65
- case LFS_ERR_NOSPC : return " LFS_ERR_NOSPC" ;
66
- case LFS_ERR_NOMEM : return " LFS_ERR_NOMEM" ;
67
-
68
- default :
69
- static char errcode[10 ];
70
- sprintf (errcode, " %d" , err);
71
- return errcode;
72
- }
73
-
74
- return NULL ;
75
- }
76
-
77
- #endif
78
-
79
- // --------------------------------------------------------------------+
80
- // flash API
81
- // --------------------------------------------------------------------+
82
-
83
39
static inline uint32_t lba2addr (uint32_t block)
84
40
{
85
41
return ((uint32_t ) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE;
@@ -95,7 +51,7 @@ static int _internal_flash_read (const struct lfs_config *c, lfs_block_t block,
95
51
return 0 ;
96
52
}
97
53
98
- // Program a region in a block. The block must have previously
54
+ // Program a region in a block. The block must have previously
99
55
// been erased. Negative error codes are propogated to the user.
100
56
// May return LFS_ERR_CORRUPT if the block should be considered bad.
101
57
static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
@@ -139,9 +95,7 @@ static int _internal_flash_sync (const struct lfs_config *c)
139
95
return 0 ;
140
96
}
141
97
142
- // --------------------------------------------------------------------+
143
- // Implementation
144
- // --------------------------------------------------------------------+
98
+
145
99
static struct lfs_config _InternalFSConfig =
146
100
{
147
101
.context = NULL ,
@@ -165,18 +119,21 @@ static struct lfs_config _InternalFSConfig =
165
119
166
120
Adafruit_LittleFS InternalFS (&_InternalFSConfig);
167
121
122
+
123
+ // --------------------------------------------------------------------+
124
+ // Implementation
125
+ // --------------------------------------------------------------------+
126
+
168
127
Adafruit_LittleFS::Adafruit_LittleFS (void )
169
128
: Adafruit_LittleFS(NULL )
170
129
{
171
130
172
131
}
173
132
174
133
Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config * cfg)
175
-
176
134
{
135
+ varclr (&_lfs);
177
136
_lfs_cfg = cfg;
178
-
179
- _begun = false ;
180
137
_mounted = false ;
181
138
}
182
139
@@ -185,65 +142,57 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
185
142
186
143
}
187
144
145
+ // Initialize and mount the file system
146
+ // Return true if mounted successfully else probably corrupted.
147
+ // User should format the disk and try again
188
148
bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
189
149
{
190
- if ( _begun ) return true ;
150
+ if ( _mounted ) return true ;
191
151
192
152
if (cfg) _lfs_cfg = cfg;
193
153
if (!_lfs_cfg) return false ;
194
154
195
- _begun = true ;
196
-
197
- int err = lfs_mount (&_lfs, _lfs_cfg);
198
-
199
- // reformat if we can't mount the filesystem
200
- if ( LFS_ERR_CORRUPT == err )
201
- {
202
- LOG_LV1 (" IFLASH" , " Format internal file system" );
203
- this ->format (false );
204
- } else {
205
- _mounted = true ;
206
- }
155
+ VERIFY_LFS (lfs_mount (&_lfs, _lfs_cfg), false );
156
+ _mounted = true ;
207
157
208
158
return true ;
209
159
}
210
160
211
- void Adafruit_LittleFS::_flash_erase_all ()
161
+ // Tear down and unmount file system
162
+ void Adafruit_LittleFS::end (void )
212
163
{
213
- for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE )
214
- {
215
- flash_nrf5x_erase (addr);
216
- }
217
- }
164
+ if (!_mounted) return ;
218
165
166
+ _mounted = false ;
167
+ VERIFY_LFS (lfs_unmount (&_lfs), );
168
+ }
219
169
220
- bool Adafruit_LittleFS::format (bool eraseall )
170
+ bool Adafruit_LittleFS::format (void )
221
171
{
222
- if ( eraseall ) {
223
- _flash_erase_all ();
224
- }
225
- if (_mounted) {
226
- VERIFY_LFS (lfs_unmount (&_lfs), false );
227
- }
172
+ // if already mounted: umount -> format -> remount
173
+ if (_mounted) VERIFY_LFS (lfs_unmount (&_lfs), false );
174
+
228
175
VERIFY_LFS (lfs_format (&_lfs, _lfs_cfg), false );
229
- VERIFY_LFS (lfs_mount (&_lfs, _lfs_cfg), false );
230
176
231
- _mounted = true ;
177
+ if ( _mounted) VERIFY_LFS ( lfs_mount (&_lfs, _lfs_cfg), false ) ;
232
178
233
179
return true ;
234
180
}
235
181
182
+ // Open a file or folder
236
183
Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode)
237
184
{
238
185
return Adafruit_LittleFS_Namespace::File (filepath, mode, *this );
239
186
}
240
187
188
+ // Check if file or folder exists
241
189
bool Adafruit_LittleFS::exists (char const *filepath)
242
190
{
243
191
struct lfs_info info;
244
192
return 0 == lfs_stat (&_lfs, filepath, &info);
245
193
}
246
194
195
+ // Create a directory, create intermediate parent if needed
247
196
bool Adafruit_LittleFS::mkdir (char const *filepath)
248
197
{
249
198
const char * slash = filepath;
@@ -275,18 +224,21 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
275
224
return true ;
276
225
}
277
226
227
+ // Remove a file
278
228
bool Adafruit_LittleFS::remove (char const *filepath)
279
229
{
280
230
VERIFY_LFS (lfs_remove (&_lfs, filepath), false );
281
231
return true ;
282
232
}
283
233
234
+ // Remove a folder
284
235
bool Adafruit_LittleFS::rmdir (char const *filepath)
285
236
{
286
237
VERIFY_LFS (lfs_remove (&_lfs, filepath));
287
238
return true ;
288
239
}
289
240
241
+ // Remove a folder recursively
290
242
bool Adafruit_LittleFS::rmdir_r (char const *filepath)
291
243
{
292
244
/* adafruit: lfs is modified to remove non-empty folder,
@@ -295,3 +247,34 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
295
247
VERIFY_LFS (lfs_remove (&_lfs, filepath));
296
248
return true ;
297
249
}
250
+
251
+ // ------------- Debug -------------//
252
+ #if CFG_DEBUG
253
+
254
+ const char * dbg_strerr_lfs (int32_t err)
255
+ {
256
+ switch ( err )
257
+ {
258
+ case LFS_ERR_OK : return " LFS_ERR_OK" ;
259
+ case LFS_ERR_IO : return " LFS_ERR_IO" ;
260
+ case LFS_ERR_CORRUPT : return " LFS_ERR_CORRUPT" ;
261
+ case LFS_ERR_NOENT : return " LFS_ERR_NOENT" ;
262
+ case LFS_ERR_EXIST : return " LFS_ERR_EXIST" ;
263
+ case LFS_ERR_NOTDIR : return " LFS_ERR_NOTDIR" ;
264
+ case LFS_ERR_ISDIR : return " LFS_ERR_ISDIR" ;
265
+ case LFS_ERR_NOTEMPTY : return " LFS_ERR_NOTEMPTY" ;
266
+ case LFS_ERR_BADF : return " LFS_ERR_BADF" ;
267
+ case LFS_ERR_INVAL : return " LFS_ERR_INVAL" ;
268
+ case LFS_ERR_NOSPC : return " LFS_ERR_NOSPC" ;
269
+ case LFS_ERR_NOMEM : return " LFS_ERR_NOMEM" ;
270
+
271
+ default :
272
+ static char errcode[10 ];
273
+ sprintf (errcode, " %d" , err);
274
+ return errcode;
275
+ }
276
+
277
+ return NULL ;
278
+ }
279
+
280
+ #endif
0 commit comments