Skip to content

dk例程: ps_qspi_flash读写例程2

minichao9901 edited this page Jun 10, 2024 · 2 revisions

D1 原子的spi_flash读写程序的接口函数

原子的spi_flash接口函数非常好用,为了移植,主要是要实现如下3个函数

  • void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
  • void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
  • void W25QXX_Erase_Sector(u32 Dst_Addr)
  • 把这3个函数实现了,整个flash的读写就通了。FATFS移植也非常简单了,可以直接照搬原子的。
//读取SPI FLASH,仅支持QPI模式  
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(最大32bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)   
{ 
}  


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

//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)   
{
}


//无检验写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;			//下一个扇区可以写完了
		}	 
	};	 
}

D2 基于zynq的ps_qspi_flash,移植到原子的读写框架上去

其中注意flash_read函数,它读出的数据是包含4个dummy数据的,需要移除。移除的方法,我们使用了memmove函数,这个非常好用!它不同于memcpy,它可以支持在一个数组内部的移动

#include "xqspips.h"		/* QSPI device driver */
#include "QspiFlash.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

/*
 * The following constants specify the page size, sector size, and number of
 * pages and sectors for the FLASH.  The page size specifies a max number of
 * bytes that can be written to the FLASH with a single transfer.
 */
/*xilinx demo程序有错误,进行了修改*/
#define PAGE_SIZE		256
#define NUM_PAGES		16384
#define SECTOR_SIZE		4096
#define NUM_SECTORS		1024

/* Flash address to which data is ot be written.*/
#define TEST_ADDRESS		0x00400000  	/*4Mbyte*/
/*
 * The following constants specify the max amount of data and the size of the
 * the buffer required to hold the data and overhead to transfer the data to
 * and from the FLASH.
 */
#define MAX_DATA		(1024 * PAGE_SIZE)



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

#define QSPI_DEVICE_ID		XPAR_XQSPIPS_0_DEVICE_ID
XQspiPs QspiInstance;
static u8 ReadBuffer[MAX_DATA + DATA_OFFSET + DUMMY_SIZE];
static u8 WriteBuffer[PAGE_SIZE + DATA_OFFSET];


void FlashWrite(XQspiPs *QspiPtr, u32 Address, u8* pBuff, u32 ByteCount,
		u8 Command) {
	u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
	u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 }; /* must send 2 bytes */
	u8 FlashStatus[2];

	/*
	 * Send the write enable command to the FLASH so that it can be
	 * written to, this needs to be sent as a seperate transfer before
	 * the write
	 */
	XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
			sizeof(WriteEnableCmd));

	/*
	 * Setup the write command with the specified address and data for the
	 * FLASH
	 */
	WriteBuffer[COMMAND_OFFSET] = Command;
	WriteBuffer[ADDRESS_1_OFFSET] = (u8) ((Address & 0xFF0000) >> 16);
	WriteBuffer[ADDRESS_2_OFFSET] = (u8) ((Address & 0xFF00) >> 8);
	WriteBuffer[ADDRESS_3_OFFSET] = (u8) (Address & 0xFF);

	memcpy(&WriteBuffer[4], pBuff, ByteCount);

	/*
	 * Send the write command, address, and data to the FLASH to be
	 * written, no receive buffer is specified since there is nothing to
	 * receive
	 */
	XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,
			ByteCount + OVERHEAD_SIZE);

	/*
	 * Wait for the write command to the FLASH to be completed, it takes
	 * some time for the data to be written
	 */
	while (1) {
		/*
		 * Poll the status register of the FLASH to determine when it
		 * completes, by sending a read status command and receiving the
		 * status byte
		 */
		XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
				sizeof(ReadStatusCmd));

		/*
		 * If the status indicates the write is done, then stop waiting,
		 * if a value of 0xFF in the status byte is read from the
		 * device and this loop never exits, the device slave select is
		 * possibly incorrect such that the device status is not being
		 * read
		 */
		if ((FlashStatus[1] & 0x01) == 0) {
			break;
		}
	}
}


void FlashRead(XQspiPs *QspiPtr, u32 Address, u8* pBuff, u32 ByteCount, u8 Command) {
	/*
	 * Setup the write command with the specified address and data for the
	 * FLASH
	 */
	WriteBuffer[COMMAND_OFFSET] = Command;
	WriteBuffer[ADDRESS_1_OFFSET] = (u8) ((Address & 0xFF0000) >> 16);
	WriteBuffer[ADDRESS_2_OFFSET] = (u8) ((Address & 0xFF00) >> 8);
	WriteBuffer[ADDRESS_3_OFFSET] = (u8) (Address & 0xFF);

	if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD)
			|| (Command == QUAD_READ_CMD)) {
		ByteCount += DUMMY_SIZE;
	}
	/*
	 * Send the read command to the FLASH to read the specified number
	 * of bytes from the FLASH, send the read command and address and
	 * receive the specified number of bytes of data in the data buffer
	 */
	XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, pBuff,
			ByteCount + OVERHEAD_SIZE);
}


void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount) {
	u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
	u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 }; /* must send 2 bytes */
	u8 FlashStatus[2];
	int Sector;

	/*
	 * If erase size is same as the total size of the flash, use bulk erase
	 * command
	 */
	if (ByteCount == (NUM_SECTORS * SECTOR_SIZE)) {
		/*
		 * Send the write enable command to the FLASH so that it can be
		 * written to, this needs to be sent as a seperate transfer
		 * before the erase
		 */
		XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
				sizeof(WriteEnableCmd));

		/* Setup the bulk erase command*/
		WriteBuffer[COMMAND_OFFSET] = BULK_ERASE_CMD;

		/*
		 * Send the bulk erase command; no receive buffer is specified
		 * since there is nothing to receive
		 */
		XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,
		BULK_ERASE_SIZE);

		/* Wait for the erase command to the FLASH to be completed*/
		while (1) {
			/*
			 * Poll the status register of the device to determine
			 * when it completes, by sending a read status command
			 * and receiving the status byte
			 */
			XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
					sizeof(ReadStatusCmd));

			/*
			 * If the status indicates the write is done, then stop
			 * waiting; if a value of 0xFF in the status byte is
			 * read from the device and this loop never exits, the
			 * device slave select is possibly incorrect such that
			 * the device status is not being read
			 */
			if ((FlashStatus[1] & 0x01) == 0) {
				break;
			}
		}

		return;
	}

	/*
	 * If the erase size is less than the total size of the flash, use
	 * sector erase command
	 */
	for (Sector = 0; Sector < ((ByteCount / SECTOR_SIZE) + 1); Sector++) {
		/*
		 * Send the write enable command to the SEEPOM so that it can be
		 * written to, this needs to be sent as a seperate transfer
		 * before the write
		 */
		XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
				sizeof(WriteEnableCmd));

		/*
		 * Setup the write command with the specified address and data
		 * for the FLASH
		 */
		WriteBuffer[COMMAND_OFFSET] = SEC_ERASE_CMD;
		WriteBuffer[ADDRESS_1_OFFSET] = (u8) (Address >> 16);
		WriteBuffer[ADDRESS_2_OFFSET] = (u8) (Address >> 8);
		WriteBuffer[ADDRESS_3_OFFSET] = (u8) (Address & 0xFF);

		/*
		 * Send the sector erase command and address; no receive buffer
		 * is specified since there is nothing to receive
		 */
		XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,
		SEC_ERASE_SIZE);

		/*
		 * Wait for the sector erse command to the
		 * FLASH to be completed
		 */
		while (1) {
			/*
			 * Poll the status register of the device to determine
			 * when it completes, by sending a read status command
			 * and receiving the status byte
			 */
			XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
					sizeof(ReadStatusCmd));

			/*
			 * If the status indicates the write is done, then stop
			 * waiting, if a value of 0xFF in the status byte is
			 * read from the device and this loop never exits, the
			 * device slave select is possibly incorrect such that
			 * the device status is not being read
			 */
			if ((FlashStatus[1] & 0x01) == 0) {
				break;
			}
		}

		Address += SECTOR_SIZE;
	}
}

int FlashReadID(void) {
	int Status;

	/* Read ID in Auto mode.*/
	WriteBuffer[COMMAND_OFFSET] = READ_ID;
	WriteBuffer[ADDRESS_1_OFFSET] = 0x23; /* 3 dummy bytes */
	WriteBuffer[ADDRESS_2_OFFSET] = 0x08;
	WriteBuffer[ADDRESS_3_OFFSET] = 0x09;

	Status = XQspiPs_PolledTransfer(&QspiInstance, WriteBuffer, ReadBuffer,
	RD_ID_SIZE);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

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

	return XST_SUCCESS;
}


void FlashQuadEnable(XQspiPs *QspiPtr) {
	u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
	u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };
	u8 QuadEnableCmd[] = { WRITE_STATUS_CMD, 0 };
	u8 FlashStatus[2];

	if (ReadBuffer[1] == 0x9D) {

		XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
				sizeof(ReadStatusCmd));

		QuadEnableCmd[1] = FlashStatus[1] | 1 << 6;

		XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
				sizeof(WriteEnableCmd));

		XQspiPs_PolledTransfer(QspiPtr, QuadEnableCmd, NULL,
				sizeof(QuadEnableCmd));
	}
}

void QspiFlash_Init(void) {
	int Status;
	XQspiPs_Config *QspiConfig;
	XQspiPs *QspiInstancePtr = &QspiInstance;
	u16 QspiDeviceId = XPAR_PS7_UART_0_DEVICE_ID;

	/* Initialize the QSPI driver so that it's ready to use*/
	QspiConfig = XQspiPs_LookupConfig(QspiDeviceId);

	Status = XQspiPs_CfgInitialize(QspiInstancePtr, QspiConfig,
			QspiConfig->BaseAddress);

	/* Perform a self-test to check hardware build*/
	Status = XQspiPs_SelfTest(QspiInstancePtr);

	/*
	 * Set Manual Start and Manual Chip select options and drive HOLD_B
	 * pin high.
	 */
	XQspiPs_SetOptions(QspiInstancePtr, XQSPIPS_MANUAL_START_OPTION |
	XQSPIPS_FORCE_SSELECT_OPTION |
	XQSPIPS_HOLD_B_DRIVE_OPTION);

	/* Set the prescaler for QSPI clock*/
	XQspiPs_SetClkPrescaler(QspiInstancePtr, XQSPIPS_CLK_PRESCALE_8);

	/* Assert the FLASH chip select.*/
	XQspiPs_SetSlaveSelect(QspiInstancePtr);

	FlashReadID();

	FlashQuadEnable(QspiInstancePtr);
}

/*****************************************************************************/
int QspiFlash_Write_Read_Demo(qspi_buff_t *pbuff) {
	u32 err_cnt = 0;
	XQspiPs *QspiInstancePtr = &QspiInstance;

	if(pbuff->wr_en){
		FlashErase(QspiInstancePtr, pbuff->flash_start_addr, pbuff->wr_length);

		/*
		 * Write the data in the write buffer to the serial FLASH a page at a
		 * time, starting from TEST_ADDRESS
		 */
		for (int Page = 0; Page < pbuff->wr_length/PAGE_SIZE+1; Page++) {
			FlashWrite(QspiInstancePtr, (Page * PAGE_SIZE) + pbuff->flash_start_addr,
					&pbuff->wr_buff[Page * PAGE_SIZE],
					PAGE_SIZE, WRITE_CMD);
		}
	}

	if(pbuff->rd_en){
		/*
		 * Read the contents of the FLASH from TEST_ADDRESS, using Normal Read
		 * command. Change the prescaler as the READ command operates at a
		 * lower frequency.
		 */
		FlashRead(QspiInstancePtr, pbuff->flash_start_addr, pbuff->rd_buff,
				pbuff->rd_length, READ_CMD);

		/*
		 * Setup a pointer to the start of the data that was read into the read
		 * buffer and verify the data read is the data that was written
		 */
		u8* BufferPtr = &pbuff->rd_buff[DATA_OFFSET];

		for (int Count = 0; Count < pbuff->rd_length; Count++) {
			printf("%d: %x---%x\n", Count, BufferPtr[Count], pbuff->wr_buff[Count]);
			if (BufferPtr[Count] != pbuff->wr_buff[Count]) {
				err_cnt++;
			}
		}
	}

	return err_cnt;
}


void QspiFlash_Read_Demo(qspi_buff_t *pbuff) {
	XQspiPs *QspiInstancePtr = &QspiInstance;

	if(pbuff->rd_en){
		/*
		 * Read the contents of the FLASH from TEST_ADDRESS, using Normal Read
		 * command. Change the prescaler as the READ command operates at a
		 * lower frequency.
		 */
		FlashRead(QspiInstancePtr, pbuff->flash_start_addr, pbuff->rd_buff,
				pbuff->rd_length, READ_CMD);

		/*
		 * Setup a pointer to the start of the data that was read into the read
		 * buffer and verify the data read is the data that was written
		 */
		u8* BufferPtr = &pbuff->rd_buff[DATA_OFFSET];
		for (int Count = 0; Count < pbuff->rd_length; Count++) {
			printf("%d: %x\n", Count, BufferPtr[Count]);
		}
	}
}



//读取SPI FLASH,仅支持QPI模式  
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(最大32bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)   
{ 
	XQspiPs *QspiInstancePtr = &QspiInstance;
	FlashRead(QspiInstancePtr, ReadAddr, pBuffer,NumByteToRead, READ_CMD);


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


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

//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)   
{
	XQspiPs *QspiInstancePtr = &QspiInstance;
	FlashErase(QspiInstancePtr, Dst_Addr*SECTOR_SIZE, SECTOR_SIZE);
}


//无检验写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"
#include "xqspips.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;

extern XQspiPs QspiInstance;

void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount);
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u8* pBuff, u32 ByteCount, u8 Command);
void FlashRead(XQspiPs *QspiPtr, u32 Address, u8* pBuff, u32 ByteCount, u8 Command);
int FlashReadID(void);
void FlashQuadEnable(XQspiPs *QspiPtr);

void QspiFlash_Init(void);
int QspiFlash_Write_Read_Demo(qspi_buff_t *pbuff);
void QspiFlash_Read_Demo(qspi_buff_t *pbuff);


void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
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_ */

D3 main.c测试程序

int main_qspi_write_read_test(void)
{
	QspiFlash_Init();
	qspi_buffer.wr_length=2000;
	qspi_buffer.rd_length=2000;

	qspi_buffer.wr_en=1;
	qspi_buffer.rd_en=1;
	qspi_buffer.flash_start_addr=0;

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

	W25QXX_Write(qspi_buffer.wr_buff, 0x00, qspi_buffer.wr_length);
	W25QXX_Read(qspi_buffer.rd_buff, 0x00, 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_printf("hello\r\n");
	main_qspi_write_read_test();
}

image

总结

要注意flash_read的dummy数据需要移除

学到了一个新的函数:memmove

Clone this wiki locally