Updated and Validated
[betaflight.git] / src / test / unit / baro_ms5611_unittest.cc
blob331bd6095c1cb860a86ff57fd763aa8cfd290de7
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 int8_t ms5611CRC(uint16_t *prom);
27 void ms5611Calculate(int32_t *pressure, int32_t *temperature);
29 extern uint16_t ms5611_c[8];
30 extern uint32_t ms5611_up;
31 extern uint32_t ms5611_ut;
36 #include "unittest_macros.h"
37 #include "gtest/gtest.h"
40 TEST(baroMS5611Test, TestValidMs5611Crc)
42 // given
43 uint16_t ms5611Prom[] = {0x3132,0x3334,0x3536,0x3738,0x3940,0x4142,0x4344,0x450B};
45 // when
46 int8_t result = ms5611CRC(ms5611Prom);
48 // then
49 EXPECT_EQ(0, result);
52 TEST(baroMS5611Test, TestInvalidMs5611Crc)
54 // given
55 uint16_t ms5611Prom[] = {0x3132,0x3334,0x3536,0x3738,0x3940,0x4142,0x4344,0x4500};
57 // when
58 int8_t result = ms5611CRC(ms5611Prom);
60 // then
61 EXPECT_EQ(-1, result);
64 TEST(baroMS5611Test, TestMs5611AllZeroProm)
66 // given
67 uint16_t ms5611Prom[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
69 // when
70 int8_t result = ms5611CRC(ms5611Prom);
72 // then
73 EXPECT_EQ(-1, result);
76 TEST(baroMS5611Test, TestMs5611AllOnesProm)
78 // given
79 uint16_t ms5611Prom[] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF};
81 // when
82 int8_t result = ms5611CRC(ms5611Prom);
84 // then
85 EXPECT_EQ(-1, result);
88 TEST(baroMS5611Test, TestMs5611CalculatePressureGT20Deg)
90 // given
91 int32_t pressure, temperature;
92 uint16_t ms5611_c_test[] = {0x0000, 40127, 36924, 23317, 23282, 33464, 28312, 0x0000}; // calibration data from MS5611 datasheet
93 memcpy(&ms5611_c, &ms5611_c_test, sizeof(ms5611_c_test));
95 ms5611_up = 9085466; // Digital pressure value from MS5611 datasheet
96 ms5611_ut = 8569150; // Digital temperature value from MS5611 datasheet
98 // when
99 ms5611Calculate(&pressure, &temperature);
101 // then
102 EXPECT_EQ(2007, temperature); // 20.07 deg C
103 EXPECT_EQ(100009, pressure); // 1000.09 mbar
106 TEST(baroMS5611Test, TestMs5611CalculatePressureLT20Deg)
108 // given
109 int32_t pressure, temperature;
110 uint16_t ms5611_c_test[] = {0x0000, 40127, 36924, 23317, 23282, 33464, 28312, 0x0000}; // calibration data from MS5611 datasheet
111 memcpy(&ms5611_c, &ms5611_c_test, sizeof(ms5611_c_test));
113 ms5611_up = 9085466; // Digital pressure value from MS5611 datasheet
114 ms5611_ut = 8069150; // Digital temperature value
116 // when
117 ms5611Calculate(&pressure, &temperature);
119 // then
120 EXPECT_EQ(205, temperature); // 2.05 deg C
121 EXPECT_EQ(96512, pressure); // 965.12 mbar
124 TEST(baroMS5611Test, TestMs5611CalculatePressureLTMinus15Deg)
126 // given
127 int32_t pressure, temperature;
128 uint16_t ms5611_c_test[] = {0x0000, 40127, 36924, 23317, 23282, 33464, 28312, 0x0000}; // calibration data from MS5611 datasheet
129 memcpy(&ms5611_c, &ms5611_c_test, sizeof(ms5611_c_test));
131 ms5611_up = 9085466; // Digital pressure value from MS5611 datasheet
132 ms5611_ut = 7369150; // Digital temperature value
134 // when
135 ms5611Calculate(&pressure, &temperature);
137 // then
138 EXPECT_EQ(-2710, temperature); // -27.10 deg C
139 EXPECT_EQ(90613, pressure); // 906.13 mbar
142 // STUBS
144 extern "C" {
146 void delay(uint32_t) {}
147 void delayMicroseconds(uint32_t) {}
149 bool busBusy(const extDevice_t*, bool*) {return false;}
150 bool busRawReadRegisterBuffer(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
151 bool busRawReadRegisterBufferStart(const extDevice_t*, uint8_t, uint8_t*, uint8_t) {return true;}
152 bool busRawWriteRegister(const extDevice_t*, uint8_t, uint8_t) {return true;}
153 bool busRawWriteRegisterStart(const extDevice_t*, uint8_t, uint8_t) {return true;}
154 void busDeviceRegister(const extDevice_t*) {}
156 uint16_t spiCalculateDivider() {
157 return 2;
160 void spiSetClkDivisor() {
163 void spiPreinitByIO() {
166 void IOConfigGPIO() {
169 void IOHi() {
172 void IOInit() {
175 void IORelease() {