From f610b6b190d369a938cdd8c0acd2c8713510fbd9 Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Sat, 29 Jan 2022 18:29:50 +0100 Subject: [PATCH 1/3] Changed the order of SPI configuration and activation. SPI should be configured while chip select pin is disabled. Tested with external flash on STM32 F4VE board. --- supervisor/shared/external_flash/spi_flash.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervisor/shared/external_flash/spi_flash.c b/supervisor/shared/external_flash/spi_flash.c index 9444e303a1..586ace9605 100644 --- a/supervisor/shared/external_flash/spi_flash.c +++ b/supervisor/shared/external_flash/spi_flash.c @@ -105,12 +105,12 @@ bool spi_flash_sector_command(uint8_t command, uint32_t address) { bool spi_flash_write_data(uint32_t address, uint8_t *data, uint32_t data_length) { uint8_t request[4] = {CMD_PAGE_PROGRAM, 0x00, 0x00, 0x00}; + common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); // Write the SPI flash write address into the bytes following the command byte. address_to_bytes(address, request + 1); if (!flash_enable()) { return false; } - common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); bool status = common_hal_busio_spi_write(&supervisor_flash_spi_bus, request, 4); if (status) { status = common_hal_busio_spi_write(&supervisor_flash_spi_bus, data, data_length); @@ -126,12 +126,12 @@ bool spi_flash_read_data(uint32_t address, uint8_t *data, uint32_t data_length) request[0] = CMD_FAST_READ_DATA; command_length = 5; } + common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); // Write the SPI flash read address into the bytes following the command byte. address_to_bytes(address, request + 1); if (!flash_enable()) { return false; } - common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); bool status = common_hal_busio_spi_write(&supervisor_flash_spi_bus, request, command_length); if (status) { status = common_hal_busio_spi_read(&supervisor_flash_spi_bus, data, data_length, 0xff); From 2a1f5866344e774005296b259f096a8c7c9641c7 Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Sun, 30 Jan 2022 09:49:07 +0100 Subject: [PATCH 2/3] Avoid excessive SPI re-configuration in run-time of SPI flash. --- supervisor/shared/external_flash/spi_flash.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/supervisor/shared/external_flash/spi_flash.c b/supervisor/shared/external_flash/spi_flash.c index 586ace9605..b7cb7b180d 100644 --- a/supervisor/shared/external_flash/spi_flash.c +++ b/supervisor/shared/external_flash/spi_flash.c @@ -105,7 +105,6 @@ bool spi_flash_sector_command(uint8_t command, uint32_t address) { bool spi_flash_write_data(uint32_t address, uint8_t *data, uint32_t data_length) { uint8_t request[4] = {CMD_PAGE_PROGRAM, 0x00, 0x00, 0x00}; - common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); // Write the SPI flash write address into the bytes following the command byte. address_to_bytes(address, request + 1); if (!flash_enable()) { @@ -126,7 +125,6 @@ bool spi_flash_read_data(uint32_t address, uint8_t *data, uint32_t data_length) request[0] = CMD_FAST_READ_DATA; command_length = 5; } - common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); // Write the SPI flash read address into the bytes following the command byte. address_to_bytes(address, request + 1); if (!flash_enable()) { @@ -144,7 +142,6 @@ void spi_flash_init(void) { cs_pin.base.type = &digitalio_digitalinout_type; common_hal_digitalio_digitalinout_construct(&cs_pin, SPI_FLASH_CS_PIN); - // Set CS high (disabled). common_hal_digitalio_digitalinout_switch_to_output(&cs_pin, true, DRIVE_MODE_PUSH_PULL); common_hal_digitalio_digitalinout_never_reset(&cs_pin); @@ -152,6 +149,8 @@ void spi_flash_init(void) { supervisor_flash_spi_bus.base.type = &busio_spi_type; common_hal_busio_spi_construct(&supervisor_flash_spi_bus, SPI_FLASH_SCK_PIN, SPI_FLASH_MOSI_PIN, SPI_FLASH_MISO_PIN); common_hal_busio_spi_never_reset(&supervisor_flash_spi_bus); + + return; } void spi_flash_init_device(const external_flash_device *device) { @@ -160,4 +159,6 @@ void spi_flash_init_device(const external_flash_device *device) { if (spi_flash_baudrate > SPI_FLASH_MAX_BAUDRATE) { spi_flash_baudrate = SPI_FLASH_MAX_BAUDRATE; } + common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); + return; } From cfa7e8a168d2a8b65d5a6fe82c3d88ccad097d1e Mon Sep 17 00:00:00 2001 From: EmergReanimator Date: Tue, 1 Feb 2022 19:39:56 +0100 Subject: [PATCH 3/3] fix #5948 Adding back the SPI configuration to spi_flash_read/write_data Removing this configures will break cases where the flash is on a shared SPI bus with a device that has different settings. This is rare but possible. --- supervisor/shared/external_flash/spi_flash.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/supervisor/shared/external_flash/spi_flash.c b/supervisor/shared/external_flash/spi_flash.c index b7cb7b180d..fdff71ea97 100644 --- a/supervisor/shared/external_flash/spi_flash.c +++ b/supervisor/shared/external_flash/spi_flash.c @@ -105,6 +105,7 @@ bool spi_flash_sector_command(uint8_t command, uint32_t address) { bool spi_flash_write_data(uint32_t address, uint8_t *data, uint32_t data_length) { uint8_t request[4] = {CMD_PAGE_PROGRAM, 0x00, 0x00, 0x00}; + common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); // Write the SPI flash write address into the bytes following the command byte. address_to_bytes(address, request + 1); if (!flash_enable()) { @@ -125,6 +126,7 @@ bool spi_flash_read_data(uint32_t address, uint8_t *data, uint32_t data_length) request[0] = CMD_FAST_READ_DATA; command_length = 5; } + common_hal_busio_spi_configure(&supervisor_flash_spi_bus, spi_flash_baudrate, 0, 0, 8); // Write the SPI flash read address into the bytes following the command byte. address_to_bytes(address, request + 1); if (!flash_enable()) {