立创梁山派--移植开源的SFUD万能的串行 Flash 通用驱动库

SFUD是什么

关于SFUD库的介绍,其开源链接(gitee,github)已经详细的阐述了.
这里是截取自它的一部分介绍:
SFUD 是一款开源的串行 SPI Flash 通用驱动库。由于现有市面的串行 Flash 种类居多,各个 Flash 的规格及命令存在差异, SFUD 就是为了解决这些 Flash 的差异现状而设计,让我们的产品能够支持不同品牌及规格的 Flash,提高了涉及到 Flash 功能的软件的可重用性及可扩展性,同时也可以规避 Flash 缺货或停产给产品所带来的风险。

主要特点:支持 SPI/QSPI 接口、面向对象(同时支持多个 Flash 对象)、可灵活裁剪、扩展性强、支持 4 字节地址
资源占用
标准占用:RAM:0.2KB ROM:5.5KB
最小占用:RAM:0.1KB ROM:3.6KB
设计思路:
什么是 SFDP :它是 JEDEC (固态技术协会)制定的串行 Flash 功能的参数表标准,最新版 V1.6B (点击这里查看)。该标准规定了,每个 Flash 中会存在一个参数表,该表中会存放 Flash 容量、写粒度、擦除命令、地址模式等 Flash 规格参数。目前,除了部分厂家旧款 Flash 型号会不支持该标准,其他绝大多数新出厂的 Flash 均已支持 SFDP 标准。所以该库在初始化时会优先读取 SFDP 表参数。
不支持 SFDP 怎么办 :如果该 Flash 不支持 SFDP 标准,SFUD 会查询配置文件 ( /sfud/inc/sfud_flash_def.h ) 中提供的 Flash 参数信息表 中是否支持该款 Flash。如果不支持,则可以在配置文件中添加该款 Flash 的参数信息(添加方法详细见 2.5 添加库目前不支持的 Flash)。获取到了 Flash 的规格参数后,就可以实现对 Flash 的全部操作。
详细的可以查看开源链接(gitee,github);

为什么选择 SFUD

避免项目因 Flash 缺货、Flash 停产或产品扩容而带来的风险;
越来越多的项目将固件存储到串行 Flash 中,例如:ESP8266 的固件、主板中的 BIOS 及其他常见电子产品中的固件等等,但是各种 Flash 规格及命令不统一。使用 SFUD 即可避免,在相同功能的软件平台基础下,无法适配不同 Flash 种类的硬件平台的问题,提高软件的可重用性;
简化软件流程,降低开发难度。现在只需要配置好 SPI 通信,即可畅快的开始玩串行 Flash 了;
可以用来制作 Flash 编程器/烧写器

开始copy代码

这篇文章的重点就是来移植这个库,所以我就不多介绍了,直接开搞。

  • 首先先下载好立创梁山派附带的资料,等下要用到里面的提供的demo来作为我们的工程模板。
  • 我们使用 005-串口打印信息 这个作为我们的工程模板,然后在从015_spi中复制一份spi的代码,作为我们的flash驱动代码,还要下载一份sfud的源代码。
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
总共就是以上三个东西就ok啦。

  • 接下啦,按照下图,将文件添加到你的工程中进来,如果这一部分操作不会的话,可以学一下立创推出的教程,我这里就不再赘述啦。
    在这里插入图片描述
  • 接下来就是对代码进行小小的修改
    对bsp_spi.c文件按照我自己的代码风格进行了小小的修改和裁剪,因为这个SFUD十分完善,只需要我们提供这个spi的读取接口函数就ok了。
#include "bsp_spi.h"void bsp_spi4_init(void)
{//SPI参数定义结构体spi_parameter_struct spi_init_struct;rcu_periph_clock_enable(RCU_GPIOF);  // 使用F端口rcu_periph_clock_enable(RCU_SPI4);     // 使能SPI4//引脚复用gpio_af_set(GPIOF, GPIO_AF_5, GPIO_PIN_7);gpio_af_set(GPIOF, GPIO_AF_5, GPIO_PIN_8);gpio_af_set(GPIOF, GPIO_AF_5, GPIO_PIN_9);//引脚模式gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);//输出模式gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);//开启CS引脚时钟rcu_periph_clock_enable(RCU_GPIOF);//配置CS引脚模式gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_6);//配置CS输出模式gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);//W25Q64不选中gpio_bit_write(GPIOF, GPIO_PIN_6, SET);spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;  	// 传输模式全双工spi_init_struct.device_mode          = SPI_MASTER;                	// 配置为主机spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;        	// 8位数据spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;		//极性相位  spi_init_struct.nss                  = SPI_NSS_SOFT;              	//软件csspi_init_struct.prescale             = SPI_PSC_2;                 	//SPI时钟预分频为2spi_init_struct.endian               = SPI_ENDIAN_MSB;            	//高位在前//将参数填入SPI4spi_init(SPI4, &spi_init_struct);//使能SPIspi_enable(SPI4);
}/******************************************************************* 函 数 名 称:spi_read_write_byte* 函 数 说 明:硬件SPI的读写* 函 数 形 参:dat=发送的数据* 函 数 返 回:读取到的数据* 作       者:LCKFB* 备       注:无
******************************************************************/
uint8_t spi4_read_write_byte(uint8_t dat)
{//等待发送缓冲区为空while(RESET == spi_i2s_flag_get(SPI4,  SPI_FLAG_TBE) );spi_i2s_data_transmit(SPI4, dat);//等待接收缓冲区为空while(RESET == spi_i2s_flag_get(SPI4,  SPI_FLAG_RBNE) );return spi_i2s_data_receive(SPI4);
}
uint16_t spi4_flash_readID(void)
{uint16_t  temp = 0;	  	gpio_bit_write(GPIOF, GPIO_PIN_6, RESET);spi4_read_write_byte(0x90);//发送读取ID命令	    spi4_read_write_byte(0x00); 	    spi4_read_write_byte(0x00); 	    spi4_read_write_byte(0x00); 		//接收数据temp |= spi4_read_write_byte(0xFF)<<8;  temp |= spi4_read_write_byte(0xFF);	gpio_bit_write(GPIOF, GPIO_PIN_6, SET);	return temp;
}void spi4_w25qxx_cs(uint8_t enable)
{if(enable)gpio_bit_write(GPIOF, GPIO_PIN_6, SET);	elsegpio_bit_write(GPIOF, GPIO_PIN_6, RESET);	
}

对应的bsp_spi.h文件如下:

#ifndef _BSP_SPI_H
#define _BSP_SPI_H#include "gd32f4xx.h"
#include "systick.h"void bsp_spi4_init(void);uint16_t spi4_flash_readID(void);
uint8_t spi4_read_write_byte(uint8_t dat);
void spi4_w25qxx_cs(uint8_t enable);
#endif
  • 接下来是对这个sfud的移植
    这个是对于sfud_cfg.h的修改
    在这里插入图片描述

  • 剩下对一个sfud_port.c文件进行修改


#include <sfud.h>
#include <stdarg.h>#include "bsp_spi.h"
#include <string.h>#define OS 0 // 0:不使用OS,1:使用OS//这里使用的是freertos,你也可以换成你熟悉的rtos
#if OS  
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#endifstatic char log_buf[256];void sfud_log_debug(const char *file, const long line, const char *format, ...);//这里是自添加的函数,实行对应接口的写buf函数
static void spi_flash_buf_write(const uint8_t *buf, size_t len)
{while (len--){spi4_read_write_byte(*buf);buf++;}
}//这里是自添加的函数,实行对应接口的读buf函数
static void spi_flash_buf_read(uint8_t *buf, size_t len)
{while (len--){*buf = spi4_read_write_byte(0xff);buf++;}
}/*** 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);spi4_w25qxx_cs(0);if (write_size){spi_flash_buf_write(write_buf, write_size);}if (read_size){spi_flash_buf_read(read_buf, read_size);}spi4_w25qxx_cs(1);return result;
}#ifdef SFUD_USING_QSPI
/*** read flash data by QSPI*/
static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,uint8_t *read_buf, size_t read_size)
{sfud_err result = SFUD_SUCCESS;/*** add your qspi read flash data code*/return result;
}
#endif /* SFUD_USING_QSPI */#if OSstatic SemaphoreHandle_t sfudMutexSemaphor;
static void spi_lock_init(void)
{sfudMutexSemaphor = xSemaphoreCreateMutex();if (sfudMutexSemaphor == NULL){SFUD_DEBUG("sfud semaphor create failed");}
}
#endifstatic void spi_lock(const sfud_spi *spi)
{
#if OSif (sfudMutexSemaphor != NULL){xSemaphoreTake(sfudMutexSemaphor, portMAX_DELAY);}
#else__disable_irq(); //? ?  ж 
#endif
}
static void spi_unlock(const sfud_spi *spi)
{
#if OSif (sfudMutexSemaphor != NULL){xSemaphoreGive(sfudMutexSemaphor);}
#else__enable_irq();  //? ?  ж 
#endif
}
static void spi_delay(void)
{
#if OSvTaskDelay(1);
#elseuint32_t delay = 120;while(delay--);
#endif
}
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*///这里也是我们要添加的函数,这里要重点注意这个SPI4,要和sfud_cfg.h中保持一致if (!strcmp(flash->spi.name, "SPI4")){bsp_spi4_init();
#if OSspi_lock_init();
#endifflash->spi.wr = spi_write_read;flash->spi.lock = spi_lock;flash->spi.unlock = spi_unlock;flash->retry.delay = spi_delay;flash->retry.times = 60*10000;}return result;
}/*** This function is print debug info.** @param file the file which has call this function* @param line the line number which has call this function* @param format output format* @param ... args*/
void sfud_log_debug(const char *file, const long line, const char *format, ...)
{va_list args;/* args point to the first variable parameter */va_start(args, format);printf("[SFUD](%s:%ld) ", file, line);/* must use vprintf to print */vsnprintf(log_buf, sizeof(log_buf), format, args);printf("%s\n", log_buf);va_end(args);
}/*** This function is print routine info.** @param format output format* @param ... args*/
void sfud_log_info(const char *format, ...)
{va_list args;/* args point to the first variable parameter */va_start(args, format);printf("[SFUD]");/* must use vprintf to print */vsnprintf(log_buf, sizeof(log_buf), format, args);printf("%s\n", log_buf);va_end(args);
}
  • 上面的代码所示,其实也只需要修改 spi_write_readsfud_spi_port_init这两个函数,就移植完成啦。

接下来我们对移植完成的sfud库进行擦除,读,写功能的测试。

对应的main函数代码。


#include "main.h"
#include "bsp_led.h"
#include "bsp_usart.h"
#include "gd32f4xx.h"
#include "sys.h"
#include "systick.h"
#include <stdio.h>#include "bsp_spi.h"
#include <sfud.h>#define SFUD_DEMO_TEST_BUFFER_SIZE 1024static void sfud_demo(uint32_t addr, size_t size, uint8_t *data);static uint8_t sfud_demo_test_buf[SFUD_DEMO_TEST_BUFFER_SIZE];/*!\brief    main function\param[in]  none\param[out] none\retval     none
*/
int main(void)
{systick_config();led_gpio_config(); // led初始化usart_gpio_config(115200U);/* SFUD initialize */if (sfud_init() == SFUD_SUCCESS){sfud_demo(0, sizeof(sfud_demo_test_buf), sfud_demo_test_buf);}while (1){}
}
/*** 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);}
}

效果如下

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
擦除读写1k字节数据正常,到这里就移植完成啦。
至于如何使用sfud库,可以参考这个测试demo的用法,记得要向flash写入数据,要先擦除哦!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1486046.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Apache Tomcat文件包含漏洞复现(详细教程)

1.漏洞原理 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器&#xff0c;其安装后会默认开启ajp连接器&#xff0c;方便与其他web服务器通过ajp协议进行交互。属于轻量级应用服务器&#xff0c;在中小型系统和并发访问用户不是很多的场合下被普遍使用&#xff0c;是开发和…

【接口自动化_07课_Pytest+Excel+Allure完整框架集成_下】

目标&#xff1a;优化框架场景 1. 生成对应的接口关联【重点】 2. 优化URL基础路径封装【理解】 3. 利用PySQL操作数据库应用【理解】--- 怎么用python连接数据库、mysql 4. 通过数据库进行数据库断言【重点】 5. 通过数据库进行关联操作【重点】 一、接口关联&#xff1a…

MSP430M03507最小系统板的keil环境搭配,用keil编辑ti单片机

转载自嘉立创MSP430M03507开发手册 这篇文章只是因为我的keil版本与嘉立创的不一样&#xff0c;所以添加了我自己遇到的问题解析 先说说为什么要用keil编辑&#xff0c;因为ti单片机自己的ccs编译环境需要对应仿真器&#xff0c;那个加芯片都240了&#xff0c;哪有那么多钱买…

node.js中nodemon : 无法加载和使用问题,这是由于windows安全策略影起的按如下操作即可

1、用管理员权限打开vscode 2、文件终端中打开&#xff0c;输入 Set-ExecutionPolicy -Scope CurrentUser 3、再输入RemoteSigned 4、使用get-ExecutionPolicy查看权限&#xff0c;可以看到变为了RemoteSigned 重启问题解决

MySQL面试索引篇

1、什么是索引&#xff1f; 作为一个数据库&#xff0c;首要任务就是把数据存储好&#xff0c;并快速查询出用户需要的数据&#xff0c;而索引就相当于图书的目录一样&#xff0c;是一种用于快速查询和检索数据的数据结构&#xff0c;其本质可以看成是一种排序好的数据结构。 …

TypeScript 教程(九):类型声明文件与异步编程

目录 前言回顾装饰器与高级类型操控1. 类型声明文件a. 什么是类型声明文件&#xff08;.d.ts&#xff09;b. 编写和使用类型声明文件 2. 异步编程a. Promise 类型b. async/awaitc. 异步迭代器 3. 并行执行与错误处理a. Promise.allb. Promise.racec. 错误处理 结语 前言 在前几…

华为云.云日志服务LTS及其基本使用

云计算 云日志服务LTS及其基本使用 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blog.csdn.net/qq_28550…

数学建模(7)——Logistic模型

一、马尔萨斯人口模型 import numpy as np import matplotlib.pyplot as plt# 初始人口 N0 100 # 人口增长率 r 0.02 # 时间段&#xff08;年&#xff09; t np.linspace(0, 200, 200)# 马尔萨斯人口模型 N N0 * np.exp(r * t)# 绘图 plt.plot(t, N, labelPopulation) plt.…

图片转pdf的软件有哪些?这几种转换工具了解下

在日常的办公学习中&#xff0c;图片转PDF的需求愈发普遍。不论是工作汇报、学习笔记还是生活点滴&#xff0c;我们都希望将重要的图片内容整理成易于查阅的PDF格式。那么&#xff0c;有哪些软件可以做到将图片转换成PDF格式呢&#xff1f;给大家介绍5种简单好用的转换方法&…

Linux第五节课(权限02)

1、Linux下的用户分类 root&#xff1a;超级用户普通用户&#xff1a;通过root新建的用户&#xff0c;adduser root不受权限约束&#xff1b;普通用户受权限约束&#xff1b; Linux系统中&#xff0c;所有用户都需要有密码&#xff0c;无论是root还是其他&#xff0c;即便是…

SpringBoot+ Sharding Sphere 轻松实现数据库字段加解密

一、介绍 在实际的软件系统开发过程中&#xff0c;由于业务的需求&#xff0c;在代码层面实现数据的脱敏还是远远不够的&#xff0c;往往还需要在数据库层面针对某些关键性的敏感信息&#xff0c;例如&#xff1a;身份证号、银行卡号、手机号、工资等信息进行加密存储&#xf…

优选算法之二分查找(上)

目录 一、二分查找 1.题目链接&#xff1a;704. 二分查找 2.题目描述&#xff1a; 3.算法流程&#xff1a; 4.算法代码&#xff1a; 二、在排序数组中查找元素的第一个和最后一个位置 1.题目链接&#xff1a;34. 在排序数组中查找元素的第一个和最后一个位置 2.题目描述…

matlab2018b安装

1.可先参考这个 2.激活 按上面教程安装后&#xff0c;打开matlab 可能会出现软件激活这个界面&#xff0c;需要按如下步骤进行操作。

从理论到实践:如何用 TDengine 打造完美数据模型​

在用 TDengine 进行数据建模之前&#xff0c;我们需要回答两个关键问题&#xff1a;建模的目标用户是谁&#xff1f;他们的具体需求是什么&#xff1f;在一个典型的时序数据管理方案中&#xff0c;数据采集和数据应用是两个主要环节。如下图所示&#xff1a; 对于数据采集工程师…

Bootstrap5 Navbar多级下拉框

实现目标&#xff1a; 1、访问 Bootstrap5-navbar 2、修改dropdown为多级 <!DOCTYPE HTML> <html lang"en-US"> <head><meta charset"UTF-8"><title></title><link rel"stylesheet" href"https…

Unity DOTS中的world

Unity DOTS中的world 注册销毁逻辑自定义创建逻辑创建world创建system group插入player loopReference DOTS中&#xff0c;world是一组entity的集合。entity的ID在其自身的世界中是唯一的。每个world都拥有一个EntityManager&#xff0c;可以用它来创建、销毁和修改world中的en…

[AWS]MSK调用,报错Access denied

背景&#xff1a;首先MSK就是配置一个AWS的托管 kafka&#xff0c;创建完成之后就交给开发进行使用&#xff0c;开发通常是从代码中&#xff0c;编写AWS的access_key 和secret_key进行调用。 但是开发在进行调用的时候&#xff0c;一直报错连接失败&#xff0c;其实问题很简单&…

【机器学习】机器学习之计算学习理论--评估机器学习能够学到什么程度

引言 计算学习理论&#xff08;Computational Learning Theory&#xff0c;CLT&#xff09;是机器学习的一个分支&#xff0c;它使用数学工具来分析和理解机器学习算法的效率和可能性 计算学习理论主要关注三个核心问题&#xff1a;学习模型的表示、学习算法的效率和学习的泛化…

Matlab画不同指标的对比图

目录 一、指标名字可修改 二、模型名字可修改 三、输入数据可修改 软件用的是Matlab R2024a。 clear,clc,close all figure1figure(1); % set(figure1,Position,[300,100,800,600],Color,[1 1 1]) axes1 axes(Parent,figure1);%% Initialize data points 一、指标名字可修…

Astro 4.12 发布,新增支持服务器岛屿

近日&#xff0c;Astro 发布了最新的 4.12 版本&#xff0c;此版本包含 Server Islands&#xff08;服务器岛屿&#xff09;&#xff0c;这是 Astro 将高性能静态 HTML 和动态服务器生成的组件集成在一起的新解决方案&#xff0c;此版本还包括对分页和语法突出显示的改进。 要…