@@ -118,79 +118,30 @@ static inline uint32_t lba2addr(uint32_t block)
118118 return ((uint32_t ) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE;
119119}
120120
121- static int _iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
122- {
123- (void ) c;
124-
125- uint32_t addr = lba2addr (block) + off;
126- flash_nrf5x_read (buffer, addr, size);
127-
128- return 0 ;
129- }
130-
131- // Program a region in a block. The block must have previously
132- // been erased. Negative error codes are propogated to the user.
133- // May return LFS_ERR_CORRUPT if the block should be considered bad.
134- static int _iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
135- lfs_size_t size)
136- {
137- (void ) c;
138-
139- uint32_t addr = lba2addr (block) + off;
140- flash_nrf5x_write (addr, buffer, size);
141-
142- return 0 ;
143- }
144-
145- // Erase a block. A block must be erased before being programmed.
146- // The state of an erased block is undefined. Negative error codes
147- // are propogated to the user.
148- // May return LFS_ERR_CORRUPT if the block should be considered bad.
149- static int _iflash_erase (const struct lfs_config *c, lfs_block_t block)
150- {
151- (void ) c;
152-
153- uint32_t addr = lba2addr (block);
154-
155- // implement as write 0xff to whole block address
156- for (int i=0 ; i <LFS_BLOCK_SIZE; i++)
157- {
158- flash_nrf5x_write8 (addr + i, 0xFF );
159- }
160-
161- // flash_nrf5x_flush();
162-
163- return 0 ;
164- }
165-
166- // Sync the state of the underlying block device. Negative error codes
167- // are propogated to the user.
168- static int _iflash_sync (const struct lfs_config *c)
169- {
170- (void ) c;
171- flash_nrf5x_flush ();
172- return 0 ;
173- }
174-
175121// --------------------------------------------------------------------+
176122// Implementation
177123// --------------------------------------------------------------------+
178124LittleFS InternalFS;
179125
180- LittleFS::LittleFS (void )
126+ LittleFS::LittleFS (void ) :
127+ LittleFS( LFS_BLOCK_SIZE, LFS_BLOCK_SIZE, LFS_BLOCK_SIZE, LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE, 128 )
128+ {
129+ }
130+
131+ LittleFS::LittleFS (lfs_size_t read_size, lfs_size_t prog_size, lfs_size_t block_size, lfs_size_t block_count, lfs_size_t lookahead)
181132{
182133 varclr (&_lfs_cfg);
183- _lfs_cfg.context = NULL ;
134+ _lfs_cfg.context = this ;
184135 _lfs_cfg.read = _iflash_read;
185136 _lfs_cfg.prog = _iflash_prog;
186137 _lfs_cfg.erase = _iflash_erase;
187138 _lfs_cfg.sync = _iflash_sync;
188139
189- _lfs_cfg.read_size = LFS_BLOCK_SIZE ;
190- _lfs_cfg.prog_size = LFS_BLOCK_SIZE ;
191- _lfs_cfg.block_size = LFS_BLOCK_SIZE ;
192- _lfs_cfg.block_count = LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE ;
193- _lfs_cfg.lookahead = 128 ;
140+ _lfs_cfg.read_size = read_size ;
141+ _lfs_cfg.prog_size = prog_size ;
142+ _lfs_cfg.block_size = block_size ;
143+ _lfs_cfg.block_count = block_count ;
144+ _lfs_cfg.lookahead = lookahead ;
194145
195146 _begun = false ;
196147 _mounted = false ;
@@ -222,12 +173,8 @@ bool LittleFS::begin (void)
222173
223174bool LittleFS::format (bool eraseall)
224175{
225- if ( eraseall )
226- {
227- for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE )
228- {
229- flash_nrf5x_erase (addr);
230- }
176+ if ( eraseall ) {
177+ _flash_erase_all ();
231178 }
232179 if (_mounted) {
233180 VERIFY_LFS (lfs_unmount (&_lfs), false );
@@ -465,3 +412,89 @@ void LittleFS::_f_rewindDirectory (void* fhdl)
465412{
466413 VERIFY_LFS (lfs_dir_rewind (&_lfs, (lfs_dir_t * ) fhdl),);
467414}
415+
416+
417+ int LittleFS::_flash_read (lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
418+ {
419+ uint32_t addr = lba2addr (block) + off;
420+ flash_nrf5x_read (buffer, addr, size);
421+
422+ return 0 ;
423+ }
424+
425+ // Program a region in a block. The block must have previously
426+ // been erased. Negative error codes are propogated to the user.
427+ // May return LFS_ERR_CORRUPT if the block should be considered bad.
428+ int LittleFS::_flash_prog (lfs_block_t block, lfs_off_t off, const void *buffer,
429+ lfs_size_t size)
430+ {
431+ uint32_t addr = lba2addr (block) + off;
432+ flash_nrf5x_write (addr, buffer, size);
433+
434+ return 0 ;
435+ }
436+
437+ // Erase a block. A block must be erased before being programmed.
438+ // The state of an erased block is undefined. Negative error codes
439+ // are propogated to the user.
440+ // May return LFS_ERR_CORRUPT if the block should be considered bad.
441+ int LittleFS::_flash_erase (lfs_block_t block)
442+ {
443+ uint32_t addr = lba2addr (block);
444+
445+ // implement as write 0xff to whole block address
446+ for (int i=0 ; i <LFS_BLOCK_SIZE; i++)
447+ {
448+ flash_nrf5x_write8 (addr + i, 0xFF );
449+ }
450+
451+ // flash_nrf5x_flush();
452+
453+ return 0 ;
454+ }
455+
456+ void LittleFS::_flash_erase_all ()
457+ {
458+ for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE )
459+ {
460+ flash_nrf5x_erase (addr);
461+ }
462+ }
463+
464+ // Sync the state of the underlying block device. Negative error codes
465+ // are propogated to the user.
466+ int LittleFS::_flash_sync ()
467+ {
468+ flash_nrf5x_flush ();
469+ return 0 ;
470+ }
471+
472+ int LittleFS::_iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
473+ {
474+ return ((LittleFS *)c->context )->_flash_read (block, off, buffer, size);
475+ }
476+
477+ // Program a region in a block. The block must have previously
478+ // been erased. Negative error codes are propogated to the user.
479+ // May return LFS_ERR_CORRUPT if the block should be considered bad.
480+ int LittleFS::_iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
481+ lfs_size_t size)
482+ {
483+ return ((LittleFS *)c->context )->_flash_prog (block, off, buffer, size);
484+ }
485+
486+ // Erase a block. A block must be erased before being programmed.
487+ // The state of an erased block is undefined. Negative error codes
488+ // are propogated to the user.
489+ // May return LFS_ERR_CORRUPT if the block should be considered bad.
490+ int LittleFS::_iflash_erase (const struct lfs_config *c, lfs_block_t block)
491+ {
492+ return ((LittleFS *)c->context )->_flash_erase (block);
493+ }
494+
495+ // Sync the state of the underlying block device. Negative error codes
496+ // are propogated to the user.
497+ int LittleFS::_iflash_sync (const struct lfs_config *c)
498+ {
499+ return ((LittleFS *)c->context )->_flash_sync ();
500+ }
0 commit comments