From 806bef7fcfa0b914842bd917aeced001c0123a35 Mon Sep 17 00:00:00 2001 From: Joy Date: Tue, 7 Dec 2021 11:07:56 +0800 Subject: [PATCH 01/37] Added external spi flash driver. --- drivers/flash/flash_spi.c | 378 ++++++++++++++++++++++++++++++++++++++ drivers/flash/flash_spi.h | 134 ++++++++++++++ 2 files changed, 512 insertions(+) create mode 100644 drivers/flash/flash_spi.c create mode 100644 drivers/flash/flash_spi.h diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c new file mode 100644 index 000000000000..dda79613370f --- /dev/null +++ b/drivers/flash/flash_spi.c @@ -0,0 +1,378 @@ +/* +Copyright (C) 2021 Westberry Technology (ChangZhou) Corp., Ltd + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include + +#include "util.h" +#include "wait.h" +#include "debug.h" +#include "timer.h" +#include "flash_spi.h" +#include "spi_master.h" + +/* + The time-out time of spi flash transmission. +*/ +#ifndef EXTERNAL_FLASH_SPI_TIMEOUT +# define EXTERNAL_FLASH_SPI_TIMEOUT 1000 +#endif + +/* ID comands */ +#define FLASH_CMD_RDID 0x9F /* RDID (Read Identification) */ +#define FLASH_CMD_RES 0xAB /* RES (Read Electronic ID) */ +#define FLASH_CMD_REMS 0x90 /* REMS (Read Electronic & Device ID) */ + +/* register comands */ +#define FLASH_CMD_WRSR 0x01 /* WRSR (Write Status register) */ +#define FLASH_CMD_RDSR 0x05 /* RDSR (Read Status register) */ + +/* READ comands */ +#define FLASH_CMD_READ 0x03 /* READ (1 x I/O) */ +#define FLASH_CMD_FASTREAD 0x0B /* FAST READ (Fast read data) */ +#define FLASH_CMD_DREAD 0x3B /* DREAD (1In/2 Out fast read) */ + +/* Program comands */ +#define FLASH_CMD_WREN 0x06 /* WREN (Write Enable) */ +#define FLASH_CMD_WRDI 0x04 /* WRDI (Write Disable) */ +#define FLASH_CMD_PP 0x02 /* PP (page program) */ + +/* Erase comands */ +#define FLASH_CMD_SE 0x20 /* SE (Sector Erase) */ +#define FLASH_CMD_BE 0xD8 /* BE (Block Erase) */ +#define FLASH_CMD_CE 0x60 /* CE (Chip Erase) hex code: 60 or C7 */ + +/* Mode setting comands */ +#define FLASH_CMD_DP 0xB9 /* DP (Deep Power Down) */ +#define FLASH_CMD_RDP 0xAB /* RDP (Release form Deep Power Down) */ + +/* Status register */ +#define FLASH_FLAG_WIP 0x01 /* Write in progress bit */ +#define FLASH_FLAG_WEL 0x02 /* Write enable latch bit */ + +// #define DEBUG_FLASH_SPI_OUTPUT + +static bool spi_flash_start(void) { return spi_start(EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN, EXTERNAL_FLASH_SPI_LSBFIRST, EXTERNAL_FLASH_SPI_MODE, EXTERNAL_FLASH_SPI_CLOCK_DIVISOR); } + +static flash_status_t spi_flash_wait_while_busy(void) { + uint32_t deadline = timer_read32() + EXTERNAL_FLASH_SPI_TIMEOUT; + flash_status_t response = FLASH_STATUS_SUCCESS; + uint8_t retval; + + do { + bool res = spi_flash_start(); + if (!res) { + dprint("Failed to start SPI! [spi flash wait while busy]\n"); + return FLASH_STATUS_ERROR; + } + + spi_write(FLASH_CMD_RDSR); + + retval = (uint8_t)spi_read(); + + spi_stop(); + + if (timer_read32() >= deadline) { + response = FLASH_STATUS_TIMEOUT; + break; + } + } while (retval & FLASH_FLAG_WIP); + + return response; +} + +static flash_status_t spi_flash_write_enable(void) { + + bool res = spi_flash_start(); + if (!res) { + dprint("Failed to start SPI! [spi flash write enable]\n"); + return FLASH_STATUS_ERROR; + } + + spi_write(FLASH_CMD_WREN); + + spi_stop(); + + return FLASH_STATUS_SUCCESS; +} + +static flash_status_t spi_flash_write_disable(void) { + + bool res = spi_flash_start(); + if (!res) { + dprint("Failed to start SPI! [spi flash write disable]\n"); + return FLASH_STATUS_ERROR; + } + + spi_write(FLASH_CMD_WRDI); + + spi_stop(); + + return FLASH_STATUS_SUCCESS; +} + +/* This function is used for read transfer, write transfer and erase transfer. */ +static flash_status_t spi_flash_transmit(uint8_t cmd, uintptr_t addr, uint8_t *data, size_t len) { + flash_status_t response = FLASH_STATUS_SUCCESS; + uint8_t buffer[EXTERNAL_FLASH_ADDRESS_SIZE + 1]; + + buffer[0] = cmd; + for (int i = 0; i < EXTERNAL_FLASH_ADDRESS_SIZE; ++i) { + buffer[EXTERNAL_FLASH_ADDRESS_SIZE - i] = addr & 0xFF; + addr >>= 8; + } + + bool res = spi_flash_start(); + if (!res) { + dprint("Failed to start SPI! [spi flash transmit]\n"); + return FLASH_STATUS_ERROR; + } + + response = spi_transmit(buffer, sizeof(buffer)); + + if ((!response) && (data != NULL)) { + switch (cmd) { + case FLASH_CMD_READ: + response = spi_receive(data, len); + break; + case FLASH_CMD_PP: + response = spi_transmit(data, len); + break; + default: + response = FLASH_STATUS_ERROR; + break; + } + } + + spi_stop(); + + return response; +} + +flash_status_t flash_erase_chip(void) { + flash_status_t response = FLASH_STATUS_SUCCESS; + + /* Wait for the write-in-progress bit to be cleared. */ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash erase chip]\n"); + return response; + } + + /* Enable writes. */ + response = spi_flash_write_enable(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to write-enable! [spi flash erase chip]\n"); + return response; + } + + /* Erase Chip. */ + bool res = spi_flash_start(); + if (!res) { + dprint("Failed to start SPI! [spi flash erase chip]\n"); + return FLASH_STATUS_ERROR; + } + spi_write(FLASH_CMD_CE); + spi_stop(); + + /* Wait for the write-in-progress bit to be cleared.*/ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash erase chip]\n"); + return response; + } + + return response; +} + +flash_status_t flash_erase_sector(const void *addr) { + flash_status_t response = FLASH_STATUS_SUCCESS; + uintptr_t target_addr = (uintptr_t)addr; + + /* Check that the address exceeds the limit. */ + if ((target_addr + EXTERNAL_FLASH_SECTOR_SIZE << 10) >= (EXTERNAL_FLASH_SIZE << 10) || + ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE << 10)) != 0)) { + dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)target_addr); + return FLASH_STATUS_ERROR; + } + + /* Wait for the write-in-progress bit to be cleared. */ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash erase sector]\n"); + return response; + } + + /* Enable writes. */ + response = spi_flash_write_enable(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to write-enable! [spi flash erase sector]\n"); + return response; + } + + /* Erase Sector. */ + response = spi_flash_transmit((uint8_t)FLASH_CMD_SE, target_addr, NULL, 0); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to erase sector! [spi flash erase sector]\n"); + return response; + } + + /* Wait for the write-in-progress bit to be cleared.*/ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash erase sector]\n"); + return response; + } + + return response; +} + +flash_status_t flash_erase_block(const void *addr) { + flash_status_t response = FLASH_STATUS_SUCCESS; + uintptr_t target_addr = (uintptr_t)addr; + + /* Check that the address exceeds the limit. */ + if ((target_addr + EXTERNAL_FLASH_BLOCK_SIZE << 10) >= (EXTERNAL_FLASH_SIZE << 10) || + ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE << 10)) != 0)) { + dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)target_addr); + return FLASH_STATUS_ERROR; + } + + /* Wait for the write-in-progress bit to be cleared. */ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash erase block]\n"); + return response; + } + + /* Enable writes. */ + response = spi_flash_write_enable(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to write-enable! [spi flash erase block]\n"); + return response; + } + + /* Erase Block. */ + response = spi_flash_transmit((uint8_t)FLASH_CMD_BE, target_addr, NULL, 0); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to erase block! [spi flash erase block]\n"); + return response; + } + + /* Wait for the write-in-progress bit to be cleared.*/ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash erase block]\n"); + return response; + } + + return response; +} + +flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len) { + flash_status_t response = FLASH_STATUS_SUCCESS; + uint8_t * read_buf = (uint8_t *)buf; + uintptr_t target_addr = (uintptr_t)addr; + + /* Wait for the write-in-progress bit to be cleared. */ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash read block]\n"); + memset(read_buf, 0, len); + return response; + } + + /* Perform read. */ + response = spi_flash_transmit((uint8_t)FLASH_CMD_READ, (uintptr_t)target_addr, read_buf, len); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to read block! [spi flash read block]\n"); + memset(read_buf, 0, len); + return response; + } + +#if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT) + dprintf("[SPI FLASH R] 0x%08lX: ", ((uint32_t)target_addr)); + for (size_t i = 0; i < len; ++i) { + dprintf(" %02X", (int)(((uint8_t *)read_buf)[i])); + } + dprintf("\n"); +#endif // DEBUG_FLASH_SPI_OUTPUT + + return response; +} + +flash_status_t flash_write_block(const uint8_t *buf, void *addr, size_t len) { + flash_status_t response = FLASH_STATUS_SUCCESS; + uint8_t * write_buf = (uint8_t *)buf; + uintptr_t target_addr = (uintptr_t)addr; + + while (len > 0) { + uintptr_t page_offset = target_addr % EXTERNAL_FLASH_PAGE_SIZE; + size_t write_length = EXTERNAL_FLASH_PAGE_SIZE - page_offset; + if (write_length > len) { + write_length = len; + } + + /* Wait for the write-in-progress bit to be cleared. */ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash write block]\n"); + return response; + } + + /* Enable writes. */ + response = spi_flash_write_enable(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to write-enable! [spi flash write block]\n"); + return response; + } + +#if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT) + dprintf("[SPI FLASH W] 0x%08lX: ", ((uint32_t)target_addr)); + for (size_t i = 0; i < write_length; i++) { + dprintf(" %02X", (int)(uint8_t)(write_buf[i])); + } + dprintf("\n"); +#endif // DEBUG_FLASH_SPI_OUTPUT + + /* Perform the write. */ + response = spi_flash_transmit((uint8_t)FLASH_CMD_PP, target_addr, write_buf, write_length); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to write block! [spi flash write block]\n"); + return response; + } + + write_buf += write_length; + target_addr += write_length; + len -= write_length; + } + + /* Wait for the write-in-progress bit to be cleared. */ + response= spi_flash_wait_while_busy(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to check WIP flag! [spi flash write block]\n"); + return response; + } + + /* Disable writes. */ + response= spi_flash_write_disable(); + if (response != FLASH_STATUS_SUCCESS) { + dprint("Failed to write-disable! [spi flash write block]\n"); + return response; + } + + return response; +} diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h new file mode 100644 index 000000000000..b1084439c08c --- /dev/null +++ b/drivers/flash/flash_spi.h @@ -0,0 +1,134 @@ +/* +Copyright (C) 2021 Westberry Technology (ChangZhou) Corp., Ltd + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#pragma once + +/* All the following default configurations are based on MX25L4006E Nor FLASH. */ + +/* + The slave select pin of the FLASH. + This needs to be a normal GPIO pin_t value, such as B14. +*/ +#ifndef EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN +# error "No chip select pin defined -- missing EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN" +#endif + +/* + The clock divisor for SPI to ensure that the MCU is within the + specifications of the FLASH chip. Generally this will be PCLK divided by + the intended divisor -- check your clock settings and the datasheet of + your FLASH. +*/ +#ifndef EXTERNAL_FLASH_SPI_CLOCK_DIVISOR +# ifdef __AVR__ +# define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 8 +# else +# define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 18 +# endif +#endif + +/* + The SPI mode to communicate with the FLASH. +*/ +#ifndef EXTERNAL_FLASH_SPI_MODE +# define EXTERNAL_FLASH_SPI_MODE 0 +#endif + +/* + Whether or not the SPI communication between the MCU and FLASH should be + LSB-first. +*/ +#ifndef EXTERNAL_FLASH_SPI_LSBFIRST +# define EXTERNAL_FLASH_SPI_LSBFIRST false +#endif + +/* + Flash address size, as specified in datasheet, in bytes. +*/ +#ifndef EXTERNAL_FLASH_ADDRESS_SIZE +# define EXTERNAL_FLASH_ADDRESS_SIZE 3 +#endif + +/* + The page size in bytes of the FLASH, as specified in the datasheet. +*/ +#ifndef EXTERNAL_FLASH_PAGE_SIZE +# define EXTERNAL_FLASH_PAGE_SIZE 256 +#endif + +/* + The sector size in Kbytes of the FLASH, as specified in the datasheet. +*/ +#ifndef EXTERNAL_FLASH_SECTOR_SIZE +# define EXTERNAL_FLASH_SECTOR_SIZE 4 +#endif + +/* + The block size in Kbytes of the FLASH, as specified in the datasheet. +*/ +#ifndef EXTERNAL_FLASH_BLOCK_SIZE +# define EXTERNAL_FLASH_BLOCK_SIZE 64 +#endif + +/* + The total size of the FLASH, in Kbytes. The FLASH datasheet will usually + specify this value in kbits, and will require conversion to bytes. +*/ +#ifndef EXTERNAL_FLASH_SIZE +# define EXTERNAL_FLASH_SIZE 512 +#endif + +/* + The block count of the FLASH, calculated by total FLASH size and block size. +*/ +#define EXTERNAL_FLASH_BLOCK_COUNT (EXTERNAL_FLASH_SIZE / EXTERNAL_FLASH_BLOCK_SIZE) + +/* + The sector count of the FLASH, calculated by total FLASH size and sector size. +*/ +#define EXTERNAL_FLASH_SECTOR_COUNT (EXTERNAL_FLASH_SIZE / EXTERNAL_FLASH_SECTOR_SIZE) + +/* + The page count of the FLASH, calculated by total FLASH size and page size. +*/ +#define EXTERNAL_FLASH_PAGE_COUNT ((EXTERNAL_FLASH_SIZE << 10) / EXTERNAL_FLASH_PAGE_SIZE) + +typedef int16_t flash_status_t; + +#define FLASH_STATUS_SUCCESS (0) +#define FLASH_STATUS_ERROR (-1) +#define FLASH_STATUS_TIMEOUT (-2) + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +flash_status_t flash_erase_chip(void); + +flash_status_t flash_erase_block(const void *addr); + +flash_status_t flash_erase_sector(const void *addr); + +flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len); + +flash_status_t flash_write_block(const uint8_t *buf, void *addr, size_t len); + +#ifdef __cplusplus +} +#endif From d8dd74d825feb652fd3a596caac3a9c0e3e9ebab Mon Sep 17 00:00:00 2001 From: Joy Date: Tue, 7 Dec 2021 13:48:21 +0800 Subject: [PATCH 02/37] Added document description of SPI Flash configuration parameters. --- docs/_summary.md | 1 + docs/flash_driver.md | 15 +++++++++++++++ docs/ja/_summary.md | 1 + docs/zh-cn/_summary.md | 1 + drivers/flash/flash_spi.h | 11 +++++------ 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 docs/flash_driver.md diff --git a/docs/_summary.md b/docs/_summary.md index e26d9ae21061..ddf574716cc8 100644 --- a/docs/_summary.md +++ b/docs/_summary.md @@ -144,6 +144,7 @@ * [SPI Driver](spi_driver.md) * [WS2812 Driver](ws2812_driver.md) * [EEPROM Driver](eeprom_driver.md) + * [FLASH Driver](flash_driver.md) * ['serial' Driver](serial_driver.md) * [UART Driver](uart_driver.md) * [GPIO Controls](internals_gpio_control.md) diff --git a/docs/flash_driver.md b/docs/flash_driver.md new file mode 100644 index 000000000000..bf45372f1dfb --- /dev/null +++ b/docs/flash_driver.md @@ -0,0 +1,15 @@ +# SPI FLASH Driver Configuration :id=spi-flash-driver-configuration + +Currently QMK supports almost all NOR Flash chips over SPI. As such, requires a working spi_master driver configuration. You can override the driver configuration via your config.h: + +`config.h` override | Description | Default Value +-----------------------------------------------|--------------------------------------------------------------------------------------|-------------- +`#define EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN` | SPI Slave select pin in order to inform that the FLASH is currently being addressed | _none_ +`#define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR` | Clock divisor used to divide the peripheral clock to derive the SPI frequency | `18` +`#define EXTERNAL_FLASH_PAGE_SIZE` | The Page size of the FLASH in bytes, as specified in the datasheet | 256 +`#define EXTERNAL_FLASH_SECTOR_SIZE` | The sector size of the FLASH in Kbytes, as specified in the datasheet | 4 +`#define EXTERNAL_FLASH_BLOCK_SIZE` | The block size of the FLASH in Kbytes, as specified in the datasheet | 64 +`#define EXTERNAL_FLASH_SIZE` | The total size of the FLASH in Kbytes, as specified in the datasheet | 512 +`#define EXTERNAL_FLASH_ADDRESS_SIZE` | The Flash address size in bytes, as specified in datasheet | 3 + +!> All the above default configurations are based on MX25L4006E NOR Flash. diff --git a/docs/ja/_summary.md b/docs/ja/_summary.md index b90480041c73..ef74f8f248b9 100644 --- a/docs/ja/_summary.md +++ b/docs/ja/_summary.md @@ -135,6 +135,7 @@ * [SPI ドライバ](ja/spi_driver.md) * [WS2812 ドライバ](ja/ws2812_driver.md) * [EEPROM ドライバ](ja/eeprom_driver.md) + * [FLASH ドライバ](ja/flash_driver.md) * [シリアル ドライバ](ja/serial_driver.md) * [UART ドライバ](ja/uart_driver.md) * [GPIO 制御](ja/internals_gpio_control.md) diff --git a/docs/zh-cn/_summary.md b/docs/zh-cn/_summary.md index cedcfbd52574..8c1f221d13da 100644 --- a/docs/zh-cn/_summary.md +++ b/docs/zh-cn/_summary.md @@ -107,6 +107,7 @@ * [SPI设备](zh-cn/spi_driver.md) * [WS2812设备](zh-cn/ws2812_driver.md) * [EEPROM设备](zh-cn/eeprom_driver.md) + * [FLASH设备](zh-cn/flash_driver.md) * [GPIO控制](zh-cn/internals_gpio_control.md) * [自定义键盘矩阵](zh-cn/custom_matrix.md) * [Proton C转换](zh-cn/proton_c_conversion.md) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index b1084439c08c..9e77c6c589b3 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -57,36 +57,35 @@ along with this program. If not, see . #endif /* - Flash address size, as specified in datasheet, in bytes. + The Flash address size in bytes, as specified in datasheet. */ #ifndef EXTERNAL_FLASH_ADDRESS_SIZE # define EXTERNAL_FLASH_ADDRESS_SIZE 3 #endif /* - The page size in bytes of the FLASH, as specified in the datasheet. + The page size of the FLASH in bytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_PAGE_SIZE # define EXTERNAL_FLASH_PAGE_SIZE 256 #endif /* - The sector size in Kbytes of the FLASH, as specified in the datasheet. + The sector size of the FLASH in Kbytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_SECTOR_SIZE # define EXTERNAL_FLASH_SECTOR_SIZE 4 #endif /* - The block size in Kbytes of the FLASH, as specified in the datasheet. + The block size of the FLASH in Kbytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_BLOCK_SIZE # define EXTERNAL_FLASH_BLOCK_SIZE 64 #endif /* - The total size of the FLASH, in Kbytes. The FLASH datasheet will usually - specify this value in kbits, and will require conversion to bytes. + The total size of the FLASH in Kbytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_SIZE # define EXTERNAL_FLASH_SIZE 512 From d6f89b7427ceff77d0d31a58555c025e357ab420 Mon Sep 17 00:00:00 2001 From: Joy Date: Tue, 7 Dec 2021 14:21:10 +0800 Subject: [PATCH 03/37] Added FLASH_DRIVER configuration --- common_features.mk | 12 ++++++++++++ docs/flash_driver.md | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/common_features.mk b/common_features.mk index 56a9a286eaca..ae6cf605af1b 100644 --- a/common_features.mk +++ b/common_features.mk @@ -201,6 +201,18 @@ else endif endif +VALID_FLASH_DRIVER_TYPES := spi +ifeq ($(filter $(FLASH_DRIVER),$(VALID_FLASH_DRIVER_TYPES)),) + $(error FLASH_DRIVER="$(FLASH_DRIVER)" is not a valid FLASH driver) +else + OPT_DEFS += -DFLASH_ENABLE + ifeq ($(strip $(EEPROM_DRIVER)), spi) + OPT_DEFS += -DFLASH_DRIVER -DFLASH_SPI + COMMON_VPATH += $(DRIVER_PATH)/flash + SRC += flash_spi.c + endif +endif + RGBLIGHT_ENABLE ?= no VALID_RGBLIGHT_TYPES := WS2812 APA102 custom diff --git a/docs/flash_driver.md b/docs/flash_driver.md index bf45372f1dfb..c5d40736538f 100644 --- a/docs/flash_driver.md +++ b/docs/flash_driver.md @@ -1,4 +1,13 @@ -# SPI FLASH Driver Configuration :id=spi-flash-driver-configuration +# FLASH Driver Configuration :id=flash-driver-configuration + +The FLASH driver can be swapped out depending on the needs of the keyboard, or whether extra hardware is present. + +Driver | Description +-----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +`FLASH_DRIVER = spi` | Supports writing to almost all NOR Flash chips. See the driver section below. + + +## SPI FLASH Driver Configuration :id=spi-flash-driver-configuration Currently QMK supports almost all NOR Flash chips over SPI. As such, requires a working spi_master driver configuration. You can override the driver configuration via your config.h: From 72ba901415ddc2faf718838ada4a857e6bf8cb8d Mon Sep 17 00:00:00 2001 From: Joy Date: Tue, 7 Dec 2021 15:05:38 +0800 Subject: [PATCH 04/37] Modified FLASH_DRIVER configuration. --- common_features.mk | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/common_features.mk b/common_features.mk index ae6cf605af1b..17835ed5a8e0 100644 --- a/common_features.mk +++ b/common_features.mk @@ -202,15 +202,18 @@ else endif VALID_FLASH_DRIVER_TYPES := spi -ifeq ($(filter $(FLASH_DRIVER),$(VALID_FLASH_DRIVER_TYPES)),) - $(error FLASH_DRIVER="$(FLASH_DRIVER)" is not a valid FLASH driver) -else - OPT_DEFS += -DFLASH_ENABLE - ifeq ($(strip $(EEPROM_DRIVER)), spi) - OPT_DEFS += -DFLASH_DRIVER -DFLASH_SPI - COMMON_VPATH += $(DRIVER_PATH)/flash - SRC += flash_spi.c - endif +FLASH_DRIVER ?= no +ifneq ($(strip $(FLASH_DRIVER)), no) + ifeq ($(filter $(FLASH_DRIVER),$(VALID_FLASH_DRIVER_TYPES)),) + $(error FLASH_DRIVER="$(FLASH_DRIVER)" is not a valid FLASH driver) + else + OPT_DEFS += -DFLASH_ENABLE + ifeq ($(strip $(EEPROM_DRIVER)), spi) + OPT_DEFS += -DFLASH_DRIVER -DFLASH_SPI + COMMON_VPATH += $(DRIVER_PATH)/flash + SRC += flash_spi.c + endif + endif endif RGBLIGHT_ENABLE ?= no From 41e3a4d6ab75fcd89f64060b38e450279f62abd5 Mon Sep 17 00:00:00 2001 From: Joy Date: Fri, 17 Dec 2021 17:13:09 +0800 Subject: [PATCH 05/37] Update --- drivers/flash/flash_spi.c | 8 ++++++-- drivers/flash/flash_spi.h | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index dda79613370f..e6278b1a63b9 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -162,6 +162,10 @@ static flash_status_t spi_flash_transmit(uint8_t cmd, uintptr_t addr, uint8_t *d return response; } +void flash_init(void) { + spi_init(); +} + flash_status_t flash_erase_chip(void) { flash_status_t response = FLASH_STATUS_SUCCESS; @@ -203,7 +207,7 @@ flash_status_t flash_erase_sector(const void *addr) { uintptr_t target_addr = (uintptr_t)addr; /* Check that the address exceeds the limit. */ - if ((target_addr + EXTERNAL_FLASH_SECTOR_SIZE << 10) >= (EXTERNAL_FLASH_SIZE << 10) || + if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE << 10)) >= (EXTERNAL_FLASH_SIZE << 10) || ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE << 10)) != 0)) { dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)target_addr); return FLASH_STATUS_ERROR; @@ -245,7 +249,7 @@ flash_status_t flash_erase_block(const void *addr) { uintptr_t target_addr = (uintptr_t)addr; /* Check that the address exceeds the limit. */ - if ((target_addr + EXTERNAL_FLASH_BLOCK_SIZE << 10) >= (EXTERNAL_FLASH_SIZE << 10) || + if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE << 10)) >= (EXTERNAL_FLASH_SIZE << 10) || ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE << 10)) != 0)) { dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)target_addr); return FLASH_STATUS_ERROR; diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index 9e77c6c589b3..c8d9bb0f6b49 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -111,6 +111,7 @@ typedef int16_t flash_status_t; #define FLASH_STATUS_SUCCESS (0) #define FLASH_STATUS_ERROR (-1) #define FLASH_STATUS_TIMEOUT (-2) +#define FLASH_STATUS_BAD_ADDRESS (-3) #ifdef __cplusplus extern "C" { @@ -118,6 +119,8 @@ extern "C" { #include +void flash_init(void); + flash_status_t flash_erase_chip(void); flash_status_t flash_erase_block(const void *addr); From 69e167f5315b7760c68c19132c56fc449862f0ed Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:06:21 +0800 Subject: [PATCH 06/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index c8d9bb0f6b49..e8652234cb16 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -35,9 +35,9 @@ along with this program. If not, see . */ #ifndef EXTERNAL_FLASH_SPI_CLOCK_DIVISOR # ifdef __AVR__ -# define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 8 +# define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 4 # else -# define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 18 +# define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 8 # endif #endif From bc72ce27d9c1c3ed8e3c49e05c0b5560817ea185 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:06:46 +0800 Subject: [PATCH 07/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index e6278b1a63b9..f89b29addca2 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -125,7 +125,7 @@ static flash_status_t spi_flash_write_disable(void) { } /* This function is used for read transfer, write transfer and erase transfer. */ -static flash_status_t spi_flash_transmit(uint8_t cmd, uintptr_t addr, uint8_t *data, size_t len) { +static flash_status_t spi_flash_transaction(uint8_t cmd, uint32_t addr, uint8_t *data, size_t len) { flash_status_t response = FLASH_STATUS_SUCCESS; uint8_t buffer[EXTERNAL_FLASH_ADDRESS_SIZE + 1]; From 1211426f52d024d1e397f28aca429d091dd15bbf Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:07:00 +0800 Subject: [PATCH 08/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index f89b29addca2..dee1f0b3f245 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -202,7 +202,7 @@ flash_status_t flash_erase_chip(void) { return response; } -flash_status_t flash_erase_sector(const void *addr) { +flash_status_t flash_erase_sector(uint32_t addr) { flash_status_t response = FLASH_STATUS_SUCCESS; uintptr_t target_addr = (uintptr_t)addr; From 8e7c2aeb3c669ad52528252c5c85653ffd66f166 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:07:19 +0800 Subject: [PATCH 09/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index e8652234cb16..b6ce81483d64 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -104,7 +104,7 @@ along with this program. If not, see . /* The page count of the FLASH, calculated by total FLASH size and page size. */ -#define EXTERNAL_FLASH_PAGE_COUNT ((EXTERNAL_FLASH_SIZE << 10) / EXTERNAL_FLASH_PAGE_SIZE) +#define EXTERNAL_FLASH_PAGE_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_PAGE_SIZE)) typedef int16_t flash_status_t; From 4d510736a5f98272b2d87caeed5cf90b6f5503ff Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:07:33 +0800 Subject: [PATCH 10/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index b6ce81483d64..4a0d95123020 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -99,7 +99,7 @@ along with this program. If not, see . /* The sector count of the FLASH, calculated by total FLASH size and sector size. */ -#define EXTERNAL_FLASH_SECTOR_COUNT (EXTERNAL_FLASH_SIZE / EXTERNAL_FLASH_SECTOR_SIZE) +#define EXTERNAL_FLASH_SECTOR_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_SECTOR_SIZE)) /* The page count of the FLASH, calculated by total FLASH size and page size. From 762ba455b68c13fb5e9991a2df61f2020f63b397 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:07:38 +0800 Subject: [PATCH 11/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index 4a0d95123020..d1f82784ae7d 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -94,7 +94,7 @@ along with this program. If not, see . /* The block count of the FLASH, calculated by total FLASH size and block size. */ -#define EXTERNAL_FLASH_BLOCK_COUNT (EXTERNAL_FLASH_SIZE / EXTERNAL_FLASH_BLOCK_SIZE) +#define EXTERNAL_FLASH_BLOCK_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_BLOCK_SIZE)) /* The sector count of the FLASH, calculated by total FLASH size and sector size. From 9c5b1981ecf886444ea2380f82b9a2ba9481a608 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:08:01 +0800 Subject: [PATCH 12/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index dee1f0b3f245..3223cd8a8f34 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -207,8 +207,8 @@ flash_status_t flash_erase_sector(uint32_t addr) { uintptr_t target_addr = (uintptr_t)addr; /* Check that the address exceeds the limit. */ - if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE << 10)) >= (EXTERNAL_FLASH_SIZE << 10) || - ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE << 10)) != 0)) { + if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || + ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE)) != 0)) { dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)target_addr); return FLASH_STATUS_ERROR; } From 21cb1ff9224fe1b31e187eea5a597a13a8b1866a Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:08:07 +0800 Subject: [PATCH 13/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 3223cd8a8f34..7915b37f4cf6 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -249,8 +249,8 @@ flash_status_t flash_erase_block(const void *addr) { uintptr_t target_addr = (uintptr_t)addr; /* Check that the address exceeds the limit. */ - if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE << 10)) >= (EXTERNAL_FLASH_SIZE << 10) || - ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE << 10)) != 0)) { + if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || + ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE)) != 0)) { dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)target_addr); return FLASH_STATUS_ERROR; } From de1b9f490c520d0fa924d1ecdda939a587813f46 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 9 Feb 2022 09:27:17 +0800 Subject: [PATCH 14/37] Revert docs. --- docs/_summary.md | 1 - docs/ja/_summary.md | 1 - docs/zh-cn/_summary.md | 1 - 3 files changed, 3 deletions(-) diff --git a/docs/_summary.md b/docs/_summary.md index ddf574716cc8..e26d9ae21061 100644 --- a/docs/_summary.md +++ b/docs/_summary.md @@ -144,7 +144,6 @@ * [SPI Driver](spi_driver.md) * [WS2812 Driver](ws2812_driver.md) * [EEPROM Driver](eeprom_driver.md) - * [FLASH Driver](flash_driver.md) * ['serial' Driver](serial_driver.md) * [UART Driver](uart_driver.md) * [GPIO Controls](internals_gpio_control.md) diff --git a/docs/ja/_summary.md b/docs/ja/_summary.md index ef74f8f248b9..b90480041c73 100644 --- a/docs/ja/_summary.md +++ b/docs/ja/_summary.md @@ -135,7 +135,6 @@ * [SPI ドライバ](ja/spi_driver.md) * [WS2812 ドライバ](ja/ws2812_driver.md) * [EEPROM ドライバ](ja/eeprom_driver.md) - * [FLASH ドライバ](ja/flash_driver.md) * [シリアル ドライバ](ja/serial_driver.md) * [UART ドライバ](ja/uart_driver.md) * [GPIO 制御](ja/internals_gpio_control.md) diff --git a/docs/zh-cn/_summary.md b/docs/zh-cn/_summary.md index 8c1f221d13da..cedcfbd52574 100644 --- a/docs/zh-cn/_summary.md +++ b/docs/zh-cn/_summary.md @@ -107,7 +107,6 @@ * [SPI设备](zh-cn/spi_driver.md) * [WS2812设备](zh-cn/ws2812_driver.md) * [EEPROM设备](zh-cn/eeprom_driver.md) - * [FLASH设备](zh-cn/flash_driver.md) * [GPIO控制](zh-cn/internals_gpio_control.md) * [自定义键盘矩阵](zh-cn/custom_matrix.md) * [Proton C转换](zh-cn/proton_c_conversion.md) From 46daf95a510d9824f5aa0aa05fc5fecf96439124 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:29:22 +0800 Subject: [PATCH 15/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 7915b37f4cf6..2ebe09591c44 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -204,7 +204,6 @@ flash_status_t flash_erase_chip(void) { flash_status_t flash_erase_sector(uint32_t addr) { flash_status_t response = FLASH_STATUS_SUCCESS; - uintptr_t target_addr = (uintptr_t)addr; /* Check that the address exceeds the limit. */ if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || From d8cacabbe9956ba5bf12cf9aa4c10acd9bdad48f Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:29:40 +0800 Subject: [PATCH 16/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 2ebe09591c44..5fd128195850 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -243,7 +243,7 @@ flash_status_t flash_erase_sector(uint32_t addr) { return response; } -flash_status_t flash_erase_block(const void *addr) { +flash_status_t flash_erase_block(uint32_t addr) { flash_status_t response = FLASH_STATUS_SUCCESS; uintptr_t target_addr = (uintptr_t)addr; From 03ecdde3b8eee489a4b8752af6028dd18f663232 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:30:02 +0800 Subject: [PATCH 17/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 5fd128195850..ea783abdcddb 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -320,7 +320,6 @@ flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len) { flash_status_t flash_write_block(const uint8_t *buf, void *addr, size_t len) { flash_status_t response = FLASH_STATUS_SUCCESS; uint8_t * write_buf = (uint8_t *)buf; - uintptr_t target_addr = (uintptr_t)addr; while (len > 0) { uintptr_t page_offset = target_addr % EXTERNAL_FLASH_PAGE_SIZE; From a06f14eed5c86e0e19bdada143c3b7baf978e7f8 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:30:22 +0800 Subject: [PATCH 18/37] Update docs/flash_driver.md Co-authored-by: Nick Brassel --- docs/flash_driver.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/flash_driver.md b/docs/flash_driver.md index c5d40736538f..a60714fda719 100644 --- a/docs/flash_driver.md +++ b/docs/flash_driver.md @@ -16,9 +16,9 @@ Currently QMK supports almost all NOR Flash chips over SPI. As such, requires a `#define EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN` | SPI Slave select pin in order to inform that the FLASH is currently being addressed | _none_ `#define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR` | Clock divisor used to divide the peripheral clock to derive the SPI frequency | `18` `#define EXTERNAL_FLASH_PAGE_SIZE` | The Page size of the FLASH in bytes, as specified in the datasheet | 256 -`#define EXTERNAL_FLASH_SECTOR_SIZE` | The sector size of the FLASH in Kbytes, as specified in the datasheet | 4 -`#define EXTERNAL_FLASH_BLOCK_SIZE` | The block size of the FLASH in Kbytes, as specified in the datasheet | 64 -`#define EXTERNAL_FLASH_SIZE` | The total size of the FLASH in Kbytes, as specified in the datasheet | 512 +`#define EXTERNAL_FLASH_SECTOR_SIZE` | The sector size of the FLASH in bytes, as specified in the datasheet | `(4 * 1024)` +`#define EXTERNAL_FLASH_BLOCK_SIZE` | The block size of the FLASH in bytes, as specified in the datasheet | `(64 * 1024)` +`#define EXTERNAL_FLASH_SIZE` | The total size of the FLASH in bytes, as specified in the datasheet | `(512 * 1024)` `#define EXTERNAL_FLASH_ADDRESS_SIZE` | The Flash address size in bytes, as specified in datasheet | 3 !> All the above default configurations are based on MX25L4006E NOR Flash. From b34e0487768a90d8ee921479612ab24626b81ecd Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:31:31 +0800 Subject: [PATCH 19/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index ea783abdcddb..1308838a3ad9 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -245,7 +245,6 @@ flash_status_t flash_erase_sector(uint32_t addr) { flash_status_t flash_erase_block(uint32_t addr) { flash_status_t response = FLASH_STATUS_SUCCESS; - uintptr_t target_addr = (uintptr_t)addr; /* Check that the address exceeds the limit. */ if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || From 20a59a3141a8b2a05579c1c834d9ce03763eb087 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:31:42 +0800 Subject: [PATCH 20/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 1308838a3ad9..33aeaef3c025 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -287,7 +287,6 @@ flash_status_t flash_erase_block(uint32_t addr) { flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len) { flash_status_t response = FLASH_STATUS_SUCCESS; uint8_t * read_buf = (uint8_t *)buf; - uintptr_t target_addr = (uintptr_t)addr; /* Wait for the write-in-progress bit to be cleared. */ response= spi_flash_wait_while_busy(); From 2466ffdbbfd8d8ea7ad62392c8adc563888b63fb Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:31:59 +0800 Subject: [PATCH 21/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 33aeaef3c025..988c40e48f0a 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -315,7 +315,7 @@ flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len) { return response; } -flash_status_t flash_write_block(const uint8_t *buf, void *addr, size_t len) { +flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { flash_status_t response = FLASH_STATUS_SUCCESS; uint8_t * write_buf = (uint8_t *)buf; From 5e81862ca94573d2a4abb02a7e3fdbaa771727c4 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:32:10 +0800 Subject: [PATCH 22/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 988c40e48f0a..644692a5198f 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -284,7 +284,7 @@ flash_status_t flash_erase_block(uint32_t addr) { return response; } -flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len) { +flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { flash_status_t response = FLASH_STATUS_SUCCESS; uint8_t * read_buf = (uint8_t *)buf; From b3647d9a417acf1b3b9674a732f837776e1b8924 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:32:27 +0800 Subject: [PATCH 23/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index d1f82784ae7d..9d13fb487b3d 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -85,10 +85,10 @@ along with this program. If not, see . #endif /* - The total size of the FLASH in Kbytes, as specified in the datasheet. + The total size of the FLASH in bytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_SIZE -# define EXTERNAL_FLASH_SIZE 512 +# define EXTERNAL_FLASH_SIZE (512 * 1024) #endif /* From ef65275427633af75b19c603cd7c53619433aca7 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:32:39 +0800 Subject: [PATCH 24/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index 9d13fb487b3d..a7a1afb8799f 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -78,10 +78,10 @@ along with this program. If not, see . #endif /* - The block size of the FLASH in Kbytes, as specified in the datasheet. + The block size of the FLASH in bytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_BLOCK_SIZE -# define EXTERNAL_FLASH_BLOCK_SIZE 64 +# define EXTERNAL_FLASH_BLOCK_SIZE (64 * 1024) #endif /* From 10b83e505bf17671b377fab6b6207910b25e2cb2 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 09:32:53 +0800 Subject: [PATCH 25/37] Update drivers/flash/flash_spi.h Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index a7a1afb8799f..0e833bf400de 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -71,10 +71,10 @@ along with this program. If not, see . #endif /* - The sector size of the FLASH in Kbytes, as specified in the datasheet. + The sector size of the FLASH in bytes, as specified in the datasheet. */ #ifndef EXTERNAL_FLASH_SECTOR_SIZE -# define EXTERNAL_FLASH_SECTOR_SIZE 4 +# define EXTERNAL_FLASH_SECTOR_SIZE (4 * 1024) #endif /* From cb46b3d9b30d0f6d1ecd52bf1707864b44b0c107 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 9 Feb 2022 09:51:37 +0800 Subject: [PATCH 26/37] Modified code format. --- drivers/flash/flash_spi.c | 110 ++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 644692a5198f..c7ac409f6df9 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -32,36 +32,36 @@ along with this program. If not, see . #endif /* ID comands */ -#define FLASH_CMD_RDID 0x9F /* RDID (Read Identification) */ -#define FLASH_CMD_RES 0xAB /* RES (Read Electronic ID) */ -#define FLASH_CMD_REMS 0x90 /* REMS (Read Electronic & Device ID) */ +#define FLASH_CMD_RDID 0x9F /* RDID (Read Identification) */ +#define FLASH_CMD_RES 0xAB /* RES (Read Electronic ID) */ +#define FLASH_CMD_REMS 0x90 /* REMS (Read Electronic & Device ID) */ /* register comands */ -#define FLASH_CMD_WRSR 0x01 /* WRSR (Write Status register) */ -#define FLASH_CMD_RDSR 0x05 /* RDSR (Read Status register) */ +#define FLASH_CMD_WRSR 0x01 /* WRSR (Write Status register) */ +#define FLASH_CMD_RDSR 0x05 /* RDSR (Read Status register) */ /* READ comands */ -#define FLASH_CMD_READ 0x03 /* READ (1 x I/O) */ -#define FLASH_CMD_FASTREAD 0x0B /* FAST READ (Fast read data) */ -#define FLASH_CMD_DREAD 0x3B /* DREAD (1In/2 Out fast read) */ +#define FLASH_CMD_READ 0x03 /* READ (1 x I/O) */ +#define FLASH_CMD_FASTREAD 0x0B /* FAST READ (Fast read data) */ +#define FLASH_CMD_DREAD 0x3B /* DREAD (1In/2 Out fast read) */ /* Program comands */ -#define FLASH_CMD_WREN 0x06 /* WREN (Write Enable) */ -#define FLASH_CMD_WRDI 0x04 /* WRDI (Write Disable) */ -#define FLASH_CMD_PP 0x02 /* PP (page program) */ +#define FLASH_CMD_WREN 0x06 /* WREN (Write Enable) */ +#define FLASH_CMD_WRDI 0x04 /* WRDI (Write Disable) */ +#define FLASH_CMD_PP 0x02 /* PP (page program) */ /* Erase comands */ -#define FLASH_CMD_SE 0x20 /* SE (Sector Erase) */ -#define FLASH_CMD_BE 0xD8 /* BE (Block Erase) */ -#define FLASH_CMD_CE 0x60 /* CE (Chip Erase) hex code: 60 or C7 */ +#define FLASH_CMD_SE 0x20 /* SE (Sector Erase) */ +#define FLASH_CMD_BE 0xD8 /* BE (Block Erase) */ +#define FLASH_CMD_CE 0x60 /* CE (Chip Erase) hex code: 60 or C7 */ /* Mode setting comands */ -#define FLASH_CMD_DP 0xB9 /* DP (Deep Power Down) */ -#define FLASH_CMD_RDP 0xAB /* RDP (Release form Deep Power Down) */ +#define FLASH_CMD_DP 0xB9 /* DP (Deep Power Down) */ +#define FLASH_CMD_RDP 0xAB /* RDP (Release form Deep Power Down) */ /* Status register */ -#define FLASH_FLAG_WIP 0x01 /* Write in progress bit */ -#define FLASH_FLAG_WEL 0x02 /* Write enable latch bit */ +#define FLASH_FLAG_WIP 0x01 /* Write in progress bit */ +#define FLASH_FLAG_WEL 0x02 /* Write enable latch bit */ // #define DEBUG_FLASH_SPI_OUTPUT @@ -70,7 +70,7 @@ static bool spi_flash_start(void) { return spi_start(EXTERNAL_FLASH_SPI_SLAVE_SE static flash_status_t spi_flash_wait_while_busy(void) { uint32_t deadline = timer_read32() + EXTERNAL_FLASH_SPI_TIMEOUT; flash_status_t response = FLASH_STATUS_SUCCESS; - uint8_t retval; + uint8_t retval; do { bool res = spi_flash_start(); @@ -95,7 +95,6 @@ static flash_status_t spi_flash_wait_while_busy(void) { } static flash_status_t spi_flash_write_enable(void) { - bool res = spi_flash_start(); if (!res) { dprint("Failed to start SPI! [spi flash write enable]\n"); @@ -110,7 +109,6 @@ static flash_status_t spi_flash_write_enable(void) { } static flash_status_t spi_flash_write_disable(void) { - bool res = spi_flash_start(); if (!res) { dprint("Failed to start SPI! [spi flash write disable]\n"); @@ -127,7 +125,7 @@ static flash_status_t spi_flash_write_disable(void) { /* This function is used for read transfer, write transfer and erase transfer. */ static flash_status_t spi_flash_transaction(uint8_t cmd, uint32_t addr, uint8_t *data, size_t len) { flash_status_t response = FLASH_STATUS_SUCCESS; - uint8_t buffer[EXTERNAL_FLASH_ADDRESS_SIZE + 1]; + uint8_t buffer[EXTERNAL_FLASH_ADDRESS_SIZE + 1]; buffer[0] = cmd; for (int i = 0; i < EXTERNAL_FLASH_ADDRESS_SIZE; ++i) { @@ -145,15 +143,15 @@ static flash_status_t spi_flash_transaction(uint8_t cmd, uint32_t addr, uint8_t if ((!response) && (data != NULL)) { switch (cmd) { - case FLASH_CMD_READ: - response = spi_receive(data, len); - break; - case FLASH_CMD_PP: - response = spi_transmit(data, len); - break; - default: - response = FLASH_STATUS_ERROR; - break; + case FLASH_CMD_READ: + response = spi_receive(data, len); + break; + case FLASH_CMD_PP: + response = spi_transmit(data, len); + break; + default: + response = FLASH_STATUS_ERROR; + break; } } @@ -162,15 +160,13 @@ static flash_status_t spi_flash_transaction(uint8_t cmd, uint32_t addr, uint8_t return response; } -void flash_init(void) { - spi_init(); -} +void flash_init(void) { spi_init(); } flash_status_t flash_erase_chip(void) { flash_status_t response = FLASH_STATUS_SUCCESS; /* Wait for the write-in-progress bit to be cleared. */ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash erase chip]\n"); return response; @@ -193,7 +189,7 @@ flash_status_t flash_erase_chip(void) { spi_stop(); /* Wait for the write-in-progress bit to be cleared.*/ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash erase chip]\n"); return response; @@ -203,17 +199,16 @@ flash_status_t flash_erase_chip(void) { } flash_status_t flash_erase_sector(uint32_t addr) { - flash_status_t response = FLASH_STATUS_SUCCESS; + flash_status_t response = FLASH_STATUS_SUCCESS; /* Check that the address exceeds the limit. */ - if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || - ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE)) != 0)) { + if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE)) != 0)) { dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)target_addr); return FLASH_STATUS_ERROR; } /* Wait for the write-in-progress bit to be cleared. */ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash erase sector]\n"); return response; @@ -234,7 +229,7 @@ flash_status_t flash_erase_sector(uint32_t addr) { } /* Wait for the write-in-progress bit to be cleared.*/ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash erase sector]\n"); return response; @@ -244,17 +239,16 @@ flash_status_t flash_erase_sector(uint32_t addr) { } flash_status_t flash_erase_block(uint32_t addr) { - flash_status_t response = FLASH_STATUS_SUCCESS; + flash_status_t response = FLASH_STATUS_SUCCESS; /* Check that the address exceeds the limit. */ - if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || - ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE)) != 0)) { + if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE)) != 0)) { dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)target_addr); return FLASH_STATUS_ERROR; } /* Wait for the write-in-progress bit to be cleared. */ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash erase block]\n"); return response; @@ -275,7 +269,7 @@ flash_status_t flash_erase_block(uint32_t addr) { } /* Wait for the write-in-progress bit to be cleared.*/ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash erase block]\n"); return response; @@ -285,11 +279,11 @@ flash_status_t flash_erase_block(uint32_t addr) { } flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { - flash_status_t response = FLASH_STATUS_SUCCESS; - uint8_t * read_buf = (uint8_t *)buf; + flash_status_t response = FLASH_STATUS_SUCCESS; + uint8_t * read_buf = (uint8_t *)buf; /* Wait for the write-in-progress bit to be cleared. */ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash read block]\n"); memset(read_buf, 0, len); @@ -316,8 +310,8 @@ flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { } flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { - flash_status_t response = FLASH_STATUS_SUCCESS; - uint8_t * write_buf = (uint8_t *)buf; + flash_status_t response = FLASH_STATUS_SUCCESS; + uint8_t * write_buf = (uint8_t *)buf; while (len > 0) { uintptr_t page_offset = target_addr % EXTERNAL_FLASH_PAGE_SIZE; @@ -327,7 +321,7 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { } /* Wait for the write-in-progress bit to be cleared. */ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to check WIP flag! [spi flash write block]\n"); return response; @@ -361,17 +355,17 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { } /* Wait for the write-in-progress bit to be cleared. */ - response= spi_flash_wait_while_busy(); + response = spi_flash_wait_while_busy(); if (response != FLASH_STATUS_SUCCESS) { - dprint("Failed to check WIP flag! [spi flash write block]\n"); - return response; + dprint("Failed to check WIP flag! [spi flash write block]\n"); + return response; } /* Disable writes. */ - response= spi_flash_write_disable(); + response = spi_flash_write_disable(); if (response != FLASH_STATUS_SUCCESS) { - dprint("Failed to write-disable! [spi flash write block]\n"); - return response; + dprint("Failed to write-disable! [spi flash write block]\n"); + return response; } return response; From fd58d309b5d74e4c098daf70e849587bb9c2c3e5 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 9 Feb 2022 09:58:48 +0800 Subject: [PATCH 27/37] Update drivers/flash/flash_spi.c --- drivers/flash/flash_spi.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index c7ac409f6df9..ed925d8c23bc 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -202,8 +202,8 @@ flash_status_t flash_erase_sector(uint32_t addr) { flash_status_t response = FLASH_STATUS_SUCCESS; /* Check that the address exceeds the limit. */ - if ((target_addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((target_addr % (EXTERNAL_FLASH_SECTOR_SIZE)) != 0)) { - dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)target_addr); + if ((addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((addr % (EXTERNAL_FLASH_SECTOR_SIZE)) != 0)) { + dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)addr); return FLASH_STATUS_ERROR; } @@ -222,7 +222,7 @@ flash_status_t flash_erase_sector(uint32_t addr) { } /* Erase Sector. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_SE, target_addr, NULL, 0); + response = spi_flash_transmit((uint8_t)FLASH_CMD_SE, addr, NULL, 0); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to erase sector! [spi flash erase sector]\n"); return response; @@ -242,8 +242,8 @@ flash_status_t flash_erase_block(uint32_t addr) { flash_status_t response = FLASH_STATUS_SUCCESS; /* Check that the address exceeds the limit. */ - if ((target_addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((target_addr % (EXTERNAL_FLASH_BLOCK_SIZE)) != 0)) { - dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)target_addr); + if ((addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((addr % (EXTERNAL_FLASH_BLOCK_SIZE)) != 0)) { + dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)addr); return FLASH_STATUS_ERROR; } @@ -262,7 +262,7 @@ flash_status_t flash_erase_block(uint32_t addr) { } /* Erase Block. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_BE, target_addr, NULL, 0); + response = spi_flash_transmit((uint8_t)FLASH_CMD_BE, addr, NULL, 0); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to erase block! [spi flash erase block]\n"); return response; @@ -291,7 +291,7 @@ flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { } /* Perform read. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_READ, (uintptr_t)target_addr, read_buf, len); + response = spi_flash_transmit((uint8_t)FLASH_CMD_READ, (uint32_t)addr, read_buf, len); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to read block! [spi flash read block]\n"); memset(read_buf, 0, len); @@ -299,7 +299,7 @@ flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { } #if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT) - dprintf("[SPI FLASH R] 0x%08lX: ", ((uint32_t)target_addr)); + dprintf("[SPI FLASH R] 0x%08lX: ", ((uint32_t)addr)); for (size_t i = 0; i < len; ++i) { dprintf(" %02X", (int)(((uint8_t *)read_buf)[i])); } @@ -314,7 +314,7 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { uint8_t * write_buf = (uint8_t *)buf; while (len > 0) { - uintptr_t page_offset = target_addr % EXTERNAL_FLASH_PAGE_SIZE; + uint32_t page_offset = addr % EXTERNAL_FLASH_PAGE_SIZE; size_t write_length = EXTERNAL_FLASH_PAGE_SIZE - page_offset; if (write_length > len) { write_length = len; @@ -335,7 +335,7 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { } #if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT) - dprintf("[SPI FLASH W] 0x%08lX: ", ((uint32_t)target_addr)); + dprintf("[SPI FLASH W] 0x%08lX: ", ((uint32_t)addr)); for (size_t i = 0; i < write_length; i++) { dprintf(" %02X", (int)(uint8_t)(write_buf[i])); } @@ -343,14 +343,14 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { #endif // DEBUG_FLASH_SPI_OUTPUT /* Perform the write. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_PP, target_addr, write_buf, write_length); + response = spi_flash_transmit((uint8_t)FLASH_CMD_PP, addr, write_buf, write_length); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to write block! [spi flash write block]\n"); return response; } write_buf += write_length; - target_addr += write_length; + addr += write_length; len -= write_length; } From 734fefd61cacf0fd500fe43220c95b33748c65b8 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 9 Feb 2022 10:02:06 +0800 Subject: [PATCH 28/37] Modified code format. --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index ed925d8c23bc..202a1b4ca367 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -315,7 +315,7 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { while (len > 0) { uint32_t page_offset = addr % EXTERNAL_FLASH_PAGE_SIZE; - size_t write_length = EXTERNAL_FLASH_PAGE_SIZE - page_offset; + size_t write_length = EXTERNAL_FLASH_PAGE_SIZE - page_offset; if (write_length > len) { write_length = len; } From ba823f55fbbc24c89e7aa2dce5c45153907e8525 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 9 Feb 2022 10:07:25 +0800 Subject: [PATCH 29/37] Update docs/flash_driver.md --- docs/flash_driver.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/flash_driver.md b/docs/flash_driver.md index a60714fda719..fa7fed5171b1 100644 --- a/docs/flash_driver.md +++ b/docs/flash_driver.md @@ -12,13 +12,13 @@ Driver | Description Currently QMK supports almost all NOR Flash chips over SPI. As such, requires a working spi_master driver configuration. You can override the driver configuration via your config.h: `config.h` override | Description | Default Value ------------------------------------------------|--------------------------------------------------------------------------------------|-------------- +-----------------------------------------------|--------------------------------------------------------------------------------------|----------------- `#define EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN` | SPI Slave select pin in order to inform that the FLASH is currently being addressed | _none_ -`#define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR` | Clock divisor used to divide the peripheral clock to derive the SPI frequency | `18` -`#define EXTERNAL_FLASH_PAGE_SIZE` | The Page size of the FLASH in bytes, as specified in the datasheet | 256 +`#define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR` | Clock divisor used to divide the peripheral clock to derive the SPI frequency | `8` +`#define EXTERNAL_FLASH_PAGE_SIZE` | The Page size of the FLASH in bytes, as specified in the datasheet | `256` `#define EXTERNAL_FLASH_SECTOR_SIZE` | The sector size of the FLASH in bytes, as specified in the datasheet | `(4 * 1024)` `#define EXTERNAL_FLASH_BLOCK_SIZE` | The block size of the FLASH in bytes, as specified in the datasheet | `(64 * 1024)` `#define EXTERNAL_FLASH_SIZE` | The total size of the FLASH in bytes, as specified in the datasheet | `(512 * 1024)` -`#define EXTERNAL_FLASH_ADDRESS_SIZE` | The Flash address size in bytes, as specified in datasheet | 3 +`#define EXTERNAL_FLASH_ADDRESS_SIZE` | The Flash address size in bytes, as specified in datasheet | `3` !> All the above default configurations are based on MX25L4006E NOR Flash. From e6c4eb0efebec8f10bfc1166f9a524a5516211d2 Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 9 Feb 2022 10:21:47 +0800 Subject: [PATCH 30/37] Update drivers/flash/flash_spi.h --- drivers/flash/flash_spi.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/flash/flash_spi.h b/drivers/flash/flash_spi.h index 0e833bf400de..abe95e955e56 100644 --- a/drivers/flash/flash_spi.h +++ b/drivers/flash/flash_spi.h @@ -123,13 +123,13 @@ void flash_init(void); flash_status_t flash_erase_chip(void); -flash_status_t flash_erase_block(const void *addr); +flash_status_t flash_erase_block(uint32_t addr); -flash_status_t flash_erase_sector(const void *addr); +flash_status_t flash_erase_sector(uint32_t addr); -flash_status_t flash_read_block(uint8_t *buf, const void *addr, size_t len); +flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len); -flash_status_t flash_write_block(const uint8_t *buf, void *addr, size_t len); +flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len); #ifdef __cplusplus } From cb78267cd4df2cf4259e556c20ac10579ebb783b Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:40:06 +0800 Subject: [PATCH 31/37] Update common_features.mk Co-authored-by: Nick Brassel --- common_features.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common_features.mk b/common_features.mk index 17835ed5a8e0..cf724bd94945 100644 --- a/common_features.mk +++ b/common_features.mk @@ -208,7 +208,7 @@ ifneq ($(strip $(FLASH_DRIVER)), no) $(error FLASH_DRIVER="$(FLASH_DRIVER)" is not a valid FLASH driver) else OPT_DEFS += -DFLASH_ENABLE - ifeq ($(strip $(EEPROM_DRIVER)), spi) + ifeq ($(strip $(FLASH_DRIVER)), spi) OPT_DEFS += -DFLASH_DRIVER -DFLASH_SPI COMMON_VPATH += $(DRIVER_PATH)/flash SRC += flash_spi.c From cc66dc424a2d5bf49c0c04d33987182ee8a3f42a Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:41:16 +0800 Subject: [PATCH 32/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 202a1b4ca367..f10b6c8d3915 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -222,7 +222,7 @@ flash_status_t flash_erase_sector(uint32_t addr) { } /* Erase Sector. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_SE, addr, NULL, 0); + response = spi_flash_transaction(FLASH_CMD_SE, addr, NULL, 0); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to erase sector! [spi flash erase sector]\n"); return response; From b64de277a68a79cca7beea93be94a0dc59388035 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:41:21 +0800 Subject: [PATCH 33/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index f10b6c8d3915..5d3c45ff5e6b 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -262,7 +262,7 @@ flash_status_t flash_erase_block(uint32_t addr) { } /* Erase Block. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_BE, addr, NULL, 0); + response = spi_flash_transaction(FLASH_CMD_BE, addr, NULL, 0); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to erase block! [spi flash erase block]\n"); return response; From 9c7c4f19618c0a57ce17c9b337b7472e4cc76c7a Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:41:27 +0800 Subject: [PATCH 34/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 5d3c45ff5e6b..974b007027fd 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -291,7 +291,7 @@ flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { } /* Perform read. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_READ, (uint32_t)addr, read_buf, len); + response = spi_flash_transaction(FLASH_CMD_READ, addr, read_buf, len); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to read block! [spi flash read block]\n"); memset(read_buf, 0, len); From 54e3886236c82854961a6fe3502cc15bb997b38a Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:41:31 +0800 Subject: [PATCH 35/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 974b007027fd..d37d25a47512 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -299,7 +299,7 @@ flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) { } #if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT) - dprintf("[SPI FLASH R] 0x%08lX: ", ((uint32_t)addr)); + dprintf("[SPI FLASH R] 0x%08lX: ", addr); for (size_t i = 0; i < len; ++i) { dprintf(" %02X", (int)(((uint8_t *)read_buf)[i])); } From 8b24ee5cf9e6538fed8c79c694447326b26a344c Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:41:37 +0800 Subject: [PATCH 36/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index d37d25a47512..4d01c7a7224b 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -335,7 +335,7 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { } #if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT) - dprintf("[SPI FLASH W] 0x%08lX: ", ((uint32_t)addr)); + dprintf("[SPI FLASH W] 0x%08lX: ", addr); for (size_t i = 0; i < write_length; i++) { dprintf(" %02X", (int)(uint8_t)(write_buf[i])); } From ab63b20f7a6919399001eebfbe5f19e9dd9690d1 Mon Sep 17 00:00:00 2001 From: Joy Lee Date: Wed, 9 Feb 2022 10:41:42 +0800 Subject: [PATCH 37/37] Update drivers/flash/flash_spi.c Co-authored-by: Nick Brassel --- drivers/flash/flash_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/flash/flash_spi.c b/drivers/flash/flash_spi.c index 4d01c7a7224b..2fa8891e3831 100644 --- a/drivers/flash/flash_spi.c +++ b/drivers/flash/flash_spi.c @@ -343,7 +343,7 @@ flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) { #endif // DEBUG_FLASH_SPI_OUTPUT /* Perform the write. */ - response = spi_flash_transmit((uint8_t)FLASH_CMD_PP, addr, write_buf, write_length); + response = spi_flash_transaction(FLASH_CMD_PP, addr, write_buf, write_length); if (response != FLASH_STATUS_SUCCESS) { dprint("Failed to write block! [spi flash write block]\n"); return response;