Support all boards and remove erase and page sizes because they never change.

This commit is contained in:
Scott Shawcroft 2018-03-01 12:45:12 -08:00
parent 2ab923862b
commit 01aceaae50
12 changed files with 157 additions and 94 deletions

View File

@ -47,9 +47,14 @@
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#define EXTERNAL_FLASH_DEVICES 25FL216K, \ #include "external_flash/devices.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 2
#define EXTERNAL_FLASH_DEVICES S25FL216K, \
GD25Q16C GD25Q16C
#include "external_flash/external_flash.h"
#define CALIBRATE_CRYSTALLESS 1 #define CALIBRATE_CRYSTALLESS 1
// Explanation of how a user got into safe mode. // Explanation of how a user got into safe mode.

View File

@ -40,5 +40,10 @@
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices/S25FL216K.h" #include "external_flash/devices.h"
#include "external_flash/devices/GD25Q16C.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 2
#define EXTERNAL_FLASH_DEVICES S25FL216K, \
GD25Q16C
#include "external_flash/external_flash.h"

View File

@ -42,4 +42,9 @@
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices/S25FL064L.h" #include "external_flash/devices.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 1
#define EXTERNAL_FLASH_DEVICES S25FL064L
#include "external_flash/external_flash.h"

View File

@ -33,13 +33,16 @@
#define MICROPY_PORT_B (PORT_PB22 | PORT_PB23 | PORT_PB03 ) #define MICROPY_PORT_B (PORT_PB22 | PORT_PB23 | PORT_PB03 )
#define MICROPY_PORT_C (0) #define MICROPY_PORT_C (0)
#include "external_flash/external_flash.h"
// If you change this, then make sure to update the linker scripts as well to // If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code. // make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256 // #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices/GD25Q16C.h" #include "external_flash/devices.h"
#include "external_flash/devices/W25Q16FW.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 2
#define EXTERNAL_FLASH_DEVICES W25Q16FW, \
GD25Q16C
#include "external_flash/external_flash.h"

View File

@ -34,8 +34,6 @@
#define MICROPY_PORT_B (PORT_PB03 | PORT_PB22 | PORT_PB23) #define MICROPY_PORT_B (PORT_PB03 | PORT_PB22 | PORT_PB23)
#define MICROPY_PORT_C (0) #define MICROPY_PORT_C (0)
#include "external_flash/external_flash.h"
// If you change this, then make sure to update the linker scripts as well to // If you change this, then make sure to update the linker scripts as well to
// make sure you don't overwrite code. // make sure you don't overwrite code.
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256 // #define CIRCUITPY_INTERNAL_NVM_SIZE 256
@ -43,5 +41,10 @@
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices/S25FL216K.h" #include "external_flash/devices.h"
#include "external_flash/devices/GD25Q16C.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 2
#define EXTERNAL_FLASH_DEVICES S25FL216K, \
GD25Q16C
#include "external_flash/external_flash.h"

View File

@ -29,7 +29,7 @@
#include "external_flash/devices.h" #include "external_flash/devices.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 2 #define EXTERNAL_FLASH_DEVICE_COUNT 3
#define EXTERNAL_FLASH_DEVICES S25FL116K, GD25Q16C #define EXTERNAL_FLASH_DEVICES S25FL116K, S25FL216K, GD25Q16C
#include "external_flash/external_flash.h" #include "external_flash/external_flash.h"

View File

@ -54,5 +54,10 @@
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices/S25FL216K.h" #include "external_flash/devices.h"
#include "external_flash/devices/GD25Q16C.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 2
#define EXTERNAL_FLASH_DEVICES S25FL216K, \
GD25Q16C
#include "external_flash/external_flash.h"

View File

@ -48,4 +48,9 @@
#define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "external_flash/devices/W25Q32BV.h" #include "external_flash/devices.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 1
#define EXTERNAL_FLASH_DEVICES W25Q32BV
#include "external_flash/external_flash.h"

View File

@ -41,5 +41,9 @@
#define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
//#include "external_flash/devices/W25Q32BV.h" #include "external_flash/devices.h"
#include "external_flash/devices/S25FL216K.h"
#define EXTERNAL_FLASH_DEVICE_COUNT 1
#define EXTERNAL_FLASH_DEVICES S25FL216K
#include "external_flash/external_flash.h"

View File

@ -31,16 +31,29 @@
typedef struct { typedef struct {
uint32_t total_size; uint32_t total_size;
uint16_t erase_size;
uint16_t page_size;
uint16_t start_up_time_us; uint16_t start_up_time_us;
// Three response bytes to 0x9f JEDEC ID command.
uint8_t manufacturer_id; uint8_t manufacturer_id;
uint8_t memory_type; uint8_t memory_type;
uint8_t capacity; uint8_t capacity;
// Max clock speed for all operations and the fastest read mode.
uint8_t max_clock_speed_mhz; uint8_t max_clock_speed_mhz;
bool has_sector_protection; bool has_sector_protection : 1;
bool supports_qspi;
bool supports_qspi_writes; // Supports the 0x0b fast read command with 8 dummy cycles.
bool supports_fast_read : 1;
// Supports the fast read, quad output command 0x6b with 8 dummy cycles.
bool supports_qspi : 1;
// Requires quad enable set in status bit 9.
bool has_quad_enable : 1;
// Supports the quad input page program command 0x32. This is known as 1-1-4 because it only
// uses all four lines for data.
bool supports_qspi_writes: 1;
} external_flash_device; } external_flash_device;
// Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. Its on the SAMD21 // Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. Its on the SAMD21
@ -48,116 +61,127 @@ typedef struct {
// Datasheet: https://www.adestotech.com/wp-content/uploads/doc8715.pdf // Datasheet: https://www.adestotech.com/wp-content/uploads/doc8715.pdf
#define AT25DF081A {\ #define AT25DF081A {\
.total_size = (1 << 20), /* 1 MiB */ \ .total_size = (1 << 20), /* 1 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 10000, \ .start_up_time_us = 10000, \
.manufacturer_id = 0x1f, \ .manufacturer_id = 0x1f, \
.memory_type = 0x45, \ .memory_type = 0x45, \
.capacity = 0x01, \ .capacity = 0x01, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 85, \
.has_sector_protection = true, \ .has_sector_protection = true, \
.supports_qspi = true, \ .supports_fast_read = true, \
.supports_qspi = false, \
.has_quad_enable = false, \
.supports_qspi_writes = false, \ .supports_qspi_writes = false, \
} }
// Settings for the Gigadevice GD25Q16C 2MiB SPI flash. // Settings for the Gigadevice GD25Q16C 2MiB SPI flash.
// Datasheet: http://www.gigadevice.com/wp-content/uploads/2017/12/DS-00086-GD25Q16C-Rev2.6.pdf // Datasheet: http://www.gigadevice.com/wp-content/uploads/2017/12/DS-00086-GD25Q16C-Rev2.6.pdf
#define GD25Q16C {\ #define GD25Q16C {\
.total_size = (1 << 21), /* 2 MiB */ \ .total_size = (1 << 21), /* 2 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 5000, \ .start_up_time_us = 5000, \
.manufacturer_id = 0xc8, \ .manufacturer_id = 0xc8, \
.memory_type = 0x40, \ .memory_type = 0x40, \
.capacity = 0x15, \ .capacity = 0x15, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 104, /* if we need 120 then we can turn on high performance mode */ \
.has_sector_protection = true, \ .has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \ .supports_qspi = true, \
.supports_qspi_writes = false, \ .has_quad_enable = true, \
.supports_qspi_writes = true, \
} }
// Settings for the Cypress (was Spansion) S25FL064L 8MiB SPI flash. // Settings for the Cypress (was Spansion) S25FL064L 8MiB SPI flash.
// Datasheet: http://www.cypress.com/file/316661/download // Datasheet: http://www.cypress.com/file/316661/download
#define S25FL064L {\ #define S25FL064L {\
.total_size = (1 << 23), /* 8 MiB */ \ .total_size = (1 << 23), /* 8 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 300, \ .start_up_time_us = 300, \
.manufacturer_id = 0x01, \ .manufacturer_id = 0x01, \
.memory_type = 0x60, \ .memory_type = 0x60, \
.capacity = 0x17, \ .capacity = 0x17, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 108, \
.has_sector_protection = true, \ .has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \ .supports_qspi = true, \
.supports_qspi_writes = false, \ .has_quad_enable = true, \
.supports_qspi_writes = true, \
} }
// Settings for the Cypress (was Spansion) S25FL116K 2MiB SPI flash. // Settings for the Cypress (was Spansion) S25FL116K 2MiB SPI flash.
// Datasheet: http://www.cypress.com/file/196886/download // Datasheet: http://www.cypress.com/file/196886/download
#define S25FL116K {\ #define S25FL116K {\
.total_size = (1 << 21), /* 2 MiB */ \ .total_size = (1 << 21), /* 2 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 10000, \ .start_up_time_us = 10000, \
.manufacturer_id = 0x01, \ .manufacturer_id = 0x01, \
.memory_type = 0x40, \ .memory_type = 0x40, \
.capacity = 0x15, \ .capacity = 0x15, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 108, \
.has_sector_protection = true, \ .has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \ .supports_qspi = true, \
.has_quad_enable = true, \
.supports_qspi_writes = false, \ .supports_qspi_writes = false, \
} }
// Settings for the Cypress (was Spansion) S25FL216K 2MiB SPI flash. // Settings for the Cypress (was Spansion) S25FL216K 2MiB SPI flash.
// Datasheet: http://www.cypress.com/file/197346/download // Datasheet: http://www.cypress.com/file/197346/download
#define S25FL216K {\ #define S25FL216K {\
.total_size = (1 << 21), /* 2 MiB */ \ .total_size = (1 << 21), /* 2 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 10000, \ .start_up_time_us = 10000, \
.manufacturer_id = 0x01, \ .manufacturer_id = 0x01, \
.memory_type = 0x40, \ .memory_type = 0x40, \
.capacity = 0x15, \ .capacity = 0x15, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 65, \
.has_sector_protection = false, \ .has_sector_protection = false, \
.supports_qspi = true, \ .supports_fast_read = true, \
.supports_qspi = false, \
.has_quad_enable = false, \
.supports_qspi_writes = false, \ .supports_qspi_writes = false, \
} }
// Settings for the Winbond W25Q16FW 2MiB SPI flash. // Settings for the Winbond W25Q16FW 2MiB SPI flash.
// Datasheet: https://www.winbond.com/resource-files/w25q16fw%20revj%2005182017%20sfdp.pdf // Datasheet: https://www.winbond.com/resource-files/w25q16fw%20revj%2005182017%20sfdp.pdf
#define W25Q16FW {\ #define W25Q16FW {\
.total_size = (1 << 21), /* 2 MiB */ \ .total_size = (1 << 21), /* 2 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 5000, \ .start_up_time_us = 5000, \
.manufacturer_id = 0xef, \ .manufacturer_id = 0xef, \
.memory_type = 0x60, \ .memory_type = 0x60, \
.capacity = 0x15, \ .capacity = 0x15, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 133, \
.has_sector_protection = false, \ .has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \ .supports_qspi = true, \
.supports_qspi_writes = false, \ .has_quad_enable = true, \
.supports_qspi_writes = true, \
}
// Settings for the Winbond W25Q16JV 2MiB SPI flash.
// Datasheet: https://www.winbond.com/resource-files/w25q16jv%20spi%20revf%2005092017.pdf
#define W25Q16JV {\
.total_size = (1 << 21), /* 2 MiB */ \
.start_up_time_us = 5000, \
.manufacturer_id = 0xef, \
.memory_type = 0x40, \
.capacity = 0x15, \
.max_clock_speed_mhz = 133, \
.has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \
.has_quad_enable = true, \
.supports_qspi_writes = true, \
} }
// Settings for the Winbond W25Q32BV 2MiB SPI flash. // Settings for the Winbond W25Q32BV 2MiB SPI flash.
// Datasheet: https://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf // Datasheet: https://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf
#define W25Q32BV {\ #define W25Q32BV {\
.total_size = (1 << 21), /* 2 MiB */ \ .total_size = (1 << 22), /* 4 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \ .start_up_time_us = 10000, \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 5000, \
.manufacturer_id = 0xef, \ .manufacturer_id = 0xef, \
.memory_type = 0x60, \ .memory_type = 0x60, \
.capacity = 0x16, \ .capacity = 0x16, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 104, \
.has_sector_protection = false, \ .has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \ .supports_qspi = true, \
.has_quad_enable = true, \
.supports_qspi_writes = false, \ .supports_qspi_writes = false, \
} }
@ -165,15 +189,15 @@ typedef struct {
// Datasheet: https://www.winbond.com/resource-files/w25q80dv%20dl_revh_10022015.pdf // Datasheet: https://www.winbond.com/resource-files/w25q80dv%20dl_revh_10022015.pdf
#define W25Q80DL {\ #define W25Q80DL {\
.total_size = (1 << 20), /* 1 MiB */ \ .total_size = (1 << 20), /* 1 MiB */ \
.erase_size = (1 << 12), /* 4 KiB */ \
.page_size = 256, /*256 bytes */ \
.start_up_time_us = 5000, \ .start_up_time_us = 5000, \
.manufacturer_id = 0xef, \ .manufacturer_id = 0xef, \
.memory_type = 0x60, \ .memory_type = 0x60, \
.capacity = 0x14, \ .capacity = 0x14, \
.max_clock_speed_mhz = 104, \ .max_clock_speed_mhz = 104, \
.has_sector_protection = false, \ .has_sector_protection = false, \
.supports_fast_read = true, \
.supports_qspi = true, \ .supports_qspi = true, \
.has_quad_enable = true, \
.supports_qspi_writes = false, \ .supports_qspi_writes = false, \
} }

View File

@ -54,9 +54,9 @@ struct spi_m_sync_descriptor spi_flash_desc;
// The currently cached sector in the cache, ram or flash based. // The currently cached sector in the cache, ram or flash based.
static uint32_t current_sector; static uint32_t current_sector;
external_flash_device possible_devices[EXTERNAL_FLASH_DEVICE_COUNT] = {EXTERNAL_FLASH_DEVICES}; const external_flash_device possible_devices[EXTERNAL_FLASH_DEVICE_COUNT] = {EXTERNAL_FLASH_DEVICES};
static external_flash_device* flash_device = NULL; static const external_flash_device* flash_device = NULL;
// Track which blocks (up to 32) in the current sector currently live in the // Track which blocks (up to 32) in the current sector currently live in the
// cache. // cache.
@ -111,13 +111,13 @@ static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_len
for (uint32_t bytes_written = 0; for (uint32_t bytes_written = 0;
bytes_written < data_length; bytes_written < data_length;
bytes_written += flash_device->page_size) { bytes_written += SPI_FLASH_PAGE_SIZE) {
if (!wait_for_flash_ready() || !write_enable()) { if (!wait_for_flash_ready() || !write_enable()) {
return false; return false;
} }
if (!spi_flash_write_data(address + bytes_written, (uint8_t*) data + bytes_written, if (!spi_flash_write_data(address + bytes_written, (uint8_t*) data + bytes_written,
flash_device->page_size)) { SPI_FLASH_PAGE_SIZE)) {
return false; return false;
} }
} }
@ -168,7 +168,7 @@ static bool erase_sector(uint32_t sector_address) {
// Sector is really 24 bits. // Sector is really 24 bits.
static bool copy_block(uint32_t src_address, uint32_t dest_address) { static bool copy_block(uint32_t src_address, uint32_t dest_address) {
// Copy page by page to minimize RAM buffer. // Copy page by page to minimize RAM buffer.
uint16_t page_size = flash_device->page_size; uint16_t page_size = SPI_FLASH_PAGE_SIZE;
uint8_t buffer[page_size]; uint8_t buffer[page_size];
for (uint32_t i = 0; i < FILESYSTEM_BLOCK_SIZE / page_size; i++) { for (uint32_t i = 0; i < FILESYSTEM_BLOCK_SIZE / page_size; i++) {
if (!read_flash(src_address + i * page_size, buffer, page_size)) { if (!read_flash(src_address + i * page_size, buffer, page_size)) {
@ -201,7 +201,7 @@ void external_flash_init(void) {
for (uint8_t i = 0; i < num_possible_devices; i++) { for (uint8_t i = 0; i < num_possible_devices; i++) {
external_flash_device* possible_device = &possible_devices[i]; const external_flash_device* possible_device = &possible_devices[i];
uint8_t jedec_id_response[3] = {0x00, 0x00, 0x00}; uint8_t jedec_id_response[3] = {0x00, 0x00, 0x00};
spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3); spi_flash_read_command(CMD_READ_JEDEC_ID, jedec_id_response, 3);
if (jedec_id_response[0] == possible_device->manufacturer_id && if (jedec_id_response[0] == possible_device->manufacturer_id &&
@ -252,7 +252,7 @@ uint32_t external_flash_get_block_size(void) {
uint32_t external_flash_get_block_count(void) { uint32_t external_flash_get_block_count(void) {
// We subtract one erase sector size because we may use it as a staging area // We subtract one erase sector size because we may use it as a staging area
// for writes. // for writes.
return SPI_FLASH_PART1_START_BLOCK + (flash_device->total_size - flash_device->erase_size) / FILESYSTEM_BLOCK_SIZE; return SPI_FLASH_PART1_START_BLOCK + (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 // Flush the cache that was written to the scratch portion of flash. Only used
@ -261,8 +261,8 @@ static bool flush_scratch_flash(void) {
// First, copy out any blocks that we haven't touched from the sector we've // First, copy out any blocks that we haven't touched from the sector we've
// cached. // cached.
bool copy_to_scratch_ok = true; bool copy_to_scratch_ok = true;
uint32_t scratch_sector = flash_device->total_size - flash_device->erase_size; uint32_t scratch_sector = flash_device->total_size - SPI_FLASH_ERASE_SIZE;
for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
if ((dirty_mask & (1 << i)) == 0) { if ((dirty_mask & (1 << i)) == 0) {
copy_to_scratch_ok = copy_to_scratch_ok && copy_to_scratch_ok = copy_to_scratch_ok &&
copy_block(current_sector + i * FILESYSTEM_BLOCK_SIZE, copy_block(current_sector + i * FILESYSTEM_BLOCK_SIZE,
@ -277,7 +277,7 @@ static bool flush_scratch_flash(void) {
// Second, erase the current sector. // Second, erase the current sector.
erase_sector(current_sector); erase_sector(current_sector);
// Finally, copy the new version into it. // Finally, copy the new version into it.
for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
copy_block(scratch_sector + i * FILESYSTEM_BLOCK_SIZE, copy_block(scratch_sector + i * FILESYSTEM_BLOCK_SIZE,
current_sector + i * FILESYSTEM_BLOCK_SIZE); current_sector + i * FILESYSTEM_BLOCK_SIZE);
} }
@ -288,8 +288,8 @@ static bool flush_scratch_flash(void) {
// ram. Each page is allocated separately so that the GC doesn't need to provide // ram. Each page is allocated separately so that the GC doesn't need to provide
// one huge block. We can free it as we write if we want to also. // one huge block. We can free it as we write if we want to also.
static bool allocate_ram_cache(void) { static bool allocate_ram_cache(void) {
uint8_t blocks_per_sector = flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE;
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false); MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
if (MP_STATE_VM(flash_ram_cache) == NULL) { if (MP_STATE_VM(flash_ram_cache) == NULL) {
return false; return false;
@ -301,7 +301,7 @@ static bool allocate_ram_cache(void) {
bool success = true; bool success = true;
for (i = 0; i < blocks_per_sector; i++) { for (i = 0; i < blocks_per_sector; i++) {
for (j = 0; j < pages_per_block; j++) { for (j = 0; j < pages_per_block; j++) {
uint8_t *page_cache = m_malloc_maybe(flash_device->page_size, false); uint8_t *page_cache = m_malloc_maybe(SPI_FLASH_PAGE_SIZE, false);
if (page_cache == NULL) { if (page_cache == NULL) {
success = false; success = false;
break; break;
@ -336,14 +336,14 @@ static bool flush_ram_cache(bool keep_cache) {
// we've cached. If we don't do this we'll erase the data during the sector // we've cached. If we don't do this we'll erase the data during the sector
// erase below. // erase below.
bool copy_to_ram_ok = true; bool copy_to_ram_ok = true;
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
if ((dirty_mask & (1 << i)) == 0) { if ((dirty_mask & (1 << i)) == 0) {
for (uint8_t j = 0; j < pages_per_block; j++) { for (uint8_t j = 0; j < pages_per_block; j++) {
copy_to_ram_ok = read_flash( copy_to_ram_ok = read_flash(
current_sector + (i * pages_per_block + j) * flash_device->page_size, current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE,
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j], MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
flash_device->page_size); SPI_FLASH_PAGE_SIZE);
if (!copy_to_ram_ok) { if (!copy_to_ram_ok) {
break; break;
} }
@ -360,11 +360,11 @@ static bool flush_ram_cache(bool keep_cache) {
// Second, erase the current sector. // Second, erase the current sector.
erase_sector(current_sector); erase_sector(current_sector);
// Lastly, write all the data in ram that we've cached. // Lastly, write all the data in ram that we've cached.
for (uint8_t i = 0; i < flash_device->erase_size / FILESYSTEM_BLOCK_SIZE; i++) { for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
for (uint8_t j = 0; j < pages_per_block; j++) { for (uint8_t j = 0; j < pages_per_block; j++) {
write_flash(current_sector + (i * pages_per_block + j) * flash_device->page_size, write_flash(current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE,
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j], MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
flash_device->page_size); SPI_FLASH_PAGE_SIZE);
if (!keep_cache) { if (!keep_cache) {
m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]); m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
} }
@ -488,21 +488,21 @@ bool external_flash_read_block(uint8_t *dest, uint32_t block) {
} }
// Mask out the lower bits that designate the address within the sector. // Mask out the lower bits that designate the address within the sector.
uint32_t this_sector = address & (~(flash_device->erase_size - 1)); uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1));
uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (flash_device->erase_size / FILESYSTEM_BLOCK_SIZE); uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE);
uint8_t mask = 1 << (block_index); uint8_t mask = 1 << (block_index);
// We're reading from the currently cached sector. // We're reading from the currently cached sector.
if (current_sector == this_sector && (mask & dirty_mask) > 0) { if (current_sector == this_sector && (mask & dirty_mask) > 0) {
if (MP_STATE_VM(flash_ram_cache) != NULL) { if (MP_STATE_VM(flash_ram_cache) != NULL) {
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
for (int i = 0; i < pages_per_block; i++) { for (int i = 0; i < pages_per_block; i++) {
memcpy(dest + i * flash_device->page_size, memcpy(dest + i * SPI_FLASH_PAGE_SIZE,
MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i], MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
flash_device->page_size); SPI_FLASH_PAGE_SIZE);
} }
return true; return true;
} else { } else {
uint32_t scratch_address = flash_device->total_size - flash_device->erase_size + block_index * FILESYSTEM_BLOCK_SIZE; 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(scratch_address, dest, FILESYSTEM_BLOCK_SIZE);
} }
} }
@ -524,8 +524,8 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) {
// Wait for any previous writes to finish. // Wait for any previous writes to finish.
wait_for_flash_ready(); wait_for_flash_ready();
// Mask out the lower bits that designate the address within the sector. // Mask out the lower bits that designate the address within the sector.
uint32_t this_sector = address & (~(flash_device->erase_size - 1)); uint32_t this_sector = address & (~(SPI_FLASH_ERASE_SIZE - 1));
uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (flash_device->erase_size / FILESYSTEM_BLOCK_SIZE); uint8_t block_index = (address / FILESYSTEM_BLOCK_SIZE) % (SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE);
uint8_t mask = 1 << (block_index); uint8_t mask = 1 << (block_index);
// Flush the cache if we're moving onto a sector or we're writing the // Flush the cache if we're moving onto a sector or we're writing the
// same block again. // same block again.
@ -539,7 +539,7 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) {
spi_flash_flush_keep_cache(true); spi_flash_flush_keep_cache(true);
} }
if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) { if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) {
erase_sector(flash_device->total_size - flash_device->erase_size); erase_sector(flash_device->total_size - SPI_FLASH_ERASE_SIZE);
wait_for_flash_ready(); wait_for_flash_ready();
} }
current_sector = this_sector; current_sector = this_sector;
@ -548,15 +548,15 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) {
dirty_mask |= mask; dirty_mask |= mask;
// Copy the block to the appropriate cache. // Copy the block to the appropriate cache.
if (MP_STATE_VM(flash_ram_cache) != NULL) { if (MP_STATE_VM(flash_ram_cache) != NULL) {
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / flash_device->page_size; uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
for (int i = 0; i < pages_per_block; i++) { for (int i = 0; i < pages_per_block; i++) {
memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i], memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
data + i * flash_device->page_size, data + i * SPI_FLASH_PAGE_SIZE,
flash_device->page_size); SPI_FLASH_PAGE_SIZE);
} }
return true; return true;
} else { } else {
uint32_t scratch_address = flash_device->total_size - flash_device->erase_size + block_index * FILESYSTEM_BLOCK_SIZE; 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 write_flash(scratch_address, data, FILESYSTEM_BLOCK_SIZE);
} }
} }

View File

@ -37,6 +37,10 @@
// Erase sector size. // Erase sector size.
#define SPI_FLASH_SECTOR_SIZE (0x1000 - 100) #define SPI_FLASH_SECTOR_SIZE (0x1000 - 100)
// These are common across all NOR Flash.
#define SPI_FLASH_ERASE_SIZE (1 << 12)
#define SPI_FLASH_PAGE_SIZE (256)
#define SPI_FLASH_SYSTICK_MASK (0x1ff) // 512ms #define SPI_FLASH_SYSTICK_MASK (0x1ff) // 512ms
#define SPI_FLASH_IDLE_TICK(tick) (((tick) & SPI_FLASH_SYSTICK_MASK) == 2) #define SPI_FLASH_IDLE_TICK(tick) (((tick) & SPI_FLASH_SYSTICK_MASK) == 2)