Skip to content

sdk例程:axi_spi_flash_fats移植

minichao9901 edited this page Jun 12, 2024 · 6 revisions

D1 关键点

使用原子的W25Qxx读写框架

  • 使用原子的W25Qxx读写框架,只需要实现底层的3个函数即可:W25QXX_Read, W25QXX_Write_Page, W25QXX_Erase_Sector
  • 原子的W25Qxx读写框架,对上层FATFS提供的函数有2个:W25QXX_Read, W25QXX_Write。这2个函数可以提供任意长度的读写,里面已经包含了Sector_Erase的过程。
  • 对于axi_quadspi这个ip的standard模式,一定要加上XSP_MANUAL_SSELECT_OPTION模式,这样才能一次发送任意长度的数据。如果不加上,一次发送的数据不超过FIFO Size长度。

AXI_SPI_Init(&AXI_SPI0, XPAR_SPI_0_DEVICE_ID, XSP_MASTER_OPTION|XSP_MANUAL_SSELECT_OPTION);

image

FATFS移植

  • FATFS的sector size注意不能小于512,不能超过4096。这个与W25QXX的Page不是一个概念。
  • 对于W25Q128,由于其PageSize=256,刚开始的时候,我把FATFS的sector size也定义成256,折腾了2天,怎么也调不通。明明flash的读写函数已经调通了,但是FATFS就是不work。后来才发现这个问题。
  • FATFS移植的注意点如下。sector_size按照512或者4096移植,都可以work。对于spi_flash推荐使用4096,这样可以提高效率。
  • FF_USE_MKFS要开启,否则不能使用f_mkfs函数
    image image image

备注:一般来说FF_MAX_SS == FF_MIN_SS即可,比如如果用sec=512,那么FF_MAX_SS = FF_MIN_SS=512; 如果用sec=4096,那么FF_MAX_SS = FF_MIN_SS=4096

D2 Sector_size=512移植的打印结果

hello
init
FlashID=0xEF 0x40 0x16
io_ctrl
read: 0
read: 63
Volume is not FAT formated; formating FAT
init
FlashID=0xEF 0x40 0x16
io_ctrl
io_ctrl
io_ctrl
write: 63
write: 64
write: 72
write: 76
write: 84
write: 92
write: 100
write: 0
io_ctrl
init
FlashID=0xEF 0x40 0x16
io_ctrl
read: 0
read: 63
Success to open SD card!
read: 76
0
write: 76
read: 64
0
15
write: 108
write: 64
read: 76
write: 76
io_ctrl
read: 108
15
www.openedv.com---www.openedv.com
src_str is equal to dest_str,SD card test success!
read: 64
read: 65
read: 66
read: 67
read: 68
read: 69
read: 70
read: 71
read: 72
read: 73
read: 74
read: 75
      4042 KiB total drive space.
      4041 KiB available.

D3 Sector_size=4096移植的打印结果

hello
init
FlashID=0xEF 0x40 0x16
io_ctrl
read: 0
read: 63
Success to open SD card!
read: 65
write: 65
read: 64
write: 64
read: 65
0
read: 64
0
15
write: 69
write: 64
read: 65
write: 65
io_ctrl
read: 69
15
www.openedv.com---www.openedv.com
src_str is equal to dest_str,SD card test success!
read: 64
       477 KiB total drive space.
       477 KiB available.

image

512和4096的fs的结构体对比

2个的容量计算结果不一样:sec=512容量计算正确,sec=4096容量计算错误。原因在这个测试程序中已经注释的很清楚了,是以sec=512计算的:

    /* Print the free space (assuming 512 bytes/sector) */
    printf("%10lu KiB total drive space.\n%10lu KiB available.\n", tot_sect / 2, fre_sect / 2);

sec=4096速度会快很多,推荐使用sec=4096。 推荐的设置为:

#define SECTOR_SIZE 4096
#define BLOCK_SIZE 4096
#define SECTOR_COUNTS 1024  //4Mbyte

如果sec=512。 推荐的设置为:

#define SECTOR_SIZE 512
#define BLOCK_SIZE 4096
#define SECTOR_COUNTS (1024*8)  //4Mbyte

注意4096时,qspi_read函数中的memmove会出现数组越界(AXI_SPI_Transfer(&AXI_SPI0, 0, pBuffer, txBuffer, NumByteToRead+data_offset)中,pBuffer会出现越界,因为写的数量超过它的长度)的情况,为此我们修改了W25QXX_Read函数,使用rxBuffer来接收数据,而rxBuffer的长度设置为不会出现越界的情况。

image image

D3 附录

FATFS版本

image 下载v13可以了

image 程序只需要include一个ff.h就够了,非常简单

移植接口:diskio.c

#include "diskio.h"
#include "ff.h"
#include "xil_types.h"

#include "sleep.h"
#include "xil_printf.h"
#include "..\ACZ702_Lib\COMMON.h"


#define SECTOR_SIZE		4096
#define BLOCK_SIZE		65536
#define NUM_SECTORS		1024

// #define SECTOR_SIZE		512
// #define BLOCK_SIZE		4096
// #define NUM_SECTORS		8192




/*-----------------------------------------------------------------------*/
/* Get Disk Status							*/
/*-----------------------------------------------------------------------*/

/*****************************************************************************/
/**
*
* Gets the status of the disk.
* In case of SD, it checks whether card is present or not.
*
* @param	pdrv - Drive number
*
* @return
*		0		Status ok
*		STA_NOINIT	Drive not initialized
*		STA_NODISK	No medium in the drive
*		STA_PROTECT	Write protected
*
* @note		In case Card detect signal is not connected,
*		this function will not be able to check if card is present.
*
******************************************************************************/
DSTATUS disk_status (
		BYTE pdrv	/* Drive number (0) */
)
{
		return RES_OK;
}

/*-----------------------------------------------------------------------*/
/* Initialize Disk Drive						 */
/*-----------------------------------------------------------------------*/
/*****************************************************************************/
/**
*
* Initializes the drive.
* In case of SD, it initializes the host controller and the card.
* This function also selects additional settings such as bus width,
* speed and block size.
*
* @param	pdrv - Drive number
*
* @return	s - which contains an OR of the following information
*		STA_NODISK	Disk is not present
*		STA_NOINIT	Drive not initialized
*		STA_PROTECT	Drive is write protected
*		0 or only STA_PROTECT both indicate successful initialization.
*
* @note
*
******************************************************************************/
DSTATUS disk_initialize (
		BYTE pdrv	/* Physical drive number (0) */
)
{
	xil_printf("init\n");
	AXI_SPI_Init(&AXI_SPI0, XPAR_SPI_0_DEVICE_ID, XSP_MASTER_OPTION|XSP_MANUAL_SSELECT_OPTION);
	FlashReadID();
	return RES_OK;
}


/*-----------------------------------------------------------------------*/
/* Read Sector(s)							 */
/*-----------------------------------------------------------------------*/
/*****************************************************************************/
/**
*
* Reads the drive
* In case of SD, it reads the SD card using ADMA2 in polled mode.
*
* @param	pdrv - Drive number
* @param	*buff - Pointer to the data buffer to store read data
* @param	sector - Start sector number
* @param	count - Sector count
*
* @return
*		RES_OK		Read successful
*		STA_NOINIT	Drive not initialized
*		RES_ERROR	Read not successful
*
* @note
*
******************************************************************************/
DRESULT disk_read (
		BYTE pdrv,	/* Physical drive number (0) */
		BYTE *buff,	/* Pointer to the data buffer to store read data */
		DWORD sector,	/* Start sector number (LBA) */
		UINT count	/* Sector count (1..128) */
)
{
    xil_printf("read: %d\n", sector);
	for(;count>0;count--)
	{
		W25QXX_Read(buff,sector*SECTOR_SIZE,SECTOR_SIZE);
		sector++;
		buff+=SECTOR_SIZE;
	}

	return RES_OK;
}

/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions						*/
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,				/* Physical drive number (0) */
	BYTE cmd,				/* Control code */
	void *buff				/* Buffer to send/receive control data */
)
{

	DRESULT res = RES_OK;
    switch(cmd)
    {
	    case CTRL_SYNC:
			res = RES_OK;
	        break;
	    case GET_SECTOR_SIZE:
	        *(WORD*)buff = SECTOR_SIZE;
	        res = RES_OK;
	        break;
	    case GET_BLOCK_SIZE:
	        *(WORD*)buff = BLOCK_SIZE/SECTOR_SIZE;
	        res = RES_OK;
	        break;
	    case GET_SECTOR_COUNT:
	        *(DWORD*)buff = NUM_SECTORS;
	        res = RES_OK;
	        break;
	    default:
	        res = RES_PARERR;
	        break;
    }

	xil_printf("io_ctrl\n");
    return res;
}

/******************************************************************************/
/**
*
* This function is User Provided Timer Function for FatFs module
*
* @return	DWORD
*
* @note		None
*
****************************************************************************/

DWORD get_fattime (void)
{
	return	((DWORD)(2010U - 1980U) << 25U)	/* Fixed to Jan. 1, 2010 */
		| ((DWORD)1 << 21)
		| ((DWORD)1 << 16)
		| ((DWORD)0 << 11)
		| ((DWORD)0 << 5)
		| ((DWORD)0 >> 1);
}

/*****************************************************************************/
/**
*
* Reads the drive
* In case of SD, it reads the SD card using ADMA2 in polled mode.
*
* @param	pdrv - Drive number
* @param	*buff - Pointer to the data to be written
* @param	sector - Sector address
* @param	count - Sector count
*
* @return
*		RES_OK		Read successful
*		STA_NOINIT	Drive not initialized
*		RES_ERROR	Read not successful
*
* @note
*
******************************************************************************/
DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	UINT count			/* Number of sectors to write (1..128) */
)
{
	xil_printf("write: %d\n", sector);
	for(;count>0;count--)
	{
		W25QXX_Write((u8*)buff,sector*SECTOR_SIZE,SECTOR_SIZE);
		buff+=SECTOR_SIZE;
		sector++;
	}

	return RES_OK;
}

ffconf.h

image image

普通的使用,设置以上2点即可

main.c

#include "ACZ702_Lib/COMMON.h"
#include "xil_cache.h"
#include "FATFS/ff.h"

qspi_buff_t qspi_buffer;

#include "xparameters.h"
#include "xil_printf.h"
#include "xdevcfg.h"

#define FILE_NAME "ZDYZ.txt"                //定义文件名

const char src_str[30] = "www.openedv.com"; //定义文本内容
char dest_str[30] = "";
static FATFS fs;                         //文件系统
BYTE work[FF_MAX_SS];

//初始化文件系统
int platform_init_fs()
{
	FRESULT status;
	TCHAR *Path = "0:";

    //注册一个工作区(挂载分区文件系统)
    //在使用任何其它文件函数之前,必须使用f_mount函数为每个使用卷注册一个工作区
	status = f_mount(&fs, Path, 1);  //挂载SD卡
	if (status != FR_OK) {
		xil_printf("Volume is not FAT formated; formating FAT\r\n");
		//格式化SD卡
		status = f_mkfs(Path, FM_ANY, 0, work, sizeof work);
		if (status != FR_OK) {
			xil_printf("Unable to format fs\r\n");
			return -1;
		}
		//格式化之后,重新挂载SD卡
		status = f_mount(&fs, Path, 1);
		if (status != FR_OK) {
			xil_printf("Unable to mount fs\r\n");
			return -1;
		}
	}
	return 0;
}

//挂载SD(TF)卡
int sd_mount()
{
    FRESULT status;
    //初始化文件系统(挂载SD卡,如果挂载不成功,则格式化SD卡)
    status = platform_init_fs();
    if(status){
        xil_printf("ERROR: f_mount returned %d!\n",status);
        return XST_FAILURE;
    }
    return XST_SUCCESS;
}

//SD卡写数据
int sd_write_data(char *file_name,u32 src_addr,u32 byte_len)
{
    FIL fil;         //文件对象
    UINT bw;         //f_write函数返回已写入的字节数
    FRESULT status;

    //打开一个文件,如果不存在,则创建一个文件
    status=f_open(&fil,file_name,FA_CREATE_ALWAYS | FA_WRITE);
    xil_printf("%d\n", status);
    //移动打开的文件对象的文件读/写指针     0:指向文件开头
    f_lseek(&fil, 0);
    //向文件中写入数据
    status==f_write(&fil,(void*) src_addr,byte_len,&bw);
    xil_printf("%d\n", status);
    xil_printf("%d\r\n", bw);
    //关闭文件
    f_close(&fil);
    return 0;
}

//SD卡读数据
int sd_read_data(char *file_name,u32 src_addr,u32 byte_len)
{
	FIL fil;         //文件对象
    UINT br;         //f_read函数返回已读出的字节数

    //打开一个只读的文件
    f_open(&fil,file_name,FA_READ);
    //移动打开的文件对象的文件读/写指针     0:指向文件开头
    f_lseek(&fil,0);
    //从SD卡中读出数据
    f_read(&fil,(void*)src_addr,byte_len,&br);
    xil_printf("%d\r\n", br);
    //关闭文件
    f_close(&fil);
    return 0;
}

//main函数
int main_fats()
{
    int status,len;

    status = sd_mount();           //挂载SD卡
    if(status != XST_SUCCESS){
		xil_printf("Failed to open SD card!\n");
		return 0;
    }
    else
        xil_printf("Success to open SD card!\n");

    len = strlen(src_str);         //计算字符串长度
    //SD卡写数据
    sd_write_data(FILE_NAME,(u32)src_str,len);
    //SD卡读数据
    sd_read_data(FILE_NAME,(u32)dest_str,len);

    xil_printf("%s---%s\n", src_str, dest_str);

    //比较写入的字符串和读出的字符串是否相等
    if (strcmp(src_str, dest_str) == 0)
    	xil_printf("src_str is equal to dest_str,SD card test success!\n");
    else
    	xil_printf("src_str is not equal to dest_str,SD card test failed!\n");



    DWORD fre_clust, fre_sect, tot_sect;

    /* Get volume information and free clusters of drive 1 */
    FATFS *pfs=&fs;
    f_getfree("0:", &fre_clust, &pfs);

    /* Get total sectors and free sectors */
    tot_sect = (fs.n_fatent - 2) * fs.csize;
    fre_sect = fre_clust * fs.csize;

    /* Print the free space (assuming 512 bytes/sector) */
    printf("%10lu KiB total drive space.\n%10lu KiB available.\n", tot_sect / 2, fre_sect / 2);

    return 0;
  }


int main_qspi_write_read_test(void)
{
	/*这里XSP_MANUAL_SSELECT_OPTION非常重要,对于spi普通模式,XSP_MANUAL_SSELECT_OPTION勾选表示自动管理NSS*/
	/*如果不勾选,那么一次只能发送不超过FIFO Size的数量,这是没法用的*/
	AXI_SPI_Init(&AXI_SPI0, XPAR_SPI_0_DEVICE_ID, XSP_MASTER_OPTION|XSP_MANUAL_SSELECT_OPTION);
	FlashReadID();
	qspi_buffer.wr_length=4096;
	qspi_buffer.rd_length=4096;

	qspi_buffer.wr_en=1;
	qspi_buffer.rd_en=1;
	qspi_buffer.flash_start_addr=69*4096;

	for(int i=0; i<4096; i++){
		qspi_buffer.wr_buff[i]=i*i;
		qspi_buffer.rd_buff[i]=0;
	}

	//W25QXX_Write(qspi_buffer.wr_buff, qspi_buffer.flash_start_addr, qspi_buffer.wr_length);
	W25QXX_Read(qspi_buffer.rd_buff, qspi_buffer.flash_start_addr, qspi_buffer.rd_length);

	for (int Count = 0; Count < qspi_buffer.rd_length; Count++) {
		printf("%d: %x--%x\n", Count, qspi_buffer.wr_buff[Count], qspi_buffer.rd_buff[Count]);
	}

	return 0;
}


int main(void)
{
	//Xil_DCacheDisable();
	xil_printf("hello\r\n");
	main_fats();

	//main_qspi_write_read_test();
}

qspi.c/h

#include "QspiFlash.h"
#include "COMMON.h"

#define WRITE_STATUS_CMD	0x01
#define WRITE_CMD			0x02
#define READ_CMD			0x03
#define WRITE_DISABLE_CMD	0x04
#define READ_STATUS_CMD		0x05
#define WRITE_ENABLE_CMD	0x06
#define FAST_READ_CMD		0x0B
#define DUAL_READ_CMD		0x3B
#define QUAD_READ_CMD		0x6B
#define BULK_ERASE_CMD		0xC7
#define	SEC_ERASE_CMD		0x20   /*xilinx demo程序有错误,进行了修改*/
#define READ_ID				0x9F

#define COMMAND_OFFSET		0 /* FLASH instruction */
#define ADDRESS_1_OFFSET	1 /* MSB byte of address to read or write */
#define ADDRESS_2_OFFSET	2 /* Middle byte of address to read or write */
#define ADDRESS_3_OFFSET	3 /* LSB byte of address to read or write */
#define DATA_OFFSET			4 /* Start of Data for Read/Write */
#define DUMMY_OFFSET		4 /* Dummy byte offset for fast, dual and quad
				   * reads
				   */
#define DUMMY_SIZE		1 /* Number of dummy bytes for fast, dual and
				   		* quad reads
				   		*/
#define RD_ID_SIZE			4 /* Read ID command + 3 bytes ID response */
#define BULK_ERASE_SIZE		1 /* Bulk Erase command size */
#define SEC_ERASE_SIZE		4 /* Sector Erase command + Sector address */

/*
 * The following constants specify the extra bytes which are sent to the
 * FLASH on the QSPI interface, that are not data, but control information
 * which includes the command and address
 */
#define OVERHEAD_SIZE		4

#define PAGE_SIZE		256
#define SECTOR_SIZE		4096
#define NUM_PAGES		16384
#define NUM_SECTORS		1024


u8 txBuffer[256+4];
u8 rxBuffer[4096+4];

void FlashReadID(void) {
	txBuffer[0] = 0x9f;
	txBuffer[1] = 0x23; /* 3 dummy bytes */
	txBuffer[2] = 0x08;
	txBuffer[3] = 0x09;
	AXI_SPI_Transfer(&AXI_SPI0, 0, rxBuffer, txBuffer, 4);

	xil_printf("FlashID=0x%x 0x%x 0x%x\n\r", rxBuffer[1], rxBuffer[2],
			rxBuffer[3]);
}

//读取SPI FLASH,仅支持QPI模式  
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(最大32bit)
//NumByteToRead:要读取的字节数(最大65535)
// void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)   
// { 
// 	u8 data_offset=4; /*需要根据不同读命令进行调整*/
// 	txBuffer[0] = 0x03;
// 	txBuffer[1] = (u8) ((ReadAddr & 0xFF0000) >> 16);
// 	txBuffer[2] = (u8) ((ReadAddr & 0xFF00) >> 8);
// 	txBuffer[3] = (u8) (ReadAddr & 0xFF);
// 	AXI_SPI_Transfer(&AXI_SPI0, 0, pBuffer, txBuffer, NumByteToRead+data_offset); 
    /*注意,用在FATFS中,当sector_size=4096时,会出现数组越界,为此采用了下面的写法*/

// 	memmove(pBuffer, pBuffer+data_offset, NumByteToRead);  /*这句非常关键!!*/
// }  

void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)   
{ 
	u8 data_offset=4; /*需要根据不同读命令进行调整*/
	txBuffer[0] = 0x03;
	txBuffer[1] = (u8) ((ReadAddr & 0xFF0000) >> 16);
	txBuffer[2] = (u8) ((ReadAddr & 0xFF00) >> 8);
	txBuffer[3] = (u8) (ReadAddr & 0xFF);
	AXI_SPI_Transfer(&AXI_SPI0, 0, rxBuffer, txBuffer, NumByteToRead+data_offset);

	memmove(pBuffer, rxBuffer+data_offset, NumByteToRead);  /*这句非常关键!!*/
}  



//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!	 
void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
	txBuffer[0] = 0x06;
	AXI_SPI_Transfer(&AXI_SPI0, 0, NULL, txBuffer, 1);

	u8 data_offset=4;
	memmove(txBuffer+data_offset, pBuffer, NumByteToWrite);  /*这句非常关键!!*/
	txBuffer[0] = 0x02;
	txBuffer[1] = (u8) ((WriteAddr & 0xFF0000) >> 16);
	txBuffer[2] = (u8) ((WriteAddr & 0xFF00) >> 8);
	txBuffer[3] = (u8) (WriteAddr & 0xFF);
	AXI_SPI_Transfer(&AXI_SPI0, 0, NULL, txBuffer, NumByteToWrite+data_offset);
	usleep(20*1000);
} 

//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)   
{
	txBuffer[0] = 0x06;
	AXI_SPI_Transfer(&AXI_SPI0, 0, NULL, txBuffer, 1);

	Dst_Addr*=SECTOR_SIZE;
	txBuffer[0] = 0x20;
	txBuffer[1] = (u8) ((Dst_Addr & 0xFF0000) >> 16);
	txBuffer[2] = (u8) ((Dst_Addr & 0xFF00) >> 8);
	txBuffer[3] = (u8) (Dst_Addr & 0xFF);
	AXI_SPI_Transfer(&AXI_SPI0, 0, NULL, txBuffer, 4);
	usleep(200*1000);
}

/****************************************************************************************************/
/****************************************************************************************************/

//无检验写SPI FLASH 
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能 
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{ 			 		 
	u16 pageremain;	   
	pageremain=256-WriteAddr%256; //单页剩余的字节数		 	    
	if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
	while(1)
	{	   
		W25QXX_Write_Page(pBuffer,WriteAddr,pageremain);
		if(NumByteToWrite==pageremain)break;//写入结束了
	 	else //NumByteToWrite>pageremain
		{
			pBuffer+=pageremain;
			WriteAddr+=pageremain;	

			NumByteToWrite-=pageremain;			  //减去已经写入了的字节数
			if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
			else pageremain=NumByteToWrite; 	  //不够256个字节了
		}
	}   
} 

//写SPI FLASH  
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)						
//NumByteToWrite:要写入的字节数(最大65535)   
u8 W25QXX_BUFFER[4096];		 
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{ 
	u32 secpos;
	u16 secoff;
	u16 secremain;	   
 	u16 i;    
	u8 * W25QXX_BUF;	  
   	W25QXX_BUF=W25QXX_BUFFER;	     
 	secpos=WriteAddr/4096;//扇区地址  
	secoff=WriteAddr%4096;//在扇区内的偏移
	secremain=4096-secoff;//扇区剩余空间大小   
 	//printf("ad:%X,nb:%X\r\n",WriteAddr,NumByteToWrite);//测试用
 	if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
	while(1) 
	{	
		W25QXX_Read(W25QXX_BUF,secpos*4096,4096);//读出整个扇区的内容
		for(i=0;i<secremain;i++)//校验数据
		{
			if(W25QXX_BUF[secoff+i]!=0XFF)break;//需要擦除  	  
		}
		if(i<secremain)//需要擦除
		{
			W25QXX_Erase_Sector(secpos);//擦除这个扇区
			for(i=0;i<secremain;i++)	   //复制
			{
				W25QXX_BUF[i+secoff]=pBuffer[i];	  
			}
			W25QXX_Write_NoCheck(W25QXX_BUF,secpos*4096,4096);//写入整个扇区  

		}else W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间. 				   
		if(NumByteToWrite==secremain)break;//写入结束了
		else//写入未结束
		{
			secpos++;//扇区地址增1
			secoff=0;//偏移位置为0 	 

		   	pBuffer+=secremain;  //指针偏移
			WriteAddr+=secremain;//写地址偏移	   
		   	NumByteToWrite-=secremain;				//字节数递减
			if(NumByteToWrite>4096)secremain=4096;	//下一个扇区还是写不完
			else secremain=NumByteToWrite;			//下一个扇区可以写完了
		}	 
	};	 
}
#ifndef QSPI_FLASH_H_
#define QSPI_FLASH_H_

#include "COMMON.h"

typedef struct{
	u8 wr_buff[1024*1024];
	u8 rd_buff[1024*1024];
	u32 wr_length;
	u32 rd_length;
	u32 flash_start_addr;

	u8 wr_en;
	u8 rd_en;
} qspi_buff_t;


void FlashReadID(void);
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead);
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
void W25QXX_Erase_Sector(u32 Dst_Addr);

#endif /* QSPI_FLASH_H_ */

D4 补充说明

关于disk_ioctrl函数的移植问题,以及spi_flash的sector与fatfs的sector的差别。

https://blog.csdn.net/weixin_47579878/article/details/135721025

image

D5 FATFS官网上3个漂亮的函数

查找指定类型文件,可以用通配符

http://elm-chan.org/fsw/ff/doc/findfirst.html

遍历文件夹,并打印出所有的文件

http://elm-chan.org/fsw/ff/doc/readdir.html

结果运行

int run_demo()
{
	demo_find_files();
	demo_scan_files();
	demo_list_dir();

	return 0;
}


int main(void)
{
	//Xil_DCacheDisable();
	//main_uart_wr_to_qspi();
	xil_printf("hello\r\n");
	//main_fats();

	//main_qspi_write_read_test();
    //main_uart_wr_to_qspi_fats();
	run_demo();
}

image

看起来demo_scan_files()这个最好用,它不仅列出了有哪些文件,还列出了文件的大小。这个很好。

Clone this wiki locally