Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD5686/ad5686.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5686.c
3 * @brief Implementation of AD5686 Driver.
4 * @author Istvan Csomortani ([email protected])
5 *******************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39 * DAMAGE.
40 *
41 ******************************************************************************/
42
43 /*****************************************************************************/
44 /***************************** Include Files *********************************/
45 /*****************************************************************************/
46 #include "AD5686.h"
47 #include "Communication.h"
48
49 #define MAX_RESOLUTION 16 /* Maximum resolution of the supported devices */
50
51 #define CMD_MASK 0xFF /* Mask for Command bits */
52 #define ADDR_MASK 0xFF /* Mask for Address bits */
53 #define CMD_OFFSET 4 /* Offset for Command */
54 #define MSB_MASK 0xFF00 /* Most significant byte of the data word */
55 #define MSB_OFFSET 8
56 #define LSB_MASK 0x00FF /* Least significant byte of the data word */
57 #define LSB_OFFSET 0
58 #define PKT_LENGTH 3 /* SPI packet length in byte */
59
60 /*****************************************************************************/
61 /***************************** Variable definition ***************************/
62 /*****************************************************************************/
63 typedef enum
64 {
65 spi,
66 i2c
67 } comm_type_t;
68
69 struct ad5686_chip_info {
70 unsigned char resolution;
71 comm_type_t communication;
72 };
73
74 static const struct ad5686_chip_info ad5686_chip_info[] = {
75 [ID_AD5684R] = {
76 .resolution = 12,
77 .communication = spi,
78 },
79 [ID_AD5685R] = {
80 .resolution = 14,
81 .communication = spi,
82 },
83 [ID_AD5686R] = {
84 .resolution = 16,
85 .communication = spi,
86 },
87 [ID_AD5694R] = {
88 .resolution = 12,
89 .communication = i2c,
90 },
91 [ID_AD5695R] = {
92 .resolution = 14,
93 .communication = i2c,
94 },
95 [ID_AD5696R] = {
96 .resolution = 16,
97 .communication = i2c,
98 }
99 };
100
101 AD5686_type_t act_device;
102
103 /**************************************************************************//**
104 * @brief Initialize SPI and Initial Values for AD5686 Board.
105 *
106 * @param device - Type of the desired device.
107 * Example: ID_AD5415, ID_AD5426, ID_AD5429, ID_AD5432,
108 * ID_AD5439, ID_AD5443, ID_AD5686,
109 * @return retValue - Result of the initialization.
110 * Example: 0 - if initialization was successful;
111 * -1 - if initialization was unsuccessful.
112 ******************************************************************************/
113 char AD5686_Init(AD5686_type_t device)
114 {
115 char status = -1;
116
117 act_device = device;
118
119 /* Initialize SPI communication. */
120 if(ad5686_chip_info[act_device].communication == spi)
121 {
122 status = SPI_Init(0, 1000000, 0, 0);
123 }
124 else
125 {
126 status = I2C_Init(100000);
127 }
128
129 /* Set GPIO pins. */
130 AD5686_LDAC_OUT;
131 AD5686_LDAC_LOW;
132 AD5686_RESET_OUT;
133 AD5686_RESET_HIGH;
134
135 return status;
136 }
137
138 /**************************************************************************//**
139 * @brief Write to input shift register.
140 *
141 * @param command - command control bits.
142 * data - data to be written in input register.
143 *
144 * @return readBack - value read from register.
145 ******************************************************************************/
146 unsigned short AD5686_SetShiftReg(unsigned char command,
147 unsigned char address,
148 unsigned short data)
149 {
150 unsigned char dataBuff[PKT_LENGTH] = {0, 0, 0};
151 unsigned short readBackData = 0;
152
153 dataBuff[0] = ((command & CMD_MASK) << CMD_OFFSET) | \
154 (address & ADDR_MASK);
155 dataBuff[1] = (data & MSB_MASK) >> MSB_OFFSET;
156 dataBuff[2] = (data & LSB_MASK);
157
158 if(ad5686_chip_info[act_device].communication == spi)
159 {
160 SPI_Write((unsigned char)AD5686_SLAVE_ID, dataBuff, PKT_LENGTH);
161 readBackData = (dataBuff[1] << MSB_OFFSET) | dataBuff[2];
162 }
163 else
164 {
165 I2C_Write(AD5686_I2C_ADDR, dataBuff, PKT_LENGTH, 1);
166 }
167 return readBackData;
168 }
169
170 /**************************************************************************//**
171 * @brief Write to Input Register n (dependent on LDAC)
172 *
173 * @param address - the chosen channel to write to.
174 * Example: 'AD5686_CH_A | AD5686_CH_C' will write to \
175 * channel A and C
176 * data - desired value to be written in register.
177 *
178 * @return None.
179 ******************************************************************************/
180 void AD5686_WriteRegister(unsigned char address, unsigned short data)
181 {
182 unsigned char dataOffset = MAX_RESOLUTION - \
183 ad5686_chip_info[act_device].resolution;
184
185 AD5686_SetShiftReg(AD5686_CTRL_WRITE, address, data << dataOffset);
186 }
187
188 /**************************************************************************//**
189 * @brief Update DAC Register n with contents of Input Register n
190 *
191 * @param address - the chosen channel to write to.
192 * Example: 'AD5686_CH_A | AD5686_CH_C' will write to \
193 * channel A and C
194 *
195 * @return None.
196 ******************************************************************************/
197 void AD5686_UpdateRegister(unsigned char address)
198 {
199 AD5686_SetShiftReg(AD5686_CTRL_UPDATE, address, 0);
200 }
201
202 /**************************************************************************//**
203 * @brief Write to and update DAC channel n
204 *
205 * @param address - the chosen channel to write to.
206 * Example: 'AD5686_CH_A | AD5686_CH_C' will write to \
207 * channel A and C
208 * data - desired value to be written in register.
209 *
210 * @return None.
211 ******************************************************************************/
212 void AD5686_WriteUpdateRegister(unsigned char address, unsigned short data)
213 {
214 unsigned dataOffset = MAX_RESOLUTION - \
215 ad5686_chip_info[act_device].resolution;
216
217 AD5686_SetShiftReg(AD5686_CTRL_WRITEUPDATE, address, data << dataOffset);
218 }
219
220 /**************************************************************************//**
221 * @brief Read back Input Register n
222 *
223 * @param address - the channel which will be read back. Note: only one
224 * channel should be selected, if there will be selected
225 * more than one channel, the channel A will be read back
226 * by default
227 * Example: 'AD5686_CH_C' will read back the channel C
228 *
229 * @return None.
230 ******************************************************************************/
231 unsigned short AD5686_ReadBackRegister(unsigned char address)
232 {
233
234 unsigned short readBackData = 0;
235 unsigned short offset = MAX_RESOLUTION - \
236 ad5686_chip_info[act_device].resolution;
237
238 if(ad5686_chip_info[act_device].communication == spi)
239 {
240 AD5686_SetShiftReg(AD5686_CTRL_RB_REG, address, 0);
241 readBackData = AD5686_SetShiftReg(AD5686_CTRL_NOP, 0, 0);
242 readBackData >>= offset;
243 }
244
245 return readBackData;
246 }
247
248 /**************************************************************************//**
249 * @brief Write to and update DAC channel n
250 *
251 * @param address - the chosen channel to write to.
252 * Example : 'AD5686_CH_A | AD5686_CH_C' will write to \
253 * channel A and C
254 * mode - Power-down operation modes.
255 Accepted values:
256 'AD5686_PWRM_NORMAL' - Normal Mode
257 * 'AD5686_PWRM_1K' - Power-down mode 1kOhm to GND
258 * 'AD5686_PWRM_100K' - Power-down mode 100kOhm to GND
259 * 'AD5686_PWRM_THREESTATE' - Three-State
260 *
261 * @return None.
262 ******************************************************************************/
263 void AD5686_PowerMode(unsigned char address, unsigned char mode)
264 {
265 unsigned short data = 0;
266
267 data |= (address & AD5686_CH_A) ? (mode << AD5686_PWRM_CHA_OFFSET) : 0x0;
268 data |= (address & AD5686_CH_B) ? (mode << AD5686_PWRM_CHB_OFFSET) : 0x0;
269 data |= (address & AD5686_CH_C) ? (mode << AD5686_PWRM_CHC_OFFSET) : 0x0;
270 data |= (address & AD5686_CH_D) ? (mode << AD5686_PWRM_CHD_OFFSET) : 0x0;
271
272 AD5686_SetShiftReg(AD5686_CTRL_PWR, address, data);
273 }
274
275 /**************************************************************************//**
276 * @brief Set hardware LDAC mask register
277 *
278 * @param ldacMask - in case of which channel ignore transitions on the LDAC * pin
279 * Example: 'AD5686_CH_A | AD5686_CH_C' will write to \
280 * channel A and C
281 *
282 * @return None.
283 ******************************************************************************/
284 void AD5686_LdacMask(unsigned char ldacMask)
285 {
286 AD5686_SetShiftReg(AD5686_CTRL_LDAC_MASK, 0, ldacMask);
287 }
288
289 /**************************************************************************//**
290 * @brief Software reset (power-on reset)
291 *
292 * @param None.
293 *
294 * @return None.
295 ******************************************************************************/
296 void AD5686_SoftwareReset()
297 {
298 AD5686_SetShiftReg(AD5686_CTRL_SWRESET, 0, 0);
299 }
300
301
302 /**************************************************************************//**
303 * @brief Write to Internal reference setup register
304 * @param value - the internal reference register value
305 * Example : 'AD5686_INTREF_EN' - enable internal reference
306 * 'AD5686_INTREF_DIS' - disable internal reference
307 *
308 * @return None.
309 ******************************************************************************/
310 void AD5686_InternalReference(unsigned char value)
311 {
312 AD5686_SetShiftReg(AD5686_CTRL_IREF_REG, 0, value);
313 }
314
315 /**************************************************************************//**
316 * @brief Set up DCEN register (daisy-chain enable)
317 *
318 * @param value - Enable or disable daisy-chain mode
319 Example : 'AD5686_DC_EN' - daisy-chain enable
320 * 'AD5686_DC_DIS' - daisy-chain disable
321 *
322 * @return None.
323 ******************************************************************************/
324 void AD5686_DaisyChainEn(unsigned char value)
325 {
326 AD5686_SetShiftReg(AD5686_CTRL_DCEN, 0, value);
327 }
328
329 /**************************************************************************//**
330 * @brief Set up readback register (readback enable)
331 *
332 * @param value - Enable or disable daisy-chain mode
333 Example : 'AD5686_RB_EN' - daisy-chain enable
334 * 'AD5686_RB_DIS' - daisy-chain disable
335 *
336 * @return None.
337 ******************************************************************************/
338 void AD5686_ReadBackEn(unsigned char value)
339 {
340 AD5686_SetShiftReg(AD5686_CTRL_RB_REG, 0, value);
341 }