42 #include "lib/sensors.h" 44 #include "dev/watchdog.h" 49 #define PRINTF(...) printf(__VA_ARGS__) 54 #define DHT22_PORT_BASE GPIO_PORT_TO_BASE(DHT22_PORT) 55 #define DHT22_PIN_MASK GPIO_PIN_MASK(DHT22_PIN) 57 static uint8_t enabled;
66 uint8_t last_state = 0xFF;
92 while(
GPIO_READ_PIN(DHT22_PORT_BASE, DHT22_PIN_MASK) == last_state) {
105 if(counter == 0xFF) {
112 if((i >= 4) && ((i % 2) == 0)) {
113 dht22_data[j / 8] <<= 1;
115 dht22_data[j / 8] |= 1;
122 PRINTF(
"DHT22: (%u) %u\n", i, dht22_data[i]);
130 checksum = dht22_data[0] + dht22_data[1] + dht22_data[2] + dht22_data[3];
132 if(dht22_data[4] == checksum) {
135 return DHT22_SUCCESS;
137 PRINTF(
"DHT22: bad checksum\n");
149 res += dht22_data[1];
155 dht22_temperature(
void)
158 res = dht22_data[2] & 0x7F;
160 res += dht22_data[3];
168 if((type != DHT22_READ_HUM) && (type != DHT22_READ_TEMP) &&
169 (type != DHT22_READ_ALL)) {
170 PRINTF(
"DHT22: Invalid type %u\n", type);
175 PRINTF(
"DHT22: ongoing operation, wait\n");
181 if(dht22_read() != DHT22_SUCCESS) {
182 PRINTF(
"DHT22: Fail to read sensor\n");
191 return dht22_humidity();
192 case DHT22_READ_TEMP:
193 return dht22_temperature();
195 return DHT22_SUCCESS;
202 dht22_read_all(int16_t *temperature, int16_t *humidity)
204 if((temperature == NULL) || (humidity == NULL)) {
205 PRINTF(
"DHT22: Invalid arguments\n");
209 if(value(DHT22_READ_ALL) != DHT22_ERROR) {
210 *temperature = dht22_temperature();
211 *humidity = dht22_humidity();
212 return DHT22_SUCCESS;
220 configure(
int type,
int value)
222 if(type != SENSORS_ACTIVE) {
236 return DHT22_SUCCESS;
240 return DHT22_SUCCESS;
243 SENSORS_SENSOR(dht22, DHT22_SENSOR, value, configure, NULL);
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
#define DHT22_START_TIME
20 ms
Header file with register and macro declarations for the cc2538 GPIO module.
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Header file with declarations for the I/O Control module.
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
#define DHT22_READING_DELAY
1 us
#define DHT22_AWAKE_TIME
250 ms
#define DHT22_READY_TIME
40 us
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
#define DHT22_MAX_TIMMING
Maximum ticks in a single operation.
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE. ...
#define DHT22_COUNT
Minimum ticks to detect a "1" bit.
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
#define IOC_OVERRIDE_OE
Output Enable.
Header file for the DHT22 temperature and humidity sensor.
#define DHT22_BUFFER
Buffer to store the samples.
#define RTIMER_BUSYWAIT(duration)
Busy-wait for a fixed duration.