From 954cef32ed28458b36b3555dbb278dff255b8822 Mon Sep 17 00:00:00 2001 From: Bosch Sensortec Date: Fri, 6 Apr 2018 12:27:25 +0200 Subject: [PATCH] Changed from single precision floating point to double precision. --- README.md | 344 ++++++++++++++++++++++++++++------------------------ bmp3.c | 108 ++++++++--------- bmp3.h | 15 ++- bmp3_defs.h | 65 ++++++---- 4 files changed, 291 insertions(+), 241 deletions(-) diff --git a/README.md b/README.md index f953799..bf1531f 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,56 @@ # BMP3 sensor API + ## Introduction + This package contains the Bosch Sensortec's BMP3 pressure sensor driver (sensor API) The sensor driver package includes bmp3.h, bmp3.c and bmp3_defs.h files ## Version + File | Version | Date ------------|---------|----- -bmp3.c | 1.0.0 | 04 Dec 2017 -bmp3.h | 1.0.0 | 04 Dec 2017 -bmp3_defs.h | 1.0.0 | 04 Dec 2017 +bmp3.c | 1.1.0 | 05 Apr 2018 +bmp3.h | 1.1.0 | 05 Apr 2018 +bmp3_defs.h | 1.1.0 | 05 Apr 2018 ## Integration details -* Integrate bmp3.h, bmp3_defs.h and bmp3.c file in to your project. -* Include the bmp3.h file in your code like below. -``` c + +- Integrate bmp3.h, bmp3_defs.h and bmp3.c file in to your project. +- Include the bmp3.h file in your code like below. + +```c #include "bmp3.h" ``` ## File information -* bmp3_defs.h : This header file has the constants, macros and datatype declarations. -* bmp3.h : This header file contains the declarations of the sensor driver APIs. -* bmp3.c : This source file contains the definitions of the sensor driver APIs. + +- bmp3_defs.h : This header file has the constants, macros and datatype declarations. +- bmp3.h : This header file contains the declarations of the sensor driver APIs. +- bmp3.c : This source file contains the definitions of the sensor driver APIs. ## Supported sensor interfaces -* SPI 4-wire -* I2C + +- SPI 4-wire +- I2C ## Usage guide + ### Initializing the sensor + To initialize the sensor, you will first need to create a device structure. You -can do this by creating an instance of the structure bmp3_dev. Then go on to +can do this by creating an instance of the structure bmp3_dev. Then go on to fill in the various parameters as shown below. Regarding Compensation functions for temperature and pressure, we have two implementations. -1) Float version +1) Double precision floating point version 2) Integer version -If you want to use float version, enable the FLOATING_POINT_COMPENSATION macro else -integer version will be used. Below example code uses floating point representation. +If you want to use the floating point version, define the BMP3_DOUBLE_PRECISION_COMPENSATION macro in your makefile or uncomment the relevant line in the bmp3_defs.h file. By default, the integer version will be used. Below example code uses floating point representation. #### Example for SPI 4-Wire -``` c + +```c struct bmp3_dev dev; int8_t rslt = BMP3_OK; @@ -54,8 +63,10 @@ dev.delay_ms = user_delay_ms; rslt = bmp3_init(&dev); ``` + #### Example for I2C -``` c + +```c struct bmp3_dev dev; int8_t rslt = BMP3_OK; @@ -69,181 +80,194 @@ rslt = bmp3_init(&dev); ``` ### Configuring the sensor + #### Forced mode + ##### Example for configuring the sensor without oversampling settings -``` c + +```c int8_t set_forced_mode(struct bmp3_dev *dev) { - int8_t rslt; - /* Used to select the settings user needs to change */ - uint16_t settings_sel; - - /* Select the pressure and temperature sensor to be enabled */ - dev->settings.press_en = BMP3_ENABLE; - dev->settings.temp_en = BMP3_ENABLE; - /* Assign the settings which needs to be set in the sensor */ - settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL; - /* Write the settings in the sensor */ - rslt = bmp3_set_sensor_settings(settings_sel, dev); - - /* Select the power mode */ - dev->settings.op_mode = BMP3_FORCED_MODE; - /* Set the power mode in the sensor */ - rslt = bmp3_set_op_mode(dev); - - return rslt; + int8_t rslt; + /* Used to select the settings user needs to change */ + uint16_t settings_sel; + + /* Select the pressure and temperature sensor to be enabled */ + dev->settings.press_en = BMP3_ENABLE; + dev->settings.temp_en = BMP3_ENABLE; + /* Assign the settings which needs to be set in the sensor */ + settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL; + /* Write the settings in the sensor */ + rslt = bmp3_set_sensor_settings(settings_sel, dev); + + /* Select the power mode */ + dev->settings.op_mode = BMP3_FORCED_MODE; + /* Set the power mode in the sensor */ + rslt = bmp3_set_op_mode(dev); + + return rslt; } ``` + ##### Example for configuring the sensor with oversampling settings -``` c + +```c int8_t set_forced_mode_with_osr(struct bmp3_dev *dev) { - int8_t rslt; - /* Used to select the settings user needs to change */ - uint16_t settings_sel; - - /* Select the pressure and temperature sensor to be enabled */ - dev->settings.press_en = BMP3_ENABLE; - dev->settings.temp_en = BMP3_ENABLE; - /* Select the oversampling settings for pressure and temperature */ - dev->settings.odr_filter.press_os = BMP3_OVERSAMPLING_2X; - dev->settings.odr_filter.temp_os = BMP3_OVERSAMPLING_2X; - /* Assign the settings which needs to be set in the sensor */ - settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL | BMP3_PRESS_OS_SEL | BMP3_TEMP_OS_SEL; - /* Write the settings in the sensor */ - rslt = bmp3_set_sensor_settings(settings_sel, dev); - - /* Select the power mode */ - dev->settings.op_mode = BMP3_FORCED_MODE; - /* Set the power mode in the sensor */ - rslt = bmp3_set_op_mode(dev); - - return rslt; + int8_t rslt; + /* Used to select the settings user needs to change */ + uint16_t settings_sel; + + /* Select the pressure and temperature sensor to be enabled */ + dev->settings.press_en = BMP3_ENABLE; + dev->settings.temp_en = BMP3_ENABLE; + /* Select the oversampling settings for pressure and temperature */ + dev->settings.odr_filter.press_os = BMP3_OVERSAMPLING_2X; + dev->settings.odr_filter.temp_os = BMP3_OVERSAMPLING_2X; + /* Assign the settings which needs to be set in the sensor */ + settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL | BMP3_PRESS_OS_SEL | BMP3_TEMP_OS_SEL; + /* Write the settings in the sensor */ + rslt = bmp3_set_sensor_settings(settings_sel, dev); + + /* Select the power mode */ + dev->settings.op_mode = BMP3_FORCED_MODE; + /* Set the power mode in the sensor */ + rslt = bmp3_set_op_mode(dev); + + return rslt; } ``` + #### Normal mode + ##### Example for configuring the sensor with output data rate and oversampling settings -``` c + +```c int8_t set_normal_mode(struct bmp3_dev *dev) { - int8_t rslt; - /* Used to select the settings user needs to change */ - uint16_t settings_sel; - - /* Select the pressure and temperature sensor to be enabled */ - dev->settings.press_en = BMP3_ENABLE; - dev->settings.temp_en = BMP3_ENABLE; - /* Select the output data rate and oversampling settings for pressure and temperature */ - dev->settings.odr_filter.press_os = BMP3_NO_OVERSAMPLING; - dev->settings.odr_filter.temp_os = BMP3_NO_OVERSAMPLING; - dev->settings.odr_filter.odr = BMP3_ODR_200_HZ; - /* Assign the settings which needs to be set in the sensor */ - settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL | BMP3_PRESS_OS_SEL | BMP3_TEMP_OS_SEL | BMP3_ODR_SEL; - rslt = bmp3_set_sensor_settings(settings_sel, dev); - - /* Set the power mode to normal mode */ - dev->settings.op_mode = BMP3_NORMAL_MODE; - rslt = bmp3_set_op_mode(dev); - - return rslt; + int8_t rslt; + /* Used to select the settings user needs to change */ + uint16_t settings_sel; + + /* Select the pressure and temperature sensor to be enabled */ + dev->settings.press_en = BMP3_ENABLE; + dev->settings.temp_en = BMP3_ENABLE; + /* Select the output data rate and oversampling settings for pressure and temperature */ + dev->settings.odr_filter.press_os = BMP3_NO_OVERSAMPLING; + dev->settings.odr_filter.temp_os = BMP3_NO_OVERSAMPLING; + dev->settings.odr_filter.odr = BMP3_ODR_200_HZ; + /* Assign the settings which needs to be set in the sensor */ + settings_sel = BMP3_PRESS_EN_SEL | BMP3_TEMP_EN_SEL | BMP3_PRESS_OS_SEL | BMP3_TEMP_OS_SEL | BMP3_ODR_SEL; + rslt = bmp3_set_sensor_settings(settings_sel, dev); + + /* Set the power mode to normal mode */ + dev->settings.op_mode = BMP3_NORMAL_MODE; + rslt = bmp3_set_op_mode(dev); + + return rslt; } ``` + ### Reading sensor data + #### Example for reading all sensor data -``` c + +```c int8_t get_sensor_data(struct bmp3_dev *dev) { - int8_t rslt; - /* Variable used to select the sensor component */ - uint8_t sensor_comp; - /* Variable used to store the compensated data */ - struct bmp3_data data; - - /* Sensor component selection */ - sensor_comp = BMP3_PRESS | BMP3_TEMP; - /* Temperature and Pressure data are read and stored in the bmp3_data instance */ - rslt = bmp3_get_sensor_data(sensor_comp, &data, dev); - - /* Print the temperature and pressure data */ - printf("Temperature\t Pressure\t\n"); - printf("%0.2f\t\t %0.2f\t\t\n",data.temperature, data.pressure); - - return rslt; + int8_t rslt; + /* Variable used to select the sensor component */ + uint8_t sensor_comp; + /* Variable used to store the compensated data */ + struct bmp3_data data; + + /* Sensor component selection */ + sensor_comp = BMP3_PRESS | BMP3_TEMP; + /* Temperature and Pressure data are read and stored in the bmp3_data instance */ + rslt = bmp3_get_sensor_data(sensor_comp, &data, dev); + + /* Print the temperature and pressure data */ + printf("Temperature\t Pressure\t\n"); + printf("%0.2f\t\t %0.2f\t\t\n",data.temperature, data.pressure); + + return rslt; } ``` ### Configure and read FIFO data + #### Example for configuring and reading the FIFO data. -``` c + +```c int8_t configure_and_get_fifo_data(struct bmp3_dev *dev) { - int8_t rslt; - /* Loop Variable */ - uint8_t i; - /* FIFO object to be assigned to device structure */ - struct bmp3_fifo fifo; - /* Pressure and temperature array of structures with maximum frame size */ - struct bmp3_data sensor_data[73] = {0}; - /* Used to select the settings user needs to change */ - uint16_t settings_sel; - /* try count for polling the watermark interrupt status */ - uint8_t try_count; - - /* Enable fifo */ - fifo.settings.mode = BMP3_ENABLE; - /* Enable Pressure sensor for fifo */ - fifo.settings.press_en = BMP3_ENABLE; - /* Enable temperature sensor for fifo */ - fifo.settings.temp_en = BMP3_ENABLE; - /* Enable fifo time */ - fifo.settings.time_en = BMP3_ENABLE; - /* No subsampling for FIFO */ - fifo.settings.down_sampling = BMP3_FIFO_NO_SUBSAMPLING; - /* FIFO watemrmark interrupt enable */ - fifo.settings.fwtm_en = BMP3_ENABLE; - - /* Link the fifo object to device structure */ - dev->fifo = &fifo; - /* Select the settings required for fifo */ - settings_sel = BMP3_FIFO_MODE_SEL | BMP3_FIFO_TIME_EN_SEL | BMP3_FIFO_TEMP_EN_SEL | - BMP3_FIFO_PRESS_EN_SEL | BMP3_FIFO_DOWN_SAMPLING_SEL | BMP3_FIFO_FWTM_EN_SEL; - /* Set the selected settings in fifo */ - rslt = bmp3_set_fifo_settings(settings_sel, dev); - - /* Set the number of frames to be read so as to set the watermark length in the sensor */ - dev->fifo->data.req_frames = 50; - rslt = bmp3_set_fifo_watermark(dev); - - /* Set the power mode to normal */ - rslt = set_normal_mode(dev); - - /* TODO : To calculate the exact time for try count variable */ - try_count = 0xFFFF; - /* Poll till watermark level is reached in fifo */ - do { - rslt = bmp3_get_status(dev); - try_count--; - } while ((dev->status.intr.fifo_wm == 0) && (try_count > 0)); - - if (try_count > 0) { - rslt = bmp3_get_fifo_data(dev); - rslt = bmp3_extract_fifo_data(sensor_data, dev); - printf("FIFO data\n"); - printf("Temp\tPress\n"); - /* Print the fifo data */ - for (i = 0; i < dev->fifo->data.req_frames; i++) { + int8_t rslt; + /* Loop Variable */ + uint8_t i; + /* FIFO object to be assigned to device structure */ + struct bmp3_fifo fifo; + /* Pressure and temperature array of structures with maximum frame size */ + struct bmp3_data sensor_data[73] = {0}; + /* Used to select the settings user needs to change */ + uint16_t settings_sel; + /* try count for polling the watermark interrupt status */ + uint8_t try_count; + + /* Enable fifo */ + fifo.settings.mode = BMP3_ENABLE; + /* Enable Pressure sensor for fifo */ + fifo.settings.press_en = BMP3_ENABLE; + /* Enable temperature sensor for fifo */ + fifo.settings.temp_en = BMP3_ENABLE; + /* Enable fifo time */ + fifo.settings.time_en = BMP3_ENABLE; + /* No subsampling for FIFO */ + fifo.settings.down_sampling = BMP3_FIFO_NO_SUBSAMPLING; + /* FIFO watemrmark interrupt enable */ + fifo.settings.fwtm_en = BMP3_ENABLE; + + /* Link the fifo object to device structure */ + dev->fifo = &fifo; + /* Select the settings required for fifo */ + settings_sel = BMP3_FIFO_MODE_SEL | BMP3_FIFO_TIME_EN_SEL | BMP3_FIFO_TEMP_EN_SEL | + BMP3_FIFO_PRESS_EN_SEL | BMP3_FIFO_DOWN_SAMPLING_SEL | BMP3_FIFO_FWTM_EN_SEL; + /* Set the selected settings in fifo */ + rslt = bmp3_set_fifo_settings(settings_sel, dev); + + /* Set the number of frames to be read so as to set the watermark length in the sensor */ + dev->fifo->data.req_frames = 50; + rslt = bmp3_set_fifo_watermark(dev); + + /* Set the power mode to normal */ + rslt = set_normal_mode(dev); + + /* TODO : To calculate the exact time for try count variable */ + try_count = 0xFFFF; + /* Poll till watermark level is reached in fifo */ + do { + rslt = bmp3_get_status(dev); + try_count--; + } while ((dev->status.intr.fifo_wm == 0) && (try_count > 0)); + + if (try_count > 0) { + rslt = bmp3_get_fifo_data(dev); + rslt = bmp3_extract_fifo_data(sensor_data, dev); + printf("FIFO data\n"); + printf("Temp\tPress\n"); + /* Print the fifo data */ + for (i = 0; i < dev->fifo->data.req_frames; i++) { #ifdef FLOATING_POINT_COMPENSATION - printf("%0.2f\t%0.2f\n", sensor_data[i].temperature,sensor_data[i].pressure); + printf("%0.2f\t%0.2f\n", sensor_data[i].temperature,sensor_data[i].pressure); #else - printf("%lld\t%lld\n", sensor_data[i].temperature,sensor_data[i].pressure); + printf("%lld\t%lld\n", sensor_data[i].temperature,sensor_data[i].pressure); #endif - } - } else { - rslt = BMP3_E_FIFO_WATERMARK_NOT_REACHED; - } + } + } else { + rslt = BMP3_E_FIFO_WATERMARK_NOT_REACHED; + } - return rslt; + return rslt; } ``` -## Copyright (C) 2016 - 2017 Bosch Sensortec GmbH \ No newline at end of file +## Copyright (C) 2017 - 2018 Bosch Sensortec GmbH \ No newline at end of file diff --git a/bmp3.c b/bmp3.c index 9748754..5f6a07d 100644 --- a/bmp3.c +++ b/bmp3.c @@ -1,5 +1,5 @@ /**\mainpage - * Copyright (C) 2016 - 2017 Bosch Sensortec GmbH + * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * File bmp3.c - * Date 04 Dec 2017 - * Version 1.0.0 + * Date 05 Apr 2018 + * Version 1.1.0 * */ @@ -142,7 +142,7 @@ static void parse_sensor_data(const uint8_t *reg_data, struct bmp3_uncomp_data * */ static int8_t compensate_data(uint8_t sensor_comp, const struct bmp3_uncomp_data *uncomp_data, struct bmp3_data *comp_data, struct bmp3_calib_data *calib_data); -#ifdef FLOATING_POINT_COMPENSATION +#ifdef BMP3_DOUBLE_PRECISION_COMPENSATION /*! * @brief This internal API is used to compensate the raw temperature data and * return the compensated temperature data. @@ -151,9 +151,9 @@ static int8_t compensate_data(uint8_t sensor_comp, const struct bmp3_uncomp_data * @param[in] calib_data : Pointer to calibration data structure. * * @return Compensated temperature data. - * @retval Compensated temperature data in float. + * @retval Compensated temperature data in double. */ -static float compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, +static double compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, struct bmp3_calib_data *calib_data); /*! @@ -164,22 +164,22 @@ static float compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, * @param[in] calib_data : Pointer to the calibration data structure. * * @return Compensated pressure data. - * @retval Compensated pressure data in float. + * @retval Compensated pressure data in double. */ -static float compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, +static double compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, const struct bmp3_calib_data *calib_data); /*! * @brief This internal API is used to calculate the power functionality for - * floating point values. + * double precision floating point values. * * @param[in] base : Contains the base value. * @param[in] power : Contains the power value. * * @return Output of power function. - * @retval Calculated power function output in float. + * @retval Calculated power function output in double. */ -static float bmp3_pow(float base, uint8_t power); +static double bmp3_pow(double base, uint8_t power); #else /*! * @brief This internal API is used to compensate the raw temperature data and @@ -217,7 +217,7 @@ static uint64_t compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, * @retval Calculated power function output in integer. */ static uint32_t bmp3_pow(uint8_t base, uint8_t power); -#endif +#endif /* BMP3_DOUBLE_PRECISION_COMPENSATION */ /*! @@ -1837,13 +1837,13 @@ static uint16_t calculate_press_meas_time(const struct bmp3_dev *dev) { uint16_t press_meas_t; struct bmp3_odr_filter_settings odr_filter = dev->settings.odr_filter; -#ifdef FLOATING_POINT_COMPENSATION - float base = 2.0; - float partial_out; +#ifdef BMP3_DOUBLE_PRECISION_COMPENSATION + double base = 2.0; + double partial_out; #else uint8_t base = 2; uint32_t partial_out; -#endif +#endif /* BMP3_DOUBLE_PRECISION_COMPENSATION */ partial_out = bmp3_pow(base, odr_filter.press_os); press_meas_t = (uint16_t)(BMP3_PRESS_SETTLE_TIME + partial_out * BMP3_ADC_CONV_TIME); @@ -1861,13 +1861,13 @@ static uint16_t calculate_temp_meas_time(const struct bmp3_dev *dev) { uint16_t temp_meas_t; struct bmp3_odr_filter_settings odr_filter = dev->settings.odr_filter; -#ifdef FLOATING_POINT_COMPENSATION +#ifdef BMP3_DOUBLE_PRECISION_COMPENSATION float base = 2.0; float partial_out; #else uint8_t base = 2; uint32_t partial_out; -#endif +#endif /* BMP3_DOUBLE_PRECISION_COMPENSATION */ partial_out = bmp3_pow(base, odr_filter.temp_os); temp_meas_t = (uint16_t)(BMP3_TEMP_SETTLE_TIME + partial_out * BMP3_ADC_CONV_TIME); @@ -1988,7 +1988,7 @@ static int8_t compensate_data(uint8_t sensor_comp, const struct bmp3_uncomp_data return rslt; } -#ifdef FLOATING_POINT_COMPENSATION +#ifdef BMP3_DOUBLE_PRECISION_COMPENSATION /*! * @brief This internal API is used to parse the calibration data, compensates * it and store it in device structure @@ -1999,80 +1999,80 @@ static void parse_calib_data(const uint8_t *reg_data, struct bmp3_dev *dev) struct bmp3_reg_calib_data *reg_calib_data = &dev->calib_data.reg_calib_data; struct bmp3_quantized_calib_data *quantized_calib_data = &dev->calib_data.quantized_calib_data; /* Temporary variable */ - float temp_var; + double temp_var; /* 1 / 2^8 */ temp_var = 0.00390625f; reg_calib_data->par_t1 = BMP3_CONCAT_BYTES(reg_data[1], reg_data[0]); - quantized_calib_data->par_t1 = ((float)reg_calib_data->par_t1 / temp_var); + quantized_calib_data->par_t1 = ((double)reg_calib_data->par_t1 / temp_var); reg_calib_data->par_t2 = BMP3_CONCAT_BYTES(reg_data[3], reg_data[2]); temp_var = 1073741824.0f; - quantized_calib_data->par_t2 = ((float)reg_calib_data->par_t2 / temp_var); + quantized_calib_data->par_t2 = ((double)reg_calib_data->par_t2 / temp_var); reg_calib_data->par_t3 = (int8_t)reg_data[4]; temp_var = 281474976710656.0f; - quantized_calib_data->par_t3 = ((float)reg_calib_data->par_t3 / temp_var); + quantized_calib_data->par_t3 = ((double)reg_calib_data->par_t3 / temp_var); reg_calib_data->par_p1 = (int16_t)BMP3_CONCAT_BYTES(reg_data[6], reg_data[5]); temp_var = 1048576.0f; - quantized_calib_data->par_p1 = ((float)(reg_calib_data->par_p1 - (16384)) / temp_var); + quantized_calib_data->par_p1 = ((double)(reg_calib_data->par_p1 - (16384)) / temp_var); reg_calib_data->par_p2 = (int16_t)BMP3_CONCAT_BYTES(reg_data[8], reg_data[7]); temp_var = 536870912.0f; - quantized_calib_data->par_p2 = ((float)(reg_calib_data->par_p2 - (16384)) / temp_var); + quantized_calib_data->par_p2 = ((double)(reg_calib_data->par_p2 - (16384)) / temp_var); reg_calib_data->par_p3 = (int8_t)reg_data[9]; temp_var = 4294967296.0f; - quantized_calib_data->par_p3 = ((float)reg_calib_data->par_p3 / temp_var); + quantized_calib_data->par_p3 = ((double)reg_calib_data->par_p3 / temp_var); reg_calib_data->par_p4 = (int8_t)reg_data[10]; temp_var = 137438953472.0f; - quantized_calib_data->par_p4 = ((float)reg_calib_data->par_p4 / temp_var); + quantized_calib_data->par_p4 = ((double)reg_calib_data->par_p4 / temp_var); reg_calib_data->par_p5 = BMP3_CONCAT_BYTES(reg_data[12], reg_data[11]); /* 1 / 2^3 */ temp_var = 0.125f; - quantized_calib_data->par_p5 = ((float)reg_calib_data->par_p5 / temp_var); + quantized_calib_data->par_p5 = ((double)reg_calib_data->par_p5 / temp_var); reg_calib_data->par_p6 = BMP3_CONCAT_BYTES(reg_data[14], reg_data[13]); temp_var = 64.0f; - quantized_calib_data->par_p6 = ((float)reg_calib_data->par_p6 / temp_var); + quantized_calib_data->par_p6 = ((double)reg_calib_data->par_p6 / temp_var); reg_calib_data->par_p7 = (int8_t)reg_data[15]; temp_var = 256.0f; - quantized_calib_data->par_p7 = ((float)reg_calib_data->par_p7 / temp_var); + quantized_calib_data->par_p7 = ((double)reg_calib_data->par_p7 / temp_var); reg_calib_data->par_p8 = (int8_t)reg_data[16]; temp_var = 32768.0f; - quantized_calib_data->par_p8 = ((float)reg_calib_data->par_p8 / temp_var); + quantized_calib_data->par_p8 = ((double)reg_calib_data->par_p8 / temp_var); reg_calib_data->par_p9 = (int16_t)BMP3_CONCAT_BYTES(reg_data[18], reg_data[17]); temp_var = 281474976710656.0f; - quantized_calib_data->par_p9 = ((float)reg_calib_data->par_p9 / temp_var); + quantized_calib_data->par_p9 = ((double)reg_calib_data->par_p9 / temp_var); reg_calib_data->par_p10 = (int8_t)reg_data[19]; temp_var = 281474976710656.0f; - quantized_calib_data->par_p10 = ((float)reg_calib_data->par_p10 / temp_var); + quantized_calib_data->par_p10 = ((double)reg_calib_data->par_p10 / temp_var); reg_calib_data->par_p11 = (int8_t)reg_data[20]; temp_var = 36893488147419103232.0f; - quantized_calib_data->par_p11 = ((float)reg_calib_data->par_p11 / temp_var); + quantized_calib_data->par_p11 = ((double)reg_calib_data->par_p11 / temp_var); } /*! * @brief This internal API is used to compensate the raw temperature data and - * return the compensated temperature data in float data type. + * return the compensated temperature data in double data type. */ -static float compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, +static double compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, struct bmp3_calib_data *calib_data) { uint32_t uncomp_temp = uncomp_data->temperature; - float partial_data1; - float partial_data2; + double partial_data1; + double partial_data2; - partial_data1 = (float)(uncomp_temp - calib_data->quantized_calib_data.par_t1); - partial_data2 = (float)(partial_data1 * calib_data->quantized_calib_data.par_t2); + partial_data1 = (double)(uncomp_temp - calib_data->quantized_calib_data.par_t1); + partial_data2 = (double)(partial_data1 * calib_data->quantized_calib_data.par_t2); /* Update the compensated temperature in calib structure since this is needed for pressure calculation */ calib_data->quantized_calib_data.t_lin = partial_data2 + (partial_data1 * partial_data1) @@ -2084,21 +2084,21 @@ static float compensate_temperature(const struct bmp3_uncomp_data *uncomp_data, /*! * @brief This internal API is used to compensate the raw pressure data and - * return the compensated pressure data in float data type. + * return the compensated pressure data in double data type. */ -static float compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, +static double compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, const struct bmp3_calib_data *calib_data) { const struct bmp3_quantized_calib_data *quantized_calib_data = &calib_data->quantized_calib_data; /* Variable to store the compensated pressure */ - float comp_press; + double comp_press; /* Temporary variables used for compensation */ - float partial_data1; - float partial_data2; - float partial_data3; - float partial_data4; - float partial_out1; - float partial_out2; + double partial_data1; + double partial_data2; + double partial_data3; + double partial_data4; + double partial_out1; + double partial_out2; partial_data1 = quantized_calib_data->par_p6 * quantized_calib_data->t_lin; partial_data2 = quantized_calib_data->par_p7 * bmp3_pow(quantized_calib_data->t_lin, 2); @@ -2111,10 +2111,10 @@ static float compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, partial_out2 = uncomp_data->pressure * (quantized_calib_data->par_p1 + partial_data1 + partial_data2 + partial_data3); - partial_data1 = bmp3_pow((float)uncomp_data->pressure, 2); + partial_data1 = bmp3_pow((double)uncomp_data->pressure, 2); partial_data2 = quantized_calib_data->par_p9 + quantized_calib_data->par_p10 * quantized_calib_data->t_lin; partial_data3 = partial_data1 * partial_data2; - partial_data4 = partial_data3 + bmp3_pow((float)uncomp_data->pressure, 3) * quantized_calib_data->par_p11; + partial_data4 = partial_data3 + bmp3_pow((double)uncomp_data->pressure, 3) * quantized_calib_data->par_p11; comp_press = partial_out1 + partial_out2 + partial_data4; return comp_press; @@ -2122,11 +2122,11 @@ static float compensate_pressure(const struct bmp3_uncomp_data *uncomp_data, /*! * @brief This internal API is used to calculate the power functionality for - * floating point values. + * double precision floating point values. */ -static float bmp3_pow(float base, uint8_t power) +static double bmp3_pow(double base, uint8_t power) { - float pow_output = 1; + double pow_output = 1; while (power != 0) { pow_output = base * pow_output; diff --git a/bmp3.h b/bmp3.h index 8d8ed1c..26dcd3e 100644 --- a/bmp3.h +++ b/bmp3.h @@ -1,5 +1,5 @@ /** - * Copyright (C) 2016 - 2017 Bosch Sensortec GmbH + * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * @file bmp3.h - * @date 04 Dec 2017 - * @version 1.0.0 + * @date 05 Apr 2018 + * @version 1.1.0 * @brief * */ @@ -57,6 +57,11 @@ /* Header includes */ #include "bmp3_defs.h" +/*! CPP guard */ +#ifdef __cplusplus +extern "C" +{ +#endif /*! * @brief This API is the entry point. @@ -317,6 +322,10 @@ int8_t bmp3_get_status(struct bmp3_dev *dev); */ int8_t bmp3_set_fifo_watermark(const struct bmp3_dev *dev); +#ifdef __cplusplus +} +#endif /* End of CPP guard */ + #endif /* BMP3_H_ */ /** @}*/ diff --git a/bmp3_defs.h b/bmp3_defs.h index 5ee72c7..ec1901e 100644 --- a/bmp3_defs.h +++ b/bmp3_defs.h @@ -1,5 +1,5 @@ /** - * Copyright (C) 2016 - 2017 Bosch Sensortec GmbH + * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * @file bmp3_defs.h - * @date 04 Dec 2017 - * @version 1.0.0 + * @date 05 Apr 2018 + * @version 1.1.0 * @brief * */ @@ -55,6 +55,12 @@ #ifndef BMP3_DEFS_H_ #define BMP3_DEFS_H_ +/*! CPP guard */ +#ifdef __cplusplus +extern "C" +{ +#endif + /********************************************************/ /* header includes */ #ifdef __KERNEL__ @@ -98,13 +104,20 @@ #endif #endif +#ifndef TRUE #define TRUE UINT8_C(1) +#endif + +#ifndef FALSE #define FALSE UINT8_C(0) +#endif /********************************************************/ /**\name Compiler switch macros */ -/**\name Comment the below line to use fixed-point compensation */ -/* #define FLOATING_POINT_COMPENSATION */ +/**\name Uncomment the below line to use floating-point compensation */ +#ifndef BMP3_DOUBLE_PRECISION_COMPENSATION +/* #define BMP3_DOUBLE_PRECISION_COMPENSATION */ +#endif /********************************************************/ /**\name Macro definitions */ @@ -617,7 +630,7 @@ struct bmp3_fifo { struct bmp3_fifo_settings settings; }; -#ifdef FLOATING_POINT_COMPENSATION +#ifdef BMP3_DOUBLE_PRECISION_COMPENSATION /*! * @brief Quantized Trim Variables */ @@ -626,21 +639,21 @@ struct bmp3_quantized_calib_data { * @ Quantized Trim Variables */ /**@{*/ - float par_t1; - float par_t2; - float par_t3; - float par_p1; - float par_p2; - float par_p3; - float par_p4; - float par_p5; - float par_p6; - float par_p7; - float par_p8; - float par_p9; - float par_p10; - float par_p11; - float t_lin; + double par_t1; + double par_t2; + double par_t3; + double par_p1; + double par_p2; + double par_p3; + double par_p4; + double par_p5; + double par_p6; + double par_p7; + double par_p8; + double par_p9; + double par_p10; + double par_p11; + double t_lin; /**@}*/ }; @@ -660,9 +673,9 @@ struct bmp3_calib_data { */ struct bmp3_data { /*! Compensated temperature */ - float temperature; + double temperature; /*! Compensated pressure */ - float pressure; + double pressure; }; #else @@ -685,7 +698,7 @@ struct bmp3_calib_data { struct bmp3_reg_calib_data reg_calib_data; }; -#endif /* BMP3_USE_FLOATING_POINT */ +#endif /* BMP3_DOUBLE_PRECISION_COMPENSATION */ /*! * @brief bmp3 sensor structure which comprises of uncompensated temperature @@ -726,6 +739,10 @@ struct bmp3_dev { struct bmp3_fifo *fifo; }; +#ifdef __cplusplus +} +#endif /* End of CPP guard */ + #endif /* BMP3_DEFS_H_ */ /** @}*/ /** @}*/