feat: SPI flash driver (#4012)

Co-authored-by: Richard Li <richard.li@ces.hk>
This commit is contained in:
Raphael Coeffic
2023-10-13 19:51:11 +10:00
committed by GitHub
co-authored by Richard Li
parent 7400a598a1
commit cb16786a74
17 changed files with 1977 additions and 31 deletions
@@ -78,6 +78,11 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
../../../../../thirdparty/libopenui/thirdparty/lz4/lz4.c
../../../../../targets/common/arm/stm32/diskio_sdio.cpp
../../../../../targets/common/arm/stm32/rtc_driver.cpp
../../../../../targets/common/arm/stm32/diskio_spi_flash.cpp
../../../../../targets/common/arm/stm32/spi_flash.cpp
../../../../../targets/common/arm/stm32/stm32_spi.cpp
../../../../../drivers/frftl.cpp
../../../../../crc.cpp
../../../../../targets/${TARGET_DIR}/sdram_driver.c
../../../../../targets/${TARGET_DIR}/haptic_driver.cpp
)
@@ -41,15 +41,21 @@ UINT BlockCount;
void sdInit(void)
{
static FATFS fatFS __DMA;
storageInit();
static FATFS fatFS __DMA;
if (f_mount(&fatFS, "", 1) == FR_OK) {
f_chdir("/");
}
}
void sdDone()
{
// unmount
f_mount(0, "", 0);
storageDeInit();
}
FRESULT openBinDir(MemoryType mt)
{
FRESULT fr = f_chdir(getBinaryPath(mt));
@@ -550,11 +550,13 @@ int bootloaderMain()
if (state != ST_FLASHING && state != ST_USB) {
if (pwrOffPressed()) {
sdDone();
boardOff();
}
}
if (state == ST_REBOOT) {
sdDone();
#if !defined(SIMU)
#if defined(RTC_BACKUP_RAM)
rtcInit();
@@ -0,0 +1,195 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "diskio_spi_flash.h"
#include "spi_flash.h"
#if !defined(DISABLE_FLASH_FTL)
#define USE_FLASH_FTL
#endif
#if defined(USE_FLASH_FTL)
#include "drivers/frftl.h"
static FrFTL _frftl;
static bool flashRead(uint32_t addr, uint8_t* buf, uint32_t len)
{
flashSpiRead(addr, buf, len);
return true;
}
static bool flashWrite(uint32_t addr, const uint8_t *buf, uint32_t len)
{
uint32_t pageSize = flashSpiGetPageSize();
if(len % pageSize != 0)
return false;
while(len > 0) {
flashSpiWrite(addr, buf, pageSize);
len -= pageSize;
buf += pageSize;
addr += pageSize;
}
if(len != 0) {
return false;
}
return true;
}
static bool flashErase(uint32_t addr)
{
flashSpiErase(addr);
return true;
}
static bool isFlashErased(uint32_t addr)
{
return flashSpiIsErased(addr);
}
static const FrFTLOps _frftl_cb = {
.flashRead = flashRead,
.flashProgram = flashWrite,
.flashErase = flashErase,
.isFlashErased = isFlashErased,
};
#endif
static DSTATUS spi_flash_initialize(BYTE lun)
{
if (!flashSpiInit()) {
return STA_NOINIT;
}
#if defined(USE_FLASH_FTL)
int flashSize = flashSpiGetSize();
int flashSizeMB = flashSize / 1024 / 1024;
if (!ftlInit(&_frftl, &_frftl_cb, flashSizeMB)) {
return STA_NOINIT;
}
#endif
return 0;
}
static DSTATUS spi_flash_status (BYTE lun)
{
return 0;
}
static DRESULT spi_flash_read(BYTE lun, BYTE * buff, DWORD sector, UINT count)
{
#if defined(USE_FLASH_FTL)
while(count) {
if(!ftlRead(&_frftl, sector, (uint8_t*)buff)) {
return RES_ERROR;
}
buff += 512;
sector++;
count --;
}
#else
flashSpiRead((uint32_t)sector * 512, buff, count * 512);
#endif
return RES_OK;
}
static DRESULT spi_flash_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
{
#if defined(USE_FLASH_FTL)
if (!ftlWrite(&_frftl, sector, count, (uint8_t*)buff)) {
return RES_ERROR;
}
#else
// write in page size
uint32_t address = (uint32_t)sector * 512;
count <<= 1;
while(count) {
if (!flashSpiWrite(address, buff, 256)) {
return RES_ERROR;
}
buff += 256;
address += 256;
count -= 1;
}
#endif
return RES_OK;
}
static DRESULT spi_flash_ioctl(BYTE lun, BYTE ctrl, void *buff)
{
DRESULT res = RES_OK;
switch (ctrl) {
case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
#if defined(USE_FLASH_FTL)
*(DWORD*)buff = _frftl.usableSectorCount;
#else
*(DWORD*)buff = flashSpiGetSize() / 512;
#endif
break;
case GET_SECTOR_SIZE: /* Get R/W sector size (WORD) */
*(WORD*)buff = 512;
break;
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
*(DWORD*)buff = 4096 / 512;
break;
case CTRL_SYNC:
#if defined(USE_FLASH_FTL)
if (!ftlSync(&_frftl)) {
res = RES_ERROR;
}
#else
flashSpiSync();
#endif
break;
case CTRL_TRIM:
#if defined(USE_FLASH_FTL)
if (!ftlTrim(&_frftl, *(DWORD*)buff, 1 + *((DWORD*)buff + 1) - *(DWORD*)buff)) {
res = RES_ERROR;
}
#endif
break;
}
return res;
}
const diskio_driver_t spi_flash_diskio_driver = {
.initialize = spi_flash_initialize,
.status = spi_flash_status,
.read = spi_flash_read,
.write = spi_flash_write,
.ioctl = spi_flash_ioctl,
};
@@ -0,0 +1,26 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "hal/fatfs_diskio.h"
extern const diskio_driver_t spi_flash_diskio_driver;
@@ -0,0 +1,378 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "stm32_spi.h"
#include "delays_driver.h"
#include "hal.h"
#define CS_HIGH() stm32_spi_unselect(&_flash_spi)
#define CS_LOW() stm32_spi_select(&_flash_spi)
#define MIN(a,b) (a < b ? a : b)
#define FLASH_SECTOR_SIZE 4096
#define FLASH_SECTOR_MASK (FLASH_SECTOR_SIZE - 1)
#define FLASH_BLOCK_SIZE 32768
#define FLASH_BLOCK_MASK (FLASH_BLOCK_SIZE - 1)
#define FLASH_PAGE_SIZE 256
#define FLASH_PAGE_MASK (FLASH_PAGE_SIZE - 1)
#define FLASH_CMD_READ_ID 0x90
#define FLASH_CMD_READ_JEDEC_ID 0x9f
#define FLASH_CMD_READ_SFDP 0x5a
#define FLASH_CMD_WRITE 0x02
#define FLASH_CMD_READ 0x03
#define FLASH_CMD_STATUS 0x05
#define FLASH_CMD_WRITE_ENABLE 0x06
#define FLASH_CMD_SECTOR_ERASE 0x20
#define FLASH_CMD_BLOCK_ERASE 0x52
#define FLASH_CMD_CHIP_ERASE 0xc7
#define FLASH_CMD_EN4B 0xb7
struct SpiFlashDescriptor {
uint16_t id;
uint8_t log2Size;
bool use4BytesAddress;
};
// * RadioMaster/Eachine TX16S, RadioKing TX18S and Jumper T18 use GD25Q127C (16 MByte)
// * FlySky PL18, Paladin EV and NV14 use WinBond W25Q64JV (8 MByte)
// TODO:
// - fallback for flash size discovery (no JEDEC SFDP; see 'pico-bootrom')
const stm32_spi_t _flash_spi = {
.SPIx = FLASH_SPI,
.SPI_GPIOx = FLASH_SPI_GPIO,
.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,
};
static SpiFlashDescriptor _flashDescriptor;
static inline void flash_select() { stm32_spi_select(&_flash_spi); }
static inline void flash_unselect() { stm32_spi_unselect(&_flash_spi); }
static inline uint8_t flash_rw_byte(uint8_t value)
{
return stm32_spi_transfer_byte(&_flash_spi, value);
}
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)
{
stm32_spi_transfer_bytes(&_flash_spi, tx, rx, count);
}
static inline void flash_put_cmd_addr(uint8_t cmd, uint32_t addr)
{
addr |= cmd << 24;
for (int i = 0; i < 4; i++) {
flash_rw_byte(addr >> 24);
addr <<= 8;
}
}
static inline void flash_put_cmd_4b_addr(uint8_t cmd, uint32_t addr)
{
flash_rw_byte(cmd);
for (int i = 0; i < 4; i++) {
flash_rw_byte(addr >> 24);
addr <<= 8;
}
}
static inline void flash_do_cmd(uint8_t cmd, const uint8_t* tx, uint8_t* rx, uint16_t count)
{
flash_select();
flash_rw_byte(cmd);
flash_rw_bytes(tx, rx, count);
flash_unselect();
}
static void flash_wait_for_not_busy()
{
uint8_t status;
do {
flash_do_cmd(FLASH_CMD_STATUS, 0, &status, 1);
} while (status & 0x01);
}
static void flash_enable_write()
{
flash_do_cmd(FLASH_CMD_WRITE_ENABLE, 0, 0, 0);
}
static void flash_read_sfdp(uint32_t addr, uint8_t *rx, uint16_t count)
{
flash_select();
flash_put_cmd_addr(FLASH_CMD_READ_SFDP, addr);
flash_read_byte(); // dummy byte
flash_rw_bytes(0, rx, count);
flash_unselect();
}
static inline uint32_t bytes_to_u32le(const uint8_t *b) {
return b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
}
static bool flash_read_id(SpiFlashDescriptor* desc)
{
uint8_t rxbuf[2];
flash_select();
flash_put_cmd_addr(FLASH_CMD_READ_ID, 0);
flash_rw_bytes(0, rxbuf, 2);
flash_unselect();
uint16_t id = (rxbuf[0] << 8) | rxbuf[1];
if (id == 0xFFFF) {
return false;
}
desc->id = id;
return true;
}
static bool flash_read_sfdp(SpiFlashDescriptor* desc)
{
uint8_t rxbuf[16];
// check magic signature
flash_read_sfdp(0, rxbuf, 16);
if (rxbuf[0] != 'S' || rxbuf[1] != 'F' || rxbuf[2] != 'D' || rxbuf[3] != 'P') {
return false;
}
// check JEDEC ID is 0
if (rxbuf[8] != 0) {
return false;
}
// read param table
uint32_t param_table_ptr = bytes_to_u32le(rxbuf + 12) & 0xffffffu;
flash_read_sfdp(param_table_ptr, rxbuf, 8);
// 1st DWORD
uint32_t param_table_dword = bytes_to_u32le(rxbuf);
if ((param_table_dword & 3) != 1) {
// 4 kilobyte erase is unavailable
return false;
}
if (((param_table_dword >> 17) & 3) != 0) {
// 4-byte addressing supported
desc->use4BytesAddress = true;
flash_do_cmd(FLASH_CMD_EN4B, 0, 0, 0);
}
// 2nd DWORD: flash memory density
// - MSB set: array >= 2 Gbit, encoded as log2 of number of bits
// - MSB clear: array < 2 Gbit, encoded as direct bit count
param_table_dword = bytes_to_u32le(rxbuf + 4);
if (param_table_dword & (1u << 31)) {
param_table_dword &= ~(1u << 31);
} else {
uint32_t ctr = 0;
param_table_dword += 1;
while (param_table_dword >>= 1)
++ctr;
param_table_dword = ctr;
}
desc->log2Size = param_table_dword - 3;
return true;
}
bool flashSpiInit(void)
{
stm32_spi_init(&_flash_spi);
delay_ms(1);
if (!flash_read_id(&_flashDescriptor)) {
return false;
}
if (!flash_read_sfdp(&_flashDescriptor)) {
return false;
}
return true;
}
void flashSpiSync()
{
uint8_t status;
do {
flash_do_cmd(FLASH_CMD_STATUS, 0, &status, 1);
} while (status & 0x01);
}
uint32_t flashSpiGetSize()
{
return (1UL << _flashDescriptor.log2Size);
}
bool flashSpiIsErased(uint32_t address)
{
// verify sector alignment
if((address & FLASH_SECTOR_MASK) != 0)
return false;
flash_wait_for_not_busy();
flash_select();
if (_flashDescriptor.use4BytesAddress) {
flash_put_cmd_4b_addr(FLASH_CMD_READ, address);
} else {
flash_put_cmd_addr(FLASH_CMD_READ, address);
}
bool ret = true;
for(uint32_t i = 0; i < FLASH_SECTOR_SIZE; i++) {
uint8_t byte = flash_rw_byte(0xFF);
if (byte != 0xff) {
ret = false;
break;
}
}
flash_unselect();
return ret;
}
uint32_t flashSpiRead(uint32_t address, uint8_t* data, uint32_t size)
{
size = MIN(size, (uint32_t)(flashSpiGetSize() - address));
flash_wait_for_not_busy();
flash_select();
if (_flashDescriptor.use4BytesAddress) {
flash_put_cmd_4b_addr(FLASH_CMD_READ, address);
} else {
flash_put_cmd_addr(FLASH_CMD_READ, address);
}
flash_rw_bytes(0, data, size);
flash_unselect();
return size;
}
uint32_t flashSpiWrite(uint32_t address, const uint8_t* data, uint32_t size)
{
size = MIN(size, (uint32_t)(flashSpiGetSize() - address));
// verify page alignment and size
if (size != FLASH_PAGE_SIZE || (address & FLASH_PAGE_MASK) != 0) {
return 0;
}
flash_wait_for_not_busy();
flash_enable_write();
flash_select();
if (_flashDescriptor.use4BytesAddress) {
flash_put_cmd_4b_addr(FLASH_CMD_WRITE, address);
} else {
flash_put_cmd_addr(FLASH_CMD_WRITE, address);
}
flash_rw_bytes(data, 0, size);
flash_unselect();
flash_wait_for_not_busy();
return size;
}
int flashSpiErase(uint32_t address)
{
if((address & FLASH_SECTOR_MASK) != 0)
return -1;
flash_wait_for_not_busy();
flash_enable_write();
flash_select();
if (_flashDescriptor.use4BytesAddress) {
flash_put_cmd_4b_addr(FLASH_CMD_SECTOR_ERASE, address);
} else {
flash_put_cmd_addr(FLASH_CMD_SECTOR_ERASE, address);
}
flash_unselect();
flash_wait_for_not_busy();
return 0;
}
int flashSpiBlockErase(uint32_t address)
{
// verify block alignment
if((address & FLASH_BLOCK_MASK) != 0)
return -1;
flash_wait_for_not_busy();
flash_enable_write();
flash_select();
if (_flashDescriptor.use4BytesAddress) {
flash_put_cmd_4b_addr(FLASH_CMD_BLOCK_ERASE, address);
} else {
flash_put_cmd_addr(FLASH_CMD_BLOCK_ERASE, address);
}
flash_unselect();
flash_wait_for_not_busy();
return 0;
}
void flashSpiEraseAll()
{
flash_wait_for_not_busy();
flash_enable_write();
flash_do_cmd(FLASH_CMD_CHIP_ERASE, 0, 0, 0);
flash_wait_for_not_busy();
}
uint16_t flashSpiGetPageSize()
{
return FLASH_PAGE_SIZE;
}
@@ -0,0 +1,38 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdint.h>
#include <stdbool.h>
bool flashSpiInit();
uint32_t flashSpiRead(uint32_t address, uint8_t* data, uint32_t size);
uint32_t flashSpiWrite(uint32_t address, const uint8_t* data, uint32_t size);
uint32_t flashSpiGetSize();
uint16_t flashSpiGetPageSize();
int flashSpiErase(uint32_t address);
bool flashSpiIsErased(uint32_t address);
int flashSpiBlockErase(uint32_t address);
void flashSpiEraseAll();
void flashSpiSync();