Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/common_drivers/platform_drivers/platform_drivers.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file platform_drivers.c
3 * @brief Implementation of Platform Drivers.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2014-2015(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43
44 #include "platform_drivers.h"
45
46 /***************************************************************************//**
47 * @brief ad_spi_init
48 *******************************************************************************/
49 // if not using the altera/xilinx spi cores, you may overwrite this function
50
51 int32_t ad_spi_init(spi_device *dev)
52 {
53
54 #ifdef ZYNQ_PS7
55 dev->base_address = XPAR_PS7_SPI_0_BASEADDR;
56 dev->device_id = XPAR_PS7_SPI_0_DEVICE_ID;
57 #endif
58
59 #ifdef ZYNQ_PSU
60 dev->base_address = XPAR_PSU_SPI_0_BASEADDR;
61 dev->device_id = XPAR_PSU_SPI_0_DEVICE_ID;
62 #endif
63
64 #ifdef MICROBLAZE
65 #endif
66
67 #ifdef NIOS_II
68 #endif
69
70 dev->chip_select = 0xff;
71 dev->cpha = 0;
72 dev->cpol = 0;
73
74 return(0);
75 }
76
77 /***************************************************************************//**
78 * @brief ad_spi_xfer
79 *******************************************************************************/
80
81 int32_t ad_spi_xfer(spi_device *dev, uint8_t *data, uint8_t no_of_bytes)
82 {
83
84 #ifdef ZYNQ
85
86 uint32_t initss;
87
88 XSpiPs m_spi;
89 XSpiPs_Config *m_spi_config;
90
91 m_spi_config = XSpiPs_LookupConfig(dev->device_id);
92
93 if (m_spi_config == NULL) {
94 return(-1);
95 }
96
97 if (XSpiPs_CfgInitialize(&m_spi, m_spi_config, m_spi_config->BaseAddress) != 0) {
98 return(-1);
99 }
100
101 initss = XSpiPs_ReadReg(dev->base_address, XSPIPS_CR_OFFSET);
102 initss = initss & (uint32_t)(~XSPIPS_CR_SSCTRL_MASK);
103 initss = initss | (0x7 << XSPIPS_CR_SSCTRL_SHIFT);
104 XSpiPs_WriteReg(dev->base_address, XSPIPS_CR_OFFSET, initss);
105 XSpiPs_SetOptions(&m_spi, XSPIPS_MASTER_OPTION |
106 XSPIPS_DECODE_SSELECT_OPTION | XSPIPS_FORCE_SSELECT_OPTION |
107 ((dev->cpol == 1) ? XSPIPS_CLK_ACTIVE_LOW_OPTION : 0) |
108 ((dev->cpha == 1) ? XSPIPS_CLK_PHASE_1_OPTION : 0));
109 XSpiPs_SetSlaveSelect(&m_spi, (uint8_t) 0x7);
110 XSpiPs_SetClkPrescaler(&m_spi, XSPIPS_CLK_PRESCALE_64);
111 XSpiPs_SetSlaveSelect(&m_spi, (uint8_t) dev->chip_select);
112 XSpiPs_PolledTransfer(&m_spi, data, data, no_of_bytes);
113 XSpiPs_SetSlaveSelect(&m_spi, (uint8_t) 0x7);
114
115 #endif
116
117 #ifdef NIOS_II
118
119 uint32_t i;
120
121 IOWR_32DIRECT(SYS_SPI_BASE, 0x0c, 0x400);
122 IOWR_32DIRECT(SYS_SPI_BASE, 0x14, ~(dev->chip_select));
123 for (i = 0; i < no_of_bytes; i++) {
124 while ((IORD_32DIRECT(SYS_SPI_BASE, 0x08) & 0x40) == 0x00) {}
125 IOWR_32DIRECT(SYS_SPI_BASE, 0x04, *(data + i));
126 while ((IORD_32DIRECT(SYS_SPI_BASE, 0x08) & 0x80) == 0x00) {}
127 *(data + i) = IORD_32DIRECT(SYS_SPI_BASE, 0x00);
128 }
129 IOWR_32DIRECT(SYS_SPI_BASE, 0x14, 0x000);
130 IOWR_32DIRECT(SYS_SPI_BASE, 0x0c, 0x000);
131
132 #endif
133
134 #ifdef MICROBLAZE
135
136 uint32_t i;
137
138 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), dev->chip_select);
139 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), (0x086 | (dev->cpol<<3) | (dev->cpha<<4)));
140 for (i = 0; i < no_of_bytes; i++) {
141 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x68), *(data + i));
142 while ((Xil_In32(XPAR_SPI_0_BASEADDR + 0x64) & 0x1) == 0x1) {}
143 *(data + i) = Xil_In32(XPAR_SPI_0_BASEADDR + 0x6c) & 0xff;
144 }
145 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), 0xff);
146 Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), (0x186 | (dev->cpol<<3) | (dev->cpha<<4)));
147
148 #endif
149
150 return(0);
151 }
152
153 /***************************************************************************//**
154 * @brief ad_gpio_set
155 *******************************************************************************/
156
157 int32_t ad_gpio_set(uint8_t pin, uint8_t data)
158 {
159
160 int32_t pstatus;
161 uint32_t ppos;
162 uint32_t pdata;
163 uint32_t pmask;
164
165 if (pin < 32) {
166 return(-1);
167 }
168
169 pstatus = -1;
170 ppos = pin - 32;
171 pmask = 0x1 << ppos;
172
173 #ifdef ZYNQ_PS7
174
175 pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x02c4);
176 Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x02c4), (pdata | pmask));
177 pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x02c8);
178 Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x02c8), (pdata | pmask));
179 pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x004c);
180 Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x004c), ((pdata & ~pmask) | (data << ppos)));
181 pstatus = 0;
182
183 #endif
184
185 #ifdef ZYNQ_PSU
186
187 pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0304);
188 Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0304), (pdata | pmask));
189 pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0308);
190 Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0308), (pdata | pmask));
191 pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0050);
192 Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0050), ((pdata & ~pmask) | (data << ppos)));
193 pstatus = 0;
194
195 #endif
196
197 #ifdef NIOS_II
198
199 pdata = IORD_32DIRECT(SYS_GPIO_OUT_BASE, 0x0);
200 IOWR_32DIRECT(SYS_GPIO_OUT_BASE, 0x0, ((pdata & ~pmask) | (data << ppos)));
201 pstatus = 0;
202
203 #endif
204
205 #ifdef MICROBLAZE
206
207 pdata = Xil_In32(XPAR_AXI_GPIO_BASEADDR + 0xc);
208 Xil_Out32((XPAR_AXI_GPIO_BASEADDR + 0xc), (pdata & ~pmask));
209 pdata = Xil_In32(XPAR_AXI_GPIO_BASEADDR + 0x8);
210 Xil_Out32((XPAR_AXI_GPIO_BASEADDR + 0x8), ((pdata & ~pmask) | (data << ppos)));
211 pstatus = 0;
212
213 #endif
214
215 return(pstatus);
216 }
217
218 /***************************************************************************//**
219 * @brief ad_gpio_get
220 *******************************************************************************/
221
222 int32_t ad_gpio_get(uint8_t pin, uint8_t *data)
223 {
224
225 int32_t pstatus;
226 uint32_t ppos;
227 uint32_t pdata;
228 #ifdef ZYNQ
229 uint32_t pmask;
230 #endif
231
232 if (pin < 32) {
233 return(-1);
234 }
235
236 pstatus = -1;
237 ppos = pin - 32;
238 #ifdef ZYNQ
239 pmask = 0x1 << ppos;
240 #endif
241
242 #ifdef ZYNQ_PS7
243
244 pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x02c4);
245 Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x02c4), (pdata & ~pmask));
246 pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x004c);
247 *data = (pdata >> ppos) & 0x1;
248 pstatus = 0;
249
250 #endif
251
252 #ifdef ZYNQ_PSU
253
254 pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0304);
255 Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0304), (pdata & ~pmask));
256 pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0050);
257 *data = (pdata >> ppos) & 0x1;
258 pstatus = 0;
259
260 #endif
261
262 #ifdef NIOS_II
263
264 pdata = IORD_32DIRECT(SYS_GPIO_OUT_BASE, 0x0);
265 *data = (pdata >> ppos) & 0x1;
266 pstatus = 0;
267
268 #endif
269
270 #ifdef MICROBLAZE
271
272 pdata = Xil_In32(XPAR_AXI_GPIO_BASEADDR + 0x8);
273 *data = (pdata >> ppos) & 0x1;
274 pstatus = 0;
275
276 #endif
277
278 return(pstatus);
279 }
280
281 /***************************************************************************//**
282 * @brief do_div
283 *******************************************************************************/
284 uint64_t do_div(uint64_t* n, uint64_t base)
285 {
286 uint64_t mod = 0;
287
288 mod = *n % base;
289 *n = *n / base;
290
291 return mod;
292 }
293
294 /***************************************************************************//**
295 * @brief ad_reg_write_16
296 *******************************************************************************/
297 void ad_reg_write_16(uint32_t addr, uint32_t data)
298 {
299 uint32_t m_data;
300
301 m_data = ad_reg_read(addr & ~0x3);
302 if ((addr & 0x3) == 0)
303 m_data = (m_data & ~0xffff) | data;
304 else
305 m_data = (m_data & 0xffff) | (data<<16);
306 ad_reg_write((addr & ~0x3), m_data);
307 }
308
309 #ifdef ALTERA
310 void ad_platform_init(void)
311 {
312 return;
313 }
314
315 void ad_platform_close(void)
316 {
317 alt_printf("%c", 0x04);
318 return;
319 }
320 #endif
321
322 #ifdef MICROBLAZE
323 void usleep(uint32_t us_count)
324 {
325 uint32_t count;
326
327 ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x4), 0x00);
328 for (count = 0; count < us_count; count++) {
329 ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x0), 0x20);
330 ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x0), 0x80);
331 while (ad_reg_read(XPAR_AXI_TIMER_BASEADDR + 0x8) < 100) {}
332 }
333 ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x0), 0x20);
334 }
335 #endif
336
337 /***************************************************************************//**
338 * @brief ad_uart_read
339 *******************************************************************************/
340
341 #ifdef ZYNQ_PS7
342 uint8_t ad_uart_read()
343 {
344 u32 RecievedByte;
345 int32_t timeout = 100000000;
346 /* Wait until there is data */
347 while (!XUartPs_IsReceiveData(STDIN_BASEADDRESS)&&timeout>0) {
348 timeout--;
349 }
350 RecievedByte = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET);
351 /* Return the byte received */
352 return (uint8_t)RecievedByte;
353 }
354 #endif