2016-09-02 20:00:30 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-09-02 20:00:30 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-10-28 23:16:39 -04:00
|
|
|
#include "internal_flash.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-06-20 13:56:05 -04:00
|
|
|
#include "extmod/vfs.h"
|
|
|
|
#include "extmod/vfs_fat.h"
|
2016-10-25 17:27:59 -04:00
|
|
|
#include "py/mphal.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
2017-06-20 13:56:05 -04:00
|
|
|
#include "lib/oofatfs/ff.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
#ifdef SAMD21
|
|
|
|
#include "hpl/pm/hpl_pm_base.h"
|
|
|
|
#endif
|
|
|
|
#include "hal/include/hal_flash.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
#include "supervisor/shared/rgb_led_status.h"
|
|
|
|
|
|
|
|
static struct flash_descriptor internal_flash_desc;
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
void internal_flash_init(void) {
|
2017-01-26 21:05:46 -05:00
|
|
|
// Activity LED for flash writes.
|
|
|
|
#ifdef MICROPY_HW_LED_MSC
|
|
|
|
struct port_config pin_conf;
|
|
|
|
port_get_config_defaults(&pin_conf);
|
2016-10-25 18:06:05 -04:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
|
|
|
|
port_pin_set_config(MICROPY_HW_LED_MSC, &pin_conf);
|
|
|
|
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
|
|
|
|
#endif
|
2017-10-10 14:36:00 -04:00
|
|
|
|
|
|
|
#ifdef SAMD51
|
|
|
|
hri_mclk_set_AHBMASK_NVMCTRL_bit(MCLK);
|
|
|
|
#endif
|
|
|
|
#ifdef SAMD21
|
|
|
|
_pm_enable_bus_clock(PM_BUS_APBB, NVMCTRL);
|
|
|
|
#endif
|
|
|
|
flash_init(&internal_flash_desc, NVMCTRL);
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
uint32_t internal_flash_get_block_size(void) {
|
2017-05-12 17:47:39 -04:00
|
|
|
return FILESYSTEM_BLOCK_SIZE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
uint32_t internal_flash_get_block_count(void) {
|
|
|
|
return INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
void internal_flash_flush(void) {
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2017-05-02 18:25:06 -04:00
|
|
|
void flash_flush(void) {
|
|
|
|
internal_flash_flush();
|
|
|
|
}
|
|
|
|
|
2016-09-02 20:00:30 -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;
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
static int32_t convert_block_to_flash_addr(uint32_t block) {
|
2016-10-13 17:53:31 -04:00
|
|
|
if (INTERNAL_FLASH_PART1_START_BLOCK <= block && block < INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS) {
|
2016-09-02 20:00:30 -04:00
|
|
|
// a block in partition 1
|
2016-10-13 17:53:31 -04:00
|
|
|
block -= INTERNAL_FLASH_PART1_START_BLOCK;
|
2017-05-12 17:47:39 -04:00
|
|
|
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
// bad block
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
bool internal_flash_read_block(uint8_t *dest, uint32_t block) {
|
2016-09-02 20:00:30 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
build_partition(dest + 446, 0, 0x01 /* FAT12 */, INTERNAL_FLASH_PART1_START_BLOCK, INTERNAL_FLASH_PART1_NUM_BLOCKS);
|
2016-09-02 20:00:30 -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;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// non-MBR block, get data from flash memory
|
2016-11-03 18:50:59 -04:00
|
|
|
int32_t src = convert_block_to_flash_addr(block);
|
2016-09-02 20:00:30 -04:00
|
|
|
if (src == -1) {
|
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-10 14:36:00 -04:00
|
|
|
int32_t error_code = flash_read(&internal_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
|
|
|
|
return error_code == ERR_NONE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
bool internal_flash_write_block(const uint8_t *src, uint32_t block) {
|
2016-09-02 20:00:30 -04:00
|
|
|
if (block == 0) {
|
|
|
|
// can't write MBR, but pretend we did
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
2016-10-25 18:06:05 -04:00
|
|
|
#ifdef MICROPY_HW_LED_MSC
|
|
|
|
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
|
|
|
|
#endif
|
2016-12-09 22:27:41 -05:00
|
|
|
temp_status_color(ACTIVE_WRITE);
|
2016-09-02 20:00:30 -04:00
|
|
|
// non-MBR block, copy to cache
|
2016-11-03 18:50:59 -04:00
|
|
|
int32_t dest = convert_block_to_flash_addr(block);
|
2016-09-02 20:00:30 -04:00
|
|
|
if (dest == -1) {
|
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-10 14:36:00 -04:00
|
|
|
int32_t error_code;
|
|
|
|
error_code = flash_erase(&internal_flash_desc,
|
|
|
|
dest,
|
|
|
|
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&internal_flash_desc));
|
|
|
|
if (error_code != ERR_NONE) {
|
2016-09-02 20:00:30 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
error_code = flash_append(&internal_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
|
|
|
|
if (error_code != ERR_NONE) {
|
|
|
|
return false;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
2016-12-09 22:27:41 -05:00
|
|
|
clear_temp_status();
|
2016-10-25 18:06:05 -04:00
|
|
|
#ifdef MICROPY_HW_LED_MSC
|
|
|
|
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
|
|
|
|
#endif
|
2016-09-02 20:00:30 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
mp_uint_t internal_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
2016-09-02 20:00:30 -04:00
|
|
|
for (size_t i = 0; i < num_blocks; i++) {
|
2017-05-12 17:47:39 -04:00
|
|
|
if (!internal_flash_read_block(dest + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
|
2016-09-02 20:00:30 -04:00
|
|
|
return 1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
mp_uint_t internal_flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
|
2016-09-02 20:00:30 -04:00
|
|
|
for (size_t i = 0; i < num_blocks; i++) {
|
2017-05-12 17:47:39 -04:00
|
|
|
if (!internal_flash_write_block(src + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
|
2016-09-02 20:00:30 -04:00
|
|
|
return 1; // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// MicroPython bindings
|
|
|
|
//
|
|
|
|
// Expose the flash as an object with the block protocol.
|
|
|
|
|
|
|
|
// there is a singleton Flash object
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC const mp_obj_base_t internal_flash_obj = {&internal_flash_type};
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t internal_flash_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2016-09-02 20:00:30 -04:00
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
|
|
|
|
// return singleton object
|
2016-10-13 17:53:31 -04:00
|
|
|
return (mp_obj_t)&internal_flash_obj;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t internal_flash_obj_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
2016-09-02 20:00:30 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
2017-05-12 17:47:39 -04:00
|
|
|
mp_uint_t ret = internal_flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
|
2016-09-02 20:00:30 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(ret);
|
|
|
|
}
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(internal_flash_obj_readblocks_obj, internal_flash_obj_readblocks);
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t internal_flash_obj_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
2016-09-02 20:00:30 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
2017-05-12 17:47:39 -04:00
|
|
|
mp_uint_t ret = internal_flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
|
2016-09-02 20:00:30 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(ret);
|
|
|
|
}
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(internal_flash_obj_writeblocks_obj, internal_flash_obj_writeblocks);
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t internal_flash_obj_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
|
2016-09-02 20:00:30 -04:00
|
|
|
mp_int_t cmd = mp_obj_get_int(cmd_in);
|
|
|
|
switch (cmd) {
|
2016-10-13 17:53:31 -04:00
|
|
|
case BP_IOCTL_INIT: internal_flash_init(); return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
case BP_IOCTL_DEINIT: internal_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0); // TODO properly
|
|
|
|
case BP_IOCTL_SYNC: internal_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(internal_flash_get_block_count());
|
|
|
|
case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(internal_flash_get_block_size());
|
2016-09-02 20:00:30 -04:00
|
|
|
default: return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(internal_flash_obj_ioctl_obj, internal_flash_obj_ioctl);
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-08-27 15:02:50 -04:00
|
|
|
STATIC const mp_rom_map_elem_t internal_flash_obj_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&internal_flash_obj_readblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&internal_flash_obj_writeblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&internal_flash_obj_ioctl_obj) },
|
2016-09-02 20:00:30 -04:00
|
|
|
};
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(internal_flash_obj_locals_dict, internal_flash_obj_locals_dict_table);
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
const mp_obj_type_t internal_flash_type = {
|
2016-09-02 20:00:30 -04:00
|
|
|
{ &mp_type_type },
|
2016-10-13 17:53:31 -04:00
|
|
|
.name = MP_QSTR_InternalFlash,
|
|
|
|
.make_new = internal_flash_obj_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&internal_flash_obj_locals_dict,
|
2016-09-02 20:00:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void flash_init_vfs(fs_user_mount_t *vfs) {
|
2017-08-07 18:56:10 -04:00
|
|
|
vfs->base.type = &mp_fat_vfs_type;
|
2017-05-12 21:26:14 -04:00
|
|
|
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
|
2017-08-07 18:56:10 -04:00
|
|
|
vfs->fatfs.drv = vfs;
|
|
|
|
vfs->fatfs.part = 1; // flash filesystem lives on first partition
|
2016-10-13 17:53:31 -04:00
|
|
|
vfs->readblocks[0] = (mp_obj_t)&internal_flash_obj_readblocks_obj;
|
|
|
|
vfs->readblocks[1] = (mp_obj_t)&internal_flash_obj;
|
|
|
|
vfs->readblocks[2] = (mp_obj_t)internal_flash_read_blocks; // native version
|
|
|
|
vfs->writeblocks[0] = (mp_obj_t)&internal_flash_obj_writeblocks_obj;
|
|
|
|
vfs->writeblocks[1] = (mp_obj_t)&internal_flash_obj;
|
|
|
|
vfs->writeblocks[2] = (mp_obj_t)internal_flash_write_blocks; // native version
|
|
|
|
vfs->u.ioctl[0] = (mp_obj_t)&internal_flash_obj_ioctl_obj;
|
|
|
|
vfs->u.ioctl[1] = (mp_obj_t)&internal_flash_obj;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|