I compiled a test setup for the BMP388 on a 16-bit MCU. Sensor readout worked fine, but the compensated temperature and pressure readings were far off (142 instead of 25 degrees). Then I defined BMP3_DOUBLE_PRECISION_COMPENSATION, which solved the issue: temp and pressure readings were good.
Investigation a bit, I came to the conclusion that the error is in the temp/pressure compensation functions (integer version) in bmp3.c.
In bmp3.c, static int64_t compensate_temperature(), line 2452:
partial_data1 = ((int64_t)uncomp_data->temperature - (256 * calib_data->reg_calib_data.par_t1));
This computation caused an overflow on my 16-bit MCU, because 256 was promoted as int16_t. On a 32-bit MCU, there probably is no overflow occurring, because 256 would be promoted as int32_t. I guess the function was tested on a 32-bit architecture.
I solved the issue by changing explicitely declaring the literal as int32_t:
partial_data1 = ((int64_t)uncomp_data->temperature - ((int32_t)256 * calib_data->reg_calib_data.par_t1));
There are more literals affected further below: lines 2494, 2495:
partial_data5 = (reg_calib_data->par_p2 - (int32_t)16384) * reg_calib_data->t_lin * 2097152; sensitivity = ((reg_calib_data->par_p1 - (int32_t)16384) * 70368744177664) + partial_data2 + partial_data4 + partial_data5;
I don't know how to solve this issue properly to make the API portable, but I think it should be addressed by the Bosch experts.
Thanks for your feedback. We will look into the API code for 16-bit MCU to see if there is any overflow issue or not.
Just to let you know that this overflow is a known API issue in BMP3 API. It has been fixed on API v2.0.3. Please make sure you use the BMP3 API version >= v2.0.3 on the Github.
Thanks for resolving this issue. However, I can't find v2.0.3 on Github. Will it be published soon?
Right, the new BMP3 API v2.0.3 will be published soon on the Github. Thanks.