feat(color): Use DMA for SPI flash access (#4478)
This commit is contained in:
@@ -62,7 +62,7 @@
|
||||
#endif
|
||||
|
||||
#if defined(SPI_FLASH)
|
||||
#include "spi_flash.h"
|
||||
#include "diskio_spi_flash.h"
|
||||
#define SEL_CLEAR_FLASH_STORAGE_MENU_LEN 2
|
||||
#endif
|
||||
|
||||
@@ -537,7 +537,9 @@ int bootloaderMain()
|
||||
lcdRefresh();
|
||||
if(event != EVT_KEY_BREAK(KEY_ENTER))
|
||||
continue;
|
||||
flashSpiEraseAll();
|
||||
sdDone();
|
||||
spiFlashDiskEraseAll();
|
||||
sdInit();
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
#endif
|
||||
|
||||
@@ -45,6 +45,10 @@ static const stm32_spi_t _sd_spi_hw = {
|
||||
.DMA_Channel = SD_SPI_DMA_CHANNEL,
|
||||
.txDMA_Stream = SD_SPI_DMA_TX_STREAM,
|
||||
.rxDMA_Stream = SD_SPI_DMA_RX_STREAM,
|
||||
.DMA_FIFOMode = LL_DMA_FIFOMODE_ENABLE,
|
||||
.DMA_FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_2,
|
||||
.DMA_MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD,
|
||||
.DMA_MemBurst = LL_DMA_MBURST_INC4,
|
||||
};
|
||||
|
||||
static uint32_t _sdcard_sectors;
|
||||
|
||||
@@ -64,6 +64,12 @@ static bool flashErase(uint32_t addr)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool flashBlockErase(uint32_t addr)
|
||||
{
|
||||
flashSpiBlockErase(addr);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool isFlashErased(uint32_t addr)
|
||||
{
|
||||
return flashSpiIsErased(addr);
|
||||
@@ -73,9 +79,10 @@ static const FrFTLOps _frftl_cb = {
|
||||
.flashRead = flashRead,
|
||||
.flashProgram = flashWrite,
|
||||
.flashErase = flashErase,
|
||||
.flashBlockErase = flashBlockErase,
|
||||
.isFlashErased = isFlashErased,
|
||||
};
|
||||
#endif
|
||||
#endif // USE_FLASH_FTL
|
||||
|
||||
static DSTATUS spi_flash_initialize(BYTE lun)
|
||||
{
|
||||
@@ -188,6 +195,17 @@ static DRESULT spi_flash_ioctl(BYTE lun, BYTE ctrl, void *buff)
|
||||
return res;
|
||||
}
|
||||
|
||||
void spiFlashDiskEraseAll()
|
||||
{
|
||||
#if defined(USE_FLASH_FTL)
|
||||
if (frftlInitDone) {
|
||||
ftlDeInit(&_frftl);
|
||||
frftlInitDone = false;
|
||||
}
|
||||
#endif
|
||||
flashSpiEraseAll();
|
||||
}
|
||||
|
||||
const diskio_driver_t spi_flash_diskio_driver = {
|
||||
.initialize = spi_flash_initialize,
|
||||
.status = spi_flash_status,
|
||||
|
||||
@@ -23,4 +23,6 @@
|
||||
|
||||
#include "hal/fatfs_diskio.h"
|
||||
|
||||
void spiFlashDiskEraseAll();
|
||||
|
||||
extern const diskio_driver_t spi_flash_diskio_driver;
|
||||
|
||||
@@ -74,10 +74,14 @@ const stm32_spi_t _flash_spi = {
|
||||
.SPI_Pins = FLASH_SPI_SCK_GPIO_PIN | FLASH_SPI_MISO_GPIO_PIN | FLASH_SPI_MOSI_GPIO_PIN,
|
||||
.CS_GPIOx = FLASH_SPI_CS_GPIO,
|
||||
.CS_Pin = FLASH_SPI_CS_GPIO_PIN,
|
||||
.DMA = nullptr,
|
||||
.DMA_Channel = 0,
|
||||
.txDMA_Stream = 0,
|
||||
.rxDMA_Stream = 0,
|
||||
.DMA = FLASH_SPI_DMA,
|
||||
.DMA_Channel = FLASH_SPI_DMA_CHANNEL,
|
||||
.txDMA_Stream = FLASH_SPI_DMA_TX_STREAM,
|
||||
.rxDMA_Stream = FLASH_SPI_DMA_RX_STREAM,
|
||||
.DMA_FIFOMode = LL_DMA_FIFOMODE_ENABLE,
|
||||
.DMA_FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_2,
|
||||
.DMA_MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD,
|
||||
.DMA_MemBurst = LL_DMA_MBURST_INC4,
|
||||
};
|
||||
|
||||
static SpiFlashDescriptor _flashDescriptor;
|
||||
@@ -95,11 +99,21 @@ static inline uint8_t flash_read_byte()
|
||||
return flash_rw_byte(0xFF);
|
||||
}
|
||||
|
||||
static void flash_rw_bytes(const uint8_t* tx, uint8_t* rx, uint16_t count)
|
||||
static inline void flash_rw_bytes(const uint8_t* tx, uint8_t* rx, uint16_t count)
|
||||
{
|
||||
stm32_spi_transfer_bytes(&_flash_spi, tx, rx, count);
|
||||
}
|
||||
|
||||
static inline void flash_dma_read_bytes(uint8_t* buffer, uint16_t count)
|
||||
{
|
||||
stm32_spi_dma_receive_bytes(&_flash_spi, buffer, count);
|
||||
}
|
||||
|
||||
static inline void flash_dma_write_bytes(const uint8_t* buffer, uint16_t count)
|
||||
{
|
||||
stm32_spi_dma_transmit_bytes(&_flash_spi, buffer, count);
|
||||
}
|
||||
|
||||
static inline void flash_put_cmd_addr(uint8_t cmd, uint32_t addr)
|
||||
{
|
||||
addr |= cmd << 24;
|
||||
@@ -293,7 +307,7 @@ uint32_t flashSpiRead(uint32_t address, uint8_t* data, uint32_t size)
|
||||
} else {
|
||||
flash_put_cmd_addr(FLASH_CMD_READ, address);
|
||||
}
|
||||
flash_rw_bytes(0, data, size);
|
||||
flash_dma_read_bytes(data, size);
|
||||
|
||||
flash_unselect();
|
||||
return size;
|
||||
@@ -317,7 +331,7 @@ uint32_t flashSpiWrite(uint32_t address, const uint8_t* data, uint32_t size)
|
||||
} else {
|
||||
flash_put_cmd_addr(FLASH_CMD_WRITE, address);
|
||||
}
|
||||
flash_rw_bytes(data, 0, size);
|
||||
flash_dma_write_bytes(data, size);
|
||||
flash_unselect();
|
||||
|
||||
flash_wait_for_not_busy();
|
||||
|
||||
@@ -151,8 +151,10 @@ static void _config_dma_streams(const stm32_spi_t* spi)
|
||||
dmaInit.PeriphOrM2MSrcAddress = (uint32_t)&spi->SPIx->DR;
|
||||
dmaInit.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
|
||||
dmaInit.Priority = LL_DMA_PRIORITY_VERYHIGH;
|
||||
dmaInit.FIFOMode = LL_DMA_FIFOMODE_ENABLE;
|
||||
dmaInit.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_FULL;
|
||||
dmaInit.FIFOMode = spi->DMA_FIFOMode;
|
||||
dmaInit.FIFOThreshold = spi->DMA_FIFOThreshold;
|
||||
dmaInit.MemoryOrM2MDstDataSize = spi->DMA_MemoryOrM2MDstDataSize;
|
||||
dmaInit.MemBurst = spi->DMA_MemBurst;
|
||||
|
||||
dmaInit.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
|
||||
LL_DMA_Init(spi->DMA, spi->rxDMA_Stream, &dmaInit);
|
||||
|
||||
@@ -36,6 +36,10 @@ struct stm32_spi_t {
|
||||
uint32_t DMA_Channel;
|
||||
uint32_t txDMA_Stream;
|
||||
uint32_t rxDMA_Stream;
|
||||
uint32_t DMA_FIFOMode;
|
||||
uint32_t DMA_FIFOThreshold;
|
||||
uint32_t DMA_MemoryOrM2MDstDataSize;
|
||||
uint32_t DMA_MemBurst;
|
||||
};
|
||||
|
||||
void stm32_spi_enable_clock(SPI_TypeDef *SPIx);
|
||||
|
||||
@@ -256,7 +256,7 @@ int8_t STORAGE_Read_FS (uint8_t lun,
|
||||
*/
|
||||
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
|
||||
{
|
||||
WATCHDOG_SUSPEND(100/*1s*/);
|
||||
WATCHDOG_SUSPEND(500/*5s*/);
|
||||
|
||||
#if defined(FWDRIVE)
|
||||
if (lun == STORAGE_EEPROM_LUN) {
|
||||
|
||||
Reference in New Issue
Block a user