Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD5446/ad5446.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5446.c
3 * @brief Implementation of AD5446 Driver. This driver supporting the
4 * following devices: AD5553, AD5543, AD5542A, AD5541A, AD5512A,
5 * AD5453, AD5452, AD5451, AD5450, AD5446, AD5444
6 * @author Istvan Csomortani ([email protected])
7 *******************************************************************************
8 * Copyright 2013(c) Analog Devices, Inc.
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification,
14 * are permitted provided that the following conditions are met:
15 * - Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 * - Neither the name of Analog Devices, Inc. nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 * - The use of this software may or may not infringe the patent rights
25 * of one or more patent holders. This license does not release you
26 * from the requirement that you obtain separate licenses from these
27 * patent holders to use this software.
28 * - Use of the software either in source or binary form, must be run
29 * on or directly connected to an Analog Devices Inc. component.
30 *
31 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * DAMAGE.
42 *
43 ******************************************************************************/
44
45 /*****************************************************************************/
46 /***************************** Include Files *********************************/
47 /*****************************************************************************/
48 #include "AD5446.h"
49 #include "Communication.h"
50
51 #define MAX_RESOLUTION 16 /* Maximum resolution of supported devices */
52 #define DATA_MASK(x) (0xFFFF >> (MAX_RESOLUTION - (x)))
53 #define DATA_OFFSET(x) ((MAX_RESOLUTION - 2) - (x))
54 #define MSB_MASK 0xFF00
55 #define MSB_OFFSET 8
56 #define LSB_MASK 0x00FF
57 #define LSB_OFFSET 0
58 #define CMD_MASK 0x3
59 #define CMD_OFFSET 15
60 #define PKT_LENGTH 2 /* SPI packet length in byte */
61
62 /*****************************************************************************/
63 /***************************** Variable definition ***************************/
64 /*****************************************************************************/
65 /* Custom type for active clock edge */
66 typedef enum
67 {
68 negedge,
69 posedge
70 } active_clk_t;
71
72 /* Custom boolean type */
73 typedef enum
74 {
75 false,
76 true
77 } bool_t;
78
79 /* Data structure for chip's attributes */
80 struct ad5446_chip_info {
81 unsigned char resolution;
82 active_clk_t data_clock_in;
83 bool_t has_ctrl;
84 };
85
86 /* Device 'table' */
87 static const struct ad5446_chip_info chip_info[] = {
88 [ID_AD5553] = {
89 .resolution = 14,
90 .data_clock_in = posedge,
91 .has_ctrl = false,
92 },
93 [ID_AD5543] = {
94 .resolution = 16,
95 .data_clock_in = posedge,
96 .has_ctrl = false,
97 },
98 [ID_AD5542A] = {
99 .resolution = 16,
100 .data_clock_in = posedge,
101 .has_ctrl = false,
102 },
103 [ID_AD5541A] = {
104 .resolution = 16,
105 .data_clock_in = posedge,
106 .has_ctrl = false,
107 },
108 [ID_AD5512A] = {
109 .resolution = 12,
110 .data_clock_in = posedge,
111 .has_ctrl = false,
112 },
113 [ID_AD5453] = {
114 .resolution = 14,
115 .data_clock_in = negedge,
116 .has_ctrl = true,
117 },
118 [ID_AD5452] = {
119 .resolution = 12,
120 .data_clock_in = negedge,
121 .has_ctrl = true,
122 },
123 [ID_AD5451] = {
124 .resolution = 10,
125 .data_clock_in = negedge,
126 .has_ctrl = true,
127 },
128 [ID_AD5450] = {
129 .resolution = 8,
130 .data_clock_in = negedge,
131 .has_ctrl = true,
132 },
133 [ID_AD5446] = {
134 .resolution = 14,
135 .data_clock_in = negedge,
136 .has_ctrl = true,
137 },
138 [ID_AD5444] = {
139 .resolution = 12,
140 .data_clock_in = negedge,
141 .has_ctrl = true,
142 }
143 };
144
145 /* Actual device type */
146 AD5446_type_t act_device;
147
148 /**************************************************************************//**
149 * @brief Initialize SPI and Initial Values for AD5446 Board.
150 *
151 * @param None.
152 *
153 * @return retValue - Result of the initialization.
154 * Example: 0 - if initialization was successful;
155 * -1 - if initialization was unsuccessful.
156 ******************************************************************************/
157 char AD5446_Init(AD5446_type_t device)
158 {
159 char status = -1;
160
161 act_device = device;
162
163 /* Initialize SPI communication. */
164 if(chip_info[act_device].data_clock_in == posedge)
165 {
166 status = SPI_Init(0, 1000000, 1, 0);
167 }
168 else
169 {
170 status = SPI_Init(0, 1000000, 0, 0);
171 }
172
173 /* Initialize configuration pins, if exist. */
174 if(act_device == ID_AD5542A)
175 {
176 AD5446_LDAC_OUT;
177 AD5446_LDAC_LOW;
178 AD5446_CLR_OUT;
179 AD5446_CLR_HIGH;
180 }
181 else if(act_device == ID_AD5541A)
182 {
183 AD5446_LDAC_OUT;
184 AD5446_LDAC_LOW;
185 }
186 else if(act_device == ID_AD5446) /* Enable the SDO line */
187 {
188 /* AD5446_CLR is mapped to GPIO0 */
189 AD5446_CLR_OUT;
190 AD5446_CLR_LOW;
191 }
192
193 return status;
194 }
195 /**************************************************************************//**
196 * @brief Writes to input register via SPI.
197 *
198 * @param data - data to be written in input register.
199 *
200 * @return None.
201 ******************************************************************************/
202 void AD5446_SetRegister(unsigned char command,
203 unsigned short data)
204 {
205 unsigned short inputShiftReg = 0;
206 unsigned char spiData[PKT_LENGTH] = {0, 0};
207
208 if(chip_info[act_device].has_ctrl == true)
209 {
210 inputShiftReg = ((command & CMD_MASK) << CMD_OFFSET) |
211 ( data & DATA_MASK(chip_info[act_device].resolution) \
212 << DATA_OFFSET(chip_info[act_device].resolution));
213 spiData[0] = (inputShiftReg & MSB_MASK) >> MSB_OFFSET;
214 spiData[1] = (inputShiftReg & LSB_MASK) >> LSB_OFFSET;
215 }
216 else
217 {
218 inputShiftReg = data & (DATA_MASK(chip_info[act_device].resolution));
219 spiData[0] = (inputShiftReg & MSB_MASK) >> MSB_OFFSET;
220 spiData[1] = (inputShiftReg & LSB_MASK);
221 }
222 SPI_Write((unsigned char)AD5446_SLAVE_ID, spiData, PKT_LENGTH);
223 }
224 /***************************************************************************//**
225 * @brief Sets the output voltage.
226 *
227 * @param outVoltage - The voltage value in volts
228 * @param vref - The voltage reference used by the device in volts.
229 *
230 * @return Actual voltage that the device can output.
231 *******************************************************************************/
232 float AD5446_SetVoltage(float voltage, float vref, vout_type_t vout_type)
233 {
234 unsigned short registerValue = 0;
235 float actualVout = 0;
236 float code = 0;
237 unsigned short max_value = DATA_MASK(chip_info[act_device].resolution);
238
239 /* Get raw data from the user's desired voltage value. */
240 switch(vout_type)
241 {
242 case unipolar :
243 {
244 code = (voltage * max_value) / vref;
245 break;
246 }
247 case unipolar_inv :
248 {
249 code = (-1) * (voltage * max_value) / vref;
250 break;
251 }
252 case bipolar :
253 {
254 code = ((voltage + vref) * (max_value/2)) / vref;
255 break;
256 }
257 }
258
259 /* Round to the nearest integer. */
260 registerValue = (unsigned short)code;
261
262 /* Check to value which will be written to register. */
263 if (registerValue > (max_value - 1) )
264 {
265 registerValue = (max_value - 1);
266 }
267
268 /* Write to DAC register. */
269 AD5446_SetRegister(0, registerValue);
270 /* Calculate the output voltage value. */
271 switch(vout_type)
272 {
273 case unipolar :
274 {
275 actualVout = ((float)registerValue / max_value) * vref;
276 break;
277 }
278 case unipolar_inv :
279 {
280 actualVout = (-1) * ((float)registerValue / max_value) * vref;
281 break;
282 }
283 case bipolar :
284 {
285 actualVout = (vref * (float)registerValue / (max_value/2)) - vref;
286 break;
287 }
288 }
289
290 return actualVout;
291 }