Updated and Validated
[betaflight.git] / src / test / unit / baro_bmp280_unittest.cc
blob2e64a3a18faf69fd9b959aead235d69576b332f6
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
17 #include <stdint.h>
19 extern "C" {
21 #include "platform.h"
22 #include "target.h"
23 #include "drivers/barometer/barometer.h"
24 #include "drivers/bus.h"
26 void bmp280Calculate(int32_t *pressure, int32_t *temperature);
28 extern uint32_t bmp280_up;
29 extern uint32_t bmp280_ut;
30 extern int32_t t_fine; /* calibration t_fine data */
32 typedef struct bmp280_calib_param_s {
33 uint16_t dig_T1; /* calibration T1 data */
34 int16_t dig_T2; /* calibration T2 data */
35 int16_t dig_T3; /* calibration T3 data */
36 uint16_t dig_P1; /* calibration P1 data */
37 int16_t dig_P2; /* calibration P2 data */
38 int16_t dig_P3; /* calibration P3 data */
39 int16_t dig_P4; /* calibration P4 data */
40 int16_t dig_P5; /* calibration P5 data */
41 int16_t dig_P6; /* calibration P6 data */
42 int16_t dig_P7; /* calibration P7 data */
43 int16_t dig_P8; /* calibration P8 data */
44 int16_t dig_P9; /* calibration P9 data */
45 } __attribute__((packed)) bmp280_calib_param_t; // packed as we read directly from the device into this structure.
47 bmp280_calib_param_t bmp280_cal;
51 #include "unittest_macros.h"
52 #include "gtest/gtest.h"
55 TEST(baroBmp280Test, TestBmp280Calculate)
57 // given
58 int32_t pressure, temperature;
59 bmp280_up = 415148; // Digital pressure value
60 bmp280_ut = 519888; // Digital temperature value
61 t_fine = 0;
63 // and
64 bmp280_cal.dig_T1 = 27504;
65 bmp280_cal.dig_T2 = 26435;
66 bmp280_cal.dig_T3 = -1000;
67 bmp280_cal.dig_P1 = 36477;
68 bmp280_cal.dig_P2 = -10685;
69 bmp280_cal.dig_P3 = 3024;
70 bmp280_cal.dig_P4 = 2855;
71 bmp280_cal.dig_P5 = 140;
72 bmp280_cal.dig_P6 = -7;
73 bmp280_cal.dig_P7 = 15500;
74 bmp280_cal.dig_P8 = -14600;
75 bmp280_cal.dig_P9 = 6000;
77 // when
78 bmp280Calculate(&pressure, &temperature);
80 // then
81 EXPECT_EQ(100653, pressure); // 100653 Pa
82 EXPECT_EQ(2508, temperature); // 25.08 degC
85 TEST(baroBmp280Test, TestBmp280CalculateHighP)
87 // given
88 int32_t pressure, temperature;
89 bmp280_up = 215148; // Digital pressure value
90 bmp280_ut = 519888; // Digital temperature value
91 t_fine = 0;
93 // and
94 bmp280_cal.dig_T1 = 27504;
95 bmp280_cal.dig_T2 = 26435;
96 bmp280_cal.dig_T3 = -1000;
97 bmp280_cal.dig_P1 = 36477;
98 bmp280_cal.dig_P2 = -10685;
99 bmp280_cal.dig_P3 = 3024;
100 bmp280_cal.dig_P4 = 2855;
101 bmp280_cal.dig_P5 = 140;
102 bmp280_cal.dig_P6 = -7;
103 bmp280_cal.dig_P7 = 15500;
104 bmp280_cal.dig_P8 = -14600;
105 bmp280_cal.dig_P9 = 6000;
107 // when
108 bmp280Calculate(&pressure, &temperature);
110 // then
111 EXPECT_EQ(135382, pressure); // 135385 Pa
112 EXPECT_EQ(2508, temperature); // 25.08 degC
115 TEST(baroBmp280Test, TestBmp280CalculateZeroP)
117 // given
118 int32_t pressure, temperature;
119 bmp280_up = 415148; // Digital pressure value
120 bmp280_ut = 519888; // Digital temperature value
121 t_fine = 0;
123 // and
124 bmp280_cal.dig_T1 = 27504;
125 bmp280_cal.dig_T2 = 26435;
126 bmp280_cal.dig_T3 = -1000;
127 bmp280_cal.dig_P1 = 0;
128 bmp280_cal.dig_P2 = -10685;
129 bmp280_cal.dig_P3 = 3024;
130 bmp280_cal.dig_P4 = 2855;
131 bmp280_cal.dig_P5 = 140;
132 bmp280_cal.dig_P6 = -7;
133 bmp280_cal.dig_P7 = 15500;
134 bmp280_cal.dig_P8 = -14600;
135 bmp280_cal.dig_P9 = 6000;
137 // when
138 bmp280Calculate(&pressure, &temperature);
140 // then
141 EXPECT_EQ(0, pressure); // P1=0 trips pressure to 0 Pa, avoiding division by zero
142 EXPECT_EQ(2508, temperature); // 25.08 degC
145 // STUBS
147 extern "C" {
149 void delay(uint32_t) {}
150 bool busBusy(const extDevice_t*, bool*) {return false;}
151 bool busReadRegisterBuffer(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
152 bool busReadRegisterBufferStart(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
153 bool busWriteRegister(const extDevice_t*, uint8_t, uint8_t) {return true;}
154 bool busWriteRegisterStart(const extDevice_t*, uint8_t, uint8_t) {return true;}
155 void busDeviceRegister(const extDevice_t*) {}
157 uint16_t spiCalculateDivider() {
158 return 2;
161 void spiSetClkDivisor() {
164 void spiPreinitByIO() {
167 void IOConfigGPIO() {
170 void IOHi() {
173 void IOInit() {
176 void IORelease() {