Skip to content

Commit 75ff8e5

Browse files
committed
esp8266: Implement vfs.rom_ioctl with support for external flash.
Not enabled by default on any board. For a board to enable ROMFS it must: - Add `#define MICROPY_VFS_ROM (1)` to its `mpconfigboard.h` file. - Add a FLASH_ROMFS partition to the linker script and expose the partition with: _micropy_hw_romfs_start = ORIGIN(FLASH_ROMFS); _micropy_hw_romfs_size = LENGTH(FLASH_ROMFS); Signed-off-by: Damien George <damien@micropython.org>
1 parent 0255cb7 commit 75ff8e5

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

ports/esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ SRC_C = \
134134
fatfs_port.c \
135135
posix_helpers.c \
136136
hspi.c \
137+
vfs_rom_ioctl.c \
137138
$(wildcard $(BOARD_DIR)/*.c) \
138139

139140
ifeq ($(MICROPY_PY_ESPNOW),1)

ports/esp8266/boards/esp8266_common.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ SECTIONS
170170
*modonewire.o(.literal* .text*)
171171
*network_wlan.o(.literal* .text*)
172172
*esp_mphal.o(.literal* .text*)
173+
*vfs_rom_ioctl.o(.literal* .text*)
173174

174175
/* we put as much rodata as possible in this section */
175176
/* note that only rodata accessed as a machine word is allowed here */

ports/esp8266/modesp.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,16 @@ static MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size);
164164
#define IS_OTA_FIRMWARE() ((*(uint32_t *)0x40200000 & 0xff00) == 0x100)
165165

166166
extern byte _firmware_size[];
167+
#if MICROPY_VFS_ROM_IOCTL
168+
extern uint8_t _micropy_hw_romfs_size;
169+
#endif
167170

168171
static mp_obj_t esp_flash_user_start(void) {
169-
return MP_OBJ_NEW_SMALL_INT((uint32_t)_firmware_size);
172+
uint32_t flash_user_start = (uint32_t)_firmware_size;
173+
#if MICROPY_VFS_ROM_IOCTL
174+
flash_user_start += (uint32_t)&_micropy_hw_romfs_size;
175+
#endif
176+
return MP_OBJ_NEW_SMALL_INT(flash_user_start);
170177
}
171178
static MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_user_start_obj, esp_flash_user_start);
172179

ports/esp8266/vfs_rom_ioctl.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
38+
#define MICROPY_HW_ROMFS_BASE (uintptr_t)(&_micropy_hw_romfs_start)
39+
#define MICROPY_HW_ROMFS_BYTES (uintptr_t)(&_micropy_hw_romfs_size)
40+
41+
#define ROMFS_SPI_FLASH_OFFSET (MICROPY_HW_ROMFS_BASE - FLASH_MEM_BASE)
42+
43+
extern uint8_t _micropy_hw_romfs_start;
44+
extern uint8_t _micropy_hw_romfs_size;
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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy