Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD5629R/ad5629r.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ad5629r.c
3 * @brief Implementation of ad5629r Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without 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 IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 ******************************************************************************/
41
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "ad5629r.h"
47
48 /******************************************************************************/
49 /************************ Variables Definitions *******************************/
50 /******************************************************************************/
51 typedef enum
52 {
53 com_spi,
54 com_i2c
55 } comm_type_t;
56
57 struct ad5629r_chip_info {
58 unsigned int resolution;
59 comm_type_t communication;
60 };
61
62 static const struct ad5629r_chip_info ad5629r_chip_info[] = {
63 [ID_AD5629R] = {
64 .resolution = 12,
65 .communication = com_i2c,
66 },
67 [ID_AD5669R] = {
68 .resolution = 16,
69 .communication = com_i2c,
70 },
71 [ID_AD5668] = {
72 .resolution = 16,
73 .communication = com_spi,
74 },
75 [ID_AD5648] = {
76 .resolution = 14,
77 .communication = com_spi,
78 },
79 [ID_AD5628] = {
80 .resolution = 12,
81 .communication = com_spi,
82 }
83 };
84
85 AD5629R_type act_device;
86
87 unsigned char ad5629rDevAddr = 0;
88 unsigned char bitsNumber = 0;
89
90 /******************************************************************************/
91 /************************** Functions Definitions *****************************/
92 /******************************************************************************/
93
94
95 /***************************************************************************//**
96 * @brief Initializes the communication with the device.
97 *
98 * @param devAddr - AD5629R device address.
99 * Example: AD5629R_I2C_ADDR_0 - A0_Pin=High;
100 * AD5629R_I2C_ADDR_1 - A0_Pin=NC;
101 * AD5629R_I2C_ADDR_2 - A0_Pin=Low.
102 * device - supported devices.
103 * Example: AD5629R, AD5669R, AD5668, AD5648, AD5628.
104 * @return status - Result of the initialization procedure.
105 * Example: 0 - if initialization was successful;
106 * -1 - if initialization was unsuccessful.
107 *******************************************************************************/
108 char AD5629R_Init(unsigned char devAddr, AD5629R_type device)
109 {
110 char status = -1;
111
112 ad5629rDevAddr = devAddr;
113 act_device = device;
114
115
116 if (ad5629r_chip_info[act_device].communication == com_spi)
117 {
118 status = SPI_Init(0, 1000000, 1, 1);
119 }
120 else
121 {
122 if (ad5629r_chip_info[act_device].communication == com_i2c)
123 {
124 status = I2C_Init(100000);
125 }
126 }
127
128 AD5629R_LDAC_OUT;
129 AD5629R_LDAC_LOW;
130 AD5629R_CLR_OUT;
131 AD5629R_CLR_HIGH;
132
133 return status;
134 }
135 /**************************************************************************//**
136 * @brief Write to input register and read from output register via SPI.
137 *
138 * @param function - command control bits;
139 * dacN - address of selected DAC;
140 * data - data to be written in register.
141 *
142 * @return readBack - value read from register.
143 ******************************************************************************/
144 void AD5629R_SetCtrl(unsigned char function,
145 unsigned char dacN,
146 unsigned long data)
147 {
148 unsigned char dataBuff[4] = {0, 0, 0, 0};
149
150 if(ad5629r_chip_info[act_device].communication == com_spi)
151 {
152 data = data & 0xFFFFF;
153
154 dataBuff[0] = function;
155 dataBuff[1] = (dacN << 4) | ((0xF0000 & data) >> 12);
156 dataBuff[2] = (0xFF00 & data) >> 8;
157 dataBuff[3] = (0xFF & data);
158
159 SPI_Write(AD5629R_SLAVE_ID, dataBuff, 4);
160 }
161 else
162 {
163 if (ad5629r_chip_info[act_device].communication == com_i2c)
164 {
165 dataBuff[0] = (function << 4) | dacN;
166 dataBuff[1] = (data & 0xFF00) >> 8;
167 dataBuff[2] = (data & 0x00FF) >> 0;
168
169 I2C_Write(AD5629R_I2C_ADDR_2,
170 dataBuff,
171 3,
172 1);
173 }
174 }
175 }
176
177 /**************************************************************************//**
178 * @brief Write to input register and read from output register via SPI.
179 *
180 * @param function - command control bits.
181 * dacN - address of selected DAC;
182 * dacValue - data to be written in input register.
183 *
184 * @return readBack - value read from register.
185 ******************************************************************************/
186 void AD5629R_SetInputReg(unsigned char function,
187 unsigned char dacN,
188 unsigned short dacValue)
189 {
190 unsigned char dataBuff[4] = {0, 0, 0, 0};
191
192 dacValue = dacValue << (MAX_RESOLUTION -
193 ad5629r_chip_info[act_device].resolution);
194
195 if(ad5629r_chip_info[act_device].communication == com_spi)
196 {
197 dacValue = dacValue & 0xFFFF;
198
199 dataBuff[0] = function;
200 dataBuff[1] = (dacN << 4) | ((0xF000 & dacValue) >> 12);
201 dataBuff[2] = (0xFF0 & dacValue) >> 4;
202 dataBuff[3] = (0xF & dacValue) << 4;
203
204 SPI_Write(AD5629R_SLAVE_ID, dataBuff, 4);
205 }
206 else
207 {
208 dataBuff[0] = (function << 4) | dacN;
209 dataBuff[1] = (dacValue & 0xFF00) >> 8;
210 dataBuff[2] = (dacValue & 0x00FF) >> 0;
211
212 I2C_Write(AD5629R_I2C_ADDR_2,
213 dataBuff,
214 3,
215 1);
216 }
217 }
218
219 /***************************************************************************//**
220 * @brief Writes a value to Input Register N of selected DAC channel.
221 *
222 * @param dacValue - Value to be written in register;
223 * dacN - Address of selected DAC.
224 *
225 * @return none.
226 *******************************************************************************/
227 void AD5629R_WriteRegN(unsigned char dacN, unsigned short dacValue)
228 {
229 AD5629R_SetInputReg(AD5629R_WRITE_N, dacN, dacValue);
230 }
231
232 /***************************************************************************//**
233 * @brief Updates selected DAC register.
234 *
235 * @param dacN - Address of selected DAC.
236 *
237 * @return none.
238 *******************************************************************************/
239 void AD5629R_UpdateDacN(unsigned char dacN)
240 {
241 AD5629R_SetInputReg(AD5629R_UPDATE_N, dacN, 0x0);
242 }
243
244 /***************************************************************************//**
245 * @brief Writes a value to Input Register N of selected DAC channel, then
246 * updates all.
247 *
248 * @param dacValue - Value to be written in register;
249 * dacN - Address of selected DAC.
250 *
251 * @return none.
252 *******************************************************************************/
253 void AD5629R_WriteRegNUpdateAll(unsigned char dacN, unsigned short dacValue)
254 {
255 AD5629R_SetInputReg(AD5629R_WRITE_N_UPDATE_ALL, dacN, dacValue);
256 }
257
258 /***************************************************************************//**
259 * @brief Writes a value to Input Register N and updates the respective DAC
260 * channel.
261 *
262 * @param dacValue - Value to be written in register;
263 * dacN - Address of selected DAC.
264 *
265 * @return none.
266 *******************************************************************************/
267 void AD5629R_WriteRegNUpdateN(unsigned char dacN, unsigned short dacValue)
268 {
269 AD5629R_SetInputReg(AD5629R_WRITE_N_UPDATE_N, dacN, dacValue);
270 }
271
272 /***************************************************************************//**
273 * @brief Sets the power mode for one or more selected DAC channels.
274 *
275 * @param dacSel - a byte where each bit is corresponding to a DAC; when a bit is
276 * set to 1, the corresponding DAC is selected.
277 * Example: DAC_A_SEL - the selected DAC is DAC A;
278 * DAC_D_SEL | DAC_F_SEL | DAC_H_SEL - the selected DACs
279 * are: DAC D, DAC F and DAC H.
280 * mode - the desired power mode to be set.
281 * Example: PWR_NORMAL - normal operation;
282 * PWR_1K_TO_GND - 1KOhm to GND;
283 * PWR_100K_TO_GND - 100KOhms to GND;
284 * PWR_3_STATE - three-state.
285 *
286 * @return none.
287 *******************************************************************************/
288 void AD5629R_SetPowerMode(unsigned char dacSel, unsigned char mode)
289 {
290 unsigned long data = 0;
291
292 data = (mode << 8) | dacSel;
293
294 AD5629R_SetCtrl(AD5629R_POWER, 0x0, data);
295 }
296
297 /***************************************************************************//**
298 * @brief Loads the Clear Code Register with a certain value.
299 *
300 * @param clearValue - the value to be set in all DAC registers after a clear
301 * operation.
302 * Example: CLR_TO_ZEROSCALE - clears to 0x0;
303 * CLR_TO_MIDSCALE - clears to 0x8000;
304 * CLR_TO_FULLSCALE - clears to 0xFFFF;
305 * CLR_NOOP - no operation.
306 *
307 * @return none.
308 *******************************************************************************/
309 void AD5629R_LoadClearCodeReg(unsigned char clearValue)
310 {
311 unsigned long data = 0;
312
313 data = (unsigned long)clearValue;
314
315 AD5629R_SetCtrl(AD5629R_LOAD_CLEAR_REG, 0x0, data);
316 }
317
318 /***************************************************************************//**
319 * @brief Loads the LDAC register with a certain value.
320 *
321 * @param dacSel - a byte where each bit is corresponding to a DAC; when a bit is
322 * set to 1, the corresponding DAC is selected to override LDAC
323 * pin.
324 * Example: DAC_A_SEL - the selected DAC to override the LDAC pin
325 * is DAC A;
326 * DAC_D_SEL | DAC_F_SEL | DAC_H_SEL - the selected DACs
327 * to override the LDAC pin are: DAC D, DAC F and DAC H.
328 *
329 * @return none.
330 *******************************************************************************/
331 void AD5629R_LoadLdacReg(unsigned char dacSel)
332 {
333 unsigned long data = 0;
334
335 data = (unsigned long)dacSel;
336
337 AD5629R_SetCtrl(AD5629R_LOAD_LDAC_REG, 0x0, data);
338 }
339
340 /***************************************************************************//**
341 * @brief Makes a power-on reset.
342 *
343 * @param none.
344 *
345 * @return none.
346 *******************************************************************************/
347 void AD5629R_Reset(void)
348 {
349 AD5629R_SetCtrl(AD5629R_RESET, 0x0, 0x0);
350 }
351
352 /***************************************************************************//**
353 * @brief Turns on/off the internal reference.
354 *
355 * @param status - the status of internal reference.
356 * Example: REF_ON - the reference is on;
357 * REF_OFF - the reference is off.
358 *
359 * @return none.
360 *******************************************************************************/
361 void AD5629R_SetRef(unsigned char status)
362 {
363 AD5629R_SetCtrl(AD5629R_REFERENCE, 0x0, (unsigned long)status);
364 }
365
366