mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-25 17:25:20 +03:00
Fix unified targets that use OctoSPI. (BF4.4.x) (#12307)
OctoSPI and Memory Mapped Flash support (#11825) * STM32H730/STM32H750 - Fix use of USE_LP_UART1 instead of USE_LPUART1. * STM32H723 - Prepare for being able to build for using non-internal-flash config storage. * STM32H723 - Prepare for using non-default strings. * STM32H723 - Disable 'use custom defaults' when using USE_EXST. * STM32H723 - Disable CUSTOM_DEFAULTS_EXTENDED when EXST is used. * OCTOSPI - Add initialisation code. * Add support for RAM_CODE. * STM32H730 - Add support for RAM_CODE via the .ram_code attribute. * OCTOSPI - Proof-of-concept for disabling/enabling memory mapped mode on a running system. NOTE: The HAL libs are compiled into a memory mapped region, and this cannot be used for OctoSPI access when memory mapped mode is disabled. * OCTOSPI - Drop HAL support after determining that it's not suitable for the memory mapped flash use-case. * OCTOSPI - Sometimes, when disabling memory map mode, the abort fails. Handle this by disabling the OSPI peripheral first and re-enabling it afterwards. * SD/FLASH - Update comments regarding possible solutions to the catch-22 issue with SD/SPI/QUADSPI/OCTOSPI pin configurations. * OCTOSPI - Use device instance directly. * OCTOSPI - Prepare W25Q flash driver for octospi support. * OCTOSPI - Add octospi flash detection. Note: The method to detect flash chips is similar to the method used for QUADSPI and as such the code was used as a base. However the initial OCTOSPI implementation doesn't support the non-memory-mapped use-case so the un-tested code is gated with `USE_OCTOSPI_EXPERIMENTAL`. The key differences are: * uses octospi api not quadspi api. * flash chip clock speeds should not be changed for memory-mapped flash chips, bootloader already set it correctly. * only certain flash chips are capable of memory mapped support. * W25Q - Ensure w25q128fv_readRegister returns 0 if the receive fails. * OCTOSPI - Implement octoSpiTransmit1LINE, octoSpiReceive1LINE and octoSpiReceive4LINES. * OCTOSPI - Specify device from init. * OCTOSPI - More fixes and work on init. Current status is that memory mapped mode is disabled and flash chip is detected, but w25q128fv_detect should not be calling w25q128fv_reset. * FLASH - Add comment regarding wasted flash space on targets that only use one bus type for the flash chip. * FLASH - Split `detect` into `identify` and `configure`. * OCTOSPI - Extract flashMemoryMappedModeEnable/Disable to separate methods. * FLASH - Reduce size of targets that don't support the use of multiple flash interfaces. * Single-flash-chip targets usually only support one type of io interface. * Without this, compiler warnings are generated in `flashSpiInit` for targets that only use flash chip drivers that support quadspi or octospi that don't even use SPI for flash. * FLASH - Use MMFLASH_CODE/DATA to conditionally move code/data to RAM. Only targets compiled to support memory mapped flash chips need the some specific code in RAM. Otherwise the code/data should be in the normal linker section. * FLASH - W25Q Prepare for memory mapped flash usage. * Wait/Delay functions must work with interrupts disabled. * Code used for reading/writing must run from RAM. * OCTOSPI - Implement remaining required methods. * OCTOSPI - Fixes for earlier code (not last commit). * FLASH - W25Q update timeout values from Datasheet Rev L. * FLASH - Prepare flash driver for use when memory mapped flash is disabled. * System - Prepare microsISR for use when memory mapped flash is disabled. * FLASH - Add support for CONFIG_IN_MEMORY_MAPPED_FLASH. * Flash - Fix incorrect gating on cli flash commands. When compiling with USE_FLASH_CHIP and without USE_FLASHFS there were compiler warnings. * MMFLASH - Fix release-mode build. * FLASH - Allow SPI pins to be reconfigured when using CONFIG_IN_MEMORY_MAPPED_FLASH. MMFLASH only works via QuadSPI/OctoSPI peripherals. * EXST - Disable the 2GB workaround which is now causing a different error. The error we get with 'remove-section' enabled is: "error in private header data: sorry, cannot handle this file". The cause of this new error in the objcopy codebase is an out of memory condition, somehow the 2GB files and this error are related but the root cause of both is still unknown. * OCTOSPI - Add support for STM32H723. * STM32H723 - Add linker scripts for EXST usage. * NucleoH723ZG - Add build script to demonstrate OCTOSPI and Memory Mapped flash support. * FLASH - WUse the size in bits to set the size of the buffer. * FLASH - Fix typo in W25N driver defines. Was using W28N instead of W25N * OCTOSPI - Fix missing semilcolon when compiling without USE_FLASH_MEMORY_MAPPED. * OCTPSPI - Fix missing call to 'memoryMappedModeInit'. * SPRacingH7RF - Add example build script to allow for testing prior to unified target / cloud-build support.
This commit is contained in:
parent
ec20d399c5
commit
5213bb5871
42 changed files with 2447 additions and 209 deletions
|
@ -36,8 +36,12 @@
|
|||
#include "flash_w25m.h"
|
||||
#include "drivers/bus_spi.h"
|
||||
#include "drivers/bus_quadspi.h"
|
||||
#include "drivers/bus_octospi.h"
|
||||
#include "drivers/io.h"
|
||||
#include "drivers/time.h"
|
||||
#include "drivers/system.h"
|
||||
|
||||
#ifdef USE_FLASH_SPI
|
||||
|
||||
// 20 MHz max SPI frequency
|
||||
#define FLASH_MAX_SPI_CLK_HZ 20000000
|
||||
|
@ -47,13 +51,143 @@
|
|||
static extDevice_t devInstance;
|
||||
static extDevice_t *dev;
|
||||
|
||||
#endif
|
||||
|
||||
static flashDevice_t flashDevice;
|
||||
static flashPartitionTable_t flashPartitionTable;
|
||||
static int flashPartitions = 0;
|
||||
|
||||
#define FLASH_INSTRUCTION_RDID 0x9F
|
||||
|
||||
#ifdef USE_QUADSPI
|
||||
#ifdef USE_FLASH_MEMORY_MAPPED
|
||||
MMFLASH_CODE_NOINLINE void flashMemoryMappedModeDisable(void)
|
||||
{
|
||||
__disable_irq();
|
||||
#ifdef USE_FLASH_OCTOSPI
|
||||
octoSpiDisableMemoryMappedMode(flashDevice.io.handle.octoSpi);
|
||||
#else
|
||||
#error Invalid configuration - Not implemented
|
||||
#endif
|
||||
}
|
||||
|
||||
MMFLASH_CODE_NOINLINE void flashMemoryMappedModeEnable(void)
|
||||
{
|
||||
#ifdef USE_FLASH_OCTOSPI
|
||||
octoSpiEnableMemoryMappedMode(flashDevice.io.handle.octoSpi);
|
||||
__enable_irq();
|
||||
#else
|
||||
#error Invalid configuration - Not implemented
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_FLASH_OCTOSPI
|
||||
MMFLASH_CODE_NOINLINE static bool flashOctoSpiInit(const flashConfig_t *flashConfig)
|
||||
{
|
||||
bool detected = false;
|
||||
|
||||
enum {
|
||||
TRY_1LINE = 0, TRY_4LINE, BAIL
|
||||
} phase = TRY_1LINE;
|
||||
|
||||
#ifdef USE_FLASH_MEMORY_MAPPED
|
||||
bool memoryMappedModeEnabledOnBoot = isMemoryMappedModeEnabledOnBoot();
|
||||
#else
|
||||
bool memoryMappedModeEnabledOnBoot = false;
|
||||
#endif
|
||||
|
||||
#ifndef USE_OCTOSPI_EXPERIMENTAL
|
||||
if (!memoryMappedModeEnabledOnBoot) {
|
||||
return false; // Not supported yet, enable USE_OCTOSPI_EXPERIMENTAL and test/update implementation as required.
|
||||
}
|
||||
#endif
|
||||
|
||||
OCTOSPI_TypeDef *instance = octoSpiInstanceByDevice(OCTOSPI_CFG_TO_DEV(flashConfig->octoSpiDevice));
|
||||
|
||||
flashDevice.io.handle.octoSpi = instance;
|
||||
flashDevice.io.mode = FLASHIO_OCTOSPI;
|
||||
|
||||
if (memoryMappedModeEnabledOnBoot) {
|
||||
flashMemoryMappedModeDisable();
|
||||
}
|
||||
|
||||
do {
|
||||
#ifdef USE_OCTOSPI_EXPERIMENTAL
|
||||
if (!memoryMappedMode) {
|
||||
octoSpiSetDivisor(instance, OCTOSPI_CLOCK_INITIALISATION);
|
||||
}
|
||||
#endif
|
||||
// for the memory-mapped use-case, we rely on the bootloader to have already selected the correct speed for the flash chip.
|
||||
|
||||
// 3 bytes for what we need, but some IC's need 8 dummy cycles after the instruction, so read 4 and make two attempts to
|
||||
// assemble the chip id from the response.
|
||||
uint8_t readIdResponse[4];
|
||||
|
||||
bool status = false;
|
||||
switch (phase) {
|
||||
case TRY_1LINE:
|
||||
status = octoSpiReceive1LINE(instance, FLASH_INSTRUCTION_RDID, 0, readIdResponse, 4);
|
||||
break;
|
||||
case TRY_4LINE:
|
||||
status = octoSpiReceive4LINES(instance, FLASH_INSTRUCTION_RDID, 2, readIdResponse, 3);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!status) {
|
||||
phase++;
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef USE_OCTOSPI_EXPERIMENTAL
|
||||
if (!memoryMappedModeEnabledOnBoot) {
|
||||
octoSpiSetDivisor(instance, OCTOSPI_CLOCK_ULTRAFAST);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (uint8_t offset = 0; offset <= 1 && !detected; offset++) {
|
||||
|
||||
uint32_t jedecID = (readIdResponse[offset + 0] << 16) | (readIdResponse[offset + 1] << 8) | (readIdResponse[offset + 2]);
|
||||
|
||||
if (offset == 0) {
|
||||
#if defined(USE_FLASH_W25Q128FV)
|
||||
if (!detected && w25q128fv_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (offset == 1) {
|
||||
#ifdef USE_OCTOSPI_EXPERIMENTAL
|
||||
if (!memoryMappedModeEnabledOnBoot) {
|
||||
// These flash chips DO NOT support memory mapped mode; suitable flash read commands must be available.
|
||||
#if defined(USE_FLASH_W25N01G)
|
||||
if (!detected && w25n01g_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
#if defined(USE_FLASH_W25M02G)
|
||||
if (!detected && w25m_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
phase++;
|
||||
} while (phase != BAIL && !detected);
|
||||
|
||||
if (memoryMappedModeEnabledOnBoot) {
|
||||
flashMemoryMappedModeEnable();
|
||||
}
|
||||
return detected;
|
||||
|
||||
}
|
||||
#endif // USE_FLASH_OCTOSPI
|
||||
|
||||
#ifdef USE_FLASH_QUADSPI
|
||||
static bool flashQuadSpiInit(const flashConfig_t *flashConfig)
|
||||
{
|
||||
bool detected = false;
|
||||
|
@ -63,6 +197,9 @@ static bool flashQuadSpiInit(const flashConfig_t *flashConfig)
|
|||
|
||||
QUADSPI_TypeDef *hqspi = quadSpiInstanceByDevice(QUADSPI_CFG_TO_DEV(flashConfig->quadSpiDevice));
|
||||
|
||||
flashDevice.io.handle.quadSpi = hqspi;
|
||||
flashDevice.io.mode = FLASHIO_QUADSPI;
|
||||
|
||||
do {
|
||||
quadSpiSetDivisor(hqspi, QUADSPI_CLOCK_INITIALISATION);
|
||||
|
||||
|
@ -87,19 +224,16 @@ static bool flashQuadSpiInit(const flashConfig_t *flashConfig)
|
|||
continue;
|
||||
}
|
||||
|
||||
flashDevice.io.handle.quadSpi = hqspi;
|
||||
flashDevice.io.mode = FLASHIO_QUADSPI;
|
||||
|
||||
quadSpiSetDivisor(hqspi, QUADSPI_CLOCK_ULTRAFAST);
|
||||
|
||||
|
||||
for (uint8_t offset = 0; offset <= 1 && !detected; offset++) {
|
||||
|
||||
uint32_t chipID = (readIdResponse[offset + 0] << 16) | (readIdResponse[offset + 1] << 8) | (readIdResponse[offset + 2]);
|
||||
uint32_t jedecID = (readIdResponse[offset + 0] << 16) | (readIdResponse[offset + 1] << 8) | (readIdResponse[offset + 2]);
|
||||
|
||||
if (offset == 0) {
|
||||
#ifdef USE_FLASH_W25Q128FV
|
||||
if (!detected && w25q128fv_detect(&flashDevice, chipID)) {
|
||||
#if defined(USE_FLASH_W25Q128FV)
|
||||
if (!detected && w25q128fv_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
|
@ -112,20 +246,20 @@ static bool flashQuadSpiInit(const flashConfig_t *flashConfig)
|
|||
}
|
||||
|
||||
if (offset == 1) {
|
||||
#ifdef USE_FLASH_W25N01G
|
||||
if (!detected && w25n01g_detect(&flashDevice, chipID)) {
|
||||
#if defined(USE_FLASH_W25N01G)
|
||||
if (!detected && w25n01g_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
#if defined(USE_FLASH_W25M02G)
|
||||
if (!detected && w25m_detect(&flashDevice, chipID)) {
|
||||
if (!detected && w25m_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (detected) {
|
||||
flashDevice.geometry.jedecId = chipID;
|
||||
flashDevice.geometry.jedecId = jedecID;
|
||||
}
|
||||
}
|
||||
phase++;
|
||||
|
@ -133,15 +267,9 @@ static bool flashQuadSpiInit(const flashConfig_t *flashConfig)
|
|||
|
||||
return detected;
|
||||
}
|
||||
#endif // USE_QUADSPI
|
||||
|
||||
#ifdef USE_SPI
|
||||
|
||||
void flashPreInit(const flashConfig_t *flashConfig)
|
||||
{
|
||||
spiPreinitRegister(flashConfig->csTag, IOCFG_IPU, 1);
|
||||
}
|
||||
#endif // USE_FLASH_QUADSPI
|
||||
|
||||
#ifdef USE_FLASH_SPI
|
||||
static bool flashSpiInit(const flashConfig_t *flashConfig)
|
||||
{
|
||||
bool detected = false;
|
||||
|
@ -186,39 +314,39 @@ static bool flashSpiInit(const flashConfig_t *flashConfig)
|
|||
spiReadRegBuf(dev, FLASH_INSTRUCTION_RDID, readIdResponse, sizeof(readIdResponse));
|
||||
|
||||
// Manufacturer, memory type, and capacity
|
||||
uint32_t chipID = (readIdResponse[0] << 16) | (readIdResponse[1] << 8) | (readIdResponse[2]);
|
||||
uint32_t jedecID = (readIdResponse[0] << 16) | (readIdResponse[1] << 8) | (readIdResponse[2]);
|
||||
|
||||
#ifdef USE_FLASH_M25P16
|
||||
if (m25p16_detect(&flashDevice, chipID)) {
|
||||
if (m25p16_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(USE_FLASH_W25M512) || defined(USE_FLASH_W25M)
|
||||
if (!detected && w25m_detect(&flashDevice, chipID)) {
|
||||
if (!detected && w25m_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!detected) {
|
||||
// Newer chips
|
||||
chipID = (readIdResponse[1] << 16) | (readIdResponse[2] << 8) | (readIdResponse[3]);
|
||||
jedecID = (readIdResponse[1] << 16) | (readIdResponse[2] << 8) | (readIdResponse[3]);
|
||||
}
|
||||
|
||||
#ifdef USE_FLASH_W25N01G
|
||||
if (!detected && w25n01g_detect(&flashDevice, chipID)) {
|
||||
if (!detected && w25n01g_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_FLASH_W25M02G
|
||||
if (!detected && w25m_detect(&flashDevice, chipID)) {
|
||||
if (!detected && w25m_identify(&flashDevice, jedecID)) {
|
||||
detected = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (detected) {
|
||||
flashDevice.geometry.jedecId = chipID;
|
||||
flashDevice.geometry.jedecId = jedecID;
|
||||
return detected;
|
||||
}
|
||||
|
||||
|
@ -226,39 +354,65 @@ static bool flashSpiInit(const flashConfig_t *flashConfig)
|
|||
|
||||
return false;
|
||||
}
|
||||
#endif // USE_SPI
|
||||
#endif // USE_FLASH_SPI
|
||||
|
||||
void flashPreInit(const flashConfig_t *flashConfig)
|
||||
{
|
||||
spiPreinitRegister(flashConfig->csTag, IOCFG_IPU, 1);
|
||||
}
|
||||
|
||||
bool flashDeviceInit(const flashConfig_t *flashConfig)
|
||||
{
|
||||
#ifdef USE_SPI
|
||||
bool haveFlash = false;
|
||||
|
||||
#ifdef USE_FLASH_SPI
|
||||
bool useSpi = (SPI_CFG_TO_DEV(flashConfig->spiDevice) != SPIINVALID);
|
||||
|
||||
if (useSpi) {
|
||||
return flashSpiInit(flashConfig);
|
||||
haveFlash = flashSpiInit(flashConfig);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_QUADSPI
|
||||
#ifdef USE_FLASH_QUADSPI
|
||||
bool useQuadSpi = (QUADSPI_CFG_TO_DEV(flashConfig->quadSpiDevice) != QUADSPIINVALID);
|
||||
if (useQuadSpi) {
|
||||
return flashQuadSpiInit(flashConfig);
|
||||
haveFlash = flashQuadSpiInit(flashConfig);
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
#ifdef USE_FLASH_OCTOSPI
|
||||
bool useOctoSpi = (OCTOSPI_CFG_TO_DEV(flashConfig->octoSpiDevice) != OCTOSPIINVALID);
|
||||
if (useOctoSpi) {
|
||||
haveFlash = flashOctoSpiInit(flashConfig);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (haveFlash && flashDevice.vTable->configure) {
|
||||
uint32_t configurationFlags = 0;
|
||||
|
||||
#ifdef USE_FLASH_MEMORY_MAPPED
|
||||
if (isMemoryMappedModeEnabledOnBoot()) {
|
||||
configurationFlags |= FLASH_CF_SYSTEM_IS_MEMORY_MAPPED;
|
||||
}
|
||||
#endif
|
||||
|
||||
flashDevice.vTable->configure(&flashDevice, configurationFlags);
|
||||
}
|
||||
|
||||
return haveFlash;
|
||||
}
|
||||
|
||||
bool flashIsReady(void)
|
||||
MMFLASH_CODE bool flashIsReady(void)
|
||||
{
|
||||
return flashDevice.vTable->isReady(&flashDevice);
|
||||
}
|
||||
|
||||
bool flashWaitForReady(void)
|
||||
MMFLASH_CODE bool flashWaitForReady(void)
|
||||
{
|
||||
return flashDevice.vTable->waitForReady(&flashDevice);
|
||||
}
|
||||
|
||||
void flashEraseSector(uint32_t address)
|
||||
MMFLASH_CODE void flashEraseSector(uint32_t address)
|
||||
{
|
||||
flashDevice.callback = NULL;
|
||||
flashDevice.vTable->eraseSector(&flashDevice, address);
|
||||
|
@ -273,12 +427,12 @@ void flashEraseCompletely(void)
|
|||
/* The callback, if provided, will receive the totoal number of bytes transfered
|
||||
* by each call to flashPageProgramContinue() once the transfer completes.
|
||||
*/
|
||||
void flashPageProgramBegin(uint32_t address, void (*callback)(uint32_t length))
|
||||
MMFLASH_CODE void flashPageProgramBegin(uint32_t address, void (*callback)(uint32_t length))
|
||||
{
|
||||
flashDevice.vTable->pageProgramBegin(&flashDevice, address, callback);
|
||||
}
|
||||
|
||||
uint32_t flashPageProgramContinue(const uint8_t **buffers, uint32_t *bufferSizes, uint32_t bufferCount)
|
||||
MMFLASH_CODE uint32_t flashPageProgramContinue(const uint8_t **buffers, uint32_t *bufferSizes, uint32_t bufferCount)
|
||||
{
|
||||
uint32_t maxBytesToWrite = flashDevice.geometry.pageSize - (flashDevice.currentWriteAddress % flashDevice.geometry.pageSize);
|
||||
|
||||
|
@ -299,23 +453,23 @@ uint32_t flashPageProgramContinue(const uint8_t **buffers, uint32_t *bufferSizes
|
|||
return flashDevice.vTable->pageProgramContinue(&flashDevice, buffers, bufferSizes, bufferCount);
|
||||
}
|
||||
|
||||
void flashPageProgramFinish(void)
|
||||
MMFLASH_CODE void flashPageProgramFinish(void)
|
||||
{
|
||||
flashDevice.vTable->pageProgramFinish(&flashDevice);
|
||||
}
|
||||
|
||||
void flashPageProgram(uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length))
|
||||
MMFLASH_CODE void flashPageProgram(uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length))
|
||||
{
|
||||
flashDevice.vTable->pageProgram(&flashDevice, address, data, length, callback);
|
||||
}
|
||||
|
||||
int flashReadBytes(uint32_t address, uint8_t *buffer, uint32_t length)
|
||||
MMFLASH_CODE int flashReadBytes(uint32_t address, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
flashDevice.callback = NULL;
|
||||
return flashDevice.vTable->readBytes(&flashDevice, address, buffer, length);
|
||||
}
|
||||
|
||||
void flashFlush(void)
|
||||
MMFLASH_CODE void flashFlush(void)
|
||||
{
|
||||
if (flashDevice.vTable->flush) {
|
||||
flashDevice.vTable->flush(&flashDevice);
|
||||
|
@ -381,7 +535,7 @@ static void flashConfigurePartitions(void)
|
|||
startSector = 0;
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_IN_EXTERNAL_FLASH)
|
||||
#if defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
|
||||
const uint32_t configSize = EEPROM_SIZE;
|
||||
flashSector_t configSectors = (configSize / flashGeometry->sectorSize);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue