BMP180.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: Beerware
2 /**
3  * @file BMP180.c
4  * @brief Digital pressure sensor driver
5  * @details Bosch Sensortec BMP180 I²C digital pressure sensor
6  * @ingroup BMP180
7  * @defgroup BMP180 BMP180 I²C digital pressure sensor
8  * @author Michael Fitzmayer
9  * @copyright "THE BEER-WARE LICENCE" (Revision 42)
10  */
11 
12 #ifdef USE_BMP180
13 
14 #include <stdint.h>
15 #include "BMP180.h"
16 #include "MCAL.h"
17 
18 /**
19  * @enum BMP180_Register
20  * @brief BMP180 register (global memory map)
21  */
22 typedef enum
23 {
24  E2PROM_AC1 = 0xAA, ///< Calibration coeff. AC1
25  E2PROM_AC2 = 0xAC, ///< Calibration coeff. AC2
26  E2PROM_AC3 = 0xAE, ///< Calibration coeff. AC3
27  E2PROM_AC4 = 0xB0, ///< Calibration coeff. AC4
28  E2PROM_AC5 = 0xB2, ///< Calibration coeff. AC5
29  E2PROM_AC6 = 0xB4, ///< Calibration coeff. AC6
30  E2PROM_B1 = 0xB6, ///< Calibration coeff. B1
31  E2PROM_B2 = 0xB8, ///< Calibration coeff. BS
32  E2PROM_MB = 0xBA, ///< Calibration coeff. MB
33  E2PROM_MC = 0xBC, ///< Calibration coeff. MC
34  E2PROM_MD = 0xBE, ///< Calibration coeff. MD
35  CHIP_ID = 0xD0, ///< Chip-ID, can be checked against @ref BMP180_CHIP_ID
36  SOFT_RESET = 0xE0, ///< Soft reset if set to 0xB6
37  CTRL_MEAS = 0xF4, ///< Measurement control
38  OUT_MSB = 0xF6, ///< ADC out LSB
39  OUT_LSB = 0xF7, ///< ADC out MSB
40  OUT_XLSB = 0xF8 ///< ADC out XLSB
41 
43 
44 /**
45  * @struct BMP180_CalibCoefficients
46  * @brief BMP180 Calibration coefficients
47  */
48 typedef struct
49 {
50  int16_t s16AC1; ///< Calibration coeff. AC1
51  int16_t s16AC2; ///< Calibration coeff. AC2
52  int16_t s16AC3; ///< Calibration coeff. AC3
53  uint16_t u16AC4; ///< Calibration coeff. AC4
54  uint16_t u16AC5; ///< Calibration coeff. AC5
55  uint16_t u16AC6; ///< Calibration coeff. AC6
56  int16_t s16B1; ///< Calibration coeff. B1
57  int16_t s16B2; ///< Calibration coeff. B2
58  int16_t s16MB; ///< Calibration coeff. MB
59  int16_t s16MC; ///< Calibration coeff. MC
60  int16_t s16MD; ///< Calibration coeff. MD
61 
63 
64 /**
65  * @struct BMP180_Data
66  * @brief BMP180 driver data
67  */
68 typedef struct
69 {
70  uint8_t u8ChipID; ///< Chip-ID
71  BMP180_CalibCoefficients stCalibData; ///< Calibration coefficients
72 
73 } BMP180_Data;
74 
75 /**
76  * @var _stBMP180Data
77  * @brief BMP180 driver private data
78  */
79 static BMP180_Data _stBMP180Data = { 0 };
80 
81 static int16_t _BMP180_GetB5(int32_t s32UT);
82 static int _BMP180_ReadRegister(const BMP180_Register eReg, int16_t* ps16Value);
83 
84 /**
85  * @brief Initialise BMP180 driver
86  * @return Error code
87  * @retval 0: OK
88  * @retval -1: Error
89  */
90 int BMP180_Init(void)
91 {
92  int nError;
93 
94  // Check if communicaton is functioning
96  if (0 != nError)
97  {
98  return -1;
99  }
101 
103  {
104  // Error: invalid chip-ID
105  return -1;
106  }
107 
108  // Read calibration data from E²PROM
120 
121  return 0;
122 }
123 
124 /**
125  * @brief Read current temperature
126  * @param ps8Temp
127  * Temperature in 1°C
128  * @return Error code
129  * @retval 0: OK
130  * @retval -1: Error
131  */
132 int BMP180_ReadTemperature(int8_t* ps8Temp)
133 {
134  int nError;
135  uint8_t u8RegValue = OSS_TEMPERATURE;
136  int32_t s32UT = 0;
137  int32_t s32T = 0;
138 
139  // Trigger temperature conversion
140  nError = I2C_Transmit(BMP180_ADDRESS_WRITE, CTRL_MEAS, I2C_MEMSIZE_8BIT, &u8RegValue, 1);
141  if (0 != nError)
142  {
143  return -1;
144  }
146 
147  // Wait until conversion is complete
148  MCAL_Sleep(5);
149 
150  // Read uncompensated temperature value
151  nError = _BMP180_ReadRegister(OUT_MSB, (int16_t*)&s32UT);
152  if (0 != nError)
153  {
154  return -1;
155  }
156 
157  // Calculate true temperature
158  s32T = (_BMP180_GetB5(s32UT) + 8) >> 4;
159  s32T /= 10;
160 
161  *ps8Temp = (int8_t)(s32T);
162 
163  return 0;
164 }
165 
166 /**
167  * @brief Convert uncompensated temperature to B5
168  * @details As required to calculate true temperature and true pressure
169  * @param s32UT
170  * Uncompensated temperature
171  * @return B5
172  */
173 static int16_t _BMP180_GetB5(int32_t s32UT)
174 {
175  int32_t s32X1 = 0;
176  int32_t s32X2 = 0;
177 
178  s32X1 = (s32UT - _stBMP180Data.stCalibData.u16AC6) * (_stBMP180Data.stCalibData.u16AC5) >> 15;
179  s32X2 = (_stBMP180Data.stCalibData.s16MC << 11) / (s32X1 + _stBMP180Data.stCalibData.s16MD);
180 
181  return s32X1 + s32X2;
182 }
183 
184 /**
185  * @brief Read register
186  * @param eReg
187  * BMP180 E²PROM register
188  * @param ps16Value
189  * Register value
190  * @return Error code
191  * @retval 0: OK
192  * @retval -1: Error
193  */
194 static int _BMP180_ReadRegister(const BMP180_Register eReg, int16_t* ps16Value)
195 {
196  int nError;
197 
198  if (OUT_LSB < eReg)
199  {
200  return -1;
201  }
202 
203  nError = I2C_Receive(BMP180_ADDRESS_READ, eReg, I2C_MEMSIZE_8BIT, (uint8_t*)ps16Value, 2);
204  if (0 != nError)
205  {
206  return -1;
207  }
209 
210  *ps16Value = ((*ps16Value << 8) + (*ps16Value >> 8));
211 
212  return 0;
213 }
214 
215 #endif // USE_BMP180
@ E2PROM_MB
Calibration coeff. MB.
Definition: BMP180.c:32
Digital pressure sensor driver.
int16_t s16AC2
Calibration coeff. AC2.
Definition: BMP180.c:51
#define BMP180_ADDRESS_WRITE
Device address for write operations.
Definition: BMP180.h:13
@ E2PROM_AC6
Calibration coeff. AC6.
Definition: BMP180.c:29
@ OUT_LSB
ADC out MSB.
Definition: BMP180.c:39
BMP180_CalibCoefficients stCalibData
Calibration coefficients.
Definition: BMP180.c:71
@ OUT_XLSB
ADC out XLSB.
Definition: BMP180.c:40
void I2C_WaitUntilReady(uint16_t u16DevAddress)
Wait for the end of the transfer.
Definition: MCAL.c:186
static int _BMP180_ReadRegister(const BMP180_Register eReg, int16_t *ps16Value)
Read register.
Definition: BMP180.c:194
uint16_t u16AC5
Calibration coeff. AC5.
Definition: BMP180.c:54
@ CTRL_MEAS
Measurement control.
Definition: BMP180.c:37
int BMP180_ReadTemperature(int8_t *ps8Temp)
Read current temperature.
Definition: BMP180.c:132
#define BMP180_CHIP_ID
Chip ID.
Definition: BMP180.h:11
#define BMP180_ADDRESS_READ
Device address for read operations.
Definition: BMP180.h:12
@ E2PROM_AC5
Calibration coeff. AC5.
Definition: BMP180.c:28
@ SOFT_RESET
Soft reset if set to 0xB6.
Definition: BMP180.c:36
@ E2PROM_AC3
Calibration coeff. AC3.
Definition: BMP180.c:26
static int16_t _BMP180_GetB5(int32_t s32UT)
Convert uncompensated temperature to B5.
Definition: BMP180.c:173
uint16_t u16AC4
Calibration coeff. AC4.
Definition: BMP180.c:53
int16_t s16MB
Calibration coeff. MB.
Definition: BMP180.c:58
static BMP180_Data _stBMP180Data
BMP180 driver private data.
Definition: BMP180.c:79
BMP180 driver data.
Definition: BMP180.c:68
@ OSS_TEMPERATURE
Temperature, max conversion time 4.5ms.
Definition: BMP180.h:24
@ E2PROM_MD
Calibration coeff. MD.
Definition: BMP180.c:34
void MCAL_Sleep(uint16_t u16DelayInUs)
Microsecond delay (blocking)
Definition: MCAL.c:207
int I2C_Receive(uint16_t u16DevAddress, uint16_t u16MemAddress, I2CMemAddSize eMemAddSize, uint8_t *pu8RxBuffer, uint16_t u16Size)
Receive an amount via I²C.
Definition: MCAL.c:105
@ E2PROM_AC1
Calibration coeff. AC1.
Definition: BMP180.c:24
int16_t s16MC
Calibration coeff. MC.
Definition: BMP180.c:59
@ E2PROM_AC4
Calibration coeff. AC4.
Definition: BMP180.c:27
@ E2PROM_MC
Calibration coeff. MC.
Definition: BMP180.c:33
Microcontroller Abstraction Layer.
@ OUT_MSB
ADC out LSB.
Definition: BMP180.c:38
@ E2PROM_AC2
Calibration coeff. AC2.
Definition: BMP180.c:25
int16_t s16B1
Calibration coeff. B1.
Definition: BMP180.c:56
int16_t s16B2
Calibration coeff. B2.
Definition: BMP180.c:57
uint8_t u8ChipID
Chip-ID.
Definition: BMP180.c:70
@ E2PROM_B1
Calibration coeff. B1.
Definition: BMP180.c:30
BMP180_Register
BMP180 register (global memory map)
Definition: BMP180.c:22
int16_t s16AC3
Calibration coeff. AC3.
Definition: BMP180.c:52
@ CHIP_ID
Chip-ID, can be checked against BMP180_CHIP_ID.
Definition: BMP180.c:35
uint16_t u16AC6
Calibration coeff. AC6.
Definition: BMP180.c:55
int16_t s16MD
Calibration coeff. MD.
Definition: BMP180.c:60
int I2C_Transmit(uint16_t u16DevAddress, uint16_t u16MemAddress, I2CMemAddSize eMemAddSize, uint8_t *pu8TxBuffer, uint16_t u16Size)
Transmit an amount via I²C.
Definition: MCAL.c:149
@ E2PROM_B2
Calibration coeff. BS.
Definition: BMP180.c:31
@ I2C_MEMSIZE_8BIT
I²C memory address size (8-Bit)
Definition: MCAL.h:32
int BMP180_Init(void)
Initialise BMP180 driver.
Definition: BMP180.c:90
BMP180 Calibration coefficients.
Definition: BMP180.c:48
int16_t s16AC1
Calibration coeff. AC1.
Definition: BMP180.c:50