lib/sw_services/xilisf/examples/xilisf_stm_read_write_example.c
static int IsfStmFlashExample()
{
	int Status = XST_FAILURE;
	u32 Index;
	u32 Address;
	XIsf_WriteParam WriteParam;
	XIsf_ReadParam ReadParam;

	/*
	 * Initialize the SPI driver so that it's ready to use,
	 * specify the device ID that is generated in xparameters.h.
	 */
	Status = XSpi_Initialize(&Spi, SPI_DEVICE_ID);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Connect the SPI driver to the interrupt subsystem such that
	 * interrupts can occur. This function is application specific.
	 */
	Status = SetupInterruptSystem(&Spi);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Setup the handler for the SPI that will be called from the interrupt
	 * context when an SPI status occurs, specify a pointer to the SPI
	 * driver instance as the callback reference so the handler is able to
	 * access the instance data.
	 */
	XIsf_SetStatusHandler(&Isf, &Spi, (XSpi_StatusHandler)SpiHandler);

	/*
	 * Start the SPI driver so that interrupts and the device are enabled.
	 */
	XSpi_Start(&Spi);

	/*
	 * Initialize the Serial Flash Library.
	 */
	Status = XIsf_Initialize(&Isf, &Spi, ISF_SPI_SELECT, IsfWriteBuffer);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Set The transfer Mode to Interrupt
	 */
	XIsf_SetTransferMode(&Isf,XISF_INTERRUPT_MODE);

	/*
	 * Specify the address in the Serial Flash for the Erase/Write/Read
	 * operations.
	 */
	Address = ISF_TEST_ADDRESS;

	/*
	 * Check if the flash part is micron or spansion in which case,
	 * switch to 4 byte addressing mode if FlashSize is >128Mb.
	 */
	if (((Isf.ManufacturerID == XISF_MANUFACTURER_ID_MICRON) ||
				(Isf.ManufacturerID == XISF_MANUFACTURER_ID_SPANSION)) &&
				(((u8)Isf.DeviceCode) > XISF_SPANSION_ID_BYTE2_128)) {
		TransferInProgress = TRUE;
		Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}

		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}

		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}

		TransferInProgress = TRUE;
		XIsf_MicronFlashEnter4BAddMode(&Isf);

		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}

		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
	}

	TransferInProgress = TRUE;
	Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}

	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	TransferInProgress = TRUE;
	Status = XIsf_Erase(&Isf, XISF_SECTOR_ERASE, Address);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}

	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	TransferInProgress = TRUE;
	Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}

	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Set the
	 * - Address within the Serial Flash where the data is to be written.
	 * - Number of bytes to be written to the Serial Flash.
	 * - Write Buffer which contains the data to be written to the Serial
	 *   Flash.
	 */
	WriteParam.Address = Address;
	WriteParam.NumBytes = ISF_PAGE_SIZE;
	WriteParam.WritePtr = WriteBuffer;

	/*
	 * Prepare the write buffer. Fill in the data need to be written into
	 * Serial Flash.
	 */
	for(Index = 0; Index < ISF_PAGE_SIZE; Index++) {
		WriteParam.WritePtr[Index] = Index + ISF_TEST_BYTE;
	}

	TransferInProgress = TRUE;
	Status = XIsf_Write(&Isf, XISF_WRITE, (void*) &WriteParam);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}

	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Set the
	 * - Address in the Serial Flash where the data is to be read from.
	 * - Number of bytes to be read from the Serial Flash.
	 * - Read Buffer to which the data is to be read.
	 */
	ReadParam.Address = Address;
	ReadParam.NumBytes = ISF_PAGE_SIZE;
	ReadParam.ReadPtr = ReadBuffer;

	TransferInProgress = TRUE;
	Status = XIsf_Read(&Isf, XISF_READ, (void*) &ReadParam);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}

	/*
	 * Compare the data read against the data Written.
	 */
	if (Isf.ManufacturerID == XISF_MANUFACTURER_ID_MICRON) {
		for(Index = 0; Index < ISF_PAGE_SIZE; Index++) {
			if(ReadBuffer[Index + XISF_CMD_SEND_EXTRA_BYTES_4BYTE_MODE] !=
						(u8)(Index + ISF_TEST_BYTE)) {
				return XST_FAILURE;
			}
		}
	} else {
		for(Index = 0; Index < ISF_PAGE_SIZE; Index++) {
			if(ReadBuffer[Index + XISF_CMD_SEND_EXTRA_BYTES] !=
						(u8)(Index + ISF_TEST_BYTE)) {
				return XST_FAILURE;
			}
		}
	}

	if (((Isf.ManufacturerID == XISF_MANUFACTURER_ID_MICRON) ||
				(Isf.ManufacturerID == XISF_MANUFACTURER_ID_SPANSION)) &&
				(((u8)Isf.DeviceCode) > XISF_SPANSION_ID_BYTE2_128)) {
		TransferInProgress = TRUE;
		Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}

		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}

		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}

		TransferInProgress = TRUE;
		XIsf_MicronFlashExit4BAddMode(&Isf);

		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}

		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
	}

	return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* Function to execute the STM Serial Flash Read/Write example.
*
* @param	None
*
* @return	XST_SUCCESS if successful else XST_FAILURE.
*
* @note		None
*
******************************************************************************/
Function to execute the STM Serial Flash Read/Write example.
Function to execute the STM Serial Flash Read / Write example .
static int IsfStmFlashExample()
{
	int Status = XST_FAILURE;
	u32 Index;
	u32 Address;
	XIsf_WriteParam WriteParam;
	XIsf_ReadParam ReadParam;
	Status = XSpi_Initialize(&Spi, SPI_DEVICE_ID);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	Status = SetupInterruptSystem(&Spi);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XIsf_SetStatusHandler(&Isf, &Spi, (XSpi_StatusHandler)SpiHandler);
	XSpi_Start(&Spi);
	Status = XIsf_Initialize(&Isf, &Spi, ISF_SPI_SELECT, IsfWriteBuffer);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XIsf_SetTransferMode(&Isf,XISF_INTERRUPT_MODE);
	Address = ISF_TEST_ADDRESS;
	if (((Isf.ManufacturerID == XISF_MANUFACTURER_ID_MICRON) ||
				(Isf.ManufacturerID == XISF_MANUFACTURER_ID_SPANSION)) &&
				(((u8)Isf.DeviceCode) > XISF_SPANSION_ID_BYTE2_128)) {
		TransferInProgress = TRUE;
		Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}
		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
		TransferInProgress = TRUE;
		XIsf_MicronFlashEnter4BAddMode(&Isf);
		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}
		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
	}
	TransferInProgress = TRUE;
	Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}
	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	TransferInProgress = TRUE;
	Status = XIsf_Erase(&Isf, XISF_SECTOR_ERASE, Address);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}
	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	TransferInProgress = TRUE;
	Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}
	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	WriteParam.Address = Address;
	WriteParam.NumBytes = ISF_PAGE_SIZE;
	WriteParam.WritePtr = WriteBuffer;
	for(Index = 0; Index < ISF_PAGE_SIZE; Index++) {
		WriteParam.WritePtr[Index] = Index + ISF_TEST_BYTE;
	}
	TransferInProgress = TRUE;
	Status = XIsf_Write(&Isf, XISF_WRITE, (void*) &WriteParam);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}
	Status = IsfWaitForFlashNotBusy();
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	ReadParam.Address = Address;
	ReadParam.NumBytes = ISF_PAGE_SIZE;
	ReadParam.ReadPtr = ReadBuffer;
	TransferInProgress = TRUE;
	Status = XIsf_Read(&Isf, XISF_READ, (void*) &ReadParam);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	while(TransferInProgress);
	if(ErrorCount != 0) {
		return XST_FAILURE;
	}
	if (Isf.ManufacturerID == XISF_MANUFACTURER_ID_MICRON) {
		for(Index = 0; Index < ISF_PAGE_SIZE; Index++) {
			if(ReadBuffer[Index + XISF_CMD_SEND_EXTRA_BYTES_4BYTE_MODE] !=
						(u8)(Index + ISF_TEST_BYTE)) {
				return XST_FAILURE;
			}
		}
	} else {
		for(Index = 0; Index < ISF_PAGE_SIZE; Index++) {
			if(ReadBuffer[Index + XISF_CMD_SEND_EXTRA_BYTES] !=
						(u8)(Index + ISF_TEST_BYTE)) {
				return XST_FAILURE;
			}
		}
	}
	if (((Isf.ManufacturerID == XISF_MANUFACTURER_ID_MICRON) ||
				(Isf.ManufacturerID == XISF_MANUFACTURER_ID_SPANSION)) &&
				(((u8)Isf.DeviceCode) > XISF_SPANSION_ID_BYTE2_128)) {
		TransferInProgress = TRUE;
		Status = XIsf_WriteEnable(&Isf, XISF_WRITE_ENABLE);
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}
		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
		TransferInProgress = TRUE;
		XIsf_MicronFlashExit4BAddMode(&Isf);
		while(TransferInProgress);
		if(ErrorCount != 0) {
			return XST_FAILURE;
		}
		Status = IsfWaitForFlashNotBusy();
		if(Status != XST_SUCCESS) {
			return XST_FAILURE;
		}
	}
	return XST_SUCCESS;
}
static int IsfStmFlashExample ( ) { int Status = XST_FAILURE ; u32 Index ; u32 Address ; XIsf_WriteParam WriteParam ; XIsf_ReadParam ReadParam ; Status = XSpi_Initialize ( & Spi , SPI_DEVICE_ID ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } Status = SetupInterruptSystem ( & Spi ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } XIsf_SetStatusHandler ( & Isf , & Spi , ( XSpi_StatusHandler ) SpiHandler ) ; XSpi_Start ( & Spi ) ; Status = XIsf_Initialize ( & Isf , & Spi , ISF_SPI_SELECT , IsfWriteBuffer ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } XIsf_SetTransferMode ( & Isf , XISF_INTERRUPT_MODE ) ; Address = ISF_TEST_ADDRESS ; if ( ( ( Isf . ManufacturerID == XISF_MANUFACTURER_ID_MICRON ) || ( Isf . ManufacturerID == XISF_MANUFACTURER_ID_SPANSION ) ) && ( ( ( u8 ) Isf . DeviceCode ) > XISF_SPANSION_ID_BYTE2_128 ) ) { TransferInProgress = TRUE ; Status = XIsf_WriteEnable ( & Isf , XISF_WRITE_ENABLE ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } TransferInProgress = TRUE ; XIsf_MicronFlashEnter4BAddMode ( & Isf ) ; while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } } TransferInProgress = TRUE ; Status = XIsf_WriteEnable ( & Isf , XISF_WRITE_ENABLE ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } TransferInProgress = TRUE ; Status = XIsf_Erase ( & Isf , XISF_SECTOR_ERASE , Address ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } TransferInProgress = TRUE ; Status = XIsf_WriteEnable ( & Isf , XISF_WRITE_ENABLE ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } WriteParam . Address = Address ; WriteParam . NumBytes = ISF_PAGE_SIZE ; WriteParam . WritePtr = WriteBuffer ; for ( Index = 0 ; Index < ISF_PAGE_SIZE ; Index ++ ) { WriteParam . WritePtr [ Index ] = Index + ISF_TEST_BYTE ; } TransferInProgress = TRUE ; Status = XIsf_Write ( & Isf , XISF_WRITE , ( void * ) & WriteParam ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } ReadParam . Address = Address ; ReadParam . NumBytes = ISF_PAGE_SIZE ; ReadParam . ReadPtr = ReadBuffer ; TransferInProgress = TRUE ; Status = XIsf_Read ( & Isf , XISF_READ , ( void * ) & ReadParam ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } if ( Isf . ManufacturerID == XISF_MANUFACTURER_ID_MICRON ) { for ( Index = 0 ; Index < ISF_PAGE_SIZE ; Index ++ ) { if ( ReadBuffer [ Index + XISF_CMD_SEND_EXTRA_BYTES_4BYTE_MODE ] != ( u8 ) ( Index + ISF_TEST_BYTE ) ) { return XST_FAILURE ; } } } else { for ( Index = 0 ; Index < ISF_PAGE_SIZE ; Index ++ ) { if ( ReadBuffer [ Index + XISF_CMD_SEND_EXTRA_BYTES ] != ( u8 ) ( Index + ISF_TEST_BYTE ) ) { return XST_FAILURE ; } } } if ( ( ( Isf . ManufacturerID == XISF_MANUFACTURER_ID_MICRON ) || ( Isf . ManufacturerID == XISF_MANUFACTURER_ID_SPANSION ) ) && ( ( ( u8 ) Isf . DeviceCode ) > XISF_SPANSION_ID_BYTE2_128 ) ) { TransferInProgress = TRUE ; Status = XIsf_WriteEnable ( & Isf , XISF_WRITE_ENABLE ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } TransferInProgress = TRUE ; XIsf_MicronFlashExit4BAddMode ( & Isf ) ; while ( TransferInProgress ) ; if ( ErrorCount != 0 ) { return XST_FAILURE ; } Status = IsfWaitForFlashNotBusy ( ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } } return XST_SUCCESS ; }
Function to execute the STM Serial Flash Read/Write example.
Function to execute the STM Serial Flash Read / Write example .
/*
	 * Initialize the SPI driver so that it's ready to use,
	 * specify the device ID that is generated in xparameters.h.
	 */ /*
	 * Connect the SPI driver to the interrupt subsystem such that
	 * interrupts can occur. This function is application specific.
	 */ /*
	 * Setup the handler for the SPI that will be called from the interrupt
	 * context when an SPI status occurs, specify a pointer to the SPI
	 * driver instance as the callback reference so the handler is able to
	 * access the instance data.
	 */ /*
	 * Start the SPI driver so that interrupts and the device are enabled.
	 */ /*
	 * Initialize the Serial Flash Library.
	 */ /*
	 * Set The transfer Mode to Interrupt
	 */ /*
	 * Specify the address in the Serial Flash for the Erase/Write/Read
	 * operations.
	 */ /*
	 * Check if the flash part is micron or spansion in which case,
	 * switch to 4 byte addressing mode if FlashSize is >128Mb.
	 */ /*
	 * Set the
	 * - Address within the Serial Flash where the data is to be written.
	 * - Number of bytes to be written to the Serial Flash.
	 * - Write Buffer which contains the data to be written to the Serial
	 *   Flash.
	 */ /*
	 * Prepare the write buffer. Fill in the data need to be written into
	 * Serial Flash.
	 */ /*
	 * Set the
	 * - Address in the Serial Flash where the data is to be read from.
	 * - Number of bytes to be read from the Serial Flash.
	 * - Read Buffer to which the data is to be read.
	 */ /*
	 * Compare the data read against the data Written.
	 */