stm32: Enable VfsMap.

Signed-off-by: Damien George <damien@micropython.org>
pull/8381/head
Damien George 2022-03-04 10:56:50 +11:00
rodzic 240b9d07e0
commit c31503c203
12 zmienionych plików z 149 dodań i 5 usunięć

Wyświetl plik

@ -61,6 +61,10 @@
#define MICROPY_BOARD_TOP_SOFT_RESET_LOOP boardctrl_top_soft_reset_loop
#endif
#ifndef MICROPY_BOARD_FROZEN_BOOT_FILE
#define MICROPY_BOARD_FROZEN_BOOT_FILE "_boot.py"
#endif
#ifndef MICROPY_BOARD_RUN_BOOT_PY
#define MICROPY_BOARD_RUN_BOOT_PY boardctrl_run_boot_py
#endif

Wyświetl plik

@ -8,7 +8,7 @@ MICROPY_VFS_FAT = 0
MICROPY_VFS_LFS1 ?= 1
# Don't include default frozen modules because MCU is tight on flash space
FROZEN_MANIFEST ?=
FROZEN_MANIFEST ?= boards/manifest_minimal.py
# LTO reduces final binary size, may be slower to build depending on gcc version and hardware
LTO ?= 1

Wyświetl plik

@ -20,7 +20,8 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 32K /* sectors 0,1 */
FLASH_APP (rx) : ORIGIN = 0x08008000, LENGTH = 480K /* sectors 2-7 */
FLASH_APP (rx) : ORIGIN = 0x08008000, LENGTH = 352K /* sectors 2-6 */
FLASH_MAPFS (rx): ORIGIN = 0x08060000, LENGTH = 128K /* sector 7 */
FLASH_EXT (rx) : ORIGIN = 0x90000000, LENGTH = 2048K /* external QSPI */
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 256K /* DTCM+SRAM1+SRAM2 */
}
@ -40,6 +41,9 @@ _ram_end = ORIGIN(RAM) + LENGTH(RAM);
_heap_start = _ebss; /* heap starts just after statically allocated memory */
_heap_end = _sstack;
_micropy_hw_mapfs_start = ORIGIN(FLASH_MAPFS);
_micropy_hw_mapfs_end = ORIGIN(FLASH_MAPFS) + LENGTH(FLASH_MAPFS);
/* Define output sections */
SECTIONS
{
@ -51,6 +55,10 @@ SECTIONS
*lib/mynewt-nimble/*(.text* .rodata*)
*lib/cyw43-driver/*(.rodata.w4343*_combined)
*drivers/cyw43/*(.rodata.cyw43_btfw_*)
*lib/oofatfs/*(.text* .rodata*)
*lib/littlefs/*(.text* .rodata*)
*lib/lwip/*(.text* .rodata*)
*extmod/*(.text* .rodata*)
. = ALIGN(4);
} >FLASH_EXT
}

Wyświetl plik

@ -18,7 +18,8 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048K
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 32K /* sector 0, 32K */
FLASH_APP (rx) : ORIGIN = 0x08008000, LENGTH = 2016K /* sectors 1-11 3x32K 1*128K 7*256K */
FLASH_APP (rx) : ORIGIN = 0x08008000, LENGTH = 1504K /* sectors 1-11 3x32K 1*128K 7*256K */
FLASH_MAPFS (rx): ORIGIN = 0x08180000, LENGTH = 512K
FLASH_EXT (rx) : ORIGIN = 0x90000000, LENGTH = 2048K /* external QSPI */
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 512K /* DTCM=128k, SRAM1=368K, SRAM2=16K */
}
@ -38,4 +39,7 @@ _ram_end = ORIGIN(RAM) + LENGTH(RAM);
_heap_start = _ebss; /* heap starts just after statically allocated memory */
_heap_end = _sstack;
_micropy_hw_mapfs_start = ORIGIN(FLASH_MAPFS);
_micropy_hw_mapfs_end = ORIGIN(FLASH_MAPFS) + LENGTH(FLASH_MAPFS);
INCLUDE common_bl.ld

Wyświetl plik

@ -0,0 +1,8 @@
import vfs, sys, pyb
mapfs = pyb.Flash("mapfs")
vfs.mount(vfs.VfsMap(mapfs.ioctl(0x100, 0), mapfs), "/mapfs")
sys.path.insert(0, "/mapfs")
del vfs, sys, pyb, mapfs

Wyświetl plik

@ -1,3 +1,4 @@
freeze(".", "_boot.py", opt=3)
include("$(MPY_DIR)/extmod/asyncio")
require("dht")

Wyświetl plik

@ -0,0 +1 @@
freeze(".", "_boot.py", opt=3)

Wyświetl plik

@ -5,7 +5,8 @@
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 236K /* sectors 0-117 */
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 226K /* sectors 0-112 */
FLASH_MAPFS (rx): ORIGIN = 0x08036000, LENGTH = 10K /* sectors 113-117 */
FLASH_FS (r) : ORIGIN = 0x0803B000, LENGTH = 20K /* sectors 118-127 */
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
}
@ -32,3 +33,6 @@ _heap_end = _sstack;
_micropy_hw_internal_flash_storage_start = ORIGIN(FLASH_FS);
_micropy_hw_internal_flash_storage_end = ORIGIN(FLASH_FS) + LENGTH(FLASH_FS);
_micropy_hw_mapfs_start = ORIGIN(FLASH_MAPFS);
_micropy_hw_mapfs_end = ORIGIN(FLASH_MAPFS) + LENGTH(FLASH_MAPFS);

Wyświetl plik

@ -8,7 +8,8 @@ MEMORY
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K /* entire flash */
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */
FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1,2,3,4 are for filesystem */
FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 896K /* sectors 5,6,7,8,9,10,11 */
FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 640K /* sectors 5,6,7,8,9 */
FLASH_MAPFS (rx): ORIGIN = 0x080c0000, LENGTH = 256K /* sectors 10,11 */
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
}
@ -33,3 +34,6 @@ _micropy_hw_internal_flash_storage_ram_cache_start = ORIGIN(CCMRAM);
_micropy_hw_internal_flash_storage_ram_cache_end = ORIGIN(CCMRAM) + LENGTH(CCMRAM);
_micropy_hw_internal_flash_storage_start = ORIGIN(FLASH_FS);
_micropy_hw_internal_flash_storage_end = ORIGIN(FLASH_FS) + LENGTH(FLASH_FS);
_micropy_hw_mapfs_start = ORIGIN(FLASH_MAPFS);
_micropy_hw_mapfs_end = ORIGIN(FLASH_MAPFS) + LENGTH(FLASH_MAPFS);

Wyświetl plik

@ -154,6 +154,8 @@
#define MICROPY_PY_ONEWIRE (1)
#endif
#define MICROPY_VFS_MAP (1)
// fatfs configuration used in ffconf.h
#define MICROPY_FATFS_ENABLE_LFN (1)
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */

Wyświetl plik

@ -32,6 +32,7 @@ Q(/flash)
Q(/flash/lib)
Q(/sd)
Q(/sd/lib)
Q(/mapfs)
// For os.sep
Q(/)

Wyświetl plik

@ -259,6 +259,8 @@ int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint3
}
#endif
static struct _pyb_mapfs_obj_t pyb_mapfs_obj;
typedef struct _pyb_flash_obj_t {
mp_obj_base_t base;
uint32_t start; // in bytes
@ -289,6 +291,11 @@ static mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, siz
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
};
if (n_args == 1 && mp_obj_is_str(all_args[0]) && mp_obj_str_get_qstr(all_args[0]) == MP_QSTR_mapfs) {
return MP_OBJ_FROM_PTR(&pyb_mapfs_obj);
}
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -475,3 +482,103 @@ void pyb_flash_init_vfs(fs_user_mount_t *vfs) {
}
#endif
/******************************************************************************/
// mapfs partition
//
// TODO: this doesn't properly implement the block device protocol because it
// has potentially large sectors but pretends they are small.
// writing to the start of a sector will erase that whole sector first.
#include "flash.h"
#define MICROPY_HW_MAPFS_BASE (uintptr_t)(&_micropy_hw_mapfs_start)
#define MICROPY_HW_MAPFS_BYTES (uintptr_t)(&_micropy_hw_mapfs_end - &_micropy_hw_mapfs_start)
#define MAPFS_BLOCK_SIZE_BYTES (256) // can be anything bigger than alignment requirements of hardware writes
typedef struct _pyb_mapfs_obj_t {
mp_obj_base_t base;
// uint32_t flash_base;
// uint32_t flash_size;
} pyb_mapfs_obj_t;
extern uint8_t _micropy_hw_mapfs_start;
extern uint8_t _micropy_hw_mapfs_end;
static const mp_obj_type_t pyb_mapfs_type;
static pyb_mapfs_obj_t pyb_mapfs_obj = {
.base = { &pyb_mapfs_type },
// .flash_base = MICROPY_HW_MAPFS_BASE,
// .flash_size = MICROPY_HW_MAPFS_BYTES,
};
static mp_obj_t pyb_mapfs_readblocks(size_t n_args, const mp_obj_t *args) {
// pyb_mapfs_obj_t *self = MP_OBJ_TO_PTR(args[0]);
uint32_t offset = mp_obj_get_int(args[1]) * MAPFS_BLOCK_SIZE_BYTES;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
memcpy(bufinfo.buf, (void *)(MICROPY_HW_MAPFS_BASE + offset), bufinfo.len);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_mapfs_readblocks_obj, 3, 3, pyb_mapfs_readblocks);
static mp_obj_t pyb_mapfs_writeblocks(size_t n_args, const mp_obj_t *args) {
// pyb_mapfs_obj_t *self = MP_OBJ_TO_PTR(args[0]);
uint32_t offset = mp_obj_get_int(args[1]) * MAPFS_BLOCK_SIZE_BYTES;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
uint32_t dest = MICROPY_HW_MAPFS_BASE + offset;
// Compute start and end address of the sector being written.
uint32_t sector_size = 0;
uint32_t sector_start = 0;
int ret = flash_get_sector_info(dest, &sector_start, &sector_size);
if (ret < 0) {
return MP_OBJ_NEW_SMALL_INT(ret);
}
// Erase sector if writing to the start of it.
if (dest == sector_start) {
ret = flash_erase(sector_start);
if (ret != 0) {
return MP_OBJ_NEW_SMALL_INT(ret);
}
}
flash_write(dest, bufinfo.buf, bufinfo.len / 4);
// TODO check return value
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_mapfs_writeblocks_obj, 3, 4, pyb_mapfs_writeblocks);
static mp_obj_t pyb_mapfs_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) {
// pyb_mapfs_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t cmd = mp_obj_get_int(cmd_in);
switch (cmd) {
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
return MP_OBJ_NEW_SMALL_INT(MICROPY_HW_MAPFS_BYTES / MAPFS_BLOCK_SIZE_BYTES);
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
return MP_OBJ_NEW_SMALL_INT(MAPFS_BLOCK_SIZE_BYTES);
case 0x100: // mmap
return mp_obj_new_int(MICROPY_HW_MAPFS_BASE);
default:
return mp_const_none;
}
}
static MP_DEFINE_CONST_FUN_OBJ_3(pyb_mapfs_ioctl_obj, pyb_mapfs_ioctl);
static const mp_rom_map_elem_t pyb_mapfs_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&pyb_mapfs_readblocks_obj) },
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&pyb_mapfs_writeblocks_obj) },
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&pyb_mapfs_ioctl_obj) },
};
static MP_DEFINE_CONST_DICT(pyb_mapfs_locals_dict, pyb_mapfs_locals_dict_table);
static MP_DEFINE_CONST_OBJ_TYPE(
pyb_mapfs_type,
MP_QSTR_Flash,
MP_TYPE_FLAG_NONE,
locals_dict, &pyb_mapfs_locals_dict
);