Skip to content

Commit d895a62

Browse files
committed
alif/alif_flash: Make flash respond to the buffer protocol.
Signed-off-by: Damien George <[email protected]>
1 parent af574a8 commit d895a62

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

ports/alif/alif_flash.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ static mp_obj_t alif_flash_make_new(const mp_obj_type_t *type, size_t n_args, si
8080
return MP_OBJ_FROM_PTR(self);
8181
}
8282

83+
static mp_int_t alif_flash_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
84+
alif_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
85+
if (flags == MP_BUFFER_READ) {
86+
bufinfo->buf = (void *)(ospi_flash_get_xip_base() + self->flash_base_addr);
87+
bufinfo->len = self->flash_size;
88+
bufinfo->typecode = 'B';
89+
return 0;
90+
} else {
91+
// Can't return a writable buffer.
92+
return 1;
93+
}
94+
}
95+
8396
static mp_obj_t alif_flash_readblocks(size_t n_args, const mp_obj_t *args) {
8497
alif_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]);
8598
uint32_t offset = mp_obj_get_int(args[1]) * MICROPY_HW_FLASH_BLOCK_SIZE_BYTES;
@@ -157,6 +170,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
157170
MP_QSTR_Flash,
158171
MP_TYPE_FLAG_NONE,
159172
make_new, alif_flash_make_new,
173+
buffer, alif_flash_get_buffer,
160174
locals_dict, &alif_flash_locals_dict
161175
);
162176
#endif // MICROPY_HW_ENABLE_OSPI

0 commit comments

Comments
 (0)