2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-02-13 06:21:46 -05:00
|
|
|
* Copyright (c) 2013-2018 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-17 09:31:35 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-02-10 11:21:38 -05:00
|
|
|
#include "py/runtime.h"
|
2017-01-27 07:11:59 -05:00
|
|
|
#include "extmod/vfs_fat.h"
|
2016-02-10 11:21:38 -05:00
|
|
|
|
2014-03-17 09:31:35 -04:00
|
|
|
#include "led.h"
|
|
|
|
#include "storage.h"
|
2015-10-31 13:44:20 -04:00
|
|
|
#include "irq.h"
|
2014-03-17 09:31:35 -04:00
|
|
|
|
2017-01-24 00:58:50 -05:00
|
|
|
#if defined(MICROPY_HW_SPIFLASH_SIZE_BITS)
|
2015-11-24 08:35:24 -05:00
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
// Use external SPI flash as the storage medium
|
|
|
|
#define BDEV_NUM_BLOCKS (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE)
|
|
|
|
#define BDEV_INIT spi_bdev_init
|
|
|
|
#define BDEV_READBLOCK spi_bdev_readblock
|
|
|
|
#define BDEV_WRITEBLOCK spi_bdev_writeblock
|
2016-03-22 12:49:39 -04:00
|
|
|
|
2015-04-18 11:27:53 -04:00
|
|
|
#else
|
2014-03-17 09:31:35 -04:00
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
// Use internal flash as the storage medium
|
|
|
|
#define BDEV_NUM_BLOCKS flash_bdev_num_blocks()
|
|
|
|
#define BDEV_INIT flash_bdev_init
|
|
|
|
#define BDEV_IRQ_HANDLER flash_bdev_irq_handler
|
|
|
|
#define BDEV_FLUSH flash_bdev_flush
|
|
|
|
#define BDEV_READBLOCK flash_bdev_readblock
|
|
|
|
#define BDEV_WRITEBLOCK flash_bdev_writeblock
|
2017-01-24 00:58:50 -05:00
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
#endif
|
2017-01-24 00:58:50 -05:00
|
|
|
|
|
|
|
#define FLASH_PART1_START_BLOCK (0x100)
|
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
static bool storage_is_initialised = false;
|
2017-08-28 21:37:18 -04:00
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
void storage_init(void) {
|
|
|
|
if (!storage_is_initialised) {
|
|
|
|
storage_is_initialised = true;
|
2017-01-24 00:58:50 -05:00
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
BDEV_INIT();
|
2017-01-24 00:58:50 -05:00
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
#if defined(BDEV_IRQ_HANDLER)
|
|
|
|
// Enable the flash IRQ, which is used to also call our storage IRQ handler
|
|
|
|
// It needs to go at a higher priority than all those components that rely on
|
|
|
|
// the flash storage (eg higher than USB MSC).
|
|
|
|
HAL_NVIC_SetPriority(FLASH_IRQn, IRQ_PRI_FLASH, IRQ_SUBPRI_FLASH);
|
|
|
|
HAL_NVIC_EnableIRQ(FLASH_IRQn);
|
2017-01-24 00:58:50 -05:00
|
|
|
#endif
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t storage_get_block_size(void) {
|
|
|
|
return FLASH_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t storage_get_block_count(void) {
|
2018-02-13 06:21:46 -05:00
|
|
|
return FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS;
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
|
2014-04-16 18:08:36 -04:00
|
|
|
void storage_irq_handler(void) {
|
2018-02-13 06:21:46 -05:00
|
|
|
#if defined(BDEV_IRQ_HANDLER)
|
|
|
|
BDEV_IRQ_HANDLER();
|
2017-01-24 00:58:50 -05:00
|
|
|
#endif
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_flush(void) {
|
2018-02-13 06:21:46 -05:00
|
|
|
#if defined(BDEV_FLUSH)
|
|
|
|
BDEV_FLUSH();
|
2017-01-24 00:58:50 -05:00
|
|
|
#endif
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
|
|
|
|
buf[0] = boot;
|
|
|
|
|
|
|
|
if (num_blocks == 0) {
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = 0;
|
|
|
|
buf[3] = 0;
|
|
|
|
} else {
|
|
|
|
buf[1] = 0xff;
|
|
|
|
buf[2] = 0xff;
|
|
|
|
buf[3] = 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[4] = type;
|
|
|
|
|
|
|
|
if (num_blocks == 0) {
|
|
|
|
buf[5] = 0;
|
|
|
|
buf[6] = 0;
|
|
|
|
buf[7] = 0;
|
|
|
|
} else {
|
|
|
|
buf[5] = 0xff;
|
|
|
|
buf[6] = 0xff;
|
|
|
|
buf[7] = 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[8] = start_block;
|
|
|
|
buf[9] = start_block >> 8;
|
|
|
|
buf[10] = start_block >> 16;
|
|
|
|
buf[11] = start_block >> 24;
|
|
|
|
|
|
|
|
buf[12] = num_blocks;
|
|
|
|
buf[13] = num_blocks >> 8;
|
|
|
|
buf[14] = num_blocks >> 16;
|
|
|
|
buf[15] = num_blocks >> 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool storage_read_block(uint8_t *dest, uint32_t block) {
|
|
|
|
//printf("RD %u\n", block);
|
|
|
|
if (block == 0) {
|
|
|
|
// fake the MBR so we can decide on our own partition table
|
|
|
|
|
|
|
|
for (int i = 0; i < 446; i++) {
|
|
|
|
dest[i] = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, BDEV_NUM_BLOCKS);
|
2014-03-17 09:31:35 -04:00
|
|
|
build_partition(dest + 462, 0, 0, 0, 0);
|
|
|
|
build_partition(dest + 478, 0, 0, 0, 0);
|
|
|
|
build_partition(dest + 494, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
dest[510] = 0x55;
|
|
|
|
dest[511] = 0xaa;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2018-02-13 06:21:46 -05:00
|
|
|
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS) {
|
|
|
|
return BDEV_READBLOCK(dest, block - FLASH_PART1_START_BLOCK);
|
2015-06-27 18:27:23 -04:00
|
|
|
} else {
|
2018-02-13 06:21:46 -05:00
|
|
|
return false;
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool storage_write_block(const uint8_t *src, uint32_t block) {
|
|
|
|
//printf("WR %u\n", block);
|
|
|
|
if (block == 0) {
|
|
|
|
// can't write MBR, but pretend we did
|
|
|
|
return true;
|
2018-02-13 06:21:46 -05:00
|
|
|
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS) {
|
|
|
|
return BDEV_WRITEBLOCK(src, block - FLASH_PART1_START_BLOCK);
|
2015-06-27 18:27:23 -04:00
|
|
|
} else {
|
2018-02-13 06:21:46 -05:00
|
|
|
return false;
|
2014-03-17 09:31:35 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 11:21:38 -05:00
|
|
|
|
|
|
|
mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
|
|
|
for (size_t i = 0; i < num_blocks; i++) {
|
|
|
|
if (!storage_read_block(dest + i * FLASH_BLOCK_SIZE, block_num + i)) {
|
|
|
|
return 1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
|
|
|
|
for (size_t i = 0; i < num_blocks; i++) {
|
|
|
|
if (!storage_write_block(src + i * FLASH_BLOCK_SIZE, block_num + i)) {
|
|
|
|
return 1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// MicroPython bindings
|
|
|
|
//
|
|
|
|
// Expose the flash as an object with the block protocol.
|
|
|
|
|
|
|
|
// there is a singleton Flash object
|
|
|
|
STATIC const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type};
|
|
|
|
|
|
|
|
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 *args) {
|
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
|
|
|
|
// return singleton object
|
|
|
|
return (mp_obj_t)&pyb_flash_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
|
|
|
mp_uint_t ret = storage_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(ret);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
|
|
|
mp_uint_t ret = storage_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(ret);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
|
|
|
|
mp_int_t cmd = mp_obj_get_int(cmd_in);
|
|
|
|
switch (cmd) {
|
2016-02-10 18:36:19 -05:00
|
|
|
case BP_IOCTL_INIT: storage_init(); return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
case BP_IOCTL_DEINIT: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); // TODO properly
|
|
|
|
case BP_IOCTL_SYNC: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(storage_get_block_count());
|
|
|
|
case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(storage_get_block_size());
|
2016-02-10 11:21:38 -05:00
|
|
|
default: return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl);
|
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_flash_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&pyb_flash_readblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&pyb_flash_writeblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&pyb_flash_ioctl_obj) },
|
2016-02-10 11:21:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t pyb_flash_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Flash,
|
|
|
|
.make_new = pyb_flash_make_new,
|
2017-05-06 03:03:40 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&pyb_flash_locals_dict,
|
2016-02-10 11:21:38 -05:00
|
|
|
};
|
2016-02-10 11:32:57 -05:00
|
|
|
|
|
|
|
void pyb_flash_init_vfs(fs_user_mount_t *vfs) {
|
2017-01-27 07:11:59 -05:00
|
|
|
vfs->base.type = &mp_fat_vfs_type;
|
2016-02-10 11:32:57 -05:00
|
|
|
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
|
2017-01-27 07:11:59 -05:00
|
|
|
vfs->fatfs.drv = vfs;
|
2017-01-29 21:02:10 -05:00
|
|
|
vfs->fatfs.part = 1; // flash filesystem lives on first partition
|
2016-02-10 11:32:57 -05:00
|
|
|
vfs->readblocks[0] = (mp_obj_t)&pyb_flash_readblocks_obj;
|
|
|
|
vfs->readblocks[1] = (mp_obj_t)&pyb_flash_obj;
|
|
|
|
vfs->readblocks[2] = (mp_obj_t)storage_read_blocks; // native version
|
|
|
|
vfs->writeblocks[0] = (mp_obj_t)&pyb_flash_writeblocks_obj;
|
|
|
|
vfs->writeblocks[1] = (mp_obj_t)&pyb_flash_obj;
|
|
|
|
vfs->writeblocks[2] = (mp_obj_t)storage_write_blocks; // native version
|
|
|
|
vfs->u.ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj;
|
|
|
|
vfs->u.ioctl[1] = (mp_obj_t)&pyb_flash_obj;
|
|
|
|
}
|