Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/AD9467-FMC-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 <xparameters.h>
48 #ifdef _XPARAMETERS_PS_H_
49 #include <xgpiops.h>
50 #include <xspips.h>
51 #include <sleep.h>
52 #else
53 #include <xspi.h>
54 static inline void usleep(unsigned long usleep)
55 {
56 unsigned long delay = 0;
57
58 for(delay = 0; delay < usleep * 10; delay++);
59 }
60 #endif
61 #include "spi.h"
62
63 /*****************************************************************************/
64 /************************ Variables Definitions ******************************/
65 /*****************************************************************************/
66 #ifdef _XPARAMETERS_PS_H_
67 XSpiPs_Config *spi_config;
68 XSpiPs spi_instance;
69 #endif
70
71 /***************************************************************************//**
72 * @brief mdelay
73 *******************************************************************************/
74 void mdelay(uint32_t msecs)
75 {
76 usleep(msecs * 1000);
77 }
78
79 /***************************************************************************//**
80 * @brief spi_init
81 *******************************************************************************/
82 int32_t spi_init(uint32_t device_id,
83 uint8_t clk_pha,
84 uint8_t clk_pol)
85 {
86 #ifdef _XPARAMETERS_PS_H_
87 uint8_t byte = 0;
88 uint32_t base_addr = 0;
89 uint32_t control_val = 0;
90
91 spi_config = XSpiPs_LookupConfig(device_id);
92 base_addr = spi_config->BaseAddress;
93 XSpiPs_CfgInitialize(&spi_instance, spi_config, base_addr);
94 control_val = XSPIPS_CR_SSFORCE_MASK |
95 XSPIPS_CR_SSCTRL_MASK |
96 4 << XSPIPS_CR_PRESC_SHIFT |
97 (clk_pha ? XSPIPS_CR_CPHA_MASK : 0) |
98 (clk_pol ? XSPIPS_CR_CPOL_MASK : 0) |
99 XSPIPS_CR_MSTREN_MASK;
100 XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET, control_val);
101 for(byte = 0; byte < 128; byte++)
102 {
103 XSpiPs_ReadReg(base_addr, XSPIPS_RXD_OFFSET);
104 }
105 #endif
106
107 return 0;
108 }
109
110 /***************************************************************************//**
111 * @brief spi_write_and_read
112 *******************************************************************************/
113 int32_t spi_write_and_read(uint8_t ss, uint8_t *data,
114 uint8_t bytes_number)
115 {
116 uint32_t cnt = 0;
117 #ifdef _XPARAMETERS_PS_H_
118 uint32_t base_addr = 0;
119 uint32_t control_val = 0;
120 uint32_t status = 0;
121
122 ss = (1 << ss);
123 base_addr = spi_config->BaseAddress;
124 control_val = XSpiPs_ReadReg(base_addr, XSPIPS_CR_OFFSET);
125
126 XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET,
127 control_val & ~(ss << XSPIPS_CR_SSCTRL_SHIFT));
128
129 XSpiPs_WriteReg(base_addr, XSPIPS_TXWR_OFFSET, 0x01);
130
131 XSpiPs_WriteReg(base_addr, XSPIPS_SR_OFFSET, XSPIPS_IXR_TXOW_MASK);
132 XSpiPs_WriteReg(base_addr, XSPIPS_IER_OFFSET, XSPIPS_IXR_TXOW_MASK);
133
134 while(cnt < bytes_number)
135 {
136 XSpiPs_WriteReg(base_addr, XSPIPS_TXD_OFFSET, data[cnt]);
137 cnt++;
138 }
139
140 XSpiPs_WriteReg(base_addr, XSPIPS_ER_OFFSET, XSPIPS_ER_ENABLE_MASK);
141
142 do
143 {
144 status = XSpiPs_ReadReg(base_addr, XSPIPS_SR_OFFSET);
145 }
146 while((status & XSPIPS_IXR_TXOW_MASK) == 0x0);
147
148 XSpiPs_WriteReg(base_addr, XSPIPS_SR_OFFSET, XSPIPS_IXR_TXOW_MASK);
149
150 XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET, control_val);
151
152 cnt = 0;
153 while(cnt < bytes_number)
154 {
155 data[cnt] = XSpiPs_ReadReg(base_addr, XSPIPS_RXD_OFFSET);
156 cnt++;
157 }
158
159 XSpiPs_WriteReg(base_addr, XSPIPS_ER_OFFSET, 0x0);
160 #else
161 uint8_t sel[2] = {0xE, 0xD};
162
163 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), 0xfff);
164 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), 0x0e6);
165 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), sel[ss]);
166
167 for(cnt = 0; cnt < bytes_number; cnt++)
168 {
169 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x68), (data[cnt] & 0xff));
170 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), 0x086);
171 do {mdelay(1);}
172 while ((Xil_In32((XPAR_SPI_0_BASEADDR + 0x64)) & 0x4) == 0x0);
173 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), 0x086);
174 data[cnt] = (Xil_In32(XPAR_SPI_0_BASEADDR + 0x6c) & 0xff);
175 }
176
177 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), 0xfff);
178 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), 0x080);
179 #endif
180
181 return 0;
182 }