1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013-2018 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 <stdint.h>
28 #include <string.h>
29
30 #include "py/runtime.h"
31 #include "py/mperrno.h"
32 #include "extmod/vfs_fat.h"
33
34 #include "systick.h"
35 #include "led.h"
36 #include "storage.h"
37 #include "irq.h"
38
39 #if MICROPY_HW_ENABLE_STORAGE
40
41 #define STORAGE_SYSTICK_MASK (0x1ff) // 512ms
42 #define STORAGE_IDLE_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & STORAGE_SYSTICK_MASK) == 0)
43
44 #if defined(MICROPY_HW_BDEV2_IOCTL)
45 #define FLASH_PART2_START_BLOCK (FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0))
46 #endif
47
48 static bool storage_is_initialised = false;
49
50 static void storage_systick_callback(uint32_t ticks_ms);
51
storage_init(void)52 void storage_init(void) {
53 if (!storage_is_initialised) {
54 storage_is_initialised = true;
55
56 systick_enable_dispatch(SYSTICK_DISPATCH_STORAGE, storage_systick_callback);
57
58 MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_INIT, 0);
59
60 #if defined(MICROPY_HW_BDEV2_IOCTL)
61 MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_INIT, 0);
62 #endif
63
64 // Enable the flash IRQ, which is used to also call our storage IRQ handler
65 // It must go at the same priority as USB (see comment in irq.h).
66 NVIC_SetPriority(FLASH_IRQn, IRQ_PRI_FLASH);
67 HAL_NVIC_EnableIRQ(FLASH_IRQn);
68 }
69 }
70
storage_get_block_size(void)71 uint32_t storage_get_block_size(void) {
72 return FLASH_BLOCK_SIZE;
73 }
74
storage_get_block_count(void)75 uint32_t storage_get_block_count(void) {
76 #if defined(MICROPY_HW_BDEV2_IOCTL)
77 return FLASH_PART2_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0);
78 #else
79 return FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0);
80 #endif
81 }
82
storage_systick_callback(uint32_t ticks_ms)83 static void storage_systick_callback(uint32_t ticks_ms) {
84 if (STORAGE_IDLE_TICK(ticks_ms)) {
85 // Trigger a FLASH IRQ to execute at a lower priority
86 NVIC->STIR = FLASH_IRQn;
87 }
88 }
89
FLASH_IRQHandler(void)90 void FLASH_IRQHandler(void) {
91 IRQ_ENTER(FLASH_IRQn);
92 MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_IRQ_HANDLER, 0);
93 #if defined(MICROPY_HW_BDEV2_IOCTL)
94 MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_IRQ_HANDLER, 0);
95 #endif
96 IRQ_EXIT(FLASH_IRQn);
97 }
98
storage_flush(void)99 void storage_flush(void) {
100 MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_SYNC, 0);
101 #if defined(MICROPY_HW_BDEV2_IOCTL)
102 MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_SYNC, 0);
103 #endif
104 }
105
build_partition(uint8_t * buf,int boot,int type,uint32_t start_block,uint32_t num_blocks)106 static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
107 buf[0] = boot;
108
109 if (num_blocks == 0) {
110 buf[1] = 0;
111 buf[2] = 0;
112 buf[3] = 0;
113 } else {
114 buf[1] = 0xff;
115 buf[2] = 0xff;
116 buf[3] = 0xff;
117 }
118
119 buf[4] = type;
120
121 if (num_blocks == 0) {
122 buf[5] = 0;
123 buf[6] = 0;
124 buf[7] = 0;
125 } else {
126 buf[5] = 0xff;
127 buf[6] = 0xff;
128 buf[7] = 0xff;
129 }
130
131 buf[8] = start_block;
132 buf[9] = start_block >> 8;
133 buf[10] = start_block >> 16;
134 buf[11] = start_block >> 24;
135
136 buf[12] = num_blocks;
137 buf[13] = num_blocks >> 8;
138 buf[14] = num_blocks >> 16;
139 buf[15] = num_blocks >> 24;
140 }
141
storage_read_block(uint8_t * dest,uint32_t block)142 bool storage_read_block(uint8_t *dest, uint32_t block) {
143 // printf("RD %u\n", block);
144 if (block == 0) {
145 // fake the MBR so we can decide on our own partition table
146
147 for (int i = 0; i < 446; i++) {
148 dest[i] = 0;
149 }
150
151 build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0));
152 #if defined(MICROPY_HW_BDEV2_IOCTL)
153 build_partition(dest + 462, 0, 0x01 /* FAT12 */, FLASH_PART2_START_BLOCK, MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0));
154 #else
155 build_partition(dest + 462, 0, 0, 0, 0);
156 #endif
157 build_partition(dest + 478, 0, 0, 0, 0);
158 build_partition(dest + 494, 0, 0, 0, 0);
159
160 dest[510] = 0x55;
161 dest[511] = 0xaa;
162
163 return true;
164
165 #if defined(MICROPY_HW_BDEV_READBLOCK)
166 } else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
167 return MICROPY_HW_BDEV_READBLOCK(dest, block - FLASH_PART1_START_BLOCK);
168 #endif
169 } else {
170 return false;
171 }
172 }
173
storage_write_block(const uint8_t * src,uint32_t block)174 bool storage_write_block(const uint8_t *src, uint32_t block) {
175 // printf("WR %u\n", block);
176 if (block == 0) {
177 // can't write MBR, but pretend we did
178 return true;
179 #if defined(MICROPY_HW_BDEV_WRITEBLOCK)
180 } else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
181 return MICROPY_HW_BDEV_WRITEBLOCK(src, block - FLASH_PART1_START_BLOCK);
182 #endif
183 } else {
184 return false;
185 }
186 }
187
storage_read_blocks(uint8_t * dest,uint32_t block_num,uint32_t num_blocks)188 int storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
189 #if defined(MICROPY_HW_BDEV_READBLOCKS)
190 if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
191 return MICROPY_HW_BDEV_READBLOCKS(dest, block_num - FLASH_PART1_START_BLOCK, num_blocks);
192 }
193 #endif
194
195 #if defined(MICROPY_HW_BDEV2_READBLOCKS)
196 if (FLASH_PART2_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART2_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
197 return MICROPY_HW_BDEV2_READBLOCKS(dest, block_num - FLASH_PART2_START_BLOCK, num_blocks);
198 }
199 #endif
200
201 for (size_t i = 0; i < num_blocks; i++) {
202 if (!storage_read_block(dest + i * FLASH_BLOCK_SIZE, block_num + i)) {
203 return -MP_EIO; // error
204 }
205 }
206 return 0; // success
207 }
208
storage_write_blocks(const uint8_t * src,uint32_t block_num,uint32_t num_blocks)209 int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
210 #if defined(MICROPY_HW_BDEV_WRITEBLOCKS)
211 if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
212 return MICROPY_HW_BDEV_WRITEBLOCKS(src, block_num - FLASH_PART1_START_BLOCK, num_blocks);
213 }
214 #endif
215
216 #if defined(MICROPY_HW_BDEV2_WRITEBLOCKS)
217 if (FLASH_PART2_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART2_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
218 return MICROPY_HW_BDEV2_WRITEBLOCKS(src, block_num - FLASH_PART2_START_BLOCK, num_blocks);
219 }
220 #endif
221
222 for (size_t i = 0; i < num_blocks; i++) {
223 if (!storage_write_block(src + i * FLASH_BLOCK_SIZE, block_num + i)) {
224 return -MP_EIO; // error
225 }
226 }
227 return 0; // success
228 }
229
230 /******************************************************************************/
231 // MicroPython bindings
232 //
233 // Expose the flash as an object with the block protocol.
234
235 #ifdef MICROPY_HW_BDEV_SPIFLASH_EXTENDED
236 // Board defined an external SPI flash for use with extended block protocol
237 #define MICROPY_HW_BDEV_BLOCKSIZE_EXT (MP_SPIFLASH_ERASE_BLOCK_SIZE)
238 #define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) \
239 (spi_bdev_readblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (dest), (bl), (off), (len)))
240 #define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(src, bl, off, len) \
241 (spi_bdev_writeblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (src), (bl), (off), (len)))
242 #define MICROPY_HW_BDEV_ERASEBLOCKS_EXT(bl, len) \
243 (spi_bdev_eraseblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (bl), (len)))
244
245 #elif (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) && MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
246 // Board uses littlefs and internal flash, so enable extended block protocol on internal flash
247 #define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
248 #define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) (flash_bdev_readblocks_ext((dest), (bl), (off), (len)))
249 #define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(dest, bl, off, len) (flash_bdev_writeblocks_ext((dest), (bl), (off), (len)))
250 #endif
251
252 #ifndef MICROPY_HW_BDEV_BLOCKSIZE_EXT
253 #define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
254 #endif
255
256 #if defined(MICROPY_HW_BDEV_READBLOCKS_EXT)
257 // Size of blocks is MICROPY_HW_BDEV_BLOCKSIZE_EXT
storage_readblocks_ext(uint8_t * dest,uint32_t block,uint32_t offset,uint32_t len)258 int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) {
259 return MICROPY_HW_BDEV_READBLOCKS_EXT(dest, block, offset, len);
260 }
261 #endif
262
263 typedef struct _pyb_flash_obj_t {
264 mp_obj_base_t base;
265 uint32_t start; // in bytes
266 uint32_t len; // in bytes
267 bool use_native_block_size;
268 } pyb_flash_obj_t;
269
270 // This Flash object represents the entire available flash, with emulated partition table at start
271 const pyb_flash_obj_t pyb_flash_obj = {
272 { &pyb_flash_type },
273 -(FLASH_PART1_START_BLOCK * FLASH_BLOCK_SIZE), // to offset FLASH_PART1_START_BLOCK
274 0, // actual size handled in ioctl, MP_BLOCKDEV_IOCTL_BLOCK_COUNT case
275 };
276
pyb_flash_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)277 STATIC void pyb_flash_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
278 pyb_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
279 if (self == &pyb_flash_obj) {
280 mp_printf(print, "Flash()");
281 } else {
282 mp_printf(print, "Flash(start=%u, len=%u)", self->start, self->len);
283 }
284 }
285
pyb_flash_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * all_args)286 STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
287 // Parse arguments
288 enum { ARG_start, ARG_len };
289 static const mp_arg_t allowed_args[] = {
290 { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
291 { MP_QSTR_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
292 };
293 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
294 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
295
296 if (args[ARG_start].u_int == -1 && args[ARG_len].u_int == -1) {
297 // Default singleton object that accesses entire flash, including virtual partition table
298 return MP_OBJ_FROM_PTR(&pyb_flash_obj);
299 }
300
301 pyb_flash_obj_t *self = m_new_obj(pyb_flash_obj_t);
302 self->base.type = &pyb_flash_type;
303 self->use_native_block_size = false;
304
305 uint32_t bl_len = (storage_get_block_count() - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE;
306
307 mp_int_t start = args[ARG_start].u_int;
308 if (start == -1) {
309 start = 0;
310 } else if (!(0 <= start && start < bl_len && start % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0)) {
311 mp_raise_ValueError(NULL);
312 }
313
314 mp_int_t len = args[ARG_len].u_int;
315 if (len == -1) {
316 len = bl_len - start;
317 } else if (!(0 < len && start + len <= bl_len && len % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0)) {
318 mp_raise_ValueError(NULL);
319 }
320
321 self->start = start;
322 self->len = len;
323
324 return MP_OBJ_FROM_PTR(self);
325 }
326
pyb_flash_readblocks(size_t n_args,const mp_obj_t * args)327 STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) {
328 pyb_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]);
329 uint32_t block_num = mp_obj_get_int(args[1]);
330 mp_buffer_info_t bufinfo;
331 mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
332 mp_uint_t ret = -MP_EIO;
333 if (n_args == 3) {
334 // Cast self->start to signed in case it's pyb_flash_obj with negative start
335 block_num += FLASH_PART1_START_BLOCK + (int32_t)self->start / FLASH_BLOCK_SIZE;
336 ret = storage_read_blocks(bufinfo.buf, block_num, bufinfo.len / FLASH_BLOCK_SIZE);
337 }
338 #if defined(MICROPY_HW_BDEV_READBLOCKS_EXT)
339 else if (self != &pyb_flash_obj) {
340 // Extended block read on a sub-section of the flash storage
341 uint32_t offset = mp_obj_get_int(args[3]);
342 if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT) >= self->len) {
343 ret = -MP_EFAULT; // Bad address
344 } else {
345 block_num += self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
346 ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
347 }
348 }
349 #endif
350 return MP_OBJ_NEW_SMALL_INT(ret);
351 }
352 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_flash_readblocks_obj, 3, 4, pyb_flash_readblocks);
353
pyb_flash_writeblocks(size_t n_args,const mp_obj_t * args)354 STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
355 pyb_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]);
356 uint32_t block_num = mp_obj_get_int(args[1]);
357 mp_buffer_info_t bufinfo;
358 mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
359 mp_uint_t ret = -MP_EIO;
360 if (n_args == 3) {
361 // Cast self->start to signed in case it's pyb_flash_obj with negative start
362 block_num += FLASH_PART1_START_BLOCK + (int32_t)self->start / FLASH_BLOCK_SIZE;
363 ret = storage_write_blocks(bufinfo.buf, block_num, bufinfo.len / FLASH_BLOCK_SIZE);
364 }
365 #if defined(MICROPY_HW_BDEV_WRITEBLOCKS_EXT)
366 else if (self != &pyb_flash_obj) {
367 // Extended block write on a sub-section of the flash storage
368 uint32_t offset = mp_obj_get_int(args[3]);
369 if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT) >= self->len) {
370 ret = -MP_EFAULT; // Bad address
371 } else {
372 block_num += self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
373 ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
374 }
375 }
376 #endif
377 return MP_OBJ_NEW_SMALL_INT(ret);
378 }
379 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_flash_writeblocks_obj, 3, 4, pyb_flash_writeblocks);
380
pyb_flash_ioctl(mp_obj_t self_in,mp_obj_t cmd_in,mp_obj_t arg_in)381 STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) {
382 pyb_flash_obj_t *self = MP_OBJ_TO_PTR(self_in);
383 mp_int_t cmd = mp_obj_get_int(cmd_in);
384 switch (cmd) {
385 case MP_BLOCKDEV_IOCTL_INIT: {
386 mp_int_t ret = 0;
387 storage_init();
388 if (mp_obj_get_int(arg_in) == 1) {
389 // Will be using extended block protocol
390 if (self == &pyb_flash_obj) {
391 ret = -1;
392 } else {
393 // Switch to use native block size of the underlying storage.
394 self->use_native_block_size = true;
395 }
396 }
397 return MP_OBJ_NEW_SMALL_INT(ret);
398 }
399 case MP_BLOCKDEV_IOCTL_DEINIT:
400 storage_flush();
401 return MP_OBJ_NEW_SMALL_INT(0); // TODO properly
402 case MP_BLOCKDEV_IOCTL_SYNC:
403 storage_flush();
404 return MP_OBJ_NEW_SMALL_INT(0);
405
406 case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: {
407 mp_int_t n;
408 if (self == &pyb_flash_obj) {
409 // Get true size
410 n = storage_get_block_count();
411 } else if (self->use_native_block_size) {
412 n = self->len / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
413 } else {
414 n = self->len / FLASH_BLOCK_SIZE;
415 }
416 return MP_OBJ_NEW_SMALL_INT(n);
417 }
418
419 case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: {
420 mp_int_t n = FLASH_BLOCK_SIZE;
421 if (self->use_native_block_size) {
422 n = MICROPY_HW_BDEV_BLOCKSIZE_EXT;
423 }
424 return MP_OBJ_NEW_SMALL_INT(n);
425 }
426
427 case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: {
428 int ret = 0;
429 #if defined(MICROPY_HW_BDEV_ERASEBLOCKS_EXT)
430 if (self->use_native_block_size) {
431 mp_int_t block_num = self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT + mp_obj_get_int(arg_in);
432
433 ret = MICROPY_HW_BDEV_ERASEBLOCKS_EXT(block_num, MICROPY_HW_BDEV_BLOCKSIZE_EXT);
434 }
435 #endif
436 return MP_OBJ_NEW_SMALL_INT(ret);
437 }
438
439 default:
440 return mp_const_none;
441 }
442 }
443 STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl);
444
445 STATIC const mp_rom_map_elem_t pyb_flash_locals_dict_table[] = {
446 { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&pyb_flash_readblocks_obj) },
447 { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&pyb_flash_writeblocks_obj) },
448 { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&pyb_flash_ioctl_obj) },
449 };
450
451 STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table);
452
453 const mp_obj_type_t pyb_flash_type = {
454 { &mp_type_type },
455 .name = MP_QSTR_Flash,
456 .print = pyb_flash_print,
457 .make_new = pyb_flash_make_new,
458 .locals_dict = (mp_obj_dict_t *)&pyb_flash_locals_dict,
459 };
460
pyb_flash_init_vfs(fs_user_mount_t * vfs)461 void pyb_flash_init_vfs(fs_user_mount_t *vfs) {
462 vfs->base.type = &mp_fat_vfs_type;
463 vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NATIVE | MP_BLOCKDEV_FLAG_HAVE_IOCTL;
464 vfs->fatfs.drv = vfs;
465 #if MICROPY_FATFS_MULTI_PARTITION
466 vfs->fatfs.part = 1; // flash filesystem lives on first partition
467 #endif
468 vfs->blockdev.readblocks[0] = MP_OBJ_FROM_PTR(&pyb_flash_readblocks_obj);
469 vfs->blockdev.readblocks[1] = MP_OBJ_FROM_PTR(&pyb_flash_obj);
470 vfs->blockdev.readblocks[2] = MP_OBJ_FROM_PTR(storage_read_blocks); // native version
471 vfs->blockdev.writeblocks[0] = MP_OBJ_FROM_PTR(&pyb_flash_writeblocks_obj);
472 vfs->blockdev.writeblocks[1] = MP_OBJ_FROM_PTR(&pyb_flash_obj);
473 vfs->blockdev.writeblocks[2] = MP_OBJ_FROM_PTR(storage_write_blocks); // native version
474 vfs->blockdev.u.ioctl[0] = MP_OBJ_FROM_PTR(&pyb_flash_ioctl_obj);
475 vfs->blockdev.u.ioctl[1] = MP_OBJ_FROM_PTR(&pyb_flash_obj);
476 }
477
478 #endif
479