Skip to content

Commit

Permalink
Merge 7828207 into caaaa2b
Browse files Browse the repository at this point in the history
  • Loading branch information
glennrub committed Jul 23, 2020
2 parents caaaa2b + 7828207 commit 5c9866f
Show file tree
Hide file tree
Showing 38 changed files with 563 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -300,6 +300,7 @@ jobs:
- arm-none-eabi-gcc --version
script:
- ports/nrf/drivers/bluetooth/download_ble_stack.sh s140_nrf52_6_1_1
- make ${MAKEOPTS} -C mpy-cross
- make ${MAKEOPTS} -C ports/nrf submodules
- make ${MAKEOPTS} -C ports/nrf BOARD=pca10040
- make ${MAKEOPTS} -C ports/nrf BOARD=microbit
Expand Down
15 changes: 13 additions & 2 deletions ports/nrf/Makefile
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 @@ -43,6 +45,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 @@ -63,6 +67,7 @@ INC += -I./modules/ubluepy
INC += -I./modules/music
INC += -I./modules/ble
INC += -I./modules/board
INC += -I./modules/nrf
INC += -I../../lib/mp-readline
INC += -I./drivers/bluetooth
INC += -I./drivers
Expand Down Expand Up @@ -122,6 +127,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 @@ -298,6 +307,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 @@ -481,13 +492,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
Expand Up @@ -109,13 +109,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 | Flash Util
Expand Down
2 changes: 2 additions & 0 deletions ports/nrf/boards/actinius_icarus/mpconfigboard.mk
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
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
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/dvk_bl652/mpconfigboard.mk
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
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
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
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
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
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
Expand Up @@ -5,3 +5,5 @@ SOFTDEV_VERSION = 6.1.1
LD_FILES += boards/nrf52840-mdk-usb-dongle/nrf52840_open_bootloader.ld 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
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
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
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
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
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
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
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
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/pca10090/mpconfigboard.mk
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
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
Expand Up @@ -930,7 +930,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_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
Expand Up @@ -26,7 +26,7 @@

#include "py/mpconfig.h"

#if MICROPY_MBFS && BLUETOOTH_SD
#if (MICROPY_HW_ENABLE_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_STORAGE || MICROPY_MBFS) && BLUETOOTH_SD
45 changes: 45 additions & 0 deletions ports/nrf/main.c
Expand Up @@ -76,6 +76,14 @@
#include "usb_cdc.h"
#endif

#if MICROPY_HW_ENABLE_STORAGE
#include "extmod/vfs_fat.h"
#include "lib/oofatfs/ff.h"
#include "extmod/vfs.h"
#include "flashbdev.h"
extern const mp_obj_type_t nrf_flashbdev_obj;
#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 +107,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_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 +199,21 @@ int main(int argc, char **argv) {

pin_init0();

#if MICROPY_HW_ENABLE_STORAGE
// 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_flashbdev_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_flashbdev_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
@@ -0,0 +1 @@
freeze("$(PORT_DIR)/modules/scripts", "_mkfs.py")

0 comments on commit 5c9866f

Please sign in to comment.