EFR32 Blue Gecko 1 Software Documentation  efr32bg1-doc-5.1.2
bmp.c
1 /***************************************************************************/
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 
20 #include "thunderboard/bmp.h"
21 #include "thunderboard/board.h"
22 #include "thunderboard/util.h"
23 #include "i2cspm.h"
24 
26 
27 /***************************************************************************/
35 /****************************************************************************/
36 /* Local Function Prototypes */
37 /****************************************************************************/
38 static int8_t i2cBusRead ( uint8_t devAddr, uint8_t regAddr, uint8_t *regData, uint8_t count );
39 static int8_t i2cBusWrite ( uint8_t devAddr, uint8_t regAddr, uint8_t *regData, uint8_t count );
40 static uint8_t readRegister ( uint8_t addr );
41 
42 
43 /***************************************************************************/
46 static uint8_t bmpDeviceId; /* The device ID of the connected chip */
47 static uint8_t bmp280PowerMode; /* The actual power mode of the BMP280 */
48 static struct bmp280_t bmp280; /* Structure to hold BMP280 driver data */
49 
52 /***************************************************************************/
58 /***************************************************************************/
68 uint32_t BMP_init( uint8_t *deviceId )
69 {
70 
71  int result;
72 
73  /* Enable power to the enviromental sensor group */
74  BOARD_envSensEnable( true );
75 
76  /* The device needs 2 ms startup time */
77  UTIL_delay( 2 );
78 
79  /* Read device ID to determine if we have a BMP280 connected */
80  bmpDeviceId = readRegister( BMP_REG_ADDR_ID );
81 
82  if( bmpDeviceId != BMP_DEVICE_ID_BMP280 ) {
84  }
85 
86  bmp280.bus_write = i2cBusWrite;
87  bmp280.bus_read = i2cBusRead;
89  bmp280.delay_msec = UTIL_delay;
90 
91  result = bmp280_init( &bmp280 );
92 
93  if( result != BMP_OK ) {
94  return result;
95  }
96 
97  result = bmp280_set_power_mode( BMP280_FORCED_MODE );
98 
99  if( result != BMP_OK ) {
100  return result;
101  }
102 
103  result = bmp280_set_work_mode( BMP280_ULTRA_HIGH_RESOLUTION_MODE );
104 
105  if( result != BMP_OK ) {
106  return result;
107  }
108 
109  bmp280PowerMode = BMP280_FORCED_MODE;
110 
111  *deviceId = bmpDeviceId;
112 
113  return BMP_OK;
114 
115 }
116 
117 /***************************************************************************/
124 void BMP_deInit( void )
125 {
126  bmp280_set_power_mode( BMP280_SLEEP_MODE );
127  return;
128 }
129 
130 /***************************************************************************/
140 uint32_t BMP_config( BMP_Config *cfg )
141 {
142 
143  uint32_t result;
144 
145  result = 0;
146 
147  result += bmp280_set_work_mode( cfg->oversampling );
148  result += bmp280_set_power_mode( cfg->powerMode );
149  bmp280PowerMode = cfg->powerMode;
150  result += bmp280_set_standby_durn( cfg->standbyTime );
151 
152  return result;
153 
154 }
155 
156 /***************************************************************************/
167 uint32_t BMP_getTemperature( float *temperature )
168 {
169 
170  int8_t result;
171  int32_t uncompTemp;
172  int32_t uncompPressure;
173  int32_t compTemp;
174 
175  if( bmp280PowerMode == BMP280_NORMAL_MODE ) {
176  result = bmp280_read_uncomp_temperature( &uncompTemp );
177  }
178  else {
179  result = bmp280_get_forced_uncomp_pressure_temperature( &uncompPressure, &uncompTemp );
180  }
181 
182  if( result != SUCCESS ) {
183  return (uint32_t) result;
184  }
185 
186  compTemp = bmp280_compensate_temperature_int32( uncompTemp );
187  *temperature = (float) compTemp;
188  *temperature /= 100.0f;
189 
190  return BMP_OK;
191 
192 }
193 
194 /***************************************************************************/
205 uint32_t BMP_getPressure( float *pressure )
206 {
207 
208  int8_t result;
209  int32_t uncompTemp;
210  int32_t uncompPressure;
211  uint32_t compPressure;
212 
213  if( bmp280PowerMode == BMP280_NORMAL_MODE ) {
214  result = bmp280_read_uncomp_pressure( &uncompPressure );
215  if( result == SUCCESS ){
216  result = bmp280_read_uncomp_temperature( &uncompTemp );
217  }
218  }
219  else {
220  result = bmp280_get_forced_uncomp_pressure_temperature( &uncompPressure, &uncompTemp );
221  }
222 
223  if( result != SUCCESS ) {
224  return (uint32_t) result;
225  }
226 
228  compPressure = bmp280_compensate_pressure_int64( uncompPressure );
229 
230  *pressure = (float) compPressure;
231  *pressure /= 256.0f;
232  *pressure /= 100.0f;
233 
234  return BMP_OK;
235 
236 }
237 
240 /***************************************************************************/
251 static uint8_t readRegister( uint8_t addr )
252 {
253 
256  uint8_t reg;
257 
258  seq.addr = BMP_I2C_BUS_ADDRESS << 1;
260 
261  seq.buf[0].len = 1;
262  seq.buf[0].data = &addr;
263  seq.buf[1].len = 1;
264  seq.buf[1].data = &reg;
265 
266  ret = I2CSPM_Transfer( BMP_I2C_DEVICE, &seq );
267 
268  if( ret != i2cTransferDone ) {
269  return 0;
270  }
271 
272  return reg;
273 
274 }
275 
276 /***************************************************************************/
295 static int8_t i2cBusWrite( uint8_t devAddr, uint8_t regAddr, uint8_t *regData, uint8_t count )
296 {
297 
300 
301  seq.addr = devAddr << 1;
303 
304  seq.buf[0].len = 1;
305  seq.buf[0].data = &regAddr;
306  seq.buf[1].len = count;
307  seq.buf[1].data = regData;
308 
309  ret = I2CSPM_Transfer( BMP_I2C_DEVICE, &seq );
310 
311  if( ret != i2cTransferDone ) {
313  }
314 
315  return BMP_OK;
316 
317 }
318 
319 /***************************************************************************/
338 static int8_t i2cBusRead( uint8_t devAddr, uint8_t regAddr, uint8_t *regData, uint8_t count )
339 {
340 
343 
344  seq.addr = devAddr << 1;
346 
347  seq.buf[0].len = 1;
348  seq.buf[0].data = &regAddr;
349  seq.buf[1].len = count;
350  seq.buf[1].data = regData;
351 
352  ret = I2CSPM_Transfer( BMP_I2C_DEVICE, &seq );
353 
354  if( ret != i2cTransferDone ) {
356  }
357 
358  return BMP_OK;
359 
360 }
361 
BMP280_RETURN_FUNCTION_TYPE bmp280_init(struct bmp280_t *bmp280)
This function is used for initialize the bus read and bus write functions and assign the chip id and ...
Definition: bmp280.c:85
uint8_t powerMode
Definition: bmp.h:74
u8 dev_addr
Definition: bmp280.h:771
uint8_t oversampling
Definition: bmp.h:73
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
Definition: i2cspm.c:124
void BMP_deInit(void)
De-initializes the barometric pressure module.
Definition: bmp.c:124
uint32_t BMP_getPressure(float *pressure)
Initiates pressure measurement on the barometric pressure sensor and reads pressure from it...
Definition: bmp.c:205
uint32_t BMP_getTemperature(float *temperature)
Initiates temperature measurement on the barometric pressure sensor and reads pressure from it...
Definition: bmp.c:167
BMP280_RETURN_FUNCTION_TYPE bmp280_set_power_mode(u8 v_power_mode_u8)
This API used to set the Operational Mode from the sensor in the register 0xF4 bit 0 and 1...
Definition: bmp280.c:809
void UTIL_delay(uint32_t ms)
Delays number of msTick Systicks (1 ms)
Definition: util.c:97
u32 bmp280_compensate_pressure_int64(s32 v_uncomp_pressure_s32)
This API used to read actual pressure from uncompensated pressure.
Definition: bmp280.c:1432
BMP280 Sensor Driver Support Header File.
BMP280_RETURN_FUNCTION_TYPE bmp280_read_uncomp_temperature(s32 *v_uncomp_temperature_s32)
This API is used to read uncompensated temperature in the registers 0xFA, 0xFB and 0xFC...
Definition: bmp280.c:139
void(* delay_msec)(BMP280_MDELAY_DATA_TYPE)
Definition: bmp280.h:778
I2C_TransferReturn_TypeDef
Definition: em_i2c.h:179
BMP280_RETURN_FUNCTION_TYPE bmp280_read_uncomp_pressure(s32 *v_uncomp_pressure_s32)
This API is used to read uncompensated pressure. in the registers 0xF7, 0xF8 and 0xF9.
Definition: bmp280.c:235
uint32_t BOARD_envSensEnable(bool enable)
Enables or disables the environmental sensor group (Pressure, RH/Temp, UV/Ambient light and Hall sens...
Definition: board.c:492
Structure to configure the BMP280 device.
Definition: bmp.h:71
BMP280_RETURN_FUNCTION_TYPE bmp280_set_work_mode(u8 v_work_mode_u8)
This API is used to write the working mode of the sensor.
Definition: bmp280.c:1157
I2C simple poll-based master mode driver for the DK/STK.
#define BMP_ERROR_I2C_TRANSACTION_FAILED
Definition: bmp.h:56
Utility Functions for the Thunderboard Sense.
struct I2C_TransferSeq_TypeDef::@0 buf[2]
#define BMP_DEVICE_ID_BMP280
Definition: bmp.h:44
#define BMP_I2C_DEVICE
Definition: bmp.h:41
uint8_t standbyTime
Definition: bmp.h:75
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
Definition: em_i2c.h:252
s32 bmp280_compensate_temperature_int32(s32 v_uncomp_temperature_s32)
Reads actual temperature from uncompensated temperature.
Definition: bmp280.c:185
BMP280_RETURN_FUNCTION_TYPE bmp280_get_forced_uncomp_pressure_temperature(s32 *v_uncomp_pressure_s32, s32 *v_uncomp_temperature_s32)
This API used to read both uncompensated pressure and temperature in forced mode. ...
Definition: bmp280.c:1236
#define I2C_FLAG_WRITE_WRITE
Indicate write sequence using two buffers: S+ADDR(W)+DATA0+DATA1+P.
Definition: em_i2c.h:159
EMSTATUS BMP_init(uint8_t *palette, uint32_t paletteSize, EMSTATUS(*fp)(uint8_t buffer[], uint32_t bufLength, uint32_t bytesToRead))
Initializes BMP Module.
Definition: bmp.c:99
#define BMP_REG_ADDR_ID
Definition: bmp_regs.h:44
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
Definition: em_i2c.h:148
uint16_t addr
Address to use after (repeated) start.
Definition: em_i2c.h:262
BMP280_RETURN_FUNCTION_TYPE bmp280_set_standby_durn(u8 v_standby_durn_u8)
This API used to Read the standby duration time from the sensor in the register 0xF5 bit 5 to 7...
Definition: bmp280.c:1111
BOARD module header file.
#define BMP_ERROR_DEVICE_ID_MISMATCH
Definition: bmp.h:57
#define BMP_I2C_BUS_ADDRESS
Definition: bmp.h:42
This structure holds BMP280 initialization parameters.
Definition: bmp280.h:767
#define BMP_OK
Definition: bmp.h:38
uint32_t BMP_config(BMP_Config *cfg)
Configure the barometric pressure sensor.
Definition: bmp.c:140