Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD5449/ad5449.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5449.c
3 * @brief Implementation of AD5449 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 "AD5449.h"
47 #include "Communication.h"
48
49 #define MAX_RESOLUTION 12 /* Maximum resolution of the supported devices */
50 #define DATA_MASK 0xFFF /* Mask for 16 data bits */
51 #define CMD_MASK 0xFF /* Mask for Command bits */
52 #define CMD_OFFSET 12 /* Offset for Command */
53 #define MSB_MASK 0xFF00 /* Most significant byte of the data word */
54 #define MSB_OFFSET 8
55 #define LSB_MASK 0x00FF /* Least significant byte of the data word */
56 #define LSB_OFFSET 0
57 #define PKT_LENGTH 2 /* SPI packet length in byte */
58
59 #define TWO_BIT_MASK 0x3
60 /*****************************************************************************/
61 /***************************** Variable definition ***************************/
62 /*****************************************************************************/
63
64 struct ad5449_chip_info {
65 unsigned char num_channels;
66 unsigned char resolution;
67 bool_t has_ctrl;
68 };
69
70 static const struct ad5449_chip_info ad5449_chip_info[] = {
71 [ID_AD5415] = {
72 .num_channels = 2,
73 .resolution = 12,
74 .has_ctrl = true,
75 },
76 [ID_AD5426] = {
77 .num_channels = 1,
78 .resolution = 8,
79 .has_ctrl = false,
80 },
81 [ID_AD5429] = {
82 .num_channels = 2,
83 .resolution = 8,
84 .has_ctrl = true,
85 },
86 [ID_AD5432] = {
87 .num_channels = 1,
88 .resolution = 10,
89 .has_ctrl = false,
90 },
91 [ID_AD5439] = {
92 .num_channels = 2,
93 .resolution = 10,
94 .has_ctrl = true,
95 },
96 [ID_AD5443] = {
97 .num_channels = 1,
98 .resolution = 12,
99 .has_ctrl = false,
100 },
101 [ID_AD5449] = {
102 .num_channels = 2,
103 .resolution = 12,
104 .has_ctrl = true,
105 }
106 };
107
108 AD5449_type_t act_device;
109 unsigned short controlReg;
110
111 /**************************************************************************//**
112 * @brief Initialize SPI and Initial Values for AD5449 Board.
113 *
114 * @param device - Type of the desired device.
115 * Example: ID_AD5415, ID_AD5426, ID_AD5429, ID_AD5432,
116 * ID_AD5439, ID_AD5443, ID_AD5449,
117 * @return retValue - Result of the initialization.
118 * Example: 0 - if initialization was successful;
119 * -1 - if initialization was unsuccessful.
120 ******************************************************************************/
121 char AD5449_Init(AD5449_type_t device)
122 {
123 char status = -1;
124
125 act_device = device;
126
127 /* Initialize SPI communication. */
128 status = SPI_Init(0, 1000000, 0, 0);
129
130 /* Set GPIO pins. */
131 AD5449_LDAC_OUT;
132 AD5449_LDAC_LOW;
133 AD5449_CLR_OUT;
134 AD5449_CLR_HIGH;
135
136 /* Initialize device */
137 controlReg = (AD5449_SDO_FULL << AD5449_SDO_BIT) | \
138 (AD5449_DAISY_CHAIN_DIS << AD5449_DSY_BIT) | \
139 (AD5449_ZERO_SCALE << AD5449_HCLR_BIT) | \
140 (AD5449_CLOCK_NEGEDGE << AD5449_SCLK_BIT);
141 if(ad5449_chip_info[act_device].has_ctrl)
142 {
143 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
144 }
145 else
146 {
147 AD5449_DaisyChainSetup(AD5449_DAISY_CHAIN_DIS);
148 }
149
150 return status;
151 }
152
153 /**************************************************************************//**
154 * @brief Write to input register and read from output register via SPI.
155 *
156 * @param command - command control bits.
157 * data - data to be written in input register.
158 *
159 * @return readBack - value read from register.
160 ******************************************************************************/
161 unsigned short AD5449_SetInputShiftReg(unsigned short command,
162 unsigned short data)
163 {
164 unsigned short inputShiftReg = 0;
165 unsigned short readBack = 0;
166 unsigned char spiData[PKT_LENGTH] = {0, 0};
167 unsigned char data_offset = MAX_RESOLUTION - \
168 ad5449_chip_info[act_device].resolution;
169
170 if(command == AD5449_CTRL_REG)
171 {
172 inputShiftReg = ((command & CMD_MASK) << CMD_OFFSET) | \
173 (data & DATA_MASK);
174 }
175 else
176 {
177 inputShiftReg = ((command & CMD_MASK) << CMD_OFFSET) | \
178 ((data & DATA_MASK) << data_offset);
179 }
180
181 spiData[0] = (inputShiftReg & MSB_MASK) >> MSB_OFFSET;
182 spiData[1] = (inputShiftReg & LSB_MASK) >> LSB_OFFSET;
183
184 SPI_Read((unsigned char)AD5449_SLAVE_ID, spiData, PKT_LENGTH);
185 readBack = ((unsigned short)spiData[0] << MSB_OFFSET) | \
186 ((unsigned short)spiData[1] << LSB_OFFSET);
187
188 return readBack;
189 }
190
191 /**************************************************************************//**
192 * @brief Loads and updates the selected DAC with a given value.
193 *
194 * @param channel - the chosen channel to write to.
195 * Example: AD5449_CH_A = 0;
196 * AD5449_CH_B = 1.
197 * dacValue - desired value to be written in register.
198 *
199 * @return None.
200 ******************************************************************************/
201 void AD5449_LoadUpdateChannel(unsigned char channel, unsigned short dacValue)
202 {
203 if(ad5449_chip_info[act_device].num_channels > 1)
204 {
205 AD5449_SetInputShiftReg(AD5449_CTRL_LOADUPDATE(channel), dacValue);
206 }
207 else
208 {
209 AD5449_SetInputShiftReg(AD5449_CTRL_LOADUPDATE(AD5449_CH_A), dacValue);
210 }
211 }
212
213 /**************************************************************************//**
214 * @brief Load selected DAC input register with a given value.
215 *
216 * @param channel - the chosen channel to write to.
217 * Example: AD5449_CH_A = 0;
218 * AD5449_CH_B = 1.
219 * dacValue - desired value to be written in register.
220 *
221 * @return None.
222 ******************************************************************************/
223 void AD5449_LoadChannel(unsigned char channel, unsigned short dacValue)
224 {
225 if(ad5449_chip_info[act_device].num_channels > 1)
226 {
227 AD5449_SetInputShiftReg(AD5449_CTRL_LOAD(channel), dacValue);
228 }
229 else
230 {
231 AD5449_SetInputShiftReg(AD5449_CTRL_LOAD(AD5449_CH_A), dacValue);
232 }
233 }
234
235 /**************************************************************************//**
236 * @brief Read from the selected DAC register.
237 *
238 * @param channel - the chosen channel to read from.
239 * Example: AD5449_CH_A = 0;
240 * AD5449_CH_B = 1.
241 *
242 * @return dacValue - value read from the register.
243 ******************************************************************************/
244 unsigned short AD5449_ReadbackChannel(unsigned char channel)
245 {
246 unsigned short dacValue = 0;
247
248 if(ad5449_chip_info[act_device].num_channels > 1)
249 {
250 AD5449_SetInputShiftReg(AD5449_CTRL_READBACK(channel), 0x0);
251 dacValue = AD5449_SetInputShiftReg(AD5449_CTRL_NOP, 0x0) & DATA_MASK;
252 }
253 else
254 {
255 AD5449_SetInputShiftReg(AD5449_CTRL_READBACK(AD5449_CH_A), 0x0);
256 dacValue = AD5449_SetInputShiftReg(AD5449_CTRL_NOP, 0x0) & DATA_MASK;
257 }
258 return dacValue;
259 }
260
261 /**************************************************************************//**
262 * @brief Update the DAC outputs. This function has a physical result just with * devices with two channels
263 *
264 * @param None.
265 *
266 * @return None.
267 ******************************************************************************/
268 void AD5449_UpdateAll(void)
269 {
270 if(ad5449_chip_info[act_device].num_channels > 1)
271 {
272 AD5449_SetInputShiftReg(AD5449_CTRL_UPDATEALL, 0x0);
273 }
274 }
275
276 /**************************************************************************//**
277 * @brief Load the DAC input registers. This function has a physical result
278 * just with devices with two channels
279 *
280 * @param dacValue - desired value to be written in register.
281 *
282 * @return None.
283 ******************************************************************************/
284 void AD5449_LoadAll(short dacValue)
285 {
286 if(ad5449_chip_info[act_device].num_channels > 1)
287 {
288 AD5449_SetInputShiftReg(AD5449_CTRL_LOADALL, dacValue);
289 }
290 }
291
292 /**************************************************************************//**
293 * @brief Set up the scale where to the output will be cleared on active CLR
294 * signal
295 *
296 * @param type - target scale when making a clear command
297 * Example: ZERO_SCALE = zero scale.
298 * MIDSCALE = midscale.
299 *
300 * @return None.
301 ******************************************************************************/
302 void AD5449_ClearScaleSetup(char type)
303 {
304 if(ad5449_chip_info[act_device].has_ctrl)
305 {
306 if(type == AD5449_ZERO_SCALE)
307 {
308 /* Reset the HCLR bit in the Control Register */
309 controlReg &= ~AD5449_HCLR_MASK;
310 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
311 }
312 else if(type == AD5449_MID_SCALE)
313 {
314 /* Set the HCLR bit in the Control Register */
315 controlReg |= AD5449_HCLR_MASK;
316 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
317 }
318 }
319 else
320 {
321 if(type == AD5449_ZERO_SCALE)
322 {
323 AD5449_SetInputShiftReg(AD5449_CTRL_CLR_ZERO, 0x0);
324 }
325 else if (type == AD5449_MID_SCALE)
326 {
327 AD5449_SetInputShiftReg(AD5449_CTRL_CLR_MID, 0x0);
328 }
329 }
330 }
331
332 /**************************************************************************//**
333 * @brief Enable/disable the Daisy-Chain mode
334 *
335 * @param type - target scale when making a clear command
336 * Example: ZERO_SCALE = zero scale.
337 * MIDSCALE = midscale.
338 *
339 * @return None.
340 ******************************************************************************/
341 void AD5449_DaisyChainSetup(char value)
342 {
343 if(ad5449_chip_info[act_device].has_ctrl)
344 {
345 if(value == AD5449_DAISY_CHAIN_EN)
346 {
347 /* Set the DSY bit in the Control Register */
348 controlReg |= AD5449_DSY_MASK;
349 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
350 }
351 else if(value == AD5449_DAISY_CHAIN_DIS)
352 {
353 /* Reset the DSY bit in the Control Register */
354 controlReg &= ~AD5449_DSY_MASK;
355 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
356 }
357 }
358 else
359 {
360 /* Disable daisy chain mode */
361 if (value == AD5449_DAISY_CHAIN_DIS)
362 {
363 AD5449_SetInputShiftReg(AD5449_CTRL_DAISY_CHAIN, 0x0);
364 }
365 }
366 }
367
368 /**************************************************************************//**
369 * @brief Control the SDO output driver strength. This function is supported
370 * by the devices : AD5415, AD5429, AD5439, AD5449
371 *
372 * @param type - target scale when making a clear command
373 * Example: AD5449_SDO_FULL = Full SDO driver.
374 * AD5449_SDO_WEAK = Weak SDO driver.
375 * AD5449_SDO_OPEN_DRAIN = SDO configured as open drain
376 * AD5449_SDO_DISABLE = Disable SDO output
377 * @return None.
378 ******************************************************************************/
379 void AD5449_SDOControl(char controlBits)
380 {
381 /* Check if the current device supports this functions */
382 if(ad5449_chip_info[act_device].has_ctrl)
383 {
384 /* Reset the SDO bits of the local Control Register */
385 controlReg &= ~AD5449_SDO_MASK;
386 /* Set up the new SDO bits */
387 controlReg |= (controlBits & TWO_BIT_MASK) << AD5449_SDO_BIT;
388 /* Send the new Control Register value to the device */
389 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
390 }
391 else /* Do Nothing! */
392 {
393
394 }
395 }
396
397 /**************************************************************************//**
398 * @brief Set up the active clock edge of the SPI interface
399 *
400 * @param type - target scale when making a clear command
401 * Example: AD5449_CLOCK_NEGEDGE = Falling edge. (Default)
402 * AD5449_CLOCK_POSEDGE = Rising edge.
403 *
404 * @return None.
405 ******************************************************************************/
406 void AD5449_SCLKSetup(char value)
407 {
408 if(ad5449_chip_info[act_device].has_ctrl)
409 {
410 if(value == AD5449_CLOCK_POSEDGE)
411 {
412 /* Set the SCLK bit in the Control Register */
413 controlReg |= AD5449_SCLK_MASK;
414 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
415 }
416 else if(value == AD5449_CLOCK_NEGEDGE)
417 {
418 /* Reset the SCLK bit in the Control Register */
419 controlReg &= ~AD5449_SCLK_MASK;
420 AD5449_SetInputShiftReg(AD5449_CTRL_REG, controlReg);
421 }
422 }
423 else
424 {
425 /* Clock data on rising edge */
426 if (value == AD5449_CLOCK_POSEDGE)
427 {
428 AD5449_SetInputShiftReg(AD5449_CTRL_CLK_EDGE, 0x0);
429 }
430 }
431 }