1 | /* $Id: xspi_slave_polled_example.c,v 1.1.2.1 2011/08/09 06:59:27 svemula Exp $ */
|
2 |
|
3 | ******************************************************************************/
|
4 | /*****************************************************************************/
|
5 | /**
|
6 | * @file xspi_slave_polled_example.c
|
7 | *
|
8 | * This file contains a design example using the Spi driver (XSpi) and the SPI
|
9 | * device as a Slave, in polled mode.
|
10 | *
|
11 | * This example fills the Spi Tx buffer with the number of data bytes it expects
|
12 | * to receive from the master and then Spi device waits for an external master to
|
13 | * initiate the transfer. When the master initiates the transfer, the Spi device
|
14 | * receives data from the master and simultaneously sends the data in Tx buffer
|
15 | * to the master. Once the transfer is complete, this example prints the data
|
16 | * received from the master. The number of bytes to be received by the Spi slave
|
17 | * is defined by the constant BUFFER_SIZE in this file.
|
18 | *
|
19 | * The external SPI devices that are present on the Xilinx boards don't support
|
20 | * the Master functionality. This example has been tested with an off board
|
21 | * external SPI Master device and the Xilinx SPI device configured as a Slave.
|
22 | * This example has been tested for byte-wide SPI transfers.
|
23 | *
|
24 | * @note
|
25 | *
|
26 | * This example assumes that there is a STDIO device in the system.
|
27 | *
|
28 | *<pre>
|
29 | * MODIFICATION HISTORY:
|
30 | *
|
31 | * Ver Who Date Changes
|
32 | * ----- ---- -------- ---------------------------------------------------------
|
33 | * 1.00a psk 09/05/08 First Release
|
34 | * 3.00a ktn 10/28/09 Converted all register accesses to 32 bit access.
|
35 | * Updated to use the HAL APIs/macros. Replaced call to
|
36 | * XSpi_Initialize API with XSpi_LookupConfig and
|
37 | * XSpi_CfgInitialize.
|
38 | *
|
39 | *</pre>
|
40 | ******************************************************************************/
|
41 |
|
42 | /***************************** Include Files *********************************/
|
43 |
|
44 | #include "xparameters.h" /* XPAR parameters */
|
45 | #include "xspi.h" /* SPI device driver */
|
46 | #include "stdio.h"
|
47 |
|
48 | /************************** Constant Definitions *****************************/
|
49 |
|
50 | /*
|
51 | * The following constants map to the XPAR parameters created in the
|
52 | * xparameters.h file. They are defined here such that a user can easily
|
53 | * change all the needed parameters in one place.
|
54 | */
|
55 | #define SPI_DEVICE_ID XPAR_SPI_0_DEVICE_ID
|
56 |
|
57 | /*
|
58 | * This is the size of the buffer to be transmitted/received in this example.
|
59 | */
|
60 | #define BUFFER_SIZE 16
|
61 |
|
62 | /**************************** Type Definitions *******************************/
|
63 |
|
64 |
|
65 | /***************** Macros (Inline Functions) Definitions *********************/
|
66 |
|
67 |
|
68 | /************************** Function Prototypes ******************************/
|
69 |
|
70 | static int SpiSlavePolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId);
|
71 |
|
72 | /************************** Variable Definitions *****************************/
|
73 |
|
74 | /*
|
75 | * The instances to support the device drivers are global such that they are
|
76 | * initialized to zero each time the program runs. They could be local but
|
77 | * should at least be static so that they are zeroed.
|
78 | */
|
79 | static XSpi SpiInstance; /* The instance of the SPI device */
|
80 |
|
81 | /*
|
82 | * The following variables are used to read from the Spi device, these are
|
83 | * global to avoid having large buffers on the stack.
|
84 | */
|
85 | u8 ReadBuffer[BUFFER_SIZE];
|
86 | u8 WriteBuffer[BUFFER_SIZE];
|
87 | u16 ReadBuffer1;
|
88 |
|
89 | /*
|
90 | * The following variable allows a test value to be added to the values that
|
91 | * are sent in reflection to the Master transmission such that unique values can
|
92 | * be generated to guarantee the transfer from Slave to Master is successful.
|
93 | */
|
94 | int Test;
|
95 |
|
96 | /*****************************************************************************/
|
97 | /**
|
98 | *
|
99 | * Main function to call the Spi Slave example in polled mode.
|
100 | *
|
101 | * @param None
|
102 | *
|
103 | * @return XST_SUCCESS if successful, otherwise XST_FAILURE.
|
104 | *
|
105 | * @note None
|
106 | *
|
107 | ******************************************************************************/
|
108 | int main(void)
|
109 | {
|
110 | int Status;
|
111 |
|
112 | /*
|
113 | * Run the Spi Slave polled example.
|
114 | */
|
115 |
|
116 | Status = SpiSlavePolledExample(&SpiInstance, SPI_DEVICE_ID);
|
117 | if (Status != XST_SUCCESS) {
|
118 | return XST_FAILURE;
|
119 |
|
120 |
|
121 | }
|
122 | return XST_SUCCESS;
|
123 | }
|
124 |
|
125 | /*****************************************************************************/
|
126 | /**
|
127 | *
|
128 | * This function does a minimal test on the Spi device and driver as a design
|
129 | * example. The purpose of this function is to illustrate the device slave
|
130 | * functionality in polled mode. This function receives data from a master and
|
131 | * prints the received data.
|
132 | *
|
133 | * @param SpiInstancePtr is a pointer to the instance of Spi component.
|
134 | *
|
135 | * @param SpiDeviceId is the Device ID of the Spi Device and is the
|
136 | * XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
|
137 | *
|
138 | * @return XST_SUCCESS if successful, otherwise XST_FAILURE.
|
139 | *
|
140 | * @note This function contains an infinite loop such that if the Spi
|
141 | * device doesn't receive any data, it may never return.
|
142 | *
|
143 | ******************************************************************************/
|
144 | int SpiSlavePolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
|
145 | {
|
146 | XSpi_Config *ConfigPtr;
|
147 | int Status;
|
148 | u32 Count;
|
149 |
|
150 | xil_printf("\r\nEntering the Spi Slave Polled Example.\r\n");
|
151 | xil_printf("Waiting for data from SPI master\r\n");
|
152 |
|
153 | /*
|
154 | * Initialize the SPI driver so that it's ready to use, specify the
|
155 | * device ID that is generated in xparameters.h.
|
156 | */
|
157 | ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
|
158 | if (ConfigPtr == NULL) {
|
159 | return XST_FAILURE;
|
160 | }
|
161 | Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
|
162 | ConfigPtr->BaseAddress);
|
163 | if (Status != XST_SUCCESS) {
|
164 | return XST_FAILURE;
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | /*
|
171 | * The SPI device is a slave by default and the clock phase and polarity
|
172 | * have to be set according to its master. In this example, CPOL is set
|
173 | * to active low and CPHA is set to 1.
|
174 | */
|
175 | Status = XSpi_SetOptions(SpiInstancePtr, XSP_CLK_PHASE_1_OPTION |
|
176 | XSP_CLK_ACTIVE_LOW_OPTION);
|
177 | if (Status != XST_SUCCESS) {
|
178 | return XST_FAILURE;
|
179 | }
|
180 |
|
181 | /*
|
182 | * Start the SPI driver so that the device is enabled.
|
183 | */
|
184 | XSpi_Start(SpiInstancePtr);
|
185 |
|
186 | /*
|
187 | * Disable Global interrupt to use polled mode operation.
|
188 | */
|
189 | XSpi_IntrGlobalDisable(SpiInstancePtr);
|
190 |
|
191 |
|
192 | /*
|
193 | * Prepare the data buffers for transmission and to send/receive data
|
194 | * when the SPI device is selected by a master.
|
195 | */
|
196 | while(1)
|
197 | {
|
198 | XSpi_Transfer(SpiInstancePtr, 0xF012, ReadBuffer1, 2);
|
199 | xil_printf("0x%x \r\n", ReadBuffer1);
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 | return XST_SUCCESS;
|
205 | }
|