文章目录
- 1 实验任务
- 2 系统框图
- 3 软件设计
1 实验任务
本实验任务是使用PS侧的QSPI Flash控制器,先后对QSPI Flash 进行写、 读操作。通过对比读出的数据是否等于写入的数据, 从而验证读写操作是否正确。
2 系统框图
3 软件设计
注意事项:这里必须详细记录一下整个的调试过程
- 最初在进行程序固化实验时
- 1)从SD卡可以启动
- 2)从QSPI Flash无法启动,显示回读数据错误,校验失败;此时,我一脸懵逼,因为刚拿到开发板的时候,也进行过程序固化实验,清晰地记得当时从SD卡和QSPI Flash启动都是可以的,怎么现在从QSPI Flash启动就不行了呢?在没有手段和头绪的情况下,只能怀疑Flash芯片是不是坏了
- 修改例程使程序能够遍历Flash芯片的16MB空间(W25Q256JV芯片大小为32MB,但是QSPI Flash控制器使用3字节地址,只能访问16MB的空间)
- 1)测试发现0 - 8MB数据读写比较有错误,8 - 16MB数据读写比较正常
- 2)研究芯片手册发现,芯片有写保护设置,通过状态寄存器的TB位、BP3~BP0位、CMP位和WPS位等可以控制芯片的哪些块只能读取不能擦除和写入,即写保护
- 3)编写FlashReadStatus函数,读取Flash的状态寄存器,发现TB=1,BP3~BP0=1000b,CMP=0,WPS=0,对应芯片的低四分之一区域被写保护了
- 4)编写FlashWriteStatus函数,将状态寄存器的值恢复到出厂设置状态
- 在恢复状态寄存器的出场设置状态时发现,Status Register-1的QE位写0写不进去,一直为1,最后在芯片手册的9.8 Ordering Information小节找到了答案,如下图所示,对于W25Q256JV系列芯片,型号最后一位是Q和N的,QE位出场设置为1且固定(即不可修改)
/***************************** Include Files *********************************/
#include "xparameters.h" /* SDK generated parameters */
#include "xqspips.h" /* QSPI device driver */
#include "stdio.h"
#include "sleep.h"
/************************** Constant Definitions *****************************/
#define QSPI_DEVICE_ID XPAR_XQSPIPS_0_DEVICE_ID/* The following constants define the commands which may be sent to the FLASH device. */
#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 0xD8
#define READ_ID 0x9F#define WRITE_STATUS2_CMD 0x31
#define WRITE_STATUS3_CMD 0x11#define READ_STATUS2_CMD 0x35
#define READ_STATUS3_CMD 0x15/* The following constants define the offsets within a FlashBuffer data type for each kind of data. */
#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 */#define OVERHEAD_SIZE 4/* The following constants specify the page size, sector size, and number of pages and sectors for the FLASH. */
#define SECTOR_SIZE 0x1000
#define NUM_SECTORS 0x1000
#define NUM_PAGES 0x20000
#define PAGE_SIZE 256/* Number of flash pages to be written.*/
#define PAGE_COUNT 16 // 4KB/* Flash address to which data is ot be written.*/
#define TEST_ADDRESS 0x00000000
#define UNIQUE_VALUE 0x00#define MAX_DATA (PAGE_COUNT * PAGE_SIZE)
/**************************** Type Definitions *******************************//***************** Macros (Inline Functions) Definitions *********************//************************** Function Prototypes ******************************/
void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount);
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command);
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command);
int FlashReadID(XQspiPs *QspiPtr);
void FlashReadStatus(XQspiPs *QspiPtr);
void FlashWriteStatus(XQspiPs *QspiPtr);
void FlashQuadEnable(XQspiPs *QspiPtr);
int QspiFlashPolledExample(XQspiPs *QspiInstancePtr, u16 QspiDeviceId);
/************************** Variable Definitions *****************************/
static XQspiPs QspiInstance;u8 ReadBuffer[MAX_DATA + DATA_OFFSET + DUMMY_SIZE];
u8 WriteBuffer[PAGE_SIZE + DATA_OFFSET];u32 BadSectorCount = 0;
/*****************************************************************************/
int main(void)
{//int Status;//printf("QSPI FLASH Write and Read Test\n");/* Run the Qspi Interrupt example.*/Status = QspiFlashPolledExample(&QspiInstance, QSPI_DEVICE_ID);if (Status != XST_SUCCESS) {printf("QSPI FLASH Polled Example Test Failed\n");return XST_FAILURE;}//printf("BadSectorCount = %lu\n", BadSectorCount);//printf("Successfully Ran QSPI FLASH Write and Read Test\n");//return XST_SUCCESS;
}
/*****************************************************************************/
int QspiFlashPolledExample(XQspiPs *QspiInstancePtr, u16 QspiDeviceId)
{//int Status;u8 *BufferPtr;u8 UniqueValue;int Count;int Page;XQspiPs_Config *QspiConfig;/* Initialize the QSPI driver so that it's ready to use */QspiConfig = XQspiPs_LookupConfig(QspiDeviceId);if (QspiConfig == NULL) {return XST_FAILURE;}Status = XQspiPs_CfgInitialize(QspiInstancePtr, QspiConfig, QspiConfig->BaseAddress);if (Status != XST_SUCCESS) {return XST_FAILURE;}/* Perform a self-test to check hardware build */Status = XQspiPs_SelfTest(QspiInstancePtr);if (Status != XST_SUCCESS) {return XST_FAILURE;}/* 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_4);/* Assert the FLASH chip select */XQspiPs_SetSlaveSelect(QspiInstancePtr);/* Write the flash status */FlashWriteStatus(QspiInstancePtr);/* Read the flash status */FlashReadStatus(QspiInstancePtr);/* Read the flash id */FlashReadID(QspiInstancePtr);/* Enabe Quad SPI Operation */FlashQuadEnable(QspiInstancePtr);//printf("\n");/* Initialize the write buffer */for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < PAGE_SIZE; Count++, UniqueValue++) {WriteBuffer[DATA_OFFSET + Count] = (u8)UniqueValue;}/* Traverse the entire flash */for (u32 SectorCount = 0; SectorCount < NUM_SECTORS; SectorCount++) {/* Erase the flash */FlashErase(QspiInstancePtr, TEST_ADDRESS + (SectorCount * SECTOR_SIZE), MAX_DATA);printf("Erase flash sector %lu done.\n", SectorCount);/* Write the flash */for (Page = 0; Page < PAGE_COUNT; Page++) {FlashWrite(QspiInstancePtr, (Page * PAGE_SIZE) + TEST_ADDRESS + (SectorCount * SECTOR_SIZE), PAGE_SIZE, WRITE_CMD);}printf("Write flash sector %lu done.\n", SectorCount);/* Read the flash */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS + (SectorCount * SECTOR_SIZE), MAX_DATA, QUAD_READ_CMD);printf("Read flash sector %lu done.\n", SectorCount);/* Compare the data */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA; Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)UniqueValue) {BadSectorCount++;printf("Sector %lu is bad, BufferPtr[%d] = %hhu\n", SectorCount, Count, BufferPtr[Count]);break;}}printf("Compare flash sector %lu done.\n", SectorCount);//printf("\n");}//return XST_SUCCESS;
}
/*****************************************************************************/
void FlashWrite(XQspiPs *QspiPtr, u32 Address, 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);/** 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, 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);
}
/*****************************************************************************/
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;}
}
/*****************************************************************************/
void FlashWriteStatus(XQspiPs *QspiPtr)
{//u8 WriteEnableCmd = {WRITE_ENABLE_CMD};u8 WriteStatus1Cmd[] = {WRITE_STATUS_CMD, 0x0};u8 WriteStatus2Cmd[] = {WRITE_STATUS2_CMD, 0x00};u8 WriteStatus3Cmd[] = {WRITE_STATUS3_CMD, 0x60};// Write Status Register - 1XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL, sizeof(WriteEnableCmd));XQspiPs_PolledTransfer(QspiPtr, WriteStatus1Cmd, NULL, sizeof(WriteStatus1Cmd));usleep(10000);// Write Status Register - 2XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL, sizeof(WriteEnableCmd));XQspiPs_PolledTransfer(QspiPtr, WriteStatus2Cmd, NULL, sizeof(WriteStatus2Cmd));usleep(10000);// Write Status Register - 3XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL, sizeof(WriteEnableCmd));XQspiPs_PolledTransfer(QspiPtr, WriteStatus3Cmd, NULL, sizeof(WriteStatus3Cmd));usleep(10000);//return;
}
/*****************************************************************************/
void FlashReadStatus(XQspiPs *QspiPtr)
{//u8 ReadStatus1Cmd[] = {READ_STATUS_CMD, 0};u8 ReadStatus2Cmd[] = {READ_STATUS2_CMD, 0};u8 ReadStatus3Cmd[] = {READ_STATUS3_CMD, 0};u8 FlashStatus[2];// Read Status Register - 1XQspiPs_PolledTransfer(QspiPtr, ReadStatus1Cmd, FlashStatus, sizeof(ReadStatus1Cmd));printf("Flash Status Register 1 = 0x%02x\n", FlashStatus[1]);usleep(10000);// Read Status Register - 2XQspiPs_PolledTransfer(QspiPtr, ReadStatus2Cmd, FlashStatus, sizeof(ReadStatus2Cmd));printf("Flash Status Register 2 = 0x%02x\n", FlashStatus[1]);usleep(10000);// Read Status Register - 3XQspiPs_PolledTransfer(QspiPtr, ReadStatus3Cmd, FlashStatus, sizeof(ReadStatus3Cmd));printf("Flash Status Register 3 = 0x%02x\n", FlashStatus[1]);usleep(10000);//return;
}
/*****************************************************************************/
int FlashReadID(XQspiPs *QspiPtr)
{//int Status;u8 ReadIdCmd[] = {READ_ID, 0x23, 0x08, 0x09}; /* 3 dummy bytes */u8 FlashId[4];//Status = XQspiPs_PolledTransfer(QspiPtr, ReadIdCmd, FlashId, RD_ID_SIZE);if (Status != XST_SUCCESS) {return XST_FAILURE;}//printf("FlashID=0x%x 0x%x 0x%x\n", FlashId[1], FlashId[2], FlashId[3]);//return XST_SUCCESS;
}
/*****************************************************************************/
void FlashQuadEnable(XQspiPs *QspiPtr)
{//u8 WriteEnableCmd = {WRITE_ENABLE_CMD};u8 ReadStatusCmd[] = {READ_STATUS2_CMD, 0};u8 QuadEnableCmd[] = {WRITE_STATUS2_CMD, 0};u8 FlashStatus[2];//if (ReadBuffer[1] == 0xEF) { // Winbond Serial Flash//XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus, sizeof(ReadStatusCmd));QuadEnableCmd[1] = FlashStatus[1] | (1 << 1);//XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL, sizeof(WriteEnableCmd));XQspiPs_PolledTransfer(QspiPtr, QuadEnableCmd, NULL, sizeof(QuadEnableCmd));}//return;
}