Skip to content

Commit 8df5ac2

Browse files
Merge pull request #11379 from sensei-hacker/feature-crsf-sensor-input
Add CRSF sensor input on dedicated UART
2 parents 75c33db + 287b06f commit 8df5ac2

24 files changed

Lines changed: 796 additions & 9 deletions

docs/Settings.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ Selection of baro hardware. See Wiki Sensor auto detect and hardware failure det
514514
| B2SMPB | |
515515
| MSP | |
516516
| FAKE | |
517+
| CRSF | |
517518

518519
---
519520

@@ -695,6 +696,7 @@ ADC, VIRTUAL, FAKE, ESC, SMARTPORT, CAN, NONE. The virtual current sensor, once
695696
| FAKE | |
696697
| ESC | |
697698
| SMARTPORT | |
699+
| CRSF | |
698700
| CAN | |
699701

700702
---
@@ -1863,6 +1865,7 @@ Which GPS protocol to be used.
18631865
| --- | --- |
18641866
| UBLOX | Default |
18651867
| MSP | |
1868+
| CRSF | |
18661869
| FAKE | |
18671870
| DRONECAN | |
18681871

@@ -6937,6 +6940,7 @@ Vbat voltage source. Possible values: `NONE`, `ADC`, `SMARTPORT`, `ESC`, 'CAN'.
69376940
| ESC | |
69386941
| FAKE | |
69396942
| SMARTPORT | |
6943+
| CRSF | |
69406944
| CAN | |
69416945

69426946
---

src/main/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ main_sources(COMMON_SRC
122122
drivers/barometer/barometer_spl06.h
123123
drivers/barometer/barometer_msp.c
124124
drivers/barometer/barometer_msp.h
125+
drivers/barometer/barometer_crsf.c
126+
drivers/barometer/barometer_crsf.h
125127
drivers/barometer/barometer_2smpb_02b.c
126128
drivers/barometer/barometer_2smpb_02b.h
127129

@@ -543,6 +545,7 @@ main_sources(COMMON_SRC
543545
io/gps_ublox.c
544546
io/gps_ublox_utils.c
545547
io/gps_msp.c
548+
io/gps_crsf.c
546549
io/gps_dronecan.c
547550
io/gps_fake.c
548551
io/gps_private.h
@@ -564,6 +567,8 @@ main_sources(COMMON_SRC
564567
io/osd_joystick.h
565568
io/smartport_master.c
566569
io/smartport_master.h
570+
io/crsf_sensor.c
571+
io/crsf_sensor.h
567572
io/vtx.c
568573
io/vtx.h
569574
io/vtx_string.c
@@ -607,6 +612,8 @@ main_sources(COMMON_SRC
607612
sensors/opflow.h
608613
sensors/battery_sensor_fake.c
609614
sensors/battery_sensor_fake.h
615+
sensors/battery_sensor_crsf.c
616+
sensors/battery_sensor_crsf.h
610617

611618
telemetry/crsf.c
612619
telemetry/crsf.h

src/main/build/atomic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ static inline uint8_t __basepriSetMemRetVal(uint8_t prio)
4444

4545
// Run block with elevated BASEPRI (using BASEPRI_MAX), restoring BASEPRI on exit. All exit paths are handled
4646
// Full memory barrier is placed at start and exit of block
47-
#ifdef UNIT_TEST
47+
#if defined(UNIT_TEST) || defined(SITL_BUILD)
4848
#define ATOMIC_BLOCK(prio) {}
4949
#else
5050
#define ATOMIC_BLOCK(prio) for ( uint8_t __basepri_save __attribute__((__cleanup__(__basepriRestoreMem))) = __get_BASEPRI(), \
5151
__ToDo = __basepriSetMemRetVal((prio) << (8U - __NVIC_PRIO_BITS)); __ToDo ; __ToDo = 0 )
5252

53-
#endif // UNIT_TEST
53+
#endif // UNIT_TEST || SITL_BUILD
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
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/.
7+
*
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:
10+
*
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.
15+
*
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.
20+
*
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/.
23+
*/
24+
25+
#include <stdbool.h>
26+
#include <stdint.h>
27+
28+
#include "platform.h"
29+
30+
#if defined(USE_BARO_CRSF)
31+
32+
#include "build/build_config.h"
33+
34+
#include "common/utils.h"
35+
#include "common/time.h"
36+
37+
#include "drivers/time.h"
38+
#include "drivers/barometer/barometer.h"
39+
#include "drivers/barometer/barometer_crsf.h"
40+
41+
#include "sensors/sensors.h"
42+
#include "sensors/barometer.h"
43+
#include "fc/runtime_config.h"
44+
45+
#define CRSF_BARO_TIMEOUT_MS 250
46+
47+
static int32_t crsfBaroPressure;
48+
static int32_t crsfBaroTemperature;
49+
static timeMs_t crsfBaroLastUpdateMs;
50+
static bool crsfBaroStarted = false;
51+
52+
static bool crsfBaroStartGet(baroDev_t *baro)
53+
{
54+
UNUSED(baro);
55+
return true;
56+
}
57+
58+
static bool crsfBaroCalculate(baroDev_t *baro, int32_t *pressure, int32_t *temperature)
59+
{
60+
UNUSED(baro);
61+
62+
if ((millis() - crsfBaroLastUpdateMs) > CRSF_BARO_TIMEOUT_MS) {
63+
sensorsClear(SENSOR_BARO);
64+
crsfBaroStarted = false;
65+
return false;
66+
}
67+
68+
if (pressure)
69+
*pressure = crsfBaroPressure;
70+
71+
if (temperature)
72+
*temperature = crsfBaroTemperature;
73+
74+
return true;
75+
}
76+
77+
void crsfBaroReceiveNewData(int32_t pressurePa, int32_t temperature)
78+
{
79+
crsfBaroPressure = pressurePa;
80+
crsfBaroTemperature = temperature;
81+
crsfBaroLastUpdateMs = millis();
82+
83+
if (crsfBaroStarted == false && !ARMING_FLAG(WAS_EVER_ARMED)) {
84+
baroStartCalibration();
85+
crsfBaroStarted = true;
86+
sensorsSet(SENSOR_BARO);
87+
}
88+
}
89+
90+
bool crsfBaroDetect(baroDev_t *baro)
91+
{
92+
crsfBaroPressure = 101325;
93+
crsfBaroTemperature = 2500;
94+
crsfBaroLastUpdateMs = 0;
95+
96+
baro->ut_delay = 10000;
97+
baro->get_ut = crsfBaroStartGet;
98+
baro->start_ut = crsfBaroStartGet;
99+
100+
baro->up_delay = 10000;
101+
baro->start_up = crsfBaroStartGet;
102+
baro->get_up = crsfBaroStartGet;
103+
104+
baro->calculate = crsfBaroCalculate;
105+
106+
return true;
107+
}
108+
109+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
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/.
7+
*
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:
10+
*
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.
15+
*
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.
20+
*
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/.
23+
*/
24+
25+
#pragma once
26+
27+
struct baroDev_s;
28+
bool crsfBaroDetect(struct baroDev_s *baro);
29+
void crsfBaroReceiveNewData(int32_t pressurePa, int32_t temperature);

src/main/fc/fc_init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#include "io/serial.h"
121121
#include "io/displayport_msp.h"
122122
#include "io/smartport_master.h"
123+
#include "io/crsf_sensor.h"
123124
#include "io/vtx.h"
124125
#include "io/vtx_control.h"
125126
#include "io/vtx_smartaudio.h"
@@ -301,6 +302,10 @@ void init(void)
301302
smartportMasterInit();
302303
#endif
303304

305+
#if defined(USE_CRSF_SENSOR_INPUT)
306+
crsfSensorInputInit();
307+
#endif
308+
304309
#if defined(USE_LOG)
305310
// LOG might use serial output, so we only can init it after serial port is ready
306311
// From this point on we can use LOG_*() to produce real-time debugging information

src/main/fc/fc_tasks.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "io/rcdevice_cam.h"
6969
#include "io/osd_joystick.h"
7070
#include "io/smartport_master.h"
71+
#include "io/crsf_sensor.h"
7172
#include "io/vtx.h"
7273
#include "io/vtx_msp.h"
7374
#include "io/osd_dji_hd.h"
@@ -296,6 +297,14 @@ void taskSmartportMaster(timeUs_t currentTimeUs)
296297
}
297298
#endif
298299

300+
#if defined(USE_CRSF_SENSOR_INPUT)
301+
void taskCrsfSensor(timeUs_t currentTimeUs)
302+
{
303+
UNUSED(currentTimeUs);
304+
crsfSensorProcess();
305+
}
306+
#endif
307+
299308
#ifdef USE_LED_STRIP
300309
void taskLedStrip(timeUs_t currentTimeUs)
301310
{
@@ -443,6 +452,9 @@ void fcTasksInit(void)
443452
#if defined(USE_SMARTPORT_MASTER)
444453
setTaskEnabled(TASK_SMARTPORT_MASTER, true);
445454
#endif
455+
#if defined(USE_CRSF_SENSOR_INPUT)
456+
setTaskEnabled(TASK_CRSF_SENSOR, true);
457+
#endif
446458

447459
#ifdef USE_SERIAL_GIMBAL
448460
setTaskEnabled(TASK_GIMBAL, true);
@@ -634,6 +646,15 @@ cfTask_t cfTasks[TASK_COUNT] = {
634646
},
635647
#endif
636648

649+
#if defined(USE_CRSF_SENSOR_INPUT)
650+
[TASK_CRSF_SENSOR] = {
651+
.taskName = "CRSF SENSOR",
652+
.taskFunc = taskCrsfSensor,
653+
.desiredPeriod = TASK_PERIOD_HZ(100), // 100 Hz
654+
.staticPriority = TASK_PRIORITY_IDLE,
655+
},
656+
#endif
657+
637658
#ifdef USE_LED_STRIP
638659
[TASK_LEDSTRIP] = {
639660
.taskName = "LEDSTRIP",

src/main/fc/settings.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tables:
1414
values: ["NONE", "CXOF", "MSP", "FAKE"]
1515
enum: opticalFlowSensor_e
1616
- name: baro_hardware
17-
values: ["NONE", "AUTO", "BMP085", "MS5611", "BMP280", "MS5607", "LPS25H", "SPL06", "BMP388", "DPS310", "B2SMPB", "MSP", "FAKE"]
17+
values: ["NONE", "AUTO", "BMP085", "MS5611", "BMP280", "MS5607", "LPS25H", "SPL06", "BMP388", "DPS310", "B2SMPB", "MSP", "FAKE", "CRSF"]
1818
enum: baroSensor_e
1919
- name: pitot_hardware
2020
values: ["NONE", "AUTO", "MS4525", "ADC", "VIRTUAL", "FAKE", "MSP", "DLVR-L10D"]
@@ -33,16 +33,16 @@ tables:
3333
- name: failsafe_procedure
3434
values: ["LAND", "DROP", "RTH", "NONE"]
3535
- name: current_sensor
36-
values: ["NONE", "ADC", "VIRTUAL", "FAKE", "ESC", "SMARTPORT", "CAN"]
36+
values: ["NONE", "ADC", "VIRTUAL", "FAKE", "ESC", "SMARTPORT", "CRSF", "CAN"]
3737
enum: currentSensor_e
3838
- name: voltage_sensor
39-
values: ["NONE", "ADC", "ESC", "FAKE", "SMARTPORT", "CAN"]
39+
values: ["NONE", "ADC", "ESC", "FAKE", "SMARTPORT", "CRSF", "CAN"]
4040
enum: voltageSensor_e
4141
- name: imu_inertia_comp_method
4242
values: ["VELNED", "TURNRATE","ADAPTIVE"]
4343
enum: imu_inertia_comp_method_e
4444
- name: gps_provider
45-
values: ["UBLOX", "MSP", "FAKE", "DRONECAN"]
45+
values: ["UBLOX", "MSP", "CRSF", "FAKE", "DRONECAN"]
4646
enum: gpsProvider_e
4747
- name: gps_sbas_mode
4848
values: ["AUTO", "EGNOS", "WAAS", "MSAS", "GAGAN", "SPAN", "NONE"]

0 commit comments

Comments
 (0)