06-03-2022 08:11 PM - edited 06-03-2022 08:12 PM
There appears to be a casting issue in this function that is causing negative temperatures to be returned as larger positive values.
Specifically in this line : temp_raw_scaled = (int32_t)data[BMA4_TEMP_BYTE] * BMA4_SCALE_TEMP;
Test case to demonstrate: set data = 0x81
The expected return value in deg C would be -104C
Actual return value is 152,000 C
06-05-2022 03:01 PM
Hi john-rubis,
Which sensor part you used?
And could you attached all the source files you used?
06-05-2022 09:32 PM
06-21-2022 08:42 AM
Hi john-rubis,
We couldn't see the line "temp_raw_scaled = (int32_t)data[BMA4_TEMP_BYTE] * BMA4_SCALE_TEMP;" in driver code.
Which degree Fahrenheit or degree Kelvin you used?
/*!
* @brief This API reads the chip temperature of sensor.
*
* @note Using a scaling factor of 1000, to obtain integer values, which
* at the user end, are used to get accurate temperature value .
* BMA4_FAHREN_SCALED = 1.8 * 1000, BMA4_KELVIN_SCALED = 273.15 * 1000
*/
int8_t bma4_get_temperature(int32_t *temp, uint8_t temp_unit, struct bma4_dev *dev)
{
int8_t rslt;
uint8_t data[BMA4_TEMP_DATA_SIZE] = { 0 };
int32_t temp_raw_scaled = 0;
/* Check the dev structure as NULL */
rslt = null_pointer_check(dev);
if ((rslt == BMA4_OK) && (temp != NULL))
{
/* Read temperature value from the register */
rslt = bma4_read_regs(BMA4_TEMPERATURE_ADDR, data, BMA4_TEMP_DATA_SIZE, dev);
if (rslt == BMA4_OK)
{
temp_raw_scaled = (int32_t)data[BMA4_TEMP_BYTE] * BMA4_SCALE_TEMP;
}
/* '0' value read from the register corresponds to 23 degree C */
(*temp) = temp_raw_scaled + (BMA4_OFFSET_TEMP * BMA4_SCALE_TEMP);
switch (temp_unit)
{
case BMA4_DEG:
break;
case BMA4_FAHREN:
/* Temperature in degree Fahrenheit */
(*temp) = (((*temp) / BMA4_SCALE_TEMP) * BMA4_FAHREN_SCALED) + (32 * BMA4_SCALE_TEMP);
break;
case BMA4_KELVIN:
/* Temperature in degree Kelvin */
(*temp) = (*temp) + BMA4_KELVIN_SCALED;
break;
default:
break;
}
}
else
{
rslt = BMA4_E_NULL_PTR;
}
return rslt;
}
06-21-2022 01:14 PM - edited 06-21-2022 01:14 PM
https://github.com/BoschSensortec/BMA456-Sensor-API/blob/master/bma4.c
Line 1634
Celcius. So temp_unit is equal to BMA4_DEG