8000 samd/samd_flash: Add the buffer protocol to samd_flash_type. · dpgeorge/micropython@31e56a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31e56a2

Browse files
robert-hhdpgeorge
authored andcommitted
samd/samd_flash: Add the buffer protocol to samd_flash_type.
It is needed for VfsRom at devices without external flash. Simplify the code for "case MP_VFS_ROM_IOCTL_WRITE_PREPARE:". Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent ca78107 commit 31e56a2

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

ports/samd/samd_flash.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ static mp_obj_t samd_flash_make_new(const mp_obj_type_t *type, size_t n_args, si
7272
// Return singleton object.
7373
return MP_OBJ_FROM_PTR(&samd_flash_obj);
7474
}
75+
76+
static mp_int_t samd_flash_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
77+
samd_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
78+
if (flags == MP_BUFFER_READ) {
79+
bufinfo->buf = (void *)((uintptr_t)self->flash_base);
80+
bufinfo->len = self->flash_size;
81+
bufinfo->typecode = 'B';
82+
return 0;
83+
} else {
84+
// Write unsupported.
85+
return 1;
86+
}
87+
}
7588
#endif // MICROPY_HW_MCUFLASH
7689

7790
// Flash init (from cctpy)
@@ -179,6 +192,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
179192
MP_QSTR_Flash,
180193
MP_TYPE_FLAG_NONE,
181194
make_new, samd_flash_make_new,
195+
buffer, samd_flash_get_buffer,
182196
locals_dict, &samd_flash_locals_dict
183197
);
184198
#endif
@@ -223,11 +237,8 @@ mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
223237
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
224238
}
225239
uint32_t dest_addr = MICROPY_HW_ROMFS_BASE;
226-
uint32_t dest_addr_max = dest_addr + mp_obj_get_int(args[2]);
227240
mp_int_t page_size = flash_get_page_size(&flash_desc); // adf4 API call
228-
for (; dest_addr < dest_addr_max; dest_addr += page_size) {
229-
flash_erase(&flash_desc, dest_addr, 1);
230-
}
241+
flash_erase(&flash_desc, dest_addr, mp_obj_get_int(args[2]) / page_size);
231242
return MP_OBJ_NEW_SMALL_INT(0);
232243
}
233244

0 commit comments

Comments
 (0)
0