1、关于SFUD
SFUD (Serial Flash Universal Driver) 串行 Flash 通用驱动库,支持众多spi flash,关于SFUD的详细资料可参考:https://github.com/armink/SFUD。
2、为什么会有通用驱动
JEDEC (固态技术协会)针对串行 Flash 功能的参数表制定了统一标准,https://www.jedec.org/standards-documents/docs/jesd216b,最新版 V1.6B 。该标准规定了,每个 Flash 中会存在一个参数表,该表中会存放 Flash 容量、写粒度、擦除命令、地址模式等 Flash 规格参数。目前,除了部分厂家旧款 Flash 型号会不支持该标准,其他绝大多数新出厂的 Flash 均已支持 SFDP 标准。所以该库在初始化时会优先读取 SFDP 表参数。这也正是SFUD驱动的由来。
对应用来说只需要配置好spi,就可以读写flash了,因为SFUD提供了这些标准驱动。
3、关于SPI Flash ZD25WQ80
翻看ZD25WQ80的手册,可以发现ZD25WQ80是符合JEDEC的标准的,因此可以可以直接使用SFUD 这个驱动。
4、代码移植
4.1、SFUD代码结构
SFUD的代码结构也比较简单:
./demo
里面是示例,
./docs
里面是文档说明,
./sfud
里面是源码,
我们移植也只关心./sfud
文件夹里面的代码:
./sfud/inc
是头文件,裁剪配置在这里面,
./sfud/port
是接口相关文件,
./sfud/src
是核心源代码。
4.2、配置
#define SFUD_DEBUG_MODE
//调试
#define SFUD_USING_SFDP
//使用SFDP标准参数表
// #define SFUD_USING_FLASH_INFO_TABLE
//flash信息表,(flash不支持SFDP时才用到)
// #define SFUD_USING_QSPI
//支持QSPI
定义所使用的spi flash名称:
enum {
SFUD_XXXX_DEVICE_INDEX = 0,
};
#define SFUD_FLASH_DEVICE_TABLE \
{ \
[SFUD_XXXX_DEVICE_INDEX] = {.name = "ZD25WQ80", .spi.name = "SPI2"}, \
}
4.3、SPI驱动
配置完成后,就需要编写spi底层驱动了,这里涉及到两个函数sfud_spi_port_init
和spi_write_read
,分别是spi初始化和spi读写。
针对我们使用的MM32L0136C7P我们完成这两个函数如下:
void SpiInit(void)
{
/* Enable GPIO clock for SPIy and SPIz */
RCC_EnableAHBPeriphs(RCC_AHB_PERIPH_GPIOB, true);
/* SPI2 :
PB12/CS
PB13/SCK
PB14/MISO
PB15/MOSI
*/
/* SPI2. */
RCC_EnableAPB1Periphs(RCC_APB1_PERIPH_SPI2, true);
RCC_ResetAPB1Periphs(RCC_APB1_PERIPH_SPI2);
GPIO_Init_Type gpio_init;
/* PB12 - SPI2_CS. */
gpio_init.Pins = GPIO_PIN_12;
gpio_init.PinMode = GPIO_PinMode_Out_PushPull;
gpio_init.Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio_init);
/* SPI2 GPIO configuration: SCK/PB13, MISO/PB14, MOSI/PB15 */
/* Configure SPIy pins: SCK, MISO and MOSI ---------------------------------*/
/* PB13 - SPI2_SCK. */
gpio_init.Pins = GPIO_PIN_13;
gpio_init.PinMode = GPIO_PinMode_AF_PushPull;
gpio_init.Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio_init);
GPIO_PinAFConf(GPIOB, gpio_init.Pins, GPIO_AF_0);
/* PB14 - SPI2_MISO. */
gpio_init.Pins = GPIO_PIN_14;
gpio_init.PinMode = GPIO_PinMode_In_Floating;
gpio_init.Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio_init);
GPIO_PinAFConf(GPIOB, gpio_init.Pins, GPIO_AF_0);
/* PB15 - SPI2_MOSI. */
gpio_init.Pins = GPIO_PIN_15;
gpio_init.PinMode = GPIO_PinMode_AF_PushPull;
gpio_init.Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpio_init);
GPIO_PinAFConf(GPIOB, gpio_init.Pins, GPIO_AF_0);
/* Setup SPI Master. */
SPI_Master_Init_Type spi_master_init;
spi_master_init.ClockFreqHz = 24000000u;
spi_master_init.BaudRate = 1000000u;
spi_master_init.XferMode = SPI_XferMode_TxRx;
spi_master_init.PolPha = SPI_PolPha_Alt0;
spi_master_init.DataWidth = SPI_DataWidth_8b;
spi_master_init.LSB = false;
spi_master_init.AutoCS = false; /* if set false, need SPI_EnableCS after spi enable. */
SPI_InitMaster(SPI2, &spi_master_init);
/* Enable SPI Master. */
SPI_Enable(SPI2, true);
}
/**
* SPI write data then read data
*/
static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
size_t read_size)
{
sfud_err result = SFUD_SUCCESS;
uint8_t send_data, read_data;
/**
* add your spi write and read code
*/
if (write_size) {
SFUD_ASSERT(write_buf);
}
if (read_size) {
SFUD_ASSERT(read_buf);
}
GPIO_ClearBits(GPIOB, GPIO_PIN_12);
/* 开始读写数据 */
for (size_t i = 0, retry_times; i < write_size + read_size; i++) {
/* 先写缓冲区中的数据到 SPI 总线,数据写完后,再写 dummy(0xFF) 到 SPI 总线 */
if (i < write_size) {
send_data = *write_buf++;
} else {
send_data = SFUD_DUMMY_DATA;
}
/* 发送数据 */
retry_times = 1000;
while ( SPI_STATUS_TX_FULL & SPI_GetStatus(SPI2) )
{
SFUD_RETRY_PROCESS(NULL, retry_times, result);
}
if (result != SFUD_SUCCESS) {
goto exit;
}
SPI_PutData(SPI2, send_data);
/* 接收数据 */
retry_times = 1000;
while (0u == (SPI_STATUS_RX_DONE & SPI_GetStatus(SPI2)) )
{
SFUD_RETRY_PROCESS(NULL, retry_times, result);
}
if (result != SFUD_SUCCESS) {
goto exit;
}
read_data = SPI_GetData(SPI2);
/* 写缓冲区中的数据发完后,再读取 SPI 总线中的数据到读缓冲区 */
if (i >= write_size) {
*read_buf++ = read_data;
}
}
exit:
GPIO_SetBits(GPIOB, GPIO_PIN_12);
return result;
}
static void spi_lock(const sfud_spi *spi)
{
}
static void spi_unlock(const sfud_spi *spi)
{
}
/* about 100 microsecond delay */
static void retry_delay_100us(void)
{
uint32_t delay = 120;
while(delay--);
}
sfud_err sfud_spi_port_init(sfud_flash *flash) {
sfud_err result = SFUD_SUCCESS;
/**
* add your port spi bus and device object initialize code like this:
* 1. rcc initialize
* 2. gpio initialize
* 3. spi device initialize
* 4. flash->spi and flash->retry item initialize
* flash->spi.wr = spi_write_read; //Required
* flash->spi.qspi_read = qspi_read; //Required when QSPI mode enable
* flash->spi.lock = spi_lock;
* flash->spi.unlock = spi_unlock;
* flash->spi.user_data = &spix;
* flash->retry.delay = null;
* flash->retry.times = 10000; //Required
*/
SpiInit();
flash->spi.wr = spi_write_read; //Required
flash->spi.lock = spi_lock;
flash->spi.unlock = spi_unlock;
flash->retry.delay = retry_delay_100us;
flash->retry.times = 10000; //Required
return result;
}
上面还涉及到了延时以及锁的问题,这里我们实际测试后,锁可以不用处理。
4.4、测试
#define SFUD_DEMO_TEST_BUFFER_SIZE 1024
static uint8_t sfud_demo_test_buf[SFUD_DEMO_TEST_BUFFER_SIZE];
/**
* SFUD demo for the first flash device test.
*
* @param addr flash start address
* @param size test flash size
* @param size test flash data buffer
*/
static void sfud_demo(uint32_t addr, size_t size, uint8_t *data) {
sfud_err result = SFUD_SUCCESS;
const sfud_flash *flash = sfud_get_device_table() + 0;
size_t i;
/* prepare write data */
for (i = 0; i < size; i++) {
data[i] = i;
}
/* erase test */
result = sfud_erase(flash, addr, size);
if (result == SFUD_SUCCESS) {
printf("Erase the %s flash data finish. Start from 0x%08X, size is %ld.\r\n", flash->name, addr,
size);
} else {
printf("Erase the %s flash data failed.\r\n", flash->name);
return;
}
/* write test */
result = sfud_write(flash, addr, size, data);
if (result == SFUD_SUCCESS) {
printf("Write the %s flash data finish. Start from 0x%08X, size is %ld.\r\n", flash->name, addr,
size);
} else {
printf("Write the %s flash data failed.\r\n", flash->name);
return;
}
/* read test */
result = sfud_read(flash, addr, size, data);
if (result == SFUD_SUCCESS) {
printf("Read the %s flash data success. Start from 0x%08X, size is %ld. The data is:\r\n", flash->name, addr,
size);
printf("Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n");
for (i = 0; i < size; i++) {
if (i % 16 == 0) {
printf("[%08X] ", addr + i);
}
printf("%02X ", data[i]);
if (((i + 1) % 16 == 0) || i == size - 1) {
printf("\r\n");
}
}
printf("\r\n");
} else {
printf("Read the %s flash data failed.\r\n", flash->name);
}
/* data check */
for (i = 0; i < size; i++) {
if (data[i] != i % 256) {
printf("Read and check write data has an error. Write the %s flash data failed.\r\n", flash->name);
break;
}
}
if (i == size) {
printf("The %s flash test is success.\r\n", flash->name);
}
}
void SFUDTest(void)
{
if(sfud_init() == SFUD_SUCCESS)
{
sfud_demo(0, sizeof(sfud_demo_test_buf), sfud_demo_test_buf);
}
}
5、现象
将读出来关键信息,与手册对比可以发现是一致的。