Skip to content

nrf: Add block device for VFS (v2) #7619

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

Merged
merged 21 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3b594f7
nrf/boards/common.ld: Calculate unused flash region.
glennrub Dec 13, 2020
127cec8
nrf/modules/nrf: Add new nrf module with flash block device.
glennrub Jul 22, 2020
5a873e2
nrf/drivers: Add support for using flash block device with SoftDevice.
glennrub Jul 23, 2020
85cad50
nrf/mpconfigport.h: Expose nrf module when MICROPY_PY_NRF is set.
glennrub Jul 23, 2020
b40dfa9
nrf/README: Update README.md to reflect internal file systems.
glennrub Jul 23, 2020
c9b72ba
nrf/mpconfigport.h: Tune FAT FS configuration.
glennrub Jul 23, 2020
0bde907
nrf/Makefile: Add _fs_size linker script override from make.
glennrub Jul 23, 2020
7a833ed
nrf/modules/uos: Allow a board to configure MICROPY_VFS_FAT/LFS1/LFS2.
glennrub Jul 23, 2020
f99aa82
nrf/mpconfigport.h: Enable MICROPY_PY_IO_FILEIO when an FS is enabled.
glennrub Jul 23, 2020
6ff3a2a
nrf/qstrdefsport.h: Add entries for in-built FS mount points.
glennrub Jul 23, 2020
b0fd437
nrf/main: Add auto mount and auto format hook for internal flash FS.
glennrub Jul 23, 2020
4326e08
nrf/boards: Enable needed features for FAT/LFS1/LFS2.
glennrub Jul 23, 2020
990341d
nrf: Facilitate use of freeze manifest.
glennrub Dec 4, 2019
4f76f66
tools/ci.sh: Add mpy-cross build to nrf port.
glennrub Dec 9, 2020
ffc636d
nrf/boards: Set FROZEN_MANIFEST blank when SD present on nrf51 targets.
glennrub Jul 23, 2020
7b6ad0c
nrf/modules/scripts: Add file system formatting script.
glennrub Jul 23, 2020
aa857eb
nrf/Makefile: Set default manifest file for all targets.
glennrub Jul 23, 2020
23e8729
nrf/mphalport: Add dummy function for mp_hal_time_ns().
glennrub Dec 9, 2020
55d4321
nrf/boards: Enable MICROPY_VFS_LFS2 for all target boards.
glennrub Jul 23, 2020
85f0ce2
tools/codeformat.py: Include ports/nrf/modules/nrf in code formatting.
glennrub Jul 25, 2020
e5e0553
nrf/modules/uos: Add ilistdir to uos module.
dpgeorge Aug 7, 2021
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
15 changes: 13 additions & 2 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ifeq ($(wildcard boards/$(BOARD)/.),)
$(error Invalid BOARD specified)
endif

BOARD_DIR ?= boards/$(BOARD)

# If SoftDevice is selected, try to use that one.
SD ?=
SD_LOWER = $(shell echo $(SD) | tr '[:upper:]' '[:lower:]')
Expand Down Expand Up @@ -50,6 +52,8 @@ ifeq ($(DEBUG), 0)
MICROPY_ROM_TEXT_COMPRESSION ?= 1
endif

FROZEN_MANIFEST ?= modules/manifest.py

# include py core make definitions
include ../../py/py.mk

Expand All @@ -70,6 +74,7 @@ INC += -I./modules/ubluepy
INC += -I./modules/music
INC += -I./modules/ble
INC += -I./modules/board
INC += -I./modules/nrf
INC += -I../../shared/readline
INC += -I./drivers/bluetooth
INC += -I./drivers
Expand Down Expand Up @@ -129,6 +134,10 @@ LDFLAGS = $(CFLAGS)
LDFLAGS += -Xlinker -Map=$(@:.elf=.map)
LDFLAGS += -mthumb -mabi=aapcs $(addprefix -T,$(LD_FILES)) -L boards/

ifneq ($(FS_SIZE),)
LDFLAGS += -Wl,'--defsym=_fs_size=$(FS_SIZE)'
endif

#Debugging/Optimization
ifeq ($(DEBUG), 1)
#ASMFLAGS += -g -gtabs+
Expand Down Expand Up @@ -322,6 +331,8 @@ DRIVERS_SRC_C += $(addprefix modules/,\
music/modmusic.c \
music/musictunes.c \
ble/modble.c \
nrf/modnrf.c \
nrf/flashbdev.c \
)

# Custom micropython startup file with smaller interrupt vector table
Expand Down Expand Up @@ -530,13 +541,13 @@ GEN_PINS_QSTR = $(BUILD)/pins_qstr.h
GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h
GEN_PINS_AF_PY = $(BUILD)/pins_af.py

ifneq ($(FROZEN_DIR),)
ifneq ($(FROZEN_MANIFEST)$(FROZEN_DIR),)
# To use frozen source modules, put your .py files in a subdirectory (eg scripts/)
# and then invoke make with FROZEN_DIR=scripts (be sure to build from scratch).
CFLAGS += -DMICROPY_MODULE_FROZEN_STR
endif

ifneq ($(FROZEN_MPY_DIR),)
ifneq ($(FROZEN_MANIFEST)$(FROZEN_MPY_DIR),)
# To use frozen bytecode, put your .py files in a subdirectory (eg frozen/) and
# then invoke make with FROZEN_MPY_DIR=frozen (be sure to build from scratch).
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
Expand Down
44 changes: 44 additions & 0 deletions ports/nrf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,57 @@ To use frozen modules, put them in a directory (e.g. `freeze/`) and supply

make BOARD=pca10040 FROZEN_MPY_DIR=freeze

## Compile with freeze manifest

Freeze manifests can be used by definining `FROZEN_MANIFEST` pointing to a
`manifest.py`. This can either be done by a `make` invocation or by defining
it in the specific target board's `mpconfigboard.mk`.

For example:

make BOARD=pca10040 FROZEN_MANIFEST=path/to/manifest.py

In case of using the target board's makefile, add a line similar to this:

FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py

In these two examples, the manual `make` invocation will have precedence.

## Enable MICROPY_VFS_FAT
As the `oofatfs` module is not having header guards that can exclude the implementation compile time, this port provides a flag to enable it explicitly. The MICROPY_VFS_FAT is by default set to 0 and has to be set to 1 if `oofatfs` files should be compiled. This will be in addition of setting `MICROPY_VFS` in mpconfigport.h.

For example:

make BOARD=pca10040 MICROPY_VFS_FAT=1

## Enable MICROPY_VFS_LFS1 or MICROPY_VFS_LFS2

In order to enable `littlefs` as device flash filesystem, `MICROPY_VFS_LFS1`
or `MICROPY_VFS_LFS2` can be set. This will be in addition of setting
`MICROPY_VFS` in mpconfigport.h or mpconfigboard.h.

For example:

make BOARD=pca10056 MICROPY_VFS_LFS2=1

## Set file system size

The size of the file system on the internal flash is configured by the linker
script parameter `_fs_size`. This can either be overriden by the linker script
or dynamically through the makefile. By seting a value to the `FS_SIZE`.
The number will be passed directly to the linker scripts in order to calculate
the start and end of the file system. Note that the parameter value must be in
linker script syntax as it is passed directly.

For example, if we want to override the default file system size set by the
linker scripts to use 256K:

make BOARD=pca10056 MICROPY_VFS_LFS2=1 FS_SIZE=256K

Also note that changing this size between builds might cause loss of files
present from a previous firmware as it will format the file system due to a new
location.

## Target Boards and Make Flags

Target Board (BOARD) | Bluetooth Stack (SD) | Bluetooth Support | Bootloader | Default Flash Util
Expand Down
2 changes: 2 additions & 0 deletions ports/nrf/boards/actinius_icarus/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ MCU_SUB_VARIANT = nrf9160
LD_FILES += boards/nrf9160_1M_256k.ld

NRF_DEFINES += -DNRF9160_XXAA -DNRF_TRUSTZONE_NONSECURE

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/arduino_primo/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ LD_FILES += boards/nrf52832_512k_64k.ld
FLASHER = pyocd

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/blueio_tag_evim/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ LD_FILES += boards/nrf52832_512k_64k.ld
FLASHER = idap

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/common.ld
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ SECTIONS
/* Define heap and stack areas */
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
_estack = ORIGIN(RAM) + LENGTH(RAM);
_unused_flash_start = (_sidata + (_edata - _sdata));
_unused_flash_len = (ORIGIN(FLASH_TEXT) + LENGTH(FLASH_TEXT)) - _unused_flash_start;
2 changes: 2 additions & 0 deletions ports/nrf/boards/dvk_bl652/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ LD_FILES += boards/nrf52832_512k_64k.ld

NRF_DEFINES += -DNRF52832_XXAA
CFLAGS += -DBLUETOOTH_LFCLK_RC

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/evk_nina_b1/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ SOFTDEV_VERSION = 6.1.1
LD_FILES += boards/nrf52832_512k_64k.ld

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
1 change: 1 addition & 0 deletions ports/nrf/boards/feather52/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ LD_FILES += boards/nrf52832_512k_64k.ld

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/ibk_blyst_nano/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ LD_FILES += boards/nrf52832_512k_64k.ld
FLASHER = idap

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/idk_blyst_nano/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ LD_FILES += boards/nrf52832_512k_64k.ld
FLASHER = idap

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
5 changes: 5 additions & 0 deletions ports/nrf/boards/microbit/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ MCU_SERIES = m0
MCU_VARIANT = nrf51
MCU_SUB_VARIANT = nrf51822
SOFTDEV_VERSION = 8.0.0

ifneq ($(SD),)
LD_FILES += boards/microbit/custom_nrf51822_s110_microbit.ld
FROZEN_MANIFEST ?=
else
MICROPY_VFS_LFS2 = 1
endif

LD_FILES += boards/nrf51x22_256k_16k.ld
FLASHER = pyocd

Expand Down
2 changes: 2 additions & 0 deletions ports/nrf/boards/nrf52840-mdk-usb-dongle/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ endif
LD_FILES += boards/nrf52840_1M_256k.ld

NRF_DEFINES += -DNRF52840_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/particle_xenon/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ NRF_DEFINES += -DNRF52840_XXAA
# The nrf52-particle.cfg is not included here, it can be found in the Particle Workbench
# Note: This requires openocd >0.10
OPENOCD_TARGET ?= boards/$(BOARD)/nrf52-particle.cfg

MICROPY_VFS_LFS2 = 1
6 changes: 6 additions & 0 deletions ports/nrf/boards/pca10000/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ MCU_VARIANT = nrf51
MCU_SUB_VARIANT = nrf51822
SOFTDEV_VERSION = 8.0.0
LD_FILES += boards/nrf51x22_256k_16k.ld

ifneq ($(SD),)
FROZEN_MANIFEST ?=
else
MICROPY_VFS_LFS2 = 1
endif
6 changes: 6 additions & 0 deletions ports/nrf/boards/pca10001/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ MCU_VARIANT = nrf51
MCU_SUB_VARIANT = nrf51822
SOFTDEV_VERSION = 8.0.0
LD_FILES += boards/nrf51x22_256k_16k.ld

ifneq ($(SD),)
FROZEN_MANIFEST ?=
else
MICROPY_VFS_LFS2 = 1
endif
6 changes: 6 additions & 0 deletions ports/nrf/boards/pca10028/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ MCU_VARIANT = nrf51
MCU_SUB_VARIANT = nrf51822
SOFTDEV_VERSION = 8.0.0
LD_FILES += boards/nrf51x22_256k_32k.ld

ifneq ($(SD),)
FROZEN_MANIFEST ?=
else
MICROPY_VFS_LFS2 = 1
endif
6 changes: 6 additions & 0 deletions ports/nrf/boards/pca10031/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ MCU_VARIANT = nrf51
MCU_SUB_VARIANT = nrf51822
SOFTDEV_VERSION = 8.0.0
LD_FILES += boards/nrf51x22_256k_32k.ld

ifneq ($(SD),)
FROZEN_MANIFEST ?=
else
MICROPY_VFS_LFS2 = 1
endif
2 changes: 2 additions & 0 deletions ports/nrf/boards/pca10040/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ SOFTDEV_VERSION = 6.1.1
LD_FILES += boards/nrf52832_512k_64k.ld

NRF_DEFINES += -DNRF52832_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/pca10056/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ SOFTDEV_VERSION = 6.1.1
LD_FILES += boards/nrf52840_1M_256k.ld

NRF_DEFINES += -DNRF52840_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/pca10059/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ endif
LD_FILES += boards/nrf52840_1M_256k.ld

NRF_DEFINES += -DNRF52840_XXAA

MICROPY_VFS_LFS2 = 1
2 changes: 2 additions & 0 deletions ports/nrf/boards/pca10090/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ MCU_SUB_VARIANT = nrf9160
LD_FILES += boards/nrf9160_1M_256k.ld

NRF_DEFINES += -DNRF9160_XXAA -DNRF_TRUSTZONE_NONSECURE

MICROPY_VFS_LFS2 = 1
6 changes: 6 additions & 0 deletions ports/nrf/boards/wt51822_s4at/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ SOFTDEV_VERSION = 8.0.0
LD_FILES += boards/nrf51x22_256k_16k.ld

CFLAGS += -DBLUETOOTH_LFCLK_RC

ifneq ($(SD),)
FROZEN_MANIFEST ?=
else
MICROPY_VFS_LFS2 = 1
endif
2 changes: 1 addition & 1 deletion ports/nrf/drivers/bluetooth/ble_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ void ble_drv_discover_descriptors(void) {

static void sd_evt_handler(uint32_t evt_id) {
switch (evt_id) {
#if MICROPY_MBFS
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE || MICROPY_MBFS
case NRF_EVT_FLASH_OPERATION_SUCCESS:
flash_operation_finished(FLASH_STATE_SUCCESS);
break;
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/drivers/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "py/mpconfig.h"

#if MICROPY_MBFS && BLUETOOTH_SD
#if (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE || MICROPY_MBFS) && BLUETOOTH_SD

#include "drivers/flash.h"
#include "drivers/bluetooth/ble_drv.h"
Expand Down Expand Up @@ -129,4 +129,4 @@ void flash_write_bytes(uint32_t dst, const uint8_t *src, uint32_t num_bytes) {
}
}

#endif // MICROPY_MBFS
#endif // (MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE || MICROPY_MBFS) && BLUETOOTH_SD
46 changes: 46 additions & 0 deletions ports/nrf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
#include "usb_cdc.h"
#endif

#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
#include "extmod/vfs_fat.h"
#include "lib/oofatfs/ff.h"
#include "extmod/vfs.h"
#include "flashbdev.h"
#endif

void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
Expand All @@ -99,6 +106,28 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
extern uint32_t _heap_start;
extern uint32_t _heap_end;

#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
STATIC int vfs_mount_and_chdir(mp_obj_t bdev, mp_obj_t mount_point) {
nlr_buf_t nlr;
mp_int_t ret = -MP_EIO;
if (nlr_push(&nlr) == 0) {
mp_obj_t args[] = { bdev, mount_point };
mp_vfs_mount(2, args, (mp_map_t *)&mp_const_empty_map);
mp_vfs_chdir(mount_point);
ret = 0; // success
nlr_pop();
} else {
mp_obj_base_t *exc = nlr.ret_val;
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
mp_obj_t v = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(exc));
mp_obj_get_int_maybe(v, &ret); // get errno value
ret = -ret;
}
}
return ret;
}
#endif

int main(int argc, char **argv) {


Expand Down Expand Up @@ -169,6 +198,23 @@ int main(int argc, char **argv) {

pin_init0();

#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 = vfs_mount_and_chdir((mp_obj_t)&nrf_flash_obj, mount_point);

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

if (ret != 0) {
printf("MPY: can't mount flash\n");
}
#endif

#if MICROPY_MBFS
microbit_filesystem_init();
#endif
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/modules/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
freeze("$(PORT_DIR)/modules/scripts", "_mkfs.py")
Loading