Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/CN0209/cn0209.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file cn0209.c
3 * @brief Implementation of CN0209 Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************/
41
42 /******************************************************************************/
43 /* Include Files */
44 /******************************************************************************/
45 #include "cn0209.h"
46 #include "Communication.h"
47 #include "TIME.h"
48
49 /***************************************************************************//**
50 * @brief Reads data from ADT7310.
51 *
52 * @param regAddress - Address of the register.
53 * @param bytesNumber - Number of bytes.
54 *
55 * @return readData - Received data.
56 *******************************************************************************/
57 unsigned short ADT7310_Read(unsigned char regAddress,
58 unsigned char bytesNumber)
59 {
60 unsigned short receivedData = 0x0;
61 unsigned char command = 0x0;
62 unsigned char i = 0;
63 unsigned char dataBuf[3] = {0x00, 0x00, 0x00};
64
65 ADT7310_CS_LOW;
66 //TIME_DelayMs(1);
67 command = ADT7310_CMD_READ + ADT7310_CMD_ADDR(regAddress);
68 dataBuf[0] = command;
69 SPI_Read(1, (unsigned char*)dataBuf, bytesNumber + 1);
70 for(i = 0; i < bytesNumber; i++)
71 {
72 receivedData = receivedData << 8;
73 receivedData += dataBuf[i+1];
74 }
75 ADT7310_CS_HIGH;
76
77 return(receivedData);
78 }
79
80 /***************************************************************************//**
81 * @brief Writes data to ADT7310.
82 *
83 * @param regAddress - Address of the register.
84 * @param bytesNumber - Number of bytes.
85 * @param regData - Data that will be loaded.
86 *
87 * @return None.
88 *******************************************************************************/
89 void ADT7310_Write(unsigned char regAddress,
90 unsigned char bytesNumber,
91 unsigned short regData)
92 {
93 unsigned char command = 0x0;
94 unsigned short mask = 0x0;
95 unsigned char currentByte = 0x0;
96 unsigned char i = 0;
97 unsigned char txBuf[3] = {0x00, 0x00, 0x00};
98
99 command = ADT7310_CMD_WRITE + ADT7310_CMD_ADDR(regAddress);
100 txBuf[0] = command;
101 ADT7310_CS_LOW;
102 for(i = 0; i < bytesNumber; i++)
103 {
104 mask = 0xFF << ((bytesNumber - i) * 8);
105 currentByte = (regData & mask) >> ((bytesNumber - i) * 8);
106 txBuf[i+1] = currentByte;
107 }
108 SPI_Write(1, (unsigned char*)txBuf, bytesNumber + 1);
109 ADT7310_CS_HIGH;
110 }
111
112 /***************************************************************************//**
113 * @brief Reads the temperature from ADT7310 device.
114 *
115 * @param None.
116 *
117 * @return adt7310TempValue - Current temperature read.
118 *******************************************************************************/
119 float ADT7310_TemperatureRead(void)
120 {
121 float adt7310TempValue = 0;
122 unsigned long temp = 0;
123
124 temp = ADT7310_Read(ADT7310_REG_TEMPERATURE, 2);
125 if(temp & 0x8000)
126 {
127 temp -= 65536;
128 }
129 adt7310TempValue = (float)temp / 128;
130
131 return adt7310TempValue;
132 }
133
134 /***************************************************************************//**
135 * @brief Resets the serial interface with the ADT7310.
136 *
137 * @param None.
138 *
139 * @return None.
140 *******************************************************************************/
141 void ADT7310_Reset(void)
142 {
143 unsigned char bytesNumber = 5;
144 unsigned char txBuf[5] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
145
146 ADT7310_CS_LOW;
147 SPI_Write(1, (unsigned char*)txBuf, bytesNumber);
148 ADT7310_CS_HIGH;
149 }
150
151 /***************************************************************************//**
152 * @brief Reads data from AD7193.
153 *
154 * @param regAddress - Address of the register.
155 * @param bytesNumber - Number of bytes.
156 *
157 * @return readData - Received data.
158 *******************************************************************************/
159 unsigned long AD7193_Read(unsigned char regAddress,
160 unsigned char bytesNumber)
161 {
162 unsigned long receivedData = 0x0;
163 unsigned char command = 0x0;
164 unsigned char i = 0;
165 unsigned char dataBuf[4] = {0x00, 0x00, 0x00, 0x00};
166
167 AD7193_CS_LOW;
168 command = AD7193_COMM_READ + AD7193_COMM_ADDR(regAddress);
169 dataBuf[0] = command;
170 SPI_Read(1, (unsigned char*)dataBuf, bytesNumber + 1);
171 for(i = 0; i < bytesNumber; i++)
172 {
173 receivedData = receivedData << 8;
174 receivedData += dataBuf[i+1];
175 }
176 AD7193_CS_HIGH;
177
178 return(receivedData);
179 }
180
181 /***************************************************************************//**
182 * @brief Writes data to AD7193.
183 *
184 * @param regAddress - Address of the register.
185 * @param bytesNumber - Number of bytes.
186 * @param regData - Data that will be loaded.
187 *
188 * @return None.
189 *******************************************************************************/
190 void AD7193_Write(unsigned char regAddress,
191 unsigned char bytesNumber,
192 unsigned long regData)
193 {
194 unsigned char command = 0x0;
195 unsigned long mask = 0x0;
196 unsigned char currentByte = 0x0;
197 unsigned char txBuf[4] = {0x00, 0x00, 0x00, 0x00};
198 unsigned char i = 1;
199
200 command = AD7193_COMM_WRITE + AD7193_COMM_ADDR(regAddress);
201 txBuf[0] = command;
202 AD7193_CS_LOW;
203 while(bytesNumber--)
204 {
205 mask = 0xFF << (bytesNumber * 8);
206 currentByte = (regData & mask) >> (bytesNumber * 8);
207 txBuf[i++] = currentByte;
208 }
209 bytesNumber = 3;
210 SPI_Write(1, (unsigned char*)txBuf, bytesNumber + 1);
211 AD7193_CS_HIGH;
212 }
213
214 /***************************************************************************//**
215 * @brief Returns the result of a single conversion from AD7193.
216 *
217 * @param None.
218 *
219 * @return readData - Received data.
220 *******************************************************************************/
221 unsigned long AD7193_SingleConversion(void)
222 {
223 unsigned long regData = 0x0;
224
225 AD7193_CS_LOW;
226 regData = AD7193_MODE_SEL(AD7193_MODE_CONT) |
227 AD7193_MODE_CLKSRC(AD7193_MODE_CLK_INT) |
228 AD7193_MODE_REJ60;
229 AD7193_Write(AD7193_REG_MODE, 3, regData);
230 // Wait until data-ready signal appears.
231 TIME_DelayMs(1);
232 regData = AD7193_Read(AD7193_REG_DATA, 3);
233 AD7193_CS_HIGH;
234
235 return regData;
236 }
237
238 /***************************************************************************//**
239 * @brief Reads the temperature from AD7193 device.
240 *
241 * @param None.
242 *
243 * @return ad7193TempValue - Current temperature read.
244 *******************************************************************************/
245 float AD7193_TemperatureRead(void)
246 {
247 float ad7193TempValue = 0;
248 unsigned long temp = 0;
249 unsigned long regData = 0x0;
250
251 regData = AD7193_CONF_TEMP + AD7193_CONF_GAIN(AD7193_CONF_GAIN_1);
252 AD7193_Write(AD7193_REG_CONF, 3, regData);
253 temp = AD7193_SingleConversion();
254 temp -= 0x800000;
255 ad7193TempValue = (float)temp / 2815; // Kelvin Temperature.
256 ad7193TempValue -= 273; // Celsius Temperature.
257
258 return ad7193TempValue;
259 }
260
261 /***************************************************************************//**
262 * @brief Converts 24-bit raw data to millivolts.
263 *
264 * @param rawData - 24-bit data sample.
265 * @param vRef - The value of the voltage reference used by the device in volts.
266 * @param gain - The set gain of the device.
267 * @param polarity - The set polarity of the input voltage.
268 * Example: 0 - Bipolar mode.
269 * 1 - Unipolar mode.
270 *
271 * @return voltage - The result of the conversion expressed in millivolts.
272 *******************************************************************************/
273 float AD7193_ConvertToVolts(unsigned long rawData,
274 float vRef,
275 unsigned char gain,
276 unsigned char polarity)
277 {
278 float voltage = 0;
279
280 if(polarity == 0) // Bipolar mode
281 {
282 voltage = 1000 * (((float)rawData / (1ul << 23)) - 1) * vRef / gain;
283 }
284 else
285 {
286 if (polarity == 1) // Unipolar mode
287 {
288 voltage = 1000 * ((float)rawData * vRef) / (1ul << 24) / gain;
289 }
290 }
291
292 return voltage;
293 }
294
295 /***************************************************************************//**
296 * @brief Reset the serial interface with the AD7193.
297 *
298 * @param None.
299 *
300 * @return None.
301 *******************************************************************************/
302 void AD7193_Reset(void)
303 {
304 unsigned char bytesNumber = 6;
305 unsigned char txBuf[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
306
307 AD7193_CS_LOW;
308 SPI_Write(1, (unsigned char*)txBuf, bytesNumber);
309 AD7193_CS_HIGH;
310 }
311
312 /***************************************************************************//**
313 * @brief Updates the state of the ADG1414's switches.
314 *
315 * @param None.
316 *
317 * @return None.
318 *******************************************************************************/
319 void ADG1414_State(unsigned char switches)
320 {
321 SPI_Init(0, 1000000, 0, 0);
322 TIME_DelayMs(100);
323 ADG1414_CS_LOW;
324 SPI_Write(1, &switches, 1);
325 ADG1414_CS_HIGH;
326 SPI_Init(0, 1000000, 1, 0);
327 TIME_DelayMs(100);
328 }
329
330 /***************************************************************************//**
331 * @brief Initializes SPI and initial values for CN0209 board.
332 *
333 * @param None.
334 *
335 * @return Always returns success.
336 *******************************************************************************/
337 char CN0209_Init(void)
338 {
339 char status = -1;
340
341 AD7193_CS_OUT;
342 ADT7310_CS_OUT;
343 ADG1414_CS_OUT;
344
345 AD7193_CS_HIGH; // (Active Low)
346 ADT7310_CS_HIGH; // (Active Low)
347 ADG1414_CS_HIGH; // (Active Low)
348
349 /* Setup SPI Interface */
350 status = SPI_Init(0, 1000000, 1, 0);
351 AD7193_Reset();
352 ADT7310_Reset();
353
354 return status;
355 }
356