Re: [PATCH v6 5/9] iio: pressure: bmp280: Refactorize reading functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, May 12, 2024 at 01:24:12PM +0100, Jonathan Cameron wrote:
> On Wed,  8 May 2024 18:52:03 +0200
> Vasileios Amoiridis <vassilisamir@xxxxxxxxx> wrote:
> 
> > For BMP18x, BMP28x, BME280, BMP38x the reading of the pressure
> > value requires an update of the t_fine variable which happens
> > through reading the temperature value.
> > 
> > So all the bmpxxx_read_press() functions of the above sensors
> > are internally calling the equivalent bmpxxx_read_temp() function
> > in order to update the t_fine value. By just looking at the code
> > this functionality is a bit hidden and is not easy to understand
> > why those channels are not independent.
> > 
> > This commit tries to clear these things a bit by splitting the
> > bmpxxx_{read/compensate}_{temp/press/humid}() to the following:
> > 
> > i. bmpxxx_read_{temp/press/humid}_adc(): read the raw value from
> > the sensor.
> > 
> > ii. bmpxx_calc_t_fine(): calculate the t_fine variable.
> > 
> > iii. bmpxxx_get_t_fine(): get the t_fine variable.
> > 
> > iv. bmpxxx_compensate_{temp/press/humid}(): compensate the adc
> > values and return the calculated value.
> > 
> > v. bmpxxx_read_{temp/press/humid}(): combine calls of the
> > aforementioned functions to return the requested value.
> > 
> > Suggested-by: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx>
> > Signed-off-by: Vasileios Amoiridis <vassilisamir@xxxxxxxxx>
> A few minor things inline.  Given I've picked up the 1st 4 patches,
> please base your v7 on top of those.
> 
> Thanks,
> 
> Jonathan
> 
> > ---
> >  drivers/iio/pressure/bmp280-core.c | 361 ++++++++++++++++++-----------
> >  drivers/iio/pressure/bmp280.h      |   6 -
> >  2 files changed, 232 insertions(+), 135 deletions(-)
> > 
> > diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> > index dd5c526dacbd..a864f8db8e24 100644
> > --- a/drivers/iio/pressure/bmp280-core.c
> > +++ b/drivers/iio/pressure/bmp280-core.c
> > @@ -288,13 +288,35 @@ static int bme280_read_calib(struct bmp280_data *data)
> >   *
> >   * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
> >   */
> 
> Seems this comment should probably follow the maths which has moved.
> 
> > +static int bme280_read_humid_adc(struct bmp280_data *data, u16 *adc_humidity)
> > +{
> > +	u16 value_humidity;
> > +	int ret;
> > +
> > +	ret = regmap_bulk_read(data->regmap, BME280_REG_HUMIDITY_MSB,
> > +			       &data->be16, sizeof(data->be16));
> > +	if (ret) {
> > +		dev_err(data->dev, "failed to read humidity\n");
> > +		return ret;
> > +	}
> > +
> > +	value_humidity = be16_to_cpu(data->be16);
> > +	if (value_humidity == BMP280_HUMIDITY_SKIPPED) {
> > +		dev_err(data->dev, "reading humidity skipped\n");
> > +		return -EIO;
> > +	}
> > +	*adc_humidity = value_humidity;
> > +
> > +	return 0;
> > +}
> 
> ...
> > @@ -313,8 +335,29 @@ static u32 bme280_compensate_humidity(struct bmp280_data *data,
> >   *
> >   * Taken from datasheet, Section 3.11.3, "Compensation formula".
> >   */
> Is this comment still relevant here? Seems it should probably move to follow
> that maths.
> 
> > -static s32 bmp280_compensate_temp(struct bmp280_data *data,
> > -				  u32 adc_temp)
> > +static int bmp280_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
> > +{
> 
> >  
> >  static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2)
> >  {
> > -	u32 adc_press, comp_press;
> > +	u32 adc_press, comp_press, t_fine;
> >  	int ret;
> >  
> > -	/* Read and compensate for temperature so we get a reading of t_fine */
> > -	ret = bmp380_read_temp(data, NULL, NULL);
> > +	ret = bmp380_get_t_fine(data, &t_fine);
> >  	if (ret)
> >  		return ret;
> >  
> > -	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
> > -			       data->buf, sizeof(data->buf));
> > -	if (ret) {
> > -		dev_err(data->dev, "failed to read pressure\n");
> > +	ret = bmp380_read_press_adc(data, &adc_press);
> > +	if (ret)
> >  		return ret;
> > -	}
> >  
> > -	adc_press = get_unaligned_le24(data->buf);
> > -	if (adc_press == BMP380_PRESS_SKIPPED) {
> > -		dev_err(data->dev, "reading pressure skipped\n");
> > -		return -EIO;
> > -	}
> > -	comp_press = bmp380_compensate_press(data, adc_press);
> > +	comp_press = bmp380_compensate_press(data, adc_press, t_fine);
> >  
> > +	/* IIO units are in kPa */
> 
> Probably worth keeping the reference to what the unit of the
> datasheet maths is around.
> 
> >  	*val = comp_press;
> > -	/* Compensated pressure is in cPa (centipascals) */
> >  	*val2 = 100000;
> >  
> >  	return IIO_VAL_FRACTIONAL;
> > @@ -1825,7 +1916,7 @@ static int bmp180_wait_for_eoc(struct bmp280_data *data, u8 ctrl_meas)
> >  	return 0;
> >  }
> 
> 
> > -static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press)
> > +static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press,
> > +				   s32 t_fine)
> >  {
> >  	struct bmp180_calib *calib = &data->calib.bmp180;
> >  	s32 oss = data->oversampling_press;
> > @@ -1965,7 +2068,7 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press)
> >  	s32 b3, b6;
> >  	u32 b4, b7;
> >  
> > -	b6 = data->t_fine - 4000;
> > +	b6 = t_fine - 4000;
> >  	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
> >  	x2 = calib->AC2 * b6 >> 11;
> >  	x3 = x1 + x2;
> > @@ -1974,7 +2077,7 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press)
> >  	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
> >  	x3 = (x1 + x2 + 2) >> 2;
> >  	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
> > -	b7 = (adc_press - b3) * (50000 >> oss);
> > +	b7 = (((u32)adc_press) - b3) * (50000 >> oss);
> 
> Casting from u32 to u32?
> 
> >  	if (b7 < 0x80000000)
> >  		p = (b7 * 2) / b4;
> >  	else
> > @@ -1990,19 +2093,19 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press)
> >  static int bmp180_read_press(struct bmp280_data *data, int *val, int *val2)
> 
> > +	/* IIO units are in kPa */
> 
> I think this is an unrelated improvement as original code doesn't have such a comment.
> So shouldn't really be in this patch. If you want to keep it here rather than pushing it
> into an additional patch, mention it in the commit message. "additional comments on base
> units added for consistency" or something like that.
> >  	*val = comp_press;
> >  	*val2 = 1000;

Hi Jonathan!

Thank you very much once again for the amazing feedback!

As it looks like, I changed the code but I forgot to move the
comments accordingly. Thank you very much for pointing this out.
The extra comments are indeed not necessary, they don't add some
specific value so I can drop them.

Cheers,
Vasilis




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux