New drivers
[hackover2013-badge-firmware.git] / drivers / sensors / lm75b / lm75b.c
1 /**************************************************************************/
2 /*!
3 @file lm75b.c
4 @author K. Townsend (microBuilder.eu)
5 @date 22 March 2010
6 @version 0.10
7
8 @section DESCRIPTION
9
10 Driver for NXP's LM75B I2C temperature sensor. This temperature
11 sensor has an accuracy of 0.125°C, and returns a temperature value
12 in degrees celsius where each unit is equal to 0.125°C. For example,
13 if the temperature reading is 198, it means that the temperature in
14 degree celsius is: 198 / 8 = 24.75°C.
15
16 @section Example
17
18 @code
19 #include "core/cpu/cpu.h"
20 #include "drivers/sensors/lm75b/lm75b.h"
21
22 int main(void)
23 {
24 cpuInit();
25
26 int32_t temp = 0;
27
28 // Initialise the LM75B
29 lm75bInit();
30
31 while (1)
32 {
33 // Get the current temperature (in 0.125°C units)
34 lm75bGetTemperature(&temp);
35
36 // Multiply value by 125 for fixed-point math (0.125°C per unit)
37 temp *= 125;
38
39 // Use modulus operator to display decimal value
40 printf("Current Temperature: %d.%d C\n", temp / 1000, temp % 1000);
41
42 // Alternatively, you could also use floating point math, though
43 // this will result in larger compiled code if you add in floating
44 // point support for printf, etc.
45 //
46 // float tempFloat = 0.0F;
47 // lm75bGetTemperature(&temp);
48 // tempFloat = (float)temp / 8.0F;
49 }
50 }
51 @endcode
52
53 @section LICENSE
54
55 Software License Agreement (BSD License)
56
57 Copyright (c) 2010, microBuilder SARL
58 All rights reserved.
59
60 Redistribution and use in source and binary forms, with or without
61 modification, are permitted provided that the following conditions are met:
62 1. Redistributions of source code must retain the above copyright
63 notice, this list of conditions and the following disclaimer.
64 2. Redistributions in binary form must reproduce the above copyright
65 notice, this list of conditions and the following disclaimer in the
66 documentation and/or other materials provided with the distribution.
67 3. Neither the name of the copyright holders nor the
68 names of its contributors may be used to endorse or promote products
69 derived from this software without specific prior written permission.
70
71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
72 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
73 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
75 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
76 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
77 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
78 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
79 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
80 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81 */
82 /**************************************************************************/
83
84 #include "lm75b.h"
85
86 extern volatile uint8_t I2CMasterBuffer[I2C_BUFSIZE];
87 extern volatile uint8_t I2CSlaveBuffer[I2C_BUFSIZE];
88 extern volatile uint32_t I2CReadLength, I2CWriteLength;
89
90 static bool _lm75bInitialised = false;
91
92 /**************************************************************************/
93 /*!
94 @brief Writes an 8 bit values over I2C
95 */
96 /**************************************************************************/
97 lm75bError_e lm75bWrite8 (uint8_t reg, uint32_t value)
98 {
99 // Clear write buffers
100 uint32_t i;
101 for ( i = 0; i < I2C_BUFSIZE; i++ )
102 {
103 I2CMasterBuffer[i] = 0x00;
104 }
105
106 I2CWriteLength = 3;
107 I2CReadLength = 0;
108 I2CMasterBuffer[0] = LM75B_ADDRESS; // I2C device address
109 I2CMasterBuffer[1] = reg; // Command register
110 I2CMasterBuffer[2] = (value & 0xFF); // Value to write
111 i2cEngine();
112 return LM75B_ERROR_OK;
113 }
114
115 /**************************************************************************/
116 /*!
117 @brief Reads a 16 bit values over I2C
118 */
119 /**************************************************************************/
120 lm75bError_e lm75bRead16(uint8_t reg, int32_t *value)
121 {
122 // Clear write buffers
123 uint32_t i;
124 for ( i = 0; i < I2C_BUFSIZE; i++ )
125 {
126 I2CMasterBuffer[i] = 0x00;
127 }
128
129 I2CWriteLength = 2;
130 I2CReadLength = 2;
131 I2CMasterBuffer[0] = LM75B_ADDRESS; // I2C device address
132 I2CMasterBuffer[1] = reg; // Command register
133 // Append address w/read bit
134 I2CMasterBuffer[2] = LM75B_ADDRESS | LM75B_READBIT;
135 i2cEngine();
136
137 // Shift values to create properly formed integer
138 *value = ((I2CSlaveBuffer[0] << 8) | I2CSlaveBuffer[1]) >> 5;
139
140 // Sign extend negative numbers
141 if (I2CSlaveBuffer[0] & 0x80)
142 {
143 // Negative number
144 *value |= 0xFFFFFC00;
145 }
146 return LM75B_ERROR_OK;
147 }
148
149 /**************************************************************************/
150 /*!
151 @brief Initialises the I2C block
152 */
153 /**************************************************************************/
154 lm75bError_e lm75bInit(void)
155 {
156 // Initialise I2C
157 if (i2cInit(I2CMASTER) == false)
158 {
159 return LM75B_ERROR_I2CINIT; /* Fatal error */
160 }
161
162 _lm75bInitialised = true;
163
164 return LM75B_ERROR_OK;
165
166 // Set device to shutdown mode by default (saves power)
167 return lm75bConfigWrite (LM75B_CONFIG_SHUTDOWN_SHUTDOWN);
168 }
169
170 /**************************************************************************/
171 /*!
172 @brief Reads the current temperature from the LM75B
173
174 @note This method will assign a signed 32-bit value (int32) to 'temp',
175 where each unit represents +/- 0.125°C. To convert the numeric
176 value to degrees celsius, you must divide the value of 'temp'
177 by 8. This conversion is not done automatically, since you may
178 or may not want to use floating point math for the calculations.
179 */
180 /**************************************************************************/
181 lm75bError_e lm75bGetTemperature (int32_t *temp)
182 {
183 if (!_lm75bInitialised) lm75bInit();
184
185 // Turn device on
186 lm75bConfigWrite (LM75B_CONFIG_SHUTDOWN_POWERON);
187
188 // Read temperature
189 lm75bError_e error = LM75B_ERROR_OK;
190 error = lm75bRead16 (LM75B_REGISTER_TEMPERATURE, temp);
191
192 // Shut device back down
193 lm75bConfigWrite (LM75B_CONFIG_SHUTDOWN_SHUTDOWN);
194
195 return error;
196 }
197
198 /**************************************************************************/
199 /*!
200 @brief Writes the supplied 8-bit value to the LM75B config register
201 */
202 /**************************************************************************/
203 lm75bError_e lm75bConfigWrite (uint8_t configValue)
204 {
205 if (!_lm75bInitialised) lm75bInit();
206
207 return lm75bWrite8 (LM75B_REGISTER_CONFIGURATION, configValue);
208 }
This page took 0.04747 seconds and 5 git commands to generate.