samd: Adapt existing samd.Flash and integrate with (Q)SPI flash in boot.

Checks are added to ensure, that only one of the flash drivers is selected.

Signed-off-by: robert-hh <robert@hammelrath.com>
This commit is contained in:
robert-hh 2023-05-24 16:19:33 +02:00 committed by Damien George
parent 2b5a5a0f35
commit 69cb5e8f2a
5 changed files with 39 additions and 29 deletions

View File

@ -113,7 +113,9 @@ SRC_C += \
pin_af.c \
samd_flash.c \
samd_isr.c \
samd_qspiflash.c \
samd_soc.c \
samd_spiflash.c \
tusb_port.c \
SHARED_SRC_C += \

View File

@ -32,7 +32,18 @@
#include "pin_af.h"
#include "samd_soc.h"
#if MICROPY_HW_MCUFLASH
extern const mp_obj_type_t samd_flash_type;
#define SPIFLASH_TYPE samd_flash_type
#endif
#ifdef MICROPY_HW_QSPIFLASH
extern const mp_obj_type_t samd_qspiflash_type;
#define SPIFLASH_TYPE samd_qspiflash_type
#endif
#if MICROPY_HW_SPIFLASH
extern const mp_obj_type_t samd_spiflash_type;
#define SPIFLASH_TYPE samd_spiflash_type
#endif
STATIC mp_obj_t samd_pininfo(mp_obj_t pin_obj) {
const machine_pin_obj_t *pin_af = pin_find(pin_obj);
@ -67,7 +78,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_pininfo_obj, samd_pininfo);
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_flash_type) },
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&SPIFLASH_TYPE) },
{ MP_ROM_QSTR(MP_QSTR_pininfo), MP_ROM_PTR(&samd_pininfo_obj) },
};
STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table);

View File

@ -2,18 +2,18 @@ import gc
import uos
import samd
samd.Flash.flash_init()
bdev = samd.Flash()
# Try to mount the filesystem, and format the flash if it doesn't exist.
fs_type = uos.VfsLfs2 if hasattr(uos, "VfsLfs2") else uos.VfsLfs1
try:
vfs = fs_type(bdev)
vfs = fs_type(bdev, progsize=256)
except:
fs_type.mkfs(bdev)
vfs = fs_type(bdev)
fs_type.mkfs(bdev, progsize=256)
vfs = fs_type(bdev, progsize=256)
uos.mount(vfs, "/")
del vfs, fs_type, bdev, uos, samd
gc.collect()
del uos, vfs, gc
del gc

View File

@ -136,6 +136,11 @@
#define MICROPY_BOARD_PENDSV_ENTRIES
#endif
// Use internal flash for the file system if no flash file system is selected.
#if !defined(MICROPY_HW_MCUFLASH) && !defined(MICROPY_HW_QSPIFLASH) && !(defined(MICROPY_HW_SPIFLASH) && defined(MICROPY_HW_SPIFLASH_ID))
#define MICROPY_HW_MCUFLASH (1)
#endif // !defined(MICROPY_HW_MCUFLASH) ....
// Miscellaneous settings
__attribute__((always_inline)) static inline void enable_irq(uint32_t state) {
__set_PRIMASK(state);

View File

@ -29,7 +29,8 @@
#include "py/runtime.h"
#include "extmod/vfs.h"
#include "samd_soc.h"
#include "hal_flash.h"
#if MICROPY_HW_MCUFLASH
// ASF 4
#include "hal_flash.h"
@ -62,18 +63,9 @@ STATIC samd_flash_obj_t samd_flash_obj = {
.flash_size = (uint32_t)&_sflash_fs, // Get from MCU-Specific loader script.
};
// FLASH stuff
STATIC mp_obj_t samd_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
// No args required. bdev=Flash(). Start Addr & Size defined in samd_flash_obj.
mp_arg_check_num(n_args, n_kw, 0, 0, false);
// Return singleton object.
return MP_OBJ_FROM_PTR(&samd_flash_obj);
}
// Flash init (from cctpy)
// Method is needed for when MP starts up in _boot.py
STATIC mp_obj_t samd_flash_init(void) {
STATIC void samd_flash_init(void) {
#ifdef SAMD51
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
#endif
@ -82,9 +74,17 @@ STATIC mp_obj_t samd_flash_init(void) {
#endif
flash_init(&flash_desc, NVMCTRL);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_init_obj, samd_flash_init);
STATIC mp_obj_t samd_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
// No args required. bdev=Flash(). Start Addr & Size defined in samd_flash_obj.
mp_arg_check_num(n_args, n_kw, 0, 0, false);
samd_flash_init();
// Return singleton object.
return MP_OBJ_FROM_PTR(&samd_flash_obj);
}
// Function for ioctl.
STATIC mp_obj_t eraseblock(uint32_t sector_in) {
@ -102,14 +102,6 @@ STATIC mp_obj_t samd_flash_version(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_version_obj, samd_flash_version);
STATIC mp_obj_t samd_flash_size(void) {
// ASF4 API calls
mp_int_t PAGES = flash_get_total_pages(&flash_desc);
mp_int_t PAGE_SIZE = flash_get_page_size(&flash_desc);
return MP_OBJ_NEW_SMALL_INT(PAGES * PAGE_SIZE);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_size_obj, samd_flash_size);
STATIC mp_obj_t samd_flash_readblocks(size_t n_args, const mp_obj_t *args) {
uint32_t offset = (mp_obj_get_int(args[1]) * BLOCK_SIZE) + samd_flash_obj.flash_base;
mp_buffer_info_t bufinfo;
@ -171,8 +163,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_flash_ioctl_obj, samd_flash_ioctl);
STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_flash_version), MP_ROM_PTR(&samd_flash_version_obj) },
{ MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&samd_flash_size_obj) },
{ MP_ROM_QSTR(MP_QSTR_flash_init), MP_ROM_PTR(&samd_flash_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&samd_flash_readblocks_obj) },
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&samd_flash_writeblocks_obj) },
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&samd_flash_ioctl_obj) },
@ -186,3 +176,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
make_new, samd_flash_make_new,
locals_dict, &samd_flash_locals_dict
);
#endif // MICROPY_HW_MCUFLASH