Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADT7420.c
3 * @brief Implementation of ADT7420 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 "ADT7420.h"
47
48 /******************************************************************************/
49 /************************ Variables Definitions *******************************/
50 /******************************************************************************/
51 unsigned char resolutionSetting = 0; // Current resolution setting
52
53 /***************************************************************************//**
54 * @brief Reads the value of a register.
55 *
56 * @param registerAddress - Address of the register.
57 *
58 * @return registerValue - Value of the register.
59 *******************************************************************************/
60 unsigned char ADT7420_GetRegisterValue(unsigned char registerAddress)
61 {
62 unsigned char registerValue = 0;
63
64 I2C_Write(ADT7420_ADDRESS,
65 &registerAddress,
66 1,
67 0);
68 I2C_Read(ADT7420_ADDRESS,
69 &registerValue,
70 1,
71 1);
72
73 return registerValue;
74 }
75
76 /***************************************************************************//**
77 * @brief Sets the value of a register.
78 *
79 * @param registerAddress - Address of the register.
80 * @param registerValue - Value of the register.
81 *
82 * @return None.
83 *******************************************************************************/
84 void ADT7420_SetRegisterValue(unsigned char registerAddress,
85 unsigned char registerValue)
86 {
87 unsigned char dataBuffer[2] = {0, 0};
88
89 dataBuffer[0] = registerAddress;
90 dataBuffer[1] = registerValue;
91 I2C_Write(ADT7420_ADDRESS,
92 dataBuffer,
93 2,
94 1);
95 }
96
97 /***************************************************************************//**
98 * @brief Initializes the communication peripheral and checks if the device is
99 * present.
100 *
101 * @return status - The result of the initialization procedure.
102 * Example: -1 - I2C peripheral was not initialized or the
103 * device is not present.
104 * 0 - I2C peripheral was initialized and the
105 * device is present.
106 *******************************************************************************/
107 char ADT7420_Init(void)
108 {
109 unsigned char status = -1;
110 unsigned char test = 0;
111
112 status = I2C_Init(100000);
113 test = ADT7420_GetRegisterValue(ADT7420_REG_ID);
114 if(test != ADT7420_DEFAULT_ID)
115 {
116 status = -1;
117 }
118
119 return status;
120 }
121
122 /***************************************************************************//**
123 * @brief Resets the ADT7420.
124 * The ADT7420 does not respond to I2C bus commands while the default
125 * values upload (approximately 200 us).
126 *
127 * @return None.
128 *******************************************************************************/
129 void ADT7420_Reset(void)
130 {
131 unsigned char registerAddress = ADT7420_REG_RESET;
132
133 I2C_Write(ADT7420_ADDRESS,
134 &registerAddress,
135 1,
136 1);
137 resolutionSetting = 0;
138 }
139
140 /***************************************************************************//**
141 * @brief Sets the operational mode for ADT7420.
142 *
143 * @param mode - Operation mode.
144 * Example: ADT7420_OP_MODE_CONT_CONV - continuous conversion;
145 * ADT7420_OP_MODE_ONE_SHOT - one shot;
146 * ADT7420_OP_MODE_1_SPS - 1 SPS mode;
147 * ADT7420_OP_MODE_SHUTDOWN - shutdown.
148 *
149 * @return None.
150 *******************************************************************************/
151 void ADT7420_SetOperationMode(unsigned char mode)
152 {
153 unsigned char registerValue = 0;
154
155 registerValue = ADT7420_GetRegisterValue(ADT7420_REG_CONFIG);
156 registerValue &= ~ADT7420_CONFIG_OP_MODE(ADT7420_OP_MODE_SHUTDOWN);
157 registerValue |= ADT7420_CONFIG_OP_MODE(mode);
158 ADT7420_SetRegisterValue(ADT7420_REG_CONFIG, registerValue);
159 }
160
161 /***************************************************************************//**
162 * @brief Sets the resolution for ADT7420.
163 *
164 * @param resolution - Resolution.
165 * Example: 0 - 13-bit resolution;
166 * 1 - 16-bit resolution.
167 *
168 * @return None.
169 *******************************************************************************/
170 void ADT7420_SetResolution(unsigned char resolution)
171 {
172 unsigned char registerValue = 0;
173
174 registerValue = ADT7420_GetRegisterValue(ADT7420_REG_CONFIG);
175 registerValue &= ~ADT7420_CONFIG_RESOLUTION;
176 registerValue |= (resolution * ADT7420_CONFIG_RESOLUTION);
177 ADT7420_SetRegisterValue(ADT7420_REG_CONFIG, registerValue);
178 resolutionSetting = resolution;
179 }
180
181 /***************************************************************************//**
182 * @brief Reads the temperature data and converts it to Celsius degrees.
183 *
184 * @return temperature - Temperature in degrees Celsius.
185 *******************************************************************************/
186 float ADT7420_GetTemperature(void)
187 {
188 unsigned char msbTemp = 0;
189 unsigned char lsbTemp = 0;
190 unsigned short temp = 0;
191 float tempC = 0;
192
193 msbTemp = ADT7420_GetRegisterValue(ADT7420_REG_TEMP_MSB);
194 lsbTemp = ADT7420_GetRegisterValue(ADT7420_REG_TEMP_LSB);
195 temp = ((unsigned short)msbTemp << 8) + lsbTemp;
196 if(resolutionSetting)
197 {
198 if(temp & 0x8000)
199 {
200 /*! Negative temperature */
201 tempC = (float)((signed long)temp - 65536) / 128;
202 }
203 else
204 {
205 /*! Positive temperature */
206 tempC = (float)temp / 128;
207 }
208 }
209 else
210 {
211 temp >>= 3;
212 if(temp & 0x1000)
213 {
214 /*! Negative temperature */
215 tempC = (float)((signed long)temp - 8192) / 16;
216 }
217 else
218 {
219 /*! Positive temperature */
220 tempC = (float)temp / 16;
221 }
222 }
223
224 return tempC;
225 }