@@ -234,24 +234,27 @@ int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl
234
234
235
235
#ifdef MICROPY_HW_BDEV_SPIFLASH_EXTENDED
236
236
// Board defined an external SPI flash for use with extended block protocol
237
- #define SPIFLASH (MICROPY_HW_BDEV_SPIFLASH_EXTENDED)
238
- #define PYB_FLASH_NATIVE_BLOCK_SIZE (MP_SPIFLASH_ERASE_BLOCK_SIZE)
239
- #define MICROPY_HW_BDEV_READBLOCKS_EXT (dest , bl , off , len ) (spi_bdev_readblocks_raw(SPIFLASH, (dest), (bl), (off), (len)))
240
- #define MICROPY_HW_BDEV_WRITEBLOCKS_EXT (dest , bl , off , len ) (spi_bdev_writeblocks_raw(SPIFLASH, (dest), (bl), (off), (len)))
237
+ #define MICROPY_HW_BDEV_BLOCKSIZE_EXT (MP_SPIFLASH_ERASE_BLOCK_SIZE)
238
+ #define MICROPY_HW_BDEV_READBLOCKS_EXT (dest , bl , off , len ) \
239
+ (spi_bdev_readblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (dest), (bl), (off), (len)))
240
+ #define MICROPY_HW_BDEV_WRITEBLOCKS_EXT (src , bl , off , len ) \
241
+ (spi_bdev_writeblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (src), (bl), (off), (len)))
242
+ #define MICROPY_HW_BDEV_ERASEBLOCKS_EXT (bl , len ) \
243
+ (spi_bdev_eraseblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (bl), (len)))
241
244
242
245
#elif (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2 ) && MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
243
246
// Board uses littlefs and internal flash, so enable extended block protocol on internal flash
244
- #define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE)
247
+ #define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
245
248
#define MICROPY_HW_BDEV_READBLOCKS_EXT (dest , bl , off , len ) (flash_bdev_readblocks_ext((dest), (bl), (off), (len)))
246
249
#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT (dest , bl , off , len ) (flash_bdev_writeblocks_ext((dest), (bl), (off), (len)))
247
250
#endif
248
251
249
- #ifndef PYB_FLASH_NATIVE_BLOCK_SIZE
250
- #define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE)
252
+ #ifndef MICROPY_HW_BDEV_BLOCKSIZE_EXT
253
+ #define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
251
254
#endif
252
255
253
256
#if defined(MICROPY_HW_BDEV_READBLOCKS_EXT )
254
- // Size of blocks is PYB_FLASH_NATIVE_BLOCK_SIZE
257
+ // Size of blocks is MICROPY_HW_BDEV_BLOCKSIZE_EXT
255
258
int storage_readblocks_ext (uint8_t * dest , uint32_t block , uint32_t offset , uint32_t len ) {
256
259
return MICROPY_HW_BDEV_READBLOCKS_EXT (dest , block , offset , len );
257
260
}
@@ -261,9 +264,7 @@ typedef struct _pyb_flash_obj_t {
261
264
mp_obj_base_t base ;
262
265
uint32_t start ; // in bytes
263
266
uint32_t len ; // in bytes
264
- #if defined(SPIFLASH )
265
267
bool use_native_block_size ;
266
- #endif
267
268
} pyb_flash_obj_t ;
268
269
269
270
// This Flash object represents the entire available flash, with emulated partition table at start
@@ -299,23 +300,21 @@ STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, siz
299
300
300
301
pyb_flash_obj_t * self = m_new_obj (pyb_flash_obj_t );
301
302
self -> base .type = & pyb_flash_type ;
302
- #if defined(SPIFLASH )
303
303
self -> use_native_block_size = false;
304
- #endif
305
304
306
305
uint32_t bl_len = (storage_get_block_count () - FLASH_PART1_START_BLOCK ) * FLASH_BLOCK_SIZE ;
307
306
308
307
mp_int_t start = args [ARG_start ].u_int ;
309
308
if (start == -1 ) {
310
309
start = 0 ;
311
- } else if (!(0 <= start && start < bl_len && start % PYB_FLASH_NATIVE_BLOCK_SIZE == 0 )) {
310
+ } else if (!(0 <= start && start < bl_len && start % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0 )) {
312
311
mp_raise_ValueError (NULL );
313
312
}
314
313
315
314
mp_int_t len = args [ARG_len ].u_int ;
316
315
if (len == -1 ) {
317
316
len = bl_len - start ;
318
- } else if (!(0 < len && start + len <= bl_len && len % PYB_FLASH_NATIVE_BLOCK_SIZE == 0 )) {
317
+ } else if (!(0 < len && start + len <= bl_len && len % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0 )) {
319
318
mp_raise_ValueError (NULL );
320
319
}
321
320
@@ -340,10 +339,10 @@ STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) {
340
339
else if (self != & pyb_flash_obj ) {
341
340
// Extended block read on a sub-section of the flash storage
342
341
uint32_t offset = mp_obj_get_int (args [3 ]);
343
- if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE ) >= self -> len ) {
342
+ if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT ) >= self -> len ) {
344
343
ret = - MP_EFAULT ; // Bad address
345
344
} else {
346
- block_num += self -> start / PYB_FLASH_NATIVE_BLOCK_SIZE ;
345
+ block_num += self -> start / MICROPY_HW_BDEV_BLOCKSIZE_EXT ;
347
346
ret = MICROPY_HW_BDEV_READBLOCKS_EXT (bufinfo .buf , block_num , offset , bufinfo .len );
348
347
}
349
348
}
@@ -367,10 +366,10 @@ STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
367
366
else if (self != & pyb_flash_obj ) {
368
367
// Extended block write on a sub-section of the flash storage
369
368
uint32_t offset = mp_obj_get_int (args [3 ]);
370
- if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE ) >= self -> len ) {
369
+ if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT ) >= self -> len ) {
371
370
ret = - MP_EFAULT ; // Bad address
372
371
} else {
373
- block_num += self -> start / PYB_FLASH_NATIVE_BLOCK_SIZE ;
372
+ block_num += self -> start / MICROPY_HW_BDEV_BLOCKSIZE_EXT ;
374
373
ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT (bufinfo .buf , block_num , offset , bufinfo .len );
375
374
}
376
375
}
@@ -390,11 +389,9 @@ STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
390
389
// Will be using extended block protocol
391
390
if (self == & pyb_flash_obj ) {
392
391
ret = -1 ;
393
- #if defined(SPIFLASH )
394
392
} else {
395
- // Switch to use native block size of SPI flash
393
+ // Switch to use native block size of the underlying storage.
396
394
self -> use_native_block_size = true;
397
- #endif
398
395
}
399
396
}
400
397
return MP_OBJ_NEW_SMALL_INT (ret );
@@ -411,10 +408,8 @@ STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
411
408
if (self == & pyb_flash_obj ) {
412
409
// Get true size
413
410
n = storage_get_block_count ();
414
- #if defined(SPIFLASH )
415
411
} else if (self -> use_native_block_size ) {
416
- n = self -> len / PYB_FLASH_NATIVE_BLOCK_SIZE ;
417
- #endif
412
+ n = self -> len / MICROPY_HW_BDEV_BLOCKSIZE_EXT ;
418
413
} else {
419
414
n = self -> len / FLASH_BLOCK_SIZE ;
420
415
}
@@ -423,20 +418,19 @@ STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
423
418
424
419
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE : {
425
420
mp_int_t n = FLASH_BLOCK_SIZE ;
426
- #if defined(SPIFLASH )
427
421
if (self -> use_native_block_size ) {
428
- n = PYB_FLASH_NATIVE_BLOCK_SIZE ;
422
+ n = MICROPY_HW_BDEV_BLOCKSIZE_EXT ;
429
423
}
430
- #endif
431
424
return MP_OBJ_NEW_SMALL_INT (n );
432
425
}
433
426
434
427
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE : {
435
428
int ret = 0 ;
436
- #if defined(SPIFLASH )
429
+ #if defined(MICROPY_HW_BDEV_ERASEBLOCKS_EXT )
437
430
if (self -> use_native_block_size ) {
438
- mp_int_t block_num = self -> start / PYB_FLASH_NATIVE_BLOCK_SIZE + mp_obj_get_int (arg_in );
439
- ret = spi_bdev_ioctl (SPIFLASH , BDEV_IOCTL_BLOCK_ERASE , block_num );
431
+ mp_int_t block_num = self -> start / MICROPY_HW_BDEV_BLOCKSIZE_EXT + mp_obj_get_int (arg_in );
432
+
433
+ ret = MICROPY_HW_BDEV_ERASEBLOCKS_EXT (block_num , MICROPY_HW_BDEV_BLOCKSIZE_EXT );
440
434
}
441
435
#endif
442
436
return MP_OBJ_NEW_SMALL_INT (ret );
0 commit comments