Skip to content

Commit bea7645

Browse files
committed
stm32: Implement vfs.rom_ioctl with support for internal/external flash.
This commit implements `vfs.rom_ioctl()` to query, erase and write both internal and external flash, depending on how the board configures its flash memory. A board can configure ROM as follows. To use internal flash memory: #define MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH (1) To use external flash memory (QSPI memory mapped): #define MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI (1) #define MICROPY_HW_ROMFS_QSPI_SPIFLASH_OBJ (&spi_obj) Then the partition must be defined as symbols in the linker script: _micropy_hw_romfs_part1_start _micropy_hw_romfs_part1_size And finally the partition needs to be enabled: #define MICROPY_HW_ROMFS_ENABLE_PART1 (1) There's support for a second, optional partition via: _micropy_hw_romfs_part2_start _micropy_hw_romfs_part2_size #define MICROPY_HW_ROMFS_ENABLE_PART1 (1) Signed-off-by: Damien George <[email protected]>
1 parent 0c98c60 commit bea7645

File tree

6 files changed

+192
-2
lines changed

6 files changed

+192
-2
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ SRC_C += \
291291
storage.c \
292292
sdcard.c \
293293
sdram.c \
294+
vfs_rom_ioctl.c \
294295
fatfs_port.c \
295296
lcd.c \
296297
accel.c \

ports/stm32/mpconfigboard_common.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@
6767
#define MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET (1)
6868
#endif
6969

70+
// Whether to enable ROMFS on the internal flash.
71+
#ifndef MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH
72+
#define MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH (0)
73+
#endif
74+
75+
// Whether to enable ROMFS on external QSPI flash.
76+
#ifndef MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI
77+
#define MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI (0)
78+
#endif
79+
80+
// Whether to enable ROMFS partition 1.
81+
#ifndef MICROPY_HW_ROMFS_ENABLE_PART1
82+
#define MICROPY_HW_ROMFS_ENABLE_PART1 (0)
83+
#endif
84+
85+
// Whether to enable ROMFS partition 2.
86+
#ifndef MICROPY_HW_ROMFS_ENABLE_PART2
87+
#define MICROPY_HW_ROMFS_ENABLE_PART2 (0)
88+
#endif
89+
7090
// Whether to enable storage on the internal flash of the MCU
7191
#ifndef MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
7292
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1)

ports/stm32/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
#define MICROPY_SCHEDULER_STATIC_NODES (1)
8080
#define MICROPY_SCHEDULER_DEPTH (8)
8181
#define MICROPY_VFS (1)
82+
#ifndef MICROPY_VFS_ROM
83+
#define MICROPY_VFS_ROM (MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH || MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI)
84+
#endif
8285

8386
// control over Python builtins
8487
#ifndef MICROPY_PY_BUILTINS_HELP_TEXT

ports/stm32/qspi.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
#if defined(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2)
3636

37-
#define QSPI_MAP_ADDR (0x90000000)
38-
3937
#ifndef MICROPY_HW_QSPI_PRESCALER
4038
#define MICROPY_HW_QSPI_PRESCALER 3 // F_CLK = F_AHB/3 (72MHz when CPU is 216MHz)
4139
#endif

ports/stm32/qspi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@
2828

2929
#include "drivers/bus/qspi.h"
3030

31+
#define QSPI_MAP_ADDR (0x90000000)
32+
#define QSPI_MAP_ADDR_MAX (0xa0000000)
33+
3134
extern const mp_qspi_proto_t qspi_proto;
3235

3336
void qspi_init(void);
3437
void qspi_memory_map(void);
3538

39+
static inline bool qspi_is_valid_addr(uint32_t addr) {
40+
return QSPI_MAP_ADDR <= addr && addr < QSPI_MAP_ADDR_MAX;
41+
}
42+
3643
#endif // MICROPY_INCLUDED_STM32_QSPI_H

ports/stm32/vfs_rom_ioctl.c

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 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/obj.h"
28+
#include "py/objarray.h"
29+
#include "py/mperrno.h"
30+
#include "extmod/vfs.h"
31+
#include "drivers/memory/spiflash.h"
32+
33+
#include "flash.h"
34+
#include "qspi.h"
35+
#include "storage.h"
36+
37+
#if MICROPY_VFS_ROM_IOCTL
38+
39+
#if MICROPY_HW_ROMFS_ENABLE_PART1 && !defined(MICROPY_HW_ROMFS_PART1_START)
40+
#define MICROPY_HW_ROMFS_PART1_START (uintptr_t)(&_micropy_hw_romfs_part1_start)
41+
#define MICROPY_HW_ROMFS_PART1_SIZE (uintptr_t)(&_micropy_hw_romfs_part1_size)
42+
extern uint8_t _micropy_hw_romfs_part1_start;
43+
extern uint8_t _micropy_hw_romfs_part1_size;
44+
#endif
45+
46+
#if MICROPY_HW_ROMFS_ENABLE_PART2 && !defined(MICROPY_HW_ROMFS_PART2_START)
47+
#define MICROPY_HW_ROMFS_PART2_START (uintptr_t)(&_micropy_hw_romfs_part2_start)
48+
#define MICROPY_HW_ROMFS_PART2_SIZE (uintptr_t)(&_micropy_hw_romfs_part2_size)
49+
extern uint8_t _micropy_hw_romfs_part2_start;
50+
extern uint8_t _micropy_hw_romfs_part2_size;
51+
#endif
52+
53+
#define ROMFS_MEMORYVIEW(base, size) {{&mp_type_memoryview}, 'B', 0, (size), (void *)(base)}
54+
55+
static const mp_obj_array_t romfs_obj_table[] = {
56+
#if MICROPY_HW_ROMFS_ENABLE_PART1
57+
ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART1_START, MICROPY_HW_ROMFS_PART1_SIZE),
58+
#endif
59+
#if MICROPY_HW_ROMFS_ENABLE_PART2
60+
ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART2_START, MICROPY_HW_ROMFS_PART2_SIZE),
61+
#endif
62+
};
63+
64+
mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
65+
mp_int_t cmd = mp_obj_get_int(args[0]);
66+
if (cmd == MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS) {
67+
return MP_OBJ_NEW_SMALL_INT(MP_ARRAY_SIZE(romfs_obj_table));
68+
}
69+
70+
if (n_args < 2) {
71+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
72+
}
73+
74+
mp_int_t romfs_id = mp_obj_get_int(args[1]);
75+
if (!(0 <= romfs_id && romfs_id < MP_ARRAY_SIZE(romfs_obj_table))) {
76+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
77+
}
78+
79+
const mp_obj_array_t *romfs_obj = &romfs_obj_table[romfs_id];
80+
uintptr_t romfs_base = (uintptr_t)romfs_obj->items;
81+
uintptr_t romfs_len = romfs_obj->len;
82+
83+
if (cmd == MP_VFS_ROM_IOCTL_GET_SEGMENT) {
84+
// Return the ROMFS memoryview object.
85+
return MP_OBJ_FROM_PTR(romfs_obj);
86+
}
87+
88+
if (cmd == MP_VFS_ROM_IOCTL_WRITE_PREPARE) {
89+
// Erase sectors in given range.
90+
if (n_args < 3) {
91+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
92+
}
93+
uint32_t dest = romfs_base;
94+
uint32_t dest_max = dest + mp_obj_get_int(args[2]);
95+
if (dest_max > romfs_base + romfs_len) {
96+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
97+
}
98+
99+
#if MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH
100+
if (flash_is_valid_addr(dest)) {
101+
while (dest < dest_max) {
102+
int ret = flash_erase(dest);
103+
if (ret < 0) {
104+
return MP_OBJ_NEW_SMALL_INT(ret);
105+
}
106+
uint32_t sector_size = 0;
107+
flash_get_sector_info(dest, NULL, &sector_size);
108+
dest += sector_size;
109+
}
110+
return MP_OBJ_NEW_SMALL_INT(16);
111+
}
112+
#endif
113+
114+
#if MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI
115+
if (qspi_is_valid_addr(dest)) {
116+
dest -= QSPI_MAP_ADDR;
117+
dest_max -= QSPI_MAP_ADDR;
118+
while (dest < dest_max) {
119+
int ret = mp_spiflash_erase_block(MICROPY_HW_ROMFS_QSPI_SPIFLASH_OBJ, dest);
120+
if (ret < 0) {
121+
return MP_OBJ_NEW_SMALL_INT(ret);
122+
}
123+
dest += MP_SPIFLASH_ERASE_BLOCK_SIZE;
124+
}
125+
return MP_OBJ_NEW_SMALL_INT(4);
126+
}
127+
#endif
128+
}
129+
130+
if (cmd == MP_VFS_ROM_IOCTL_WRITE) {
131+
// Write data to flash.
132+
if (n_args < 4) {
133+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
134+
}
135+
uint32_t dest = romfs_base + mp_obj_get_int(args[2]);
136+
mp_buffer_info_t bufinfo;
137+
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
138+
if (dest + bufinfo.len > romfs_base + romfs_len) {
139+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
140+
}
141+
142+
#if MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH
143+
if (flash_is_valid_addr(dest)) {
144+
int ret = flash_write(dest, bufinfo.buf, bufinfo.len / 4);
145+
return MP_OBJ_NEW_SMALL_INT(ret);
146+
}
147+
#endif
148+
149+
#if MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI
150+
if (qspi_is_valid_addr(dest)) {
151+
dest -= QSPI_MAP_ADDR;
152+
int ret = mp_spiflash_write(MICROPY_HW_ROMFS_QSPI_SPIFLASH_OBJ, dest, bufinfo.len, bufinfo.buf);
153+
return MP_OBJ_NEW_SMALL_INT(ret);
154+
}
155+
#endif
156+
}
157+
158+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
159+
}
160+
161+
#endif // MICROPY_VFS_ROM_IOCTL

0 commit comments

Comments
 (0)