Skip to content

Commit 20c257d

Browse files
committed
more internal flash clean up
1 parent 58c47d3 commit 20c257d

File tree

7 files changed

+96
-91
lines changed

7 files changed

+96
-91
lines changed

cores/nRF5/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void suspendLoop(void);
6666
#include "utility/debug.h"
6767
#include "utility/utilities.h"
6868
#include "utility/AdaCallback.h"
69+
#include "flash/flash_nrf5x.h"
6970

7071
#ifdef USE_TINYUSB
7172
#include "Adafruit_TinyUSB_Core.h"

cores/nRF5/flash/flash_nrf5x.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ bool flash_nrf5x_erase(uint32_t addr)
8282
return fal_erase(addr);
8383
}
8484

85+
bool flash_nrf5x_erase_all(void)
86+
{
87+
for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE )
88+
{
89+
VERIFY( flash_nrf5x_erase(addr) );
90+
}
91+
92+
return true;
93+
}
94+
8595
//--------------------------------------------------------------------+
8696
// HAL for caching
8797
//--------------------------------------------------------------------+

cores/nRF5/flash/flash_nrf5x.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@
2929

3030
#define FLASH_NRF52_PAGE_SIZE 4096
3131

32+
#ifdef NRF52840_XXAA
33+
#define LFS_FLASH_ADDR 0xED000
34+
#else
35+
#define LFS_FLASH_ADDR 0x6D000
36+
#endif
37+
38+
#define LFS_FLASH_TOTAL_SIZE (7*FLASH_NRF52_PAGE_SIZE)
39+
3240
#ifdef __cplusplus
3341
extern "C" {
3442
#endif
3543

3644
void flash_nrf5x_flush (void);
3745
bool flash_nrf5x_erase(uint32_t addr);
46+
bool flash_nrf5x_erase_all(void);
3847

3948
uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len);
4049
uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len);

libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ void setup()
3535
// Initialize Internal File System
3636
InternalFS.begin();
3737

38-
Serial.print("Formating ...");
38+
Serial.print("Formating ... ");
39+
delay(1); // for message appear on monitor
3940

40-
// Format without erase
41-
// Pass true for full external flash erasing (take time)
42-
InternalFS.format(true);
41+
// Erase all sectors of internal flash region for Filesystem.
42+
// Low level format
43+
flash_nrf5x_erase_all();
44+
45+
// Format
46+
InternalFS.format();
4347

4448
Serial.println("Done");
4549
}

libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ void setup()
7070
Serial.println("Failed!");
7171
}
7272
}
73+
74+
Serial.println("Done");
7375
}
7476

7577
// the loop function runs over and over again forever
7678
void loop()
7779
{
78-
digitalToggle(LED_RED);
79-
delay(1000);
8080
}

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp

Lines changed: 62 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,12 @@
3030

3131
using 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-
8339
static 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.
10157
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)
13995
return 0;
14096
}
14197

142-
//--------------------------------------------------------------------+
143-
// Implementation
144-
//--------------------------------------------------------------------+
98+
14599
static struct lfs_config _InternalFSConfig =
146100
{
147101
.context = NULL,
@@ -165,18 +119,21 @@ static struct lfs_config _InternalFSConfig =
165119

166120
Adafruit_LittleFS InternalFS(&_InternalFSConfig);
167121

122+
123+
//--------------------------------------------------------------------+
124+
// Implementation
125+
//--------------------------------------------------------------------+
126+
168127
Adafruit_LittleFS::Adafruit_LittleFS (void)
169128
: Adafruit_LittleFS(NULL)
170129
{
171130

172131
}
173132

174133
Adafruit_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
188148
bool 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
236183
Adafruit_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
241189
bool 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
247196
bool 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
278228
bool Adafruit_LittleFS::remove (char const *filepath)
279229
{
280230
VERIFY_LFS(lfs_remove(&_lfs, filepath), false);
281231
return true;
282232
}
283233

234+
// Remove a folder
284235
bool Adafruit_LittleFS::rmdir (char const *filepath)
285236
{
286237
VERIFY_LFS(lfs_remove(&_lfs, filepath));
287238
return true;
288239
}
289240

241+
// Remove a folder recursively
290242
bool 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

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class Adafruit_LittleFS
4040
Adafruit_LittleFS (struct lfs_config* cfg);
4141
virtual ~Adafruit_LittleFS ();
4242

43-
bool begin (struct lfs_config * cfg = NULL);
43+
bool begin(struct lfs_config * cfg = NULL);
44+
void end(void);
4445

4546
// Open the specified file/directory with the supplied mode (e.g. read or
4647
// write, etc). Returns a File object for interacting with the file.
@@ -63,20 +64,17 @@ class Adafruit_LittleFS
6364
// Delete a folder (recursively)
6465
bool rmdir_r (char const *filepath);
6566

66-
// format whole file system
67-
bool format (bool eraseall);
67+
// format file system
68+
bool format (void);
6869

6970
lfs_t* getFS(void)
7071
{
7172
return &_lfs;
7273
}
7374

7475
protected:
75-
bool _begun;
7676
bool _mounted;
7777

78-
virtual void _flash_erase_all();
79-
8078
private:
8179
struct lfs_config* _lfs_cfg;
8280
lfs_t _lfs;

0 commit comments

Comments
 (0)