author | Heinz Junkes <junkes@fhi-berlin.mpg.de> |
Wed, 09 Sep 2015 18:06:59 +0200 | |
changeset 1 | 7029db7ac3db |
parent 0 | bd6bb22c6533 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2014 Adafruit Industries |
2 |
// Author: Tony DiCola |
|
3 |
||
4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|
5 |
// of this software and associated documentation files (the "Software"), to deal |
|
6 |
// in the Software without restriction, including without limitation the rights |
|
7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
8 |
// copies of the Software, and to permit persons to whom the Software is |
|
9 |
// furnished to do so, subject to the following conditions: |
|
10 |
||
11 |
// The above copyright notice and this permission notice shall be included in all |
|
12 |
// copies or substantial portions of the Software. |
|
13 |
||
14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
20 |
// SOFTWARE. |
|
21 |
#include <stdbool.h> |
|
22 |
#include <stdlib.h> |
|
23 |
||
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
24 |
#include "epicsExit.h" |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
25 |
#include "epicsThread.h" |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
26 |
#include "iocsh.h" |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
27 |
|
0 | 28 |
#include "pi_2_dht_read.h" |
29 |
#include "pi_2_mmio.h" |
|
30 |
||
31 |
// This is the only processor specific magic value, the maximum amount of time to |
|
32 |
// spin in a loop before bailing out and considering the read a timeout. This should |
|
33 |
// be a high value, but if you're running on a much faster platform than a Raspberry |
|
34 |
// Pi or Beaglebone Black then it might need to be increased. |
|
35 |
#define DHT_MAXCOUNT 32000 |
|
36 |
||
37 |
// Number of bit pulses to expect from the DHT. Note that this is 41 because |
|
38 |
// the first pulse is a constant 50 microsecond pulse, with 40 pulses to represent |
|
39 |
// the data afterwards. |
|
40 |
#define DHT_PULSES 41 |
|
41 |
||
42 |
int pi_2_dht_read(int type, int pin, float* humidity, float* temperature) { |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
43 |
int i; |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
44 |
volatile int j; |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
45 |
unsigned int defPrio; |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
46 |
epicsThreadId threadIdSelf; |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
47 |
|
0 | 48 |
// Validate humidity and temperature arguments and set them to zero. |
49 |
if (humidity == NULL || temperature == NULL) { |
|
50 |
return DHT_ERROR_ARGUMENT; |
|
51 |
} |
|
52 |
*temperature = 0.0f; |
|
53 |
*humidity = 0.0f; |
|
54 |
||
55 |
// Initialize GPIO library. |
|
56 |
if (pi_2_mmio_init() < 0) { |
|
57 |
return DHT_ERROR_GPIO; |
|
58 |
} |
|
59 |
||
60 |
// Store the count that each DHT bit pulse is low and high. |
|
61 |
// Make sure array is initialized to start at zero. |
|
62 |
int pulseCounts[DHT_PULSES*2] = {0}; |
|
63 |
||
64 |
// Set pin to output. |
|
65 |
pi_2_mmio_set_output(pin); |
|
66 |
||
67 |
// Bump up process priority and change scheduler to try to try to make process more 'real time'. |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
68 |
defPrio = epicsThreadGetPrioritySelf(); |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
69 |
threadIdSelf = epicsThreadGetIdSelf(); |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
70 |
epicsThreadSetPriority( threadIdSelf , epicsThreadPriorityHigh); |
0 | 71 |
|
72 |
// Set pin high for ~500 milliseconds. |
|
73 |
pi_2_mmio_set_high(pin); |
|
74 |
sleep_milliseconds(500); |
|
75 |
||
76 |
// The next calls are timing critical and care should be taken |
|
77 |
// to ensure no unnecssary work is done below. |
|
78 |
||
79 |
// Set pin low for ~20 milliseconds. |
|
80 |
pi_2_mmio_set_low(pin); |
|
81 |
busy_wait_milliseconds(20); |
|
82 |
||
83 |
// Set pin at input. |
|
84 |
pi_2_mmio_set_input(pin); |
|
85 |
// Need a very short delay before reading pins or else value is sometimes still low. |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
86 |
for (j = 0; j < 50; ++j) { } |
0 | 87 |
|
88 |
// Wait for DHT to pull pin low. |
|
89 |
uint32_t count = 0; |
|
90 |
while (pi_2_mmio_input(pin)) { |
|
91 |
if (++count >= DHT_MAXCOUNT) { |
|
92 |
// Timeout waiting for response. |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
93 |
epicsThreadSetPriority( threadIdSelf , defPrio); |
0 | 94 |
return DHT_ERROR_TIMEOUT; |
95 |
} |
|
96 |
} |
|
97 |
||
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
98 |
|
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
99 |
// Record pulse widths for the expected result bits. |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
100 |
for ( i=0; i < DHT_PULSES*2; i+=2) { |
0 | 101 |
// Count how long pin is low and store in pulseCounts[i] |
102 |
while (!pi_2_mmio_input(pin)) { |
|
103 |
if (++pulseCounts[i] >= DHT_MAXCOUNT) { |
|
104 |
// Timeout waiting for response. |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
105 |
epicsThreadSetPriority( threadIdSelf , defPrio); |
0 | 106 |
return DHT_ERROR_TIMEOUT; |
107 |
} |
|
108 |
} |
|
109 |
// Count how long pin is high and store in pulseCounts[i+1] |
|
110 |
while (pi_2_mmio_input(pin)) { |
|
111 |
if (++pulseCounts[i+1] >= DHT_MAXCOUNT) { |
|
112 |
// Timeout waiting for response. |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
113 |
epicsThreadSetPriority( threadIdSelf , defPrio); |
0 | 114 |
return DHT_ERROR_TIMEOUT; |
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
// Done with timing critical code, now interpret the results. |
|
120 |
// Drop back to normal priority. |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
121 |
// set_default_priority(); |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
122 |
epicsThreadSetPriority( threadIdSelf , defPrio); |
0 | 123 |
|
124 |
// Compute the average low pulse width to use as a 50 microsecond reference threshold. |
|
125 |
// Ignore the first two readings because they are a constant 80 microsecond pulse. |
|
126 |
uint32_t threshold = 0; |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
127 |
for (i=2; i < DHT_PULSES*2; i+=2) { |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
128 |
threshold += pulseCounts[i]; |
0 | 129 |
} |
130 |
threshold /= DHT_PULSES-1; |
|
131 |
||
132 |
// Interpret each high pulse as a 0 or 1 by comparing it to the 50us reference. |
|
133 |
// If the count is less than 50us it must be a ~28us 0 pulse, and if it's higher |
|
134 |
// then it must be a ~70us 1 pulse. |
|
135 |
uint8_t data[5] = {0}; |
|
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
136 |
for ( i=3; i < DHT_PULSES*2; i+=2) { |
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
137 |
int index = (i-3)/16; |
0 | 138 |
data[index] <<= 1; |
1
7029db7ac3db
Add adcPi device support
Heinz Junkes <junkes@fhi-berlin.mpg.de>
parents:
0
diff
changeset
|
139 |
if (pulseCounts[i] >= threshold) { |
0 | 140 |
// One bit for long pulse. |
141 |
data[index] |= 1; |
|
142 |
} |
|
143 |
// Else zero bit for short pulse. |
|
144 |
} |
|
145 |
||
146 |
// Useful debug info: |
|
147 |
//printf("Data: 0x%x 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3], data[4]); |
|
148 |
||
149 |
// Verify checksum of received data. |
|
150 |
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) { |
|
151 |
if (type == DHT11) { |
|
152 |
// Get humidity and temp for DHT11 sensor. |
|
153 |
*humidity = (float)data[0]; |
|
154 |
*temperature = (float)data[2]; |
|
155 |
} |
|
156 |
else if (type == DHT22) { |
|
157 |
// Calculate humidity and temp for DHT22 sensor. |
|
158 |
*humidity = (data[0] * 256 + data[1]) / 10.0f; |
|
159 |
*temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f; |
|
160 |
if (data[2] & 0x80) { |
|
161 |
*temperature *= -1.0f; |
|
162 |
} |
|
163 |
} |
|
164 |
return DHT_SUCCESS; |
|
165 |
} |
|
166 |
else { |
|
167 |
return DHT_ERROR_CHECKSUM; |
|
168 |
} |
|
169 |
} |