1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-17 05:15:25 +03:00

Merge pull request #3590 from martinbudden/bf_flash_config

Configurable FLASH
This commit is contained in:
jflyper 2017-07-30 19:01:26 +09:00 committed by GitHub
commit 94e3273415
3 changed files with 32 additions and 25 deletions

View file

@ -30,4 +30,5 @@ typedef struct flashGeometry_s {
typedef struct flashConfig_s {
ioTag_t csTag;
uint8_t spiDevice;
} flashConfig_t;

View file

@ -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;

View file

@ -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"
@ -168,16 +169,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