Skip to content

Commit 69cb5e8

Browse files
robert-hhdpgeorge
authored andcommitted
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>
1 parent 2b5a5a0 commit 69cb5e8

5 files changed

Lines changed: 39 additions & 29 deletions

File tree

ports/samd/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ SRC_C += \
113113
pin_af.c \
114114
samd_flash.c \
115115
samd_isr.c \
116+
samd_qspiflash.c \
116117
samd_soc.c \
118+
samd_spiflash.c \
117119
tusb_port.c \
118120

119121
SHARED_SRC_C += \

ports/samd/modsamd.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@
3232
#include "pin_af.h"
3333
#include "samd_soc.h"
3434

35+
#if MICROPY_HW_MCUFLASH
3536
extern const mp_obj_type_t samd_flash_type;
37+
#define SPIFLASH_TYPE samd_flash_type
38+
#endif
39+
#ifdef MICROPY_HW_QSPIFLASH
40+
extern const mp_obj_type_t samd_qspiflash_type;
41+
#define SPIFLASH_TYPE samd_qspiflash_type
42+
#endif
43+
#if MICROPY_HW_SPIFLASH
44+
extern const mp_obj_type_t samd_spiflash_type;
45+
#define SPIFLASH_TYPE samd_spiflash_type
46+
#endif
3647

3748
STATIC mp_obj_t samd_pininfo(mp_obj_t pin_obj) {
3849
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);
6778

6879
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
6980
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
70-
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_flash_type) },
81+
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&SPIFLASH_TYPE) },
7182
{ MP_ROM_QSTR(MP_QSTR_pininfo), MP_ROM_PTR(&samd_pininfo_obj) },
7283
};
7384
STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table);

ports/samd/modules/_boot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
import uos
33
import samd
44

5-
samd.Flash.flash_init()
65
bdev = samd.Flash()
76

87
# Try to mount the filesystem, and format the flash if it doesn't exist.
98
fs_type = uos.VfsLfs2 if hasattr(uos, "VfsLfs2") else uos.VfsLfs1
109

1110
try:
12-
vfs = fs_type(bdev)
11+
vfs = fs_type(bdev, progsize=256)
1312
except:
14-
fs_type.mkfs(bdev)
15-
vfs = fs_type(bdev)
13+
fs_type.mkfs(bdev, progsize=256)
14+
vfs = fs_type(bdev, progsize=256)
1615
uos.mount(vfs, "/")
1716

17+
del vfs, fs_type, bdev, uos, samd
1818
gc.collect()
19-
del uos, vfs, gc
19+
del gc

ports/samd/mpconfigport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
#define MICROPY_BOARD_PENDSV_ENTRIES
137137
#endif
138138

139+
// Use internal flash for the file system if no flash file system is selected.
140+
#if !defined(MICROPY_HW_MCUFLASH) && !defined(MICROPY_HW_QSPIFLASH) && !(defined(MICROPY_HW_SPIFLASH) && defined(MICROPY_HW_SPIFLASH_ID))
141+
#define MICROPY_HW_MCUFLASH (1)
142+
#endif // !defined(MICROPY_HW_MCUFLASH) ....
143+
139144
// Miscellaneous settings
140145
__attribute__((always_inline)) static inline void enable_irq(uint32_t state) {
141146
__set_PRIMASK(state);

ports/samd/samd_flash.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#include "py/runtime.h"
3030
#include "extmod/vfs.h"
3131
#include "samd_soc.h"
32-
#include "hal_flash.h"
32+
33+
#if MICROPY_HW_MCUFLASH
3334

3435
// ASF 4
3536
#include "hal_flash.h"
@@ -62,18 +63,9 @@ STATIC samd_flash_obj_t samd_flash_obj = {
6263
.flash_size = (uint32_t)&_sflash_fs, // Get from MCU-Specific loader script.
6364
};
6465

65-
// FLASH stuff
66-
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) {
67-
// No args required. bdev=Flash(). Start Addr & Size defined in samd_flash_obj.
68-
mp_arg_check_num(n_args, n_kw, 0, 0, false);
69-
70-
// Return singleton object.
71-
return MP_OBJ_FROM_PTR(&samd_flash_obj);
72-
}
73-
7466
// Flash init (from cctpy)
7567
// Method is needed for when MP starts up in _boot.py
76-
STATIC mp_obj_t samd_flash_init(void) {
68+
STATIC void samd_flash_init(void) {
7769
#ifdef SAMD51
7870
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
7971
#endif
@@ -82,9 +74,17 @@ STATIC mp_obj_t samd_flash_init(void) {
8274
#endif
8375

8476
flash_init(&flash_desc, NVMCTRL);
85-
return mp_const_none;
8677
}
87-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_init_obj, samd_flash_init);
78+
79+
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) {
80+
// No args required. bdev=Flash(). Start Addr & Size defined in samd_flash_obj.
81+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
82+
83+
samd_flash_init();
84+
85+
// Return singleton object.
86+
return MP_OBJ_FROM_PTR(&samd_flash_obj);
87+
}
8888

8989
// Function for ioctl.
9090
STATIC mp_obj_t eraseblock(uint32_t sector_in) {
@@ -102,14 +102,6 @@ STATIC mp_obj_t samd_flash_version(void) {
102102
}
103103
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_version_obj, samd_flash_version);
104104

105-
STATIC mp_obj_t samd_flash_size(void) {
106-
// ASF4 API calls
107-
mp_int_t PAGES = flash_get_total_pages(&flash_desc);
108-
mp_int_t PAGE_SIZE = flash_get_page_size(&flash_desc);
109-
return MP_OBJ_NEW_SMALL_INT(PAGES * PAGE_SIZE);
110-
}
111-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_size_obj, samd_flash_size);
112-
113105
STATIC mp_obj_t samd_flash_readblocks(size_t n_args, const mp_obj_t *args) {
114106
uint32_t offset = (mp_obj_get_int(args[1]) * BLOCK_SIZE) + samd_flash_obj.flash_base;
115107
mp_buffer_info_t bufinfo;
@@ -171,8 +163,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_flash_ioctl_obj, samd_flash_ioctl);
171163

172164
STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table[] = {
173165
{ MP_ROM_QSTR(MP_QSTR_flash_version), MP_ROM_PTR(&samd_flash_version_obj) },
174-
{ MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&samd_flash_size_obj) },
175-
{ MP_ROM_QSTR(MP_QSTR_flash_init), MP_ROM_PTR(&samd_flash_init_obj) },
176166
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&samd_flash_readblocks_obj) },
177167
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&samd_flash_writeblocks_obj) },
178168
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&samd_flash_ioctl_obj) },
@@ -186,3 +176,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
186176
make_new, samd_flash_make_new,
187177
locals_dict, &samd_flash_locals_dict
188178
);
179+
180+
#endif // MICROPY_HW_MCUFLASH

0 commit comments

Comments
 (0)