Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1342174ybd; Sun, 23 Jun 2019 05:29:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqwSoKpGlkRW7WCn8yslIVobUbY4aVhJhKoChhqmba6RVJpVz4p6VAXLH+UZjrTDkVqRm1NI X-Received: by 2002:a17:90a:601:: with SMTP id j1mr18146801pjj.96.1561292996449; Sun, 23 Jun 2019 05:29:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561292996; cv=none; d=google.com; s=arc-20160816; b=xJr1ICIo7wSWP0yy3LFIzQ4d5RHZR6ceYQqSDVY90Hq+OkrFIwUv+DV4GM2c37W/2C NtxIDDMXEkZ2+PJGfIn9HCfnEWZoI49qb4HA3jm3wJmeenOeCZRmnZyM4GovHj7NFf/n OJ/Hup8iPSp5tox6GfaILWpXLEsyepa0c4KUVfhS2Q2lyiDOyT6q1Tmf7bqk+yj/DlTS zDJMKhGnHw4UXbxb3HmLot/vE/vGXQW9g9yjHxi1tZJ4r/BE6Gv2LkU6hZYfhNvNHlr0 XcglDthdTdJgQlZ6/cZ5QdMauKVDNiDbmsvVFSpeakcjkI2h19mSD01xio+jrmMpwpwa 9+VQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-transfer-encoding :content-disposition:mime-version:message-id:subject:cc:to:from:date; bh=2/Mdef1+X5LHByJ9gh3ly481hBodzBRl9DY4tM3WNJY=; b=q7m628fxA60m+57fw6TiKUlxGtmdx2jCrbUZ48SKjQmpr4WpU9OtIOuJmfNzAq5RpL wXrBfHf+Tt3rgp+zGH7Jsdc4l7FXQXN8lRJzH818q3FjXFmTmqnZblGKDBN1mf555e8d dTdqTzTXtVkwBYM23JZPqeM854/LvW0A8FLpbRF9QJw7rHTvMcT3rnR6ukMWtSbRM+fX 7akBV7On/kAB4EGjTl5iYxoJdsStaZfYhg4oliy+XS1saJZu81oOZ6VssekADYrEq5DM IhUW38LxWHqV0EkBwt7/c/xMaWUxnKIcOwRRshwd8YUEkTbRRh4K+Mfpg2vK8FmBlJXe F92A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x21si7110029pgh.400.2019.06.23.05.29.41; Sun, 23 Jun 2019 05:29:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726464AbfFWM3h convert rfc822-to-8bit (ORCPT + 99 others); Sun, 23 Jun 2019 08:29:37 -0400 Received: from mout.kundenserver.de ([212.227.126.133]:52089 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725963AbfFWM3g (ORCPT ); Sun, 23 Jun 2019 08:29:36 -0400 Received: from localhost ([46.78.234.140]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MWixU-1i7VnU36Uq-00X7UB; Sun, 23 Jun 2019 14:29:12 +0200 Date: Sun, 23 Jun 2019 14:29:10 +0200 From: Andreas Klinger To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, zbynek.kocur@fel.cvut.cz, linux-kernel@vger.kernel.org Subject: [PATCH] iio: srf04: fix wrong limitation in distance measuring Message-ID: <20190623122909.hhzskp6k5vm4wify@arbad> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-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an8DifxdKSzsIrqZJJshDh7tJ1O MUulB/j83e2dJi24nm57J+Ct+lF49pnY3k3HlA== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The measured time value in the driver is limited to the maximum distance which can be read by the sensor. This limitation was wrong and is fixed by this patch. It also takes into account that we are supporting a variety of sensors today and that the recently added sensors have a higher maximum distance range. Suggested-by: Zbyněk Kocur Signed-off-by: Andreas Klinger --- drivers/iio/proximity/srf04.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/drivers/iio/proximity/srf04.c b/drivers/iio/proximity/srf04.c index 8b50d56b0a03..01eb8cc63076 100644 --- a/drivers/iio/proximity/srf04.c +++ b/drivers/iio/proximity/srf04.c @@ -110,7 +110,7 @@ static int srf04_read(struct srf04_data *data) udelay(data->cfg->trigger_pulse_us); gpiod_set_value(data->gpiod_trig, 0); - /* it cannot take more than 20 ms */ + /* it should not take more than 20 ms until echo is rising */ ret = wait_for_completion_killable_timeout(&data->rising, HZ/50); if (ret < 0) { mutex_unlock(&data->lock); @@ -120,7 +120,8 @@ static int srf04_read(struct srf04_data *data) return -ETIMEDOUT; } - ret = wait_for_completion_killable_timeout(&data->falling, HZ/50); + /* it cannot take more than 50 ms until echo is falling */ + ret = wait_for_completion_killable_timeout(&data->falling, HZ/20); if (ret < 0) { mutex_unlock(&data->lock); return ret; @@ -135,19 +136,19 @@ static int srf04_read(struct srf04_data *data) dt_ns = ktime_to_ns(ktime_dt); /* - * measuring more than 3 meters is beyond the capabilities of - * the sensor + * measuring more than 6,45 meters is beyond the capabilities of + * the supported sensors * ==> filter out invalid results for not measuring echos of * another us sensor * * formula: - * distance 3 m - * time = ---------- = --------- = 9404389 ns - * speed 319 m/s + * distance 6,45 * 2 m + * time = ---------- = ------------ = 40438871 ns + * speed 319 m/s * * using a minimum speed at -20 °C of 319 m/s */ - if (dt_ns > 9404389) + if (dt_ns > 40438871) return -EIO; time_ns = dt_ns; @@ -159,20 +160,20 @@ static int srf04_read(struct srf04_data *data) * with Temp in °C * and speed in m/s * - * use 343 m/s as ultrasonic speed at 20 °C here in absence of the + * use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the * temperature * * therefore: - * time 343 - * distance = ------ * ----- - * 10^6 2 + * time 343,5 time * 106 + * distance = ------ * ------- = ------------ + * 10^6 2 617176 * with time in ns * and distance in mm (one way) * - * because we limit to 3 meters the multiplication with 343 just + * because we limit to 6,45 meters the multiplication with 106 just * fits into 32 bit */ - distance_mm = time_ns * 343 / 2000000; + distance_mm = time_ns * 106 / 617176; return distance_mm; } -- 2.11.0