Skip to content

Commit 6105520

Browse files
robert-hhdpgeorge
authored andcommitted
nrf: Add support for VfsRom filesystem.
The implementation uses the object based capabilities, which avoids complication about different flash block sizes. The ROM partition is placed between the text and writable filesystem sections, and the latter size is unchanged. VfsRom sizes are: - NRF51x22: 12K - NRF52832: 128K - NRF52840: 256K - NRF9160: 256K Use frozen `_boot.py` to set up and mount the filesystem, replacing a mix of C-Code and Python code. The mkfs part has been simplified to save code. Tested with Microbit and Arduino Nano Connect. Signed-off-by: Damien George <damien@micropython.org> Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent cee9506 commit 6105520

12 files changed

Lines changed: 89 additions & 42 deletions

File tree

ports/nrf/boards/memory.ld

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ _head_size = DEFINED(_sd_size) ? _sd_size : _bootloader_head_size;
88
_head_ram = DEFINED(_sd_ram) ? _sd_ram : _bootloader_head_ram_size;
99
_sd_size = DEFINED(_sd_size) ? _sd_size : 0;
1010
_sd_ram = DEFINED(_sd_ram) ? _sd_ram : 0;
11+
_micropy_hw_romfs_part0_size = DEFINED(_micropy_hw_romfs_part0_size) ? _micropy_hw_romfs_part0_size : 0;
1112
_fs_size = DEFINED(_fs_size) ? _fs_size : 64K; /* TODO: set to 0 if not using the filesystem */
12-
_app_size = _flash_size - _head_size - _fs_size - _bootloader_tail_size;
13+
_app_size = _flash_size - _head_size - _micropy_hw_romfs_part0_size - _fs_size - _bootloader_tail_size;
1314
_app_start = _head_size;
14-
_fs_start = _head_size + _app_size;
15+
_micropy_hw_romfs_part0_start = _head_size + _app_size;
16+
_fs_start = _micropy_hw_romfs_part0_start + _micropy_hw_romfs_part0_size;
1517
_fs_end = _fs_start + _fs_size;
1618
_app_ram_start = 0x20000000 + _head_ram;
1719
_app_ram_size = _ram_size - _head_ram;

ports/nrf/boards/nrf51x22_256k_16k.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GROUP(-lgcc -lc -lnosys)
66

77
_flash_size = 256K;
88
_ram_size = 16K;
9+
_micropy_hw_romfs_part0_size = 12K;
910

1011
/* Default stack size when there is no SoftDevice */
1112
_stack_size = 4K;

ports/nrf/boards/nrf51x22_256k_32k.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GROUP(-lgcc -lc -lnosys)
66

77
_flash_size = 256K;
88
_ram_size = 32K;
9+
_micropy_hw_romfs_part0_size = 12K;
910

1011
/* Default stack size when there is no SoftDevice */
1112
_stack_size = 4K;

ports/nrf/boards/nrf52832_512k_64k.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
_flash_size = 512K;
66
_ram_size = 64K;
7+
_micropy_hw_romfs_part0_size = 128K;
78

89
/* produce a link error if there is not this amount of RAM for these sections */
910
_stack_size = 8K;

ports/nrf/boards/nrf52840_1M_256k.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
_flash_size = 1M;
66
_ram_size = 256K;
7+
_micropy_hw_romfs_part0_size = 256K;
78

89
/* produce a link error if there is not this amount of RAM for these sections */
910
_stack_size = 8K;

ports/nrf/boards/nrf9160_1M_256k.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ _ram_size = 256K;
77
_sd_size = 0x00008000;
88
_sd_ram = 0x00020000;
99
_fs_size = 80K;
10+
_micropy_hw_romfs_part0_size = 256K;
1011

1112
/* produce a link error if there is not this amount of RAM for these sections */
1213
_stack_size = 32K;

ports/nrf/main.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,8 @@ void MP_NORETURN _start(void) {
177177

178178
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
179179
flashbdev_init();
180-
181-
// Try to mount the flash on "/flash" and chdir to it for the boot-up directory.
182-
mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash);
183-
int ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
184-
185-
if ((ret == -MP_ENODEV) || (ret == -MP_EIO)) {
186-
pyexec_frozen_module("_mkfs.py", false); // Frozen script for formatting flash filesystem.
187-
ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
188-
}
189-
190-
if (ret != 0) {
191-
printf("MPY: can't mount flash\n");
192-
} else {
193-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
194-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
195-
}
180+
// Execute _boot.py to set up the filesystem.
181+
pyexec_frozen_module("_boot.py", false);
196182
#endif
197183

198184
#if MICROPY_MBFS

ports/nrf/modules/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
module("_mkfs.py", base_path="$(PORT_DIR)/modules/scripts", opt=3)
1+
module("_boot.py", base_path="$(PORT_DIR)/modules/scripts", opt=3)
22
include("$(MPY_DIR)/extmod/asyncio")

ports/nrf/modules/nrf/flashbdev.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,26 @@ static mp_obj_t nrf_flashbdev_make_new(const mp_obj_type_t *type, size_t n_args,
183183
return MP_OBJ_FROM_PTR(self);
184184
}
185185

186+
static mp_int_t nrf_flashbdev_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
187+
nrf_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
188+
if (flags == MP_BUFFER_READ) {
189+
bufinfo->buf = (void *)self->start;
190+
bufinfo->len = self->len;
191+
bufinfo->typecode = 'B';
192+
return 0;
193+
} else {
194+
// Unsupported.
195+
return 1;
196+
}
197+
}
198+
186199
MP_DEFINE_CONST_OBJ_TYPE(
187200
nrf_flashbdev_type,
188201
MP_QSTR_Flash,
189202
MP_TYPE_FLAG_NONE,
190203
make_new, nrf_flashbdev_make_new,
191204
print, nrf_flashbdev_print,
205+
buffer, nrf_flashbdev_get_buffer,
192206
locals_dict, &nrf_flashbdev_locals_dict
193207
);
194208

@@ -202,4 +216,34 @@ void flashbdev_init(void) {
202216
nrf_flash_obj.len = num_pages * FLASH_PAGESIZE;
203217
}
204218

219+
#if MICROPY_VFS_ROM
220+
221+
extern byte _micropy_hw_romfs_part0_start[];
222+
extern byte _micropy_hw_romfs_part0_size[];
223+
224+
#define MICROPY_HW_ROMFS_BASE ((uint32_t)_micropy_hw_romfs_part0_start)
225+
#define MICROPY_HW_ROMFS_BYTES ((uint32_t)_micropy_hw_romfs_part0_size)
226+
227+
static nrf_flash_obj_t nrf_flash_romfs_obj = {
228+
.base = { &nrf_flashbdev_type },
229+
.start = MICROPY_HW_ROMFS_BASE, // Get from MCU-Specific loader script.
230+
.len = MICROPY_HW_ROMFS_BYTES, // Get from MCU-Specific loader script.
231+
};
232+
233+
mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
234+
if (MICROPY_HW_ROMFS_BYTES <= 0) {
235+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
236+
}
237+
switch (mp_obj_get_int(args[0])) {
238+
case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS:
239+
return MP_OBJ_NEW_SMALL_INT(1);
240+
case MP_VFS_ROM_IOCTL_GET_SEGMENT:
241+
return MP_OBJ_FROM_PTR(&nrf_flash_romfs_obj);
242+
default:
243+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
244+
}
245+
}
246+
247+
#endif // MICROPY_VFS_ROM
248+
205249
#endif // MICROPY_PY_NRF && MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE

ports/nrf/modules/scripts/_boot.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def setup_fs():
2+
import gc
3+
import vfs
4+
import sys
5+
import nrf
6+
import os
7+
8+
fs_type = getattr(vfs, "VfsLfs2", getattr(vfs, "VfsLfs1", getattr(vfs, "VfsFat", None)))
9+
try:
10+
bdev = nrf.Flash()
11+
vfs.mount(bdev, "/flash")
12+
except:
13+
if fs_type is not None:
14+
try:
15+
fs_type.mkfs(bdev)
16+
vfs.mount(bdev, "/flash")
17+
except:
18+
return
19+
20+
os.chdir("/flash")
21+
sys.path.append("/flash")
22+
sys.path.append("/flash/lib")
23+
24+
gc.collect()
25+
26+
27+
setup_fs()
28+
del setup_fs

0 commit comments

Comments
 (0)