概述
SFUD (串行Flash通用驱动库)是一款开源的串行 SPI Flash 通用驱动库,支持市面上多数的串行flash芯片。本文主要介绍如何移植SFUD到项目工程中使用。
正文
第一步:下载源文件
链接
第二步:工程
准备好一个项目工程。
第三步:将源文件添加进工程,并包含好头文件路径
第四步:修改工程文件
修改sfud_port.c文件。
#include <sfud.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stm32f10x_conf.h>
#include "include.h"
typedef struct {
SPI_TypeDef *spix;
GPIO_TypeDef *cs_gpiox;
uint16_t cs_gpio_pin;
} spi_user_data, *spi_user_data_t;
static char log_buf[256];
void sfud_log_debug(const char *file, const long line, const char *format, ...);
static void rcc_configuration(spi_user_data_t spi) {
if (spi->spix == SPI1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
} else if (spi->spix == SPI2) {
}
}
static void gpio_configuration(spi_user_data_t spi) {
GPIO_InitTypeDef GPIO_InitStructure;
if (spi->spix == SPI1) {
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_2);
} else if (spi->spix == SPI2) {
}
}
static void spi_configuration(spi_user_data_t spi) {
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_I2S_DeInit(spi->spix);
SPI_Init(spi->spix, &SPI_InitStructure);
SPI_CalculateCRC(spi->spix, DISABLE);
SPI_Cmd(spi->spix, ENABLE);
}
static void spi_lock(const sfud_spi *spi) {
rt_enter_critical();
}
static void spi_unlock(const sfud_spi *spi) {
rt_exit_critical();
}
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;
spi_user_data_t spi_dev = (spi_user_data_t) spi->user_data;
if (write_size) {
SFUD_ASSERT(write_buf);
}
if (read_size) {
SFUD_ASSERT(read_buf);
}
GPIO_ResetBits(spi_dev->cs_gpiox, spi_dev->cs_gpio_pin);
for (size_t i = 0, retry_times; i < write_size + read_size; i++) {
if (i < write_size) {
send_data = *write_buf++;
} else {
send_data = SFUD_DUMMY_DATA;
}
retry_times = 1000;
while (SPI_I2S_GetFlagStatus(spi_dev->spix, SPI_I2S_FLAG_TXE) == RESET) {
SFUD_RETRY_PROCESS(NULL, retry_times, result);
}
if (result != SFUD_SUCCESS) {
goto exit;
}
SPI_I2S_SendData(spi_dev->spix, send_data);
retry_times = 1000;
while (SPI_I2S_GetFlagStatus(spi_dev->spix, SPI_I2S_FLAG_RXNE) == RESET) {
SFUD_RETRY_PROCESS(NULL, retry_times, result);
}
if (result != SFUD_SUCCESS) {
goto exit;
}
read_data = SPI_I2S_ReceiveData(spi_dev->spix);
if (i >= write_size) {
*read_buf++ = read_data;
}
}
exit:
GPIO_SetBits(spi_dev->cs_gpiox, spi_dev->cs_gpio_pin);
return result;
}
extern void rt_hw_us_delay(rt_uint32_t us);
static void retry_delay_100us(void) {
rt_hw_us_delay(100);
}
static spi_user_data spi1 = { .spix = SPI1, .cs_gpiox = GPIOA, .cs_gpio_pin = GPIO_Pin_2 };
sfud_err sfud_spi_port_init(sfud_flash *flash) {
sfud_err result = SFUD_SUCCESS;
switch (flash->index) {
case SFUD_SST25_DEVICE_INDEX: {
rcc_configuration(&spi1);
gpio_configuration(&spi1);
spi_configuration(&spi1);
flash->spi.wr = spi_write_read;
flash->spi.lock = spi_lock;
flash->spi.unlock = spi_unlock;
flash->spi.user_data = &spi1;
flash->retry.delay = retry_delay_100us;
flash->retry.times = 60 * 10000;
break;
}
}
return result;
}
void sfud_print(const char *format, ...) {
va_list args;
va_start(args, format);
rt_vsprintf(log_buf, format, args);
uart_write(DEV_UART1,(uint8_t *)log_buf, strlen(log_buf));
va_end(args);
}
void sfud_log_debug(const char *file, const long line, const char *format, ...) {
va_list args;
va_start(args, format);
sfud_print("[SFUD](%s:%ld) ", file, line);
vsnprintf(log_buf, sizeof(log_buf), format, args);
sfud_print("%s\r\n", log_buf);
va_end(args);
}
void sfud_log_info(const char *format, ...) {
va_list args;
va_start(args, format);
sfud_print("[SFUD]");
vsnprintf(log_buf, sizeof(log_buf), format, args);
sfud_print("%s\r\n", log_buf);
va_end(args);
}
修改sfud_cfg.h文件。
#ifndef _SFUD_CFG_H_
#define _SFUD_CFG_H_
#define SFUD_DEBUG_MODE
#define SFUD_USING_SFDP
#define SFUD_USING_FLASH_INFO_TABLE
enum {
SFUD_SST25_DEVICE_INDEX = 0,
};
#define SFUD_FLASH_DEVICE_TABLE \
{ \
[SFUD_SST25_DEVICE_INDEX] = {.name = "W25Q64CV", .spi.name = "SPI1"}, \
}
#endif
第五步:调用接口
根据sfud.h提供的接口调用即可。如:
总结
这样就完成了sfud驱动的简单移植。
|