/****************************************************************************** * * 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_slave_intr_example.c * * * This file contains a design example using the Spi driver (XSpi) and the Spi * device as a Slave, in interrupt mode. * * This example fills the Spi Tx buffer with the number of data bytes it expects * to receive from the master and then Spi device waits for an external master to * initiate the transfer. When the master initiates the transfer, the Spi device * receives data from the master and simultaneously sends the data in Tx buffer * to the master. Once the transfer is complete, a transfer complete interrupt is * generated and this example prints the data received from the master. The * number of bytes to be received by the Spi slave is defined by the constant * BUFFER_SIZE in this file. * * The external SPI devices that are present on the Xilinx boards don't support * the Master functionality. This example has been tested with Aardvark I2C/SPI * Host Adapter, an off board external SPI Master device and the Xilinx SPI * device configured as a Slave. This example has been tested for byte-wide SPI * transfers. * * @note * * This example assumes that there is a STDIO device in the system. * *
* MODIFICATION HISTORY: * * Ver Who Date Changes * ----- ---- -------- --------------------------------------------------------- * 1.00a psk 09/05/08 First Release * 3.00a ktn 10/22/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.01a sdm 04/23/10 Enabled DTR Half_empty interrupt so that Tx FIFO is * not empty during a transfer in slave mode. * *******************************************************************************/ /***************************** Include Files *********************************/ #include "xparameters.h" /* XPAR parameters */ #include "xspi.h" /* SPI device driver */ #include "xintc.h" /* Interrupt controller devive driver */ #include "stdio.h" #include "xil_exception.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 #define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID #define SPI_IRPT_INTR XPAR_INTC_0_SPI_0_VEC_ID /* * This is the size of the buffer to be transmitted/received in this example. */ #define BUFFER_SIZE 32 /**************************** Type Definitions *******************************/ /***************** Macros (Inline Functions) Definitions *********************/ /************************** Function Prototypes ******************************/ static int SpiSlaveIntrExample(XSpi *SpiInstancePtr, u16 SpiDeviceId); static int SetupInterruptSystem(XSpi *SpiInstance); static void SpiHandler(void *CallBackRef, u32 StatusEvent, unsigned int ByteCount); /************************** Variable Definitions *****************************/ /* * The instances to support the device drivers are global such that they are * initialized to zero each time the program runs. They could be local but * should at least be static so that they are zeroed. */ static XSpi SpiInstance; /* Instance of the SPI device */ static XIntc IntcInstance; /* Instance of the Interrupt controller device */ /* * The following variables are used to read/write from the Spi device, these * are global to avoid having large buffers on the stack. */ u8 ReadBuffer[BUFFER_SIZE]; u8 WriteBuffer[BUFFER_SIZE]; /* * The following variable allows a test value to be added to the values that * are sent in reflection to the Master transmission such that unique values can * be generated to guarantee the transfer from Slave to Master is successful. */ int Test; /* * The following variables are shared between non-interrupt processing and * interrupt processing such that they must be global. */ static volatile int TransferInProgress; /*****************************************************************************/ /** * * Main function to call the Spi Slave example in interrupt mode. * * @param None * * @return XST_SUCCESS if successful, otherwise XST_FAILURE. * * @note None * ******************************************************************************/ int main(void) { int Status; /* * Run the Spi Slave interrupt example. */ Status = SpiSlaveIntrExample(&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 the device slave * functionality in interrupt mode. This function receives data from a master and * prints the received 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_