diff options
| author | Phil Howard <phil@gadgetoid.com> | 2022-09-05 11:20:10 +0100 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2022-09-08 14:37:41 +1000 |
| commit | 6aa3c946347281875165392c09753547d8c77fc3 (patch) | |
| tree | e142375841de0a8f1afec0be6a3d9c8d1cb1212d | |
| parent | 24678fe452e9c0d0b96575424f81fb4a5f5f4302 (diff) | |
rp2/rp2_flash: Add start/len support to rp2.Flash() constructor.
This allows support for partitioned flash on rp2 boards.
See issue #9208.
Signed-off-by: Phil Howard <phil@gadgetoid.com>
| -rw-r--r-- | ports/rp2/rp2_flash.c | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index db6b9f3bf..47c95ea5c 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -70,16 +70,45 @@ bi_decl(bi_block_device( BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN)); STATIC mp_obj_t rp2_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - // Check args. - mp_arg_check_num(n_args, n_kw, 0, 0, false); + // Parse arguments + enum { ARG_start, ARG_len }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_start].u_int == -1 && args[ARG_len].u_int == -1) { + #ifndef NDEBUG + extern char __flash_binary_end; + assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE); + #endif + + // Default singleton object that accesses entire flash + return MP_OBJ_FROM_PTR(&rp2_flash_obj); + } + + rp2_flash_obj_t *self = mp_obj_malloc(rp2_flash_obj_t, &rp2_flash_type); + + mp_int_t start = args[ARG_start].u_int; + if (start == -1) { + start = 0; + } else if (!(0 <= start && start < MICROPY_HW_FLASH_STORAGE_BYTES && start % BLOCK_SIZE_BYTES == 0)) { + mp_raise_ValueError(NULL); + } + + mp_int_t len = args[ARG_len].u_int; + if (len == -1) { + len = MICROPY_HW_FLASH_STORAGE_BYTES - start; + } else if (!(0 < len && start + len <= MICROPY_HW_FLASH_STORAGE_BYTES && len % BLOCK_SIZE_BYTES == 0)) { + mp_raise_ValueError(NULL); + } - #ifndef NDEBUG - extern char __flash_binary_end; - assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE); - #endif + self->flash_base = MICROPY_HW_FLASH_STORAGE_BASE + start; + self->flash_size = len; - // Return singleton object. - return MP_OBJ_FROM_PTR(&rp2_flash_obj); + return MP_OBJ_FROM_PTR(self); } STATIC mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) { |
