drv_bmp280.c
Go to the documentation of this file.
1 #include <stdint.h>
2 
3 #include <breezystm32.h>
4 
5 #include "drv_bmp280.h"
6 #include "drv_i2c.h"
7 
8 #define BMP280_DEFAULT_ADDR 0x76
9 
10 #define BMP280_DEFAULT_CHIP_ID 0x58
11 
12 #define BMP280_CHIP_ID_REG 0xD0 /* Chip ID Register */
13 #define BMP280_RST_REG 0xE0 /* Softreset Register */
14 #define BMP280_STAT_REG 0xF3 /* Status Register */
15 #define BMP280_CTRL_MEAS_REG 0xF4 /* Ctrl Measure Register */
16 #define BMP280_CONFIG_REG 0xF5 /* Configuration Register */
17 #define BMP280_PRESSURE_MSB_REG 0xF7 /* Pressure MSB Register */
18 #define BMP280_PRESSURE_LSB_REG 0xF8 /* Pressure LSB Register */
19 #define BMP280_PRESSURE_XLSB_REG 0xF9 /* Pressure XLSB Register */
20 #define BMP280_TEMPERATURE_MSB_REG 0xFA /* Temperature MSB Reg */
21 #define BMP280_TEMPERATURE_LSB_REG 0xFB /* Temperature LSB Reg */
22 #define BMP280_TEMPERATURE_XLSB_REG 0xFC /* Temperature XLSB Reg */
23 #define BMP280_FORCED_MODE 0x01
24 #define BMP280_NORMAL_MODE 0x03
25 
26 #define BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG 0x88
27 #define BMP280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH 24
28 #define BMP280_DATA_FRAME_SIZE 6
29 
30 #define BMP280_OVERSAMP_SKIPPED 0x00
31 #define BMP280_OVERSAMP_1X 0x01
32 #define BMP280_OVERSAMP_2X 0x02
33 #define BMP280_OVERSAMP_4X 0x03
34 #define BMP280_OVERSAMP_8X 0x04
35 #define BMP280_OVERSAMP_16X 0x05
36 
37 
38 typedef struct bmp280_calib_param_s {
39  uint16_t dig_T1; /* calibration T1 data */
40  int16_t dig_T2; /* calibration T2 data */
41  int16_t dig_T3; /* calibration T3 data */
42  uint16_t dig_P1; /* calibration P1 data */
43  int16_t dig_P2; /* calibration P2 data */
44  int16_t dig_P3; /* calibration P3 data */
45  int16_t dig_P4; /* calibration P4 data */
46  int16_t dig_P5; /* calibration P5 data */
47  int16_t dig_P6; /* calibration P6 data */
48  int16_t dig_P7; /* calibration P7 data */
49  int16_t dig_P8; /* calibration P8 data */
50  int16_t dig_P9; /* calibration P9 data */
51  int32_t t_fine; /* calibration t_fine data */
53 
55 
56 static bool sensor_present = false;
57 static uint32_t pressure_raw;
58 static uint32_t temperature_raw;
59 static float pressure;
60 static float temperature;
62 static bool new_data = false;
63 
65 {
66  return sensor_present;
67 }
68 
70 {
71  // wait for chip to power up
72  while(millis() < 20);
73 
74  uint8_t buf;
76  return false;
77  else
78  sensor_present = true;
79 
80  // read calibration
82 
83  // set oversampling + power mode (forced), and start sampling
85 
86  return true;
87 }
88 
89 // Returns temperature in DegC, resolution is 0.01 DegC. Output value of "5123" equals 51.23 DegC
90 // t_fine carries fine temperature as global value
91 static int32_t bmp280_compensate_T(int32_t adc_T)
92 {
93  int32_t var1, var2, T;
94 
95  var1 = ((((adc_T >> 3) - ((int32_t)bmp280_cal.dig_T1 << 1))) * ((int32_t)bmp280_cal.dig_T2)) >> 11;
96  var2 = (((((adc_T >> 4) - ((int32_t)bmp280_cal.dig_T1)) * ((adc_T >> 4) - ((int32_t)bmp280_cal.dig_T1))) >> 12) * ((int32_t)bmp280_cal.dig_T3)) >> 14;
97  bmp280_cal.t_fine = var1 + var2;
98  T = (bmp280_cal.t_fine * 5 + 128) >> 8;
99 
100  return T;
101 }
102 
103 // Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
104 // Output value of "24674867" represents 24674867/256 = 96386.2 Pa = 963.862 hPa
105 static uint32_t bmp280_compensate_P(int32_t adc_P)
106 {
107  int64_t var1, var2, p;
108  var1 = ((int64_t)bmp280_cal.t_fine) - 128000;
109  var2 = var1 * var1 * (int64_t)bmp280_cal.dig_P6;
110  var2 = var2 + ((var1*(int64_t)bmp280_cal.dig_P5) << 17);
111  var2 = var2 + (((int64_t)bmp280_cal.dig_P4) << 35);
112  var1 = ((var1 * var1 * (int64_t)bmp280_cal.dig_P3) >> 8) + ((var1 * (int64_t)bmp280_cal.dig_P2) << 12);
113  var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)bmp280_cal.dig_P1) >> 33;
114  if (var1 == 0)
115  return 0;
116  p = 1048576 - adc_P;
117  p = (((p << 31) - var2) * 3125) / var1;
118  var1 = (((int64_t)bmp280_cal.dig_P9) * (p >> 13) * (p >> 13)) >> 25;
119  var2 = (((int64_t)bmp280_cal.dig_P8) * p) >> 19;
120  p = ((p + var1 + var2) >> 8) + (((int64_t)bmp280_cal.dig_P7) << 4);
121  return (uint32_t)p;
122 }
123 
125 {
127  pressure = (float)(bmp280_compensate_P(pressure_raw))/256.0;
128  new_data = false;
129 }
130 
131 void bmp280_update(void)
132 {
133  // read data from sensor
135  pressure_raw = (int32_t)((((uint32_t)(buffer[0])) << 12) | (((uint32_t)(buffer[1])) << 4) | ((uint32_t)buffer[2] >> 4));
136  temperature_raw = (int32_t)((((uint32_t)(buffer[3])) << 12) | (((uint32_t)(buffer[4])) << 4) | ((uint32_t)buffer[5] >> 4));
137  new_data = true;
138 }
139 
140 void bmp280_read(float* pres, float* temp)
141 {
142  if (new_data)
143  {
145  }
146 
147  *pres = pressure;
148  *temp = temperature;
149 }
150 
152 static volatile uint8_t async_read_status;
153 static void bmp280_async_read_cb(uint8_t result)
154 {
155  if (result != I2C_JOB_ERROR)
156  {
157  new_data = true;
158  pressure_raw = (int32_t)((((uint32_t)(buffer[0])) << 12) | (((uint32_t)(buffer[1])) << 4) | ((uint32_t)buffer[2] >> 4));
159  temperature_raw = (int32_t)((((uint32_t)(buffer[3])) << 12) | (((uint32_t)(buffer[4])) << 4) | ((uint32_t)buffer[5] >> 4));
160  }
161 }
162 
164 {
165  static uint32_t next_update_ms = 0;
166  uint32_t now_ms = millis();
167  if (now_ms > next_update_ms)
168  {
172  buffer,
173  6,
176  next_update_ms += 20; // Read at 50 Hz
177  }
178 }
179 
180 void bmp280_async_read(float *pres, float *temp)
181 {
182  if (new_data)
183  {
185  }
186 
187  *pres = pressure;
188  *temp = temperature;
189 }
static uint32_t pressure_raw
Definition: drv_bmp280.c:57
bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t *buf)
Definition: drv_i2c.c:157
static uint32_t temperature_raw
Definition: drv_bmp280.c:58
#define BMP280_PRESSURE_MSB_REG
Definition: drv_bmp280.c:17
volatile uint32_t millis(void)
Definition: system.c:50
static bool new_data
Definition: drv_bmp280.c:62
static bool sensor_present
Definition: drv_bmp280.c:56
static float pressure
Definition: drv_bmp280.c:59
#define BMP280_CTRL_MEAS_REG
Definition: drv_bmp280.c:15
static uint8_t buffer[BMP280_DATA_FRAME_SIZE]
Definition: drv_bmp280.c:61
static float temperature
Definition: drv_bmp280.c:60
#define BMP280_NORMAL_MODE
Definition: drv_bmp280.c:24
#define BMP280_CHIP_ID_REG
Definition: drv_bmp280.c:12
bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data)
Definition: drv_i2c.c:152
static volatile int16_t temp
Definition: drv_mpu6050.c:278
#define BMP280_OVERSAMP_8X
Definition: drv_bmp280.c:34
void bmp280_async_read(float *pres, float *temp)
Definition: drv_bmp280.c:180
static volatile uint8_t async_read_status
ASYNC Methods.
Definition: drv_bmp280.c:152
#define BMP280_DATA_FRAME_SIZE
Definition: drv_bmp280.c:28
void i2c_queue_job(i2cJobType_t type, uint8_t addr_, uint8_t reg_, uint8_t *data, uint8_t length, volatile uint8_t *status_, void(*CB)(uint8_t))
Definition: drv_i2c.c:579
void bmp280_update(void)
Definition: drv_bmp280.c:131
static int32_t bmp280_compensate_T(int32_t adc_T)
Definition: drv_bmp280.c:91
bool bmp280_init()
Definition: drv_bmp280.c:69
static bmp280_calib_param_t bmp280_cal
Definition: drv_bmp280.c:54
static uint32_t bmp280_compensate_P(int32_t adc_P)
Definition: drv_bmp280.c:105
#define BMP280_OVERSAMP_1X
Definition: drv_bmp280.c:31
#define BMP280_TEMPERATURE_CALIB_DIG_T1_LSB_REG
Definition: drv_bmp280.c:26
bool bmp280_present()
Definition: drv_bmp280.c:64
Definition: drv_i2c.h:31
void bmp280_async_update()
Definition: drv_bmp280.c:163
struct bmp280_calib_param_s bmp280_calib_param_t
void bmp280_calculate(void)
Definition: drv_bmp280.c:124
#define BMP280_DEFAULT_ADDR
Definition: drv_bmp280.c:8
static void bmp280_async_read_cb(uint8_t result)
Definition: drv_bmp280.c:153
void bmp280_read(float *pres, float *temp)
Definition: drv_bmp280.c:140


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:46