Skip to content

Commit b240bc6

Browse files
committed
samd: Integrate the block device drivers into the (Q)SPI drivers.
And roll back the change to samd_flash.c. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 575c97b commit b240bc6

File tree

9 files changed

+340
-270
lines changed

9 files changed

+340
-270
lines changed

ports/samd/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ SRC_C += \
112112
pendsv.c \
113113
pin_af.c \
114114
samd_isr.c \
115-
samd_mcuflash.c \
115+
samd_flash.c \
116116
samd_qspiflash.c \
117117
samd_soc.c \
118-
spiflash.c \
118+
samd_spiflash.c \
119119
tusb_port.c \
120120

121121
SHARED_SRC_C += \

ports/samd/boards/SPARKFUN_SAMD51_THING_PLUS/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
// Enabling both two lines below will set the boot file system to
1313
// the board's external flash.
1414
#define MICROPY_HW_SPIFLASH (1)
15-
// #define MICROPY_HW_SPIFLASH_ID (0)
15+
#define MICROPY_HW_SPIFLASH_ID (0)

ports/samd/modsamd.c

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

35-
extern const mp_obj_type_t samd_mcuflash_type;
35+
extern const mp_obj_type_t samd_flash_type;
3636
#ifdef MICROPY_HW_QSPIFLASH
3737
extern const mp_obj_type_t samd_qspiflash_type;
3838
#endif
@@ -75,7 +75,7 @@ STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
7575
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
7676
{ MP_ROM_QSTR(MP_QSTR_pininfo), MP_ROM_PTR(&samd_pininfo_obj) },
7777
#if MICROPY_HW_MCUFLASH
78-
{ MP_ROM_QSTR(MP_QSTR_MCUflash), MP_ROM_PTR(&samd_mcuflash_type) },
78+
{ MP_ROM_QSTR(MP_QSTR_MCUflash), MP_ROM_PTR(&samd_flash_type) },
7979
#endif
8080
#ifdef MICROPY_HW_QSPIFLASH
8181
{ MP_ROM_QSTR(MP_QSTR_QSPIflash), MP_ROM_PTR(&samd_qspiflash_type) },

ports/samd/modules/_boot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import gc
22
import uos
33
import samd
4-
from flashbdev import FlashBdev
54

65
flashfs = False
76
# Try to mount an external flash drive.
87
try:
98
if hasattr(samd, "QSPIflash"):
10-
bdev = FlashBdev(samd.QSPIflash())
9+
bdev = samd.QSPIflash()
1110
progsize = 256
1211
flashfs = True
1312
elif hasattr(samd, "SPIflash") and hasattr(samd, "SPI_ID"):
@@ -16,12 +15,13 @@
1615
spi = SPI(
1716
samd.SPI_ID, sck="FLASH_SCK", mosi="FLASH_MOSI", miso="FLASH_MISO", baudrate=24_000_000
1817
)
19-
bdev = FlashBdev(samd.SPIflash(spi, Pin("FLASH_CS", Pin.OUT, value=1)))
18+
bdev = samd.SPIflash(spi, Pin("FLASH_CS", Pin.OUT, value=1))
2019
progsize = 256
2120
flashfs = True
2221
del SPI, Pin, spi
2322
elif hasattr(samd, "MCUflash"):
24-
bdev = FlashBdev(samd.MCUflash())
23+
samd.MCUflash.flash_init()
24+
bdev = samd.MCUflash()
2525
progsize = 32 # which is the default
2626
flashfs = True
2727
except:
@@ -40,5 +40,5 @@
4040
uos.mount(vfs, "/")
4141
del vfs, fs_type, progsize, bdev, flashfs
4242

43-
del uos, samd, FlashBdev
43+
del uos, samd
4444
gc.collect()

ports/samd/modules/flashbdev.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

ports/samd/samd_flash.c

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
29+
#include "py/runtime.h"
30+
#include "extmod/vfs.h"
31+
#include "samd_soc.h"
32+
#include "hal_flash.h"
33+
34+
// ASF 4
35+
#include "hal_flash.h"
36+
#include "hal_init.h"
37+
#include "hpl_gclk_base.h"
38+
39+
#if defined(MCU_SAMD21)
40+
#include "lib/asf4/samd21/hpl/pm/hpl_pm_base.h"
41+
#elif defined(MCU_SAMD51)
42+
#include "lib/asf4/samd51/hpl/pm/hpl_pm_base.h"
43+
#include "lib/asf4/samd51/hri/hri_mclk_d51.h"
44+
#endif
45+
46+
static struct flash_descriptor flash_desc;
47+
STATIC mp_int_t BLOCK_SIZE = VFS_BLOCK_SIZE_BYTES; // Board specific: mpconfigboard.h
48+
extern const mp_obj_type_t samd_flash_type;
49+
50+
typedef struct _samd_flash_obj_t {
51+
mp_obj_base_t base;
52+
uint32_t flash_base;
53+
uint32_t flash_size;
54+
} samd_flash_obj_t;
55+
56+
extern uint8_t _oflash_fs, _sflash_fs;
57+
58+
// Build a Flash storage at top.
59+
STATIC samd_flash_obj_t samd_flash_obj = {
60+
.base = { &samd_flash_type },
61+
.flash_base = (uint32_t)&_oflash_fs, // Get from MCU-Specific loader script.
62+
.flash_size = (uint32_t)&_sflash_fs, // Get from MCU-Specific loader script.
63+
};
64+
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+
74+
// Flash init (from cctpy)
75+
// Method is needed for when MP starts up in _boot.py
76+
STATIC mp_obj_t samd_flash_init(void) {
77+
#ifdef SAMD51
78+
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
79+
#endif
80+
#ifdef SAMD21
81+
_pm_enable_bus_clock(PM_BUS_APBB, NVMCTRL);
82+
#endif
83+
84+
flash_init(&flash_desc, NVMCTRL);
85+
return mp_const_none;
86+
}
87+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_init_obj, samd_flash_init);
88+
89+
// Function for ioctl.
90+
STATIC mp_obj_t eraseblock(uint32_t sector_in) {
91+
// Destination address aligned with page start to be erased.
92+
uint32_t DEST_ADDR = sector_in; // Number of pages to be erased.
93+
mp_int_t PAGE_SIZE = flash_get_page_size(&flash_desc); // adf4 API call
94+
95+
flash_erase(&flash_desc, DEST_ADDR, (BLOCK_SIZE / PAGE_SIZE));
96+
97+
return mp_const_none;
98+
}
99+
100+
STATIC mp_obj_t samd_flash_version(void) {
101+
return MP_OBJ_NEW_SMALL_INT(flash_get_version());
102+
}
103+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(samd_flash_version_obj, samd_flash_version);
104+
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+
113+
STATIC mp_obj_t samd_flash_readblocks(size_t n_args, const mp_obj_t *args) {
114+
uint32_t offset = (mp_obj_get_int(args[1]) * BLOCK_SIZE) + samd_flash_obj.flash_base;
115+
mp_buffer_info_t bufinfo;
116+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
117+
if (n_args == 4) {
118+
offset += mp_obj_get_int(args[3]);
119+
}
120+
121+
// Read data to flash (adf4 API)
122+
flash_read(&flash_desc, offset, bufinfo.buf, bufinfo.len);
123+
124+
return mp_const_none;
125+
}
126+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(samd_flash_readblocks_obj, 3, 4, samd_flash_readblocks);
127+
128+
STATIC mp_obj_t samd_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
129+
uint32_t offset = (mp_obj_get_int(args[1]) * BLOCK_SIZE) + samd_flash_obj.flash_base;
130+
mp_buffer_info_t bufinfo;
131+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
132+
if (n_args == 3) {
133+
eraseblock(offset);
134+
// TODO check return value
135+
} else {
136+
offset += mp_obj_get_int(args[3]);
137+
}
138+
// Write data to flash (adf4 API)
139+
flash_write(&flash_desc, offset, bufinfo.buf, bufinfo.len);
140+
// TODO check return value
141+
return mp_const_none;
142+
}
143+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(samd_flash_writeblocks_obj, 3, 4, samd_flash_writeblocks);
144+
145+
STATIC mp_obj_t samd_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) {
146+
samd_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
147+
mp_int_t cmd = mp_obj_get_int(cmd_in);
148+
149+
switch (cmd) {
150+
case MP_BLOCKDEV_IOCTL_INIT:
151+
samd_flash_init();
152+
return MP_OBJ_NEW_SMALL_INT(0);
153+
case MP_BLOCKDEV_IOCTL_DEINIT:
154+
return MP_OBJ_NEW_SMALL_INT(0);
155+
case MP_BLOCKDEV_IOCTL_SYNC:
156+
return MP_OBJ_NEW_SMALL_INT(0);
157+
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
158+
return MP_OBJ_NEW_SMALL_INT(self->flash_size / BLOCK_SIZE);
159+
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
160+
return MP_OBJ_NEW_SMALL_INT(BLOCK_SIZE);
161+
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: {
162+
eraseblock(mp_obj_get_int(arg_in) * BLOCK_SIZE + samd_flash_obj.flash_base);
163+
// TODO check return value
164+
return MP_OBJ_NEW_SMALL_INT(0);
165+
}
166+
default:
167+
return mp_const_none;
168+
}
169+
}
170+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_flash_ioctl_obj, samd_flash_ioctl);
171+
172+
STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table[] = {
173+
{ 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) },
176+
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&samd_flash_readblocks_obj) },
177+
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&samd_flash_writeblocks_obj) },
178+
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&samd_flash_ioctl_obj) },
179+
};
180+
STATIC MP_DEFINE_CONST_DICT(samd_flash_locals_dict, samd_flash_locals_dict_table);
181+
182+
MP_DEFINE_CONST_OBJ_TYPE(
183+
samd_flash_type,
184+
MP_QSTR_MCU_Flash,
185+
MP_TYPE_FLAG_NONE,
186+
make_new, samd_flash_make_new,
187+
locals_dict, &samd_flash_locals_dict
188+
);

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