8000 rp2: Enable VfsRom. · dpgeorge/micropython@dfcb341 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfcb341

Browse files
committed
rp2: Enable VfsRom.
Signed-off-by: Damien George <damien@micropython.org>
1 parent e6b2259 commit dfcb341

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

ports/rp2/modules/_boot.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import vfs
2+
import sys
23
import machine, rp2
34

5+
# Try to mount the ROMFS.
6+
try:
7+
vfs.mount(vfs.VfsRom(vfs.rom_ioctl(2)), "/rom")
8+
sys.path.insert(0, "/rom")
9+
except:
10+
pass
411

512
# Try to mount the filesystem, and format the flash if it doesn't exist.
613
# Note: the flash requires the programming size to be aligned to 256 bytes.
@@ -12,4 +19,4 @@
1219
fs = vfs.VfsLfs2(bdev, progsize=256)
1320
vfs.mount(fs, "/")
1421

15-
del vfs, bdev, fs
22+
del vfs, sys, bdev, fs

ports/rp2/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
#define MICROPY_VFS (1)
171171
#define MICROPY_VFS_LFS2 (1)
172172
#define MICROPY_VFS_FAT (1)
173+
#define MICROPY_VFS_ROM (1)
173174
#define MICROPY_SSL_MBEDTLS (1)
174175
#define MICROPY_PY_LWIP_PPP (MICROPY_PY_NETWORK_PPP_LWIP)
175176
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)

ports/rp2/rp2_flash.c

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

2929
#include "py/mphal.h"
3030
#include "py/runtime.h"
31+
#include "py/mperrno.h"
3132
#include "extmod/vfs.h"
3233
#include "modrp2.h"
3334
#include "hardware/flash.h"
@@ -44,6 +45,9 @@ static_assert(MICROPY_HW_FLASH_STORAGE_BYTES % 4096 == 0, "Flash storage size mu
4445
#define MICROPY_HW_FLASH_STORAGE_BASE (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES)
4546
#endif
4647

48+
#define MICROPY_HW_ROMFS_BASE (512 * 1024) // leave 512k for firmware...
49+
#define MICROPY_HW_ROMFS_BYTES (MICROPY_HW_FLASH_STORAGE_BASE - MICROPY_HW_ROMFS_BASE)
50+
4751
static_assert(MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big");
4852
static_assert(MICROPY_HW_FLASH_STORAGE_BASE + MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big");
4953

@@ -59,6 +63,14 @@ static rp2_flash_obj_t rp2_flash_obj = {
5963
.flash_size = MICROPY_HW_FLASH_STORAGE_BYTES,
6064
};
6165

66+
#if MICROPY_HW_ROMFS_BYTES > 0
67+
static rp2_flash_obj_t rp2_flash_romfs_obj = {
68+
.base = { &rp2_flash_type },
69+
.flash_base = MICROPY_HW_ROMFS_BASE,
70+
.flash_size = MICROPY_HW_ROMFS_BYTES,
71+
};
72+
#endif
73+
6274
// Tag the flash drive in the binary as readable/writable (but not reformatable)
6375
bi_decl(bi_block_device(
6476
BINARY_INFO_TAG_MICROPYTHON,
@@ -138,6 +150,19 @@ static mp_obj_t rp2_flash_make_new(const mp_obj_type_t *type, size_t n_args, siz
138150
return MP_OBJ_FROM_PTR(self);
139151
}
140152

153+
static mp_int_t rp2_flash_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
154+
rp2_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
155+
if (flags == MP_BUFFER_READ) {
156+
bufinfo->buf = (void *)(XIP_BASE + self->flash_base);
157+
bufinfo->len = self->flash_size;
158+
bufinfo->typecode = 'B';
159+
return 0;
160+
} else {
161+
// Write unsupported.
162+
return 1;
163+
}
164+
}
165+
141166
static mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) {
142167
rp2_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]);
143168
uint32_t offset = mp_obj_get_int(args[1]) * BLOCK_SIZE_BYTES;
@@ -218,5 +243,19 @@ MP_DEFINE_CONST_OBJ_TYPE(
218243
MP_QSTR_Flash,
219244
MP_TYPE_FLAG_NONE,
220245
make_new, rp2_flash_make_new,
246+
buffer, rp2_flash_get_buffer,
221247
locals_dict, &rp2_flash_locals_dict
222248
);
249+
250+
mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
251+
switch (mp_obj_get_int(args[0])) {
252+
#if MICROPY_HW_ROMFS_BYTES > 0
253+
case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS:
254+
return MP_OBJ_NEW_SMALL_INT(1);
255+
case MP_VFS_ROM_IOCTL_GET_SEGMENT:
256+
return MP_OBJ_FROM_PTR(&rp2_flash_romfs_obj);
257+
#endif
258+
default:
259+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
260+
}
261+
}

0 commit comments

Comments
 (0)
0