Merge pull request #1338 from tannewt/fix_nrf_internal_flash

Fix nrf internal flash
This commit is contained in:
Dan Halbert 2018-11-14 22:54:23 -05:00 committed by GitHub
commit 7e2a3bf52e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 182 additions and 239 deletions

View File

@ -70,7 +70,7 @@ uint32_t supervisor_flash_get_block_size(void) {
}
uint32_t supervisor_flash_get_block_count(void) {
return INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS;
return INTERNAL_FLASH_PART1_NUM_BLOCKS;
}
void supervisor_flash_flush(void) {
@ -80,46 +80,9 @@ void flash_flush(void) {
supervisor_flash_flush();
}
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;
}
static int32_t convert_block_to_flash_addr(uint32_t block) {
if (INTERNAL_FLASH_PART1_START_BLOCK <= block && block < INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS) {
if (0 <= block && block < INTERNAL_FLASH_PART1_NUM_BLOCKS) {
// a block in partition 1
block -= INTERNAL_FLASH_PART1_START_BLOCK;
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
}
// bad block
@ -127,69 +90,44 @@ static int32_t convert_block_to_flash_addr(uint32_t block) {
}
bool supervisor_flash_read_block(uint8_t *dest, uint32_t 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;
}
build_partition(dest + 446, 0, 0x01 /* FAT12 */, INTERNAL_FLASH_PART1_START_BLOCK, INTERNAL_FLASH_PART1_NUM_BLOCKS);
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
int32_t src = convert_block_to_flash_addr(block);
if (src == -1) {
// bad block number
return false;
}
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
return error_code == ERR_NONE;
// non-MBR block, get data from flash memory
int32_t src = convert_block_to_flash_addr(block);
if (src == -1) {
// bad block number
return false;
}
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
return error_code == ERR_NONE;
}
bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
if (block == 0) {
// can't write MBR, but pretend we did
return true;
} else {
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
temp_status_color(ACTIVE_WRITE);
// non-MBR block, copy to cache
int32_t dest = convert_block_to_flash_addr(block);
if (dest == -1) {
// bad block number
return false;
}
int32_t error_code;
error_code = flash_erase(&supervisor_flash_desc,
dest,
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
if (error_code != ERR_NONE) {
return false;
}
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
if (error_code != ERR_NONE) {
return false;
}
clear_temp_status();
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
return true;
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
temp_status_color(ACTIVE_WRITE);
// non-MBR block, copy to cache
int32_t dest = convert_block_to_flash_addr(block);
if (dest == -1) {
// bad block number
return false;
}
int32_t error_code;
error_code = flash_erase(&supervisor_flash_desc,
dest,
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
if (error_code != ERR_NONE) {
return false;
}
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
if (error_code != ERR_NONE) {
return false;
}
clear_temp_status();
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
return true;
}
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {

View File

@ -41,7 +41,6 @@
#endif
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms

View File

@ -31,8 +31,6 @@
#include "py/mpconfig.h"
#define FLASH_ROOT_POINTERS
#define FLASH_PAGE_SIZE 0x1000
#define CIRCUITPY_INTERNAL_NVM_SIZE 0

View File

@ -40,8 +40,6 @@
#include "supervisor/memory.h"
#include "supervisor/shared/rgb_led_status.h"
#define SPI_FLASH_PART1_START_BLOCK (0x1)
#define NO_SECTOR_LOADED 0xFFFFFFFF
// The currently cached sector in the cache, ram or flash based.
@ -268,7 +266,7 @@ uint32_t supervisor_flash_get_block_size(void) {
uint32_t supervisor_flash_get_block_count(void) {
// We subtract one erase sector size because we may use it as a staging area
// for writes.
return SPI_FLASH_PART1_START_BLOCK + (flash_device->total_size - SPI_FLASH_ERASE_SIZE) / FILESYSTEM_BLOCK_SIZE;
return (flash_device->total_size - SPI_FLASH_ERASE_SIZE) / FILESYSTEM_BLOCK_SIZE;
}
// Flush the cache that was written to the scratch portion of flash. Only used
@ -444,48 +442,9 @@ void supervisor_flash_flush(void) {
spi_flash_flush_keep_cache(false);
}
// Builds a partition entry for the MBR.
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;
}
static int32_t convert_block_to_flash_addr(uint32_t block) {
if (SPI_FLASH_PART1_START_BLOCK <= block && block < supervisor_flash_get_block_count()) {
if (0 <= block && block < supervisor_flash_get_block_count()) {
// a block in partition 1
block -= SPI_FLASH_PART1_START_BLOCK;
return block * FILESYSTEM_BLOCK_SIZE;
}
// bad block
@ -493,107 +452,79 @@ static int32_t convert_block_to_flash_addr(uint32_t block) {
}
bool external_flash_read_block(uint8_t *dest, uint32_t 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;
}
build_partition(dest + 446, 0, 0x01 /* FAT12 */,
SPI_FLASH_PART1_START_BLOCK,
supervisor_flash_get_block_count() - SPI_FLASH_PART1_START_BLOCK);
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 if (block < SPI_FLASH_PART1_START_BLOCK) {
memset(dest, 0, FILESYSTEM_BLOCK_SIZE);
return true;
} else {
// Non-MBR block, get data from flash memory.
int32_t address = convert_block_to_flash_addr(block);
if (address == -1) {
// bad block number
return false;
}
// Mask out the lower bits that designate the address within the sector.
uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1));
uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE);
uint8_t mask = 1 << (block_index);
// We're reading from the currently cached sector.
if (current_sector == this_sector && (mask & dirty_mask) > 0) {
if (MP_STATE_VM(flash_ram_cache) != NULL) {
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
for (int i = 0; i < pages_per_block; i++) {
memcpy(dest + i * SPI_FLASH_PAGE_SIZE,
MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
SPI_FLASH_PAGE_SIZE);
}
return true;
} else {
uint32_t scratch_address = flash_device->total_size - SPI_FLASH_ERASE_SIZE + block_index * FILESYSTEM_BLOCK_SIZE;
return read_flash(scratch_address, dest, FILESYSTEM_BLOCK_SIZE);
}
}
return read_flash(address, dest, FILESYSTEM_BLOCK_SIZE);
int32_t address = convert_block_to_flash_addr(block);
if (address == -1) {
// bad block number
return false;
}
}
bool external_flash_write_block(const uint8_t *data, uint32_t block) {
if (block < SPI_FLASH_PART1_START_BLOCK) {
// Fake writing below the flash partition.
return true;
} else {
// Non-MBR block, copy to cache
int32_t address = convert_block_to_flash_addr(block);
if (address == -1) {
// bad block number
return false;
}
// Wait for any previous writes to finish.
wait_for_flash_ready();
// Mask out the lower bits that designate the address within the sector.
uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1));
uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE);
uint8_t mask = 1 << (block_index);
// Flush the cache if we're moving onto a sector or we're writing the
// same block again.
if (current_sector != this_sector || (mask & dirty_mask) > 0) {
// Check to see if we'd write to an erased page. In that case we
// can write directly.
if (page_erased(address)) {
return write_flash(address, data, FILESYSTEM_BLOCK_SIZE);
}
if (current_sector != NO_SECTOR_LOADED) {
spi_flash_flush_keep_cache(true);
}
if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) {
erase_sector(flash_device->total_size - SPI_FLASH_ERASE_SIZE);
wait_for_flash_ready();
}
current_sector = this_sector;
dirty_mask = 0;
}
dirty_mask |= mask;
// Copy the block to the appropriate cache.
// Mask out the lower bits that designate the address within the sector.
uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1));
uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE);
uint8_t mask = 1 << (block_index);
// We're reading from the currently cached sector.
if (current_sector == this_sector && (mask & dirty_mask) > 0) {
if (MP_STATE_VM(flash_ram_cache) != NULL) {
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
for (int i = 0; i < pages_per_block; i++) {
memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
data + i * SPI_FLASH_PAGE_SIZE,
memcpy(dest + i * SPI_FLASH_PAGE_SIZE,
MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
SPI_FLASH_PAGE_SIZE);
}
return true;
} else {
uint32_t scratch_address = flash_device->total_size - SPI_FLASH_ERASE_SIZE + block_index * FILESYSTEM_BLOCK_SIZE;
return write_flash(scratch_address, data, FILESYSTEM_BLOCK_SIZE);
return read_flash(scratch_address, dest, FILESYSTEM_BLOCK_SIZE);
}
}
return read_flash(address, dest, FILESYSTEM_BLOCK_SIZE);
}
bool external_flash_write_block(const uint8_t *data, uint32_t block) {
// Non-MBR block, copy to cache
int32_t address = convert_block_to_flash_addr(block);
if (address == -1) {
// bad block number
return false;
}
// Wait for any previous writes to finish.
wait_for_flash_ready();
// Mask out the lower bits that designate the address within the sector.
uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1));
uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE);
uint8_t mask = 1 << (block_index);
// Flush the cache if we're moving onto a sector or we're writing the
// same block again.
if (current_sector != this_sector || (mask & dirty_mask) > 0) {
// Check to see if we'd write to an erased page. In that case we
// can write directly.
if (page_erased(address)) {
return write_flash(address, data, FILESYSTEM_BLOCK_SIZE);
}
if (current_sector != NO_SECTOR_LOADED) {
spi_flash_flush_keep_cache(true);
}
if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) {
erase_sector(flash_device->total_size - SPI_FLASH_ERASE_SIZE);
wait_for_flash_ready();
}
current_sector = this_sector;
dirty_mask = 0;
}
dirty_mask |= mask;
// Copy the block to the appropriate cache.
if (MP_STATE_VM(flash_ram_cache) != NULL) {
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
for (int i = 0; i < pages_per_block; i++) {
memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
data + i * SPI_FLASH_PAGE_SIZE,
SPI_FLASH_PAGE_SIZE);
}
return true;
} else {
uint32_t scratch_address = flash_device->total_size - SPI_FLASH_ERASE_SIZE + block_index * FILESYSTEM_BLOCK_SIZE;
return write_flash(scratch_address, data, FILESYSTEM_BLOCK_SIZE);
}
}
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {

View File

@ -59,7 +59,6 @@ void filesystem_init(bool create_allowed, bool force_create) {
// Flush the new file system to make sure it's repaired immediately.
supervisor_flash_flush();
if (res != FR_OK) {
asm("bkpt");
return;
}
@ -75,7 +74,6 @@ void filesystem_init(bool create_allowed, bool force_create) {
// and ensure everything is flushed
supervisor_flash_flush();
} else if (res != FR_OK) {
asm("bkpt");
return;
}
mp_vfs_mount_t *vfs = &_mp_vfs;

View File

@ -31,6 +31,8 @@
#define VFS_INDEX 0
#define PART1_START_BLOCK (0x1)
void supervisor_flash_set_usb_writable(bool usb_writable) {
mp_vfs_mount_t* current_mount = MP_STATE_VM(vfs_mount_table);
for (uint8_t i = 0; current_mount != NULL; i++) {
@ -63,10 +65,87 @@ STATIC mp_obj_t supervisor_flash_obj_make_new(const mp_obj_type_t *type, size_t
return (mp_obj_t)&supervisor_flash_obj;
}
uint32_t flash_get_block_count(void) {
return PART1_START_BLOCK + supervisor_flash_get_block_count();
}
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;
}
mp_uint_t flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
if (block_num == 0) {
if (block_num > 1) {
return 1; // error
}
// fake the MBR so we can decide on our own partition table
for (int i = 0; i < 446; i++) {
dest[i] = 0;
}
build_partition(dest + 446, 0, 0x01 /* FAT12 */, PART1_START_BLOCK, supervisor_flash_get_block_count());
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 0; // ok
}
return supervisor_flash_read_blocks(dest, block_num - PART1_START_BLOCK, num_blocks);
}
mp_uint_t flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
if (block_num == 0) {
if (num_blocks > 1) {
return 1; // error
}
// can't write MBR, but pretend we did
return 0;
} else {
return supervisor_flash_write_blocks(src, block_num - PART1_START_BLOCK, num_blocks);
}
}
STATIC mp_obj_t supervisor_flash_obj_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 = supervisor_flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
mp_uint_t ret = flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
return MP_OBJ_NEW_SMALL_INT(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_readblocks_obj, supervisor_flash_obj_readblocks);
@ -74,7 +153,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_readblocks_obj, supervisor
STATIC mp_obj_t supervisor_flash_obj_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 = supervisor_flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
mp_uint_t ret = flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
return MP_OBJ_NEW_SMALL_INT(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_writeblocks_obj, supervisor_flash_obj_writeblocks);
@ -85,7 +164,7 @@ STATIC mp_obj_t supervisor_flash_obj_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_ob
case BP_IOCTL_INIT: supervisor_flash_init(); return MP_OBJ_NEW_SMALL_INT(0);
case BP_IOCTL_DEINIT: supervisor_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0); // TODO properly
case BP_IOCTL_SYNC: supervisor_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0);
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(supervisor_flash_get_block_count());
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(flash_get_block_count());
case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(supervisor_flash_get_block_size());
default: return mp_const_none;
}
@ -114,10 +193,10 @@ void supervisor_flash_init_vfs(fs_user_mount_t *vfs) {
vfs->fatfs.part = 1; // flash filesystem lives on first partition
vfs->readblocks[0] = (mp_obj_t)&supervisor_flash_obj_readblocks_obj;
vfs->readblocks[1] = (mp_obj_t)&supervisor_flash_obj;
vfs->readblocks[2] = (mp_obj_t)supervisor_flash_read_blocks; // native version
vfs->readblocks[2] = (mp_obj_t)flash_read_blocks; // native version
vfs->writeblocks[0] = (mp_obj_t)&supervisor_flash_obj_writeblocks_obj;
vfs->writeblocks[1] = (mp_obj_t)&supervisor_flash_obj;
vfs->writeblocks[2] = (mp_obj_t)supervisor_flash_write_blocks; // native version
vfs->writeblocks[2] = (mp_obj_t)flash_write_blocks; // native version
vfs->u.ioctl[0] = (mp_obj_t)&supervisor_flash_obj_ioctl_obj;
vfs->u.ioctl[1] = (mp_obj_t)&supervisor_flash_obj;
}