Damien George | 75ff8e5 | 2024-11-15 11:18:09 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2024-2025 Damien P. George |
| 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 "extmod/vfs.h" |
| 30 | #include "ets_alt_task.h" |
| 31 | #include "user_interface.h" |
| 32 | |
| 33 | #if MICROPY_VFS_ROM_IOCTL |
| 34 | |
| 35 | #define FLASH_MEM_BASE (0x40200000) |
| 36 | #define FLASH_PAGE_SIZE (4096) |
| 37 | |
Damien George | d5aeca2 | 2025-03-11 13:23:50 +1100 | [diff] [blame] | 38 | #define MICROPY_HW_ROMFS_BASE (uintptr_t)(&_micropy_hw_romfs_part0_start) |
| 39 | #define MICROPY_HW_ROMFS_BYTES (uintptr_t)(&_micropy_hw_romfs_part0_size) |
Damien George | 75ff8e5 | 2024-11-15 11:18:09 +1100 | [diff] [blame] | 40 | |
| 41 | #define ROMFS_SPI_FLASH_OFFSET (MICROPY_HW_ROMFS_BASE - FLASH_MEM_BASE) |
| 42 | |
Damien George | d5aeca2 | 2025-03-11 13:23:50 +1100 | [diff] [blame] | 43 | extern uint8_t _micropy_hw_romfs_part0_start; |
| 44 | extern uint8_t _micropy_hw_romfs_part0_size; |
Damien George | 75ff8e5 | 2024-11-15 11:18:09 +1100 | [diff] [blame] | 45 | |
| 46 | static const MP_DEFINE_MEMORYVIEW_OBJ(romfs_obj, 'B', 0, MICROPY_HW_ROMFS_BYTES, (void *)MICROPY_HW_ROMFS_BASE); |
| 47 | |
| 48 | mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) { |
| 49 | switch (mp_obj_get_int(args[0])) { |
| 50 | case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS: |
| 51 | return MP_OBJ_NEW_SMALL_INT(1); |
| 52 | |
| 53 | case MP_VFS_ROM_IOCTL_GET_SEGMENT: |
| 54 | return MP_OBJ_FROM_PTR(&romfs_obj); |
| 55 | |
| 56 | case MP_VFS_ROM_IOCTL_WRITE_PREPARE: { |
| 57 | uint32_t dest = ROMFS_SPI_FLASH_OFFSET; |
| 58 | uint32_t dest_max = dest + mp_obj_get_int(args[2]); |
| 59 | while (dest < dest_max) { |
| 60 | ets_loop_iter(); // flash access takes time so run any pending tasks |
| 61 | SpiFlashOpResult res = spi_flash_erase_sector(dest / FLASH_PAGE_SIZE); |
| 62 | ets_loop_iter(); |
| 63 | if (res != SPI_FLASH_RESULT_OK) { |
| 64 | return MP_OBJ_NEW_SMALL_INT(res == SPI_FLASH_RESULT_TIMEOUT ? -MP_ETIMEDOUT : -MP_EIO); |
| 65 | } |
| 66 | dest += FLASH_PAGE_SIZE; |
| 67 | } |
| 68 | return MP_OBJ_NEW_SMALL_INT(4); // minimum write size |
| 69 | } |
| 70 | |
| 71 | case MP_VFS_ROM_IOCTL_WRITE: { |
| 72 | mp_int_t offset = ROMFS_SPI_FLASH_OFFSET + mp_obj_get_int(args[2]); |
| 73 | mp_buffer_info_t bufinfo; |
| 74 | mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); |
| 75 | ets_loop_iter(); // flash access takes time so run any pending tasks |
| 76 | SpiFlashOpResult res = spi_flash_write(offset, bufinfo.buf, (bufinfo.len + 3) & ~3); |
| 77 | ets_loop_iter(); |
| 78 | if (res == SPI_FLASH_RESULT_OK) { |
| 79 | return MP_OBJ_NEW_SMALL_INT(0); |
| 80 | } |
| 81 | return MP_OBJ_NEW_SMALL_INT(res == SPI_FLASH_RESULT_TIMEOUT ? -MP_ETIMEDOUT : -MP_EIO); |
| 82 | } |
| 83 | |
| 84 | default: |
| 85 | return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #endif // MICROPY_VFS_ROM_IOCTL |