22
22
23
23
namespace mbed {
24
24
25
- extern " C" void lfs_crc (uint32_t * crc, const void *buffer, size_t size)
25
+ extern " C" uint32_t lfs_crc (uint32_t crc, const void *buffer, size_t size)
26
26
{
27
- uint32_t initial_xor = * crc;
27
+ uint32_t initial_xor = crc;
28
28
MbedCRC<POLY_32BIT_REV_ANSI, 32 > ct (initial_xor, 0x0 , true , false );
29
- ct.compute ((void *)buffer, size, (uint32_t *) crc);
29
+ ct.compute ((void *)buffer, size, &crc);
30
+ return crc;
30
31
}
31
32
32
33
// //// Conversion functions //////
@@ -142,14 +143,15 @@ static int lfs_bd_sync(const struct lfs_config *c)
142
143
143
144
// Filesystem implementation (See LittleFileSystem.h)
144
145
LittleFileSystem::LittleFileSystem (const char *name, BlockDevice *bd,
145
- lfs_size_t read_size, lfs_size_t prog_size ,
146
- lfs_size_t block_size , lfs_size_t lookahead )
146
+ lfs_size_t block_size, uint32_t block_cycles ,
147
+ lfs_size_t cache_size , lfs_size_t lookahead_size )
147
148
: FileSystem(name)
148
- , _read_size(read_size)
149
- , _prog_size(prog_size)
150
- , _block_size(block_size)
151
- , _lookahead(lookahead)
152
149
{
150
+ memset (&_config, 0 , sizeof (_config));
151
+ _config.block_size = block_size;
152
+ _config.block_cycles = block_cycles;
153
+ _config.cache_size = cache_size;
154
+ _config.lookahead_size = lookahead_size;
153
155
if (bd) {
154
156
mount (bd);
155
157
}
@@ -174,29 +176,18 @@ int LittleFileSystem::mount(BlockDevice *bd)
174
176
return err;
175
177
}
176
178
177
- memset (&_config, 0 , sizeof (_config));
178
- _config.context = bd;
179
- _config.read = lfs_bd_read;
180
- _config.prog = lfs_bd_prog;
181
- _config.erase = lfs_bd_erase;
182
- _config.sync = lfs_bd_sync;
183
- _config.read_size = bd->get_read_size ();
184
- if (_config.read_size < _read_size) {
185
- _config.read_size = _read_size;
186
- }
187
- _config.prog_size = bd->get_program_size ();
188
- if (_config.prog_size < _prog_size) {
189
- _config.prog_size = _prog_size;
190
- }
191
- _config.block_size = bd->get_erase_size ();
192
- if (_config.block_size < _block_size) {
193
- _config.block_size = _block_size;
194
- }
195
- _config.block_count = bd->size () / _config.block_size ;
196
- _config.lookahead = 32 * ((_config.block_count + 31 ) / 32 );
197
- if (_config.lookahead > _lookahead) {
198
- _config.lookahead = _lookahead;
199
- }
179
+ _config.context = bd;
180
+ _config.read = lfs_bd_read;
181
+ _config.prog = lfs_bd_prog;
182
+ _config.erase = lfs_bd_erase;
183
+ _config.sync = lfs_bd_sync;
184
+ _config.read_size = bd->get_read_size ();
185
+ _config.prog_size = bd->get_program_size ();
186
+ _config.block_size = lfs_max (_config.block_size , (lfs_size_t )bd->get_erase_size ());
187
+ _config.block_count = bd->size () / _config.block_size ;
188
+ _config.block_cycles = _config.block_cycles ;
189
+ _config.cache_size = lfs_max (_config.cache_size , _config.prog_size );
190
+ _config.lookahead_size = lfs_min (_config.lookahead_size , 8 * ((_config.block_count + 63 ) / 64 ));
200
191
201
192
err = lfs_mount (&_lfs, &_config);
202
193
if (err) {
@@ -236,11 +227,11 @@ int LittleFileSystem::unmount()
236
227
}
237
228
238
229
int LittleFileSystem::format (BlockDevice *bd,
239
- lfs_size_t read_size, lfs_size_t prog_size ,
240
- lfs_size_t block_size , lfs_size_t lookahead )
230
+ lfs_size_t block_size, uint32_t block_cycles ,
231
+ lfs_size_t cache_size , lfs_size_t lookahead_size )
241
232
{
242
233
LFS_INFO (" format(%p, %ld, %ld, %ld, %ld)" ,
243
- bd, read_size, prog_size, block_size, lookahead );
234
+ bd, block_size, block_cycles, cache_size, lookahead_size );
244
235
int err = bd->init ();
245
236
if (err) {
246
237
LFS_INFO (" format -> %d" , err);
@@ -251,28 +242,18 @@ int LittleFileSystem::format(BlockDevice *bd,
251
242
struct lfs_config _config;
252
243
253
244
memset (&_config, 0 , sizeof (_config));
254
- _config.context = bd;
255
- _config.read = lfs_bd_read;
256
- _config.prog = lfs_bd_prog;
257
- _config.erase = lfs_bd_erase;
258
- _config.sync = lfs_bd_sync;
259
- _config.read_size = bd->get_read_size ();
260
- if (_config.read_size < read_size) {
261
- _config.read_size = read_size;
262
- }
263
- _config.prog_size = bd->get_program_size ();
264
- if (_config.prog_size < prog_size) {
265
- _config.prog_size = prog_size;
266
- }
267
- _config.block_size = bd->get_erase_size ();
268
- if (_config.block_size < block_size) {
269
- _config.block_size = block_size;
270
- }
271
- _config.block_count = bd->size () / _config.block_size ;
272
- _config.lookahead = 32 * ((_config.block_count + 31 ) / 32 );
273
- if (_config.lookahead > lookahead) {
274
- _config.lookahead = lookahead;
275
- }
245
+ _config.context = bd;
246
+ _config.read = lfs_bd_read;
247
+ _config.prog = lfs_bd_prog;
248
+ _config.erase = lfs_bd_erase;
249
+ _config.sync = lfs_bd_sync;
250
+ _config.read_size = bd->get_read_size ();
251
+ _config.prog_size = bd->get_program_size ();
252
+ _config.block_size = lfs_max (block_size, (lfs_size_t )bd->get_erase_size ());
253
+ _config.block_count = bd->size () / _config.block_size ;
254
+ _config.block_cycles = block_cycles;
255
+ _config.cache_size = lfs_max (cache_size, _config.prog_size );
256
+ _config.lookahead_size = lfs_min (lookahead_size, 8 * ((_config.block_count + 63 ) / 64 ));
276
257
277
258
err = lfs_format (&_lfs, &_config);
278
259
if (err) {
@@ -314,7 +295,10 @@ int LittleFileSystem::reformat(BlockDevice *bd)
314
295
}
315
296
316
297
int err = LittleFileSystem::format (bd,
317
- _read_size, _prog_size, _block_size, _lookahead);
298
+ _config.block_size ,
299
+ _config.block_cycles ,
300
+ _config.cache_size ,
301
+ _config.lookahead_size );
318
302
if (err) {
319
303
LFS_INFO (" reformat -> %d" , err);
320
304
_mutex.unlock ();
@@ -376,24 +360,18 @@ int LittleFileSystem::stat(const char *name, struct stat *st)
376
360
return lfs_toerror (err);
377
361
}
378
362
379
- static int lfs_statvfs_count (void *p, lfs_block_t b)
380
- {
381
- *(lfs_size_t *)p += 1 ;
382
- return 0 ;
383
- }
384
-
385
363
int LittleFileSystem::statvfs (const char *name, struct statvfs *st)
386
364
{
387
365
memset (st, 0 , sizeof (struct statvfs ));
388
366
389
- lfs_size_t in_use = 0 ;
367
+ lfs_ssize_t in_use = 0 ;
390
368
_mutex.lock ();
391
369
LFS_INFO (" statvfs(\" %s\" , %p)" , name, st);
392
- int err = lfs_traverse (&_lfs, lfs_statvfs_count, &in_use );
393
- LFS_INFO (" statvfs -> %d" , lfs_toerror (err ));
370
+ in_use = lfs_fs_size (&_lfs);
371
+ LFS_INFO (" statvfs -> %d" , lfs_toerror (in_use ));
394
372
_mutex.unlock ();
395
- if (err ) {
396
- return err ;
373
+ if (in_use < 0 ) {
374
+ return in_use ;
397
375
}
398
376
399
377
st->f_bsize = _config.block_size ;
0 commit comments