42 #include "lib/sensors.h" 44 #include "dev/watchdog.h" 49 #define PRINTF(...) printf(__VA_ARGS__) 54 #define BUSYWAIT_UNTIL(max_time) \ 58 while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))) { \ 59 watchdog_periodic(); \ 63 #define DHT22_PORT_BASE GPIO_PORT_TO_BASE(DHT22_PORT) 64 #define DHT22_PIN_MASK GPIO_PIN_MASK(DHT22_PIN) 66 static uint8_t enabled;
75 uint8_t last_state = 0xFF;
101 while(
GPIO_READ_PIN(DHT22_PORT_BASE, DHT22_PIN_MASK) == last_state) {
106 if(counter == 0xFF) {
114 if(counter == 0xFF) {
121 if((i >= 4) && ((i % 2) == 0)) {
122 dht22_data[j / 8] <<= 1;
124 dht22_data[j / 8] |= 1;
131 PRINTF(
"DHT22: (%u) %u\n", i, dht22_data[i]);
139 checksum = dht22_data[0] + dht22_data[1] + dht22_data[2] + dht22_data[3];
141 if(dht22_data[4] == checksum) {
144 return DHT22_SUCCESS;
146 PRINTF(
"DHT22: bad checksum\n");
158 res += dht22_data[1];
164 dht22_temperature(
void)
167 res = dht22_data[2] & 0x7F;
169 res += dht22_data[3];
177 if((type != DHT22_READ_HUM) && (type != DHT22_READ_TEMP) &&
178 (type != DHT22_READ_ALL)) {
179 PRINTF(
"DHT22: Invalid type %u\n", type);
184 PRINTF(
"DHT22: ongoing operation, wait\n");
190 if(dht22_read() != DHT22_SUCCESS) {
191 PRINTF(
"DHT22: Fail to read sensor\n");
200 return dht22_humidity();
201 case DHT22_READ_TEMP:
202 return dht22_temperature();
204 return DHT22_SUCCESS;
211 dht22_read_all(int16_t *temperature, int16_t *humidity)
213 if((temperature == NULL) || (humidity == NULL)) {
214 PRINTF(
"DHT22: Invalid arguments\n");
218 if(value(DHT22_READ_ALL) != DHT22_ERROR) {
219 *temperature = dht22_temperature();
220 *humidity = dht22_humidity();
221 return DHT22_SUCCESS;
229 configure(
int type,
int value)
231 if(type != SENSORS_ACTIVE) {
245 return DHT22_SUCCESS;
249 return DHT22_SUCCESS;
252 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.