-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patham2302.c
235 lines (197 loc) · 5.43 KB
/
am2302.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* DHT21/DHT22 driver
*/
#include "ch.h"
#include "hal.h"
#include "am2302.h"
#define DHT_BIT_TIMEOUT_US (80 * 4) /* one bit timeout */
#define DHT_START_PULSE_MS 18
#define DHT_IRQ_TIMEOUT_MS 2 /* irq timeout */
#define DHT_PKT_SIZE 5
#define DHT_PKT_TIMEOUT_MS 10
#define DHT_CHANNEL1_GPIO GPIOA /* channel 1 GPIO */
#define DHT_CHANNEL2_GPIO GPIOB /* channel 2 GPIO */
#define DHT_CHANNEL1_PIN 15 /* channel 1 PIN */
#define DHT_CHANNEL2_PIN 3 /* channel 2 PIN */
#define DHT_CHANNEL1 ICU_CHANNEL_1
#define DHT_CHANNEL2 ICU_CHANNEL_2
#define ERROR_DIV 2
#define PERIOD_OK(x, l, h) \
((x)->low >= ((l) - (l) / ERROR_DIV) && \
(x)->low < ((l) + (l) / ERROR_DIV) && \
((x)->period - (x)->low) >= ((h) - (h) / ERROR_DIV) && \
((x)->period - (x)->low) < ((h) + (h) / ERROR_DIV))
/*-----------------------------------------------------------------------------*/
typedef struct _icu_capture_t icu_capture_t;
struct _icu_capture_t {
uint16_t period;
uint16_t low;
};
typedef struct _dht_read_t dht_read_t;
struct _dht_read_t {
uint8_t channel; /* in */
dht_error_t error; /* out */
uint8_t data[DHT_PKT_SIZE]; /* out */
};
static BinarySemaphore icusem, cb_sem;
static volatile icu_capture_t icu_data;
static void icuwidthcb(ICUDriver *icup) {
icu_data.low = icuGetWidth(icup);
}
static void icuperiodcb(ICUDriver *icup) {
icu_data.period = icuGetPeriod(icup);
chBSemSignalI(&cb_sem);
}
static void icuoverflowcb(ICUDriver *icup) {
(void)icup;
icu_data.period = 0;
chBSemSignalI(&cb_sem);
}
static ICUConfig icucfgch1 = {
ICU_INPUT_ACTIVE_LOW,
1000000, /* 1mHz ICU clock frequency. */
icuwidthcb,
icuperiodcb,
icuoverflowcb,
DHT_CHANNEL1,
0
};
static ICUConfig icucfgch2 = {
ICU_INPUT_ACTIVE_LOW,
1000000, /* 1mHz ICU clock frequency. */
icuwidthcb,
icuperiodcb,
icuoverflowcb,
DHT_CHANNEL2,
0
};
/*
* AM2302 read thread.
*/
static Thread *DHTThread_p;
static WORKING_AREA(waDHTThread, 128);
__attribute__((noreturn))
static msg_t DHTThread(void *arg) {
(void)arg;
chRegSetThreadName("DHTThd");
chBSemInit(&cb_sem, TRUE);
while (TRUE) {
/* wait for read request */
dht_read_t *req;
Thread *tp;
tp = chMsgWait();
req = (dht_read_t *) chMsgGet(tp);
chMsgRelease(tp, (msg_t) req);
AFIO->MAPR |= AFIO_MAPR_TIM2_REMAP_NOREMAP;
// set DHT pin low on 2ms
if (req->channel == 1){
palSetPadMode(DHT_CHANNEL1_GPIO, DHT_CHANNEL1_PIN, PAL_MODE_OUTPUT_OPENDRAIN);
palClearPad(DHT_CHANNEL1_GPIO, DHT_CHANNEL1_PIN);
}
else if (req->channel == 2){
palSetPadMode(DHT_CHANNEL2_GPIO, DHT_CHANNEL2_PIN, PAL_MODE_OUTPUT_OPENDRAIN);
palClearPad(DHT_CHANNEL2_GPIO, DHT_CHANNEL2_PIN);
}
chThdSleepMilliseconds(DHT_START_PULSE_MS);
AFIO->MAPR |= AFIO_MAPR_TIM2_REMAP_FULLREMAP;
if (req->channel == 1){
icuStart(&ICUD2, &icucfgch1);
}
else if (req->channel == 2){
icuStart(&ICUD2, &icucfgch2);
}
if (req->channel == 1){
palSetPadMode(DHT_CHANNEL1_GPIO, DHT_CHANNEL1_PIN, PAL_MODE_INPUT);
}
else if (req->channel == 2){
palSetPadMode(DHT_CHANNEL2_GPIO, DHT_CHANNEL2_PIN, PAL_MODE_INPUT);
}
icuEnable(&ICUD2);
/* skip first falling edge */
int i;
// IRQ timeout or receive timeout
if(chBSemWaitTimeout(&cb_sem, MS2ST(DHT_IRQ_TIMEOUT_MS)) == RDY_TIMEOUT) {
req->error = DHT_IRQ_TIMEOUT;
goto reply;
}
if(!icu_data.period) {
req->error = DHT_TIMEOUT;
goto reply;
}
/* start sequence received */
if(!PERIOD_OK(&icu_data, 80, 80)) {
req->error = DHT_DECODE_ERROR;
goto reply;
}
for(i = 0; i < DHT_PKT_SIZE; i++) {
unsigned int mask = 0x80;
uint8_t byte = 0;
while(mask) {
if(chBSemWaitTimeout(&cb_sem, MS2ST(DHT_IRQ_TIMEOUT_MS)) == RDY_TIMEOUT) {
req->error = DHT_IRQ_TIMEOUT;
goto reply;
}
if(!icu_data.period) {
req->error = DHT_TIMEOUT;
goto reply;
}
/* next bit received */
if(PERIOD_OK(&icu_data, 50, 70)) {
byte |= mask; /* 1 */
} else if(!PERIOD_OK(&icu_data, 50, 27)) {
req->error = DHT_DECODE_ERROR;
goto reply;
}
mask >>= 1;
}
req->data[i] = byte;
}
req->error = DHT_NO_ERROR;
reply:
icuDisable(&ICUD2);
icuStop(&ICUD2);
chBSemSignal(&icusem);
}
}
dht_error_t dht_read(uint8_t channel, int *temperature, int *humidity) {
dht_read_t rd;
dht_read_t *rd_p = &rd;
rd.channel = channel;
chBSemWait(&icusem); /* to be sure */
chMsgSend(DHTThread_p, (msg_t) rd_p);
/* wait for reply */
if(chBSemWaitTimeout(&icusem, MS2ST(DHT_PKT_TIMEOUT_MS)) == RDY_TIMEOUT) {
chBSemReset(&icusem, FALSE);
return DHT_RCV_TIMEOUT;
}
chBSemReset(&icusem, FALSE);
if(rd.error != DHT_NO_ERROR) {
return rd.error;
}
/* compute checksum */
unsigned int sum = 0;
uint8_t i;
for(i = 0; i < DHT_PKT_SIZE - 1; i++) sum += rd.data[i];
if((sum & 0xff) != rd.data[i]) {
return DHT_CHECKSUM_ERROR;
}
if (rd.data[1] == 0 && rd.data[3] == 0) { // DHT11
*humidity = ((unsigned int)rd.data[0]);
*temperature = ((unsigned int)rd.data[2]);
}
else { // DHT21/22
/* read 16 bit humidity value */
*humidity = ((unsigned int)rd.data[0] << 8) |
(unsigned int)rd.data[1];
/* read 16 bit temperature value */
int val = ((unsigned int)rd.data[2] << 8) |
(unsigned int)rd.data[3];
*temperature = val & 0x8000 ? -(val & ~0x8000) : val;
}
return DHT_NO_ERROR;
}
void dht_init(void){
AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
chBSemInit(&icusem, FALSE);
DHTThread_p = chThdCreateStatic(waDHTThread, sizeof(waDHTThread), DHT_PRIO, DHTThread, NULL);
}