Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad7616-sdz/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-2016(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 #include <xparameters.h>
44 #ifdef _XPARAMETERS_PS_H_
45 #include <sleep.h>
46 #else
47 #include <microblaze_sleep.h>
48 #endif
49 #include "platform_drivers.h"
50 #include "spi_engine.h"
51
52 /******************************************************************************/
53 /************************ Variables Definitions *******************************/
54 /******************************************************************************/
55 #ifdef XPAR_PS7_SPI_0_DEVICE_ID
56 uint8_t spi_decoded_cs = 0;
57 #endif
58
59 /***************************************************************************//**
60 * @brief spi_init
61 *******************************************************************************/
62 int32_t spi_init(spi_device *dev)
63 {
64 uint8_t clk_pha;
65 uint8_t clk_pol;
66 uint32_t spi_options = 0;
67
68 clk_pha = (dev->mode & SPI_CPHA) >> 0;
69 clk_pol = (dev->mode & SPI_CPOL) >> 1;
70
71 switch (dev->type) {
72 case AXI_SPI:
73 #ifdef XPAR_SPI_0_DEVICE_ID
74 XSpi_Initialize(&dev->axi_instance,
75 dev->device_id);
76
77 XSpi_Stop(&dev->axi_instance);
78
79 dev->axi_config = XSpi_LookupConfig(dev->device_id);
80
81 XSpi_CfgInitialize(&dev->axi_instance,
82 dev->axi_config,
83 dev->axi_config->BaseAddress);
84
85 spi_options = XSP_MASTER_OPTION |
86 (clk_pol ? XSP_CLK_ACTIVE_LOW_OPTION : 0) |
87 (clk_pha ? XSP_CLK_PHASE_1_OPTION : 0) |
88 XSP_MANUAL_SSELECT_OPTION;
89 XSpi_SetOptions(&dev->axi_instance, spi_options);
90
91 XSpi_Start(&dev->axi_instance);
92
93 XSpi_IntrGlobalDisable(&dev->axi_instance);
94 #endif
95 break;
96 case PS7_SPI:
97 #ifdef XPAR_PS7_SPI_0_DEVICE_ID
98 dev->ps7_config = XSpiPs_LookupConfig(dev->device_id);
99
100 XSpiPs_CfgInitialize(&dev->ps7_instance,
101 dev->ps7_config,
102 dev->ps7_config->BaseAddress);
103
104 spi_options = XSPIPS_MASTER_OPTION |
105 (clk_pol ? XSPIPS_CLK_ACTIVE_LOW_OPTION : 0) |
106 (clk_pha ? XSPIPS_CLK_PHASE_1_OPTION : 0) |
107 (spi_decoded_cs ? XSPIPS_DECODE_SSELECT_OPTION : 0) |
108 XSPIPS_FORCE_SSELECT_OPTION;
109 XSpiPs_SetOptions(&dev->ps7_instance,
110 spi_options);
111
112 XSpiPs_SetClkPrescaler(&dev->ps7_instance,
113 XSPIPS_CLK_PRESCALE_32);
114 #endif
115 break;
116 case SPI_ENGINE:
117 spi_engine_setup(XPAR_AXI_AD7616_BASEADDR);
118 break;
119 default:
120 return -1;
121 }
122
123 return 0;
124 }
125
126 /***************************************************************************//**
127 * @brief spi_write_and_read
128 *******************************************************************************/
129 int32_t spi_write_and_read(spi_device *dev,
130 uint8_t *data,
131 uint8_t bytes_number)
132 {
133 switch (dev->type) {
134 case AXI_SPI:
135 #ifdef XPAR_SPI_0_DEVICE_ID
136 uint8_t ss = (1 << dev->chip_select);
137 uint8_t send_buffer[20];
138 uint32_t cnt = 0;
139
140 XSpi_SetSlaveSelect(&dev->axi_instance, ss);
141
142 for(cnt = 0; cnt < bytes_number; cnt++)
143 {
144 send_buffer[cnt] = data[cnt];
145 }
146 XSpi_Transfer(&dev->axi_instance,
147 send_buffer,
148 data,
149 bytes_number);
150 #endif
151 break;
152 case PS7_SPI:
153 #ifdef XPAR_PS7_SPI_0_DEVICE_ID
154 XSpiPs_SetSlaveSelect(&dev->ps7_instance, dev->chip_select);
155 XSpiPs_PolledTransfer(&dev->ps7_instance, data, data, bytes_number);
156 #endif
157 break;
158 case SPI_ENGINE:
159 spi_engine_write_and_read(dev->chip_select, data, bytes_number);
160 break;
161 default:
162 return -1;
163 }
164
165 return 0;
166 }
167
168 /***************************************************************************//**
169 * @brief gpio_init
170 *******************************************************************************/
171 int32_t gpio_init(gpio_device *dev)
172 {
173 if (dev->type == AXI_GPIO) {
174 #ifdef XPAR_GPIO_0_DEVICE_ID
175 dev->axi_config = XGpio_LookupConfig(dev->device_id);
176 XGpio_CfgInitialize(&dev->axi_instance,
177 dev->axi_config,
178 dev->axi_config->BaseAddress);
179 #endif
180 } else {
181 #ifdef XPAR_PS7_GPIO_0_DEVICE_ID
182 dev->ps7_config = XGpioPs_LookupConfig(dev->device_id);
183 XGpioPs_CfgInitialize(&dev->ps7_instance,
184 dev->ps7_config,
185 dev->ps7_config->BaseAddr);
186 #endif
187 }
188
189 return 0;
190 #if 0
191 uint32_t base_addr = 0;
192
193 #ifdef _XPARAMETERS_PS_H_
194 gpio_config = XGpioPs_LookupConfig(device_id);
195
196 base_addr = gpio_config->BaseAddr;
197 XGpioPs_CfgInitialize(&gpio_instance, gpio_config, base_addr);
198 #else
199 gpio_config = XGpio_LookupConfig(device_id);
200
201 base_addr = gpio_config->BaseAddress;
202 XGpio_CfgInitialize(&gpio_instance, gpio_config, base_addr);
203 #endif
204
205 return 0;
206 #endif
207 }
208
209 /***************************************************************************//**
210 * @brief gpio_direction
211 *******************************************************************************/
212 int32_t gpio_set_direction(gpio_device *dev,
213 uint8_t pin,
214 uint8_t direction)
215 {
216 if (dev->type == AXI_GPIO) {
217 #ifdef XPAR_GPIO_0_DEVICE_ID
218 uint32_t channel = 1;
219 uint32_t config = 0;
220
221 /* We assume that pin 32 is the first pin from channel 2 */
222 if (pin >= 32) {
223 channel = 2;
224 pin -= 32;
225 }
226
227 config = XGpio_GetDataDirection(&dev->axi_instance,
228 channel);
229 if (direction) {
230 config &= ~(1 << pin);
231 } else {
232 config |= (1 << pin);
233 }
234 XGpio_SetDataDirection(&dev->axi_instance,
235 channel,
236 config);
237 #endif
238 } else {
239 #ifdef XPAR_PS7_GPIO_0_DEVICE_ID
240 XGpioPs_SetDirectionPin(&dev->ps7_instance,
241 pin,
242 direction);
243 XGpioPs_SetOutputEnablePin(&dev->ps7_instance,
244 pin,
245 1);
246 #endif
247 }
248
249 return 0;
250 #if 0
251
252 #ifdef _XPARAMETERS_PS_H_
253 XGpioPs_SetDirectionPin(&gpio_instance, pin, direction);
254 XGpioPs_SetOutputEnablePin(&gpio_instance, pin, 1);
255 #else
256 uint32_t channel = 1;
257 uint32_t config = 0;
258
259 /* We assume that pin 32 is the first pin from channel 2 */
260 if (pin >= 32) {
261 channel = 2;
262 pin -= 32;
263 }
264
265 config = XGpio_GetDataDirection(&gpio_instance, channel);
266 if (direction) {
267 config &= ~(1 << pin);
268 } else {
269 config |= (1 << pin);
270 }
271 XGpio_SetDataDirection(&gpio_instance, channel, config);
272 #endif
273
274 return 0;
275 #endif
276 }
277
278 /***************************************************************************//**
279 * @brief gpio_set_value
280 *******************************************************************************/
281 int32_t gpio_set_value(gpio_device *dev,
282 uint8_t pin,
283 uint8_t data)
284 {
285 if (dev->type == AXI_GPIO) {
286 #ifdef XPAR_GPIO_0_DEVICE_ID
287 uint32_t channel = 1;
288 uint32_t config = 0;
289
290 /* We assume that pin 32 is the first pin from channel 2 */
291 if (pin >= 32) {
292 channel = 2;
293 pin -= 32;
294 }
295
296 config = XGpio_DiscreteRead(&dev->axi_instance,
297 channel);
298 if(data) {
299 config |= (1 << pin);
300 } else {
301 config &= ~(1 << pin);
302 }
303 XGpio_DiscreteWrite(&dev->axi_instance,
304 channel,
305 config);
306 #endif
307 } else {
308 #ifdef XPAR_PS7_GPIO_0_DEVICE_ID
309 XGpioPs_WritePin(&dev->ps7_instance,
310 pin,
311 data);
312 #endif
313 }
314
315 return 0;
316 #if 0
317 #ifdef _XPARAMETERS_PS_H_
318 XGpioPs_WritePin(&gpio_instance, pin, data);
319 #else
320 uint32_t channel = 1;
321 uint32_t config = 0;
322
323 /* We assume that pin 32 is the first pin from channel 2 */
324 if (pin >= 32) {
325 channel = 2;
326 pin -= 32;
327 }
328
329 config = XGpio_DiscreteRead(&gpio_instance, channel);
330 if(data) {
331 config |= (1 << pin);
332 } else {
333 config &= ~(1 << pin);
334 }
335 XGpio_DiscreteWrite(&gpio_instance, channel, config);
336 #endif
337
338 return 0;
339 #endif
340 }
341
342 /***************************************************************************//**
343 * @brief gpio_get_value
344 *******************************************************************************/
345 int32_t gpio_get_value(gpio_device *dev,
346 uint8_t pin,
347 uint8_t *data)
348 {
349 if (dev->type == AXI_GPIO) {
350 #ifdef XPAR_GPIO_0_DEVICE_ID
351 uint32_t channel = 1;
352 uint32_t config = 0;
353
354 /* We assume that pin 32 is the first pin from channel 2 */
355 if (pin >= 32) {
356 channel = 2;
357 pin -= 32;
358 }
359
360 config = XGpio_DiscreteRead(&dev->axi_instance,
361 channel);
362 *data = (config & (1 << pin)) ? 1 : 0;
363 #endif
364 } else {
365 #ifdef XPAR_PS7_GPIO_0_DEVICE_ID
366 *data = XGpioPs_ReadPin(&dev->ps7_instance, pin);
367 #endif
368 }
369
370 return 0;
371 #if 0
372 #ifdef _XPARAMETERS_PS_H_
373 *data = XGpioPs_ReadPin(&gpio_instance, pin);
374 #else
375 uint32_t channel = 1;
376 uint32_t config = 0;
377
378 /* We assume that pin 32 is the first pin from channel 2 */
379 if (pin >= 32) {
380 channel = 2;
381 pin -= 32;
382 }
383
384 config = XGpio_DiscreteRead(&gpio_instance, channel);
385 *data = (config & (1 << pin)) ? 1 : 0;
386 #endif
387
388 return 0;
389 #endif
390 }
391
392 /***************************************************************************//**
393 * @brief mdelay
394 *******************************************************************************/
395 void mdelay(uint32_t msecs)
396 {
397 #ifdef _XPARAMETERS_PS_H_
398 usleep(msecs * 1000);
399 #else
400 MB_Sleep(msecs);
401 #endif
402 }
403
404 /***************************************************************************//**
405 * @brief do_div
406 *******************************************************************************/
407 uint64_t do_div(uint64_t* n, uint64_t base)
408 {
409 uint64_t mod = 0;
410
411 mod = *n % base;
412 *n = *n / base;
413
414 return mod;
415 }