Debian Codegraph @

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