|
1 /* |
|
2 * MIT License |
|
3 * |
|
4 * Copyright (c) 2017 David Antliff |
|
5 * |
|
6 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7 * of this software and associated documentation files (the "Software"), to deal |
|
8 * in the Software without restriction, including without limitation the rights |
|
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10 * copies of the Software, and to permit persons to whom the Software is |
|
11 * furnished to do so, subject to the following conditions: |
|
12 * |
|
13 * The above copyright notice and this permission notice shall be included in all |
|
14 * copies or substantial portions of the Software. |
|
15 * |
|
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22 * SOFTWARE. |
|
23 */ |
|
24 |
|
25 /** |
|
26 * @file ds18b20.h |
|
27 * @brief Interface definitions for the Maxim Integrated DS18B20 Programmable |
|
28 * Resolution 1-Wire Digital Thermometer device. |
|
29 * |
|
30 * This component provides structures and functions that are useful for communicating |
|
31 * with DS18B20 devices connected via a Maxim Integrated 1-Wire® bus. |
|
32 */ |
|
33 |
|
34 #ifndef DS18B20_H |
|
35 #define DS18B20_H |
|
36 |
|
37 #include "owb.h" |
|
38 |
|
39 #ifdef __cplusplus |
|
40 extern "C" { |
|
41 #endif |
|
42 |
|
43 /** |
|
44 * @brief Success and error codes. |
|
45 */ |
|
46 typedef enum |
|
47 { |
|
48 DS18B20_ERROR_UNKNOWN = -1, |
|
49 DS18B20_OK = 0, |
|
50 DS18B20_ERROR_DEVICE, ///< A device error occurred |
|
51 DS18B20_ERROR_CRC, ///< A CRC error occurred |
|
52 DS18B20_ERROR_OWB, ///< A One Wire Bus error occurred |
|
53 DS18B20_ERROR_NULL, ///< A parameter or value is NULL |
|
54 } DS18B20_ERROR; |
|
55 |
|
56 /** |
|
57 * @brief Symbols for the supported temperature resolution of the device. |
|
58 */ |
|
59 typedef enum |
|
60 { |
|
61 DS18B20_RESOLUTION_INVALID = -1, ///< Invalid resolution |
|
62 DS18B20_RESOLUTION_9_BIT = 9, ///< 9-bit resolution, LSB bits 2,1,0 undefined |
|
63 DS18B20_RESOLUTION_10_BIT = 10, ///< 10-bit resolution, LSB bits 1,0 undefined |
|
64 DS18B20_RESOLUTION_11_BIT = 11, ///< 11-bit resolution, LSB bit 0 undefined |
|
65 DS18B20_RESOLUTION_12_BIT = 12, ///< 12-bit resolution (default) |
|
66 } DS18B20_RESOLUTION; |
|
67 |
|
68 /** |
|
69 * @brief Structure containing information related to a single DS18B20 device connected |
|
70 * via a 1-Wire bus. |
|
71 */ |
|
72 typedef struct |
|
73 { |
|
74 bool init; ///< True if struct has been initialised, otherwise false |
|
75 bool solo; ///< True if device is intended to be the only one connected to the bus, otherwise false |
|
76 bool use_crc; ///< True if CRC checks are to be used when retrieving information from a device on the bus |
|
77 const OneWireBus * bus; ///< Pointer to 1-Wire bus information relevant to this device |
|
78 OneWireBus_ROMCode rom_code; ///< The ROM code used to address this device on the bus |
|
79 DS18B20_RESOLUTION resolution; ///< Temperature measurement resolution per reading |
|
80 } DS18B20_Info; |
|
81 |
|
82 /** |
|
83 * @brief Construct a new device info instance. |
|
84 * New instance should be initialised before calling other functions. |
|
85 * @return Pointer to new device info instance, or NULL if it cannot be created. |
|
86 */ |
|
87 DS18B20_Info * ds18b20_malloc(void); |
|
88 |
|
89 /** |
|
90 * @brief Delete an existing device info instance. |
|
91 * @param[in] ds18b20_info Pointer to device info instance. |
|
92 * @param[in,out] ds18b20_info Pointer to device info instance that will be freed and set to NULL. |
|
93 */ |
|
94 void ds18b20_free(DS18B20_Info ** ds18b20_info); |
|
95 |
|
96 /** |
|
97 * @brief Initialise a device info instance with the specified GPIO. |
|
98 * @param[in] ds18b20_info Pointer to device info instance. |
|
99 * @param[in] bus Pointer to initialised 1-Wire bus instance. |
|
100 * @param[in] rom_code Device-specific ROM code to identify a device on the bus. |
|
101 */ |
|
102 void ds18b20_init(DS18B20_Info * ds18b20_info, const OneWireBus * bus, OneWireBus_ROMCode rom_code); |
|
103 |
|
104 /** |
|
105 * @brief Initialise a device info instance with the specified GPIO as a solo device on the bus. |
|
106 * |
|
107 * This is subject to the requirement that this device is the ONLY device on the bus. |
|
108 * This allows for faster commands to be used without ROM code addressing. |
|
109 * |
|
110 * NOTE: if additional devices are added to the bus, operation will cease to work correctly. |
|
111 * |
|
112 * @param[in] ds18b20_info Pointer to device info instance. |
|
113 * @param[in] bus Pointer to initialised 1-Wire bus instance. |
|
114 */ |
|
115 void ds18b20_init_solo(DS18B20_Info * ds18b20_info, const OneWireBus * bus); |
|
116 |
|
117 /** |
|
118 * @brief Enable or disable use of CRC checks on device communications. |
|
119 * @param[in] ds18b20_info Pointer to device info instance. |
|
120 * @param[in] use_crc True to enable CRC checks, false to disable. |
|
121 */ |
|
122 void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc); |
|
123 |
|
124 /** |
|
125 * @brief Set temperature measurement resolution. |
|
126 * |
|
127 * This programs the hardware to the specified resolution and sets the cached value to be the same. |
|
128 * If the program fails, the value currently in hardware is used to refresh the cache. |
|
129 * |
|
130 * @param[in] ds18b20_info Pointer to device info instance. |
|
131 * @param[in] resolution Selected resolution. |
|
132 * @return True if successful, otherwise false. |
|
133 */ |
|
134 bool ds18b20_set_resolution(DS18B20_Info * ds18b20_info, DS18B20_RESOLUTION resolution); |
|
135 |
|
136 /** |
|
137 * @brief Update and return the current temperature measurement resolution from the device. |
|
138 * @param[in] ds18b20_info Pointer to device info instance. |
|
139 * @return The currently configured temperature measurement resolution. |
|
140 */ |
|
141 DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info * ds18b20_info); |
|
142 |
|
143 /** |
|
144 * @brief Read 64-bit ROM code from device - only works when there is a single device on the bus. |
|
145 * @param[in] ds18b20_info Pointer to device info instance. |
|
146 * @return The 64-bit value read from the device's ROM. |
|
147 */ |
|
148 OneWireBus_ROMCode ds18b20_read_rom(DS18B20_Info * ds18b20_info); |
|
149 |
|
150 /** |
|
151 * @brief Start a temperature measurement conversion on a single device. |
|
152 * @param[in] ds18b20_info Pointer to device info instance. |
|
153 * @return True if successfull. |
|
154 */ |
|
155 bool ds18b20_convert(const DS18B20_Info * ds18b20_info); |
|
156 |
|
157 /** |
|
158 * @brief Start temperature conversion on all connected devices. |
|
159 * |
|
160 * This should be followed by a sufficient delay to ensure all devices complete |
|
161 * their conversion before the measurements are read. |
|
162 * @param[in] bus Pointer to initialised bus instance. |
|
163 */ |
|
164 void ds18b20_convert_all(const OneWireBus * bus); |
|
165 |
|
166 /** |
|
167 * @brief Wait for the maximum conversion time according to the current resolution of the device. |
|
168 * @param[in] ds18b20_info Pointer to device info instance. Must be initialised first. |
|
169 * @return An estimate of the time elapsed, in milliseconds. Actual elapsed time may be greater. |
|
170 */ |
|
171 float ds18b20_wait_for_conversion(const DS18B20_Info * ds18b20_info); |
|
172 |
|
173 /** |
|
174 * @brief Read last temperature measurement from device. |
|
175 * |
|
176 * This is typically called after ds18b20_start_mass_conversion(), provided enough time |
|
177 * has elapsed to ensure that all devices have completed their conversions. |
|
178 * @param[in] ds18b20_info Pointer to device info instance. Must be initialised first. |
|
179 * @param[out] value Pointer to the measurement value returned by the device, in degrees Celsius. |
|
180 * @return DS18B20_OK if read is successful, otherwise error. |
|
181 */ |
|
182 DS18B20_ERROR ds18b20_read_temp(const DS18B20_Info * ds18b20_info, float * value); |
|
183 |
|
184 /** |
|
185 * @brief Convert, wait and read current temperature from device. |
|
186 * @param[in] ds18b20_info Pointer to device info instance. Must be initialised first. |
|
187 * @param[out] value Pointer to the measurement value returned by the device, in degrees Celsius. |
|
188 * @return DS18B20_OK if read is successful, otherwise error. |
|
189 */ |
|
190 DS18B20_ERROR ds18b20_convert_and_read_temp(const DS18B20_Info * ds18b20_info, float * value); |
|
191 |
|
192 #ifdef __cplusplus |
|
193 } |
|
194 #endif |
|
195 |
|
196 #endif // DS18B20_H |