Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/adgs5412/adgs5412.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file adgs5412.c
3 * @brief Implementation of ADGS5412 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 "platform_drivers.h"
47 #include "adgs5412.h"
48
49 /******************************************************************************/
50 /************************** Functions Implementation **************************/
51 /******************************************************************************/
52 /**
53 * Compute CRC8 checksum.
54 * @param data - The data buffer.
55 * @param data_size - The size of the data buffer.
56 * @return CRC8 checksum.
57 */
58 uint8_t adgs5412_compute_crc8(uint8_t *data,
59 uint8_t data_size)
60 {
61 uint8_t i;
62 uint8_t crc = 0;
63
64 while (data_size) {
65 for (i = 0x80; i != 0; i >>= 1) {
66 if (((crc & 0x80) != 0) != ((*data & i) != 0)) {
67 crc <<= 1;
68 crc ^= ADGS5412_CRC8_POLY;
69 } else
70 crc <<= 1;
71 }
72 data++;
73 data_size--;
74 }
75
76 return crc;
77 }
78
79 /**
80 * SPI register read from device.
81 * @param dev - The device structure.
82 * @param reg_addr - The register address.
83 * @param reg_data - The register data.
84 * @return SUCCESS in case of success, negative error code otherwise.
85 */
86 int32_t adgs5412_spi_reg_read(adgs5412_dev *dev,
87 uint8_t reg_addr,
88 uint8_t *reg_data)
89 {
90 uint8_t buf[3];
91 uint8_t buf_size = 2;
92 uint8_t crc;
93 int32_t ret;
94
95 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
96 printf("%s: This feature is not available in Daisy-Chain mode.\n",
97 __func__);
98 return FAILURE;
99 }
100
101 buf[0] = 0x80 | (reg_addr & 0x7F);
102 buf[1] = 0x00;
103 buf[2] = 0x00;
104 if (dev->crc_en == ADGS5412_ENABLE)
105 buf_size = 3;
106 ret = spi_write_and_read(&dev->spi_dev, buf, buf_size);
107 if (buf[0] != ADGS5412_ALIGNMENT) {
108 printf("%s: Alignment Error: 0x%x.\n", __func__, buf[0]);
109 ret = FAILURE;
110 }
111 *reg_data = buf[1];
112 if (dev->crc_en == ADGS5412_ENABLE) {
113 buf[0] = 0x80 | (reg_addr & 0x7F);
114 crc = adgs5412_compute_crc8(&buf[0], 2);
115 if (crc != buf[2]) {
116 printf("%s: CRC Error.\n", __func__);
117 ret = FAILURE;
118 }
119 }
120
121 return ret;
122 }
123
124 /**
125 * SPI register write to device.
126 * @param dev - The device structure.
127 * @param reg_addr - The register address.
128 * @param reg_data - The register data.
129 * @return SUCCESS in case of success, negative error code otherwise.
130 */
131 int32_t adgs5412_spi_reg_write(adgs5412_dev *dev,
132 uint8_t reg_addr,
133 uint8_t reg_data)
134 {
135 uint8_t buf[3];
136 uint8_t buf_size = 2;
137 int32_t ret;
138
139 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
140 printf("%s: This feature is not available in Daisy-Chain mode.\n",
141 __func__);
142 return FAILURE;
143 }
144
145 buf[0] = 0x00 | (reg_addr & 0x7F);
146 buf[1] = reg_data;
147 if (dev->crc_en == ADGS5412_ENABLE) {
148 buf[2] = adgs5412_compute_crc8(&buf[0], 2);
149 buf_size = 3;
150 }
151 ret = spi_write_and_read(&dev->spi_dev, buf, buf_size);
152 if (buf[0] != ADGS5412_ALIGNMENT) {
153 printf("%s: Alignment Error: 0x%x.\n", __func__, buf[0]);
154 ret = FAILURE;
155 }
156
157 return ret;
158 }
159
160 /**
161 * SPI register read from device using a mask.
162 * @param dev - The device structure.
163 * @param reg_addr - The register address.
164 * @param mask - The mask.
165 * @param data - The register data.
166 * @return SUCCESS in case of success, negative error code otherwise.
167 */
168 int32_t adgs5412_spi_reg_read_mask(adgs5412_dev *dev,
169 uint8_t reg_addr,
170 uint8_t mask,
171 uint8_t *data)
172 {
173 uint8_t reg_data;
174 int32_t ret;
175
176 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
177 printf("%s: This feature is not available in Daisy-Chain mode.\n",
178 __func__);
179 return FAILURE;
180 }
181
182 ret = adgs5412_spi_reg_read(dev, reg_addr, &reg_data);
183 *data = (reg_data & mask);
184
185 return ret;
186 }
187
188 /**
189 * SPI internal register write to device using a mask.
190 * @param dev - The device structure.
191 * @param reg_addr - The register address.
192 * @param mask - The mask.
193 * @param data - The register data.
194 * @return SUCCESS in case of success, negative error code otherwise.
195 */
196 int32_t adgs5412_spi_reg_write_mask(adgs5412_dev *dev,
197 uint8_t reg_addr,
198 uint8_t mask,
199 uint8_t data)
200 {
201 uint8_t reg_data;
202 int32_t ret;
203
204 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
205 printf("%s: This feature is not available in Daisy-Chain mode.\n",
206 __func__);
207 return FAILURE;
208 }
209
210 ret = adgs5412_spi_reg_read(dev, reg_addr, &reg_data);
211 reg_data &= ~mask;
212 reg_data |= data;
213 ret |= adgs5412_spi_reg_write(dev, reg_addr, reg_data);
214
215 return ret;
216 }
217
218 /**
219 * Do a software reset.
220 * @param dev - The device structure.
221 * @return SUCCESS in case of success, negative error code otherwise.
222 */
223 int32_t adgs5412_do_soft_reset(adgs5412_dev *dev)
224 {
225 int32_t ret;
226
227 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
228 printf("%s: This feature is not available in Daisy-Chain mode.\n",
229 __func__);
230 return FAILURE;
231 }
232
233 ret = adgs5412_spi_reg_write(dev,
234 ADGS5412_REG_SOFT_RESETB,
235 ADGS5412_RESET_1);
236 ret |= adgs5412_spi_reg_write(dev,
237 ADGS5412_REG_SOFT_RESETB,
238 ADGS5412_RESET_2);
239
240 return ret;
241 }
242
243 /**
244 * Clear the Error Flags Register.
245 * @param dev - The device structure.
246 * @return SUCCESS in case of success, negative error code otherwise.
247 */
248 int32_t adgs5412_clear_err_flags(adgs5412_dev *dev)
249 {
250 uint8_t buf[3];
251 uint8_t buf_size = 2;
252 int32_t ret;
253
254 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
255 printf("%s: This feature is not available in Daisy-Chain mode.\n",
256 __func__);
257 return FAILURE;
258 }
259
260 buf[0] = ADGS5412_CLR_1;
261 buf[1] = ADGS5412_CLR_2;
262 if (dev->crc_en == ADGS5412_ENABLE) {
263 buf[2] = adgs5412_compute_crc8(&buf[0], 2);
264 buf_size = 3;
265 }
266 ret = spi_write_and_read(&dev->spi_dev, buf, buf_size);
267
268 return ret;
269 }
270
271 /**
272 * Enter Daisy-Chain Mode.
273 * @param dev - The device structure.
274 * @return SUCCESS in case of success, negative error code otherwise.
275 */
276 int32_t adgs5412_enter_daisy_chain(adgs5412_dev *dev)
277 {
278 uint8_t buf[2];
279 int32_t ret;
280
281 if (dev->daisy_chain_en == ADGS5412_ENABLE) {
282 printf("%s: This feature is not available in Daisy-Chain mode.\n",
283 __func__);
284 return FAILURE;
285 }
286
287 buf[0] = ADGS5412_DAISY_CHAIN_1;
288 buf[1] = ADGS5412_DAISY_CHAIN_2;
289 ret = spi_write_and_read(&dev->spi_dev, buf, 2);
290
291 return ret;
292 }
293
294 /**
295 * Send Daisy-Chain commands.
296 * @param dev - The device structure.
297 * @param cmds - The commands to be sent.
298 * @param cmds_size - The number of commands.
299 * @return SUCCESS in case of success, negative error code otherwise.
300 */
301 int32_t adgs5412_send_daisy_chain_cmds(adgs5412_dev *dev,
302 uint8_t *cmds,
303 uint8_t cmds_size)
304 {
305 int32_t ret;
306
307 if (dev->daisy_chain_en == ADGS5412_DISABLE) {
308 printf("%s: This feature is available in Daisy-Chain mode only.\n",
309 __func__);
310 return FAILURE;
311 }
312
313 ret = spi_write_and_read(&dev->spi_dev, cmds, cmds_size);
314
315 return ret;
316 }
317
318 /**
319 * Initialize the device.
320 * @param device - The device structure.
321 * @param init_param - The structure that contains the device initial
322 * parameters.
323 * @return SUCCESS in case of success, negative error code otherwise.
324 */
325 int32_t adgs5412_setup(adgs5412_dev **device,
326 adgs5412_init_param init_param)
327 {
328 adgs5412_dev *dev;
329 int32_t ret;
330
331 dev = (adgs5412_dev *)malloc(sizeof(*dev));
332 if (!dev) {
333 return -1;
334 }
335
336 /* SPI */
337 dev->spi_dev.chip_select = init_param.spi_chip_select;
338 dev->spi_dev.mode = init_param.spi_mode;
339 dev->spi_dev.device_id = init_param.spi_device_id;
340 dev->spi_dev.type = init_param.spi_type;
341 ret = spi_init(&dev->spi_dev);
342
343 /* Device Settings */
344 dev->crc_en = ADGS5412_DISABLE;
345 dev->daisy_chain_en = ADGS5412_DISABLE;
346 adgs5412_do_soft_reset(dev);
347
348 if (init_param.crc_en == ADGS5412_ENABLE) {
349 adgs5412_spi_reg_write_mask(dev,
350 ADGS5412_REG_ERR_CONFIG,
351 ADGS5412_CRC_ERR_EN,
352 ADGS5412_CRC_ERR_EN);
353 dev->crc_en = ADGS5412_ENABLE;
354 }
355
356 dev->burst_mode_en = init_param.burst_mode_en;
357 if (dev->burst_mode_en == ADGS5412_ENABLE)
358 adgs5412_spi_reg_write_mask(dev,
359 ADGS5412_REG_BURST_EN,
360 ADGS5412_BURST_MODE_EN,
361 ADGS5412_BURST_MODE_EN);
362
363 if (init_param.daisy_chain_en == ADGS5412_ENABLE) {
364 adgs5412_enter_daisy_chain(dev);
365 dev->daisy_chain_en = ADGS5412_ENABLE;
366 }
367
368 *device = dev;
369
370 if (!ret)
371 printf("ADGS5412 successfully initialized\n");
372 else
373 printf("ADGS5412 initialization error (%d)\n", ret);
374
375 return ret;
376 }