Merge pull request #11190 from mathiasvr/pr-arraylen
[betaflight.git] / src / test / unit / baro_bmp388_unittest.cc
blob56b532f4678e8e1e0158a4d965c8cbcb1d8f4c5f
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 bmp388Calculate(int32_t *pressure, int32_t *temperature);
28 extern uint32_t bmp388_up;
29 extern uint32_t bmp388_ut;
30 extern int64_t t_lin; // when temperature is calculated this is updated, the result is used in the pressure calculations
32 typedef struct bmp388_calib_param_s {
33 uint16_t T1;
34 uint16_t T2;
35 int8_t T3;
36 int16_t P1;
37 int16_t P2;
38 int8_t P3;
39 int8_t P4;
40 uint16_t P5;
41 uint16_t P6;
42 int8_t P7;
43 int8_t P8;
44 int16_t P9;
45 int8_t P10;
46 int8_t P11;
47 } __attribute__((packed)) bmp388_calib_param_t; // packed as we read directly from the device into this structure.
49 bmp388_calib_param_t bmp388_cal;
51 } // extern "C"
54 #include "unittest_macros.h"
55 #include "gtest/gtest.h"
57 void baroBmp388ConfigureZeroCalibration(void)
59 bmp388_cal = {
60 .T1 = 0,
61 .T2 = 0,
62 .T3 = 0,
63 .P1 = 0,
64 .P2 = 0,
65 .P3 = 0,
66 .P4 = 0,
67 .P5 = 0,
68 .P6 = 0,
69 .P7 = 0,
70 .P8 = 0,
71 .P9 = 0,
72 .P10 = 0,
73 .P11 = 0
77 void baroBmp388ConfigureSampleCalibration(void)
79 bmp388_cal = {
80 .T1 = 27772,
81 .T2 = 18638,
82 .T3 = -10,
83 .P1 = 878,
84 .P2 = -2023,
85 .P3 = 35,
86 .P4 = 0,
87 .P5 = 24476,
88 .P6 = 30501,
89 .P7 = -13,
90 .P8 = -10,
91 .P9 = 16545,
92 .P10 = 21,
93 .P11 = -60
97 TEST(baroBmp388Test, TestBmp388CalculateWithZeroCalibration)
99 // given
100 int32_t pressure, temperature;
101 bmp388_up = 0; // uncompensated pressure value
102 bmp388_ut = 0; // uncompensated temperature value
103 t_lin = 0;
105 // and
106 baroBmp388ConfigureZeroCalibration();
108 // when
109 bmp388Calculate(&pressure, &temperature);
111 // then
112 EXPECT_EQ(0, temperature);
113 EXPECT_EQ(0, t_lin);
115 // and
116 EXPECT_EQ(0, pressure);
119 TEST(baroBmp388Test, TestBmp388CalculateWithSampleCalibration)
121 // given
122 int32_t pressure, temperature;
123 bmp388_up = 7323488; // uncompensated pressure value
124 bmp388_ut = 9937920; // uncompensated temperature value
125 t_lin = 0;
127 // and
128 baroBmp388ConfigureSampleCalibration();
130 // when
131 bmp388Calculate(&pressure, &temperature);
133 // then
134 EXPECT_EQ(4880, temperature); // 48.80 degrees C
135 EXPECT_NE(0, t_lin);
137 // and
138 EXPECT_EQ(39535, pressure); // 39535 Pa
141 // STUBS
143 extern "C" {
145 void delay() {}
147 bool busBusy(const extDevice_t*, bool*) {return false;}
148 bool busReadRegisterBuffer(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
149 bool busReadRegisterBufferStart(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
150 bool busWriteRegister(const extDevice_t*, uint8_t, uint8_t) {return true;}
151 bool busWriteRegisterStart(const extDevice_t*, uint8_t, uint8_t) {return true;}
152 void busDeviceRegister(const extDevice_t*) {}
154 uint16_t spiCalculateDivider()
156 return 2;
159 void spiSetClkDivisor()
163 void spiPreinitByIO(IO_t)
167 void IOConfigGPIO()
171 void IOHi()
175 IO_t IOGetByTag(ioTag_t)
177 return IO_NONE;
180 void IOInit()
184 void EXTIHandlerInit(extiCallbackRec_t *, extiHandlerCallback *)
188 void EXTIConfig(IO_t, extiCallbackRec_t *, int, ioConfig_t, extiTrigger_t)
192 void EXTIEnable(IO_t)
195 void EXTIDisable(IO_t)
199 } // extern "C"