This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdht_gpio.c
499 lines (434 loc) · 13.5 KB
/
dht_gpio.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/************************************************************************
This file is part of the libdht "DHT Temperature & Humidity Sensor"
library.
This is the implementation of the DHT sensor reading functions using
GPIO pins as communication bus.
Author: Ondrej Wisniewski
Features:
- Support for DHT11 and DHT22/AM2302/RHT03
- Auto detect sensor model
Based on code for Arduino from Mark Ruys, [email protected]
http://www.github.com./markruys/arduino-DHT
Changelog:
18-10-2013: Initial version (porting from arduino-DHT)
17-03-2014: Added functions for sensor power switching
11-11-2014: Moved the GPIO specific functions into their own file
24-11-2014: Changed handling of sysfs filenames to support different
namig schemes used by various micro processors and
kernel versions
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <stdint.h>
#include "dht.h"
// Debug mode: set to 1 to print debug information
#define DEBUG 0
// For systems not using the standard GPIO sysfs naming scheme
// /sys/class/gpio/gpio<N>
// uncomment the line referring to your system
//#define AT91_SYSFS
#define GPIO_BASE_DIR "/sys/class/gpio"
#define EXPORT_FILE "/sys/class/gpio/export"
#define UNEXPORT_FILE "/sys/class/gpio/unexport"
// timing parameters for serial bit detection
// (numbers are in microseconds)
#define MAX_PULSE_LENGTH_ZERO 50 // 26-28us
#define MAX_PULSE_LENGTH_ONE 120 // 70us
#define MAX_BIT_LENGTH MAX_PULSE_LENGTH_ONE
#define MAX_RESPONSE_BITS 40 // 5 bytes
#define MAX_RESPONSE_EDGES MAX_RESPONSE_BITS*2
#define INIT_DELAY 500000
#define DHT11_START_DELAY 20*1000 // min 18ms
#define DHT22_START_DELAY 1000 // min 800us
/* Imported variables */
extern float temperature;
extern float humidity;
extern uint8_t data_pin;
extern DHT_MODEL_t sensor_model;
extern DHT_ERROR_t error_code;
extern uint32_t last_read_time;
static int value_fd;
static int direction_fd;
/*********************************************************************
* INTERNAL FUNCTIONS
********************************************************************/
/*********************************************************************
* Function: pinMode()
*
* Description: Set the direction mode for the data pin
*
* Parameters: iomode - direction (IN|OUT)
*
********************************************************************/
static void pinMode(DHT_IOMODE_t iomode)
{
int fd=direction_fd;
int res;
if (iomode == INPUT)
res = pwrite(fd, "in", 3, 0);
else
res = pwrite(fd, "out", 4, 0);
if (res < 0) {
fprintf(stderr, "Unable to pwrite to gpio direction for pin %d: %s\n",
data_pin, strerror(errno));
}
}
/*********************************************************************
* Function: digitalWrite()
*
* Description: Write to the data pin
*
* Parameters: value - outpur value (HIGF|LOW)
*
********************************************************************/
static void digitalWrite(PIN_STATE_t value)
{
int fd=value_fd;
char d[1];
d[0] = (value == LOW ? '0' : '1');
if (pwrite(fd, d, 1, 0) != 1) {
fprintf(stderr, "Unable to pwrite %c to gpio value: %s\n",
d[0], strerror(errno));
}
}
/*********************************************************************
* Function: digitalRead()
*
* Description: Read from the data pin
*
* Parameters: none
*
********************************************************************/
static PIN_STATE_t digitalRead(void)
{
int fd=value_fd;
char d[1];
if (pread(fd, d, 1, 0) != 1) {
fprintf(stderr, "Unable to pread gpio value: %s\n",
strerror(errno));
}
return (d[0] == '0' ? LOW : HIGH);
}
/*********************************************************************
* Function: micros()
*
* Description: Reads the current time
*
* Parameters: none
*
* Return: The microsseconds part of the current time
*
********************************************************************/
static long micros(void)
{
static long first = -1;
long nsec;
struct timespec now_ts;
/* clock_gettime() needs '-lrt' on the link line */
if (clock_gettime(CLOCK_MONOTONIC, &now_ts) < 0) {
fprintf(stderr, "clock_gettime(CLOCK_MONOTONIC) failed: %s\n",
strerror(errno));
return 0;
}
// store first reading
if (first == -1) first=now_ts.tv_nsec;
// check for zero crossing of nano seconds
if (now_ts.tv_nsec < first)
nsec=now_ts.tv_nsec+1000000;
else
nsec=now_ts.tv_nsec;
// convert to micro seconds
return nsec/1000;
}
/*********************************************************************
* Function: check_clkres()
*
* Description: Checks the available clock resolution and issues
* a warning when it is not good enough (>1ms).
* This happens with kernels that don't have
* CONFIG_HIGH_RES_TIMERS option enabled.
*
* Parameters: none
*
* Return: none
*
********************************************************************/
static void check_clkres(void)
{
struct timespec res_ts;
clock_getres(CLOCK_MONOTONIC, &res_ts);
if (res_ts.tv_nsec > 1000) {
fprintf(stderr, "WARNING: clock resolution (%uns) is not good enough on you system\n",
(unsigned int)res_ts.tv_nsec);
}
}
/*********************************************************************
* Function: sysfs_filename()
*
* Description: Assembles the filename of the GPIO sysfs file
* associated to the pin number and function.
*
* The standard naming scheme according to kernel
* documentation is the following:
* /sys/class/gpio/gpio<id>/<function>
*
* However this seems to differ depending on the
* used micro processor and kernel version.
*
* Parameters: filename (out): complete name of sysfs file
* len (in) : length of the filename buffer
* pin (in) : GPIO Kernel Id of used IO pin
* function (in) : name of GPIO function
*
* Return: none
*
********************************************************************/
static void sysfs_filename(char *filename, int len, int pin, const char *function)
{
#ifdef AT91_SYSFS
// Use the naming scheme for AT91 micro processor family
snprintf(filename, len, "%s/pio%c%d/%s", GPIO_BASE_DIR, 'A'+pin/32, pin%32, function);
#else
// Use the standard naming scheme
snprintf(filename, len, "%s/gpio%d/%s", GPIO_BASE_DIR, pin, function);
#endif
// Note: might need to add more naming schemes here
}
/*********************************************************************
* PUBLIC FUNCTIONS
********************************************************************/
/*********************************************************************
* Function: dhtSetup_gpio()
*
* Description: Setup of globally used resources
*
* Parameters: pin - GPIO Kernel Id of used IO pin
* model - sensors model
*
********************************************************************/
void dhtSetup_gpio(uint8_t pin, DHT_MODEL_t model)
{
int fd;
char b[64];
// store globals
data_pin = pin;
sensor_model = model;
resetTimer(); // Make sure we do read the sensor in the next readSensor()
// Prepare GPIO pin connected to sensors data pin to be used with GPIO sysfs
// (export to user space)
fd = open(EXPORT_FILE, O_WRONLY);
if (fd < 0) {
perror(EXPORT_FILE);
error_code = ERROR_OTHER;
return;
}
snprintf(b, sizeof(b), "%d", pin);
if (pwrite(fd, b, strlen(b), 0) < 0) {
fprintf(stderr, "Unable to export pin=%d (already in use?): %s\n",
pin, strerror(errno));
error_code = ERROR_OTHER;
return;
}
close(fd);
// Open gpio direction file for fast reading/writing when requested
sysfs_filename(b, sizeof(b), pin, "direction");
fd = open(b, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Open %s: %s\n", b, strerror(errno));
error_code = ERROR_OTHER;
return;
}
direction_fd=fd;
// Open gpio value file for fast reading/writing when requested
sysfs_filename(b, sizeof(b), pin, "value");
fd = open(b, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Open %s: %s\n", b, strerror(errno));
error_code = ERROR_OTHER;
return;
}
value_fd=fd;
// sensor model handling
if ( model == AM2302 || model == RHT03) {
sensor_model = DHT22;
}
else if ( model == AUTO_DETECT) {
sensor_model = DHT22;
readSensor();
if ( error_code == ERROR_TIMEOUT ) {
sensor_model = DHT11;
// Warning: in case we auto detect a DHT11, you should wait at least 1000 msec
// before your first read request. Otherwise you will get a time out error.
}
}
error_code = ERROR_NONE;
}
/*********************************************************************
* Function: dhtCleanup_gpio()
*
* Description: Cleanup of globally used resources
*
* Parameters: none
*
********************************************************************/
void dhtCleanup_gpio(void)
{
int fd;
char b[8];
// close gpio value file
close(value_fd);
// close gpio direction file
close(direction_fd);
// free GPIO pin connected to sensors data pin to be used with GPIO sysfs
fd = open(UNEXPORT_FILE, O_WRONLY);
if (fd < 0) {
perror(UNEXPORT_FILE);
error_code = ERROR_OTHER;
return;
}
snprintf(b, sizeof(b), "%d", data_pin);
if (pwrite(fd, b, strlen(b), 0) < 0) {
fprintf(stderr, "Unable to unexport pin=%d: %s\n",
data_pin, strerror(errno));
error_code = ERROR_OTHER;
return;
}
close(fd);
error_code = ERROR_NONE;
}
/*********************************************************************
* Function: readSensor_gpio()
*
* Description: handles the communication with the sensor and reads
* the current sensor data
*
* Parameters: none
*
* Return: sets the following global variables:
* - error_code
* - temperature
* - humidity
********************************************************************/
void readSensor_gpio()
{
long startTime = micros();
int8_t i;
uint32_t k;
uint8_t age;
uint16_t rawHumidity=0;
uint16_t rawTemperature=0;
uint16_t data=0;
#if DEBUG
long t1, t2, t3, t4; // debug info
#endif
last_read_time = 0;
#if 0
// Make sure we don't poll the sensor too often
// - Max sample rate DHT11 is 1 Hz (duty cicle 1000 ms)
// - Max sample rate DHT22 is 0.5 Hz (duty cicle 2000 ms)
unsigned long startTime = micros();
if ( (unsigned long)(startTime - last_read_time) < (model == DHT11 ? 999L : 1999L) ) {
return;
}
last_read_time = startTime;
#endif
temperature = 0;
humidity = 0;
// Check clock resolution
check_clkres();
// Request sample
pinMode(OUTPUT);
digitalWrite(HIGH); // Init
usleep(INIT_DELAY);
digitalWrite(LOW); // Send start signal
#if DEBUG
t1 = micros();
#endif
if ( sensor_model == DHT11 ) {
usleep(DHT11_START_DELAY);
}
else {
// This will fail for a DHT11 - that's how we can detect such a device
usleep(DHT22_START_DELAY);
}
digitalWrite(HIGH); // Switch bus to receive data
#if DEBUG
t2 = micros();
#endif
pinMode(INPUT);
#if DEBUG
t3 = micros();
#endif
// We're going to read 83 edges:
// - First a FALLING, RISING, and FALLING edge for the start bit
// - Then 40 bits: RISING and then a FALLING edge per bit
// To keep our code simple, we accept any HIGH or LOW reading if it's max 85 usecs long
for ( i = -3 ; i < MAX_RESPONSE_EDGES; i++ ) {
startTime = micros();
// wait for edge change and measure pulse length
k=0;
do {
k++;
age = (uint8_t)(micros() - startTime);
if ( age > MAX_BIT_LENGTH ) {
// pulse length for single bit has timed out
#if DEBUG
t4 = micros();
printf("i=%d, k=%lu, age=%u, data_pin=%u, data=0x%08X\n",
i, (long unsigned int)k, age, digitalRead(), data);
printf("dt2=%ld, dt3=%ld, dt4=%ld\n", t2-t1, t3-t2, t4-t3);
#endif
error_code = ERROR_TIMEOUT;
return;
}
// sleep 10us
//usleep(10);
}
while ( digitalRead() == (i & 1) ? HIGH : LOW );
if ( i >= 0 && (i & 1) ) {
// Now we are being fed our 40 bits
data <<= 1;
// A zero lasts max 30 usecs, a one at least 68 usecs.
if ( age > MAX_PULSE_LENGTH_ZERO ) {
data |= 1; // we got a one
}
}
switch ( i ) {
case 31:
rawHumidity = data;
data = 0;
break;
case 63:
rawTemperature = data;
data = 0;
break;
}
}
// Verify checksum
if ( (uint8_t)(((uint8_t)rawHumidity) + (rawHumidity >> 8) + ((uint8_t)rawTemperature) + (rawTemperature >> 8)) != data ) {
#if DEBUG
printf("data_pin=%d, data=0x%04X%04X%02X\n",
digitalRead(), rawHumidity, rawTemperature, data);
#endif
error_code = ERROR_CHECKSUM;
return;
}
// Convert raw readings and store in global variables
if ( sensor_model == DHT11 ) {
humidity = rawHumidity >> 8;
temperature = rawTemperature >> 8;
}
else {
humidity = rawHumidity * 0.1;
if ( rawTemperature & 0x8000 ) {
rawTemperature = -(int16_t)(rawTemperature & 0x7FFF);
}
temperature = ((int16_t)rawTemperature) * 0.1;
}
error_code = ERROR_NONE;
}