sdk例程:ps_qspi_flash_fats移植 - minichao9901/TangNano-20k-Zynq-7020 GitHub Wiki
D1 关键点
使用原子的W25Qxx读写框架
- 使用原子的W25Qxx读写框架,只需要实现底层的3个函数即可:W25QXX_Read, W25QXX_Write_Page, W25QXX_Erase_Sector
- 原子的W25Qxx读写框架,对上层FATFS提供的函数有2个:W25QXX_Read, W25QXX_Write。这2个函数可以提供任意长度的读写,里面已经包含了Sector_Erase的过程。
- 对于ps_qspi,可以使用官方demo提供的几个读写函数,进行适当的改造即可用于产生W25QXX_Read, W25QXX_Write_Page, W25QXX_Erase_Sector
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函数
- 具体可以参考axi_qspi_flash_fats移植
D2 Sector_size=4096移植的打印结果
hello
init
FlashID=0xEF 0x40 0x18
io_ctrl
read: 0
read: 63
Volume is not FAT formated; formating FAT
init
FlashID=0xEF 0x40 0x18
io_ctrl
io_ctrl
io_ctrl
write: 63
write: 64
write: 65
write: 66
write: 67
write: 68
write: 69
write: 70
write: 71
write: 72
write: 73
write: 74
write: 75
write: 76
write: 77
write: 78
write: 79
write: 0
io_ctrl
init
FlashID=0xEF 0x40 0x18
io_ctrl
read: 0
read: 63
Success to open SD card!
read: 76
0
write: 76
read: 64
0
15
write: 80
write: 64
read: 76
write: 76
io_ctrl
read: 80
15
www.openedv.com---www.openedv.com
src_str is equal to dest_str,SD card test success!
read: 64
472 KiB total drive space.
471 KiB available.
D3 附录
移植接口: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");
QspiFlash_Init();
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
移植接口:main.c
#include "ACZ702_Lib/COMMON.h"
#include "xil_cache.h"
#include "FATFS/ff.h"
rec_buff_t rec_buffer;
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"; //定义文本内容
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;
char dest_str[30] = "";
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_uart_wr_to_qspi(void)
{
PS_UART_Init(&UartPs1,XPAR_PS7_UART_0_DEVICE_ID, XUARTPS_OPER_MODE_NORMAL, 115200);
QspiFlash_Init();
PS_UART_RX(&rec_buffer);
PS_UART_TX(&rec_buffer);
u32 err_cnt=0;
qspi_buffer.wr_length=rec_buffer.recv_length;
qspi_buffer.rd_length=rec_buffer.recv_length;
// qspi_buffer.wr_en=1;
// qspi_buffer.rd_en=1;
qspi_buffer.flash_start_addr= 0x400000;
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]);
if(qspi_buffer.wr_buff[Count]!=qspi_buffer.rd_buff[Count]){
err_cnt++;
}
}
xil_printf("Error_cnt=%d\r\n", err_cnt);
return 0;
}
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, 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();
//main_uart_wr_to_qspi();
xil_printf("hello\r\n");
main_fats();
//main_qspi_write_read_test();
}
移植接口:QspiFlash.c/h
#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, ReadBuffer,
ByteCount + OVERHEAD_SIZE);
memmove(pBuff, ReadBuffer+OVERHEAD_SIZE, ByteCount);
}
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);
}
/************************************************************************************************/
/************************************************************************************************/
//读取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);
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_ */
补充例程,从uart读取图片,并通过fatfs写入qspi_flash,然后读出进行比较
首先,选择一幅图,用lvgl.io提供的tool进行转换为bin文件
其次,增加一个函数,从uart读取图片,并通过fatfs写入qspi_flash,然后读出进行比较
int main_uart_wr_to_qspi_fats(void)
{
PS_UART_Init(&UartPs1,XPAR_PS7_UART_0_DEVICE_ID, XUARTPS_OPER_MODE_NORMAL, 115200);
QspiFlash_Init();
PS_UART_RX(&rec_buffer);
u32 err_cnt=0;
memcpy(qspi_buffer.wr_buff, rec_buffer.rec_buff, rec_buffer.recv_length);
qspi_buffer.wr_length=rec_buffer.recv_length;
qspi_buffer.rd_length=rec_buffer.recv_length;
platform_init_fs();
char file_name[30]="aa.bin";
//SD卡写数据
sd_write_data(file_name,(u32)qspi_buffer.wr_buff,qspi_buffer.wr_length);
//SD卡读数据
sd_read_data(file_name,(u32)qspi_buffer.rd_buff,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]);
if(qspi_buffer.wr_buff[Count]!=qspi_buffer.rd_buff[Count]){
err_cnt++;
}
}
xil_printf("Error_cnt=%d\r\n", err_cnt);
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();
}
结果
可以看到,写入与读出的数量是正确的。比对的结果是正确的。