Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad7616-sdz/ad7616.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad7616.c
3 * @brief Implementation of AD7616 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 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 <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <sleep.h>
47 #include "platform_drivers.h"
48 #include "ad7616_core.h"
49 #include "ad7616.h"
50
51 /**
52 * Read from device.
53 * @param dev - The device structure.
54 * @param reg_addr - The register address.
55 * @param reg_data - The register data.
56 * @return 0 in case of success, negative error code otherwise.
57 */
58 int32_t ad7616_read(ad7616_dev *dev,
59 uint8_t reg_addr,
60 uint16_t *reg_data)
61 {
62 if (dev->interface == AD7616_SERIAL)
63 return ad7616_spi_read(dev, reg_addr, reg_data);
64 else
65 return ad7616_par_read(dev, reg_addr, reg_data);
66 }
67
68 /**
69 * Write to device.
70 * @param dev - The device structure.
71 * @param reg_addr - The register address.
72 * @param reg_data - The register data.
73 * @return 0 in case of success, negative error code otherwise.
74 */
75 int32_t ad7616_write(ad7616_dev *dev,
76 uint8_t reg_addr,
77 uint16_t reg_data)
78 {
79 if (dev->interface == AD7616_SERIAL)
80 return ad7616_spi_write(dev, reg_addr, reg_data);
81 else
82 return ad7616_par_write(dev, reg_addr, reg_data);
83 }
84
85 /**
86 * Read from device using a mask.
87 * @param dev - The device structure.
88 * @param reg_addr - The register address.
89 * @param mask - The mask.
90 * @param data - The register data.
91 * @return 0 in case of success, negative error code otherwise.
92 */
93 int32_t ad7616_read_mask(ad7616_dev *dev,
94 uint8_t reg_addr,
95 uint16_t mask,
96 uint16_t *data)
97 {
98 uint16_t reg_data;
99 int32_t ret;
100
101 if (dev->interface == AD7616_SERIAL)
102 ret = ad7616_spi_read(dev, reg_addr, &reg_data);
103 else
104 ret = ad7616_par_read(dev, reg_addr, &reg_data);
105 *data = (reg_data & mask);
106
107 return ret;
108 }
109
110 /**
111 * SPI write to device using a mask.
112 * @param dev - The device structure.
113 * @param reg_addr - The register address.
114 * @param mask - The mask.
115 * @param data - The register data.
116 * @return 0 in case of success, negative error code otherwise.
117 */
118 int32_t ad7616_write_mask(ad7616_dev *dev,
119 uint8_t reg_addr,
120 uint16_t mask,
121 uint16_t data)
122 {
123 uint16_t reg_data;
124 int32_t ret;
125
126 if (dev->interface == AD7616_SERIAL)
127 ret = ad7616_spi_read(dev, reg_addr, &reg_data);
128 else
129 ret = ad7616_par_read(dev, reg_addr, &reg_data);
130 reg_data &= ~mask;
131 reg_data |= data;
132 if (dev->interface == AD7616_SERIAL)
133 ret |= ad7616_spi_write(dev, reg_addr, reg_data);
134 else
135 ret |= ad7616_par_write(dev, reg_addr, reg_data);
136
137 return ret;
138 }
139
140 /**
141 * SPI read from device.
142 * @param dev - The device structure.
143 * @param reg_addr - The register address.
144 * @param reg_data - The register data.
145 * @return 0 in case of success, negative error code otherwise.
146 */
147 int32_t ad7616_spi_read(ad7616_dev *dev,
148 uint8_t reg_addr,
149 uint16_t *reg_data)
150 {
151 uint8_t buf[2];
152 int32_t ret;
153
154 buf[0] = 0x00 | ((reg_addr & 0x3F) << 1);
155 buf[1] = 0x00;
156 ret = spi_write_and_read(&dev->spi_dev, buf, 2);
157
158 buf[0] = 0x00 | ((reg_addr & 0x3F) << 1);
159 buf[1] = 0x00;
160 ret = spi_write_and_read(&dev->spi_dev, buf, 2);
161 *reg_data = ((buf[0] & 0x01) << 8) | buf[1];
162
163 return ret;
164 }
165
166 /**
167 * SPI write to device.
168 * @param dev - The device structure.
169 * @param reg_addr - The register address.
170 * @param reg_data - The register data.
171 * @return 0 in case of success, negative error code otherwise.
172 */
173 int32_t ad7616_spi_write(ad7616_dev *dev,
174 uint8_t reg_addr,
175 uint16_t reg_data)
176 {
177 uint8_t buf[2];
178 int32_t ret;
179
180 buf[0] = 0x80 | ((reg_addr & 0x3F) << 1) | ((reg_data & 0x100) >> 8);
181 buf[1] = (reg_data & 0xFF);
182 ret = spi_write_and_read(&dev->spi_dev, buf, 2);
183
184 return ret;
185 }
186
187 /**
188 * PAR read from device.
189 * @param dev - The device structure.
190 * @param reg_addr - The register address.
191 * @param reg_data - The register data.
192 * @return 0 in case of success, negative error code otherwise.
193 */
194 int32_t ad7616_par_read(ad7616_dev *dev,
195 uint8_t reg_addr,
196 uint16_t *reg_data)
197 {
198 uint32_t read;
199
200 ad7616_core_write(*dev->core, AD7616_REG_UP_WRITE_DATA,
201 0x0000 | ((reg_addr & 0x3F) << 9));
202 usleep(50);
203 ad7616_core_read(*dev->core, AD7616_REG_UP_READ_DATA, &read);
204 *reg_data = read & 0xFF;
205 mdelay(1);
206
207 return 0;
208 }
209
210 /**
211 * PAR write to device.
212 * @param dev - The device structure.
213 * @param reg_addr - The register address.
214 * @param reg_data - The register data.
215 * @return 0 in case of success, negative error code otherwise.
216 */
217 int32_t ad7616_par_write(ad7616_dev *dev,
218 uint8_t reg_addr,
219 uint16_t reg_data)
220 {
221 ad7616_core_write(*dev->core, AD7616_REG_UP_WRITE_DATA,
222 0x8000 | ((reg_addr & 0x3F) << 9) | (reg_data & 0xFF));
223 mdelay(1);
224
225 return 0;
226 }
227
228 /**
229 * Perform a full reset of the device.
230 * @param dev - The device structure.
231 * @return 0 in case of success, negative error code otherwise.
232 */
233 int32_t ad7616_reset(ad7616_dev *dev)
234 {
235 int32_t ret;
236
237 ret = gpio_set_value(&dev->gpio_dev, dev->gpio_reset, GPIO_LOW);
238 /* Low pulse width for a full reset should be at least 1200 ns */
239 mdelay(20);
240 ret |= gpio_set_value(&dev->gpio_dev, dev->gpio_reset, GPIO_HIGH);
241 /* 15 ms are required to completely reconfigure the device */
242 mdelay(150);
243
244 return ret;
245 }
246
247 /**
248 * Set the analog input range for the selected analog input channel.
249 * @param dev - The device structure.
250 * @param ch - The channel number.
251 * Accepted values: AD7616_VA0
252 * AD7616_VA1
253 * AD7616_VA2
254 * AD7616_VA3
255 * AD7616_VA4
256 * AD7616_VA5
257 * AD7616_VA6
258 * AD7616_VA7
259 * AD7616_VB0
260 * AD7616_VB1
261 * AD7616_VB2
262 * AD7616_VB3
263 * AD7616_VB4
264 * AD7616_VB5
265 * AD7616_VB6
266 * AD7616_VB7
267 * @param range - The analog input range.
268 * Accepted values: AD7616_2V5
269 * AD7616_5V
270 * AD7616_10V
271 * @return 0 in case of success, negative error code otherwise.
272 */
273 int32_t ad7616_set_range(ad7616_dev *dev,
274 ad7616_ch ch,
275 ad7616_range range)
276 {
277 uint8_t reg_addr;
278 uint8_t mask;
279 uint8_t data;
280 uint8_t i;
281 int32_t ret;
282
283 if (dev->mode == AD7616_SW) {
284 if (ch <= AD7616_VA7) {
285 dev->va[ch] = range;
286 if (ch <= AD7616_VA3) {
287 reg_addr = AD7616_REG_INPUT_RANGE_A1;
288 mask = AD7616_INPUT_RANGE(ch, AD7616_10V);
289 data = AD7616_INPUT_RANGE(ch, range);
290 } else {
291 reg_addr = AD7616_REG_INPUT_RANGE_A2;
292 mask = AD7616_INPUT_RANGE(ch - AD7616_VA4, AD7616_10V);
293 data = AD7616_INPUT_RANGE(ch - AD7616_VA4, range);
294 }
295 } else {
296 dev->vb[ch - AD7616_VB0] = range;
297 if (ch <= AD7616_VB3) {
298 reg_addr = AD7616_REG_INPUT_RANGE_B1;
299 mask = AD7616_INPUT_RANGE(ch - AD7616_VB0, AD7616_10V);
300 data = AD7616_INPUT_RANGE(ch - AD7616_VB0, range);
301 } else {
302 reg_addr = AD7616_REG_INPUT_RANGE_B2;
303 mask = AD7616_INPUT_RANGE(ch - AD7616_VB4, AD7616_10V);
304 data = AD7616_INPUT_RANGE(ch - AD7616_VB4, range);
305 }
306 }
307 ret = ad7616_write_mask(dev, reg_addr, mask, data);
308 } else {
309 for (i = 0; i <= AD7616_VA7; i++) {
310 dev->va[i] = range;
311 dev->vb[i] = range;
312 }
313 ret = gpio_set_value(&dev->gpio_dev,
314 dev->gpio_hw_rngsel0, ((range & 0x01) >> 0));
315 ret |= gpio_set_value(&dev->gpio_dev,
316 dev->gpio_hw_rngsel1, ((range & 0x02) >> 1));
317 }
318
319 return ret;
320 }
321
322 /**
323 * Set the operation mode (software or hardware).
324 * @param dev - The device structure.
325 * @param mode - The operation mode.
326 * Accepted values: AD7616_SW
327 * AD7616_HW
328 * @return 0 in case of success, negative error code otherwise.
329 */
330 int32_t ad7616_set_mode(ad7616_dev *dev,
331 ad7616_mode mode)
332 {
333 uint8_t i;
334 int32_t ret = 0;
335
336 dev->mode = mode;
337 for (i = 0; i <= AD7616_VA7; i++) {
338 ret |= ad7616_set_range(dev, i, dev->va[i]);
339 ret |= ad7616_set_range(dev, i + AD7616_VB0, dev->vb[i]);
340 }
341
342 return ret;
343 }
344
345 /**
346 * Set the oversampling ratio.
347 * @param dev - The device structure.
348 * @param osr - The oversampling ratio.
349 * Accepted values: AD7616_OSR_0
350 * AD7616_OSR_2
351 * AD7616_OSR_4
352 * AD7616_OSR_8
353 * AD7616_OSR_16
354 * AD7616_OSR_32
355 * AD7616_OSR_64
356 * AD7616_OSR_128
357 * @return 0 in case of success, negative error code otherwise.
358 */
359 int32_t ad7616_set_oversampling_ratio(ad7616_dev *dev,
360 ad7616_osr osr)
361 {
362 int32_t ret = 0;
363
364 if (dev->mode == AD7616_SW) {
365 ret = ad7616_write_mask(dev, AD7616_REG_CONFIG,
366 AD7616_OS(0x7), AD7616_OS(osr));
367 } else {
368 ret = gpio_set_value(&dev->gpio_dev,
369 dev->gpio_os0, ((osr & 0x01) >> 0));
370 ret |= gpio_set_value(&dev->gpio_dev,
371 dev->gpio_os1, ((osr & 0x02) >> 1));
372 ret |= gpio_set_value(&dev->gpio_dev,
373 dev->gpio_os2, ((osr & 0x04) >> 2));
374 }
375
376 return ret;
377 }
378
379 /**
380 * Initialize the device.
381 * @param device - The device structure.
382 * @param init_param - The structure that contains the device initial
383 * parameters.
384 * @return 0 in case of success, negative error code otherwise.
385 */
386 int32_t ad7616_setup(ad7616_dev **device,
387 adc_core *core,
388 ad7616_init_param init_param)
389 {
390 ad7616_dev *dev;
391 uint8_t i;
392 uint32_t if_type;
393 int32_t ret = 0;
394
395 dev = (ad7616_dev *)malloc(sizeof(*dev));
396 if (!dev) {
397 return -1;
398 }
399
400 dev->core = core;
401 ad7616_core_read(*dev->core, AD7616_REG_UP_IF_TYPE, &if_type);
402 if (if_type)
403 dev->interface = AD7616_PARALLEL;
404 else
405 dev->interface = AD7616_SERIAL;
406
407 dev->spi_dev.chip_select = init_param.spi_chip_select;
408 dev->spi_dev.mode = init_param.spi_mode;
409 dev->spi_dev.device_id = init_param.spi_device_id;
410 dev->spi_dev.type = init_param.spi_type;
411 if (dev->interface == AD7616_SERIAL)
412 ret |= spi_init(&dev->spi_dev);
413
414 dev->gpio_dev.device_id = init_param.gpio_device_id;
415 dev->gpio_dev.type = init_param.gpio_type;
416 ret |= gpio_init(&dev->gpio_dev);
417
418 dev->gpio_reset = init_param.gpio_reset;
419 if (dev->gpio_reset >= 0) {
420 ret |= gpio_set_direction(&dev->gpio_dev,
421 dev->gpio_reset, GPIO_OUT);
422 ret |= gpio_set_value(&dev->gpio_dev,
423 dev->gpio_reset,
424 GPIO_HIGH);
425 }
426
427 dev->mode = init_param.mode;
428 for (i = 0; i <= AD7616_VA7; i++) {
429 dev->va[i] = init_param.va[i];
430 dev->vb[i] = init_param.vb[i];
431 }
432 ret |= ad7616_set_mode(dev, dev->mode);
433
434 dev->osr = init_param.osr;
435 ret |= ad7616_set_oversampling_ratio(dev, dev->osr);
436
437 *device = dev;
438
439 if (!ret)
440 printf("AD7616 successfully initialized\n");
441
442 return ret;
443 }