251 lines
7.3 KiB
C
251 lines
7.3 KiB
C
![]() |
/******************************************************************************
|
||
|
*
|
||
|
* Copyright (C) 2008 - 2014 Xilinx, Inc. All rights reserved.
|
||
|
*
|
||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
* of this software and associated documentation files (the "Software"), to deal
|
||
|
* in the Software without restriction, including without limitation the rights
|
||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
* copies of the Software, and to permit persons to whom the Software is
|
||
|
* furnished to do so, subject to the following conditions:
|
||
|
*
|
||
|
* The above copyright notice and this permission notice shall be included in
|
||
|
* all copies or substantial portions of the Software.
|
||
|
*
|
||
|
* Use of the Software is limited solely to applications:
|
||
|
* (a) running on a Xilinx device, or
|
||
|
* (b) that interact with a Xilinx device through a bus or interconnect.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
* XILINX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||
|
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
* SOFTWARE.
|
||
|
*
|
||
|
* Except as contained in this notice, the name of the Xilinx shall not be used
|
||
|
* in advertising or otherwise to promote the sale, use or other dealings in
|
||
|
* this Software without prior written authorization from Xilinx.
|
||
|
*
|
||
|
******************************************************************************/
|
||
|
/*****************************************************************************/
|
||
|
/**
|
||
|
* @file xspi_polled_example.c
|
||
|
*
|
||
|
*
|
||
|
* This file contains a design example using the Spi driver (XSpi) and the Spi
|
||
|
* device using the polled mode.
|
||
|
*
|
||
|
* To put the driver in polled mode the Global Interrupt must be disabled after
|
||
|
* the Spi is Initialized and Spi driver is started.
|
||
|
*
|
||
|
* This example works with a PPC/MicroBlaze processor.
|
||
|
*
|
||
|
* @note
|
||
|
*
|
||
|
* None.
|
||
|
*
|
||
|
*<pre>
|
||
|
* MODIFICATION HISTORY:
|
||
|
*
|
||
|
* Ver Who Date Changes
|
||
|
* ----- ---- -------- ---------------------------------------------------------
|
||
|
* 1.00a sdm 03/03/08 First Release
|
||
|
* 3.00a ktn 10/28/09 Converted all register accesses to 32 bit access.
|
||
|
* Updated to use the HAL APIs/macros. Replaced call to
|
||
|
* XSpi_Initialize API with XSpi_LookupConfig and
|
||
|
* XSpi_CfgInitialize.
|
||
|
* 3.02a sdm 05/04/11 Updated to run the loopback test only in standard spi
|
||
|
* mode.
|
||
|
*</pre>
|
||
|
******************************************************************************/
|
||
|
|
||
|
/***************************** Include Files *********************************/
|
||
|
|
||
|
#include "xparameters.h" /* XPAR parameters */
|
||
|
#include "xspi.h" /* SPI device driver */
|
||
|
#include "xspi_l.h"
|
||
|
|
||
|
/************************** Constant Definitions *****************************/
|
||
|
|
||
|
/*
|
||
|
* The following constants map to the XPAR parameters created in the
|
||
|
* xparameters.h file. They are defined here such that a user can easily
|
||
|
* change all the needed parameters in one place.
|
||
|
*/
|
||
|
#define SPI_DEVICE_ID XPAR_SPI_0_DEVICE_ID
|
||
|
|
||
|
/*
|
||
|
* This is the size of the buffer to be transmitted/received in this example.
|
||
|
*/
|
||
|
#define BUFFER_SIZE 12
|
||
|
|
||
|
|
||
|
/**************************** Type Definitions *******************************/
|
||
|
|
||
|
/*
|
||
|
* The following data type is used to send and receive data on the SPI
|
||
|
* interface.
|
||
|
*/
|
||
|
typedef u8 DataBuffer[BUFFER_SIZE];
|
||
|
|
||
|
|
||
|
/***************** Macros (Inline Functions) Definitions *********************/
|
||
|
|
||
|
|
||
|
/************************** Function Prototypes ******************************/
|
||
|
|
||
|
int SpiPolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId);
|
||
|
|
||
|
/************************** Variable Definitions *****************************/
|
||
|
|
||
|
/*
|
||
|
* The instances to support the device drivers are global such that the
|
||
|
* are initialized to zero each time the program runs.
|
||
|
*/
|
||
|
static XSpi SpiInstance; /* The instance of the SPI device */
|
||
|
|
||
|
/*
|
||
|
* The following variables are used to read and write to the Spi device, they
|
||
|
* are global to avoid having large buffers on the stack.
|
||
|
*/
|
||
|
u8 ReadBuffer[BUFFER_SIZE];
|
||
|
u8 WriteBuffer[BUFFER_SIZE];
|
||
|
|
||
|
|
||
|
/*****************************************************************************/
|
||
|
/**
|
||
|
*
|
||
|
* Main function to call the Spi Polled example.
|
||
|
*
|
||
|
* @param None
|
||
|
*
|
||
|
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
|
||
|
*
|
||
|
* @note None
|
||
|
*
|
||
|
******************************************************************************/
|
||
|
int main(void)
|
||
|
{
|
||
|
int Status;
|
||
|
|
||
|
/*
|
||
|
* Run the Spi Polled example.
|
||
|
*/
|
||
|
Status = SpiPolledExample(&SpiInstance, SPI_DEVICE_ID);
|
||
|
if (Status != XST_SUCCESS) {
|
||
|
return XST_FAILURE;
|
||
|
}
|
||
|
|
||
|
return XST_SUCCESS;
|
||
|
}
|
||
|
|
||
|
/*****************************************************************************/
|
||
|
/**
|
||
|
*
|
||
|
* This function does a minimal test on the Spi device and driver as a
|
||
|
* design example. The purpose of this function is to illustrate how to use
|
||
|
* the XSpi component using the polled mode.
|
||
|
*
|
||
|
* This function sends data and expects to receive the same data.
|
||
|
*
|
||
|
*
|
||
|
* @param SpiInstancePtr is a pointer to the instance of Spi component.
|
||
|
* @param SpiDeviceId is the Device ID of the Spi Device and is the
|
||
|
* XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
|
||
|
*
|
||
|
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
|
||
|
*
|
||
|
* @note
|
||
|
*
|
||
|
* This function contains an infinite loop such that if the Spi device is not
|
||
|
* working it may never return.
|
||
|
*
|
||
|
******************************************************************************/
|
||
|
int SpiPolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
|
||
|
{
|
||
|
int Status;
|
||
|
u32 Count;
|
||
|
u8 Test;
|
||
|
XSpi_Config *ConfigPtr; /* Pointer to Configuration data */
|
||
|
|
||
|
/*
|
||
|
* Initialize the SPI driver so that it is ready to use.
|
||
|
*/
|
||
|
ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
|
||
|
if (ConfigPtr == NULL) {
|
||
|
return XST_DEVICE_NOT_FOUND;
|
||
|
}
|
||
|
|
||
|
Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
|
||
|
ConfigPtr->BaseAddress);
|
||
|
if (Status != XST_SUCCESS) {
|
||
|
return XST_FAILURE;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Perform a self-test to ensure that the hardware was built correctly.
|
||
|
*/
|
||
|
Status = XSpi_SelfTest(SpiInstancePtr);
|
||
|
if (Status != XST_SUCCESS) {
|
||
|
return XST_FAILURE;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Run loopback test only in case of standard SPI mode.
|
||
|
*/
|
||
|
if (SpiInstancePtr->SpiMode != XSP_STANDARD_MODE) {
|
||
|
return XST_SUCCESS;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Set the Spi device as a master and in loopback mode.
|
||
|
*/
|
||
|
Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION |
|
||
|
XSP_LOOPBACK_OPTION);
|
||
|
if (Status != XST_SUCCESS) {
|
||
|
return XST_FAILURE;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
* Start the SPI driver so that the device is enabled.
|
||
|
*/
|
||
|
XSpi_Start(SpiInstancePtr);
|
||
|
|
||
|
/*
|
||
|
* Disable Global interrupt to use polled mode operation
|
||
|
*/
|
||
|
XSpi_IntrGlobalDisable(SpiInstancePtr);
|
||
|
|
||
|
/*
|
||
|
* Initialize the write buffer with pattern to write, initialize the
|
||
|
* read buffer to zero so it can be verified after the read, the
|
||
|
* Test value that is added to the unique value allows the value to be
|
||
|
* changed in a debug environment.
|
||
|
*/
|
||
|
Test = 0x10;
|
||
|
for (Count = 0; Count < BUFFER_SIZE; Count++) {
|
||
|
WriteBuffer[Count] = (u8)(Count + Test);
|
||
|
ReadBuffer[Count] = 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
* Transmit the data.
|
||
|
*/
|
||
|
XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE);
|
||
|
|
||
|
/*
|
||
|
* Compare the data received with the data that was transmitted.
|
||
|
*/
|
||
|
for (Count = 0; Count < BUFFER_SIZE; Count++) {
|
||
|
if (WriteBuffer[Count] != ReadBuffer[Count]) {
|
||
|
return XST_FAILURE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return XST_SUCCESS;
|
||
|
}
|