From: Jonathan Cameron <jic23@kernel.org>
To: "Stefan Brüns" <stefan.bruens@rwth-aachen.de>
Cc: <linux-iio@vger.kernel.org>,
Peter Meerwald-Stadler <pmeerw@pmeerw.net>,
Maciej Purski <m.purski@samsung.com>,
<linux-kernel@vger.kernel.org>, "Andrew F . Davis" <afd@ti.com>,
Lars-Peter Clausen <lars@metafoo.de>,
Hartmut Knaack <knaack.h@gmx.de>
Subject: Re: [PATCH v2 5/7] iio: adc: ina2xx: Use a monotonic clock for delay calculation
Date: Fri, 29 Dec 2017 17:48:06 +0000 [thread overview]
Message-ID: <20171229174806.5a615b63@archlinux> (raw)
In-Reply-To: <b91d1c8c-2467-44bc-b8bc-adc74dfb9bee@rwthex-w2-a.rwth-ad.de>
On Thu, 21 Dec 2017 19:31:36 +0100
Stefan Brüns <stefan.bruens@rwth-aachen.de> wrote:
> The iio timestamp clock is user selectable and may be non-monotonic. Also,
> only part of the acquisition time is measured, thus the delay was longer
> than intended.
>
> Use a monotonic timestamp to track the time for the next poll iteration.
> The timestamp is advanced by the sampling interval each iteration. In case
> the conversion overrruns the register readout (i.e. fast sampling combined
> with a slow bus), one or multiple samples will be dropped.
>
> Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to play with it.
Thanks,
Jonathan
>
> ---
>
> Changes in v2:
> - add a comment mentioning skipping samples on overrun
>
> drivers/iio/adc/ina2xx-adc.c | 41 +++++++++++++++++++++++++++--------------
> 1 file changed, 27 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
> index 2621a34ee5c6..415e53b5c0a6 100644
> --- a/drivers/iio/adc/ina2xx-adc.c
> +++ b/drivers/iio/adc/ina2xx-adc.c
> @@ -703,10 +703,10 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev)
> /* data buffer needs space for channel data and timestap */
> unsigned short data[4 + sizeof(s64)/sizeof(short)];
> int bit, ret, i = 0;
> - s64 time_a, time_b;
> + s64 time;
> unsigned int alert;
>
> - time_a = iio_get_time_ns(indio_dev);
> + time = iio_get_time_ns(indio_dev);
>
> /*
> * Because the timer thread and the chip conversion clock
> @@ -752,11 +752,9 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev)
> data[i++] = val;
> }
>
> - time_b = iio_get_time_ns(indio_dev);
> + iio_push_to_buffers_with_timestamp(indio_dev, data, time);
>
> - iio_push_to_buffers_with_timestamp(indio_dev, data, time_a);
> -
> - return (unsigned long)(time_b - time_a) / 1000;
> + return 0;
> };
>
> static int ina2xx_capture_thread(void *data)
> @@ -764,7 +762,9 @@ static int ina2xx_capture_thread(void *data)
> struct iio_dev *indio_dev = data;
> struct ina2xx_chip_info *chip = iio_priv(indio_dev);
> int sampling_us = SAMPLING_PERIOD(chip);
> - int buffer_us, delay_us;
> + int ret;
> + struct timespec64 next, now, delta;
> + s64 delay_us;
>
> /*
> * Poll a bit faster than the chip internal Fs, in case
> @@ -773,15 +773,28 @@ static int ina2xx_capture_thread(void *data)
> if (!chip->allow_async_readout)
> sampling_us -= 200;
>
> + ktime_get_ts64(&next);
> +
> do {
> - buffer_us = ina2xx_work_buffer(indio_dev);
> - if (buffer_us < 0)
> - return buffer_us;
> + ret = ina2xx_work_buffer(indio_dev);
> + if (ret < 0)
> + return ret;
>
> - if (sampling_us > buffer_us) {
> - delay_us = sampling_us - buffer_us;
> - usleep_range(delay_us, (delay_us * 3) >> 1);
> - }
> + ktime_get_ts64(&now);
> +
> + /*
> + * Advance the timestamp for the next poll by one sampling
> + * interval, and sleep for the remainder (next - now).
> + * In case "next" has already passed, the interval is added
> + * multiple times, i.e. samples are dropped.
> + */
> + do {
> + timespec64_add_ns(&next, 1000 * sampling_us);
> + delta = timespec64_sub(next, now);
> + delay_us = timespec64_to_ns(&delta) / 1000;
> + } while (delay_us <= 0);
> +
> + usleep_range(delay_us, (delay_us * 3) >> 1);
>
> } while (!kthread_should_stop());
>
next prev parent reply other threads:[~2017-12-29 17:48 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20171221183138.361-1-stefan.bruens@rwth-aachen.de>
2017-12-21 18:31 ` [PATCH v2 1/7] iio: adc: ina2xx: Remove bogus cast for data argument Stefan Brüns
2017-12-29 17:46 ` Jonathan Cameron
2017-12-21 18:31 ` [PATCH v2 2/7] iio: adc: ina2xx: Clarify size requirement for data buffer Stefan Brüns
2017-12-21 18:31 ` [PATCH v2 3/7] iio: adc: ina2xx: Remove unneeded dummy read to clear CNVR flag Stefan Brüns
2017-12-21 18:31 ` [PATCH v2 4/7] iio: adc: ina2xx: Do not udelay for several seconds Stefan Brüns
2017-12-21 18:31 ` [PATCH v2 5/7] iio: adc: ina2xx: Use a monotonic clock for delay calculation Stefan Brüns
2017-12-29 17:48 ` Jonathan Cameron [this message]
2017-12-31 19:55 ` [PATCH v3 " Stefan Brüns
2018-01-01 1:24 ` [PATCH v4 " Stefan Brüns
2018-01-01 9:30 ` Jonathan Cameron
2017-12-21 18:31 ` [PATCH v2 6/7] iio: adc: ina2xx: Align timestamp with conversion ready flag Stefan Brüns
2017-12-29 17:49 ` Jonathan Cameron
2017-12-21 18:31 ` [PATCH v2 7/7] iio: adc: ina2xx: Actually align the loop with the " Stefan Brüns
2017-12-29 17:52 ` Jonathan Cameron
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=20171229174806.5a615b63@archlinux \
--to=jic23@kernel.org \
--cc=afd@ti.com \
--cc=knaack.h@gmx.de \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=m.purski@samsung.com \
--cc=pmeerw@pmeerw.net \
--cc=stefan.bruens@rwth-aachen.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;
as well as URLs for NNTP newsgroup(s).