Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD7156.c
3 * @brief Implementation of AD7156 Driver.
4 * @author DNechita([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, 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
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "AD7156.h"
45 #include "Communication.h"
46
47 /******************************************************************************/
48 /************************ Variables Declarations ******************************/
49 /******************************************************************************/
50 float ad7156Channel1Range = 2;
51 float ad7156Channel2Range = 2;
52
53 /******************************************************************************/
54 /************************ Functions Definitions *******************************/
55 /******************************************************************************/
56
57 /***************************************************************************//**
58 * @brief Performs a burst read of a specified number of registers.
59 *
60 * @param pReadData - The read values are stored in this buffer.
61 * @param registerAddress - The start address of the burst read.
62 * @param bytesNumber - Number of bytes to read.
63 *
64 * @return None.
65 *******************************************************************************/
66 void AD7156_GetRegisterValue(unsigned char* pReadData,
67 unsigned char registerAddress,
68 unsigned char bytesNumber)
69 {
70 I2C_Write(AD7156_ADDRESS, &registerAddress, 1, 0);
71 I2C_Read(AD7156_ADDRESS, pReadData, bytesNumber, 1);
72 }
73
74 /***************************************************************************//**
75 * @brief Writes data into one or two registers.
76 *
77 * @param registerValue - Data value to write.
78 * @param registerAddress - Address of the register.
79 * @param bytesNumber - Number of bytes. Accepted values: 0 - 1.
80 *
81 * @return None.
82 *******************************************************************************/
83 void AD7156_SetRegisterValue(unsigned short registerValue,
84 unsigned char registerAddress,
85 unsigned char bytesNumber)
86 {
87 unsigned char dataBuffer[3] = {0, 0, 0};
88
89 dataBuffer[0] = registerAddress;
90 dataBuffer[1] = (registerValue >> 8);
91 dataBuffer[bytesNumber] = (registerValue & 0x00FF);
92 I2C_Write(AD7156_ADDRESS, dataBuffer, bytesNumber + 1, 1);
93 }
94
95 /***************************************************************************//**
96 * @brief Initializes the communication peripheral and checks if the device is
97 * present.
98 *
99 * @return status - The result of the initialization procedure.
100 * Example: -1 - I2C peripheral was not initialized or the
101 * device is not present.
102 * 0 - I2C peripheral was initialized and the
103 * device is present.
104 *******************************************************************************/
105 char AD7156_Init(void)
106 {
107 char status = -1;
108 unsigned char test = 0;
109
110 status = I2C_Init(100000);
111 AD7156_GetRegisterValue(&test, AD7156_REG_CHIP_ID, 1);
112 if(test != AD7156_DEFAULT_ID)
113 {
114 status = -1;
115 }
116 return status;
117 }
118
119 /***************************************************************************//**
120 * @brief Resets the device.
121 *
122 * @return None.
123 *******************************************************************************/
124 void AD7156_Reset(void)
125 {
126 unsigned char addressPointer = 0;
127
128 addressPointer = AD7156_RESET_CMD;
129 I2C_Write(AD7156_ADDRESS, &addressPointer, 1, 1);
130 }
131
132 /***************************************************************************//**
133 * @brief Sets the converter mode of operation.
134 *
135 * @oaram pwrMode - Mode of operation option.
136 * Example: AD7156_CONV_MODE_IDLE - Idle
137 * AD7156_CONV_MODE_CONT_CONV - Continuous conversion
138 * AD7156_CONV_MODE_SINGLE_CONV - Single conversion
139 * AD7156_CONV_MODE_PWR_DWN - Power-down
140 *
141 * @return None.
142 *******************************************************************************/
143 void AD7156_SetPowerMode(unsigned char pwrMode)
144 {
145 unsigned char oldConfigReg = 0;
146 unsigned char newConfigReg = 0;
147
148 AD7156_GetRegisterValue(&oldConfigReg, AD7156_REG_CONFIG, 1);
149 oldConfigReg &= ~AD7156_CONFIG_MD(0x3);
150 newConfigReg = oldConfigReg| AD7156_CONFIG_MD(pwrMode);
151 AD7156_SetRegisterValue(newConfigReg, AD7156_REG_CONFIG, 1);
152 }
153
154 /***************************************************************************//**
155 * @brief Enables or disables conversion on the selected channel.
156 *
157 * @param channel - Channel option.
158 * Example: AD7156_CHANNEL1
159 * AD7156_CHANNEL2
160 * @param enableConv - The state of channel activity.
161 * Example: 0 - disable conversion on selected channel.
162 * 1 - enable conversion on selected channel.
163 *
164 * @return None.
165 *******************************************************************************/
166 void AD7156_ChannelState(unsigned char channel, unsigned char enableConv)
167 {
168 unsigned char oldConfigReg = 0;
169 unsigned char newConfigReg = 0;
170 unsigned char channelMask = 0;
171
172 channelMask = (channel == 1) ? AD7156_CONFIG_EN_CH1 : AD7156_CONFIG_EN_CH2;
173
174 AD7156_GetRegisterValue(&oldConfigReg, AD7156_REG_CONFIG, 1);
175 oldConfigReg &= ~channelMask;
176 newConfigReg = oldConfigReg | (channelMask * enableConv);
177 AD7156_SetRegisterValue(newConfigReg, AD7156_REG_CONFIG, 1);
178 }
179
180 /***************************************************************************//**
181 * @brief Sets the input range of the specified channel.
182 *
183 * @param channel - Channel option.
184 * Example: AD7156_CHANNEL1
185 * AD7156_CHANNEL2
186 * @param range - Input range option.
187 * Example: AD7156_CDC_RANGE_2_PF - 2pF input range.
188 * AD7156_CDC_RANGE_0_5_PF - 0.5pF input range.
189 * AD7156_CDC_RANGE_1_PF - 1pF input range.
190 * AD7156_CDC_RANGE_4_PF - 4pF input range.
191 *
192 * @return None.
193 *******************************************************************************/
194 void AD7156_SetRange(unsigned channel, unsigned char range)
195 {
196 unsigned char oldSetupReg = 0;
197 unsigned char newSetupReg = 0;
198 unsigned char regAddress = 0;
199
200 regAddress = (channel == 1) ? AD7156_REG_CH1_SETUP : AD7156_REG_CH2_SETUP;
201 AD7156_GetRegisterValue(&oldSetupReg, regAddress, 1);
202 oldSetupReg &= ~AD7156_CH1_SETUP_RANGE(0x3);
203 newSetupReg = oldSetupReg | AD7156_CH1_SETUP_RANGE(range);
204 AD7156_SetRegisterValue(newSetupReg, regAddress, 1);
205 /* Update global variables that hold range information. */
206 if(channel == 1)
207 {
208 ad7156Channel1Range = AD7156_GetRange(channel);
209 }
210 else
211 {
212 ad7156Channel2Range = AD7156_GetRange(channel);
213 }
214 }
215
216 /***************************************************************************//**
217 * @brief Reads the range bits from the device and returns the range in pF.
218 *
219 * @param channel - Channel option.
220 * Example: AD7156_CHANNEL1
221 * AD7156_CHANNEL2
222 *
223 * @return The capacitive input range(pF).
224 *******************************************************************************/
225 float AD7156_GetRange(unsigned channel)
226 {
227 unsigned char setupReg = 0;
228 unsigned char regAddress = 0;
229 float range = 0;
230
231 regAddress = (channel == 1) ? AD7156_REG_CH1_SETUP : AD7156_REG_CH2_SETUP;
232 AD7156_GetRegisterValue(&setupReg, regAddress, 1);
233 setupReg = (setupReg & AD7156_CH1_SETUP_RANGE(0x3)) >> 6;
234 switch(setupReg)
235 {
236 case AD7156_CDC_RANGE_2_PF:
237 range = 2.0;
238 break;
239 case AD7156_CDC_RANGE_0_5_PF:
240 range = 0.5;
241 break;
242 case AD7156_CDC_RANGE_1_PF:
243 range = 1.0;
244 break;
245 case AD7156_CDC_RANGE_4_PF:
246 range = 4.0;
247 break;
248 }
249 /* Update global variables that hold range information. */
250 if(channel == 1)
251 {
252 ad7156Channel1Range = range;
253 }
254 else
255 {
256 ad7156Channel2Range = range;
257 }
258
259 return range;
260 }
261
262 /***************************************************************************//**
263 * @brief Selects the threshold mode of operation.
264 *
265 * @param thrMode - Output comparator mode.
266 * Example: AD7156_THR_MODE_NEGATIVE
267 * AD7156_THR_MODE_POSITIVE
268 * AD7156_THR_MODE_IN_WINDOW
269 * AD7156_THR_MODE_OU_WINDOW
270 * @param thrFixed - Selects the threshold mode.
271 * Example: AD7156_ADAPTIVE_THRESHOLD
272 * AD7156_FIXED_THRESHOLD
273 *
274 * @return None.
275 *******************************************************************************/
276 void AD7156_SetThresholdMode(unsigned char thrMode, unsigned char thrFixed)
277 {
278 unsigned char oldConfigReg = 0;
279 unsigned char newConfigReg = 0;
280
281 AD7156_GetRegisterValue(&oldConfigReg, AD7156_REG_CONFIG, 1);
282 oldConfigReg &= ~(AD7156_CONFIG_THR_FIXED | AD7156_CONFIG_THR_MD(0x3));
283 newConfigReg = oldConfigReg |
284 (AD7156_CONFIG_THR_FIXED * thrFixed) |
285 (AD7156_CONFIG_THR_MD(thrMode));
286 AD7156_SetRegisterValue(newConfigReg, AD7156_REG_CONFIG, 1);
287 }
288
289 /***************************************************************************//**
290 * @brief Writes to the threshold register when threshold fixed mode is enabled.
291 *
292 * @param channel - Channel option.
293 * Example: AD7156_CHANNEL1
294 * AD7156_CHANNEL2
295 * @param pFthr - The threshold value in picofarads(pF). The value must not be
296 * out of the selected input range.
297 *
298 * @return None.
299 *******************************************************************************/
300 void AD7156_SetThreshold(unsigned char channel, float pFthr)
301 {
302 unsigned char thrRegAddress = 0;
303 unsigned short rawThr = 0;
304 float range = 0;
305
306 thrRegAddress = (channel == 1) ? AD7156_REG_CH1_SENS_THRSH_H :
307 AD7156_REG_CH2_SENS_THRSH_H;
308 range = AD7156_GetRange(channel);
309 rawThr = (unsigned short)((pFthr * 0xA000 / range) + 0x3000);
310 if(rawThr > 0xD000)
311 {
312 rawThr = 0xD000;
313 }
314 else if(rawThr < 0x3000)
315 {
316 rawThr = 0x3000;
317 }
318 AD7156_SetRegisterValue(rawThr, thrRegAddress, 2);
319 }
320
321 /***************************************************************************//**
322 * @brief Writes a value(pF) to the sensitivity register. This functions
323 * should be used when adaptive threshold mode is selected.
324 *
325 * @param channel - Channel option.
326 * Example: AD7156_CHANNEL1
327 * AD7156_CHANNEL2
328 * @param pFsensitivity - The sensitivity value in picofarads(pF).
329 *
330 * @return None.
331 *******************************************************************************/
332 void AD7156_SetSensitivity(unsigned char channel, float pFsensitivity)
333 {
334 unsigned char sensitivityRegAddr = 0;
335 unsigned short rawSensitivity = 0;
336 float range = 0;
337
338 sensitivityRegAddr = (channel == 1) ? AD7156_REG_CH1_SENS_THRSH_H :
339 AD7156_REG_CH2_SENS_THRSH_H;
340 range = (channel == 1) ? ad7156Channel1Range : ad7156Channel2Range;
341 rawSensitivity = (unsigned short)(pFsensitivity * 0xA00 / range);
342 rawSensitivity = (rawSensitivity << 4) & 0x0FF0;
343 AD7156_SetRegisterValue(rawSensitivity, sensitivityRegAddr, 2);
344 }
345
346 /***************************************************************************//**
347 * @brief Reads a 12-bit sample from the selected channel.
348 *
349 * @param channel - Channel option.
350 * Example: AD7156_CHANNEL1
351 * AD7156_CHANNEL2
352 * @return Conversion result form the selected channel.
353 *******************************************************************************/
354 unsigned short AD7156_ReadChannelData(unsigned char channel)
355 {
356 unsigned short chResult = 0;
357 unsigned char regData[2] = {0, 0};
358 unsigned char chAddress = 0;
359
360 if(channel == 1)
361 {
362 chAddress = AD7156_REG_CH1_DATA_H;
363 }
364 else
365 {
366 chAddress = AD7156_REG_CH2_DATA_H;
367 }
368 AD7156_GetRegisterValue(regData, chAddress, 2);
369 chResult = (regData[0] << 8) + regData[1];
370
371 return chResult;
372 }
373
374 /***************************************************************************//**
375 * @brief Waits for a finished CDC conversion and reads a 12-bit sample from
376 * the selected channel.
377 *
378 * @param channel - Channel option.
379 * Example: AD7156_CHANNEL1
380 * AD7156_CHANNEL2
381 * @return Conversion result form the selected channel.
382 *******************************************************************************/
383 unsigned short AD7156_WaitReadChannelData(unsigned char channel)
384 {
385 unsigned short chResult = 0;
386 unsigned char regData[2] = {0, 0};
387 unsigned char status = 0;
388 unsigned char chRdyMask = 0;
389 unsigned char chAddress = 0;
390
391 if(channel == 1)
392 {
393 chRdyMask = AD7156_STATUS_RDY1;
394 chAddress = AD7156_REG_CH1_DATA_H;
395 }
396 else
397 {
398 chRdyMask = AD7156_STATUS_RDY2;
399 chAddress = AD7156_REG_CH2_DATA_H;
400 }
401 do
402 {
403 AD7156_GetRegisterValue(&status, AD7156_REG_STATUS, 1);
404 }while((status & chRdyMask) != 0);
405 AD7156_GetRegisterValue(regData, chAddress, 2);
406 chResult = (regData[0] << 8) + regData[1];
407
408 return chResult;
409 }
410
411 /***************************************************************************//**
412 * @brief Reads a sample the selected channel and converts the data to
413 * picofarads(pF).
414 *
415 * @param channel - Channel option.
416 * Example: AD7156_CHANNEL1
417 * AD7156_CHANNEL2
418 * @return Conversion result form the selected channel as picofarads(pF).
419 *******************************************************************************/
420 float AD7156_ReadChannelCapacitance(unsigned char channel)
421 {
422 unsigned short rawCh = 0;
423 float chRange = 0;
424 float pFdata = 0;
425
426 chRange = (channel == 1) ? ad7156Channel1Range : ad7156Channel2Range;
427 rawCh = AD7156_ReadChannelData(channel);
428 if(rawCh < 0x3000)
429 {
430 rawCh= 0x3000;
431 }
432 else if(rawCh > 0xD000)
433 {
434 rawCh = 0xD000;
435 }
436 pFdata = (((rawCh) - 0x3000) * chRange) / 0xA000;
437
438 return pFdata;
439 }
440
441 /***************************************************************************//**
442 * @brief Waits for a finished CDC conversion the selected channel, reads a
443 * sample and converts the data to picofarads(pF).
444 *
445 * @param channel - Channel option.
446 * Example: AD7156_CHANNEL1
447 * AD7156_CHANNEL2
448 * @return Conversion result form the selected channel as picofarads(pF).
449 *******************************************************************************/
450 float AD7156_WaitReadChannelCapacitance(unsigned char channel)
451 {
452 unsigned short rawCh = 0;
453 float chRange = 0;
454 float pFdata = 0;
455
456 chRange = (channel == 1) ? ad7156Channel1Range : ad7156Channel2Range;
457 rawCh = AD7156_WaitReadChannelData(channel);
458 if(rawCh < 0x3000)
459 {
460 rawCh= 0x3000;
461 }
462 else if(rawCh > 0xD000)
463 {
464 rawCh = 0xD000;
465 }
466 pFdata = (((rawCh) - 0x3000) * chRange) / 0xA000;
467
468 return pFdata;
469 }