Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 6 | * Copyright (c) 2016-2018 Damien P. George |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "py/mperrno.h" |
| 31 | #include "py/mphal.h" |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 32 | #include "drivers/memory/spiflash.h" |
| 33 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 34 | #define QSPI_QE_MASK (0x02) |
| 35 | #define USE_WR_DELAY (1) |
| 36 | |
| 37 | #define CMD_WRSR (0x01) |
| 38 | #define CMD_WRITE (0x02) |
| 39 | #define CMD_READ (0x03) |
| 40 | #define CMD_RDSR (0x05) |
| 41 | #define CMD_WREN (0x06) |
| 42 | #define CMD_SEC_ERASE (0x20) |
| 43 | #define CMD_RDCR (0x35) |
| 44 | #define CMD_RD_DEVID (0x9f) |
| 45 | #define CMD_CHIP_ERASE (0xc7) |
| 46 | #define CMD_C4READ (0xeb) |
iabdalkader | b5e80fa | 2024-11-29 16:56:20 +0100 | [diff] [blame] | 47 | #define CMD_RSTEN (0x66) |
| 48 | #define CMD_RESET (0x99) |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 49 | |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 50 | // 32 bit addressing commands |
| 51 | #define CMD_WRITE_32 (0x12) |
| 52 | #define CMD_READ_32 (0x13) |
| 53 | #define CMD_SEC_ERASE_32 (0x21) |
| 54 | #define CMD_C4READ_32 (0xec) |
| 55 | |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 56 | #define WAIT_SR_TIMEOUT (1000000) |
| 57 | |
| 58 | #define PAGE_SIZE (256) // maximum bytes we can write in one SPI transfer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 59 | #define SECTOR_SIZE MP_SPIFLASH_ERASE_BLOCK_SIZE |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 60 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 61 | static void mp_spiflash_acquire_bus(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 62 | const mp_spiflash_config_t *c = self->config; |
| 63 | if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 64 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE, 0); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 65 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 66 | } |
| 67 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 68 | static void mp_spiflash_release_bus(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 69 | const mp_spiflash_config_t *c = self->config; |
| 70 | if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 71 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE, 0); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void mp_spiflash_notify_modified(mp_spiflash_t *self, uint32_t addr, uint32_t len) { |
| 76 | const mp_spiflash_config_t *c = self->config; |
| 77 | if (c->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 78 | uintptr_t arg[2] = { addr, len }; |
| 79 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_MEMORY_MODIFIED, (uintptr_t)&arg[0]); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 80 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 81 | } |
| 82 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 83 | static int mp_spiflash_write_cmd_data(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t data) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 84 | int ret = 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 85 | const mp_spiflash_config_t *c = self->config; |
| 86 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 87 | // Note: len/data are unused for standard SPI |
| 88 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 89 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL); |
| 90 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 91 | } else { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 92 | ret = c->bus.u_qspi.proto->write_cmd_data(c->bus.u_qspi.data, cmd, len, data); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 93 | } |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 94 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 95 | } |
| 96 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 97 | 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) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 98 | int ret = 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 99 | const mp_spiflash_config_t *c = self->config; |
| 100 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 101 | uint8_t buf[5] = {cmd, 0}; |
| 102 | uint8_t buff_len = 1 + mp_spi_set_addr_buff(&buf[1], addr); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 103 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 104 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, buff_len, buf, NULL); |
| 105 | if (len && (src != NULL)) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 106 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, src, NULL); |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 107 | } else if (len && (dest != NULL)) { |
| 108 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, dest, dest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 109 | } |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 110 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 111 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 112 | } else { |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 113 | if (dest != NULL) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 114 | ret = c->bus.u_qspi.proto->read_cmd_qaddr_qdata(c->bus.u_qspi.data, cmd, addr, len, dest); |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 115 | } else { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 116 | ret = c->bus.u_qspi.proto->write_cmd_addr_data(c->bus.u_qspi.data, cmd, addr, len, src); |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 117 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 118 | } |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 119 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 120 | } |
| 121 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 122 | static int mp_spiflash_read_cmd(mp_spiflash_t *self, uint8_t cmd, size_t len, uint32_t *dest) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 123 | const mp_spiflash_config_t *c = self->config; |
| 124 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 125 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 126 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL); |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 127 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, len, (void*)dest, (void*)dest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 128 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 129 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 130 | } else { |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 131 | return c->bus.u_qspi.proto->read_cmd(c->bus.u_qspi.data, cmd, len, dest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 135 | static int mp_spiflash_read_data(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 136 | const mp_spiflash_config_t *c = self->config; |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 137 | uint8_t cmd; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 138 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 139 | cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_READ_32 : CMD_READ; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 140 | } else { |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 141 | cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_C4READ_32 : CMD_C4READ; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 142 | } |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 143 | return mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, NULL, dest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 144 | } |
| 145 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 146 | static int mp_spiflash_write_cmd(mp_spiflash_t *self, uint8_t cmd) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 147 | return mp_spiflash_write_cmd_data(self, cmd, 0, 0); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 148 | } |
| 149 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 150 | static int mp_spiflash_wait_sr(mp_spiflash_t *self, uint8_t mask, uint8_t val, uint32_t timeout) { |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 151 | do { |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 152 | uint32_t sr; |
| 153 | int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr); |
| 154 | if (ret != 0) { |
| 155 | return ret; |
| 156 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 157 | if ((sr & mask) == val) { |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 158 | return 0; // success |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 159 | } |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 160 | } while (timeout--); |
| 161 | |
| 162 | return -MP_ETIMEDOUT; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 163 | } |
| 164 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 165 | static int mp_spiflash_wait_wel1(mp_spiflash_t *self) { |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 166 | return mp_spiflash_wait_sr(self, 2, 2, WAIT_SR_TIMEOUT); |
| 167 | } |
| 168 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 169 | static int mp_spiflash_wait_wip0(mp_spiflash_t *self) { |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 170 | return mp_spiflash_wait_sr(self, 1, 0, WAIT_SR_TIMEOUT); |
| 171 | } |
| 172 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame] | 173 | static inline void mp_spiflash_deepsleep_internal(mp_spiflash_t *self, int value) { |
| 174 | mp_spiflash_write_cmd(self, value ? 0xb9 : 0xab); // sleep/wake |
| 175 | } |
| 176 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 177 | void mp_spiflash_init(mp_spiflash_t *self) { |
| 178 | self->flags = 0; |
| 179 | |
| 180 | if (self->config->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 181 | mp_hal_pin_write(self->config->bus.u_spi.cs, 1); |
| 182 | mp_hal_pin_output(self->config->bus.u_spi.cs); |
Damien George | a739b35 | 2018-03-09 17:32:28 +1100 | [diff] [blame] | 183 | self->config->bus.u_spi.proto->ioctl(self->config->bus.u_spi.data, MP_SPI_IOCTL_INIT); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 184 | } else { |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 185 | self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT, 0); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | mp_spiflash_acquire_bus(self); |
| 189 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame] | 190 | // Ensure SPI flash is out of sleep mode |
| 191 | mp_spiflash_deepsleep_internal(self, 0); |
| 192 | |
iabdalkader | b5e80fa | 2024-11-29 16:56:20 +0100 | [diff] [blame] | 193 | // Software reset. |
| 194 | #if MICROPY_HW_SPIFLASH_SOFT_RESET |
| 195 | mp_spiflash_write_cmd(self, CMD_RSTEN); |
| 196 | mp_spiflash_write_cmd(self, CMD_RESET); |
| 197 | mp_spiflash_wait_wip0(self); |
| 198 | mp_hal_delay_ms(1); |
| 199 | #endif |
| 200 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 201 | #if defined(CHECK_DEVID) |
| 202 | // Validate device id |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 203 | uint32_t devid; |
| 204 | int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid); |
| 205 | if (ret != 0 || devid != CHECK_DEVID) { |
| 206 | mp_spiflash_release_bus(self); |
| 207 | return; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 208 | } |
| 209 | #endif |
| 210 | |
| 211 | if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 212 | // Set QE bit |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 213 | uint32_t sr = 0, cr = 0; |
| 214 | int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr); |
| 215 | if (ret == 0) { |
| 216 | ret = mp_spiflash_read_cmd(self, CMD_RDCR, 1, &cr); |
| 217 | } |
| 218 | uint32_t data = (sr & 0xff) | (cr & 0xff) << 8; |
| 219 | if (ret == 0 && !(data & (QSPI_QE_MASK << 8))) { |
Damien George | cc34b08 | 2018-03-11 11:25:38 +1100 | [diff] [blame] | 220 | data |= QSPI_QE_MASK << 8; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 221 | mp_spiflash_write_cmd(self, CMD_WREN); |
| 222 | mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data); |
| 223 | mp_spiflash_wait_wip0(self); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | mp_spiflash_release_bus(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 228 | } |
| 229 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame] | 230 | void mp_spiflash_deepsleep(mp_spiflash_t *self, int value) { |
| 231 | if (value) { |
| 232 | mp_spiflash_acquire_bus(self); |
| 233 | } |
| 234 | mp_spiflash_deepsleep_internal(self, value); |
| 235 | if (!value) { |
| 236 | mp_spiflash_release_bus(self); |
| 237 | } |
| 238 | } |
| 239 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 240 | static int mp_spiflash_erase_block_internal(mp_spiflash_t *self, uint32_t addr) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 241 | int ret = 0; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 242 | // enable writes |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 243 | ret = mp_spiflash_write_cmd(self, CMD_WREN); |
| 244 | if (ret != 0) { |
| 245 | return ret; |
| 246 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 247 | |
| 248 | // wait WEL=1 |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 249 | ret = mp_spiflash_wait_wel1(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 250 | if (ret != 0) { |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | // erase the sector |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 255 | uint8_t cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_SEC_ERASE_32 : CMD_SEC_ERASE; |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 256 | ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, 0, NULL, NULL); |
| 257 | if (ret != 0) { |
| 258 | return ret; |
| 259 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 260 | |
| 261 | // wait WIP=0 |
| 262 | return mp_spiflash_wait_wip0(self); |
| 263 | } |
| 264 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 265 | static int mp_spiflash_write_page(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 266 | int ret = 0; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 267 | // enable writes |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 268 | ret = mp_spiflash_write_cmd(self, CMD_WREN); |
| 269 | if (ret != 0) { |
| 270 | return ret; |
| 271 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 272 | |
| 273 | // wait WEL=1 |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 274 | ret = mp_spiflash_wait_wel1(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 275 | if (ret != 0) { |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | // write the page |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 280 | uint8_t cmd = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? CMD_WRITE_32 : CMD_WRITE; |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 281 | ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, src, NULL); |
| 282 | if (ret != 0) { |
| 283 | return ret; |
| 284 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 285 | |
| 286 | // wait WIP=0 |
| 287 | return mp_spiflash_wait_wip0(self); |
| 288 | } |
| 289 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 290 | /******************************************************************************/ |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 291 | // Interface functions that go direct to the SPI flash device |
| 292 | |
| 293 | int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr) { |
| 294 | mp_spiflash_acquire_bus(self); |
| 295 | int ret = mp_spiflash_erase_block_internal(self, addr); |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 296 | mp_spiflash_notify_modified(self, addr, SECTOR_SIZE); |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 297 | mp_spiflash_release_bus(self); |
| 298 | return ret; |
| 299 | } |
| 300 | |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 301 | int mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 302 | if (len == 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 303 | return 0; |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 304 | } |
| 305 | mp_spiflash_acquire_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 306 | int ret = mp_spiflash_read_data(self, addr, len, dest); |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 307 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 308 | return ret; |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 312 | uint32_t orig_addr = addr; |
| 313 | uint32_t orig_len = len; |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 314 | mp_spiflash_acquire_bus(self); |
| 315 | int ret = 0; |
| 316 | uint32_t offset = addr & (PAGE_SIZE - 1); |
| 317 | while (len) { |
| 318 | size_t rest = PAGE_SIZE - offset; |
| 319 | if (rest > len) { |
| 320 | rest = len; |
| 321 | } |
| 322 | ret = mp_spiflash_write_page(self, addr, rest, src); |
| 323 | if (ret != 0) { |
| 324 | break; |
| 325 | } |
| 326 | len -= rest; |
| 327 | addr += rest; |
| 328 | src += rest; |
| 329 | offset = 0; |
| 330 | } |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 331 | mp_spiflash_notify_modified(self, orig_addr, orig_len); |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 332 | mp_spiflash_release_bus(self); |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /******************************************************************************/ |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 337 | // Interface functions that use the cache |
Damien George | c61e859 | 2025-03-19 15:47:35 +1100 | [diff] [blame] | 338 | // |
| 339 | // These functions do not call mp_spiflash_notify_modified(), so shouldn't be |
| 340 | // used for memory-mapped flash (for example). |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 341 | |
Damien George | e43a74a | 2020-12-17 16:59:54 +1100 | [diff] [blame] | 342 | #if MICROPY_HW_SPIFLASH_ENABLE_CACHE |
| 343 | |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 344 | int mp_spiflash_cached_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 345 | if (len == 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 346 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 347 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 348 | mp_spiflash_acquire_bus(self); |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 349 | mp_spiflash_cache_t *cache = self->config->cache; |
| 350 | if (cache->user == self && cache->block != 0xffffffff) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 351 | uint32_t bis = addr / SECTOR_SIZE; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 352 | uint32_t bie = (addr + len - 1) / SECTOR_SIZE; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 353 | if (bis <= cache->block && cache->block <= bie) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 354 | // Read straddles current buffer |
| 355 | size_t rest = 0; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 356 | if (bis < cache->block) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 357 | // Read direct from flash for first part |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 358 | rest = cache->block * SECTOR_SIZE - addr; |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 359 | int ret = mp_spiflash_read_data(self, addr, rest, dest); |
| 360 | if (ret != 0) { |
| 361 | mp_spiflash_release_bus(self); |
| 362 | return ret; |
| 363 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 364 | len -= rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 365 | dest += rest; |
| 366 | addr += rest; |
| 367 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 368 | uint32_t offset = addr & (SECTOR_SIZE - 1); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 369 | rest = SECTOR_SIZE - offset; |
| 370 | if (rest > len) { |
| 371 | rest = len; |
| 372 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 373 | memcpy(dest, &cache->buf[offset], rest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 374 | len -= rest; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 375 | if (len == 0) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 376 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 377 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 378 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 379 | dest += rest; |
| 380 | addr += rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 381 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 382 | } |
| 383 | // Read rest direct from flash |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 384 | int ret = mp_spiflash_read_data(self, addr, len, dest); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 385 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 386 | return ret; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 387 | } |
| 388 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 389 | static int mp_spiflash_cache_flush_internal(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 390 | #if USE_WR_DELAY |
| 391 | if (!(self->flags & 1)) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 392 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 393 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 394 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 395 | self->flags &= ~1; |
| 396 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 397 | mp_spiflash_cache_t *cache = self->config->cache; |
| 398 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 399 | // Erase sector |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 400 | int ret = mp_spiflash_erase_block_internal(self, cache->block * SECTOR_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 401 | if (ret != 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 402 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // Write |
| 406 | for (int i = 0; i < 16; i += 1) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 407 | uint32_t addr = cache->block * SECTOR_SIZE + i * PAGE_SIZE; |
| 408 | int ret = mp_spiflash_write_page(self, addr, PAGE_SIZE, cache->buf + i * PAGE_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 409 | if (ret != 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 410 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | #endif |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 414 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 415 | } |
| 416 | |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 417 | int mp_spiflash_cache_flush(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 418 | mp_spiflash_acquire_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 419 | int ret = mp_spiflash_cache_flush_internal(self); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 420 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 421 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 422 | } |
| 423 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 424 | static int mp_spiflash_cached_write_part(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 425 | // Align to 4096 sector |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 426 | uint32_t offset = addr & 0xfff; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 427 | uint32_t sec = addr >> 12; |
| 428 | addr = sec << 12; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 429 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 430 | // Restriction for now, so we don't need to erase multiple pages |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 431 | if (offset + len > SECTOR_SIZE) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 432 | printf("mp_spiflash_cached_write_part: len is too large\n"); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 433 | return -MP_EIO; |
| 434 | } |
| 435 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 436 | mp_spiflash_cache_t *cache = self->config->cache; |
| 437 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 438 | // Acquire the sector buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 439 | if (cache->user != self) { |
| 440 | if (cache->user != NULL) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 441 | mp_spiflash_cache_flush(cache->user); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 442 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 443 | cache->user = self; |
| 444 | cache->block = 0xffffffff; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 445 | } |
| 446 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 447 | if (cache->block != sec) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 448 | // Read sector |
| 449 | #if USE_WR_DELAY |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 450 | if (cache->block != 0xffffffff) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 451 | int ret = mp_spiflash_cache_flush_internal(self); |
| 452 | if (ret != 0) { |
| 453 | return ret; |
| 454 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 455 | } |
| 456 | #endif |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 457 | int ret = mp_spiflash_read_data(self, addr, SECTOR_SIZE, cache->buf); |
| 458 | if (ret != 0) { |
| 459 | return ret; |
| 460 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 461 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 462 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 463 | #if USE_WR_DELAY |
| 464 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 465 | cache->block = sec; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 466 | // Just copy to buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 467 | memcpy(cache->buf + offset, src, len); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 468 | // And mark dirty |
| 469 | self->flags |= 1; |
| 470 | |
| 471 | #else |
| 472 | |
| 473 | uint32_t dirty = 0; |
| 474 | for (size_t i = 0; i < len; ++i) { |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 475 | if (cache->buf[offset + i] != src[i]) { |
| 476 | if (cache->buf[offset + i] != 0xff) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 477 | // Erase sector |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 478 | int ret = mp_spiflash_erase_block_internal(self, addr); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 479 | if (ret != 0) { |
| 480 | return ret; |
| 481 | } |
| 482 | dirty = 0xffff; |
| 483 | break; |
| 484 | } else { |
| 485 | dirty |= (1 << ((offset + i) >> 8)); |
| 486 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 490 | cache->block = sec; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 491 | // Copy new block into buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 492 | memcpy(cache->buf + offset, src, len); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 493 | |
| 494 | // Write sector in pages of 256 bytes |
| 495 | for (size_t i = 0; i < 16; ++i) { |
| 496 | if (dirty & (1 << i)) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 497 | int ret = mp_spiflash_write_page(self, addr + i * PAGE_SIZE, PAGE_SIZE, cache->buf + i * PAGE_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 498 | if (ret != 0) { |
| 499 | return ret; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | #endif |
| 505 | |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 506 | return 0; // success |
| 507 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 508 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 509 | int mp_spiflash_cached_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 510 | uint32_t bis = addr / SECTOR_SIZE; |
| 511 | uint32_t bie = (addr + len - 1) / SECTOR_SIZE; |
| 512 | |
| 513 | mp_spiflash_acquire_bus(self); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 514 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 515 | mp_spiflash_cache_t *cache = self->config->cache; |
| 516 | if (cache->user == self && bis <= cache->block && bie >= cache->block) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 517 | // Write straddles current buffer |
| 518 | uint32_t pre; |
| 519 | uint32_t offset; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 520 | if (cache->block * SECTOR_SIZE >= addr) { |
| 521 | pre = cache->block * SECTOR_SIZE - addr; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 522 | offset = 0; |
| 523 | } else { |
| 524 | pre = 0; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 525 | offset = addr - cache->block * SECTOR_SIZE; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 526 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 527 | |
| 528 | // Write buffered part first |
| 529 | uint32_t len_in_buf = len - pre; |
| 530 | len = 0; |
| 531 | if (len_in_buf > SECTOR_SIZE - offset) { |
| 532 | len = len_in_buf - (SECTOR_SIZE - offset); |
| 533 | len_in_buf = SECTOR_SIZE - offset; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 534 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 535 | memcpy(&cache->buf[offset], &src[pre], len_in_buf); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 536 | self->flags |= 1; // Mark dirty |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 537 | |
| 538 | // Write part before buffer sector |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 539 | while (pre) { |
| 540 | int rest = pre & (SECTOR_SIZE - 1); |
| 541 | if (rest == 0) { |
| 542 | rest = SECTOR_SIZE; |
| 543 | } |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 544 | int ret = mp_spiflash_cached_write_part(self, addr, rest, src); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 545 | if (ret != 0) { |
| 546 | mp_spiflash_release_bus(self); |
| 547 | return ret; |
| 548 | } |
| 549 | src += rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 550 | addr += rest; |
| 551 | pre -= rest; |
| 552 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 553 | src += len_in_buf; |
| 554 | addr += len_in_buf; |
| 555 | |
| 556 | // Fall through to write remaining part |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 557 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 558 | |
| 559 | uint32_t offset = addr & (SECTOR_SIZE - 1); |
| 560 | while (len) { |
| 561 | int rest = SECTOR_SIZE - offset; |
| 562 | if (rest > len) { |
| 563 | rest = len; |
| 564 | } |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 565 | int ret = mp_spiflash_cached_write_part(self, addr, rest, src); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 566 | if (ret != 0) { |
| 567 | mp_spiflash_release_bus(self); |
| 568 | return ret; |
| 569 | } |
| 570 | len -= rest; |
| 571 | addr += rest; |
| 572 | src += rest; |
| 573 | offset = 0; |
| 574 | } |
| 575 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 576 | mp_spiflash_release_bus(self); |
| 577 | return 0; |
| 578 | } |
Damien George | e43a74a | 2020-12-17 16:59:54 +1100 | [diff] [blame] | 579 | |
| 580 | #endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE |