Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / barometer / barometer_msp.c
blob5c5db4ca3f521908b1e0a6020f56a61b1d2234e4
1 /*
2 * This file is part of INAV Project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
25 #include <stdbool.h>
26 #include <stdint.h>
28 #include "platform.h"
30 #if defined(USE_BARO_MSP)
32 #include "build/build_config.h"
33 #include "build/debug.h"
35 #include "common/utils.h"
36 #include "common/time.h"
38 #include "drivers/time.h"
39 #include "drivers/barometer/barometer.h"
40 #include "drivers/barometer/barometer_msp.h"
42 #include "sensors/sensors.h"
43 #include "sensors/barometer.h"
44 #include "fc/runtime_config.h"
46 #include "msp/msp_protocol_v2_sensor_msg.h"
48 #define MSP_BARO_TIMEOUT_MS 250 // Less than 4Hz updates is considered a failure
50 static int32_t mspBaroPressure;
51 static int32_t mspBaroTemperature;
52 static timeMs_t mspBaroLastUpdateMs;
54 static bool mspBaroStarted = false;
56 static bool mspBaroStartGet(baroDev_t * baro)
58 UNUSED(baro);
59 return true;
62 static bool mspBaroCalculate(baroDev_t * baro, int32_t *pressure, int32_t *temperature)
64 UNUSED(baro);
66 if (((millis() - mspBaroLastUpdateMs) > MSP_BARO_TIMEOUT_MS)) {
67 sensorsClear(SENSOR_BARO);
68 mspBaroStarted = false;
69 return false;
72 if (pressure)
73 *pressure = mspBaroPressure;
75 if (temperature)
76 *temperature = mspBaroTemperature;
78 return true;
81 void mspBaroReceiveNewData(uint8_t * bufferPtr)
83 const mspSensorBaroDataMessage_t * pkt = (const mspSensorBaroDataMessage_t *)bufferPtr;
85 mspBaroPressure = pkt->pressurePa;
86 mspBaroTemperature = pkt->temp;
87 mspBaroLastUpdateMs = millis();
89 // This should only happen after a reset (!ARMING_FLAG(WAS_EVER_ARMED)) to avoid
90 // getting calibrations mid-air or on a surface that is above the home position
91 if (mspBaroStarted == false && !ARMING_FLAG(WAS_EVER_ARMED)){
92 baroStartCalibration();
93 mspBaroStarted = true;
94 sensorsSet(SENSOR_BARO);
98 bool mspBaroDetect(baroDev_t *baro)
100 mspBaroPressure = 101325; // pressure in Pa (0m MSL)
101 mspBaroTemperature = 2500; // temperature in 0.01 C = 25 deg
102 mspBaroLastUpdateMs = 0;
104 // these are dummy as temperature is measured as part of pressure
105 baro->ut_delay = 10000;
106 baro->get_ut = mspBaroStartGet;
107 baro->start_ut = mspBaroStartGet;
109 // only _up part is executed, and gets both temperature and pressure
110 baro->up_delay = 10000;
111 baro->start_up = mspBaroStartGet;
112 baro->get_up = mspBaroStartGet;
114 baro->calculate = mspBaroCalculate;
116 return true;
119 #endif