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)
|
|
|
|
*
|
2017-05-02 18:25:06 -04:00
|
|
|
* Copyright (c) 2016, 2017 Scott Shawcroft for Adafruit Industries
|
2016-09-02 20:00:30 -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.
|
|
|
|
*/
|
2016-10-28 23:16:39 -04:00
|
|
|
#include "spi_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"
|
2018-01-23 18:58:05 -05:00
|
|
|
#include "py/misc.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"
|
2017-11-22 23:07:21 -05:00
|
|
|
#include "peripherals.h"
|
|
|
|
#include "supervisor/shared/rgb_led_status.h"
|
|
|
|
|
|
|
|
//#include "shared_dma.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
#include "hal_gpio.h"
|
|
|
|
#include "hal_spi_m_sync.h"
|
2016-10-13 17:53:31 -04:00
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
#define SPI_FLASH_PART1_START_BLOCK (0x1)
|
2016-10-14 15:22:06 -04:00
|
|
|
|
|
|
|
#define NO_SECTOR_LOADED 0xFFFFFFFF
|
|
|
|
|
|
|
|
#define CMD_READ_JEDEC_ID 0x9f
|
|
|
|
#define CMD_READ_DATA 0x03
|
|
|
|
#define CMD_SECTOR_ERASE 0x20
|
|
|
|
// #define CMD_SECTOR_ERASE CMD_READ_JEDEC_ID
|
2017-05-02 18:25:06 -04:00
|
|
|
#define CMD_DISABLE_WRITE 0x04
|
2016-10-14 15:22:06 -04:00
|
|
|
#define CMD_ENABLE_WRITE 0x06
|
|
|
|
#define CMD_PAGE_PROGRAM 0x02
|
|
|
|
// #define CMD_PAGE_PROGRAM CMD_READ_JEDEC_ID
|
|
|
|
#define CMD_READ_STATUS 0x05
|
2017-05-12 17:47:39 -04:00
|
|
|
#define CMD_WRITE_STATUS_BYTE1 0x01
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
static bool spi_flash_is_initialised = false;
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
struct spi_m_sync_descriptor spi_flash_desc;
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// The currently cached sector in the cache, ram or flash based.
|
2016-10-14 15:22:06 -04:00
|
|
|
static uint32_t current_sector;
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Track which blocks (up to 32) in the current sector currently live in the
|
|
|
|
// cache.
|
|
|
|
static uint32_t dirty_mask;
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Address of the scratch flash sector.
|
2017-05-12 17:47:39 -04:00
|
|
|
#define SCRATCH_SECTOR (SPI_FLASH_TOTAL_SIZE - SPI_FLASH_ERASE_SIZE)
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Enable the flash over SPI.
|
2016-11-03 18:50:59 -04:00
|
|
|
static void flash_enable(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_level(SPI_FLASH_CS_PIN, false);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Disable the flash over SPI.
|
2016-11-03 18:50:59 -04:00
|
|
|
static void flash_disable(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_level(SPI_FLASH_CS_PIN, true);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Wait until both the write enable and write in progress bits have cleared.
|
2016-11-03 18:50:59 -04:00
|
|
|
static bool wait_for_flash_ready(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
uint8_t read_status_request[2] = {CMD_READ_STATUS, 0x00};
|
|
|
|
uint8_t read_status_response[2] = {0x00, 0x00};
|
|
|
|
struct spi_xfer read_status_xfer = {read_status_request, read_status_response, 2};
|
|
|
|
int32_t status;
|
2016-10-18 17:56:17 -04:00
|
|
|
// Both the write enable and write in progress bits should be low.
|
2017-11-22 23:07:21 -05:00
|
|
|
do {
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_enable();
|
2017-11-22 23:07:21 -05:00
|
|
|
status = spi_m_sync_transfer(&spi_flash_desc, &read_status_xfer);
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_disable();
|
2017-11-22 23:07:21 -05:00
|
|
|
} while (status >= 0 && (read_status_response[1] & 0x3) != 0);
|
|
|
|
return status >= 0; // status is number of chars read or a negative error code.
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Turn on the write enable bit so we can program and erase the flash.
|
2016-11-03 18:50:59 -04:00
|
|
|
static bool write_enable(void) {
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_enable();
|
2017-11-22 23:07:21 -05:00
|
|
|
uint8_t enable_write_request[1] = {CMD_ENABLE_WRITE};
|
|
|
|
struct spi_xfer enable_write_xfer = {enable_write_request, 0, 1};
|
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &enable_write_xfer);
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_disable();
|
2017-11-22 23:07:21 -05:00
|
|
|
return status >= 0; // status is number of chars read or a negative error code.
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Pack the low 24 bits of the address into a uint8_t array.
|
2016-10-14 15:22:06 -04:00
|
|
|
static void address_to_bytes(uint32_t address, uint8_t* bytes) {
|
|
|
|
bytes[0] = (address >> 16) & 0xff;
|
|
|
|
bytes[1] = (address >> 8) & 0xff;
|
|
|
|
bytes[2] = address & 0xff;
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Read data_length's worth of bytes starting at address into data.
|
2016-10-14 15:22:06 -04:00
|
|
|
static bool read_flash(uint32_t address, uint8_t* data, uint32_t data_length) {
|
2017-05-12 17:47:39 -04:00
|
|
|
if (!spi_flash_is_initialised) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!wait_for_flash_ready()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-14 15:22:06 -04:00
|
|
|
// We can read as much as we want sequentially.
|
2017-11-22 23:07:21 -05:00
|
|
|
uint8_t read_data_request[4] = {CMD_READ_DATA, 0x00, 0x00, 0x00};
|
|
|
|
struct spi_xfer read_data_xfer = {read_data_request, 0, 4};
|
|
|
|
// Write the SPI flash read address into the bytes following the command byte.
|
|
|
|
address_to_bytes(address, read_data_request + 1);
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_enable();
|
2017-11-22 23:07:21 -05:00
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &read_data_xfer);
|
|
|
|
struct spi_xfer read_data_buffer_xfer = {0, data, data_length};
|
|
|
|
if (status >= 0) {
|
|
|
|
status = spi_m_sync_transfer(&spi_flash_desc, &read_data_buffer_xfer);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
flash_disable();
|
2017-11-22 23:07:21 -05:00
|
|
|
return status >= 0;
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Writes data_length's worth of bytes starting at address from data. Assumes
|
|
|
|
// that the sector that address resides in has already been erased. So make sure
|
|
|
|
// to run erase_sector.
|
2016-10-14 15:22:06 -04:00
|
|
|
static bool write_flash(uint32_t address, const uint8_t* data, uint32_t data_length) {
|
2017-05-12 17:47:39 -04:00
|
|
|
if (!spi_flash_is_initialised) {
|
2016-10-14 15:22:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-24 13:43:32 -04:00
|
|
|
// Don't bother writing if the data is all 1s. Thats equivalent to the flash
|
|
|
|
// state after an erase.
|
|
|
|
bool all_ones = true;
|
|
|
|
for (uint16_t i = 0; i < data_length; i++) {
|
|
|
|
if (data[i] != 0xff) {
|
|
|
|
all_ones = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all_ones) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:22:06 -04:00
|
|
|
for (uint32_t bytes_written = 0;
|
|
|
|
bytes_written < data_length;
|
2017-05-12 17:47:39 -04:00
|
|
|
bytes_written += SPI_FLASH_PAGE_SIZE) {
|
2016-10-14 15:22:06 -04:00
|
|
|
if (!wait_for_flash_ready() || !write_enable()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 23:07:21 -05:00
|
|
|
int32_t status;
|
2017-05-12 17:47:39 -04:00
|
|
|
|
2017-05-24 13:43:32 -04:00
|
|
|
#ifdef SPI_FLASH_SECTOR_PROTECTION
|
2017-05-12 17:47:39 -04:00
|
|
|
// Print out the protection status.
|
2017-05-24 13:43:32 -04:00
|
|
|
// uint8_t protect_check[5] = {0x3C, 0x00, 0x00, 0x00, 0x00};
|
|
|
|
// address_to_bytes(address + bytes_written, protect_check + 1);
|
|
|
|
// flash_enable();
|
2017-11-22 23:07:21 -05:00
|
|
|
// status = spi_write_buffer_wait(&spi_flash_desc, protect_check, 5);
|
2017-05-24 13:43:32 -04:00
|
|
|
// flash_disable();
|
|
|
|
#endif
|
2017-05-12 17:47:39 -04:00
|
|
|
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_enable();
|
2017-11-22 23:07:21 -05:00
|
|
|
uint8_t page_program_request[4] = {CMD_PAGE_PROGRAM, 0x00, 0x00, 0x00};
|
|
|
|
// Write the SPI flash write address into the bytes following the command byte.
|
|
|
|
address_to_bytes(address + bytes_written, page_program_request + 1);
|
|
|
|
struct spi_xfer page_program_xfer = {page_program_request, 0, 4};
|
|
|
|
status = spi_m_sync_transfer(&spi_flash_desc, &page_program_xfer);
|
|
|
|
if (status >= 0) {
|
|
|
|
struct spi_xfer write_data_buffer_xfer = {(uint8_t*) data + bytes_written, 0, SPI_FLASH_PAGE_SIZE};
|
|
|
|
status = spi_m_sync_transfer(&spi_flash_desc, &write_data_buffer_xfer);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
flash_disable();
|
2017-11-22 23:07:21 -05:00
|
|
|
if (status < 0) {
|
2016-10-14 15:22:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-24 13:43:32 -04:00
|
|
|
static bool page_erased(uint32_t sector_address) {
|
|
|
|
// Check the first few bytes to catch the common case where there is data
|
|
|
|
// without using a bunch of memory.
|
|
|
|
uint8_t short_buffer[4];
|
|
|
|
if (read_flash(sector_address, short_buffer, 4)) {
|
|
|
|
for (uint16_t i = 0; i < 4; i++) {
|
|
|
|
if (short_buffer[i] != 0xff) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check the full length.
|
|
|
|
uint8_t full_buffer[FILESYSTEM_BLOCK_SIZE];
|
|
|
|
if (read_flash(sector_address, full_buffer, FILESYSTEM_BLOCK_SIZE)) {
|
|
|
|
for (uint16_t i = 0; i < FILESYSTEM_BLOCK_SIZE; i++) {
|
|
|
|
if (short_buffer[i] != 0xff) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Erases the given sector. Make sure you copied all of the data out of it you
|
|
|
|
// need! Also note, sector_address is really 24 bits.
|
2016-10-14 15:22:06 -04:00
|
|
|
static bool erase_sector(uint32_t sector_address) {
|
|
|
|
// Before we erase the sector we need to wait for any writes to finish and
|
2016-10-18 17:56:17 -04:00
|
|
|
// and then enable the write again.
|
|
|
|
if (!wait_for_flash_ready() || !write_enable()) {
|
2016-10-14 15:22:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t erase_request[4] = {CMD_SECTOR_ERASE, 0x00, 0x00, 0x00};
|
|
|
|
address_to_bytes(sector_address, erase_request + 1);
|
2017-11-22 23:07:21 -05:00
|
|
|
struct spi_xfer erase_xfer = {erase_request, 0, 4};
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_enable();
|
2017-11-22 23:07:21 -05:00
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &erase_xfer);
|
2016-10-14 15:22:06 -04:00
|
|
|
flash_disable();
|
2017-11-22 23:07:21 -05:00
|
|
|
return status >= 0;
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sector is really 24 bits.
|
|
|
|
static bool copy_block(uint32_t src_address, uint32_t dest_address) {
|
|
|
|
// Copy page by page to minimize RAM buffer.
|
2017-05-12 17:47:39 -04:00
|
|
|
uint8_t buffer[SPI_FLASH_PAGE_SIZE];
|
|
|
|
for (uint32_t i = 0; i < FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE; i++) {
|
|
|
|
if (!read_flash(src_address + i * SPI_FLASH_PAGE_SIZE, buffer, SPI_FLASH_PAGE_SIZE)) {
|
2016-10-14 15:22:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-12 17:47:39 -04:00
|
|
|
if (!write_flash(dest_address + i * SPI_FLASH_PAGE_SIZE, buffer, SPI_FLASH_PAGE_SIZE)) {
|
2016-10-14 15:22:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
void spi_flash_init(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
if (spi_flash_is_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-23 18:58:05 -05:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
samd_peripherals_sercom_clock_init(SPI_FLASH_SERCOM, SPI_FLASH_SERCOM_INDEX);
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
// Set up with defaults, then change.
|
|
|
|
spi_m_sync_init(&spi_flash_desc, SPI_FLASH_SERCOM);
|
2018-01-23 18:58:05 -05:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
|
|
|
|
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);
|
2016-10-21 14:59:02 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_SCK_PIN, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_SCK_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_SCK_PIN, SPI_FLASH_SCK_PIN_FUNCTION);
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_MOSI_PIN, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_MOSI_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_MOSI_PIN, SPI_FLASH_MOSI_PIN_FUNCTION);
|
2017-05-12 17:47:39 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_MISO_PIN, GPIO_DIRECTION_IN);
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_MISO_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_MISO_PIN, SPI_FLASH_MISO_PIN_FUNCTION);
|
|
|
|
|
|
|
|
hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
|
|
|
|
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);
|
2017-05-02 18:25:06 -04:00
|
|
|
|
2018-01-29 22:52:23 -05:00
|
|
|
spi_m_sync_set_baudrate(&spi_flash_desc, samd_peripherals_spi_baudrate_to_baud_reg_value(SPI_FLASH_BAUDRATE));
|
2017-05-02 18:25:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_CS_PIN, GPIO_DIRECTION_OUT);
|
|
|
|
// There's already a pull-up on the board.
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_CS_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_CS_PIN, GPIO_PIN_FUNCTION_OFF);
|
|
|
|
|
|
|
|
// Set CS high (disabled).
|
|
|
|
flash_disable();
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
spi_m_sync_enable(&spi_flash_desc);
|
|
|
|
|
|
|
|
// Activity LED for flash writes.
|
|
|
|
#ifdef MICROPY_HW_LED_MSC
|
|
|
|
gpio_set_pin_function(SPI_FLASH_CS_PIN, GPIO_PIN_FUNCTION_OFF);
|
|
|
|
gpio_set_pin_direction(MICROPY_HW_LED_MSC, GPIO_DIRECTION_OUT);
|
|
|
|
// There's already a pull-up on the board.
|
|
|
|
gpio_set_pin_level(MICROPY_HW_LED_MSC, false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t jedec_id_request[4] = {CMD_READ_JEDEC_ID, 0x00, 0x00, 0x00};
|
|
|
|
uint8_t jedec_id_response[4] = {0x00, 0x00, 0x00, 0x00};
|
|
|
|
struct spi_xfer jedec_id_xfer = { jedec_id_request, jedec_id_response, 4 };
|
|
|
|
flash_enable();
|
|
|
|
spi_m_sync_transfer(&spi_flash_desc, &jedec_id_xfer);
|
|
|
|
flash_disable();
|
|
|
|
uint8_t manufacturer = jedec_id_response[1];
|
|
|
|
if ((jedec_id_response[1] == SPI_FLASH_JEDEC_MANUFACTURER
|
|
|
|
#ifdef SPI_FLASH_JEDEC_MANUFACTURER_2
|
|
|
|
|| jedec_id_response[1] == SPI_FLASH_JEDEC_MANUFACTURER_2
|
|
|
|
#endif
|
|
|
|
) &&
|
|
|
|
jedec_id_response[2] == SPI_FLASH_JEDEC_MEMORY_TYPE &&
|
|
|
|
jedec_id_response[3] == SPI_FLASH_JEDEC_CAPACITY) {
|
2016-10-14 15:22:06 -04:00
|
|
|
spi_flash_is_initialised = true;
|
2017-11-22 23:07:21 -05:00
|
|
|
} else {
|
|
|
|
// Unknown flash chip!
|
|
|
|
spi_flash_is_initialised = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((manufacturer == SPI_FLASH_JEDEC_MANUFACTURER && SPI_FLASH_SECTOR_PROTECTION)
|
|
|
|
#ifdef SPI_FLASH_JEDEC_MANUFACTURER_2
|
|
|
|
|| (manufacturer == SPI_FLASH_JEDEC_MANUFACTURER_2 && SPI_FLASH_SECTOR_PROTECTION_2)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
write_enable();
|
|
|
|
|
|
|
|
// Turn off sector protection
|
|
|
|
uint8_t disable_protect_request[2] = {CMD_WRITE_STATUS_BYTE1, 0x00};
|
|
|
|
struct spi_xfer disable_protect_xfer = { disable_protect_request, 0, 4 };
|
|
|
|
flash_enable();
|
|
|
|
spi_m_sync_transfer(&spi_flash_desc, &disable_protect_xfer);
|
|
|
|
flash_disable();
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
2017-11-22 23:07:21 -05:00
|
|
|
|
|
|
|
// Turn off writes in case this is a microcontroller only reset.
|
|
|
|
uint8_t disable_write_request[1] = {CMD_DISABLE_WRITE};
|
|
|
|
struct spi_xfer disable_write_xfer = { disable_write_request, 0, 1 };
|
|
|
|
flash_enable();
|
|
|
|
spi_m_sync_transfer(&spi_flash_desc, &disable_write_xfer);
|
|
|
|
flash_disable();
|
|
|
|
|
|
|
|
wait_for_flash_ready();
|
|
|
|
|
|
|
|
current_sector = NO_SECTOR_LOADED;
|
|
|
|
dirty_mask = 0;
|
|
|
|
MP_STATE_VM(flash_ram_cache) = NULL;
|
|
|
|
|
|
|
|
spi_flash_is_initialised = true;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// The size of each individual block.
|
2016-10-13 17:53:31 -04:00
|
|
|
uint32_t spi_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-18 17:56:17 -04:00
|
|
|
// The total number of available blocks.
|
2016-10-13 17:53:31 -04:00
|
|
|
uint32_t spi_flash_get_block_count(void) {
|
2016-10-18 17:56:17 -04:00
|
|
|
// We subtract one erase sector size because we may use it as a staging area
|
|
|
|
// for writes.
|
2017-05-12 17:47:39 -04:00
|
|
|
return SPI_FLASH_PART1_START_BLOCK + (SPI_FLASH_TOTAL_SIZE - SPI_FLASH_ERASE_SIZE) / FILESYSTEM_BLOCK_SIZE;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Flush the cache that was written to the scratch portion of flash. Only used
|
|
|
|
// when ram is tight.
|
2016-11-03 18:50:59 -04:00
|
|
|
static bool flush_scratch_flash(void) {
|
2016-10-14 15:22:06 -04:00
|
|
|
// First, copy out any blocks that we haven't touched from the sector we've
|
|
|
|
// cached.
|
|
|
|
bool copy_to_scratch_ok = true;
|
2017-05-12 17:47:39 -04:00
|
|
|
for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
|
2016-10-14 15:22:06 -04:00
|
|
|
if ((dirty_mask & (1 << i)) == 0) {
|
|
|
|
copy_to_scratch_ok = copy_to_scratch_ok &&
|
2017-05-12 17:47:39 -04:00
|
|
|
copy_block(current_sector + i * FILESYSTEM_BLOCK_SIZE,
|
|
|
|
SCRATCH_SECTOR + i * FILESYSTEM_BLOCK_SIZE);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!copy_to_scratch_ok) {
|
|
|
|
// TODO(tannewt): Do more here. We opted to not erase and copy bad data
|
|
|
|
// in. We still risk losing the data written to the scratch sector.
|
2016-10-18 17:56:17 -04:00
|
|
|
return false;
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
// Second, erase the current sector.
|
|
|
|
erase_sector(current_sector);
|
|
|
|
// Finally, copy the new version into it.
|
2017-05-12 17:47:39 -04:00
|
|
|
for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
|
|
|
|
copy_block(SCRATCH_SECTOR + i * FILESYSTEM_BLOCK_SIZE,
|
|
|
|
current_sector + i * FILESYSTEM_BLOCK_SIZE);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
2016-10-18 17:56:17 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempts to allocate a new set of page buffers for caching a full sector in
|
|
|
|
// 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.
|
2016-11-03 18:50:59 -04:00
|
|
|
static bool allocate_ram_cache(void) {
|
2017-05-12 17:47:39 -04:00
|
|
|
uint8_t blocks_per_sector = SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE;
|
|
|
|
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
|
2018-01-23 18:58:05 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache) = m_malloc_maybe(blocks_per_sector * pages_per_block * sizeof(uint32_t), false);
|
2016-11-29 17:29:33 -05:00
|
|
|
if (MP_STATE_VM(flash_ram_cache) == NULL) {
|
2016-10-18 17:56:17 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Declare i and j outside the loops in case we fail to allocate everything
|
|
|
|
// we need. In that case we'll give it back.
|
2016-11-03 18:50:59 -04:00
|
|
|
uint8_t i = 0;
|
|
|
|
uint8_t j = 0;
|
2016-10-18 17:56:17 -04:00
|
|
|
bool success = true;
|
2016-11-03 18:50:59 -04:00
|
|
|
for (i = 0; i < blocks_per_sector; i++) {
|
|
|
|
for (j = 0; j < pages_per_block; j++) {
|
2018-01-23 18:58:05 -05:00
|
|
|
uint8_t *page_cache = m_malloc_maybe(SPI_FLASH_PAGE_SIZE, false);
|
2016-10-18 17:56:17 -04:00
|
|
|
if (page_cache == NULL) {
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-29 17:29:33 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j] = page_cache;
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
|
|
|
if (!success) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We couldn't allocate enough so give back what we got.
|
|
|
|
if (!success) {
|
2016-11-03 18:50:59 -04:00
|
|
|
// We add 1 so that we delete 0 when i is 1. Going to zero (i >= 0)
|
|
|
|
// would never stop because i is unsigned.
|
|
|
|
i++;
|
|
|
|
for (; i > 0; i--) {
|
|
|
|
for (; j > 0; j--) {
|
2018-01-23 18:58:05 -05:00
|
|
|
m_free(MP_STATE_VM(flash_ram_cache)[(i - 1) * pages_per_block + (j - 1)]);
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
j = pages_per_block;
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
2018-01-23 18:58:05 -05:00
|
|
|
m_free(MP_STATE_VM(flash_ram_cache));
|
2016-11-29 17:29:33 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache) = NULL;
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the cached sector from ram onto the flash. We'll free the cache unless
|
|
|
|
// keep_cache is true.
|
|
|
|
static bool flush_ram_cache(bool keep_cache) {
|
|
|
|
// First, copy out any blocks that we haven't touched from the sector
|
|
|
|
// we've cached. If we don't do this we'll erase the data during the sector
|
|
|
|
// erase below.
|
|
|
|
bool copy_to_ram_ok = true;
|
2017-05-12 17:47:39 -04:00
|
|
|
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
|
|
|
|
for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
|
2016-10-18 17:56:17 -04:00
|
|
|
if ((dirty_mask & (1 << i)) == 0) {
|
2016-11-03 18:50:59 -04:00
|
|
|
for (uint8_t j = 0; j < pages_per_block; j++) {
|
2016-10-18 17:56:17 -04:00
|
|
|
copy_to_ram_ok = read_flash(
|
2017-05-12 17:47:39 -04:00
|
|
|
current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE,
|
2016-11-29 17:29:33 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
|
2017-05-12 17:47:39 -04:00
|
|
|
SPI_FLASH_PAGE_SIZE);
|
2016-10-18 17:56:17 -04:00
|
|
|
if (!copy_to_ram_ok) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!copy_to_ram_ok) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!copy_to_ram_ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Second, erase the current sector.
|
|
|
|
erase_sector(current_sector);
|
|
|
|
// Lastly, write all the data in ram that we've cached.
|
2017-05-12 17:47:39 -04:00
|
|
|
for (uint8_t i = 0; i < SPI_FLASH_ERASE_SIZE / FILESYSTEM_BLOCK_SIZE; i++) {
|
2016-11-03 18:50:59 -04:00
|
|
|
for (uint8_t j = 0; j < pages_per_block; j++) {
|
2017-05-12 17:47:39 -04:00
|
|
|
write_flash(current_sector + (i * pages_per_block + j) * SPI_FLASH_PAGE_SIZE,
|
2016-11-29 17:29:33 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j],
|
2017-05-12 17:47:39 -04:00
|
|
|
SPI_FLASH_PAGE_SIZE);
|
2016-10-18 17:56:17 -04:00
|
|
|
if (!keep_cache) {
|
2018-01-23 18:58:05 -05:00
|
|
|
m_free(MP_STATE_VM(flash_ram_cache)[i * pages_per_block + j]);
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We're done with the cache for now so give it back.
|
|
|
|
if (!keep_cache) {
|
2018-01-23 18:58:05 -05:00
|
|
|
m_free(MP_STATE_VM(flash_ram_cache));
|
2016-11-29 17:29:33 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache) = NULL;
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delegates to the correct flash flush method depending on the existing cache.
|
|
|
|
static void spi_flash_flush_keep_cache(bool keep_cache) {
|
|
|
|
if (current_sector == NO_SECTOR_LOADED) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-21 14:59:02 -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-10-18 17:56:17 -04:00
|
|
|
// If we've cached to the flash itself flush from there.
|
2016-11-29 17:29:33 -05:00
|
|
|
if (MP_STATE_VM(flash_ram_cache) == NULL) {
|
2016-10-18 17:56:17 -04:00
|
|
|
flush_scratch_flash();
|
|
|
|
} else {
|
|
|
|
flush_ram_cache(keep_cache);
|
|
|
|
}
|
2016-10-14 15:22:06 -04:00
|
|
|
current_sector = NO_SECTOR_LOADED;
|
2016-12-09 22:27:41 -05:00
|
|
|
clear_temp_status();
|
2016-10-21 14:59:02 -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
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// External flash function used. If called externally we assume we won't need
|
|
|
|
// the cache after.
|
|
|
|
void spi_flash_flush(void) {
|
|
|
|
spi_flash_flush_keep_cache(false);
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:25:06 -04:00
|
|
|
void flash_flush(void) {
|
|
|
|
spi_flash_flush();
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// 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) {
|
2016-09-02 20:00:30 -04:00
|
|
|
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-14 15:22:06 -04:00
|
|
|
if (SPI_FLASH_PART1_START_BLOCK <= block && block < spi_flash_get_block_count()) {
|
2016-09-02 20:00:30 -04:00
|
|
|
// a block in partition 1
|
2016-10-13 17:53:31 -04:00
|
|
|
block -= SPI_FLASH_PART1_START_BLOCK;
|
2017-05-12 17:47:39 -04:00
|
|
|
return 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 spi_flash_read_block(uint8_t *dest, uint32_t block) {
|
2016-09-02 20:00:30 -04:00
|
|
|
if (block == 0) {
|
2016-10-18 17:56:17 -04:00
|
|
|
// Fake the MBR so we can decide on our own partition table
|
2016-09-02 20:00:30 -04:00
|
|
|
for (int i = 0; i < 446; i++) {
|
|
|
|
dest[i] = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
build_partition(dest + 446, 0, 0x01 /* FAT12 */,
|
|
|
|
SPI_FLASH_PART1_START_BLOCK,
|
|
|
|
spi_flash_get_block_count() - SPI_FLASH_PART1_START_BLOCK);
|
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;
|
2016-10-18 17:56:17 -04:00
|
|
|
} else if (block < SPI_FLASH_PART1_START_BLOCK) {
|
2017-05-12 17:47:39 -04:00
|
|
|
memset(dest, 0, FILESYSTEM_BLOCK_SIZE);
|
2016-10-18 17:56:17 -04:00
|
|
|
return true;
|
2016-09-02 20:00:30 -04:00
|
|
|
} else {
|
2016-10-18 17:56:17 -04:00
|
|
|
// Non-MBR block, get data from flash memory.
|
2016-11-03 18:50:59 -04:00
|
|
|
int32_t address = convert_block_to_flash_addr(block);
|
2016-10-21 19:04:18 -04:00
|
|
|
if (address == -1) {
|
2016-09-02 20:00:30 -04:00
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-21 19:04:18 -04:00
|
|
|
|
|
|
|
// Mask out the lower bits that designate the address within the sector.
|
2017-05-12 17:47:39 -04:00
|
|
|
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);
|
2016-10-21 19:04:18 -04:00
|
|
|
uint8_t mask = 1 << (block_index);
|
|
|
|
// We're reading from the currently cached sector.
|
|
|
|
if (current_sector == this_sector && (mask & dirty_mask) > 0) {
|
2016-11-29 17:29:33 -05:00
|
|
|
if (MP_STATE_VM(flash_ram_cache) != NULL) {
|
2017-05-12 17:47:39 -04:00
|
|
|
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
|
2016-10-21 19:04:18 -04:00
|
|
|
for (int i = 0; i < pages_per_block; i++) {
|
2017-05-12 17:47:39 -04:00
|
|
|
memcpy(dest + i * SPI_FLASH_PAGE_SIZE,
|
2016-11-29 17:29:33 -05:00
|
|
|
MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
|
2017-05-12 17:47:39 -04:00
|
|
|
SPI_FLASH_PAGE_SIZE);
|
2016-10-21 19:04:18 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
2017-05-12 17:47:39 -04:00
|
|
|
uint32_t scratch_address = SCRATCH_SECTOR + block_index * FILESYSTEM_BLOCK_SIZE;
|
|
|
|
return read_flash(scratch_address, dest, FILESYSTEM_BLOCK_SIZE);
|
2016-10-21 19:04:18 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-12 17:47:39 -04:00
|
|
|
return read_flash(address, dest, FILESYSTEM_BLOCK_SIZE);
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:22:06 -04:00
|
|
|
bool spi_flash_write_block(const uint8_t *data, uint32_t block) {
|
2016-10-18 17:56:17 -04:00
|
|
|
if (block < SPI_FLASH_PART1_START_BLOCK) {
|
|
|
|
// Fake writing below the flash partition.
|
2016-09-02 20:00:30 -04:00
|
|
|
return true;
|
|
|
|
} else {
|
2016-10-18 17:56:17 -04:00
|
|
|
// Non-MBR block, copy to cache
|
2016-11-03 18:50:59 -04:00
|
|
|
int32_t address = convert_block_to_flash_addr(block);
|
2016-10-14 15:22:06 -04:00
|
|
|
if (address == -1) {
|
2016-09-02 20:00:30 -04:00
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-14 15:22:06 -04:00
|
|
|
// Wait for any previous writes to finish.
|
|
|
|
wait_for_flash_ready();
|
2016-10-18 17:56:17 -04:00
|
|
|
// Mask out the lower bits that designate the address within the sector.
|
2017-05-12 17:47:39 -04:00
|
|
|
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);
|
2016-10-14 15:22:06 -04:00
|
|
|
uint8_t mask = 1 << (block_index);
|
2017-05-24 13:43:32 -04:00
|
|
|
// Flush the cache if we're moving onto a sector or we're writing the
|
2016-10-18 17:56:17 -04:00
|
|
|
// same block again.
|
2016-10-14 15:22:06 -04:00
|
|
|
if (current_sector != this_sector || (mask & dirty_mask) > 0) {
|
2017-05-24 13:43:32 -04:00
|
|
|
// 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);
|
|
|
|
}
|
2016-10-14 15:22:06 -04:00
|
|
|
if (current_sector != NO_SECTOR_LOADED) {
|
2016-10-18 17:56:17 -04:00
|
|
|
spi_flash_flush_keep_cache(true);
|
|
|
|
}
|
2016-11-29 17:29:33 -05:00
|
|
|
if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) {
|
2016-10-18 17:56:17 -04:00
|
|
|
erase_sector(SCRATCH_SECTOR);
|
|
|
|
wait_for_flash_ready();
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
current_sector = this_sector;
|
|
|
|
dirty_mask = 0;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
2016-10-14 15:22:06 -04:00
|
|
|
dirty_mask |= mask;
|
2016-10-18 17:56:17 -04:00
|
|
|
// Copy the block to the appropriate cache.
|
2016-11-29 17:29:33 -05:00
|
|
|
if (MP_STATE_VM(flash_ram_cache) != NULL) {
|
2017-05-12 17:47:39 -04:00
|
|
|
uint8_t pages_per_block = FILESYSTEM_BLOCK_SIZE / SPI_FLASH_PAGE_SIZE;
|
2016-10-18 17:56:17 -04:00
|
|
|
for (int i = 0; i < pages_per_block; i++) {
|
2016-11-29 17:29:33 -05:00
|
|
|
memcpy(MP_STATE_VM(flash_ram_cache)[block_index * pages_per_block + i],
|
2017-05-12 17:47:39 -04:00
|
|
|
data + i * SPI_FLASH_PAGE_SIZE,
|
|
|
|
SPI_FLASH_PAGE_SIZE);
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
2017-05-12 17:47:39 -04:00
|
|
|
uint32_t scratch_address = SCRATCH_SECTOR + block_index * FILESYSTEM_BLOCK_SIZE;
|
|
|
|
return write_flash(scratch_address, data, FILESYSTEM_BLOCK_SIZE);
|
2016-10-18 17:56:17 -04:00
|
|
|
}
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
mp_uint_t spi_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 (!spi_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 spi_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 (!spi_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 spi_flash_obj = {&spi_flash_type};
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t spi_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)&spi_flash_obj;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t spi_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 = spi_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(spi_flash_obj_readblocks_obj, spi_flash_obj_readblocks);
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t spi_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 = spi_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(spi_flash_obj_writeblocks_obj, spi_flash_obj_writeblocks);
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-13 17:53:31 -04:00
|
|
|
STATIC mp_obj_t spi_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: spi_flash_init(); return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
case BP_IOCTL_DEINIT: spi_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0); // TODO properly
|
|
|
|
case BP_IOCTL_SYNC: spi_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(spi_flash_get_block_count());
|
|
|
|
case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(spi_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(spi_flash_obj_ioctl_obj, spi_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 spi_flash_obj_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&spi_flash_obj_readblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&spi_flash_obj_writeblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&spi_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(spi_flash_obj_locals_dict, spi_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 spi_flash_type = {
|
2016-09-02 20:00:30 -04:00
|
|
|
{ &mp_type_type },
|
2016-10-13 17:53:31 -04:00
|
|
|
.name = MP_QSTR_SPIFlash,
|
|
|
|
.make_new = spi_flash_obj_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&spi_flash_obj_locals_dict,
|
2016-09-02 20:00:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void flash_init_vfs(fs_user_mount_t *vfs) {
|
2017-06-26 17:04:22 -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-06-26 17:04:22 -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)&spi_flash_obj_readblocks_obj;
|
|
|
|
vfs->readblocks[1] = (mp_obj_t)&spi_flash_obj;
|
|
|
|
vfs->readblocks[2] = (mp_obj_t)spi_flash_read_blocks; // native version
|
|
|
|
vfs->writeblocks[0] = (mp_obj_t)&spi_flash_obj_writeblocks_obj;
|
|
|
|
vfs->writeblocks[1] = (mp_obj_t)&spi_flash_obj;
|
|
|
|
vfs->writeblocks[2] = (mp_obj_t)spi_flash_write_blocks; // native version
|
|
|
|
vfs->u.ioctl[0] = (mp_obj_t)&spi_flash_obj_ioctl_obj;
|
|
|
|
vfs->u.ioctl[1] = (mp_obj_t)&spi_flash_obj;
|
2016-09-02 20:00:30 -04:00
|
|
|
}
|