sdk例程模板1:原子的W25QXX驱动层FATFS接口 - minichao9901/TangNano-20k-Zynq-7020 GitHub Wiki
说明
底层需要实现3个函数即可:W25QXX_Read, W25QXX_Write_Page, W25QXX_Erase_Sector
QspiFlash.c
#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, 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; //下一个扇区可以写完了
}
};
}
QspiFlash.h
#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_ */
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;
}