Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/Common/sw/spi_ps7.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file spi_ps7.c
3 * @brief ZYNQ Hardware SPI functions implementation.
4 * @author ATofan ([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 "system_config.h"
48 #include "xparameters.h"
49 #include "xil_io.h"
50
51 #if(USE_PS7 == 1)
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 typedef struct _stHwSpiConfig
58 {
59 u32 hwBaseAddr;
60 u32 config;
61 }stHwSpiConfig;
62
63 static stHwSpiConfig hwSpiConfig[2] = {{0, 0}, {0, 0}};
64 static char emptyTxBuf[MAX_TX_SIZE];
65
66 /**************************************************************************//**
67 * @brief Initializes the communication with the Microblaze SPI peripheral.
68 *
69 * @param hwBaseAddr - Microblaze SPI peripheral AXI base address.
70 * @param lsbFirst - Set to 1 if the data is transmitted LSB first.
71 * @param cpha - Set to 1 if CPHA mode is used.
72 * @param cpol - Set to 1 if CPOL mode is used.
73 * @return TRUE
74 ******************************************************************************/
75 u32 SPI_Init_ps7(u32 hwBaseAddr, char lsbFirst, char cpha, char cpol)
76 {
77 u32 i = 0;
78 u32 cfgValue = 0;
79
80 //set the empty Tx buffer to 0
81 for(i = 0; i < MAX_TX_SIZE; i++)
82 {
83 emptyTxBuf[i] = 0;
84 }
85
86 // Configuration Register Settings
87 cfgValue |= (1 << Modefail_gen_en) |
88 (0 << Man_start_com) |
89 (0 << Man_start_en) |
90 (1 << Manual_CS) |
91 (0x0F << CS_Bits) |
92 (0 << PERI_SEL) |
93 (0 << SPI_REF_CLK) |
94 (4 << SPI_BAUD_RATE_DIV) |
95 (cpha << SPI_CLK_PH) |
96 (cpol << SPI_CLK_POL) |
97 (1 << SPI_MODE_SEL);
98
99 //initialize the SPI configuration
100 for(i = 0; i < sizeof(hwSpiConfig) / sizeof(stHwSpiConfig); i++)
101 {
102 if(hwSpiConfig[i].hwBaseAddr == 0)
103 {
104 hwSpiConfig[i].hwBaseAddr = hwBaseAddr;
105 hwSpiConfig[i].config = cfgValue;
106 break;
107 }
108 }
109
110 Xil_Out32(hwBaseAddr + HW_SPI_CONFIG_REG, cfgValue);
111
112 // Clear RX FIFO
113 for(i = 0; i < 128; i++)
114 {
115 Xil_In32(hwBaseAddr + HW_SPI_RXDATA_REG);
116 }
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_ps7(u32 hwBaseAddr, 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 flushCnt = 0;
140 u32 flush = 0;
141
142 // Get the configuration of the SPI core
143 for(i = 0; i < sizeof(hwSpiConfig) / sizeof(stHwSpiConfig); i++)
144 {
145 if(hwSpiConfig[i].hwBaseAddr == hwBaseAddr)
146 {
147 cfgValue = hwSpiConfig[i].config;
148 break;
149 }
150 }
151
152 // Check if the transmit buffer is empty
153 if(txSize == 0)
154 {
155 txSize = rxSize;
156 txBuf = emptyTxBuf;
157 }
158
159 // Write configuration data to Master SPI Device
160 Xil_Out32(hwBaseAddr + HW_SPI_CONFIG_REG, cfgValue);
161
162 // Write configuration data to manually assert CS
163 Xil_Out32(hwBaseAddr + HW_SPI_CONFIG_REG, cfgValue & ~(1 << CS_Bits));
164
165 // Write TX FIFO Not Full Level
166 Xil_Out32(hwBaseAddr + HW_SPI_TX_THRES_REG, 0x01);
167
168 // Enable TX FIFO Not Full Interrupts
169 Xil_Out32(hwBaseAddr + HW_SPI_INTR_STS_REG, (1 << SPI_TX_FIFO_not_full));
170 Xil_Out32(hwBaseAddr + HW_SPI_INTR_EN_REG, (1 << SPI_TX_FIFO_not_full));
171
172 // Write initial data to TX FIFO
173 Xil_Out32(hwBaseAddr + HW_SPI_TXDATA_REG, txBuf[txCnt]);
174 txCnt++;
175
176 // Write rest of the data to TX FIFO
177 while(txCnt < txSize)
178 {
179 Xil_Out32(hwBaseAddr + HW_SPI_TXDATA_REG, txBuf[txCnt]);
180 txCnt++;
181 }
182
183 // Enable Controller
184 Xil_Out32(hwBaseAddr + HW_SPI_EN_REG, 0x01);
185
186 // Wait for TX FIFO to empty
187 do
188 {
189 SPIStatus = Xil_In32(hwBaseAddr + HW_SPI_INTR_STS_REG);
190 }
191 while((SPIStatus & (1 << 2)) == 0x00);
192
193 // Clear Interrupt
194 Xil_Out32(hwBaseAddr + HW_SPI_INTR_STS_REG, (1 << 2));
195
196 // Deassert CS
197 Xil_Out32(hwBaseAddr + HW_SPI_CONFIG_REG, cfgValue);
198
199 while(rxCnt < rxSize)
200 {
201 rxBuf[rxCnt] = Xil_In32(hwBaseAddr + HW_SPI_RXDATA_REG);
202 rxCnt++;
203 }
204
205 while(flushCnt < txSize - rxSize)
206 {
207 flush = Xil_In32(hwBaseAddr + HW_SPI_RXDATA_REG);
208 flushCnt++;
209 }
210
211 // Disable SPI
212 Xil_Out32(hwBaseAddr + HW_SPI_EN_REG, 0x00);
213
214 return(TRUE);
215 }
216 #endif