Debian Codegraph @

ports/stm32/boards/LEGO_HUB_NO6/spiflash.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2024 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 #if !BUILDING_MBOOT
28
29 #include "py/runtime.h"
30 #include "extmod/vfs.h"
31
32 #include "storage.h"
33
34 // Expose the entire external SPI flash as an object with the block protocol.
35
36 #define FLASH_SIZE (MICROPY_HW_SPIFLASH_OFFSET_BYTES + MICROPY_HW_SPIFLASH_SIZE_BITS / 8)
37 #define BLOCK_SIZE (4096)
38
39 typedef struct _spi_flash_obj_t {
40 mp_obj_base_t base;
41 uint32_t start; // in bytes
42 uint32_t len; // in bytes
43 } spi_flash_obj_t;
44
45 static const mp_obj_type_t spi_flash_type;
46
47 static void spi_flash_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
48 spi_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
49 mp_printf(print, "SPIFlash(start=%u, len=%u)", self->start, self->len);
50 }
51
52 static mp_obj_t spi_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
53 enum { ARG_start, ARG_len };
54 static const mp_arg_t allowed_args[] = {
55 { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
56 { MP_QSTR_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
57 };
58 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
59 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
60
61 spi_flash_obj_t *self = mp_obj_malloc(spi_flash_obj_t, &spi_flash_type);
62
63 mp_int_t start = args[ARG_start].u_int;
64 if (!(0 <= start && start < FLASH_SIZE && start % BLOCK_SIZE == 0)) {
65 mp_raise_ValueError(NULL);
66 }
67
68 mp_int_t len = args[ARG_len].u_int;
69 if (len == -1) {
70 len = FLASH_SIZE - start;
71 } else if (!(0 < len && start + len <= FLASH_SIZE && len % BLOCK_SIZE == 0)) {
72 mp_raise_ValueError(NULL);
73 }
74
75 self->start = start;
76 self->len = len;
77
78 return MP_OBJ_FROM_PTR(self);
79 }
80
81 static mp_obj_t spi_flash_readblocks(size_t n_args, const mp_obj_t *args) {
82 spi_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]);
83 uint32_t block_num = self->start / BLOCK_SIZE + mp_obj_get_int(args[1]);
84 mp_buffer_info_t bufinfo;
85 mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
86 int ret = spi_bdev_readblocks_raw(&spi_bdev, bufinfo.buf, block_num, 0, bufinfo.len);
87 return MP_OBJ_NEW_SMALL_INT(ret);
88 }
89 static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(spi_flash_readblocks_obj, 3, 3, spi_flash_readblocks);
90
91 static mp_obj_t spi_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
92 spi_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]);
93 uint32_t block_num = self->start / BLOCK_SIZE + mp_obj_get_int(args[1]);
94 mp_buffer_info_t bufinfo;
95 mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
96 int ret = spi_bdev_eraseblocks_raw(&spi_bdev, block_num, bufinfo.len);
97 if (ret == 0) {
98 ret = spi_bdev_writeblocks_raw(&spi_bdev, bufinfo.buf, block_num, 0, bufinfo.len);
99 }
100 return MP_OBJ_NEW_SMALL_INT(ret);
101 }
102 static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(spi_flash_writeblocks_obj, 3, 3, spi_flash_writeblocks);
103
104 static mp_obj_t spi_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) {
105 spi_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
106
107 mp_int_t cmd = mp_obj_get_int(cmd_in);
108 switch (cmd) {
109 case MP_BLOCKDEV_IOCTL_INIT:
110 storage_init();
111 return MP_OBJ_NEW_SMALL_INT(0);
112
113 case MP_BLOCKDEV_IOCTL_DEINIT:
114 return MP_OBJ_NEW_SMALL_INT(0);
115
116 case MP_BLOCKDEV_IOCTL_SYNC:
117 return MP_OBJ_NEW_SMALL_INT(0);
118
119 case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: {
120 mp_int_t n = self->len / BLOCK_SIZE;
121 return MP_OBJ_NEW_SMALL_INT(n);
122 }
123
124 case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
125 return MP_OBJ_NEW_SMALL_INT(BLOCK_SIZE);
126
127 default:
128 return mp_const_none;
129 }
130 }
131 static MP_DEFINE_CONST_FUN_OBJ_3(spi_flash_ioctl_obj, spi_flash_ioctl);
132
133 static const mp_rom_map_elem_t spi_flash_locals_dict_table[] = {
134 { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&spi_flash_readblocks_obj) },
135 { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&spi_flash_writeblocks_obj) },
136 { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&spi_flash_ioctl_obj) },
137 };
138 static MP_DEFINE_CONST_DICT(spi_flash_locals_dict, spi_flash_locals_dict_table);
139
140 static MP_DEFINE_CONST_OBJ_TYPE(
141 spi_flash_type,
142 MP_QSTR_SPIFlash,
143 MP_TYPE_FLAG_NONE,
144 make_new, spi_flash_make_new,
145 print, spi_flash_print,
146 locals_dict, &spi_flash_locals_dict
147 );
148
149 /******************************************************************************/
150 // The `spiflash` module.
151
152 static const mp_rom_map_elem_t spiflash_module_globals_table[] = {
153 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_spiflash) },
154
155 { MP_ROM_QSTR(MP_QSTR_SPIFlash), MP_ROM_PTR(&spi_flash_type) },
156 };
157 static MP_DEFINE_CONST_DICT(spiflash_module_globals, spiflash_module_globals_table);
158
159 const mp_obj_module_t spiflash_module = {
160 .base = { &mp_type_module },
161 .globals = (mp_obj_dict_t *)&spiflash_module_globals,
162 };
163
164 MP_REGISTER_MODULE(MP_QSTR_spiflash, spiflash_module);
165
166 #endif