Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/AD6673-EBZ/spi.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file spi.c
3 * @brief SPI functions implementations.
4 * @author acozma ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2011(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************
41 * SVN Revision: $WCREV$
42 ******************************************************************************/
43
44 /*****************************************************************************/
45 /***************************** Include Files *********************************/
46 /*****************************************************************************/
47 #include "spi.h"
48 #include "xil_io.h"
49
50 extern void delay_ms(u32 ms_count);
51
52 /*****************************************************************************/
53 /************************ Constants Definitions ******************************/
54 /*****************************************************************************/
55 #define MAX_TX_SIZE 16 /*!< Maximum number of bytes that can be transmitted in one SPI transfer */
56
57 /*****************************************************************************/
58 /************************ Variables Definitions ******************************/
59 /*****************************************************************************/
60 typedef struct _stSpiConfig
61 {
62 u32 axiBaseAddr; /*!< Base address of the SPI Microblaze IP */
63 u32 config; /*!< Configuration of the SPI Microblaze IP */
64 }stSpiConfig;
65
66 static stSpiConfig spiConfig[2] = {{0,0}, {0,0}};
67 static char emptyTxBuf[MAX_TX_SIZE];
68
69 /**************************************************************************//**
70 * @brief Initializes the communication with the Microblaze SPI peripheral.
71 *
72 * @param axiBaseAddr - Microblaze SPI peripheral AXI base address.
73 * @param lsbFirst - Set to 1 if the data is transmitted LSB first.
74 * @param cpha - Set to 1 if CPHA mode is used.
75 * @param cpol - Set to 1 if CPOL mode is used.
76 * @return TRUE
77 ******************************************************************************/
78 u32 SPI_Init(u32 axiBaseAddr, char lsbFirst, char cpha, char cpol)
79 {
80 u32 i = 0;
81 u32 cfgValue = 0;
82
83 //set the empty Tx buffer to 0
84 for(i = 0; i < MAX_TX_SIZE; i++)
85 {
86 emptyTxBuf[i] = 0;
87 }
88
89 // Configuration Register Settings
90 cfgValue |= (lsbFirst << LSBFirst) | // MSB First transfer format
91 (1 << MasterTranInh) | // Master transactions disabled
92 (1 << ManualSlaveAssEn) | // Slave select output follows data in slave select register
93 (1 << RxFifoReset) | // Receive FIFO normal operation
94 (0 << TxFifoReset) | // Transmit FIFO normal operation
95 (cpha << CHPA) | // Data valid on first SCK edge
96 (cpol << CPOL) | // Active high clock, SCK idles low
97 (1 << Master) | // SPI in Master configuration mode
98 (1 << SPE) | // SPI enabled
99 (0 << LOOP); // Normal operation
100
101 //initialize the SPI configuration
102 for(i = 0; i < sizeof(spiConfig) / sizeof(stSpiConfig); i++)
103 {
104 if(spiConfig[i].axiBaseAddr == 0)
105 {
106 spiConfig[i].axiBaseAddr = axiBaseAddr;
107 spiConfig[i].config = cfgValue;
108 break;
109 }
110 }
111
112 //set the slave select register to all ones
113 Xil_Out32(axiBaseAddr + SPISSR, 0xFFFFFFFF);
114
115 // Set corresponding value to the Configuration Register
116 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
117
118 return TRUE;
119 }
120
121 /**************************************************************************//**
122 * @brief Transfers data to and from a SPI slave.
123 *
124 * @param axiBaseAddr - Microblaze SPI peripheral AXI base address.
125 * @param txSize - Number of bytes to transmit to the SPI slave.
126 * @param txBuffer - Buffer which holds the data to be transmitted to the SPI slave.
127 * @param rxSize - Number of bytes to receive from the SPI slave.
128 * @param txBuffer - Buffer to store the data read to the SPI slave.
129 * @param ssNo - Slave select line on which the slave is connected.
130 * @return TRUE.
131 ******************************************************************************/
132 u32 SPI_TransferData(u32 axiBaseAddr, char txSize, char* txBuf, char rxSize, char* rxBuf, char ssNo)
133 {
134 u32 i = 0;
135 u32 cfgValue = 0;
136 u32 SPIStatus = 0;
137 u32 rxCnt = 0;
138 u32 txCnt = 0;
139 u32 timeout = 0xFFFF;
140
141 // Get the configuration of the SPI core
142 for(i = 0; i < sizeof(spiConfig) / sizeof(stSpiConfig); i++)
143 {
144 if(spiConfig[i].axiBaseAddr == axiBaseAddr)
145 {
146 cfgValue = spiConfig[i].config;
147 break;
148 }
149 }
150
151 // Check if the transmit buffer is empty
152 if(txSize == 0)
153 {
154 txSize = rxSize;
155 txBuf = emptyTxBuf;
156 }
157
158 // Write configuration data to master SPI device SPICR
159 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
160
161 // Write to SPISSR to manually assert SSn
162 Xil_Out32(axiBaseAddr + SPISSR, ~(0x00000001 << (ssNo - 1)));
163
164 // Write initial data to master SPIDTR register
165 Xil_Out32(axiBaseAddr + SPIDTR, txBuf[0]);
166
167 // Enable the master transactions
168 cfgValue &= ~(1 << MasterTranInh);
169 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
170
171 // Send and receive the data
172 while(txCnt < txSize)
173 {
174 // Poll status for completion
175 do
176 {
177 SPIStatus = Xil_In32(axiBaseAddr + SPISR);
178 }
179 while(((SPIStatus & 0x01) == 1) && timeout--);
180 if(timeout == -1)
181 {
182 // Disable the master transactions
183 cfgValue |= (1 << MasterTranInh);
184 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
185
186 //reset the SPI core
187 Xil_Out32(axiBaseAddr + SRR, 0x0000000A);
188
189 //set the slave select register to all ones
190 Xil_Out32(axiBaseAddr + SPISSR, 0xFFFFFFFF);
191
192 // Set corresponding value to the Configuration Register
193 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
194
195 return FALSE;
196 }
197 timeout = 0xFFFF;
198
199 // Read received data from SPI Core buffer
200 if(rxCnt < rxSize)
201 {
202 rxBuf[rxCnt] = Xil_In32(axiBaseAddr + SPIDRR);
203 rxCnt++;
204 }
205 // Send next data
206 txCnt++;
207 if(txCnt < txSize)
208 {
209 // Disable the master transactions
210 cfgValue |= (1 << MasterTranInh);
211 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
212
213 // Write data
214 Xil_Out32(axiBaseAddr + SPIDTR, txBuf[txCnt]);
215
216 /// Enable the master transactions
217 cfgValue &= ~(1 << MasterTranInh);
218 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
219 }
220 }
221
222 // Disable the master transactions
223 cfgValue |= (1 << MasterTranInh);
224 Xil_Out32(axiBaseAddr + SPICR, cfgValue);
225
226 // Write all ones to SPISSR
227 Xil_Out32(axiBaseAddr + SPISSR, 0xFFFFFFFF);
228
229 delay_ms(1);
230
231 return TRUE;
232 }
233