Skip to content

Commit 860d712

Browse files
committed
samd/Flash: Adapt samd_flash.c, Makefile, mpconfigport.h and _boot.py.
Checks are added to ensure, that only one of the flash drivers is selected. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 2a2b698 commit 860d712

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

ports/samd/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ SRC_C += \
111111
mphalport.c \
112112
pendsv.c \
113113
pin_af.c \
114-
samd_flash.c \
115114
samd_isr.c \
115+
samd_flash.c \
116+
samd_qspiflash.c \
116117
samd_soc.c \
118+
samd_spiflash.c \
117119
tusb_port.c \
118120

119121
SHARED_SRC_C += \
@@ -129,6 +131,7 @@ SHARED_SRC_C += \
129131
shared/runtime/sys_stdio_mphal.c \
130132
shared/timeutils/timeutils.c \
131133
shared/tinyusb/mp_cdc_common.c \
134+
extmod/virtpin.c \
132135

133136
ASF4_SRC_C += $(addprefix lib/asf4/$(MCU_SERIES_LOWER)/,\
134137
hal/src/hal_atomic.c \

ports/samd/modsamd.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
#include "samd_soc.h"
3434

3535
extern const mp_obj_type_t samd_flash_type;
36+
#ifdef MICROPY_HW_QSPIFLASH
37+
extern const mp_obj_type_t samd_qspiflash_type;
38+
#endif
39+
#if MICROPY_HW_SPIFLASH
40+
extern const mp_obj_type_t spiflash_type;
41+
#endif
3642

3743
STATIC mp_obj_t samd_pininfo(mp_obj_t pin_obj) {
3844
const machine_pin_obj_t *pin_af = pin_find(pin_obj);
@@ -67,8 +73,25 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_pininfo_obj, samd_pininfo);
6773

6874
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
6975
{ 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) },
7176
{ MP_ROM_QSTR(MP_QSTR_pininfo), MP_ROM_PTR(&samd_pininfo_obj) },
77+
#if MICROPY_HW_MCUFLASH
78+
#if defined(MICROPY_HW_QSPIFLASH) || defined(MICROPY_HW_SPIFLASH)
79+
#error Only one type of flash must be used
80+
#endif
81+
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_flash_type) },
82+
#endif
83+
#ifdef MICROPY_HW_QSPIFLASH
84+
#if defined(MICROPY_HW_MCUFLASH) || defined(MICROPY_HW_SPIFLASH)
85+
#error Only one type of flash must be used
86+
#endif
87+
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_qspiflash_type) },
88+
#endif
89+
#if defined(MICROPY_HW_SPIFLASH) && defined(MICROPY_HW_SPIFLASH_ID)
90+
#if defined(MICROPY_HW_MCUFLASH) || defined(MICROPY_HW_QSPIFLASH)
91+
#error Only one type of flash must be used
92+
#endif
93+
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&spiflash_type) },
94+
#endif
7295
};
7396
STATIC MP_DEFINE_CONST_DICT(samd_module_globals, samd_module_globals_table);
7497

ports/samd/modules/_boot.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
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, progsize
1818
gc.collect()
19-
del uos, vfs, 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: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
#include <stdio.h>
2828

2929
#include "py/runtime.h"
30+
#include "py/mphal.h"
3031
#include "extmod/vfs.h"
3132
#include "samd_soc.h"
3233
#include "hal_flash.h"
3334

35+
#if MICROPY_HW_MCUFLASH
36+
3437
// ASF 4
3538
#include "hal_flash.h"
3639
#include "hal_init.h"
@@ -62,18 +65,9 @@ STATIC samd_flash_obj_t samd_flash_obj = {
6265
.flash_size = (uint32_t)&_sflash_fs, // Get from MCU-Specific loader script.
6366
};
6467

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-
7468
// Flash init (from cctpy)
7569
// Method is needed for when MP starts up in _boot.py
76-
STATIC mp_obj_t samd_flash_init(void) {
70+
STATIC void samd_flash_init(void) {
7771
#ifdef SAMD51
7872
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
7973
#endif
@@ -82,9 +76,17 @@ STATIC mp_obj_t samd_flash_init(void) {
8276
#endif
8377

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

8991
// Function for ioctl.
9092
STATIC mp_obj_t eraseblock(uint32_t sector_in) {
@@ -102,14 +104,6 @@ STATIC mp_obj_t samd_flash_version(void) {
102104
}
103105
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_version_obj, samd_flash_version);
104106

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-
113107
STATIC mp_obj_t samd_flash_readblocks(size_t n_args, const mp_obj_t *args) {
114108
uint32_t offset = (mp_obj_get_int(args[1]) * BLOCK_SIZE) + samd_flash_obj.flash_base;
115109
mp_buffer_info_t bufinfo;
@@ -171,8 +165,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_flash_ioctl_obj, samd_flash_ioctl);
171165

172166
STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table[] = {
173167
{ 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) },
176168
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&samd_flash_readblocks_obj) },
177169
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&samd_flash_writeblocks_obj) },
178170
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&samd_flash_ioctl_obj) },
@@ -186,3 +178,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
186178
make_new, samd_flash_make_new,
187179
locals_dict, &samd_flash_locals_dict
188180
);
181+
182+
#endif // MICROPY_HW_MCUFLASH

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy