Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/AD5933/AD5933.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD5933.c
3 * @brief Implementation of AD5933 Driver.
4 * @author DBogdan ([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 "AD5933.h"
47 #include <math.h>
48
49 /******************************************************************************/
50 /************************** Constants Definitions *****************************/
51 /******************************************************************************/
52 const long POW_2_27 = 134217728ul; // 2 to the power of 27
53
54 /******************************************************************************/
55 /************************ Variables Definitions *******************************/
56 /******************************************************************************/
57 unsigned long currentSysClk = AD5933_INTERNAL_SYS_CLK;
58 unsigned char currentClockSource = AD5933_CONTROL_INT_SYSCLK;
59 unsigned char currentGain = AD5933_GAIN_X5;
60 unsigned char currentRange = AD5933_RANGE_2000mVpp;
61
62 /******************************************************************************/
63 /************************ Functions Definitions *******************************/
64 /******************************************************************************/
65
66 /***************************************************************************//**
67 * @brief Initializes the communication peripheral.
68 *
69 * @return status - The result of the initialization procedure.
70 * Example: -1 - I2C peripheral was not initialized.
71 * 0 - I2C peripheral was initialized.
72 *******************************************************************************/
73 char AD5933_Init(void)
74 {
75 unsigned char status = -1;
76
77 status = I2C_Init(100000);
78
79 return status;
80 }
81
82 /***************************************************************************//**
83 * @brief Writes data into a register.
84 *
85 * @param registerAddress - Address of the register.
86 * @param registerValue - Data value to write.
87 * @param bytesNumber - Number of bytes.
88 *
89 * @return None.
90 *******************************************************************************/
91 void AD5933_SetRegisterValue(unsigned char registerAddress,
92 unsigned long registerValue,
93 unsigned char bytesNumber)
94 {
95 unsigned char byte = 0;
96 unsigned char writeData[2] = {0, 0};
97
98 for(byte = 0;byte < bytesNumber; byte++)
99 {
100 writeData[0] = registerAddress + bytesNumber - byte - 1;
101 writeData[1] = (unsigned char)((registerValue >> (byte * 8)) & 0xFF);
102 I2C_Write(AD5933_ADDRESS, writeData, 2, 1);
103 }
104 }
105
106 /***************************************************************************//**
107 * @brief Reads the value of a register.
108 *
109 * @param registerAddress - Address of the register.
110 * @param bytesNumber - Number of bytes.
111 *
112 * @return registerValue - Value of the register.
113 *******************************************************************************/
114 unsigned long AD5933_GetRegisterValue(unsigned char registerAddress,
115 unsigned char bytesNumber)
116 {
117 unsigned long registerValue = 0;
118 unsigned char byte = 0;
119 unsigned char writeData[2] = {0, 0};
120 unsigned char readData[2] = {0, 0};
121
122 for(byte = 0;byte < bytesNumber;byte ++)
123 {
124 /* Set the register pointer. */
125 writeData[0] = AD5933_ADDR_POINTER;
126 writeData[1] = registerAddress + byte;
127 I2C_Write(AD5933_ADDRESS, writeData, 2, 1);
128 /* Read Register Data. */
129 readData[0] = 0xFF;
130 I2C_Read(AD5933_ADDRESS, readData, 1, 1);
131 registerValue = registerValue << 8;
132 registerValue += readData[0];
133 }
134
135 return registerValue;
136 }
137
138 /***************************************************************************//**
139 * @brief Resets the device.
140 *
141 * @return None.
142 *******************************************************************************/
143 void AD5933_Reset(void)
144 {
145 AD5933_SetRegisterValue(AD5933_REG_CONTROL_LB,
146 AD5933_CONTROL_RESET | currentClockSource,
147 1);
148 }
149
150 /***************************************************************************//**
151 * @brief Selects the source of the system clock.
152 *
153 * @param clkSource - Selects the source of the system clock.
154 * Example: AD5933_CONTROL_INT_SYSCLK
155 * AD5933_CONTROL_EXT_SYSCLK
156 * @param extClkFreq - Frequency value of the external clock, if used.
157 *
158 * @return None.
159 *******************************************************************************/
160 void AD5933_SetSystemClk(char clkSource, unsigned long extClkFreq)
161 {
162 currentClockSource = clkSource;
163 if(clkSource == AD5933_CONTROL_EXT_SYSCLK)
164 {
165 currentSysClk = extClkFreq; // External clock frequency
166 }
167 else
168 {
169 currentSysClk = AD5933_INTERNAL_SYS_CLK; // 16 MHz
170 }
171 AD5933_SetRegisterValue(AD5933_REG_CONTROL_LB, currentClockSource, 1);
172 }
173
174
175 /***************************************************************************//**
176 * @brief Selects the range and gain of the device.
177 *
178 * @param range - Range option.
179 * Example: AD5933_RANGE_2000mVpp
180 * AD5933_RANGE_200mVpp
181 * AD5933_RANGE_400mVpp
182
183 * AD5933_RANGE_1000mVpp
184 * @param gain - Gain option.
185 * Example: AD5933_GAIN_X5
186 * AD5933_GAIN_X1
187 *
188 * @return None.
189 *******************************************************************************/
190 void AD5933_SetRangeAndGain(char range, char gain)
191 {
192 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
193 AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_NOP) |
194 AD5933_CONTROL_RANGE(range) |
195 AD5933_CONTROL_PGA_GAIN(gain),
196 1);
197 /* Store the last settings made to range and gain. */
198 currentRange = range;
199 currentGain = gain;
200 }
201
202 /***************************************************************************//**
203 * @brief Reads the temperature from the part and returns the data in
204 * degrees Celsius.
205 *
206 * @return temperature - Temperature.
207 *******************************************************************************/
208 float AD5933_GetTemperature(void)
209 {
210 float temperature = 0;
211 unsigned char status = 0;
212
213 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
214 AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_MEASURE_TEMP) |
215 AD5933_CONTROL_RANGE(currentRange) |
216 AD5933_CONTROL_PGA_GAIN(currentGain),
217 1);
218 while((status & AD5933_STAT_TEMP_VALID) == 0)
219 {
220 status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
221 }
222
223 temperature = AD5933_GetRegisterValue(AD5933_REG_TEMP_DATA,2);
224 if(temperature < 8192)
225 {
226 temperature /= 32;
227 }
228 else
229 {
230 temperature -= 16384;
231 temperature /= 32;
232 }
233
234 return temperature;
235 }
236
237 /***************************************************************************//**
238 * @brief Configures the sweep parameters: Start frequency, Frequency increment
239 * and Number of increments.
240 *
241 * @param startFreq - Start frequency in Hz;
242 * @param incFreq - Frequency increment in Hz;
243 * @param incNum - Number of increments. Maximum value is 511(0x1FF).
244 *
245 * @return None.
246 *******************************************************************************/
247 void AD5933_ConfigSweep(unsigned long startFreq,
248 unsigned long incFreq,
249 unsigned short incNum)
250 {
251 unsigned long startFreqReg = 0;
252 unsigned long incFreqReg = 0;
253 unsigned short incNumReg = 0;
254
255 /* Ensure that incNum is a valid data. */
256 if(incNum > AD5933_MAX_INC_NUM)
257 {
258 incNumReg = AD5933_MAX_INC_NUM;
259 }
260 else
261 {
262 incNumReg = incNum;
263 }
264
265 /* Convert users start frequency to binary code. */
266 startFreqReg = (unsigned long)((double)startFreq * 4 / currentSysClk *
267 POW_2_27);
268
269 /* Convert users increment frequency to binary code. */
270 incFreqReg = (unsigned long)((double)incFreq * 4 / currentSysClk *
271 POW_2_27);
272
273 /* Configure the device with the sweep parameters. */
274 AD5933_SetRegisterValue(AD5933_REG_FREQ_START,
275 startFreqReg,
276 3);
277 AD5933_SetRegisterValue(AD5933_REG_FREQ_INC,
278 incFreqReg,
279 3);
280 AD5933_SetRegisterValue(AD5933_REG_INC_NUM,
281 incNumReg,
282 2);
283 }
284
285 /***************************************************************************//**
286 * @brief Starts the sweep operation.
287 *
288 * @return None.
289 *******************************************************************************/
290 void AD5933_StartSweep(void)
291 {
292 unsigned char status = 0;
293
294 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
295 AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_STANDBY) |
296 AD5933_CONTROL_RANGE(currentRange) |
297 AD5933_CONTROL_PGA_GAIN(currentGain),
298 1);
299 AD5933_Reset();
300 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
301 AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_INIT_START_FREQ)|
302 AD5933_CONTROL_RANGE(currentRange) |
303 AD5933_CONTROL_PGA_GAIN(currentGain),
304 1);
305 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
306 AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_START_SWEEP) |
307 AD5933_CONTROL_RANGE(currentRange) |
308 AD5933_CONTROL_PGA_GAIN(currentGain),
309 1);
310 status = 0;
311 while((status & AD5933_STAT_DATA_VALID) == 0)
312 {
313 status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
314 };
315 }
316
317 /***************************************************************************//**
318 * @brief Reads the real and the imaginary data and calculates the Gain Factor.
319 *
320 * @param calibrationImpedance - The calibration impedance value.
321 * @param freqFunction - Frequency function.
322 * Example: AD5933_FUNCTION_INC_FREQ - Increment
323 freq.;
324 * AD5933_FUNCTION_REPEAT_FREQ - Repeat
325 freq..
326 *
327 * @return gainFactor - Calculated gain factor.
328 *******************************************************************************/
329 double AD5933_CalculateGainFactor(unsigned long calibrationImpedance,
330 unsigned char freqFunction)
331 {
332 double gainFactor = 0;
333 double magnitude = 0;
334 signed short realData = 0;
335 signed short imagData = 0;
336 unsigned char status = 0;
337
338 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
339 AD5933_CONTROL_FUNCTION(freqFunction) |
340 AD5933_CONTROL_RANGE(currentRange) |
341 AD5933_CONTROL_PGA_GAIN(currentGain),
342 1);
343 status = 0;
344 while((status & AD5933_STAT_DATA_VALID) == 0)
345 {
346 status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
347 }
348 realData = AD5933_GetRegisterValue(AD5933_REG_REAL_DATA,2);
349 imagData = AD5933_GetRegisterValue(AD5933_REG_IMAG_DATA,2);
350 magnitude = sqrt((realData * realData) + (imagData * imagData));
351 gainFactor = 1 / (magnitude * calibrationImpedance);
352
353 return gainFactor;
354 }
355
356 /***************************************************************************//**
357 * @brief Reads the real and the imaginary data and calculates the Impedance.
358 *
359 * @param gainFactor - The gain factor.
360 * @param freqFunction - Frequency function.
361 * Example: AD5933_FUNCTION_INC_FREQ - Increment freq.;
362 * AD5933_FUNCTION_REPEAT_FREQ - Repeat freq..
363 *
364 * @return impedance - Calculated impedance.
365 *******************************************************************************/
366 double AD5933_CalculateImpedance(double gainFactor,
367 unsigned char freqFunction)
368 {
369 signed short realData = 0;
370 signed short imagData = 0;
371 double magnitude = 0;
372 double impedance = 0;
373 unsigned char status = 0;
374
375 AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
376 AD5933_CONTROL_FUNCTION(freqFunction) |
377 AD5933_CONTROL_RANGE(currentRange) |
378 AD5933_CONTROL_PGA_GAIN(currentGain),
379 1);
380 status = 0;
381 while((status & AD5933_STAT_DATA_VALID) == 0)
382 {
383 status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
384 }
385 realData = AD5933_GetRegisterValue(AD5933_REG_REAL_DATA,2);
386 imagData = AD5933_GetRegisterValue(AD5933_REG_IMAG_DATA,2);
387 magnitude = sqrt((realData * realData) + (imagData * imagData));
388
389 impedance = 1 / (magnitude * gainFactor);
390
391 return impedance;
392 }