@@ -76,35 +76,105 @@ const char* dbg_strerr_lfs (int32_t err)
7676
7777#endif
7878
79+ // --------------------------------------------------------------------+
80+ // flash API
81+ // --------------------------------------------------------------------+
82+
7983static inline uint32_t lba2addr (uint32_t block)
8084{
8185 return ((uint32_t ) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE;
8286}
8387
88+ static int _internal_flash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
89+ {
90+ (void ) c;
91+
92+ uint32_t addr = lba2addr (block) + off;
93+ flash_nrf5x_read (buffer, addr, size);
94+
95+ return 0 ;
96+ }
97+
98+ // Program a region in a block. The block must have previously
99+ // been erased. Negative error codes are propogated to the user.
100+ // May return LFS_ERR_CORRUPT if the block should be considered bad.
101+ static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
102+ lfs_size_t size)
103+ {
104+ (void ) c;
105+
106+ uint32_t addr = lba2addr (block) + off;
107+ flash_nrf5x_write (addr, buffer, size);
108+
109+ return 0 ;
110+ }
111+
112+ // Erase a block. A block must be erased before being programmed.
113+ // The state of an erased block is undefined. Negative error codes
114+ // are propogated to the user.
115+ // May return LFS_ERR_CORRUPT if the block should be considered bad.
116+ static int _internal_flash_erase (const struct lfs_config *c, lfs_block_t block)
117+ {
118+ (void ) c;
119+
120+ uint32_t addr = lba2addr (block);
121+
122+ // implement as write 0xff to whole block address
123+ for (int i=0 ; i <LFS_BLOCK_SIZE; i++)
124+ {
125+ flash_nrf5x_write8 (addr + i, 0xFF );
126+ }
127+
128+ // flash_nrf5x_flush();
129+
130+ return 0 ;
131+ }
132+
133+ // Sync the state of the underlying block device. Negative error codes
134+ // are propogated to the user.
135+ static int _internal_flash_sync (const struct lfs_config *c)
136+ {
137+ (void ) c;
138+ flash_nrf5x_flush ();
139+ return 0 ;
140+ }
141+
84142// --------------------------------------------------------------------+
85143// Implementation
86144// --------------------------------------------------------------------+
87- Adafruit_LittleFS InternalFS;
145+ static struct lfs_config _InternalFSConfig =
146+ {
147+ .context = NULL ,
148+
149+ .read = _internal_flash_read,
150+ .prog = _internal_flash_prog,
151+ .erase = _internal_flash_erase,
152+ .sync = _internal_flash_sync,
153+
154+ .read_size = LFS_BLOCK_SIZE,
155+ .prog_size = LFS_BLOCK_SIZE,
156+ .block_size = LFS_BLOCK_SIZE,
157+ .block_count = LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE,
158+ .lookahead = 128 ,
159+
160+ .read_buffer = NULL ,
161+ .prog_buffer = NULL ,
162+ .lookahead_buffer = NULL ,
163+ .file_buffer = NULL
164+ };
88165
89- Adafruit_LittleFS::Adafruit_LittleFS (void ) :
90- Adafruit_LittleFS( LFS_BLOCK_SIZE, LFS_BLOCK_SIZE, LFS_BLOCK_SIZE, LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE, 128 )
166+ Adafruit_LittleFS InternalFS (&_InternalFSConfig);
167+
168+ Adafruit_LittleFS::Adafruit_LittleFS (void )
169+ : Adafruit_LittleFS(NULL )
91170{
171+
92172}
93173
94- Adafruit_LittleFS::Adafruit_LittleFS (uint32_t read_size, uint32_t prog_size, uint32_t block_size, uint32_t block_count, uint32_t lookahead)
174+ Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config * cfg)
175+
95176{
96- varclr (&_lfs_cfg);
97- _lfs_cfg.context = this ;
98- _lfs_cfg.read = _iflash_read;
99- _lfs_cfg.prog = _iflash_prog;
100- _lfs_cfg.erase = _iflash_erase;
101- _lfs_cfg.sync = _iflash_sync;
102-
103- _lfs_cfg.read_size = read_size;
104- _lfs_cfg.prog_size = prog_size;
105- _lfs_cfg.block_size = block_size;
106- _lfs_cfg.block_count = block_count;
107- _lfs_cfg.lookahead = lookahead;
177+ _lfs_cfg = cfg;
108178
109179 _begun = false ;
110180 _mounted = false ;
@@ -115,12 +185,16 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
115185
116186}
117187
118- bool Adafruit_LittleFS::begin (void )
188+ bool Adafruit_LittleFS::begin (struct lfs_config * cfg )
119189{
120190 if ( _begun ) return true ;
191+
192+ if (cfg) _lfs_cfg = cfg;
193+ if (!_lfs_cfg) return false ;
194+
121195 _begun = true ;
122196
123- int err = lfs_mount (&_lfs, & _lfs_cfg);
197+ int err = lfs_mount (&_lfs, _lfs_cfg);
124198
125199 // reformat if we can't mount the filesystem
126200 if ( LFS_ERR_CORRUPT == err )
@@ -134,6 +208,15 @@ bool Adafruit_LittleFS::begin (void)
134208 return true ;
135209}
136210
211+ void Adafruit_LittleFS::_flash_erase_all ()
212+ {
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+ }
218+
219+
137220bool Adafruit_LittleFS::format (bool eraseall)
138221{
139222 if ( eraseall ) {
@@ -142,8 +225,8 @@ bool Adafruit_LittleFS::format (bool eraseall)
142225 if (_mounted) {
143226 VERIFY_LFS (lfs_unmount (&_lfs), false );
144227 }
145- VERIFY_LFS (lfs_format (&_lfs, & _lfs_cfg), false );
146- VERIFY_LFS (lfs_mount (&_lfs, & _lfs_cfg), false );
228+ VERIFY_LFS (lfs_format (&_lfs, _lfs_cfg), false );
229+ VERIFY_LFS (lfs_mount (&_lfs, _lfs_cfg), false );
147230
148231 _mounted = true ;
149232
@@ -212,92 +295,3 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
212295 VERIFY_LFS (lfs_remove (&_lfs, filepath));
213296 return true ;
214297}
215-
216- // --------------------------------------------------------------------+
217- // flash API
218- // --------------------------------------------------------------------+
219-
220- int Adafruit_LittleFS::_flash_read (lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
221- {
222- uint32_t addr = lba2addr (block) + off;
223- flash_nrf5x_read (buffer, addr, size);
224-
225- return 0 ;
226- }
227-
228- // Program a region in a block. The block must have previously
229- // been erased. Negative error codes are propogated to the user.
230- // May return LFS_ERR_CORRUPT if the block should be considered bad.
231- int Adafruit_LittleFS::_flash_prog (lfs_block_t block, lfs_off_t off, const void *buffer,
232- lfs_size_t size)
233- {
234- uint32_t addr = lba2addr (block) + off;
235- flash_nrf5x_write (addr, buffer, size);
236-
237- return 0 ;
238- }
239-
240- // Erase a block. A block must be erased before being programmed.
241- // The state of an erased block is undefined. Negative error codes
242- // are propogated to the user.
243- // May return LFS_ERR_CORRUPT if the block should be considered bad.
244- int Adafruit_LittleFS::_flash_erase (lfs_block_t block)
245- {
246- uint32_t addr = lba2addr (block);
247-
248- // implement as write 0xff to whole block address
249- for (int i=0 ; i <LFS_BLOCK_SIZE; i++)
250- {
251- flash_nrf5x_write8 (addr + i, 0xFF );
252- }
253-
254- // flash_nrf5x_flush();
255-
256- return 0 ;
257- }
258-
259- void Adafruit_LittleFS::_flash_erase_all ()
260- {
261- for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE )
262- {
263- flash_nrf5x_erase (addr);
264- }
265- }
266-
267- // Sync the state of the underlying block device. Negative error codes
268- // are propogated to the user.
269- int Adafruit_LittleFS::_flash_sync ()
270- {
271- flash_nrf5x_flush ();
272- return 0 ;
273- }
274-
275- int Adafruit_LittleFS::_iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
276- {
277- return ((Adafruit_LittleFS *)c->context )->_flash_read (block, off, buffer, size);
278- }
279-
280- // Program a region in a block. The block must have previously
281- // been erased. Negative error codes are propogated to the user.
282- // May return LFS_ERR_CORRUPT if the block should be considered bad.
283- int Adafruit_LittleFS::_iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
284- lfs_size_t size)
285- {
286- return ((Adafruit_LittleFS *)c->context )->_flash_prog (block, off, buffer, size);
287- }
288-
289- // Erase a block. A block must be erased before being programmed.
290- // The state of an erased block is undefined. Negative error codes
291- // are propogated to the user.
292- // May return LFS_ERR_CORRUPT if the block should be considered bad.
293- int Adafruit_LittleFS::_iflash_erase (const struct lfs_config *c, lfs_block_t block)
294- {
295- return ((Adafruit_LittleFS *)c->context )->_flash_erase (block);
296- }
297-
298- // Sync the state of the underlying block device. Negative error codes
299- // are propogated to the user.
300- int Adafruit_LittleFS::_iflash_sync (const struct lfs_config *c)
301- {
302- return ((Adafruit_LittleFS *)c->context )->_flash_sync ();
303- }
0 commit comments