Skip to content

Commit 27f84a2

Browse files
committed
samd: Harmonize the flash driver interfaces.
- Move the block device API out of samd_flash.c - Rename the internal functions and objects to ...samd_mcuflash_.... - Change _boot.py accordingly. Saves about 300 bytes of code.
1 parent 7010138 commit 27f84a2

File tree

4 files changed

+149
-191
lines changed

4 files changed

+149
-191
lines changed

ports/samd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ SRC_C += \
100100
mphalport.c \
101101
pendsv.c \
102102
pin_af.c \
103-
samd_flash.c \
104103
samd_isr.c \
104+
samd_mcuflash.c \
105105
samd_qspiflash.c \
106106
samd_soc.c \
107107
spiflash.c \

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_flash_type;
35+
extern const mp_obj_type_t samd_mcuflash_type;
3636
#ifdef MICROPY_HW_QSPIFLASH
3737
extern const mp_obj_type_t samd_qspiflash_type;
3838
#endif
@@ -76,7 +76,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_pininfo_obj, samd_pininfo);
7676

7777
STATIC const mp_rom_map_elem_t samd_module_globals_table[] = {
7878
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_samd) },
79-
{ MP_ROM_QSTR(MP_QSTR_Flash), MP_ROM_PTR(&samd_flash_type) },
79+
{ MP_ROM_QSTR(MP_QSTR_MCUflash), MP_ROM_PTR(&samd_mcuflash_type) },
8080
{ MP_ROM_QSTR(MP_QSTR_pininfo), MP_ROM_PTR(&samd_pininfo_obj) },
8181
#ifdef MICROPY_HW_QSPIFLASH
8282
{ MP_ROM_QSTR(MP_QSTR_QSPIflash), MP_ROM_PTR(&samd_qspiflash_type) },

ports/samd/samd_flash.c

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

ports/samd/samd_mcuflash.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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_mcuflash_type;
49+
50+
typedef struct _samd_mcuflash_obj_t {
51+
mp_obj_base_t base;
52+
uint32_t flash_base;
53+
uint32_t flash_size;
54+
} samd_mcuflash_obj_t;
55+
56+
extern uint8_t _oflash_fs, _sflash_fs;
57+
58+
// Build a Flash storage at top.
59+
STATIC samd_mcuflash_obj_t samd_mcuflash_obj = {
60+
.base = { &samd_mcuflash_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_mcuflash_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_mcuflash_obj.
68+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
69+
70+
#ifdef SAMD51
71+
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
72+
#endif
73+
#ifdef SAMD21
74+
_pm_enable_bus_clock(PM_BUS_APBB, NVMCTRL);
75+
#endif
76+
77+
flash_init(&flash_desc, NVMCTRL);
78+
79+
// Return singleton object.
80+
return MP_OBJ_FROM_PTR(&samd_mcuflash_obj);
81+
}
82+
83+
// Function for ioctl.
84+
STATIC mp_obj_t samd_mcuflash_erase(mp_obj_t self_in, mp_obj_t addr_in) {
85+
// Destination address aligned with page start to be erased.
86+
uint32_t DEST_ADDR = mp_obj_get_int(addr_in) + samd_mcuflash_obj.flash_base;
87+
mp_int_t PAGE_SIZE = flash_get_page_size(&flash_desc); // adf4 API call
88+
89+
flash_erase(&flash_desc, DEST_ADDR, (BLOCK_SIZE / PAGE_SIZE));
90+
91+
return mp_const_none;
92+
}
93+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(samd_mcuflash_erase_obj, samd_mcuflash_erase);
94+
95+
STATIC mp_obj_t samd_mcuflash_size(mp_obj_t self_in) {
96+
samd_mcuflash_obj_t *self = MP_OBJ_TO_PTR(self_in);
97+
return MP_OBJ_NEW_SMALL_INT(self->flash_size);
98+
}
99+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_mcuflash_size_obj, samd_mcuflash_size);
100+
101+
STATIC mp_obj_t samd_mcuflash_sectorsize(mp_obj_t self_in) {
102+
return MP_OBJ_NEW_SMALL_INT(BLOCK_SIZE);
103+
}
104+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(samd_mcuflash_sectorsize_obj, samd_mcuflash_sectorsize);
105+
106+
STATIC mp_obj_t samd_mcuflash_read(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t buf_in) {
107+
samd_mcuflash_obj_t *self = MP_OBJ_TO_PTR(self_in);
108+
uint32_t offset = mp_obj_get_int(addr_in) + self->flash_base;
109+
mp_buffer_info_t bufinfo;
110+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
111+
// Read data to flash (asf4 API)
112+
flash_read(&flash_desc, offset, bufinfo.buf, bufinfo.len);
113+
114+
return mp_const_none;
115+
}
116+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_mcuflash_read_obj, samd_mcuflash_read);
117+
118+
STATIC mp_obj_t samd_mcuflash_write(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t buf_in) {
119+
samd_mcuflash_obj_t *self = MP_OBJ_TO_PTR(self_in);
120+
uint32_t offset = mp_obj_get_int(addr_in) + self->flash_base;
121+
mp_buffer_info_t bufinfo;
122+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
123+
// Write data to flash (asf4 API)
124+
flash_write(&flash_desc, offset, bufinfo.buf, bufinfo.len);
125+
// TODO check return value
126+
return mp_const_none;
127+
}
128+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_mcuflash_write_obj, samd_mcuflash_write);
129+
130+
STATIC const mp_rom_map_elem_t samd_mcuflash_locals_dict_table[] = {
131+
{ MP_ROM_QSTR(MP_QSTR_flash_size), MP_ROM_PTR(&samd_mcuflash_size_obj) },
132+
{ MP_ROM_QSTR(MP_QSTR_flash_sectorsize), MP_ROM_PTR(&samd_mcuflash_sectorsize_obj) },
133+
{ MP_ROM_QSTR(MP_QSTR_flash_read), MP_ROM_PTR(&samd_mcuflash_read_obj) },
134+
{ MP_ROM_QSTR(MP_QSTR_flash_write), MP_ROM_PTR(&samd_mcuflash_write_obj) },
135+
{ MP_ROM_QSTR(MP_QSTR_flash_erase), MP_ROM_PTR(&samd_mcuflash_erase_obj) },
136+
};
137+
STATIC MP_DEFINE_CONST_DICT(samd_mcuflash_locals_dict, samd_mcuflash_locals_dict_table);
138+
139+
140+
MP_DEFINE_CONST_OBJ_TYPE(
141+
samd_mcuflash_type,
142+
MP_QSTR_MCU_flash,
143+
MP_TYPE_FLAG_NONE,
144+
make_new, samd_mcuflash_make_new,
145+
locals_dict, &samd_mcuflash_locals_dict
146+
);

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