|
| 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