Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodIA/AD5933.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5933.c
3 * @brief AD5933 Driver.
4 * @author ATofan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(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 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /*****************************************************************************/
44 /***************************** Include Files *********************************/
45 /*****************************************************************************/
46 #include <stdio.h>
47 #include <math.h>
48 #include "system_config.h"
49 #include "AD5933.h"
50
51 volatile int rxData;
52 int temperature = 0;
53 unsigned long impedanceKohms = 0;
54 unsigned long impedanceOhms = 0;
55 float impedance = 0.0f;
56 float gainFactor = 0.0f;
57
58
59 /******************************************************************************
60 * @brief Set an AD5933 internal register value.
61 *
62 * @param registerAddress - Address of AD5933 register.
63 *
64 * @param registerValue - Value of data to be written in the register.
65 *
66 * @param numberOfBytes - Number of bytes to be written in the register
67 *
68 * @return None.
69 ******************************************************************************/
70 void AD5933_SetRegisterValue(int registerAddress,
71 int registerValue,
72 char numberOfBytes)
73 {
74 char byte = 0;
75 unsigned char writeData[4] = {0, 0, 0, 0};
76 // Write numberOfBytes from starting address to starting address + numberOfBytes
77 for(byte = 0; byte < numberOfBytes; byte++)
78 {
79 writeData[0] = registerAddress + numberOfBytes - byte - 1;
80 writeData[1] = ((registerValue >> (8*byte)) & 0xFF);
81 I2C_Write_axi(I2C_BASEADDR, AD5933_I2C_ADDR, -1, 2, writeData);
82 }
83 }
84
85 /******************************************************************************
86 * @brief Read an AD5933 internal register value.
87 *
88 * @param registerAddress - Address of AD5933 register.
89 *
90 * @param numberOfBytes - Number of bytes to be read from the register.
91 *
92 * @return Register value.
93 ******************************************************************************/
94 int AD5933_GetRegisterValue(int registerAddress,
95 char numberOfBytes)
96 {
97 int registerValue = 0;
98 char byte = 0;
99 unsigned char writeData[4] = {0, 0, 0, 0};
100 unsigned char readData[4] = {0, 0, 0, 0};
101
102 for(byte = 0; byte < numberOfBytes; byte++)
103 {
104 // Set pointer to desired register
105 writeData[0] = AD5933_ADDR_POINTER;
106 writeData[1] = registerAddress + byte;
107 I2C_Write_axi(I2C_BASEADDR, AD5933_I2C_ADDR, -1, 2, writeData);
108
109 // Read register data
110 I2C_Read_axi(I2C_BASEADDR, AD5933_I2C_ADDR, -1, 1, readData);
111 registerValue |= readData[0] << (8 * (numberOfBytes - byte - 1));
112 }
113 return(registerValue);
114 }
115
116 /******************************************************************************
117 * @brief Read the AD5933 temperature.
118 *
119 * @param None.
120 *
121 * @return Temperature value.
122 ******************************************************************************/
123 int AD5933_GetTemperature(void)
124 {
125 int temperature = 0;
126 int status = 0;
127
128 // Enable temperature measurement
129 AD5933_SetRegisterValue(AD5933_CONTROL_REG_HB,
130 AD5933_CONTROL_FUNCTION(AD5933_MEAS_TEMP),
131 1);
132 // Wait for read temperature to be valid
133 while((status & 1) == 0)
134 {
135 status = AD5933_GetRegisterValue(AD5933_STATUS_REG, 1);
136 }
137
138 // Read correct temperature value
139 temperature = AD5933_GetRegisterValue(AD5933_TEMP_REG_HB, 2);
140
141 // Calculate temperature according to datasheet specifications
142 if(temperature < 8192)
143 {
144 temperature /= 32;
145 }
146 else
147 {
148 temperature -= 16384;
149 temperature /= 32;
150 }
151
152 return(temperature);
153 }
154
155 /******************************************************************************
156 * @brief Configure the AD5933 frequency sweep parameters.
157 *
158 * @param startFreq - Starting frequency value.
159 *
160 * @param incSteps - Number of increment steps.
161 *
162 * @param incFreq - Frequency step value.
163 *
164 * @return None.
165 ******************************************************************************/
166 void AD5933_ConfigSweep(int startFreq,
167 int incSteps,
168 int incFreq)
169 {
170 // Configure starting frequency
171 AD5933_SetRegisterValue(AD5933_START_FREQ_REG_HB,
172 startFreq,
173 3);
174 // Configure number of steps
175 AD5933_SetRegisterValue(AD5933_NR_INCR_REG_HB,
176 incSteps,
177 2);
178 // Configure frequency increment step
179 AD5933_SetRegisterValue(AD5933_FREQ_INCR_REG_HB,
180 incFreq,
181 3);
182 }
183
184 /******************************************************************************
185 * @brief Start AD5933 frequency sweep.
186 *
187 * @param None.
188 *
189 * @return None.
190 ******************************************************************************/
191 void AD5933_StartSweep(void)
192 {
193 int status = 0;
194
195 // Place AD5933 in standby
196 AD5933_SetRegisterValue(AD5933_CONTROL_REG_HB,
197 AD5933_CONTROL_FUNCTION(AD5933_STANDBY),
198 1);
199
200 // Select internal system clock
201 AD5933_SetRegisterValue(AD5933_CONTROL_REG_LB,
202 0x00,
203 1);
204
205 // Initialize starting frequency
206 AD5933_SetRegisterValue(AD5933_CONTROL_REG_HB,
207 AD5933_CONTROL_FUNCTION(AD5933_INIT_START_FREQ),
208 1);
209
210 // Start frequency sweep
211 AD5933_SetRegisterValue(AD5933_CONTROL_REG_HB,
212 AD5933_CONTROL_FUNCTION(AD5933_START_FREQ_SWEEP),
213 1);
214
215 // Wait for data to be valid
216 while((status & AD5933_STATUS_DATA_VALID) == 0)
217 {
218 status = AD5933_GetRegisterValue(AD5933_STATUS_REG,1);
219 }
220 }
221
222 /******************************************************************************
223 * @brief Calculate gain factor
224 *
225 * @param calibrationImpedance - Known value of connected impedance for calibration.
226 *
227 * @param freqFunction - Select Repeat Frequency Sweep.
228 *
229 * @return gainFactor.
230 ******************************************************************************/
231 double AD5933_CalculateGainFactor(unsigned long calibrationImpedance,
232 char freqFunction)
233 {
234 double gainFactor = 0;
235 double magnitude = 0;
236 int status = 0;
237 signed short realData = 0;
238 signed short imgData = 0;
239
240 // Repeat frequency sweep with last set parameters
241 AD5933_SetRegisterValue(AD5933_CONTROL_REG_HB,
242 AD5933_CONTROL_FUNCTION(freqFunction),
243 1);
244
245 // Wait for data received to be valid
246 while((status & AD5933_STATUS_DATA_VALID) == 0)
247 {
248 status = AD5933_GetRegisterValue(AD5933_STATUS_REG, 1);
249 }
250
251 // Read real and imaginary data
252 realData = AD5933_GetRegisterValue(AD5933_REAL_REG_HB,2);
253 imgData = AD5933_GetRegisterValue(AD5933_IMG_REG_HB,2);
254
255 // Calculate magnitude
256 magnitude = sqrtf((realData * realData) + (imgData * imgData));
257
258 // Calculate gain factor
259 gainFactor = 1000000000 / (magnitude * calibrationImpedance);
260
261 return(gainFactor);
262 }
263
264 /******************************************************************************
265 * @brief Calculate impedance.
266 *
267 * @param gainFactor - Gain factor calculated using a known impedance.
268 *
269 * @param freqFunction - Select Repeat Frequency Sweep.
270 *
271 * @return impedance.
272 ******************************************************************************/
273 double AD5933_CalculateImpedance(double gainFactor,
274 char freqFunction)
275 {
276 signed short realData = 0;
277 signed short imgData = 0;
278 double magnitude = 0;
279 double impedance = 0;
280 int status = 0;
281
282 // Repeat frequency sweep with last set parameters
283 AD5933_SetRegisterValue(AD5933_CONTROL_REG_HB,
284 AD5933_CONTROL_FUNCTION(freqFunction),
285 1);
286
287 // Wait for data received to be valid
288 while((status & AD5933_STATUS_DATA_VALID) == 0)
289 {
290 status = AD5933_GetRegisterValue(AD5933_STATUS_REG,1);
291 }
292
293 // Read real and imaginary data
294 realData = AD5933_GetRegisterValue(AD5933_REAL_REG_HB,2);
295 imgData = AD5933_GetRegisterValue(AD5933_IMG_REG_HB,2);
296
297 // Calculate magnitude
298 magnitude = sqrtf((realData * realData) + (imgData * imgData));
299
300 // Calculate impedance
301 impedance = 1 / (magnitude * gainFactor / 1000000000);
302
303 return(impedance);
304 }
305
306 /******************************************************************************
307 * @brief Demo program for AD5933
308 *
309 * @param none.
310 *
311 * @return none.
312 ******************************************************************************/
313 void DemoProgram(void)
314 {
315 // Read temperature from device
316 temperature = AD5933_GetTemperature();
317 xil_printf("\n\rTemperature: %d C\n\r",temperature);
318
319 // Set sweep parameters
320 AD5933_ConfigSweep(0x0F5C28,
321 0x01F4,
322 0x000D16);
323
324 // Start the sweep
325 AD5933_StartSweep();
326
327 // Calculate gain factor for an impedance of 47kohms
328 gainFactor = AD5933_CalculateGainFactor(47000,
329 AD5933_REPEAT_FREQ);
330
331 // Change the resistor used for calibration with the one you wish to measure
332 xil_printf("Replace calibration component with desired one for measurement and press any key\n\r");
333
334 UartReadChar();
335
336 // Calculate impedance between Vout and Vin
337 impedance = AD5933_CalculateImpedance(gainFactor,
338 AD5933_REPEAT_FREQ);
339 impedanceOhms = (unsigned long)impedance;
340
341 // Print impedance
342 xil_printf("Impedance read: %d ohms\n\r", impedanceOhms);
343
344 // Calculate impedance in kohm
345 impedanceKohms = impedanceOhms / 1000;
346 impedanceOhms %= 1000;
347
348 // Print impedance in kohm
349 if (impedanceOhms < 10)
350 {
351 xil_printf("Impedance read: %d.00%d Kohms\n\r", impedanceKohms, impedanceOhms);
352 }
353 else if (impedanceOhms < 100)
354 {
355 xil_printf("Impedance read: %d.0%d Kohms\n\r", impedanceKohms, impedanceOhms);
356 }
357 else
358 {
359 xil_printf("Impedance read: %d.%d Kohms\n\r", impedanceKohms, impedanceOhms);
360 }
361
362 xil_printf("Exiting application.\n\r");
363 }