XilinxProcessorIPLib/drivers/qspips/examples/xqspips_g128_flash_example.c
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command,
				u8 *WriteBfrPtr)
{
	u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
	u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };  /* Must send 2 bytes */
	u8 FlashStatus[2];
	u32 RealAddr;
	u32 BankSel;
	u8 ReadFlagSRCmd[] = {READ_FLAG_STATUS_CMD, 0};
	u8 FlagStatus[2];

	/*
	 * Translate address based on type of connection
	 * If stacked assert the slave select based on address
	 */
	RealAddr = GetRealAddr(QspiPtr, Address);
	/*
	 * Bank Select
	 */
	if(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB) {
		/*
		 * Calculate bank
		 */
		BankSel = RealAddr/SIXTEENMB;
		/*
		 * Select bank
		 */
		SendBankSelect(QspiPtr, WriteBfrPtr, BankSel);
	}

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


	/*
	 * Setup the write command with the specified address and data for the
	 * Flash
	 */
	/*
	 * This will ensure a 3B address is transferred even when address
	 * is greater than 128Mb.
	 */
	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);

	/*
	 * 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, WriteBfrPtr, NULL,
				ByteCount + OVERHEAD_SIZE);

	if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
			(FlashMake == MICRON_ID_BYTE0)) {
		XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
					sizeof(ReadFlagSRCmd));
	}
	/*
	 * 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;
		}
	}

		if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
			(FlashMake == MICRON_ID_BYTE0)) {
		XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
					sizeof(ReadFlagSRCmd));
	}

}
/*****************************************************************************/
/**
*
*
* This function writes to the  serial Flash connected to the QSPI interface.
* All the data put into the buffer must be in the same page of the device with
* page boundaries being on 256 byte boundaries.
*
* @param	QspiPtr is a pointer to the QSPI driver component to use.
* @param	Address contains the address to write data to in the Flash.
* @param	ByteCount contains the number of bytes to write.
* @param	Command is the command used to write data to the flash. QSPI
*		device supports only Page Program command to write data to the
*		flash.
* @param	WriteBfrptr is the pointer to the write buffer(which is to be transmitted)
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
This function writes to the  serial Flash connected to the QSPI interface.
All the data put into the buffer must be in the same page of the device with
page boundaries being on 256 byte boundaries.
This function writes to the serial Flash connected to the QSPI interface . All the data put into the buffer must be in the same page of the device with page boundaries being on 256 byte boundaries .
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command,
				u8 *WriteBfrPtr)
{
	u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
	u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };  
	u8 FlashStatus[2];
	u32 RealAddr;
	u32 BankSel;
	u8 ReadFlagSRCmd[] = {READ_FLAG_STATUS_CMD, 0};
	u8 FlagStatus[2];
	RealAddr = GetRealAddr(QspiPtr, Address);
	if(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB) {
		BankSel = RealAddr/SIXTEENMB;
		SendBankSelect(QspiPtr, WriteBfrPtr, BankSel);
	}
	XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
				sizeof(WriteEnableCmd));
	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);
	XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr, NULL,
				ByteCount + OVERHEAD_SIZE);
	if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
			(FlashMake == MICRON_ID_BYTE0)) {
		XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
					sizeof(ReadFlagSRCmd));
	}
	while (1) {
		XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
					sizeof(ReadStatusCmd));
		if ((FlashStatus[1] & 0x01) == 0) {
			break;
		}
	}
		if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
			(FlashMake == MICRON_ID_BYTE0)) {
		XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
					sizeof(ReadFlagSRCmd));
	}
}
void FlashWrite ( XQspiPs * QspiPtr , u32 Address , u32 ByteCount , u8 Command , u8 * WriteBfrPtr ) { u8 WriteEnableCmd = { WRITE_ENABLE_CMD } ; u8 ReadStatusCmd [ ] = { READ_STATUS_CMD , 0 } ; u8 FlashStatus [ 2 ] ; u32 RealAddr ; u32 BankSel ; u8 ReadFlagSRCmd [ ] = { READ_FLAG_STATUS_CMD , 0 } ; u8 FlagStatus [ 2 ] ; RealAddr = GetRealAddr ( QspiPtr , Address ) ; if ( Flash_Config_Table [ FCTIndex ] . FlashDeviceSize > SIXTEENMB ) { BankSel = RealAddr / SIXTEENMB ; SendBankSelect ( QspiPtr , WriteBfrPtr , BankSel ) ; } XQspiPs_PolledTransfer ( QspiPtr , & WriteEnableCmd , NULL , sizeof ( WriteEnableCmd ) ) ; 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 ) ; XQspiPs_PolledTransfer ( QspiPtr , WriteBfrPtr , NULL , ByteCount + OVERHEAD_SIZE ) ; if ( ( Flash_Config_Table [ FCTIndex ] . NumDie > 1 ) && ( FlashMake == MICRON_ID_BYTE0 ) ) { XQspiPs_PolledTransfer ( QspiPtr , ReadFlagSRCmd , FlagStatus , sizeof ( ReadFlagSRCmd ) ) ; } while ( 1 ) { XQspiPs_PolledTransfer ( QspiPtr , ReadStatusCmd , FlashStatus , sizeof ( ReadStatusCmd ) ) ; if ( ( FlashStatus [ 1 ] & 0x01 ) == 0 ) { break ; } } if ( ( Flash_Config_Table [ FCTIndex ] . NumDie > 1 ) && ( FlashMake == MICRON_ID_BYTE0 ) ) { XQspiPs_PolledTransfer ( QspiPtr , ReadFlagSRCmd , FlagStatus , sizeof ( ReadFlagSRCmd ) ) ; } }
This function writes to the  serial Flash connected to the QSPI interface.
This function writes to the serial Flash connected to the QSPI interface .
/* Must send 2 bytes */ /*
	 * Translate address based on type of connection
	 * If stacked assert the slave select based on address
	 */ /*
	 * Bank Select
	 */ /*
		 * Calculate bank
		 */ /*
		 * Select bank
		 */ /*
	 * Send the write enable command to the Flash so that it can be
	 * written to, this needs to be sent as a separate transfer before
	 * the write
	 */ /*
	 * Setup the write command with the specified address and data for the
	 * Flash
	 */ /*
	 * This will ensure a 3B address is transferred even when address
	 * is greater than 128Mb.
	 */ /*
	 * Send the write command, address, and data to the Flash to be
	 * written, no receive buffer is specified since there is nothing to
	 * receive
	 */ /*
	 * Wait for the write command to the Flash to be completed, it takes
	 * some time for the data to be written
	 */ /*
		 * Poll the status register of the Flash to determine when it
		 * completes, by sending a read status command and receiving the
		 * status byte
		 */ /*
		 * 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
		 */