Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD7193.c
3 * @brief Implementation of AD7193 Driver.
4 * @author DNechita ([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 "AD7193.h" // AD7193 definitions.
47
48 /******************************************************************************/
49 /************************ Variables Definitions *******************************/
50 /******************************************************************************/
51 static unsigned char currentPolarity = 0;
52 static unsigned char currentGain = 1;
53
54 /******************************************************************************/
55 /************************ Functions Definitions *******************************/
56 /******************************************************************************/
57
58 /***************************************************************************//**
59 * @brief Checks if the AD7139 part is present.
60 *
61 * @return status - Indicates if the part is present or not.
62 * Example: -1 - SPI peripheral was not initialized.
63 * 0 - SPI peripheral is initialized.
64 *******************************************************************************/
65 char AD7193_Init(void)
66 {
67 char status = 0;
68 unsigned char regVal = 0;
69
70 status = SPI_Init(0, 1000000, 1, 0);
71 regVal = AD7193_GetRegisterValue(AD7193_REG_ID, 1, 1);
72 if((regVal & AD7193_ID_MASK) != ID_AD7193)
73 {
74 status = -1;
75 }
76 return status;
77 }
78
79 /***************************************************************************//**
80 * @brief Writes data into a register.
81 *
82 * @param registerAddress - Address of the register.
83 * @param registerValue - Data value to write.
84 * @param bytesNumber - Number of bytes to be written.
85 * @param modifyCS - Allows Chip Select to be modified.
86 *
87 * @return none.
88 *******************************************************************************/
89 void AD7193_SetRegisterValue(unsigned char registerAddress,
90 unsigned long registerValue,
91 unsigned char bytesNumber,
92 unsigned char modifyCS)
93 {
94 unsigned char writeCommand[5] = {0, 0, 0, 0, 0};
95 unsigned char* dataPointer = (unsigned char*)&registerValue;
96 unsigned char bytesNr = bytesNumber;
97
98 writeCommand[0] = AD7193_COMM_WRITE |
99 AD7193_COMM_ADDR(registerAddress);
100 while(bytesNr > 0)
101 {
102 writeCommand[bytesNr] = *dataPointer;
103 dataPointer ++;
104 bytesNr --;
105 }
106 SPI_Write(AD7193_SLAVE_ID * modifyCS, writeCommand, bytesNumber + 1);
107 }
108
109 /***************************************************************************//**
110 * @brief Reads the value of a register.
111 *
112 * @param registerAddress - Address of the register.
113 * @param bytesNumber - Number of bytes that will be read.
114 * @param modifyCS - Allows Chip Select to be modified.
115 *
116 * @return buffer - Value of the register.
117 *******************************************************************************/
118 unsigned long AD7193_GetRegisterValue(unsigned char registerAddress,
119 unsigned char bytesNumber,
120 unsigned char modifyCS)
121 {
122 unsigned char registerWord[5] = {0, 0, 0, 0, 0};
123 unsigned long buffer = 0x0;
124 unsigned char i = 0;
125
126 registerWord[0] = AD7193_COMM_READ |
127 AD7193_COMM_ADDR(registerAddress);
128 SPI_Read(AD7193_SLAVE_ID * modifyCS, registerWord, bytesNumber + 1);
129 for(i = 1; i < bytesNumber + 1; i++)
130 {
131 buffer = (buffer << 8) + registerWord[i];
132 }
133
134 return buffer;
135 }
136
137 /***************************************************************************//**
138 * @brief Resets the device.
139 *
140 * @return none.
141 *******************************************************************************/
142 void AD7193_Reset(void)
143 {
144 unsigned char registerWord[6] = {0, 0, 0, 0, 0, 0};
145
146 registerWord[0] = 0xFF;
147 registerWord[1] = 0xFF;
148 registerWord[2] = 0xFF;
149 registerWord[3] = 0xFF;
150 registerWord[4] = 0xFF;
151 registerWord[5] = 0xFF;
152 SPI_Write(AD7193_SLAVE_ID, registerWord, 6);
153 }
154
155 /***************************************************************************//**
156 * @brief Set device to idle or power-down.
157 *
158 * @param pwrMode - Selects idle mode or power-down mode.
159 * Example: 0 - power-down
160 * 1 - idle
161 *
162 * @return none.
163 *******************************************************************************/
164 void AD7193_SetPower(unsigned char pwrMode)
165 {
166 unsigned long oldPwrMode = 0x0;
167 unsigned long newPwrMode = 0x0;
168
169 oldPwrMode = AD7193_GetRegisterValue(AD7193_REG_MODE, 3, 1);
170 oldPwrMode &= ~(AD7193_MODE_SEL(0x7));
171 newPwrMode = oldPwrMode |
172 AD7193_MODE_SEL((pwrMode * (AD7193_MODE_IDLE)) |
173 (!pwrMode * (AD7193_MODE_PWRDN)));
174 AD7193_SetRegisterValue(AD7193_REG_MODE, newPwrMode, 3, 1);
175 }
176
177 /***************************************************************************//**
178 * @brief Waits for RDY pin to go low.
179 *
180 * @return none.
181 *******************************************************************************/
182 void AD7193_WaitRdyGoLow(void)
183 {
184 while(AD7193_RDY_STATE)
185 {
186 ;
187 }
188 }
189
190 /***************************************************************************//**
191 * @brief Selects the channel to be enabled.
192 *
193 * @param channel - Selects a channel.
194 * Example: AD7193_CH_0 - AIN1(+) - AIN2(-); (Pseudo = 0)
195 * AD7193_CH_1 - AIN3(+) - AIN4(-); (Pseudo = 0)
196 * AD7193_TEMP - Temperature sensor
197 * AD7193_SHORT - AIN2(+) - AIN2(-); (Pseudo = 0)
198 *
199 * @return none.
200 *******************************************************************************/
201 void AD7193_ChannelSelect(unsigned short channel)
202 {
203 unsigned long oldRegValue = 0x0;
204 unsigned long newRegValue = 0x0;
205
206 oldRegValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1);
207 oldRegValue &= ~(AD7193_CONF_CHAN(0x3FF));
208 newRegValue = oldRegValue | AD7193_CONF_CHAN(1 << channel);
209 AD7193_SetRegisterValue(AD7193_REG_CONF, newRegValue, 3, 1);
210 }
211
212 /***************************************************************************//**
213 * @brief Performs the given calibration to the specified channel.
214 *
215 * @param mode - Calibration type.
216 * @param channel - Channel to be calibrated.
217 *
218 * @return none.
219 *******************************************************************************/
220 void AD7193_Calibrate(unsigned char mode, unsigned char channel)
221 {
222 unsigned long oldRegValue = 0x0;
223 unsigned long newRegValue = 0x0;
224
225 AD7193_ChannelSelect(channel);
226 oldRegValue = AD7193_GetRegisterValue(AD7193_REG_MODE, 3, 1);
227 oldRegValue &= ~AD7193_MODE_SEL(0x7);
228 newRegValue = oldRegValue | AD7193_MODE_SEL(mode);
229 AD7193_CS_LOW;
230 AD7193_SetRegisterValue(AD7193_REG_MODE, newRegValue, 3, 0); // CS is not modified.
231 AD7193_WaitRdyGoLow();
232 AD7193_CS_HIGH;
233 }
234
235 /***************************************************************************//**
236 * @brief Selects the polarity of the conversion and the ADC input range.
237 *
238 * @param polarity - Polarity select bit.
239 Example: 0 - bipolar operation is selected.
240 1 - unipolar operation is selected.
241 * @param range - Gain select bits. These bits are written by the user to select
242 the ADC input range.
243 *
244 * @return none.
245 *******************************************************************************/
246 void AD7193_RangeSetup(unsigned char polarity, unsigned char range)
247 {
248 unsigned long oldRegValue = 0x0;
249 unsigned long newRegValue = 0x0;
250
251 oldRegValue = AD7193_GetRegisterValue(AD7193_REG_CONF,3, 1);
252 oldRegValue &= ~(AD7193_CONF_UNIPOLAR |
253 AD7193_CONF_GAIN(0x7));
254 newRegValue = oldRegValue |
255 (polarity * AD7193_CONF_UNIPOLAR) |
256 AD7193_CONF_GAIN(range);
257 AD7193_SetRegisterValue(AD7193_REG_CONF, newRegValue, 3, 1);
258 /* Store the last settings regarding polarity and gain. */
259 currentPolarity = polarity;
260 currentGain = 1 << range;
261 }
262
263 /***************************************************************************//**
264 * @brief Returns the result of a single conversion.
265 *
266 * @return regData - Result of a single analog-to-digital conversion.
267 *******************************************************************************/
268 unsigned long AD7193_SingleConversion(void)
269 {
270 unsigned long command = 0x0;
271 unsigned long regData = 0x0;
272
273 command = AD7193_MODE_SEL(AD7193_MODE_SINGLE) |
274 AD7193_MODE_CLKSRC(AD7193_CLK_INT) |
275 AD7193_MODE_RATE(0x060);
276 AD7193_CS_LOW;
277 AD7193_SetRegisterValue(AD7193_REG_MODE, command, 3, 0); // CS is not modified.
278 AD7193_WaitRdyGoLow();
279 regData = AD7193_GetRegisterValue(AD7193_REG_DATA, 3, 0);
280 AD7193_CS_HIGH;
281
282 return regData;
283 }
284
285 /***************************************************************************//**
286 * @brief Returns the average of several conversion results.
287 *
288 * @return samplesAverage - The average of the conversion results.
289 *******************************************************************************/
290 unsigned long AD7193_ContinuousReadAvg(unsigned char sampleNumber)
291 {
292 unsigned long samplesAverage = 0;
293 unsigned long command = 0;
294 unsigned char count = 0;
295
296 command = AD7193_MODE_SEL(AD7193_MODE_CONT) |
297 AD7193_MODE_CLKSRC(AD7193_CLK_INT) |
298 AD7193_MODE_RATE(0x060);
299 AD7193_CS_LOW;
300 AD7193_SetRegisterValue(AD7193_REG_MODE, command, 3, 0); // CS is not modified.
301 for(count = 0; count < sampleNumber; count++)
302 {
303 AD7193_WaitRdyGoLow();
304 samplesAverage += AD7193_GetRegisterValue(AD7193_REG_DATA, 3, 0); // CS is not modified.
305 }
306 AD7193_CS_HIGH;
307 samplesAverage = samplesAverage / sampleNumber;
308
309 return samplesAverage;
310 }
311
312 /***************************************************************************//**
313 * @brief Read data from temperature sensor and converts it to Celsius degrees.
314 *
315 * @return temperature - Celsius degrees.
316 *******************************************************************************/
317 float AD7193_TemperatureRead(void)
318 {
319 unsigned long dataReg = 0;
320 float temperature = 0;
321
322 AD7193_RangeSetup(0, AD7193_CONF_GAIN_1); // Bipolar operation, 0 Gain.
323 AD7193_ChannelSelect(AD7193_CH_TEMP);
324 dataReg = AD7193_SingleConversion();
325 dataReg -= 0x800000;
326 temperature = (float) dataReg / 2815; // Kelvin Temperature
327 temperature -= 273; // Celsius Temperature
328
329 return temperature;
330 }
331
332 /***************************************************************************//**
333 * @brief Converts 24-bit raw data to milivolts.
334 *
335 * @param rawData - 24-bit data sample.
336 * @param vRef - The value of the voltage reference used by the device.
337 *
338 * @return voltage - The result of the conversion expressed as volts.
339 *******************************************************************************/
340 float AD7193_ConvertToVolts(unsigned long rawData, float vRef)
341 {
342 float voltage = 0;
343
344 if(currentPolarity == 0 ) // Bipolar mode
345 {
346 voltage = 1000 * (((float)rawData / (1ul << 23)) - 1) * vRef / currentGain;
347 }
348 else // Unipolar mode
349 {
350 voltage = 1000 * ((float)rawData * vRef) / (1ul << 24) / currentGain;
351 }
352
353 return voltage;
354 }