XilinxProcessorIPLib/drivers/qspips/examples/xqspips_g128_flash_example.c
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command,
				u8 *WriteBfrPtr, u8 *ReadBfrPtr)
{
	u32 RealAddr;
	u32 RealByteCnt;
	u32 BankSel;
	u32 BufferIndex;
	u32 TotalByteCnt;
	u8 ShiftSize;

	/*
	 * Retain the actual byte count
	 */
	TotalByteCnt = ByteCount;

	while(((signed long)(ByteCount)) > 0) {

		/*
		 * Translate address based on type of connection
		 * If stacked assert the slave select based on address
		 */
		RealAddr = GetRealAddr(QspiPtr, Address);

		/*
		 * Select bank
		 */
		if(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB) {
			BankSel = RealAddr/SIXTEENMB;
			SendBankSelect(QspiPtr, WriteBfrPtr, BankSel);
		}

		/*
		 * If data to be read spans beyond the current bank, then
		 * calculate RealByteCnt in current bank. Else
		 * RealByteCnt is the same as ByteCount
		 */
		if((Address & BANKMASK) != ((Address+ByteCount) & BANKMASK)) {
			RealByteCnt = (Address & BANKMASK) + SIXTEENMB - Address;
		}else {
			RealByteCnt = ByteCount;
		}


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

		if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
		    (Command == QUAD_READ_CMD)) {
			RealByteCnt += 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, WriteBfrPtr,
				&(ReadBfrPtr[TotalByteCnt - ByteCount]),
				RealByteCnt + OVERHEAD_SIZE);

		/*
		 * To discard the first 5 dummy bytes, shift the data in read buffer
		 */
		if((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
			    (Command == QUAD_READ_CMD)){
			ShiftSize = OVERHEAD_SIZE + DUMMY_SIZE;
		}else{
			ShiftSize =  OVERHEAD_SIZE;
		}

		for(BufferIndex = (TotalByteCnt - ByteCount);
				BufferIndex < (TotalByteCnt - ByteCount) + RealByteCnt;
				BufferIndex++) {
			ReadBfrPtr[BufferIndex] = ReadBfrPtr[BufferIndex + ShiftSize];
		}

		/*
		 * Increase address to next bank
		 */
		Address = (Address & BANKMASK) + SIXTEENMB;
		/*
		 * Decrease byte count by bytes already read.
		 */
		if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
		    (Command == QUAD_READ_CMD)) {
			ByteCount = ByteCount - (RealByteCnt - DUMMY_SIZE);
		}else {
			ByteCount = ByteCount - RealByteCnt;
		}

	}

}
/*****************************************************************************/
/**
*
* This function performs an I/O read.
*
* @param	QspiPtr is a pointer to the QSPI driver component to use.
* @param	Address contains the address of the first sector which needs to
*			be erased.
* @param	ByteCount contains the total size to be erased.
* @param	Command is the command used to read data from the flash. Supports
* 			normal, fast, dual and quad read commands.
* @param	WriteBfrPtr is pointer to the write buffer which contains
*		data to be transmitted
* @param	ReadBfrPtr is pointer to the read buffer to which valid
*		received data should be written
*
* @return	none.
*
* @note		None.
*
******************************************************************************/
This function performs an I/O read.
This function performs an I / O read .
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command,
				u8 *WriteBfrPtr, u8 *ReadBfrPtr)
{
	u32 RealAddr;
	u32 RealByteCnt;
	u32 BankSel;
	u32 BufferIndex;
	u32 TotalByteCnt;
	u8 ShiftSize;
	TotalByteCnt = ByteCount;
	while(((signed long)(ByteCount)) > 0) {
		RealAddr = GetRealAddr(QspiPtr, Address);
		if(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB) {
			BankSel = RealAddr/SIXTEENMB;
			SendBankSelect(QspiPtr, WriteBfrPtr, BankSel);
		}
		if((Address & BANKMASK) != ((Address+ByteCount) & BANKMASK)) {
			RealByteCnt = (Address & BANKMASK) + SIXTEENMB - Address;
		}else {
			RealByteCnt = ByteCount;
		}
		WriteBfrPtr[COMMAND_OFFSET]   = Command;
		WriteBfrPtr[ADDRESS_1_OFFSET] = (u8)((RealAddr & 0xFF0000) >> 16);
		WriteBfrPtr[ADDRESS_2_OFFSET] = (u8)((RealAddr & 0xFF00) >> 8);
		WriteBfrPtr[ADDRESS_3_OFFSET] = (u8)(RealAddr & 0xFF);
		if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
		    (Command == QUAD_READ_CMD)) {
			RealByteCnt += DUMMY_SIZE;
		}
		XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr,
				&(ReadBfrPtr[TotalByteCnt - ByteCount]),
				RealByteCnt + OVERHEAD_SIZE);
		if((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
			    (Command == QUAD_READ_CMD)){
			ShiftSize = OVERHEAD_SIZE + DUMMY_SIZE;
		}else{
			ShiftSize =  OVERHEAD_SIZE;
		}
		for(BufferIndex = (TotalByteCnt - ByteCount);
				BufferIndex < (TotalByteCnt - ByteCount) + RealByteCnt;
				BufferIndex++) {
			ReadBfrPtr[BufferIndex] = ReadBfrPtr[BufferIndex + ShiftSize];
		}
		Address = (Address & BANKMASK) + SIXTEENMB;
		if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
		    (Command == QUAD_READ_CMD)) {
			ByteCount = ByteCount - (RealByteCnt - DUMMY_SIZE);
		}else {
			ByteCount = ByteCount - RealByteCnt;
		}
	}
}
void FlashRead ( XQspiPs * QspiPtr , u32 Address , u32 ByteCount , u8 Command , u8 * WriteBfrPtr , u8 * ReadBfrPtr ) { u32 RealAddr ; u32 RealByteCnt ; u32 BankSel ; u32 BufferIndex ; u32 TotalByteCnt ; u8 ShiftSize ; TotalByteCnt = ByteCount ; while ( ( ( signed long ) ( ByteCount ) ) > 0 ) { RealAddr = GetRealAddr ( QspiPtr , Address ) ; if ( Flash_Config_Table [ FCTIndex ] . FlashDeviceSize > SIXTEENMB ) { BankSel = RealAddr / SIXTEENMB ; SendBankSelect ( QspiPtr , WriteBfrPtr , BankSel ) ; } if ( ( Address & BANKMASK ) != ( ( Address + ByteCount ) & BANKMASK ) ) { RealByteCnt = ( Address & BANKMASK ) + SIXTEENMB - Address ; } else { RealByteCnt = ByteCount ; } WriteBfrPtr [ COMMAND_OFFSET ] = Command ; WriteBfrPtr [ ADDRESS_1_OFFSET ] = ( u8 ) ( ( RealAddr & 0xFF0000 ) >> 16 ) ; WriteBfrPtr [ ADDRESS_2_OFFSET ] = ( u8 ) ( ( RealAddr & 0xFF00 ) >> 8 ) ; WriteBfrPtr [ ADDRESS_3_OFFSET ] = ( u8 ) ( RealAddr & 0xFF ) ; if ( ( Command == FAST_READ_CMD ) || ( Command == DUAL_READ_CMD ) || ( Command == QUAD_READ_CMD ) ) { RealByteCnt += DUMMY_SIZE ; } XQspiPs_PolledTransfer ( QspiPtr , WriteBfrPtr , & ( ReadBfrPtr [ TotalByteCnt - ByteCount ] ) , RealByteCnt + OVERHEAD_SIZE ) ; if ( ( Command == FAST_READ_CMD ) || ( Command == DUAL_READ_CMD ) || ( Command == QUAD_READ_CMD ) ) { ShiftSize = OVERHEAD_SIZE + DUMMY_SIZE ; } else { ShiftSize = OVERHEAD_SIZE ; } for ( BufferIndex = ( TotalByteCnt - ByteCount ) ; BufferIndex < ( TotalByteCnt - ByteCount ) + RealByteCnt ; BufferIndex ++ ) { ReadBfrPtr [ BufferIndex ] = ReadBfrPtr [ BufferIndex + ShiftSize ] ; } Address = ( Address & BANKMASK ) + SIXTEENMB ; if ( ( Command == FAST_READ_CMD ) || ( Command == DUAL_READ_CMD ) || ( Command == QUAD_READ_CMD ) ) { ByteCount = ByteCount - ( RealByteCnt - DUMMY_SIZE ) ; } else { ByteCount = ByteCount - RealByteCnt ; } } }
This function performs an I/O read.
This function performs an I / O read .
/*
	 * Retain the actual byte count
	 */ /*
		 * Translate address based on type of connection
		 * If stacked assert the slave select based on address
		 */ /*
		 * Select bank
		 */ /*
		 * If data to be read spans beyond the current bank, then
		 * calculate RealByteCnt in current bank. Else
		 * RealByteCnt is the same as ByteCount
		 */ /*
		 * Setup the write command with the specified address and data for the
		 * Flash
		 */ /*
		 * 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
		 */ /*
		 * To discard the first 5 dummy bytes, shift the data in read buffer
		 */ /*
		 * Increase address to next bank
		 */ /*
		 * Decrease byte count by bytes already read.
		 */