drivers: sensor: dht: update to new GPIO API

Document IO signal behavior, switch to new API.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2019-12-19 09:30:11 -06:00 committed by Carles Cufí
commit c44290f75d
4 changed files with 32 additions and 28 deletions

View file

@ -21,17 +21,16 @@ LOG_MODULE_REGISTER(DHT, CONFIG_SENSOR_LOG_LEVEL);
* @brief Measure duration of signal send by sensor * @brief Measure duration of signal send by sensor
* *
* @param drv_data Pointer to the driver data structure * @param drv_data Pointer to the driver data structure
* @param signal_val Value of signal being measured * @param active Whether current signal is active
* *
* @return duration in usec of signal being measured, * @return duration in usec of signal being measured,
* -1 if duration exceeds DHT_SIGNAL_MAX_WAIT_DURATION * -1 if duration exceeds DHT_SIGNAL_MAX_WAIT_DURATION
*/ */
static s8_t dht_measure_signal_duration(struct device *dev, static s8_t dht_measure_signal_duration(struct device *dev,
u32_t signal_val) bool active)
{ {
struct dht_data *drv_data = dev->driver_data; struct dht_data *drv_data = dev->driver_data;
const struct dht_config *cfg = dev->config->config_info; const struct dht_config *cfg = dev->config->config_info;
u32_t val;
u32_t elapsed_cycles; u32_t elapsed_cycles;
u32_t max_wait_cycles = (u32_t)( u32_t max_wait_cycles = (u32_t)(
(u64_t)DHT_SIGNAL_MAX_WAIT_DURATION * (u64_t)DHT_SIGNAL_MAX_WAIT_DURATION *
@ -39,15 +38,17 @@ static s8_t dht_measure_signal_duration(struct device *dev,
(u64_t)USEC_PER_SEC (u64_t)USEC_PER_SEC
); );
u32_t start_cycles = k_cycle_get_32(); u32_t start_cycles = k_cycle_get_32();
int rc;
do { do {
gpio_pin_read(drv_data->gpio, cfg->pin, &val); rc = gpio_pin_get(drv_data->gpio, cfg->pin);
elapsed_cycles = k_cycle_get_32() - start_cycles; elapsed_cycles = k_cycle_get_32() - start_cycles;
if (elapsed_cycles > max_wait_cycles) { if ((rc < 0)
|| (elapsed_cycles > max_wait_cycles)) {
return -1; return -1;
} }
} while (val == signal_val); } while ((bool)rc == active);
return (u64_t)elapsed_cycles * return (u64_t)elapsed_cycles *
(u64_t)USEC_PER_SEC / (u64_t)USEC_PER_SEC /
@ -66,45 +67,45 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
/* send start signal */ /* assert to send start signal */
gpio_pin_write(drv_data->gpio, cfg->pin, 0); gpio_pin_set(drv_data->gpio, cfg->pin, true);
k_busy_wait(DHT_START_SIGNAL_DURATION); k_busy_wait(DHT_START_SIGNAL_DURATION);
gpio_pin_write(drv_data->gpio, cfg->pin, 1); gpio_pin_set(drv_data->gpio, cfg->pin, false);
/* switch to DIR_IN to read sensor signals */ /* switch to DIR_IN to read sensor signals */
gpio_pin_configure(drv_data->gpio, cfg->pin, gpio_pin_configure(drv_data->gpio, cfg->pin,
GPIO_DIR_IN); GPIO_INPUT | cfg->flags);
/* wait for sensor response */ /* wait for sensor active response */
if (dht_measure_signal_duration(dev, 1) == -1) { if (dht_measure_signal_duration(dev, false) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* read sensor response */ /* read sensor response */
if (dht_measure_signal_duration(dev, 0) == -1) { if (dht_measure_signal_duration(dev, true) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* wait for sensor data */ /* wait for sensor data start */
if (dht_measure_signal_duration(dev, 1) == -1) { if (dht_measure_signal_duration(dev, false) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* read sensor data */ /* read sensor data */
for (i = 0U; i < DHT_DATA_BITS_NUM; i++) { for (i = 0U; i < DHT_DATA_BITS_NUM; i++) {
/* LOW signal to indicate a new bit */ /* Active signal to indicate a new bit */
if (dht_measure_signal_duration(dev, 0) == -1) { if (dht_measure_signal_duration(dev, true) == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
} }
/* HIGH signal duration indicates bit value */ /* Inactive signal duration indicates bit value */
signal_duration[i] = dht_measure_signal_duration(dev, 1); signal_duration[i] = dht_measure_signal_duration(dev, false);
if (signal_duration[i] == -1) { if (signal_duration[i] == -1) {
ret = -EIO; ret = -EIO;
goto cleanup; goto cleanup;
@ -154,9 +155,9 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
} }
cleanup: cleanup:
/* switch to DIR_OUT and leave pin to HIGH until next fetch */ /* Switch to output inactive until next fetch. */
gpio_pin_configure(drv_data->gpio, cfg->pin, GPIO_DIR_OUT); gpio_pin_configure(drv_data->gpio, cfg->pin,
gpio_pin_write(drv_data->gpio, cfg->pin, 1); GPIO_OUTPUT_INACTIVE | cfg->flags);
return ret; return ret;
} }
@ -230,10 +231,8 @@ static int dht_init(struct device *dev)
return -EINVAL; return -EINVAL;
} }
rc = gpio_pin_configure(drv_data->gpio, cfg->pin, GPIO_DIR_OUT); rc = gpio_pin_configure(drv_data->gpio, cfg->pin,
if (rc == 0) { GPIO_OUTPUT_INACTIVE | cfg->flags);
rc = gpio_pin_write(drv_data->gpio, cfg->pin, 1);
}
return rc; return rc;
} }
@ -241,6 +240,7 @@ static int dht_init(struct device *dev)
static struct dht_data dht_data; static struct dht_data dht_data;
static const struct dht_config dht_config = { static const struct dht_config dht_config = {
.ctrl = DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER, .ctrl = DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER,
.flags = DT_INST_0_AOSONG_DHT_DIO_GPIOS_FLAGS,
.pin = DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN, .pin = DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN,
}; };

View file

@ -20,7 +20,8 @@ struct dht_data {
struct dht_config { struct dht_config {
const char *ctrl; const char *ctrl;
u8_t pin; gpio_devicetree_flags_t flags;
gpio_pin_t pin;
}; };
#endif #endif

View file

@ -22,6 +22,9 @@ properties:
description: | description: |
Pin on which sensor communication will be performed. Pin on which sensor communication will be performed.
Control and data are encoded by the duration of active low
signals. A pull up may be appropriate.
dht22: dht22:
type: boolean type: boolean
description: | description: |

View file

@ -9,7 +9,7 @@
compatible = "aosong,dht"; compatible = "aosong,dht";
status = "okay"; status = "okay";
label = "DHT22"; label = "DHT22";
dio-gpios = <&gpio0 11 0>; dio-gpios = <&gpio0 11 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
dht22; dht22;
}; };
}; };