Linux IIO development
 help / color / mirror / Atom feed
From: Dimitri Fedrau <dima.fedrau@gmail.com>
To: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Cc: dimitri.fedrau@liebherr.com, "Li peiyu" <579lpy@gmail.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Chris Lesiak" <chris.lesiak@licorbio.com>
Subject: Re: [PATCH 2/2] iio: humditiy: hdc3020: fix units for thresholds and hysteresis
Date: Thu, 28 Aug 2025 09:43:54 +0200	[thread overview]
Message-ID: <20250828074354.GA3177@legfed1> (raw)
In-Reply-To: <DCANVO3ZBHUN.A8E9WABNLHG4@gmail.com>

Hi Javier,

Am Sun, Aug 24, 2025 at 08:36:58PM +0800 schrieb Javier Carrasco:
> Hello Dimitri, thank you for your patch. A few comments inline:
> 
> > From: Dimitri Fedrau <dimitri.fedrau@liebherr.com>
> >
> > According to the ABI the units after application of scale and offset are
> > milli degree celsius for temperature thresholds and milli percent for
> > relative humidity thresholds. Change scale factor to fix this issue.
> 
> I miss some explanation of what is going on (i.e. wrong) at the moment,
> the scale factor that is being used and what results are being obtained.
> 
Yes, will add it. Temperature is currently returned in degree celsius,
but should be returned in milli degree celsius. Relative humdity is
returned in percent, but should be returned in milli percent.

> > @@ -379,12 +379,12 @@ static int hdc3020_thresh_get_temp(u16 thresh)
> >  	 * Get the temperature threshold from 9 LSBs, shift them to get
> >  	 * the truncated temperature threshold representation and
> >  	 * calculate the threshold according to the formula in the
> 
> Having used 65535 back then without explaining why, or at least without using
> a #define was not the best idea. It's difficult to understand why it is used
> without getting into details.
>

Yes, will add a define and change the comment.

> 13107 is even less obvious. I believe you divided 65535 by 5 everywhere in the
> code, but it's not clear why.
>

The reason for it is to avoid overflows, because the thresholds and
hysteresis is now multiplied by 1000. Dividing the scale factor fixes this
issue, but makes the code even harder to understand. Will try to fix
this.

> I'd suggest a clear definition at the beginning of the code because it
> is used in different parts of the code, after having explained why it
> is necessary in the commit message as I mentioned before.
> 

Ok.

> > -	 * datasheet. Result is degree celsius scaled by 65535.
> > +	 * datasheet. Result is degree celsius scaled by 13107.
> >  	 */
> >  	temp = FIELD_GET(HDC3020_THRESH_TEMP_MASK, thresh) <<
> >  	       HDC3020_THRESH_TEMP_TRUNC_SHIFT;
> >  
> 
> Again, it's difficult to understand why everything is divided by 5.
>

Ok.

> > -	return -2949075 + (175 * temp);
> > +	return -589815 + (35 * temp);
> >  }
> >  
> >  static int hdc3020_thresh_get_hum(u16 thresh)
> > @@ -395,12 +395,12 @@ static int hdc3020_thresh_get_hum(u16 thresh)
> >  	 * Get the humidity threshold from 7 MSBs, shift them to get the
> >  	 * truncated humidity threshold representation and calculate the
> >  	 * threshold according to the formula in the datasheet. Result is
> > -	 * percent scaled by 65535.
> > +	 * percent scaled by 13107.
> >  	 */
> >  	hum = FIELD_GET(HDC3020_THRESH_HUM_MASK, thresh) <<
> >  	      HDC3020_THRESH_HUM_TRUNC_SHIFT;
> >  
> 
> Similarly, multiplying by 20 (100/5) looks weird for a percentage.
>

Ok, see above.

> > -	return hum * 100;
> > +	return hum * 20;
> >  }
> 
> ...
> 
> > @@ -630,7 +630,7 @@ static int hdc3020_read_thresh(struct iio_dev *indio_dev,
> >  		thresh = hdc3020_thresh_get_temp(ret);
> >  		switch (info) {
> >  		case IIO_EV_INFO_VALUE:
> 
> MILLI, as suggested for [1/2]? The same would apply to the following
> diffs.
> 

Ok.

> > -			*val = thresh;
> > +			*val = thresh * 1000;
> >  			break;
> >  		case IIO_EV_INFO_HYSTERESIS:
> >  			ret = hdc3020_read_be16(data, reg_clr);
> > @@ -638,18 +638,18 @@ static int hdc3020_read_thresh(struct iio_dev *indio_dev,
> >  				return ret;
> >  
> >  			clr = hdc3020_thresh_get_temp(ret);
> 
Best regards,
Dimitri Fedrau

      reply	other threads:[~2025-08-28  7:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-21 15:23 [PATCH 0/2] iio: humditiy: hdc3020: fix units Dimitri Fedrau via B4 Relay
2025-08-21 15:23 ` [PATCH 1/2] iio: humditiy: hdc3020: fix units for temperature and humidity measurement Dimitri Fedrau via B4 Relay
2025-08-21 15:44   ` Andy Shevchenko
2025-08-21 15:45     ` Andy Shevchenko
2025-08-22 12:36       ` Dimitri Fedrau
2025-08-21 15:23 ` [PATCH 2/2] iio: humditiy: hdc3020: fix units for thresholds and hysteresis Dimitri Fedrau via B4 Relay
2025-08-24 12:36   ` Javier Carrasco
2025-08-28  7:43     ` Dimitri Fedrau [this message]

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=20250828074354.GA3177@legfed1 \
    --to=dima.fedrau@gmail.com \
    --cc=579lpy@gmail.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=andy@kernel.org \
    --cc=chris.lesiak@licorbio.com \
    --cc=dimitri.fedrau@liebherr.com \
    --cc=dlechner@baylibre.com \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /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