|
28 | 28 | #include <stdint.h> |
29 | 29 | #include <string.h> |
30 | 30 |
|
| 31 | +#include "py/objarray.h" |
31 | 32 | #include "py/runtime.h" |
32 | 33 | #include "py/mperrno.h" |
33 | 34 | #include "extmod/vfs_fat.h" |
|
36 | 37 | #include "led.h" |
37 | 38 | #include "storage.h" |
38 | 39 | #include "irq.h" |
| 40 | +#include "flash.h" |
| 41 | + |
| 42 | +typedef struct _pyb_flash_obj_t { |
| 43 | + mp_obj_base_t base; |
| 44 | + uint32_t start; // in bytes |
| 45 | + uint32_t len; // in bytes |
| 46 | +} pyb_flash_obj_t; |
39 | 47 |
|
40 | 48 | #if MICROPY_HW_ENABLE_STORAGE |
41 | 49 |
|
@@ -236,12 +244,6 @@ int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint3 |
236 | 244 | } |
237 | 245 | #endif |
238 | 246 |
|
239 | | -typedef struct _pyb_flash_obj_t { |
240 | | - mp_obj_base_t base; |
241 | | - uint32_t start; // in bytes |
242 | | - uint32_t len; // in bytes |
243 | | -} pyb_flash_obj_t; |
244 | | - |
245 | 247 | // This Flash object represents the entire available flash, with emulated partition table at start |
246 | 248 | const pyb_flash_obj_t pyb_flash_obj = { |
247 | 249 | { &pyb_flash_type }, |
@@ -427,3 +429,57 @@ void pyb_flash_init_vfs(fs_user_mount_t *vfs) { |
427 | 429 | } |
428 | 430 |
|
429 | 431 | #endif |
| 432 | + |
| 433 | +#if MICROPY_VFS_ROM |
| 434 | + |
| 435 | +extern uint32_t _micropy_hw_romfs_part0_start, _micropy_hw_romfs_part0_size; |
| 436 | + |
| 437 | +#define MICROPY_HW_ROMFS_BASE ((uint32_t)&_micropy_hw_romfs_part0_start) |
| 438 | +#define MICROPY_HW_ROMFS_BYTES ((uint32_t)&_micropy_hw_romfs_part0_size) |
| 439 | +#define VFSROM_BLOCK_SIZE (2048) |
| 440 | + |
| 441 | +static const MP_DEFINE_MEMORYVIEW_OBJ(romfs_obj, 'B', 0, MICROPY_HW_ROMFS_BYTES, (void *)MICROPY_HW_ROMFS_BASE); |
| 442 | + |
| 443 | +mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) { |
| 444 | + if (MICROPY_HW_ROMFS_BYTES <= 0) { |
| 445 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 446 | + } |
| 447 | + switch (mp_obj_get_int(args[0])) { |
| 448 | + case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS: |
| 449 | + return MP_OBJ_NEW_SMALL_INT(1); |
| 450 | + |
| 451 | + case MP_VFS_ROM_IOCTL_GET_SEGMENT: |
| 452 | + return MP_OBJ_FROM_PTR(&romfs_obj); |
| 453 | + |
| 454 | + case MP_VFS_ROM_IOCTL_WRITE_PREPARE: { |
| 455 | + // Erase sectors in given range. |
| 456 | + if (n_args < 3) { |
| 457 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 458 | + } |
| 459 | + uint32_t dest = MICROPY_HW_ROMFS_BASE; |
| 460 | + uint32_t dest_max = dest + mp_obj_get_int(args[2]); |
| 461 | + uint32_t sec_size = sector_size(dest); |
| 462 | + for (; dest < dest_max; dest += sec_size) { |
| 463 | + flash_erase(dest, sec_size); |
| 464 | + } |
| 465 | + return MP_OBJ_NEW_SMALL_INT(4); // minimum write size |
| 466 | + } |
| 467 | + |
| 468 | + case MP_VFS_ROM_IOCTL_WRITE: { |
| 469 | + // Write data to flash. |
| 470 | + if (n_args < 4) { |
| 471 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 472 | + } |
| 473 | + uint32_t dest = MICROPY_HW_ROMFS_BASE + mp_obj_get_int(args[2]); |
| 474 | + mp_buffer_info_t bufinfo; |
| 475 | + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); |
| 476 | + flash_write(dest, bufinfo.buf, bufinfo.len); |
| 477 | + return MP_OBJ_NEW_SMALL_INT(0); |
| 478 | + } |
| 479 | + |
| 480 | + default: |
| 481 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 482 | + } |
| 483 | +} |
| 484 | + |
| 485 | +#endif // MICROPY_VFS_ROM |
0 commit comments