Skip to content

Commit

Permalink
data caching removed
Browse files Browse the repository at this point in the history
  • Loading branch information
hugueslarrive committed Jun 13, 2023
1 parent e62c6b8 commit 645ea94
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 64 deletions.
81 changes: 23 additions & 58 deletions drivers/dht/dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
#define READ_THRESHOLD (40U)
/* If an expected pulse is not detected within 85µs, something is wrong */
#define SPIN_TIMEOUT (85U)
/* According to datasheet the DHT sensor cannot measure more than once
* every 2 seconds but most are faster */
#define RESPAWN_TIMEOUT (2U * US_PER_SEC)
/* The start signal by pulling data low for at least 18 ms for DHT11, at
* most 20 ms (AM2301 / DHT22 / DHT21). Then release the bus and the
* sensor should respond by pulling data low for 80 µs, then release for
Expand Down Expand Up @@ -238,68 +235,36 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
{
assert(dev);

bool first_read = 0;
if (!dev->data_hold_time) {
first_read = 1;
uint16_t timeout = RESPAWN_TIMEOUT / US_PER_MS;
while (_send_start_signal(dev) == -ENODEV
&& (timeout -= START_LOW_TIME / US_PER_MS) > 0) {}
}

uint32_t now_us = ztimer_now(ZTIMER_USEC);
if ((now_us - dev->last_read_us) > dev->data_hold_time) {
dev->last_read_us = now_us;
struct dht_data data = {
.pin = dev->params.pin,
.in_mode = dev->params.in_mode,
.bit_pos = 0,
};
struct dht_data data = {
.pin = dev->params.pin,
.in_mode = dev->params.in_mode,
.bit_pos = 0,
};

if (first_read) {
uint16_t timeout = RESPAWN_TIMEOUT / US_PER_MS;
while (_send_start_signal(dev) == -ENODEV
&& (timeout -= START_LOW_TIME / US_PER_MS) > 0) {}
dev->last_read_us = ztimer_now(ZTIMER_USEC);
dev->data_hold_time = (dev->last_read_us - now_us);
/* DEBUG() delayed after _busy_wait_read() */
}
else if (_send_start_signal(dev) == -ENODEV) {
DEBUG_PUTS("[dht] error: No response from device, "
"insufficient RESPAWN_TIMEOUT?");
DEBUG("[dht] data_hold_time = %" PRIu32 " µs\n", dev->data_hold_time);
return -ENODEV;
}

/* read the data */
_busy_wait_read(&data);

if (first_read) {
DEBUG("[dht] first read, set data_hold_time to: %" PRIu32 " ms\n",
(uint32_t)(dev->data_hold_time / US_PER_MS));
}
if (_send_start_signal(dev) == -ENODEV) {
DEBUG_PUTS("[dht] error: No response from device");
return -ENODEV;
}

if (_validate_checksum(data.data) == -EIO) {
DEBUG("[dht] error: checksum doesn't match\n"
"[dht] RAW data: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
(unsigned)data.data[0], (unsigned)data.data[1],
(unsigned)data.data[2], (unsigned)data.data[3],
(unsigned)data.data[4]);
return -EIO;
}
/* read the data */
_busy_wait_read(&data);

if (_parse_raw_values(dev, data.data) == -ENOSYS) {
DEBUG_PUTS("[dht] error: data format not implemented");
return -ENOSYS;
}
if (_validate_checksum(data.data) == -EIO) {
DEBUG("[dht] error: checksum doesn't match\n"
"[dht] RAW data: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
(unsigned)data.data[0], (unsigned)data.data[1],
(unsigned)data.data[2], (unsigned)data.data[3],
(unsigned)data.data[4]);
return -EIO;
}

if (temp) {
*temp = dev->last_val.temperature;
if (_parse_raw_values(dev, data.data) == -ENOSYS) {
DEBUG_PUTS("[dht] error: data format not implemented");
return -ENOSYS;
}

if (hum) {
*hum = dev->last_val.humidity;
}
*hum = dev->last_val.humidity;
*temp = dev->last_val.temperature;

return 0;
}
4 changes: 1 addition & 3 deletions drivers/include/dht.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ enum {
* @brief Data type for storing DHT sensor readings
*/
typedef struct {
uint16_t humidity; /**< relative percent */
uint16_t humidity; /**< relative humidity in deci-percent */
uint16_t temperature; /**< temperature in deci-Celsius */
} dht_data_t;

Expand Down Expand Up @@ -87,8 +87,6 @@ typedef struct {
typedef struct {
dht_params_t params; /**< Device parameters */
dht_data_t last_val; /**< Values of the last measurement */
uint32_t last_read_us; /**< Time of the last measurement */
uint32_t data_hold_time;/**< Measured minimal data hold time */
} dht_t;

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/drivers/dht/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "time_units.h"
#include "ztimer.h"

#define DELAY (500 * US_PER_MS)
#define DELAY (2 * US_PER_SEC)

int main(void)
{
Expand All @@ -49,15 +49,15 @@ int main(void)

/* periodically read temp and humidity values */
while (1) {
ztimer_sleep(ZTIMER_USEC, DELAY);

if (dht_read(&dev, &temp, &hum) != DHT_OK) {
puts("Error reading values");
continue;
}

printf("DHT values - temp: %d.%d°C - relative humidity: %d.%d%%\n",
temp/10, temp%10, hum/10, hum%10);

ztimer_sleep(ZTIMER_USEC, DELAY);
}

return 0;
Expand Down

0 comments on commit 645ea94

Please sign in to comment.