2017-01-24 05:56:03 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-03-02 05:01:18 +00:00
|
|
|
* Copyright (c) 2016-2018 Damien P. George
|
2017-01-24 05:56:03 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "drivers/memory/spiflash.h"
|
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
#define QSPI_QE_MASK (0x02)
|
|
|
|
#define USE_WR_DELAY (1)
|
|
|
|
|
|
|
|
#define CMD_WRSR (0x01)
|
|
|
|
#define CMD_WRITE (0x02)
|
|
|
|
#define CMD_READ (0x03)
|
|
|
|
#define CMD_RDSR (0x05)
|
|
|
|
#define CMD_WREN (0x06)
|
|
|
|
#define CMD_SEC_ERASE (0x20)
|
|
|
|
#define CMD_RDCR (0x35)
|
|
|
|
#define CMD_RD_DEVID (0x9f)
|
|
|
|
#define CMD_CHIP_ERASE (0xc7)
|
|
|
|
#define CMD_C4READ (0xeb)
|
|
|
|
|
2020-01-28 03:59:05 +00:00
|
|
|
// 32 bit addressing commands
|
|
|
|
#define CMD_WRITE_32 (0x12)
|
|
|
|
#define CMD_READ_32 (0x13)
|
|
|
|
#define CMD_SEC_ERASE_32 (0x21)
|
|
|
|
#define CMD_C4READ_32 (0xec)
|
|
|
|
|
2017-01-24 05:56:03 +00:00
|
|
|
#define WAIT_SR_TIMEOUT (1000000)
|
|
|
|
|
|
|
|
#define PAGE_SIZE (256) // maximum bytes we can write in one SPI transfer
|
2018-06-07 05:09:10 +01:00
|
|
|
#define SECTOR_SIZE MP_SPIFLASH_ERASE_BLOCK_SIZE
|
2017-01-24 05:56:03 +00:00
|
|
|
|
|
|
|
STATIC void mp_spiflash_acquire_bus(mp_spiflash_t *self) {
|
2018-03-02 05:01:18 +00:00
|
|
|
const mp_spiflash_config_t *c = self->config;
|
|
|
|
if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) {
|
|
|
|
c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE);
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void mp_spiflash_release_bus(mp_spiflash_t *self) {
|
2018-03-02 05:01:18 +00:00
|
|
|
const mp_spiflash_config_t *c = self->config;
|
|
|
|
if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) {
|
|
|
|
c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE);
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
STATIC int mp_spiflash_write_cmd_data(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t data) {
|
|
|
|
int ret = 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
const mp_spiflash_config_t *c = self->config;
|
|
|
|
if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
|
|
|
|
// Note: len/data are unused for standard SPI
|
|
|
|
mp_hal_pin_write(c->bus.u_spi.cs, 0);
|
|
|
|
c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL);
|
|
|
|
mp_hal_pin_write(c->bus.u_spi.cs, 1);
|
|
|
|
} else {
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = c->bus.u_qspi.proto->write_cmd_data(c->bus.u_qspi.data, cmd, len, data);
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
STATIC int mp_spiflash_transfer_cmd_addr_data(mp_spiflash_t *self, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src, uint8_t *dest) {
|
|
|
|
int ret = 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
const mp_spiflash_config_t *c = self->config;
|
|
|
|
if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
|
2020-01-28 03:59:05 +00:00
|
|
|
uint8_t buf[5] = {cmd, 0};
|
|
|
|
uint8_t buff_len = 1 + mp_spi_set_addr_buff(&buf[1], addr);
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_hal_pin_write(c->bus.u_spi.cs, 0);
|
2020-01-28 03:59:05 +00:00
|
|
|
c->bus.u_spi.proto->transfer(c->bus.u_spi.data, buff_len, buf, NULL);
|
|
|
|
if (len && (src != NULL)) {
|
2018-03-02 05:01:18 +00:00
|
|
|
c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, src, NULL);
|
2020-01-28 03:59:05 +00:00
|
|
|
} else if (len && (dest != NULL)) {
|
|
|
|
c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, dest, dest);
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2020-01-28 03:59:05 +00:00
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_hal_pin_write(c->bus.u_spi.cs, 1);
|
|
|
|
} else {
|
2020-01-28 03:59:05 +00:00
|
|
|
if (dest != NULL) {
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = c->bus.u_qspi.proto->read_cmd_qaddr_qdata(c->bus.u_qspi.data, cmd, addr, len, dest);
|
2020-01-28 03:59:05 +00:00
|
|
|
} else {
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = c->bus.u_qspi.proto->write_cmd_addr_data(c->bus.u_qspi.data, cmd, addr, len, src);
|
2020-01-28 03:59:05 +00:00
|
|
|
}
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 01:28:54 +00:00
|
|
|
STATIC int mp_spiflash_read_cmd(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t *dest) {
|
2018-03-02 05:01:18 +00:00
|
|
|
const mp_spiflash_config_t *c = self->config;
|
|
|
|
if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
|
|
|
|
mp_hal_pin_write(c->bus.u_spi.cs, 0);
|
|
|
|
c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL);
|
2022-12-09 01:28:54 +00:00
|
|
|
c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, (void*)dest, (void*)dest);
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_hal_pin_write(c->bus.u_spi.cs, 1);
|
2022-12-09 01:28:54 +00:00
|
|
|
return 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
} else {
|
2022-12-09 01:28:54 +00:00
|
|
|
return c->bus.u_qspi.proto->read_cmd(c->bus.u_qspi.data, cmd, len, dest);
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
STATIC int mp_spiflash_read_data(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
|
2018-03-02 05:01:18 +00:00
|
|
|
const mp_spiflash_config_t *c = self->config;
|
2020-01-28 03:59:05 +00:00
|
|
|
uint8_t cmd;
|
2018-03-02 05:01:18 +00:00
|
|
|
if (c->bus_kind == MP_SPIFLASH_BUS_SPI) {
|
2022-06-01 09:50:43 +01:00
|
|
|
cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_READ_32 : CMD_READ;
|
2018-03-02 05:01:18 +00:00
|
|
|
} else {
|
2022-06-01 09:50:43 +01:00
|
|
|
cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_C4READ_32 : CMD_C4READ;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2021-03-04 23:15:29 +00:00
|
|
|
return mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, NULL, dest);
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
STATIC int mp_spiflash_write_cmd(mp_spiflash_t *self, uint8_t cmd) {
|
|
|
|
return mp_spiflash_write_cmd_data(self, cmd, 0, 0);
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 05:56:03 +00:00
|
|
|
STATIC int mp_spiflash_wait_sr(mp_spiflash_t *self, uint8_t mask, uint8_t val, uint32_t timeout) {
|
2019-01-29 04:20:01 +00:00
|
|
|
do {
|
2022-12-09 01:28:54 +00:00
|
|
|
uint32_t sr;
|
|
|
|
int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-02 05:01:18 +00:00
|
|
|
if ((sr & mask) == val) {
|
2019-01-29 04:20:01 +00:00
|
|
|
return 0; // success
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
2019-01-29 04:20:01 +00:00
|
|
|
} while (timeout--);
|
|
|
|
|
|
|
|
return -MP_ETIMEDOUT;
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int mp_spiflash_wait_wel1(mp_spiflash_t *self) {
|
|
|
|
return mp_spiflash_wait_sr(self, 2, 2, WAIT_SR_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int mp_spiflash_wait_wip0(mp_spiflash_t *self) {
|
|
|
|
return mp_spiflash_wait_sr(self, 1, 0, WAIT_SR_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:03:25 +01:00
|
|
|
static inline void mp_spiflash_deepsleep_internal(mp_spiflash_t *self, int value) {
|
|
|
|
mp_spiflash_write_cmd(self, value ? 0xb9 : 0xab); // sleep/wake
|
|
|
|
}
|
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
void mp_spiflash_init(mp_spiflash_t *self) {
|
|
|
|
self->flags = 0;
|
|
|
|
|
|
|
|
if (self->config->bus_kind == MP_SPIFLASH_BUS_SPI) {
|
|
|
|
mp_hal_pin_write(self->config->bus.u_spi.cs, 1);
|
|
|
|
mp_hal_pin_output(self->config->bus.u_spi.cs);
|
2018-03-09 06:32:28 +00:00
|
|
|
self->config->bus.u_spi.proto->ioctl(self->config->bus.u_spi.data, MP_SPI_IOCTL_INIT);
|
2018-03-02 05:01:18 +00:00
|
|
|
} else {
|
|
|
|
self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_spiflash_acquire_bus(self);
|
|
|
|
|
2019-07-02 16:03:25 +01:00
|
|
|
// Ensure SPI flash is out of sleep mode
|
|
|
|
mp_spiflash_deepsleep_internal(self, 0);
|
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
#if defined(CHECK_DEVID)
|
|
|
|
// Validate device id
|
2022-12-09 01:28:54 +00:00
|
|
|
uint32_t devid;
|
|
|
|
int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid);
|
|
|
|
if (ret != 0 || devid != CHECK_DEVID) {
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) {
|
|
|
|
// Set QE bit
|
2022-12-09 01:28:54 +00:00
|
|
|
uint32_t sr = 0, cr = 0;
|
|
|
|
int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = mp_spiflash_read_cmd(self, CMD_RDCR, 1, &cr);
|
|
|
|
}
|
|
|
|
uint32_t data = (sr & 0xff) | (cr & 0xff) << 8;
|
|
|
|
if (ret == 0 && !(data & (QSPI_QE_MASK << 8))) {
|
2018-03-11 00:25:38 +00:00
|
|
|
data |= QSPI_QE_MASK << 8;
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_spiflash_write_cmd(self, CMD_WREN);
|
|
|
|
mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data);
|
|
|
|
mp_spiflash_wait_wip0(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_spiflash_release_bus(self);
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:03:25 +01:00
|
|
|
void mp_spiflash_deepsleep(mp_spiflash_t *self, int value) {
|
|
|
|
if (value) {
|
|
|
|
mp_spiflash_acquire_bus(self);
|
|
|
|
}
|
|
|
|
mp_spiflash_deepsleep_internal(self, value);
|
|
|
|
if (!value) {
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 06:39:46 +01:00
|
|
|
STATIC int mp_spiflash_erase_block_internal(mp_spiflash_t *self, uint32_t addr) {
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = 0;
|
2017-01-24 05:56:03 +00:00
|
|
|
// enable writes
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = mp_spiflash_write_cmd(self, CMD_WREN);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
|
|
|
// wait WEL=1
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = mp_spiflash_wait_wel1(self);
|
2017-01-24 05:56:03 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// erase the sector
|
2022-06-01 09:50:43 +01:00
|
|
|
uint8_t cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_SEC_ERASE_32 : CMD_SEC_ERASE;
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, 0, NULL, NULL);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
|
|
|
// wait WIP=0
|
|
|
|
return mp_spiflash_wait_wip0(self);
|
|
|
|
}
|
|
|
|
|
2018-06-07 06:39:46 +01:00
|
|
|
STATIC int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = 0;
|
2017-01-24 05:56:03 +00:00
|
|
|
// enable writes
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = mp_spiflash_write_cmd(self, CMD_WREN);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
|
|
|
// wait WEL=1
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = mp_spiflash_wait_wel1(self);
|
2017-01-24 05:56:03 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the page
|
2022-06-01 09:50:43 +01:00
|
|
|
uint8_t cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_WRITE_32 : CMD_WRITE;
|
2021-03-04 23:15:29 +00:00
|
|
|
ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, src, NULL);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
|
|
|
// wait WIP=0
|
|
|
|
return mp_spiflash_wait_wip0(self);
|
|
|
|
}
|
|
|
|
|
2018-06-07 06:39:46 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
// Interface functions that go direct to the SPI flash device
|
|
|
|
|
|
|
|
int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr) {
|
|
|
|
mp_spiflash_acquire_bus(self);
|
|
|
|
int ret = mp_spiflash_erase_block_internal(self, addr);
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
int mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
|
2018-06-07 06:39:46 +01:00
|
|
|
if (len == 0) {
|
2021-03-04 23:15:29 +00:00
|
|
|
return 0;
|
2018-06-07 06:39:46 +01:00
|
|
|
}
|
|
|
|
mp_spiflash_acquire_bus(self);
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = mp_spiflash_read_data(self, addr, len, dest);
|
2018-06-07 06:39:46 +01:00
|
|
|
mp_spiflash_release_bus(self);
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2018-06-07 06:39:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
|
|
|
|
mp_spiflash_acquire_bus(self);
|
|
|
|
int ret = 0;
|
|
|
|
uint32_t offset = addr & (PAGE_SIZE - 1);
|
|
|
|
while (len) {
|
|
|
|
size_t rest = PAGE_SIZE - offset;
|
|
|
|
if (rest > len) {
|
|
|
|
rest = len;
|
|
|
|
}
|
|
|
|
ret = mp_spiflash_write_page(self, addr, rest, src);
|
|
|
|
if (ret != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= rest;
|
|
|
|
addr += rest;
|
|
|
|
src += rest;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-07 06:36:27 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
// Interface functions that use the cache
|
|
|
|
|
2020-12-17 05:59:54 +00:00
|
|
|
#if MICROPY_HW_SPIFLASH_ENABLE_CACHE
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
int mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) {
|
2018-03-02 05:01:18 +00:00
|
|
|
if (len == 0) {
|
2021-03-04 23:15:29 +00:00
|
|
|
return 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
mp_spiflash_acquire_bus(self);
|
2018-06-07 05:09:10 +01:00
|
|
|
mp_spiflash_cache_t *cache = self->config->cache;
|
|
|
|
if (cache->user == self && cache->block != 0xffffffff) {
|
2018-03-02 05:01:18 +00:00
|
|
|
uint32_t bis = addr / SECTOR_SIZE;
|
2018-03-13 03:13:30 +00:00
|
|
|
uint32_t bie = (addr + len - 1) / SECTOR_SIZE;
|
2018-06-07 05:09:10 +01:00
|
|
|
if (bis <= cache->block && cache->block <= bie) {
|
2018-03-13 03:13:30 +00:00
|
|
|
// Read straddles current buffer
|
|
|
|
size_t rest = 0;
|
2018-06-07 05:09:10 +01:00
|
|
|
if (bis < cache->block) {
|
2018-03-13 03:13:30 +00:00
|
|
|
// Read direct from flash for first part
|
2018-06-07 05:09:10 +01:00
|
|
|
rest = cache->block * SECTOR_SIZE - addr;
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = mp_spiflash_read_data(self, addr, rest, dest);
|
|
|
|
if (ret != 0) {
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-02 05:01:18 +00:00
|
|
|
len -= rest;
|
|
|
|
dest += rest;
|
|
|
|
addr += rest;
|
|
|
|
}
|
2018-03-13 03:13:30 +00:00
|
|
|
uint32_t offset = addr & (SECTOR_SIZE - 1);
|
2018-03-02 05:01:18 +00:00
|
|
|
rest = SECTOR_SIZE - offset;
|
|
|
|
if (rest > len) {
|
|
|
|
rest = len;
|
|
|
|
}
|
2018-06-07 05:09:10 +01:00
|
|
|
memcpy(dest, &cache->buf[offset], rest);
|
2018-03-02 05:01:18 +00:00
|
|
|
len -= rest;
|
2018-03-13 03:13:30 +00:00
|
|
|
if (len == 0) {
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_spiflash_release_bus(self);
|
2021-03-04 23:15:29 +00:00
|
|
|
return 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-03-13 03:13:30 +00:00
|
|
|
dest += rest;
|
|
|
|
addr += rest;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Read rest direct from flash
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = mp_spiflash_read_data(self, addr, len, dest);
|
2017-01-24 05:56:03 +00:00
|
|
|
mp_spiflash_release_bus(self);
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
STATIC int mp_spiflash_cache_flush_internal(mp_spiflash_t *self) {
|
2018-03-02 05:01:18 +00:00
|
|
|
#if USE_WR_DELAY
|
|
|
|
if (!(self->flags & 1)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
return 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self->flags &= ~1;
|
|
|
|
|
2018-06-07 05:09:10 +01:00
|
|
|
mp_spiflash_cache_t *cache = self->config->cache;
|
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
// Erase sector
|
2018-06-07 06:39:46 +01:00
|
|
|
int ret = mp_spiflash_erase_block_internal(self, cache->block * SECTOR_SIZE);
|
2018-03-02 05:01:18 +00:00
|
|
|
if (ret != 0) {
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write
|
|
|
|
for (int i = 0; i < 16; i += 1) {
|
2018-06-07 06:39:46 +01:00
|
|
|
uint32_t addr = cache->block * SECTOR_SIZE + i * PAGE_SIZE;
|
|
|
|
int ret = mp_spiflash_write_page(self, addr, PAGE_SIZE, cache->buf + i * PAGE_SIZE);
|
2018-03-02 05:01:18 +00:00
|
|
|
if (ret != 0) {
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-03-04 23:15:29 +00:00
|
|
|
return 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
2021-03-04 23:15:29 +00:00
|
|
|
int mp_spiflash_cache_flush(mp_spiflash_t *self) {
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_spiflash_acquire_bus(self);
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = mp_spiflash_cache_flush_internal(self);
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_spiflash_release_bus(self);
|
2021-03-04 23:15:29 +00:00
|
|
|
return ret;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 06:36:27 +01:00
|
|
|
STATIC int mp_spiflash_cached_write_part(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
|
2018-03-02 05:01:18 +00:00
|
|
|
// Align to 4096 sector
|
2017-01-24 05:56:03 +00:00
|
|
|
uint32_t offset = addr & 0xfff;
|
2018-03-02 05:01:18 +00:00
|
|
|
uint32_t sec = addr >> 12;
|
|
|
|
addr = sec << 12;
|
2017-01-24 05:56:03 +00:00
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
// Restriction for now, so we don't need to erase multiple pages
|
2018-06-07 05:09:10 +01:00
|
|
|
if (offset + len > SECTOR_SIZE) {
|
2018-06-07 06:36:27 +01:00
|
|
|
printf("mp_spiflash_cached_write_part: len is too large\n");
|
2017-01-24 05:56:03 +00:00
|
|
|
return -MP_EIO;
|
|
|
|
}
|
|
|
|
|
2018-06-07 05:09:10 +01:00
|
|
|
mp_spiflash_cache_t *cache = self->config->cache;
|
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
// Acquire the sector buffer
|
2018-06-07 05:09:10 +01:00
|
|
|
if (cache->user != self) {
|
|
|
|
if (cache->user != NULL) {
|
2018-06-07 06:36:27 +01:00
|
|
|
mp_spiflash_cache_flush(cache->user);
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-06-07 05:09:10 +01:00
|
|
|
cache->user = self;
|
|
|
|
cache->block = 0xffffffff;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
2018-06-07 05:09:10 +01:00
|
|
|
if (cache->block != sec) {
|
2018-03-02 05:01:18 +00:00
|
|
|
// Read sector
|
|
|
|
#if USE_WR_DELAY
|
2018-06-07 05:09:10 +01:00
|
|
|
if (cache->block != 0xffffffff) {
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = mp_spiflash_cache_flush_internal(self);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
2021-03-04 23:15:29 +00:00
|
|
|
int ret = mp_spiflash_read_data(self, addr, SECTOR_SIZE, cache->buf);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
#if USE_WR_DELAY
|
|
|
|
|
2018-06-07 05:09:10 +01:00
|
|
|
cache->block = sec;
|
2018-03-02 05:01:18 +00:00
|
|
|
// Just copy to buffer
|
2018-06-07 05:09:10 +01:00
|
|
|
memcpy(cache->buf + offset, src, len);
|
2018-03-02 05:01:18 +00:00
|
|
|
// And mark dirty
|
|
|
|
self->flags |= 1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
uint32_t dirty = 0;
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2018-06-07 05:09:10 +01:00
|
|
|
if (cache->buf[offset + i] != src[i]) {
|
|
|
|
if (cache->buf[offset + i] != 0xff) {
|
2018-03-02 05:01:18 +00:00
|
|
|
// Erase sector
|
2018-06-07 06:39:46 +01:00
|
|
|
int ret = mp_spiflash_erase_block_internal(self, addr);
|
2018-03-02 05:01:18 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
dirty = 0xffff;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
dirty |= (1 << ((offset + i) >> 8));
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 05:09:10 +01:00
|
|
|
cache->block = sec;
|
2018-03-02 05:01:18 +00:00
|
|
|
// Copy new block into buffer
|
2018-06-07 05:09:10 +01:00
|
|
|
memcpy(cache->buf + offset, src, len);
|
2017-01-24 05:56:03 +00:00
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
// Write sector in pages of 256 bytes
|
|
|
|
for (size_t i = 0; i < 16; ++i) {
|
|
|
|
if (dirty & (1 << i)) {
|
2018-06-07 06:39:46 +01:00
|
|
|
int ret = mp_spiflash_write_page(self, addr + i * PAGE_SIZE, PAGE_SIZE, cache->buf + i * PAGE_SIZE);
|
2018-03-02 05:01:18 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-24 05:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-24 05:56:03 +00:00
|
|
|
return 0; // success
|
|
|
|
}
|
2018-03-02 05:01:18 +00:00
|
|
|
|
2018-06-07 06:36:27 +01:00
|
|
|
int mp_spiflash_cached_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) {
|
2018-03-02 05:01:18 +00:00
|
|
|
uint32_t bis = addr / SECTOR_SIZE;
|
|
|
|
uint32_t bie = (addr + len - 1) / SECTOR_SIZE;
|
|
|
|
|
|
|
|
mp_spiflash_acquire_bus(self);
|
2018-03-13 03:13:30 +00:00
|
|
|
|
2018-06-07 05:09:10 +01:00
|
|
|
mp_spiflash_cache_t *cache = self->config->cache;
|
|
|
|
if (cache->user == self && bis <= cache->block && bie >= cache->block) {
|
2018-03-13 03:13:30 +00:00
|
|
|
// Write straddles current buffer
|
|
|
|
uint32_t pre;
|
|
|
|
uint32_t offset;
|
2018-06-07 05:09:10 +01:00
|
|
|
if (cache->block * SECTOR_SIZE >= addr) {
|
|
|
|
pre = cache->block * SECTOR_SIZE - addr;
|
2018-03-02 05:01:18 +00:00
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
pre = 0;
|
2018-06-07 05:09:10 +01:00
|
|
|
offset = addr - cache->block * SECTOR_SIZE;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-03-13 03:13:30 +00:00
|
|
|
|
|
|
|
// Write buffered part first
|
|
|
|
uint32_t len_in_buf = len - pre;
|
|
|
|
len = 0;
|
|
|
|
if (len_in_buf > SECTOR_SIZE - offset) {
|
|
|
|
len = len_in_buf - (SECTOR_SIZE - offset);
|
|
|
|
len_in_buf = SECTOR_SIZE - offset;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-06-07 05:09:10 +01:00
|
|
|
memcpy(&cache->buf[offset], &src[pre], len_in_buf);
|
2018-03-02 05:01:18 +00:00
|
|
|
self->flags |= 1; // Mark dirty
|
2018-03-13 03:13:30 +00:00
|
|
|
|
|
|
|
// Write part before buffer sector
|
2018-03-02 05:01:18 +00:00
|
|
|
while (pre) {
|
|
|
|
int rest = pre & (SECTOR_SIZE - 1);
|
|
|
|
if (rest == 0) {
|
|
|
|
rest = SECTOR_SIZE;
|
|
|
|
}
|
2018-06-07 06:36:27 +01:00
|
|
|
int ret = mp_spiflash_cached_write_part(self, addr, rest, src);
|
2018-03-13 03:13:30 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
src += rest;
|
2018-03-02 05:01:18 +00:00
|
|
|
addr += rest;
|
|
|
|
pre -= rest;
|
|
|
|
}
|
2018-03-13 03:13:30 +00:00
|
|
|
src += len_in_buf;
|
|
|
|
addr += len_in_buf;
|
|
|
|
|
|
|
|
// Fall through to write remaining part
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = addr & (SECTOR_SIZE - 1);
|
|
|
|
while (len) {
|
|
|
|
int rest = SECTOR_SIZE - offset;
|
|
|
|
if (rest > len) {
|
|
|
|
rest = len;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-06-07 06:36:27 +01:00
|
|
|
int ret = mp_spiflash_cached_write_part(self, addr, rest, src);
|
2018-03-13 03:13:30 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return ret;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-03-13 03:13:30 +00:00
|
|
|
len -= rest;
|
|
|
|
addr += rest;
|
|
|
|
src += rest;
|
|
|
|
offset = 0;
|
2018-03-02 05:01:18 +00:00
|
|
|
}
|
2018-03-13 03:13:30 +00:00
|
|
|
|
2018-03-02 05:01:18 +00:00
|
|
|
mp_spiflash_release_bus(self);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-17 05:59:54 +00:00
|
|
|
|
|
|
|
#endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE
|