|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 OpenMV LLC. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/mperrno.h" |
| 28 | +#include "py/objarray.h" |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "extmod/vfs.h" |
| 31 | +#include "modalif.h" |
| 32 | +#include "mpu.h" |
| 33 | +#include "mram.h" |
| 34 | +#include "ospi_flash.h" |
| 35 | + |
| 36 | +#if MICROPY_VFS_ROM_IOCTL |
| 37 | + |
| 38 | +#if MICROPY_HW_ROMFS_ENABLE_PART0 && !defined(MICROPY_HW_ROMFS_PART0_START) |
| 39 | +#define MICROPY_HW_ROMFS_PART0_START (uintptr_t)(&_micropy_hw_romfs_part0_start) |
| 40 | +#define MICROPY_HW_ROMFS_PART0_SIZE (uintptr_t)(&_micropy_hw_romfs_part0_size) |
| 41 | +extern uint8_t _micropy_hw_romfs_part0_start; |
| 42 | +extern uint8_t _micropy_hw_romfs_part0_size; |
| 43 | +#endif |
| 44 | + |
| 45 | +#if MICROPY_HW_ROMFS_ENABLE_PART1 && !defined(MICROPY_HW_ROMFS_PART1_START) |
| 46 | +#define MICROPY_HW_ROMFS_PART1_START (uintptr_t)(&_micropy_hw_romfs_part1_start) |
| 47 | +#define MICROPY_HW_ROMFS_PART1_SIZE (uintptr_t)(&_micropy_hw_romfs_part1_size) |
| 48 | +extern uint8_t _micropy_hw_romfs_part1_start; |
| 49 | +extern uint8_t _micropy_hw_romfs_part1_size; |
| 50 | +#endif |
| 51 | + |
| 52 | +#define ROMFS_MEMORYVIEW(base, size) {{&mp_type_memoryview}, 'B', 0, (size), (void *)(base)} |
| 53 | + |
| 54 | +static const mp_obj_array_t romfs_obj_table[] = { |
| 55 | + #if MICROPY_HW_ROMFS_ENABLE_PART0 |
| 56 | + ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART0_START, MICROPY_HW_ROMFS_PART0_SIZE), |
| 57 | + #endif |
| 58 | + #if MICROPY_HW_ROMFS_ENABLE_PART1 |
| 59 | + ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART1_START, MICROPY_HW_ROMFS_PART1_SIZE), |
| 60 | + #endif |
| 61 | +}; |
| 62 | + |
| 63 | +static inline bool mram_is_valid_addr(uintptr_t addr) { |
| 64 | + return MRAM_BASE <= addr && addr < MRAM_BASE + MRAM_SIZE; |
| 65 | +} |
| 66 | + |
| 67 | +static inline bool ospi_is_valid_addr(uintptr_t xip_base, uintptr_t addr) { |
| 68 | + MP_STATIC_ASSERT(OSPI0_XIP_SIZE == OSPI1_XIP_SIZE); |
| 69 | + return xip_base <= addr && addr < xip_base + OSPI0_XIP_SIZE; |
| 70 | +} |
| 71 | + |
| 72 | +mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) { |
| 73 | + mp_int_t cmd = mp_obj_get_int(args[0]); |
| 74 | + if (cmd == MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS) { |
| 75 | + return MP_OBJ_NEW_SMALL_INT(MP_ARRAY_SIZE(romfs_obj_table)); |
| 76 | + } |
| 77 | + |
| 78 | + if (n_args < 2) { |
| 79 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 80 | + } |
| 81 | + |
| 82 | + mp_int_t romfs_id = mp_obj_get_int(args[1]); |
| 83 | + if (!(0 <= romfs_id && romfs_id < MP_ARRAY_SIZE(romfs_obj_table))) { |
| 84 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 85 | + } |
| 86 | + |
| 87 | + const mp_obj_array_t *romfs_obj = &romfs_obj_table[romfs_id]; |
| 88 | + |
| 89 | + if (cmd == MP_VFS_ROM_IOCTL_GET_SEGMENT) { |
| 90 | + // Return the ROMFS memoryview object. |
| 91 | + return MP_OBJ_FROM_PTR(romfs_obj); |
| 92 | + } |
| 93 | + |
| 94 | + uintptr_t romfs_base = (uintptr_t)romfs_obj->items; |
| 95 | + uintptr_t romfs_len = romfs_obj->len; |
| 96 | + |
| 97 | + #if MICROPY_HW_ENABLE_OSPI |
| 98 | + const uintptr_t ospi_base = ospi_flash_get_xip_base(); |
| 99 | + #endif |
| 100 | + |
| 101 | + if (cmd == MP_VFS_ROM_IOCTL_WRITE_PREPARE) { |
| 102 | + if (n_args < 3) { |
| 103 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 104 | + } |
| 105 | + uint32_t dest = romfs_base; |
| 106 | + uint32_t dest_max = dest + mp_obj_get_int(args[2]); |
| 107 | + if (dest_max > romfs_base + romfs_len) { |
| 108 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 109 | + } |
| 110 | + |
| 111 | + if (mram_is_valid_addr(dest)) { |
| 112 | + // No preparation needed for MRAM. |
| 113 | + // Return the minimum write size. |
| 114 | + return MP_OBJ_NEW_SMALL_INT(MRAM_SECTOR_SIZE); |
| 115 | + } |
| 116 | + |
| 117 | + #if MICROPY_HW_ENABLE_OSPI |
| 118 | + if (ospi_is_valid_addr(ospi_base, dest)) { |
| 119 | + // Erase OSPI flash. |
| 120 | + dest -= ospi_base; |
| 121 | + dest_max -= ospi_base; |
| 122 | + while (dest < dest_max) { |
| 123 | + int ret = ospi_flash_erase_sector(dest); |
| 124 | + mp_event_handle_nowait(); |
| 125 | + if (ret < 0) { |
| 126 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 127 | + } |
| 128 | + dest += MICROPY_HW_FLASH_BLOCK_SIZE_BYTES; |
| 129 | + } |
| 130 | + // Return the minimum write size. |
| 131 | + return MP_OBJ_NEW_SMALL_INT(4); |
| 132 | + } |
| 133 | + #endif |
| 134 | + } |
| 135 | + |
| 136 | + if (cmd == MP_VFS_ROM_IOCTL_WRITE) { |
| 137 | + if (n_args < 4) { |
| 138 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 139 | + } |
| 140 | + uint32_t dest = romfs_base + mp_obj_get_int(args[2]); |
| 141 | + mp_buffer_info_t bufinfo; |
| 142 | + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); |
| 143 | + if (dest + bufinfo.len > romfs_base + romfs_len) { |
| 144 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 145 | + } |
| 146 | + |
| 147 | + if (mram_is_valid_addr(dest)) { |
| 148 | + // Write data to MRAM. |
| 149 | + mpu_config_mram(false); |
| 150 | + const uint8_t *src = bufinfo.buf; |
| 151 | + const uint8_t *max = src + bufinfo.len; |
| 152 | + while (src < max) { |
| 153 | + mram_write_128bit((uint8_t *)dest, src); |
| 154 | + dest += MRAM_SECTOR_SIZE; |
| 155 | + src += MRAM_SECTOR_SIZE; |
| 156 | + } |
| 157 | + mpu_config_mram(true); |
| 158 | + return MP_OBJ_NEW_SMALL_INT(0); // success |
| 159 | + } |
| 160 | + |
| 161 | + #if MICROPY_HW_ENABLE_OSPI |
| 162 | + if (ospi_is_valid_addr(ospi_base, dest)) { |
| 163 | + // Write data to OSPI flash. |
| 164 | + dest -= ospi_base; |
| 165 | + int ret = ospi_flash_write(dest, bufinfo.len, bufinfo.buf); |
| 166 | + mp_event_handle_nowait(); |
| 167 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 168 | + } |
| 169 | + #endif |
| 170 | + } |
| 171 | + |
| 172 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 173 | +} |
| 174 | + |
| 175 | +#endif // MICROPY_VFS_ROM_IOCTL |
0 commit comments