3030
3131using namespace Adafruit_LittleFS_Namespace ;
3232
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)
4033#define LFS_BLOCK_SIZE 128
4134
4235// --------------------------------------------------------------------+
4336// LFS Disk IO
4437// --------------------------------------------------------------------+
4538
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-
8339static inline uint32_t lba2addr (uint32_t block)
8440{
8541 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,
9551 return 0 ;
9652}
9753
98- // Program a region in a block. The block must have previously
54+ // Program a region in a block. The block must have previously
9955// been erased. Negative error codes are propogated to the user.
10056// May return LFS_ERR_CORRUPT if the block should be considered bad.
10157static 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)
13995 return 0 ;
14096}
14197
142- // --------------------------------------------------------------------+
143- // Implementation
144- // --------------------------------------------------------------------+
98+
14599static struct lfs_config _InternalFSConfig =
146100{
147101 .context = NULL ,
@@ -165,18 +119,21 @@ static struct lfs_config _InternalFSConfig =
165119
166120Adafruit_LittleFS InternalFS (&_InternalFSConfig);
167121
122+
123+ // --------------------------------------------------------------------+
124+ // Implementation
125+ // --------------------------------------------------------------------+
126+
168127Adafruit_LittleFS::Adafruit_LittleFS (void )
169128 : Adafruit_LittleFS(NULL )
170129{
171130
172131}
173132
174133Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config * cfg)
175-
176134{
135+ varclr (&_lfs);
177136 _lfs_cfg = cfg;
178-
179- _begun = false ;
180137 _mounted = false ;
181138}
182139
@@ -185,65 +142,57 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
185142
186143}
187144
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
188148bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
189149{
190- if ( _begun ) return true ;
150+ if ( _mounted ) return true ;
191151
192152 if (cfg) _lfs_cfg = cfg;
193153 if (!_lfs_cfg) return false ;
194154
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 ;
207157
208158 return true ;
209159}
210160
211- void Adafruit_LittleFS::_flash_erase_all ()
161+ // Tear down and unmount file system
162+ void Adafruit_LittleFS::end (void )
212163{
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 ;
218165
166+ _mounted = false ;
167+ VERIFY_LFS (lfs_unmount (&_lfs), );
168+ }
219169
220- bool Adafruit_LittleFS::format (bool eraseall )
170+ bool Adafruit_LittleFS::format (void )
221171{
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+
228175 VERIFY_LFS (lfs_format (&_lfs, _lfs_cfg), false );
229- VERIFY_LFS (lfs_mount (&_lfs, _lfs_cfg), false );
230176
231- _mounted = true ;
177+ if ( _mounted) VERIFY_LFS ( lfs_mount (&_lfs, _lfs_cfg), false ) ;
232178
233179 return true ;
234180}
235181
182+ // Open a file or folder
236183Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode)
237184{
238185 return Adafruit_LittleFS_Namespace::File (filepath, mode, *this );
239186}
240187
188+ // Check if file or folder exists
241189bool Adafruit_LittleFS::exists (char const *filepath)
242190{
243191 struct lfs_info info;
244192 return 0 == lfs_stat (&_lfs, filepath, &info);
245193}
246194
195+ // Create a directory, create intermediate parent if needed
247196bool Adafruit_LittleFS::mkdir (char const *filepath)
248197{
249198 const char * slash = filepath;
@@ -275,18 +224,21 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
275224 return true ;
276225}
277226
227+ // Remove a file
278228bool Adafruit_LittleFS::remove (char const *filepath)
279229{
280230 VERIFY_LFS (lfs_remove (&_lfs, filepath), false );
281231 return true ;
282232}
283233
234+ // Remove a folder
284235bool Adafruit_LittleFS::rmdir (char const *filepath)
285236{
286237 VERIFY_LFS (lfs_remove (&_lfs, filepath));
287238 return true ;
288239}
289240
241+ // Remove a folder recursively
290242bool Adafruit_LittleFS::rmdir_r (char const *filepath)
291243{
292244 /* adafruit: lfs is modified to remove non-empty folder,
@@ -295,3 +247,34 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
295247 VERIFY_LFS (lfs_remove (&_lfs, filepath));
296248 return true ;
297249}
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