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) { |
| 64 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_ACQUIRE); |
| 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) { |
| 71 | c->bus.u_qspi.proto->ioctl(c->bus.u_qspi.data, MP_QSPI_IOCTL_BUS_RELEASE); |
| 72 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 73 | } |
| 74 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 75 | 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] | 76 | int ret = 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 77 | const mp_spiflash_config_t *c = self->config; |
| 78 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 79 | // Note: len/data are unused for standard SPI |
| 80 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 81 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, 1, &cmd, NULL); |
| 82 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 83 | } else { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 84 | 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] | 85 | } |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 86 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 87 | } |
| 88 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 89 | 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] | 90 | int ret = 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 91 | const mp_spiflash_config_t *c = self->config; |
| 92 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 93 | uint8_t buf[5] = {cmd, 0}; |
| 94 | 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] | 95 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 96 | c->bus.u_spi.proto->transfer(c->bus.u_spi.data, buff_len, buf, NULL); |
| 97 | if (len && (src != NULL)) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 98 | 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] | 99 | } else if (len && (dest != NULL)) { |
| 100 | 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] | 101 | } |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 102 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 103 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
| 104 | } else { |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 105 | if (dest != NULL) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 106 | 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] | 107 | } else { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 108 | 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] | 109 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 110 | } |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 111 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 112 | } |
| 113 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 114 | 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] | 115 | const mp_spiflash_config_t *c = self->config; |
| 116 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 117 | mp_hal_pin_write(c->bus.u_spi.cs, 0); |
| 118 | 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] | 119 | 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] | 120 | mp_hal_pin_write(c->bus.u_spi.cs, 1); |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 121 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 122 | } else { |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 123 | 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] | 124 | } |
| 125 | } |
| 126 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 127 | 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] | 128 | const mp_spiflash_config_t *c = self->config; |
Andrew Leech | 30501d3 | 2020-01-28 14:59:05 +1100 | [diff] [blame] | 129 | uint8_t cmd; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 130 | if (c->bus_kind == MP_SPIFLASH_BUS_SPI) { |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 131 | 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] | 132 | } else { |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 133 | 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] | 134 | } |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 135 | 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] | 136 | } |
| 137 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 138 | 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] | 139 | return mp_spiflash_write_cmd_data(self, cmd, 0, 0); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 140 | } |
| 141 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 142 | 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] | 143 | do { |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 144 | uint32_t sr; |
| 145 | int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr); |
| 146 | if (ret != 0) { |
| 147 | return ret; |
| 148 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 149 | if ((sr & mask) == val) { |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 150 | return 0; // success |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 151 | } |
Andrew Leech | 2ed2ec1 | 2019-01-29 15:20:01 +1100 | [diff] [blame] | 152 | } while (timeout--); |
| 153 | |
| 154 | return -MP_ETIMEDOUT; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 155 | } |
| 156 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 157 | static int mp_spiflash_wait_wel1(mp_spiflash_t *self) { |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 158 | return mp_spiflash_wait_sr(self, 2, 2, WAIT_SR_TIMEOUT); |
| 159 | } |
| 160 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 161 | static int mp_spiflash_wait_wip0(mp_spiflash_t *self) { |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 162 | return mp_spiflash_wait_sr(self, 1, 0, WAIT_SR_TIMEOUT); |
| 163 | } |
| 164 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame] | 165 | static inline void mp_spiflash_deepsleep_internal(mp_spiflash_t *self, int value) { |
| 166 | mp_spiflash_write_cmd(self, value ? 0xb9 : 0xab); // sleep/wake |
| 167 | } |
| 168 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 169 | void mp_spiflash_init(mp_spiflash_t *self) { |
| 170 | self->flags = 0; |
| 171 | |
| 172 | if (self->config->bus_kind == MP_SPIFLASH_BUS_SPI) { |
| 173 | mp_hal_pin_write(self->config->bus.u_spi.cs, 1); |
| 174 | mp_hal_pin_output(self->config->bus.u_spi.cs); |
Damien George | a739b35 | 2018-03-09 17:32:28 +1100 | [diff] [blame] | 175 | 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] | 176 | } else { |
| 177 | self->config->bus.u_qspi.proto->ioctl(self->config->bus.u_qspi.data, MP_QSPI_IOCTL_INIT); |
| 178 | } |
| 179 | |
| 180 | mp_spiflash_acquire_bus(self); |
| 181 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame] | 182 | // Ensure SPI flash is out of sleep mode |
| 183 | mp_spiflash_deepsleep_internal(self, 0); |
| 184 | |
iabdalkader | b5e80fa | 2024-11-29 16:56:20 +0100 | [diff] [blame^] | 185 | // Software reset. |
| 186 | #if MICROPY_HW_SPIFLASH_SOFT_RESET |
| 187 | mp_spiflash_write_cmd(self, CMD_RSTEN); |
| 188 | mp_spiflash_write_cmd(self, CMD_RESET); |
| 189 | mp_spiflash_wait_wip0(self); |
| 190 | mp_hal_delay_ms(1); |
| 191 | #endif |
| 192 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 193 | #if defined(CHECK_DEVID) |
| 194 | // Validate device id |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 195 | uint32_t devid; |
| 196 | int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid); |
| 197 | if (ret != 0 || devid != CHECK_DEVID) { |
| 198 | mp_spiflash_release_bus(self); |
| 199 | return; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 200 | } |
| 201 | #endif |
| 202 | |
| 203 | if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) { |
| 204 | // Set QE bit |
Damien George | b042fd5 | 2022-12-09 12:28:54 +1100 | [diff] [blame] | 205 | uint32_t sr = 0, cr = 0; |
| 206 | int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr); |
| 207 | if (ret == 0) { |
| 208 | ret = mp_spiflash_read_cmd(self, CMD_RDCR, 1, &cr); |
| 209 | } |
| 210 | uint32_t data = (sr & 0xff) | (cr & 0xff) << 8; |
| 211 | if (ret == 0 && !(data & (QSPI_QE_MASK << 8))) { |
Damien George | cc34b08 | 2018-03-11 11:25:38 +1100 | [diff] [blame] | 212 | data |= QSPI_QE_MASK << 8; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 213 | mp_spiflash_write_cmd(self, CMD_WREN); |
| 214 | mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data); |
| 215 | mp_spiflash_wait_wip0(self); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | mp_spiflash_release_bus(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 220 | } |
| 221 | |
Damien George | 8cde5fa | 2019-07-03 01:03:25 +1000 | [diff] [blame] | 222 | void mp_spiflash_deepsleep(mp_spiflash_t *self, int value) { |
| 223 | if (value) { |
| 224 | mp_spiflash_acquire_bus(self); |
| 225 | } |
| 226 | mp_spiflash_deepsleep_internal(self, value); |
| 227 | if (!value) { |
| 228 | mp_spiflash_release_bus(self); |
| 229 | } |
| 230 | } |
| 231 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 232 | 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] | 233 | int ret = 0; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 234 | // enable writes |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 235 | ret = mp_spiflash_write_cmd(self, CMD_WREN); |
| 236 | if (ret != 0) { |
| 237 | return ret; |
| 238 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 239 | |
| 240 | // wait WEL=1 |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 241 | ret = mp_spiflash_wait_wel1(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 242 | if (ret != 0) { |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | // erase the sector |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 247 | 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] | 248 | ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, 0, NULL, NULL); |
| 249 | if (ret != 0) { |
| 250 | return ret; |
| 251 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 252 | |
| 253 | // wait WIP=0 |
| 254 | return mp_spiflash_wait_wip0(self); |
| 255 | } |
| 256 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 257 | 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] | 258 | int ret = 0; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 259 | // enable writes |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 260 | ret = mp_spiflash_write_cmd(self, CMD_WREN); |
| 261 | if (ret != 0) { |
| 262 | return ret; |
| 263 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 264 | |
| 265 | // wait WEL=1 |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 266 | ret = mp_spiflash_wait_wel1(self); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 267 | if (ret != 0) { |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | // write the page |
Damien George | 54f1694 | 2022-06-01 18:50:43 +1000 | [diff] [blame] | 272 | 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] | 273 | ret = mp_spiflash_transfer_cmd_addr_data(self, cmd, addr, len, src, NULL); |
| 274 | if (ret != 0) { |
| 275 | return ret; |
| 276 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 277 | |
| 278 | // wait WIP=0 |
| 279 | return mp_spiflash_wait_wip0(self); |
| 280 | } |
| 281 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 282 | /******************************************************************************/ |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 283 | // Interface functions that go direct to the SPI flash device |
| 284 | |
| 285 | int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr) { |
| 286 | mp_spiflash_acquire_bus(self); |
| 287 | int ret = mp_spiflash_erase_block_internal(self, addr); |
| 288 | mp_spiflash_release_bus(self); |
| 289 | return ret; |
| 290 | } |
| 291 | |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 292 | 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] | 293 | if (len == 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 294 | return 0; |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 295 | } |
| 296 | mp_spiflash_acquire_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 297 | int ret = mp_spiflash_read_data(self, addr, len, dest); |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 298 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 299 | return ret; |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | int mp_spiflash_write(mp_spiflash_t *self, uint32_t addr, size_t len, const uint8_t *src) { |
| 303 | mp_spiflash_acquire_bus(self); |
| 304 | int ret = 0; |
| 305 | uint32_t offset = addr & (PAGE_SIZE - 1); |
| 306 | while (len) { |
| 307 | size_t rest = PAGE_SIZE - offset; |
| 308 | if (rest > len) { |
| 309 | rest = len; |
| 310 | } |
| 311 | ret = mp_spiflash_write_page(self, addr, rest, src); |
| 312 | if (ret != 0) { |
| 313 | break; |
| 314 | } |
| 315 | len -= rest; |
| 316 | addr += rest; |
| 317 | src += rest; |
| 318 | offset = 0; |
| 319 | } |
| 320 | mp_spiflash_release_bus(self); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | /******************************************************************************/ |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 325 | // Interface functions that use the cache |
| 326 | |
Damien George | e43a74a | 2020-12-17 16:59:54 +1100 | [diff] [blame] | 327 | #if MICROPY_HW_SPIFLASH_ENABLE_CACHE |
| 328 | |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 329 | 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] | 330 | if (len == 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 331 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 332 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 333 | mp_spiflash_acquire_bus(self); |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 334 | mp_spiflash_cache_t *cache = self->config->cache; |
| 335 | if (cache->user == self && cache->block != 0xffffffff) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 336 | uint32_t bis = addr / SECTOR_SIZE; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 337 | uint32_t bie = (addr + len - 1) / SECTOR_SIZE; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 338 | if (bis <= cache->block && cache->block <= bie) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 339 | // Read straddles current buffer |
| 340 | size_t rest = 0; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 341 | if (bis < cache->block) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 342 | // Read direct from flash for first part |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 343 | rest = cache->block * SECTOR_SIZE - addr; |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 344 | int ret = mp_spiflash_read_data(self, addr, rest, dest); |
| 345 | if (ret != 0) { |
| 346 | mp_spiflash_release_bus(self); |
| 347 | return ret; |
| 348 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 349 | len -= rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 350 | dest += rest; |
| 351 | addr += rest; |
| 352 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 353 | uint32_t offset = addr & (SECTOR_SIZE - 1); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 354 | rest = SECTOR_SIZE - offset; |
| 355 | if (rest > len) { |
| 356 | rest = len; |
| 357 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 358 | memcpy(dest, &cache->buf[offset], rest); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 359 | len -= rest; |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 360 | if (len == 0) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 361 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 362 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 363 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 364 | dest += rest; |
| 365 | addr += rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 366 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 367 | } |
| 368 | // Read rest direct from flash |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 369 | int ret = mp_spiflash_read_data(self, addr, len, dest); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 370 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 371 | return ret; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 372 | } |
| 373 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 374 | static int mp_spiflash_cache_flush_internal(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 375 | #if USE_WR_DELAY |
| 376 | if (!(self->flags & 1)) { |
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 | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 379 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 380 | self->flags &= ~1; |
| 381 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 382 | mp_spiflash_cache_t *cache = self->config->cache; |
| 383 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 384 | // Erase sector |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 385 | int ret = mp_spiflash_erase_block_internal(self, cache->block * SECTOR_SIZE); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 386 | if (ret != 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 387 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Write |
| 391 | for (int i = 0; i < 16; i += 1) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 392 | uint32_t addr = cache->block * SECTOR_SIZE + i * PAGE_SIZE; |
| 393 | 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] | 394 | if (ret != 0) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 395 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | #endif |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 399 | return 0; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 400 | } |
| 401 | |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 402 | int mp_spiflash_cache_flush(mp_spiflash_t *self) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 403 | mp_spiflash_acquire_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 404 | int ret = mp_spiflash_cache_flush_internal(self); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 405 | mp_spiflash_release_bus(self); |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 406 | return ret; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 407 | } |
| 408 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 409 | 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] | 410 | // Align to 4096 sector |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 411 | uint32_t offset = addr & 0xfff; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 412 | uint32_t sec = addr >> 12; |
| 413 | addr = sec << 12; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 414 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 415 | // Restriction for now, so we don't need to erase multiple pages |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 416 | if (offset + len > SECTOR_SIZE) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 417 | printf("mp_spiflash_cached_write_part: len is too large\n"); |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 418 | return -MP_EIO; |
| 419 | } |
| 420 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 421 | mp_spiflash_cache_t *cache = self->config->cache; |
| 422 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 423 | // Acquire the sector buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 424 | if (cache->user != self) { |
| 425 | if (cache->user != NULL) { |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 426 | mp_spiflash_cache_flush(cache->user); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 427 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 428 | cache->user = self; |
| 429 | cache->block = 0xffffffff; |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 430 | } |
| 431 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 432 | if (cache->block != sec) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 433 | // Read sector |
| 434 | #if USE_WR_DELAY |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 435 | if (cache->block != 0xffffffff) { |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 436 | int ret = mp_spiflash_cache_flush_internal(self); |
| 437 | if (ret != 0) { |
| 438 | return ret; |
| 439 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 440 | } |
| 441 | #endif |
Andrew Leech | 7ee5afe | 2021-03-05 10:15:29 +1100 | [diff] [blame] | 442 | int ret = mp_spiflash_read_data(self, addr, SECTOR_SIZE, cache->buf); |
| 443 | if (ret != 0) { |
| 444 | return ret; |
| 445 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 446 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 447 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 448 | #if USE_WR_DELAY |
| 449 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 450 | cache->block = sec; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 451 | // Just copy to buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 452 | memcpy(cache->buf + offset, src, len); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 453 | // And mark dirty |
| 454 | self->flags |= 1; |
| 455 | |
| 456 | #else |
| 457 | |
| 458 | uint32_t dirty = 0; |
| 459 | for (size_t i = 0; i < len; ++i) { |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 460 | if (cache->buf[offset + i] != src[i]) { |
| 461 | if (cache->buf[offset + i] != 0xff) { |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 462 | // Erase sector |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 463 | int ret = mp_spiflash_erase_block_internal(self, addr); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 464 | if (ret != 0) { |
| 465 | return ret; |
| 466 | } |
| 467 | dirty = 0xffff; |
| 468 | break; |
| 469 | } else { |
| 470 | dirty |= (1 << ((offset + i) >> 8)); |
| 471 | } |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 475 | cache->block = sec; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 476 | // Copy new block into buffer |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 477 | memcpy(cache->buf + offset, src, len); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 478 | |
| 479 | // Write sector in pages of 256 bytes |
| 480 | for (size_t i = 0; i < 16; ++i) { |
| 481 | if (dirty & (1 << i)) { |
Damien George | b78ca32 | 2018-06-07 15:39:46 +1000 | [diff] [blame] | 482 | 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] | 483 | if (ret != 0) { |
| 484 | return ret; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | #endif |
| 490 | |
Damien George | 784e023 | 2017-01-24 16:56:03 +1100 | [diff] [blame] | 491 | return 0; // success |
| 492 | } |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 493 | |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 494 | 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] | 495 | uint32_t bis = addr / SECTOR_SIZE; |
| 496 | uint32_t bie = (addr + len - 1) / SECTOR_SIZE; |
| 497 | |
| 498 | mp_spiflash_acquire_bus(self); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 499 | |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 500 | mp_spiflash_cache_t *cache = self->config->cache; |
| 501 | if (cache->user == self && bis <= cache->block && bie >= cache->block) { |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 502 | // Write straddles current buffer |
| 503 | uint32_t pre; |
| 504 | uint32_t offset; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 505 | if (cache->block * SECTOR_SIZE >= addr) { |
| 506 | pre = cache->block * SECTOR_SIZE - addr; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 507 | offset = 0; |
| 508 | } else { |
| 509 | pre = 0; |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 510 | offset = addr - cache->block * SECTOR_SIZE; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 511 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 512 | |
| 513 | // Write buffered part first |
| 514 | uint32_t len_in_buf = len - pre; |
| 515 | len = 0; |
| 516 | if (len_in_buf > SECTOR_SIZE - offset) { |
| 517 | len = len_in_buf - (SECTOR_SIZE - offset); |
| 518 | len_in_buf = SECTOR_SIZE - offset; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 519 | } |
Damien George | 86fe73b | 2018-06-07 14:09:10 +1000 | [diff] [blame] | 520 | memcpy(&cache->buf[offset], &src[pre], len_in_buf); |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 521 | self->flags |= 1; // Mark dirty |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 522 | |
| 523 | // Write part before buffer sector |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 524 | while (pre) { |
| 525 | int rest = pre & (SECTOR_SIZE - 1); |
| 526 | if (rest == 0) { |
| 527 | rest = SECTOR_SIZE; |
| 528 | } |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 529 | int ret = mp_spiflash_cached_write_part(self, addr, rest, src); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 530 | if (ret != 0) { |
| 531 | mp_spiflash_release_bus(self); |
| 532 | return ret; |
| 533 | } |
| 534 | src += rest; |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 535 | addr += rest; |
| 536 | pre -= rest; |
| 537 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 538 | src += len_in_buf; |
| 539 | addr += len_in_buf; |
| 540 | |
| 541 | // Fall through to write remaining part |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 542 | } |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 543 | |
| 544 | uint32_t offset = addr & (SECTOR_SIZE - 1); |
| 545 | while (len) { |
| 546 | int rest = SECTOR_SIZE - offset; |
| 547 | if (rest > len) { |
| 548 | rest = len; |
| 549 | } |
Damien George | cc5a940 | 2018-06-07 15:36:27 +1000 | [diff] [blame] | 550 | int ret = mp_spiflash_cached_write_part(self, addr, rest, src); |
Damien George | bdc875e | 2018-03-13 14:13:30 +1100 | [diff] [blame] | 551 | if (ret != 0) { |
| 552 | mp_spiflash_release_bus(self); |
| 553 | return ret; |
| 554 | } |
| 555 | len -= rest; |
| 556 | addr += rest; |
| 557 | src += rest; |
| 558 | offset = 0; |
| 559 | } |
| 560 | |
Damien George | 4e48700 | 2018-03-02 16:01:18 +1100 | [diff] [blame] | 561 | mp_spiflash_release_bus(self); |
| 562 | return 0; |
| 563 | } |
Damien George | e43a74a | 2020-12-17 16:59:54 +1100 | [diff] [blame] | 564 | |
| 565 | #endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE |