Skip to content

ports: Provide VFS ROM support for the MIXRT, SAMD, NRF and Renesas port. #17452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ ifeq ($(USE_UF2_BOOTLOADER),1)
CFLAGS += -DMICROPY_MACHINE_UF2_BOOTLOADER=1
endif

# Set the default size of the VfsRom file system. Can be changed at board level
ifeq ($(MICROPY_HW_ROMFS_BYTES),)
MICROPY_HW_ROMFS_BYTES ?= 0x40000
endif

# Add sources for respective board flash type
# Add hal/flexspi_nor_flash.c or hal/flashspi_hyper_flash.c respectively
SRC_HAL_C += hal/flexspi_$(subst qspi_,,$(FLEXSPI_FLASH_TYPE)).c
Expand Down Expand Up @@ -452,9 +457,10 @@ LDFLAGS += \
# LDDEFINES are used for link time adaptation of linker scripts, utilizing
# the C preprocessor. Therefore keep LDDEFINES separated from LDFLAGS!

LDDEFINES = \
LDDEFINES += \
-DMICROPY_HW_FLASH_BASE=$(MICROPY_HW_FLASH_BASE) \
-DMICROPY_HW_FLASH_SIZE=$(MICROPY_HW_FLASH_SIZE)
-DMICROPY_HW_FLASH_SIZE=$(MICROPY_HW_FLASH_SIZE) \
-DMICROPY_HW_ROMFS_BYTES=$(MICROPY_HW_ROMFS_BYTES)

ifdef MICROPY_HW_FLASH_RESERVED
LDDEFINES += -DMICROPY_HW_FLASH_RESERVED=$(MICROPY_HW_FLASH_RESERVED)
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1011.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00008000;
dtcm_start = 0x20000000;
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1015.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00008000;
dtcm_start = 0x20000000;
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1021.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00010000;
dtcm_start = 0x20000000;
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1052.ld
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00200000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00020000;
dtcm_start = 0x20000000;
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1062.ld
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00020000;
dtcm_start = 0x20000000;
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1064.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00020000;
dtcm_start = 0x20000000;
Expand Down
4 changes: 3 additions & 1 deletion ports/mimxrt/boards/MIMXRT1176.ld
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ m_core1_image_start = vfs_start - 0x00040000;
m_core1_image_size = 0x00040000;
#endif

text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
vfsrom_start = ((vfs_start) - MICROPY_HW_ROMFS_BYTES);
vfsrom_end = ((vfsrom_start) + MICROPY_HW_ROMFS_BYTES);
text_size = ((vfsrom_start) - (text_start));
itcm_start = 0x00000000;
itcm_size = 0x00020000;
dtcm_start = 0x20000000;
Expand Down
49 changes: 49 additions & 0 deletions ports/mimxrt/mimxrt_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* Copyright (c) 2020-2021 Damien P. George
* Copyright (c) 2021-2023 Philipp Ebensberger
* Copyright (c) 2021-2024 Robert Hammelrath
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -29,6 +30,7 @@

#include "py/runtime.h"
#include "extmod/vfs.h"
#include "py/mperrno.h"
#include "modmimxrt.h"
#include "flash.h"
#include BOARD_FLASH_OPS_HEADER_H
Expand Down Expand Up @@ -60,6 +62,19 @@ static mp_obj_t mimxrt_flash_make_new(const mp_obj_type_t *type, size_t n_args,
return MP_OBJ_FROM_PTR(&mimxrt_flash_obj);
}

static mp_int_t mimxrt_flash_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
mimxrt_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (flags == MP_BUFFER_READ) {
bufinfo->buf = (void *)((uintptr_t)&__flash_start + self->flash_base);
bufinfo->len = self->flash_size;
bufinfo->typecode = 'B';
return 0;
} else {
// Write unsupported.
return 1;
}
}

// readblocks(block_num, buf, [offset])
// read size of buffer number of bytes from block (with offset) into buffer
static mp_obj_t mimxrt_flash_readblocks(size_t n_args, const mp_obj_t *args) {
Expand Down Expand Up @@ -151,5 +166,39 @@ MP_DEFINE_CONST_OBJ_TYPE(
MP_QSTR_Flash,
MP_TYPE_FLAG_NONE,
make_new, mimxrt_flash_make_new,
buffer, mimxrt_flash_get_buffer,
locals_dict, &mimxrt_flash_locals_dict
);

#if MICROPY_VFS_ROM

extern uint8_t vfsrom_start;
extern uint8_t vfsrom_end;

// Put VfsRom file system between the code space and the VFS file system.
// The size is defined in Makefile(s) as linker symbol MICROPY_HW_ROMFS_BYTES.
// For machine.mem32 the absolute address is required, for the flash functions
// erase and write the offset to the flash start address.
#define MICROPY_HW_ROMFS_BASE ((uintptr_t)&vfsrom_start - (uintptr_t)&__flash_start)
#define MICROPY_HW_ROMFS_BYTES ((uintptr_t)&vfsrom_end - (uintptr_t)&vfsrom_start)

static mimxrt_flash_obj_t mimxrt_flash_romfs_obj = {
.base = { &mimxrt_flash_type },
};

mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
if (MICROPY_HW_ROMFS_BYTES <= 0) {
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
}
switch (mp_obj_get_int(args[0])) {
case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS:
return MP_OBJ_NEW_SMALL_INT(1);
case MP_VFS_ROM_IOCTL_GET_SEGMENT:
mimxrt_flash_romfs_obj.flash_base = MICROPY_HW_ROMFS_BASE;
mimxrt_flash_romfs_obj.flash_size = MICROPY_HW_ROMFS_BYTES;
return MP_OBJ_FROM_PTR(&mimxrt_flash_romfs_obj);
default:
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
}
}
#endif // MICROPY_VFS_ROM
3 changes: 3 additions & 0 deletions ports/mimxrt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ uint32_t trng_random_u32(void);
#define MICROPY_SCHEDULER_DEPTH (8)
#define MICROPY_SCHEDULER_STATIC_NODES (1)
#define MICROPY_VFS (1)
#ifndef MICROPY_VFS_ROM
#define MICROPY_VFS_ROM (1)
#endif

// Control over Python builtins
#define MICROPY_PY_BUILTINS_HELP_TEXT mimxrt_help_text
Expand Down
6 changes: 4 additions & 2 deletions ports/nrf/boards/memory.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ _head_size = DEFINED(_sd_size) ? _sd_size : _bootloader_head_size;
_head_ram = DEFINED(_sd_ram) ? _sd_ram : _bootloader_head_ram_size;
_sd_size = DEFINED(_sd_size) ? _sd_size : 0;
_sd_ram = DEFINED(_sd_ram) ? _sd_ram : 0;
_vfsrom_size = DEFINED(_vfsrom_size) ? _vfsrom_size : 0;
_fs_size = DEFINED(_fs_size) ? _fs_size : 64K; /* TODO: set to 0 if not using the filesystem */
_app_size = _flash_size - _head_size - _fs_size - _bootloader_tail_size;
_app_size = _flash_size - _head_size - _vfsrom_size - _fs_size - _bootloader_tail_size;
_app_start = _head_size;
_fs_start = _head_size + _app_size;
_vfsrom_start = _head_size + _app_size;
_fs_start = _vfsrom_start + _vfsrom_size;
_fs_end = _fs_start + _fs_size;
_app_ram_start = 0x20000000 + _head_ram;
_app_ram_size = _ram_size - _head_ram;
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/nrf51x22_256k_16k.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GROUP(-lgcc -lc -lnosys)

_flash_size = 256K;
_ram_size = 16K;
_vfsrom_size = 12K;

/* Default stack size when there is no SoftDevice */
_stack_size = 4K;
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/nrf51x22_256k_32k.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GROUP(-lgcc -lc -lnosys)

_flash_size = 256K;
_ram_size = 32K;
_vfsrom_size = 12K;

/* Default stack size when there is no SoftDevice */
_stack_size = 4K;
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/nrf52832_512k_64k.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

_flash_size = 512K;
_ram_size = 64K;
_vfsrom_size = 128K;

/* produce a link error if there is not this amount of RAM for these sections */
_stack_size = 8K;
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/nrf52840_1M_256k.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

_flash_size = 1M;
_ram_size = 256K;
_vfsrom_size = 128K;

/* produce a link error if there is not this amount of RAM for these sections */
_stack_size = 8K;
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/nrf9160_1M_256k.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _ram_size = 256K;
_sd_size = 0x00008000;
_sd_ram = 0x00020000;
_fs_size = 80K;
_vfsrom_size = 128K;

/* produce a link error if there is not this amount of RAM for these sections */
_stack_size = 32K;
Expand Down
18 changes: 2 additions & 16 deletions ports/nrf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,8 @@ void MP_NORETURN _start(void) {

#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
flashbdev_init();

// Try to mount the flash on "/flash" and chdir to it for the boot-up directory.
mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash);
int ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);

if ((ret == -MP_ENODEV) || (ret == -MP_EIO)) {
pyexec_frozen_module("_mkfs.py", false); // Frozen script for formatting flash filesystem.
ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
}

if (ret != 0) {
printf("MPY: can't mount flash\n");
} else {
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
}
// Execute _boot.py to set up the filesystem.
pyexec_frozen_module("_boot.py", false);
#endif

#if MICROPY_MBFS
Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/modules/manifest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module("_mkfs.py", base_path="$(PORT_DIR)/modules/scripts", opt=3)
module("_boot.py", base_path="$(PORT_DIR)/modules/scripts", opt=3)
include("$(MPY_DIR)/extmod/asyncio")
44 changes: 44 additions & 0 deletions ports/nrf/modules/nrf/flashbdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,26 @@ static mp_obj_t nrf_flashbdev_make_new(const mp_obj_type_t *type, size_t n_args,
return MP_OBJ_FROM_PTR(self);
}

static mp_int_t nrf_flashbdev_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
nrf_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (flags == MP_BUFFER_READ) {
bufinfo->buf = (void *)self->start;
bufinfo->len = self->len;
bufinfo->typecode = 'B';
return 0;
} else {
// Unsupported.
return 1;
}
}

MP_DEFINE_CONST_OBJ_TYPE(
nrf_flashbdev_type,
MP_QSTR_Flash,
MP_TYPE_FLAG_NONE,
make_new, nrf_flashbdev_make_new,
print, nrf_flashbdev_print,
buffer, nrf_flashbdev_get_buffer,
locals_dict, &nrf_flashbdev_locals_dict
);

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

#if MICROPY_VFS_ROM

extern byte _vfsrom_start[];
extern byte _vfsrom_size[];

#define MICROPY_HW_ROMFS_BASE ((uint32_t)_vfsrom_start)
#define MICROPY_HW_ROMFS_BYTES ((uint32_t)_vfsrom_size)

static nrf_flash_obj_t nrf_flash_romfs_obj = {
.base = { &nrf_flashbdev_type },
.start = MICROPY_HW_ROMFS_BASE, // Get from MCU-Specific loader script.
.len = MICROPY_HW_ROMFS_BYTES, // Get from MCU-Specific loader script.
};

mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
if (MICROPY_HW_ROMFS_BYTES <= 0) {
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
}
switch (mp_obj_get_int(args[0])) {
case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS:
return MP_OBJ_NEW_SMALL_INT(1);
case MP_VFS_ROM_IOCTL_GET_SEGMENT:
return MP_OBJ_FROM_PTR(&nrf_flash_romfs_obj);
default:
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
}
}

#endif // MICROPY_VFS_ROM

#endif // MICROPY_PY_NRF && MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
31 changes: 31 additions & 0 deletions ports/nrf/modules/scripts/_boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import gc
import vfs
import sys
import nrf
import os

try:
bdev = nrf.Flash()
fs_type = (
vfs.VfsLfs2
if hasattr(vfs, "VfsLfs2")
else (vfs.VfsLfs1 if hasattr(vfs, "VfsLfs1") else vfs.VfsFat)
)
vfs.mount(fs_type(bdev), "/flash")
except AttributeError:
fs_type = None
except:
try:
fs_type.mkfs(bdev)
vfs.mount(fs_type(bdev), "/flash")
except:
pass

if fs_type is not None:
os.chdir("/flash")
sys.path.append("/flash")
sys.path.append("/flash/lib")

del os, nrf, vfs, sys, bdev, fs_type
gc.collect()
del gc
23 changes: 0 additions & 23 deletions ports/nrf/modules/scripts/_mkfs.py

This file was deleted.

Loading
Loading
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