Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Andreas Feldner <pelzi@flying-snail.de>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Harald Geyer <harald@ccbib.org>
Subject: Re: [PATCH] iio: dht11: Read bit stream from IRQ on falling edges only
Date: Mon, 30 Jan 2023 20:22:16 +0000	[thread overview]
Message-ID: <20230130202216.42034309@jic23-huawei> (raw)
In-Reply-To: <Y9a0RZ+inWs44Kn8@debian-qemu.internal.flying-snail.de>

On Sun, 29 Jan 2023 19:05:23 +0100
Andreas Feldner <pelzi@flying-snail.de> wrote:

> Currently, IRQs for both falling and raising edges of the GPIO
> line connected to the DHT11 device are requested. However, the
> low states do not carry information, it is possible to determine
> 0 and 1 bits from the timing of two adjacent falling edges as
> well.
> 
> Doing so does no longer requires to read the GPIO line value
> within the IRQ handler, plus halves the number of IRQs to be
> handled at all.
> 
> Signed-off-by: Andreas Feldner <pelzi@flying-snail.de>

+CC Harald  Not been that many years since Harald replied, so address
may still be good.


> ---
>  drivers/iio/humidity/dht11.c | 28 +++++++++++-----------------
>  1 file changed, 11 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/iio/humidity/dht11.c b/drivers/iio/humidity/dht11.c
> index c97e25448772..d1cd053c5dd4 100644
> --- a/drivers/iio/humidity/dht11.c
> +++ b/drivers/iio/humidity/dht11.c
> @@ -30,13 +30,13 @@
>  
>  #define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
>  
> -#define DHT11_EDGES_PREAMBLE 2
> +#define DHT11_EDGES_PREAMBLE 1
>  #define DHT11_BITS_PER_READ 40
>  /*
>   * Note that when reading the sensor actually 84 edges are detected, but
>   * since the last edge is not significant, we only store 83:
>   */
> -#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
> +#define DHT11_EDGES_PER_READ (DHT11_BITS_PER_READ + \
>  			      DHT11_EDGES_PREAMBLE + 1)
>  
>  /*
> @@ -46,6 +46,7 @@
>   * 1-bit: 68-75uS -- typically 70uS (AM2302)
>   * The acutal timings also depend on the properties of the cable, with
>   * longer cables typically making pulses shorter.
> + * Low time is constant 50uS.
>   *
>   * Our decoding depends on the time resolution of the system:
>   * timeres > 34uS ... don't know what a 1-tick pulse is
> @@ -63,7 +64,8 @@
>  #define DHT11_START_TRANSMISSION_MIN	18000  /* us */
>  #define DHT11_START_TRANSMISSION_MAX	20000  /* us */
>  #define DHT11_MIN_TIMERES	34000  /* ns */
> -#define DHT11_THRESHOLD		49000  /* ns */
> +#define DHT11_LOW		50000  /* ns */
> +#define DHT11_THRESHOLD		(49000 + DHT11_LOW)  /* ns */
>  #define DHT11_AMBIG_LOW		23000  /* ns */
>  #define DHT11_AMBIG_HIGH	30000  /* ns */
>  
> @@ -83,7 +85,7 @@ struct dht11 {
>  
>  	/* num_edges: -1 means "no transmission in progress" */
>  	int				num_edges;
> -	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
> +	struct {s64 ts; }	edges[DHT11_EDGES_PER_READ];
>  };
>  
>  #ifdef CONFIG_DYNAMIC_DEBUG
> @@ -99,7 +101,7 @@ static void dht11_edges_print(struct dht11 *dht11)
>  	for (i = 1; i < dht11->num_edges; ++i) {
>  		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
>  			dht11->edges[i].ts - dht11->edges[i - 1].ts,
> -			dht11->edges[i - 1].value ? "high" : "low");
> +			"falling");
>  	}
>  }
>  #endif /* CONFIG_DYNAMIC_DEBUG */
> @@ -125,14 +127,8 @@ static int dht11_decode(struct dht11 *dht11, int offset)
>  	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
>  
>  	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
> -		t = dht11->edges[offset + 2 * i + 2].ts -
> -			dht11->edges[offset + 2 * i + 1].ts;
> -		if (!dht11->edges[offset + 2 * i + 1].value) {
> -			dev_dbg(dht11->dev,
> -				"lost synchronisation at edge %d\n",
> -				offset + 2 * i + 1);
> -			return -EIO;
> -		}
> +		t = dht11->edges[offset + i + 1].ts -
> +		    dht11->edges[offset + i].ts;
>  		bits[i] = t > DHT11_THRESHOLD;
>  	}
>  
> @@ -174,9 +170,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
>  	struct dht11 *dht11 = iio_priv(iio);
>  
>  	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
> -		dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
> -		dht11->edges[dht11->num_edges++].value =
> -						gpiod_get_value(dht11->gpiod);
> +		dht11->edges[dht11->num_edges++].ts = ktime_get_boottime_ns();
>  
>  		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
>  			complete(&dht11->completion);
> @@ -224,7 +218,7 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
>  			goto err;
>  
>  		ret = request_irq(dht11->irq, dht11_handle_irq,
> -				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
> +				  IRQF_TRIGGER_FALLING,
>  				  iio_dev->name, iio_dev);
>  		if (ret)
>  			goto err;


  reply	other threads:[~2023-01-30 20:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-29 18:05 [PATCH] iio: dht11: Read bit stream from IRQ on falling edges only Andreas Feldner
2023-01-30 20:22 ` Jonathan Cameron [this message]
2023-01-31  9:44   ` harald
2023-02-05 20:41     ` pelzi
2023-02-07 10:33       ` harald
2023-02-11 10:41         ` pelzi
2023-02-11 21:25           ` harald

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230130202216.42034309@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=harald@ccbib.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pelzi@flying-snail.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox