|
@@ -0,0 +1,298 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: Jon Trulson <jtrulson@ics.com>
|
|
3
|
+ * Copyright (c) 2016 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+ * a copy of this software and associated documentation files (the
|
|
7
|
+ * "Software"), to deal in the Software without restriction, including
|
|
8
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+ * the following conditions:
|
|
12
|
+ *
|
|
13
|
+ * The above copyright notice and this permission notice shall be
|
|
14
|
+ * included in all copies or substantial portions of the Software.
|
|
15
|
+ *
|
|
16
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+ */
|
|
24
|
+
|
|
25
|
+#include <iostream>
|
|
26
|
+#include <time.h>
|
|
27
|
+#include <stdexcept>
|
|
28
|
+
|
|
29
|
+#include "ds18b20.h"
|
|
30
|
+
|
|
31
|
+using namespace upm;
|
|
32
|
+using namespace std;
|
|
33
|
+
|
|
34
|
+// conversion from celcius to fahrenheit
|
|
35
|
+static float c2f(float c)
|
|
36
|
+{
|
|
37
|
+ return (c * (9.0 / 5.0) + 32.0);
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+DS18B20::DS18B20(int uart) :
|
|
41
|
+ m_uart(uart)
|
|
42
|
+{
|
|
43
|
+ m_devicesFound = 0;
|
|
44
|
+
|
|
45
|
+ // check basic access to the 1-wire bus (presence detect)
|
|
46
|
+ mraa::Result rv;
|
|
47
|
+
|
|
48
|
+ if ((rv = m_uart.reset()) != mraa::SUCCESS)
|
|
49
|
+ {
|
|
50
|
+ throw std::runtime_error(std::string(__FUNCTION__) +
|
|
51
|
+ ": reset() failed, no devices on bus?");
|
|
52
|
+ }
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+DS18B20::~DS18B20()
|
|
56
|
+{
|
|
57
|
+}
|
|
58
|
+
|
|
59
|
+void DS18B20::init()
|
|
60
|
+{
|
|
61
|
+ // iterate through the bus and build up a list of detected DS18B20
|
|
62
|
+ // devices (only)
|
|
63
|
+
|
|
64
|
+ // empty the map, in case this method has already been run once
|
|
65
|
+ // before
|
|
66
|
+ m_devicesFound = 0;
|
|
67
|
+ m_deviceMap.clear();
|
|
68
|
+
|
|
69
|
+ sensor_info_t sinfo;
|
|
70
|
+
|
|
71
|
+ // defaults
|
|
72
|
+ sinfo.temperature = 0.0;
|
|
73
|
+ sinfo.resolution = RESOLUTION_12BITS;
|
|
74
|
+
|
|
75
|
+ // start the search from scratch
|
|
76
|
+ string id = m_uart.search(true);
|
|
77
|
+ if (id.empty())
|
|
78
|
+ {
|
|
79
|
+ throw std::runtime_error(std::string(__FUNCTION__) +
|
|
80
|
+ ": no devices detected on bus");
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ while (!id.empty())
|
|
84
|
+ {
|
|
85
|
+ // The first byte (id[0]]) is the device type (family) code. We
|
|
86
|
+ // are only interested in the family code for these devices.
|
|
87
|
+
|
|
88
|
+ if ((uint8_t)id[0] == DS18B20_FAMILY_CODE)
|
|
89
|
+ {
|
|
90
|
+ // we have a winner, add it to our map and continue searching
|
|
91
|
+
|
|
92
|
+ sinfo.id = id;
|
|
93
|
+ m_deviceMap[m_devicesFound] = sinfo;
|
|
94
|
+
|
|
95
|
+ m_devicesFound++;
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ // continue search
|
|
99
|
+ id = m_uart.search(false);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ if (!m_devicesFound)
|
|
103
|
+ {
|
|
104
|
+ throw std::runtime_error(std::string(__FUNCTION__) +
|
|
105
|
+ ": no DS18B20 devices found on bus");
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ // iterate through the found devices and query their resolutions
|
|
109
|
+ for (int i=0; i<m_devicesFound; i++)
|
|
110
|
+ {
|
|
111
|
+ // read only the first 5 bytes of the scratchpad
|
|
112
|
+ static const int numScratch = 5;
|
|
113
|
+ uint8_t scratch[numScratch];
|
|
114
|
+
|
|
115
|
+ m_uart.command(CMD_READ_SCRATCHPAD, m_deviceMap[i].id);
|
|
116
|
+ for (int j=0; j<numScratch; j++)
|
|
117
|
+ scratch[j] = m_uart.readByte();
|
|
118
|
+
|
|
119
|
+ // config byte, shift the resolution to bit 0
|
|
120
|
+ scratch[4] >>= _CFG_RESOLUTION_SHIFT;
|
|
121
|
+
|
|
122
|
+ switch (scratch[4] & _CFG_RESOLUTION_MASK)
|
|
123
|
+ {
|
|
124
|
+ case 0: m_deviceMap[i].resolution = RESOLUTION_9BITS; break;
|
|
125
|
+ case 1: m_deviceMap[i].resolution = RESOLUTION_10BITS; break;
|
|
126
|
+ case 2: m_deviceMap[i].resolution = RESOLUTION_11BITS; break;
|
|
127
|
+ case 3: m_deviceMap[i].resolution = RESOLUTION_12BITS; break;
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ // reset the bus
|
|
131
|
+ m_uart.reset();
|
|
132
|
+ }
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+void DS18B20::update(int index)
|
|
136
|
+{
|
|
137
|
+ if (index >= m_devicesFound)
|
|
138
|
+ {
|
|
139
|
+ throw std::out_of_range(std::string(__FUNCTION__) +
|
|
140
|
+ ": device index out of range");
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ // should we update all of them?
|
|
144
|
+ bool doAll = (index < 0) ? true : false;
|
|
145
|
+
|
|
146
|
+ if (doAll)
|
|
147
|
+ {
|
|
148
|
+ // if we want to update all of them, we will first send the
|
|
149
|
+ // convert command to all of them, then wait. This will be
|
|
150
|
+ // faster, timey-wimey wise, then converting, sleeping, and
|
|
151
|
+ // reading each individual sensor.
|
|
152
|
+
|
|
153
|
+ for (int i=0; i<m_devicesFound; i++)
|
|
154
|
+ m_uart.command(CMD_CONVERT, m_deviceMap[i].id);
|
|
155
|
+ }
|
|
156
|
+ else
|
|
157
|
+ m_uart.command(CMD_CONVERT, m_deviceMap[index].id);
|
|
158
|
+
|
|
159
|
+ // wait for conversion(s) to finish
|
|
160
|
+ usleep(750000); // 750ms max
|
|
161
|
+
|
|
162
|
+ if (doAll)
|
|
163
|
+ {
|
|
164
|
+ for (int i=0; i<m_devicesFound; i++)
|
|
165
|
+ m_deviceMap[i].temperature = readSingleTemp(i);
|
|
166
|
+ }
|
|
167
|
+ else
|
|
168
|
+ m_deviceMap[index].temperature = readSingleTemp(index);
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+// utility function to read temp data from a single sensor
|
|
172
|
+float DS18B20::readSingleTemp(int index)
|
|
173
|
+{
|
|
174
|
+ if (index < 0 || index >= m_devicesFound)
|
|
175
|
+ {
|
|
176
|
+ throw std::out_of_range(std::string(__FUNCTION__) +
|
|
177
|
+ ": device index out of range");
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ static const int numScratch = 9;
|
|
181
|
+ uint8_t scratch[numScratch];
|
|
182
|
+
|
|
183
|
+ // read the 9-byte scratchpad
|
|
184
|
+ m_uart.command(CMD_READ_SCRATCHPAD, m_deviceMap[index].id);
|
|
185
|
+ for (int i=0; i<numScratch; i++)
|
|
186
|
+ scratch[i] = m_uart.readByte();
|
|
187
|
+
|
|
188
|
+ // validate cksum -- if we get an error, we will warn and simply
|
|
189
|
+ // return the current (previously read) temperature
|
|
190
|
+ uint8_t crc = m_uart.crc8(scratch, 8);
|
|
191
|
+
|
|
192
|
+ if (crc != scratch[8])
|
|
193
|
+ {
|
|
194
|
+ cerr << __FUNCTION__ << ": crc check failed for device "
|
|
195
|
+ << index << ", returning previously measured temperature" << endl;
|
|
196
|
+ return m_deviceMap[index].temperature;
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ // check the sign bit(s)
|
|
200
|
+ bool negative = (scratch[1] & 0x80) ? true : false;
|
|
201
|
+
|
|
202
|
+ // shift everything into position
|
|
203
|
+ int16_t temp = (scratch[1] << 8) | scratch[0];
|
|
204
|
+
|
|
205
|
+ // grab the fractional
|
|
206
|
+ uint8_t frac = temp & 0x0f;
|
|
207
|
+
|
|
208
|
+ // depending on the resolution, some frac bits should be ignored, so
|
|
209
|
+ // we mask them off. For 12bits, all bits are valid so we leve them
|
|
210
|
+ // alone.
|
|
211
|
+
|
|
212
|
+ switch (m_deviceMap[index].resolution)
|
|
213
|
+ {
|
|
214
|
+ case RESOLUTION_9BITS: frac &= 0x08; break;
|
|
215
|
+ case RESOLUTION_10BITS: frac &= 0x0c; break;
|
|
216
|
+ case RESOLUTION_11BITS: frac &= 0x0e; break;
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ // remove the fractional with extreme prejudice
|
|
220
|
+ temp >>= 4;
|
|
221
|
+
|
|
222
|
+ // compensate for sign
|
|
223
|
+ if (negative)
|
|
224
|
+ temp -= 65536; // 2^^16
|
|
225
|
+
|
|
226
|
+ // convert
|
|
227
|
+ return ( float(temp) + (float(frac) * 0.0625) );
|
|
228
|
+}
|
|
229
|
+
|
|
230
|
+float DS18B20::getTemperature(int index, bool fahrenheit)
|
|
231
|
+{
|
|
232
|
+ if (index < 0 || index >= m_devicesFound)
|
|
233
|
+ {
|
|
234
|
+ throw std::out_of_range(std::string(__FUNCTION__) +
|
|
235
|
+ ": device index out of range");
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ if (fahrenheit)
|
|
239
|
+ return c2f(m_deviceMap[index].temperature);
|
|
240
|
+ else
|
|
241
|
+ return m_deviceMap[index].temperature;
|
|
242
|
+}
|
|
243
|
+
|
|
244
|
+void DS18B20::setResolution(int index, RESOLUTIONS_T res)
|
|
245
|
+{
|
|
246
|
+ if (index < 0 || index >= m_devicesFound)
|
|
247
|
+ {
|
|
248
|
+ throw std::out_of_range(std::string(__FUNCTION__) +
|
|
249
|
+ ": device index out of range");
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+ static const int numScratch = 9;
|
|
253
|
+ uint8_t scratch[numScratch];
|
|
254
|
+
|
|
255
|
+ // read the 9-byte scratchpad
|
|
256
|
+ m_uart.command(CMD_READ_SCRATCHPAD, m_deviceMap[index].id);
|
|
257
|
+ for (int i=0; i<numScratch; i++)
|
|
258
|
+ scratch[i] = m_uart.readByte();
|
|
259
|
+
|
|
260
|
+ // resolution is stored in byte 4
|
|
261
|
+ scratch[4] = ((scratch[4] & ~(_CFG_RESOLUTION_MASK << _CFG_RESOLUTION_SHIFT))
|
|
262
|
+ | (res << _CFG_RESOLUTION_SHIFT));
|
|
263
|
+
|
|
264
|
+ // now, write back, we only write 3 bytes (2-4), no cksum.
|
|
265
|
+ m_uart.command(CMD_WRITE_SCRATCHPAD, m_deviceMap[index].id);
|
|
266
|
+ for (int i=0; i<3; i++)
|
|
267
|
+ m_uart.writeByte(scratch[i+2]);
|
|
268
|
+}
|
|
269
|
+
|
|
270
|
+void DS18B20::copyScratchPad(int index)
|
|
271
|
+{
|
|
272
|
+ if (index < 0 || index >= m_devicesFound)
|
|
273
|
+ {
|
|
274
|
+ throw std::out_of_range(std::string(__FUNCTION__) +
|
|
275
|
+ ": device index out of range");
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ // issue the command
|
|
279
|
+ m_uart.command(CMD_COPY_SCRATCHPAD, m_deviceMap[index].id);
|
|
280
|
+
|
|
281
|
+ sleep(1); // to be safe...
|
|
282
|
+}
|
|
283
|
+
|
|
284
|
+void DS18B20::recallEEPROM(int index)
|
|
285
|
+{
|
|
286
|
+ if (index < 0 || index >= m_devicesFound)
|
|
287
|
+ {
|
|
288
|
+ throw std::out_of_range(std::string(__FUNCTION__) +
|
|
289
|
+ ": device index out of range");
|
|
290
|
+ }
|
|
291
|
+
|
|
292
|
+ // issue the command
|
|
293
|
+ m_uart.command(CMD_RECALL_EEPROM, m_deviceMap[index].id);
|
|
294
|
+
|
|
295
|
+ // issue read timeslots until a '1' is read back, indicating completion
|
|
296
|
+ while (!m_uart.writeBit(1))
|
|
297
|
+ usleep(100);
|
|
298
|
+}
|