From 9fe0e9eb2ae194ed5edc879febc0d5ddec64987d Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Fri, 21 Jul 2017 10:03:35 +0100 Subject: [PATCH] Configurable FLASH --- src/main/drivers/flash.h | 1 + src/main/drivers/flash_m25p16.c | 36 ++++++++++++++++++--------------- src/main/fc/config.c | 20 +++++++++--------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/main/drivers/flash.h b/src/main/drivers/flash.h index f9158b15e8..2b6b5ac525 100644 --- a/src/main/drivers/flash.h +++ b/src/main/drivers/flash.h @@ -33,4 +33,5 @@ typedef struct flashGeometry_s { typedef struct flashConfig_s { ioTag_t csTag; + uint8_t spiDevice; } flashConfig_t; diff --git a/src/main/drivers/flash_m25p16.c b/src/main/drivers/flash_m25p16.c index 4a43f361f5..980adbe21a 100644 --- a/src/main/drivers/flash_m25p16.c +++ b/src/main/drivers/flash_m25p16.c @@ -51,8 +51,11 @@ #define JEDEC_ID_WINBOND_W25Q128 0xEF4018 #define JEDEC_ID_MACRONIX_MX25L25635E 0xC22019 -#define DISABLE_M25P16 IOHi(m25p16CsPin); __NOP() -#define ENABLE_M25P16 __NOP(); IOLo(m25p16CsPin) +#define DISABLE_M25P16 IOHi(bus->busdev_u.spi.csnPin); __NOP() +#define ENABLE_M25P16 __NOP(); IOLo(bus->busdev_u.spi.csnPin) + +static busDevice_t busInstance; +static busDevice_t *bus; // The timeout we expect between being able to issue page program instructions #define DEFAULT_TIMEOUT_MILLIS 6 @@ -63,8 +66,6 @@ static flashGeometry_t geometry = {.pageSize = M25P16_PAGESIZE}; -static IO_t m25p16CsPin = IO_NONE; - /* * Whether we've performed an action that could have made the device busy for writes. * @@ -79,7 +80,7 @@ static void m25p16_performOneByteCommand(uint8_t command) { ENABLE_M25P16; - spiTransferByte(M25P16_SPI_INSTANCE, command); + spiTransferByte(bus->busdev_u.spi.instance, command); DISABLE_M25P16; } @@ -103,7 +104,7 @@ static uint8_t m25p16_readStatus(void) ENABLE_M25P16; - spiTransfer(M25P16_SPI_INSTANCE, command, in, sizeof(command)); + spiTransfer(bus->busdev_u.spi.instance, command, in, sizeof(command)); DISABLE_M25P16; @@ -149,7 +150,7 @@ static bool m25p16_readIdentification(void) ENABLE_M25P16; - spiTransfer(M25P16_SPI_INSTANCE, out, in, sizeof(out)); + spiTransfer(bus->busdev_u.spi.instance, out, in, sizeof(out)); // Clearing the CS bit terminates the command early so we don't have to read the chip UID: DISABLE_M25P16; @@ -216,20 +217,23 @@ bool m25p16_init(const flashConfig_t *flashConfig) return true; } + bus = &busInstance; + bus->bustype = BUSTYPE_SPI; + spiBusSetInstance(bus, spiInstanceByDevice(SPI_CFG_TO_DEV(flashConfig->spiDevice))); if (flashConfig->csTag) { - m25p16CsPin = IOGetByTag(flashConfig->csTag); + bus->busdev_u.spi.csnPin = IOGetByTag(flashConfig->csTag); } else { return false; } - IOInit(m25p16CsPin, OWNER_FLASH_CS, 0); - IOConfigGPIO(m25p16CsPin, SPI_IO_CS_CFG); + IOInit(bus->busdev_u.spi.csnPin, OWNER_FLASH_CS, 0); + IOConfigGPIO(bus->busdev_u.spi.csnPin, SPI_IO_CS_CFG); DISABLE_M25P16; #ifndef M25P16_SPI_SHARED //Maximum speed for standard READ command is 20mHz, other commands tolerate 25mHz - spiSetDivisor(M25P16_SPI_INSTANCE, SPI_CLOCK_FAST); + spiSetDivisor(bus->busdev_u.spi.instance, SPI_CLOCK_FAST); #endif return m25p16_readIdentification(); @@ -248,7 +252,7 @@ void m25p16_eraseSector(uint32_t address) ENABLE_M25P16; - spiTransfer(M25P16_SPI_INSTANCE, out, NULL, sizeof(out)); + spiTransfer(bus->busdev_u.spi.instance, out, NULL, sizeof(out)); DISABLE_M25P16; } @@ -272,12 +276,12 @@ void m25p16_pageProgramBegin(uint32_t address) ENABLE_M25P16; - spiTransfer(M25P16_SPI_INSTANCE, command, NULL, sizeof(command)); + spiTransfer(bus->busdev_u.spi.instance, command, NULL, sizeof(command)); } void m25p16_pageProgramContinue(const uint8_t *data, int length) { - spiTransfer(M25P16_SPI_INSTANCE, data, NULL, length); + spiTransfer(bus->busdev_u.spi.instance, data, NULL, length); } void m25p16_pageProgramFinish(void) @@ -327,8 +331,8 @@ int m25p16_readBytes(uint32_t address, uint8_t *buffer, int length) ENABLE_M25P16; - spiTransfer(M25P16_SPI_INSTANCE, command, NULL, sizeof(command)); - spiTransfer(M25P16_SPI_INSTANCE, NULL, buffer, length); + spiTransfer(bus->busdev_u.spi.instance, command, NULL, sizeof(command)); + spiTransfer(bus->busdev_u.spi.instance, NULL, buffer, length); DISABLE_M25P16; diff --git a/src/main/fc/config.c b/src/main/fc/config.c index c979df0707..9cc1494030 100755 --- a/src/main/fc/config.c +++ b/src/main/fc/config.c @@ -40,6 +40,7 @@ #include "config/parameter_group_ids.h" #include "drivers/accgyro/accgyro.h" +#include "drivers/bus_spi.h" #include "drivers/compass/compass.h" #include "drivers/inverter.h" #include "drivers/io.h" @@ -161,16 +162,17 @@ PG_REGISTER_WITH_RESET_FN(ppmConfig_t, ppmConfig, PG_PPM_CONFIG, 0); #endif #ifdef USE_FLASHFS -PG_REGISTER_WITH_RESET_TEMPLATE(flashConfig_t, flashConfig, PG_FLASH_CONFIG, 0); -#ifdef M25P16_CS_PIN -#define FLASH_CONFIG_CSTAG IO_TAG(M25P16_CS_PIN) -#else -#define FLASH_CONFIG_CSTAG IO_TAG_NONE -#endif +PG_REGISTER_WITH_RESET_FN(flashConfig_t, flashConfig, PG_FLASH_CONFIG, 0); -PG_RESET_TEMPLATE(flashConfig_t, flashConfig, - .csTag = FLASH_CONFIG_CSTAG -); +void pgResetFn_flashConfig(flashConfig_t *flashConfig) +{ +#ifdef M25P16_CS_PIN + flashConfig->csTag = IO_TAG(M25P16_CS_PIN); +#else + flashConfig->csTag = IO_TAG_NONE; +#endif + flashConfig->spiDevice = SPI_DEV_TO_CFG(spiDeviceByInstance(M25P16_SPI_INSTANCE)); +} #endif // USE_FLASH_FS #ifdef USE_SDCARD