Skip to content

Commit b24b4f7

Browse files
committed
ports/rp2: Add start/len support to rp2.Flash().
Allow support for partitioned flash on RP2. Signed-off-by: Phil Howard <phil@gadgetoid.com>
1 parent 5852fd7 commit b24b4f7

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

ports/rp2/rp2_flash.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,45 @@ bi_decl(bi_block_device(
7070
BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN));
7171

7272
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) {
73-
// Check args.
74-
mp_arg_check_num(n_args, n_kw, 0, 0, false);
73+
// Parse arguments
74+
enum { ARG_start, ARG_len };
75+
static const mp_arg_t allowed_args[] = {
76+
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
77+
{ MP_QSTR_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
78+
};
79+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
80+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
81+
82+
if (args[ARG_start].u_int == -1 && args[ARG_len].u_int == -1) {
83+
#ifndef NDEBUG
84+
extern char __flash_binary_end;
85+
assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE);
86+
#endif
87+
88+
// Default singleton object that accesses entire flash
89+
return MP_OBJ_FROM_PTR(&rp2_flash_obj);
90+
}
91+
92+
rp2_flash_obj_t *self = mp_obj_malloc(rp2_flash_obj_t, &rp2_flash_type);
93+
94+
mp_int_t start = args[ARG_start].u_int;
95+
if (start == -1) {
96+
start = 0;
97+
} else if (!(0 <= start && start < MICROPY_HW_FLASH_STORAGE_BYTES && start % BLOCK_SIZE_BYTES == 0)) {
98+
mp_raise_ValueError(NULL);
99+
}
100+
101+
mp_int_t len = args[ARG_len].u_int;
102+
if (len == -1) {
103+
len = MICROPY_HW_FLASH_STORAGE_BYTES;
104+
} else if (!(0 < len && start + len <= MICROPY_HW_FLASH_STORAGE_BYTES && len % BLOCK_SIZE_BYTES == 0)) {
105+
mp_raise_ValueError(NULL);
106+
}
75107

76-
#ifndef NDEBUG
77-
extern char __flash_binary_end;
78-
assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE);
79-
#endif
108+
self->flash_base = MICROPY_HW_FLASH_STORAGE_BASE + start;
109+
self->flash_size = len;
80110

81-
// Return singleton object.
82-
return MP_OBJ_FROM_PTR(&rp2_flash_obj);
111+
return MP_OBJ_FROM_PTR(self);
83112
}
84113

85114
STATIC mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) {

0 commit comments

Comments
 (0)