8000 samd: Update to use new rom_ioctl protocol. · dpgeorge/micropython@d9111eb · GitHub
[go: up one dir, main page]

Skip to content

Commit d9111eb

Browse files
committed
samd: Update to use new rom_ioctl protocol.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 77f12f3 commit d9111eb

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

ports/samd/modules/_boot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55

66
try:
7-
vfs.mount(vfs.VfsRom(vfs.rom_ioctl(1)), "/rom")
7+
vfs.mount(vfs.VfsRom(vfs.rom_ioctl(2)), "/rom")
88
sys.path.insert(0, "/rom")
99
except:
1010
pass

ports/samd/samd_flash.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdio.h>
2828

29+
#include "py/objarray.h"
2930
#include "py/runtime.h"
3031
#include "extmod/vfs.h"
3132
#include "py/mperrno.h"
@@ -192,58 +193,58 @@ extern uint8_t _oflash_vfsrom, _sflash_vfsrom;
192193

193194
#define MICROPY_HW_ROMFS_BASE ((uint32_t)&_oflash_vfsrom)
194195
#define MICROPY_HW_ROMFS_BYTES ((uint32_t)&_sflash_vfsrom)
195-
#define VFSROM_BLOCK_SIZE (2048)
196196

197197
#if MICROPY_HW_MCUFLASH
198198
static samd_flash_obj_t samd_flash_romfs_obj = {
199199
.base = { &samd_flash_type },
200200
.flash_base = MICROPY_HW_ROMFS_BASE, // Get from MCU-Specific loader script.
201201
.flash_size = MICROPY_HW_ROMFS_BYTES, // Get from MCU-Specific loader script.
202202
};
203+
#else
204+
static const MP_DEFINE_MEMORYVIEW_OBJ(samd_flash_romfs_obj, 'B', 0, MICROPY_HW_ROMFS_BYTES, (void *)MICROPY_HW_ROMFS_BASE);
203205
#endif
204206

205207
mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
206208
if (MICROPY_HW_ROMFS_BYTES <= 0) {
207209
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
208210
}
209211
switch (mp_obj_get_int(args[0])) {
210-
#if MICROPY_HW_MCUFLASH
211-
case -1: // request object-based capabilities
212+
case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS:
213+
return MP_OBJ_NEW_SMALL_INT(1);
214+
215+
case MP_VFS_ROM_IOCTL_GET_SEGMENT:
212216
return MP_OBJ_FROM_PTR(&samd_flash_romfs_obj);
213-
#endif
214217

215-
case 0: // number of segments
216-
return MP_OBJ_NEW_SMALL_INT(1);
217-
case 1: // address
218-
return mp_obj_new_int(MICROPY_HW_ROMFS_BASE);
219-
220-
#if !MICROPY_HW_MCUFLASH
221-
case 2: // num blocks
222-
return MP_OBJ_NEW_SMALL_INT(MICROPY_HW_ROMFS_BYTES / VFSROM_BLOCK_SIZE);
223-
case 3: // block_size
224-
return MP_OBJ_NEW_SMALL_INT(VFSROM_BLOCK_SIZE);
225-
case 4: { // erase one block at offset address of flash
226-
if (n_args < 2) {
218+
#if !MICROPY_HW_MCUFLASH
219+
220+
case MP_VFS_ROM_IOCTL_WRITE_PREPARE: {
221+
// Erase sectors in given range.
222+
if (n_args < 3) {
227223
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
228224
}
229-
// Erase sector.
230-
uint32_t dest_addr = MICROPY_HW_ROMFS_BASE + mp_obj_get_int(args[1]);
225+
uint32_t dest_addr = MICROPY_HW_ROMFS_BASE;
226+
uint32_t dest_addr_max = dest_addr + mp_obj_get_int(args[2]);
231227
mp_int_t page_size = flash_get_page_size(&flash_desc); // adf4 API call
232-
flash_erase(&flash_desc, dest_addr, (VFSROM_BLOCK_SIZE / page_size));
228+
for (; dest_addr < dest_addr_max; dest_addr += page_size) {
229+
flash_erase(&flash_desc, dest_addr, 1);
230+
}
233231
return MP_OBJ_NEW_SMALL_INT(0);
234232
}
235-
case 5: { // write to byte offset address in flash
236-
if (n_args < 3) {
233+
234+
case MP_VFS_ROM_IOCTL_WRITE: {
235+
// Write data to flash.
236+
if (n_args < 4) {
237237
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
238238
}
239-
uint32_t dest_addr = MICROPY_HW_ROMFS_BASE + mp_obj_get_int(args[1]);
239+
uint32_t dest_addr = MICROPY_HW_ROMFS_BASE + mp_obj_get_int(args[2]);
240240
mp_buffer_info_t bufinfo;
241-
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
242-
// Write data to flash.
241+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
243242
flash_write(&flash_desc, dest_addr, bufinfo.buf, bufinfo.len);
244243
return MP_OBJ_NEW_SMALL_INT(0);
245244
}
246-
#endif
245+
246+
#endif
247+
247248
default:
248249
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
249250
}

0 commit comments

Comments
 (0)
0