2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
28 #if defined(USE_TELEMETRY_SRXL)
30 #include "build/version.h"
34 #include "common/crc.h"
35 #include "common/streambuf.h"
36 #include "common/utils.h"
38 #include "config/config.h"
39 #include "config/feature.h"
41 #include "drivers/dshot.h"
42 #include "drivers/vtx_common.h"
44 #include "fc/rc_controls.h"
45 #include "fc/runtime_config.h"
47 #include "flight/imu.h"
48 #include "flight/mixer.h"
50 #include "io/displayport_srxl.h"
52 #include "io/serial.h"
53 #include "io/vtx_smartaudio.h"
54 #include "io/vtx_tramp.h"
60 #include "rx/spektrum.h"
61 #include "io/spektrum_vtx_control.h"
64 #include "sensors/adcinternal.h"
65 #include "sensors/battery.h"
66 #include "sensors/esc_sensor.h"
68 #include "telemetry/telemetry.h"
72 #define SRXL_ADDRESS_FIRST 0xA5
73 #define SRXL_ADDRESS_SECOND 0x80
74 #define SRXL_PACKET_LENGTH 0x15
76 #define SRXL_FRAMETYPE_TELE_QOS 0x7F
77 #define SRXL_FRAMETYPE_TELE_RPM 0x7E
78 #define SRXL_FRAMETYPE_POWERBOX 0x0A
79 #define SRXL_FRAMETYPE_TELE_FP_MAH 0x34
80 #define TELE_DEVICE_VTX 0x0D // Video Transmitter Status
81 #define SRXL_FRAMETYPE_SID 0x00
82 #define SRXL_FRAMETYPE_GPS_LOC 0x16 // GPS Location Data (Eagle Tree)
83 #define SRXL_FRAMETYPE_GPS_STAT 0x17
85 static bool srxlTelemetryEnabled
;
86 static bool srxl2
= false;
87 static uint8_t srxlFrame
[SRXL_FRAME_SIZE_MAX
];
89 static void srxlInitializeFrame(sbuf_t
*dst
)
92 #if defined(USE_SERIALRX_SRXL2)
93 srxl2InitializeFrame(dst
);
97 dst
->end
= ARRAYEND(srxlFrame
);
99 sbufWriteU8(dst
, SRXL_ADDRESS_FIRST
);
100 sbufWriteU8(dst
, SRXL_ADDRESS_SECOND
);
101 sbufWriteU8(dst
, SRXL_PACKET_LENGTH
);
105 static void srxlFinalize(sbuf_t
*dst
)
108 #if defined(USE_SERIALRX_SRXL2)
109 srxl2FinalizeFrame(dst
);
112 crc16_ccitt_sbuf_append(dst
, &srxlFrame
[3]); // start at byte 3, since CRC does not include device address and packet length
113 sbufSwitchToReader(dst
, srxlFrame
);
114 // write the telemetry frame to the receiver.
115 srxlRxWriteTelemetryData(sbufPtr(dst
), sbufBytesRemaining(dst
));
120 SRXL frame has the structure:
121 <0xA5><0x80><Length><16-byte telemetry packet><2 Byte CRC of payload>
122 The <Length> shall be 0x15 (length of the 16-byte telemetry packet + overhead).
128 UINT8 identifier; // Source device = 0x7F
129 UINT8 sID; // Secondary ID
136 UINT16 rxVoltage; // Volts, 0.01V increments
140 #define STRU_TELE_QOS_EMPTY_FIELDS_COUNT 14
141 #define STRU_TELE_QOS_EMPTY_FIELDS_VALUE 0xff
143 bool srxlFrameQos(sbuf_t
*dst
, timeUs_t currentTimeUs
)
145 UNUSED(currentTimeUs
);
147 sbufWriteU8(dst
, SRXL_FRAMETYPE_TELE_QOS
);
148 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
150 sbufFill(dst
, STRU_TELE_QOS_EMPTY_FIELDS_VALUE
, STRU_TELE_QOS_EMPTY_FIELDS_COUNT
); // Clear remainder
152 // Mandatory frame, send it unconditionally.
160 UINT8 identifier; // Source device = 0x7E
161 UINT8 sID; // Secondary ID
162 UINT16 microseconds; // microseconds between pulse leading edges
163 UINT16 volts; // 0.01V increments
164 INT16 temperature; // degrees F
165 INT8 dBm_A, // Average signal for A antenna in dBm
166 INT8 dBm_B; // Average signal for B antenna in dBm.
167 // If only 1 antenna, set B = A
171 #define STRU_TELE_RPM_EMPTY_FIELDS_COUNT 8
172 #define STRU_TELE_RPM_EMPTY_FIELDS_VALUE 0xff
174 #define SPEKTRUM_RPM_UNUSED 0xffff
175 #define SPEKTRUM_TEMP_UNUSED 0x7fff
176 #define MICROSEC_PER_MINUTE 60000000
178 //Original range of 1 - 65534 uSec gives an RPM range of 915 - 60000000rpm, 60MegaRPM
179 #define SPEKTRUM_MIN_RPM 999 // Min RPM to show the user, indicating RPM is really below 999
180 #define SPEKTRUM_MAX_RPM 60000000
182 uint16_t getMotorAveragePeriod(void)
185 #if defined( USE_ESC_SENSOR_TELEMETRY) || defined( USE_DSHOT_TELEMETRY)
187 uint16_t period_us
= SPEKTRUM_RPM_UNUSED
;
189 #if defined( USE_ESC_SENSOR_TELEMETRY)
190 escSensorData_t
*escData
= getEscSensorData(ESC_SENSOR_COMBINED
);
191 if (escData
!= NULL
) {
196 #if defined(USE_DSHOT_TELEMETRY)
197 // Calculate this way when no rpm from esc data
198 if (useDshotTelemetry
&& rpm
== 0) {
199 rpm
= getDshotAverageRpm();
203 if (rpm
> SPEKTRUM_MIN_RPM
&& rpm
< SPEKTRUM_MAX_RPM
) {
204 period_us
= MICROSEC_PER_MINUTE
/ rpm
; // revs/minute -> microSeconds
206 period_us
= MICROSEC_PER_MINUTE
/ SPEKTRUM_MIN_RPM
;
211 return SPEKTRUM_RPM_UNUSED
;
215 bool srxlFrameRpm(sbuf_t
*dst
, timeUs_t currentTimeUs
)
217 int16_t coreTemp
= SPEKTRUM_TEMP_UNUSED
;
218 #if defined(USE_ADC_INTERNAL)
219 coreTemp
= getCoreTemperatureCelsius();
220 coreTemp
= coreTemp
* 9 / 5 + 32; // C -> F
223 UNUSED(currentTimeUs
);
225 sbufWriteU8(dst
, SRXL_FRAMETYPE_TELE_RPM
);
226 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
227 sbufWriteU16BigEndian(dst
, getMotorAveragePeriod()); // pulse leading edges
228 if (telemetryConfig()->report_cell_voltage
) {
229 sbufWriteU16BigEndian(dst
, getBatteryAverageCellVoltage()); // Cell voltage is in units of 0.01V
231 sbufWriteU16BigEndian(dst
, getBatteryVoltage()); // vbat is in units of 0.01V
233 sbufWriteU16BigEndian(dst
, coreTemp
); // temperature
234 sbufFill(dst
, STRU_TELE_RPM_EMPTY_FIELDS_VALUE
, STRU_TELE_RPM_EMPTY_FIELDS_COUNT
);
236 // Mandatory frame, send it unconditionally.
242 // From Frsky implementation
243 static void GPStoDDDMM_MMMM(int32_t mwiigps
, gpsCoordinateDDDMMmmmm_t
*result
)
245 int32_t absgps
, deg
, min
;
246 absgps
= abs(mwiigps
);
247 deg
= absgps
/ GPS_DEGREES_DIVIDER
;
248 absgps
= (absgps
- deg
* GPS_DEGREES_DIVIDER
) * 60; // absgps = Minutes left * 10^7
249 min
= absgps
/ GPS_DEGREES_DIVIDER
; // minutes left
250 result
->dddmm
= deg
* 100 + min
;
251 result
->mmmm
= (absgps
- min
* GPS_DEGREES_DIVIDER
) / 1000;
255 static uint32_t dec2bcd(uint16_t dec
)
261 result
|= (dec
% 10) << counter
* 4;
271 UINT8 identifier; // Source device = 0x16
272 UINT8 sID; // Secondary ID
273 UINT16 altitudeLow; // BCD, meters, format 3.1 (Low order of altitude)
274 UINT32 latitude; // BCD, format 4.4, Degrees * 100 + minutes, less than 100 degrees
275 UINT32 longitude; // BCD, format 4.4 , Degrees * 100 + minutes, flag indicates > 99 degrees
276 UINT16 course; // BCD, 3.1
277 UINT8 HDOP; // BCD, format 1.1
278 UINT8 GPSflags; // see definitions below
282 // GPS flags definitions
283 #define GPS_FLAGS_IS_NORTH_BIT 0x01
284 #define GPS_FLAGS_IS_EAST_BIT 0x02
285 #define GPS_FLAGS_LONGITUDE_GREATER_99_BIT 0x04
286 #define GPS_FLAGS_GPS_FIX_VALID_BIT 0x08
287 #define GPS_FLAGS_GPS_DATA_RECEIVED_BIT 0x10
288 #define GPS_FLAGS_3D_FIX_BIT 0x20
289 #define GPS_FLAGS_NEGATIVE_ALT_BIT 0x80
291 bool srxlFrameGpsLoc(sbuf_t
*dst
, timeUs_t currentTimeUs
)
293 UNUSED(currentTimeUs
);
294 gpsCoordinateDDDMMmmmm_t coordinate
;
295 uint32_t latitudeBcd
, longitudeBcd
, altitudeLo
;
296 uint16_t altitudeLoBcd
, groundCourseBcd
, hdop
;
297 uint8_t hdopBcd
, gpsFlags
;
299 if (!featureIsEnabled(FEATURE_GPS
) || !STATE(GPS_FIX
) || gpsSol
.numSat
< GPS_MIN_SAT_COUNT
) {
304 GPStoDDDMM_MMMM(gpsSol
.llh
.lat
, &coordinate
);
305 latitudeBcd
= (dec2bcd(coordinate
.dddmm
) << 16) | dec2bcd(coordinate
.mmmm
);
308 GPStoDDDMM_MMMM(gpsSol
.llh
.lon
, &coordinate
);
309 longitudeBcd
= (dec2bcd(coordinate
.dddmm
) << 16) | dec2bcd(coordinate
.mmmm
);
311 // altitude (low order)
312 altitudeLo
= abs(gpsSol
.llh
.altCm
) / 10;
313 altitudeLoBcd
= dec2bcd(altitudeLo
% 100000);
316 groundCourseBcd
= dec2bcd(gpsSol
.groundCourse
);
319 hdop
= gpsSol
.dop
.hdop
/ 10;
320 hdop
= (hdop
> 99) ? 99 : hdop
;
321 hdopBcd
= dec2bcd(hdop
);
324 gpsFlags
= GPS_FLAGS_GPS_DATA_RECEIVED_BIT
| GPS_FLAGS_GPS_FIX_VALID_BIT
| GPS_FLAGS_3D_FIX_BIT
;
325 gpsFlags
|= (gpsSol
.llh
.lat
> 0) ? GPS_FLAGS_IS_NORTH_BIT
: 0;
326 gpsFlags
|= (gpsSol
.llh
.lon
> 0) ? GPS_FLAGS_IS_EAST_BIT
: 0;
327 gpsFlags
|= (gpsSol
.llh
.altCm
< 0) ? GPS_FLAGS_NEGATIVE_ALT_BIT
: 0;
328 gpsFlags
|= (gpsSol
.llh
.lon
/ GPS_DEGREES_DIVIDER
> 99) ? GPS_FLAGS_LONGITUDE_GREATER_99_BIT
: 0;
331 sbufWriteU8(dst
, SRXL_FRAMETYPE_GPS_LOC
);
332 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
333 sbufWriteU16(dst
, altitudeLoBcd
);
334 sbufWriteU32(dst
, latitudeBcd
);
335 sbufWriteU32(dst
, longitudeBcd
);
336 sbufWriteU16(dst
, groundCourseBcd
);
337 sbufWriteU8(dst
, hdopBcd
);
338 sbufWriteU8(dst
, gpsFlags
);
346 UINT8 identifier; // Source device = 0x17
347 UINT8 sID; // Secondary ID
348 UINT16 speed; // BCD, knots, format 3.1
349 UINT32 UTC; // BCD, format HH:MM:SS.S, format 6.1
350 UINT8 numSats; // BCD, 0-99
351 UINT8 altitudeHigh; // BCD, meters, format 2.0 (High bits alt)
352 } STRU_TELE_GPS_STAT;
355 #define STRU_TELE_GPS_STAT_EMPTY_FIELDS_VALUE 0xff
356 #define STRU_TELE_GPS_STAT_EMPTY_FIELDS_COUNT 6
357 #define SPEKTRUM_TIME_UNKNOWN 0xFFFFFFFF
359 bool srxlFrameGpsStat(sbuf_t
*dst
, timeUs_t currentTimeUs
)
361 UNUSED(currentTimeUs
);
363 uint16_t speedKnotsBcd
, speedTmp
;
364 uint8_t numSatBcd
, altitudeHighBcd
;
365 bool timeProvided
= false;
367 if (!featureIsEnabled(FEATURE_GPS
) || !STATE(GPS_FIX
) || gpsSol
.numSat
< GPS_MIN_SAT_COUNT
) {
371 // Number of sats and altitude (high bits)
372 numSatBcd
= (gpsSol
.numSat
> 99) ? dec2bcd(99) : dec2bcd(gpsSol
.numSat
);
373 altitudeHighBcd
= dec2bcd(gpsSol
.llh
.altCm
/ 100000);
376 speedTmp
= gpsSol
.groundSpeed
* 1944 / 1000;
377 speedKnotsBcd
= (speedTmp
> 9999) ? dec2bcd(9999) : dec2bcd(speedTmp
);
384 timeBcd
= dec2bcd(dt
.hours
);
385 timeBcd
= timeBcd
<< 8;
386 timeBcd
= timeBcd
| dec2bcd(dt
.minutes
);
387 timeBcd
= timeBcd
<< 8;
388 timeBcd
= timeBcd
| dec2bcd(dt
.seconds
);
389 timeBcd
= timeBcd
<< 4;
390 timeBcd
= timeBcd
| dec2bcd(dt
.millis
/ 100);
394 timeBcd
= (timeProvided
) ? timeBcd
: SPEKTRUM_TIME_UNKNOWN
;
397 sbufWriteU8(dst
, SRXL_FRAMETYPE_GPS_STAT
);
398 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
399 sbufWriteU16(dst
, speedKnotsBcd
);
400 sbufWriteU32(dst
, timeBcd
);
401 sbufWriteU8(dst
, numSatBcd
);
402 sbufWriteU8(dst
, altitudeHighBcd
);
403 sbufFill(dst
, STRU_TELE_GPS_STAT_EMPTY_FIELDS_VALUE
, STRU_TELE_GPS_STAT_EMPTY_FIELDS_COUNT
);
413 UINT8 identifier; // Source device = 0x34
414 UINT8 sID; // Secondary ID
415 INT16 current_A; // Instantaneous current, 0.1A (0-3276.8A)
416 INT16 chargeUsed_A; // Integrated mAh used, 1mAh (0-32.766Ah)
417 UINT16 temp_A; // Temperature, 0.1C (0-150C, 0x7FFF indicates not populated)
418 INT16 current_B; // Instantaneous current, 0.1A (0-3276.8A)
419 INT16 chargeUsed_B; // Integrated mAh used, 1mAh (0-32.766Ah)
420 UINT16 temp_B; // Temperature, 0.1C (0-150C, 0x7FFF indicates not populated)
421 UINT16 spare; // Not used
424 #define STRU_TELE_FP_EMPTY_FIELDS_COUNT 2
425 #define STRU_TELE_FP_EMPTY_FIELDS_VALUE 0xff
427 #define SPEKTRUM_AMPS_UNUSED 0x7fff
428 #define SPEKTRUM_AMPH_UNUSED 0x7fff
430 #define FP_MAH_KEEPALIVE_TIME_OUT 2000000 // 2s
432 bool srxlFrameFlightPackCurrent(sbuf_t
*dst
, timeUs_t currentTimeUs
)
434 uint16_t amps
= getAmperage() / 10;
435 uint16_t mah
= getMAhDrawn();
436 static uint16_t sentAmps
;
437 static uint16_t sentMah
;
438 static timeUs_t lastTimeSentFPmAh
= 0;
440 timeUs_t keepAlive
= currentTimeUs
- lastTimeSentFPmAh
;
442 if ( amps
!= sentAmps
||
444 keepAlive
> FP_MAH_KEEPALIVE_TIME_OUT
) {
446 sbufWriteU8(dst
, SRXL_FRAMETYPE_TELE_FP_MAH
);
447 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
448 sbufWriteU16(dst
, amps
);
449 sbufWriteU16(dst
, mah
);
450 sbufWriteU16(dst
, SPEKTRUM_TEMP_UNUSED
); // temp A
451 sbufWriteU16(dst
, SPEKTRUM_AMPS_UNUSED
); // Amps B
452 sbufWriteU16(dst
, SPEKTRUM_AMPH_UNUSED
); // mAH B
453 sbufWriteU16(dst
, SPEKTRUM_TEMP_UNUSED
); // temp B
455 sbufFill(dst
, STRU_TELE_FP_EMPTY_FIELDS_VALUE
, STRU_TELE_FP_EMPTY_FIELDS_COUNT
);
459 lastTimeSentFPmAh
= currentTimeUs
;
465 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
467 // Betaflight CMS using Spektrum Tx telemetry TEXT_GEN sensor as display.
469 #define SPEKTRUM_SRXL_DEVICE_TEXTGEN (0x0C) // Text Generator
470 #define SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS (9) // Text Generator ROWS
471 #define SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS (13) // Text Generator COLS
477 UINT8 sID; // Secondary ID
478 UINT8 lineNumber; // Line number to display (0 = title, 1-8 for general, 254 = Refresh backlight, 255 = Erase all text on screen)
479 char text[13]; // 0-terminated text when < 13 chars
480 } STRU_SPEKTRUM_SRXL_TEXTGEN;
483 #if ( SPEKTRUM_SRXL_TEXTGEN_BUFFER_COLS > SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS )
484 static char srxlTextBuff
[SPEKTRUM_SRXL_TEXTGEN_BUFFER_ROWS
][SPEKTRUM_SRXL_TEXTGEN_BUFFER_COLS
];
485 static bool lineSent
[SPEKTRUM_SRXL_TEXTGEN_BUFFER_ROWS
];
487 static char srxlTextBuff
[SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS
][SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS
];
488 static bool lineSent
[SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS
];
491 //**************************************************************************
492 // API Running in external client task context. E.g. in the CMS task
493 int spektrumTmTextGenPutChar(uint8_t col
, uint8_t row
, char c
)
495 if (row
< SPEKTRUM_SRXL_TEXTGEN_BUFFER_ROWS
&& col
< SPEKTRUM_SRXL_TEXTGEN_BUFFER_COLS
) {
496 // Only update and force a tm transmision if something has actually changed.
497 if (srxlTextBuff
[row
][col
] != c
) {
498 srxlTextBuff
[row
][col
] = c
;
499 lineSent
[row
] = false;
504 //**************************************************************************
506 bool srxlFrameText(sbuf_t
*dst
, timeUs_t currentTimeUs
)
508 UNUSED(currentTimeUs
);
509 static uint8_t lineNo
= 0;
512 // Skip already sent lines...
513 while (lineSent
[lineNo
] &&
514 lineCount
< SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS
) {
515 lineNo
= (lineNo
+ 1) % SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS
;
519 sbufWriteU8(dst
, SPEKTRUM_SRXL_DEVICE_TEXTGEN
);
520 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
521 sbufWriteU8(dst
, lineNo
);
522 sbufWriteData(dst
, srxlTextBuff
[lineNo
], SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS
);
524 lineSent
[lineNo
] = true;
525 lineNo
= (lineNo
+ 1) % SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS
;
527 // Always send something, Always one user frame after the two mandatory frames
528 // I.e. All of the three frame prep routines QOS, RPM, TEXT should always return true
529 // too keep the "Waltz" sequence intact.
534 #if defined(USE_SPEKTRUM_VTX_TELEMETRY) && defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
536 static uint8_t vtxDeviceType
;
538 static void collectVtxTmData(spektrumVtx_t
* vtx
)
540 const vtxDevice_t
*vtxDevice
= vtxCommonDevice();
541 vtxDeviceType
= vtxCommonGetDeviceType(vtxDevice
);
543 // Collect all data from VTX, if VTX is ready
545 if (vtxDevice
== NULL
|| !(vtxCommonGetBandAndChannel(vtxDevice
, &vtx
->band
, &vtx
->channel
) &&
546 vtxCommonGetStatus(vtxDevice
, &vtxStatus
) &&
547 vtxCommonGetPowerIndex(vtxDevice
, &vtx
->power
)) )
554 vtx
->pitMode
= (vtxStatus
& VTX_STATUS_PIT_MODE
) ? 1 : 0;
558 #ifdef USE_SPEKTRUM_REGION_CODES
559 vtx
->region
= SpektrumRegion
;
561 vtx
->region
= SPEKTRUM_VTX_REGION_NONE
;
565 // Reverse lookup, device power index to Spektrum power range index.
566 static void convertVtxPower(spektrumVtx_t
* vtx
)
568 uint8_t const * powerIndexTable
= NULL
;
570 vtxCommonLookupPowerValue(vtxCommonDevice(), vtx
->power
, &vtx
->powerValue
);
571 switch (vtxDeviceType
) {
573 #if defined(USE_VTX_TRAMP)
575 powerIndexTable
= vtxTrampPi
;
578 #if defined(USE_VTX_SMARTAUDIO)
579 case VTXDEV_SMARTAUDIO
:
580 powerIndexTable
= vtxSaPi
;
583 #if defined(USE_VTX_RTC6705)
585 powerIndexTable
= vtxRTC6705Pi
;
590 case VTXDEV_UNSUPPORTED
:
596 if (powerIndexTable
!= NULL
) {
597 for (int i
= 0; i
< SPEKTRUM_VTX_POWER_COUNT
; i
++)
598 if (powerIndexTable
[i
] >= vtx
->power
) {
599 vtx
->power
= i
; // Translate device power index to Spektrum power index.
605 static void convertVtxTmData(spektrumVtx_t
* vtx
)
607 // Convert from internal band indexes to Spektrum indexes
608 for (int i
= 0; i
< SPEKTRUM_VTX_BAND_COUNT
; i
++) {
609 if (spek2commonBand
[i
] == vtx
->band
) {
615 // De-bump channel no 1 based interally, 0-based in Spektrum.
618 // Convert Power index to Spektrum ranges, different per brand.
619 convertVtxPower(vtx
);
626 UINT8 sID; // Secondary ID
627 UINT8 band; // VTX Band (0 = Fatshark, 1 = Raceband, 2 = E, 3 = B, 4 = A, 5-7 = Reserved)
628 UINT8 channel; // VTX Channel (0-7)
629 UINT8 pit; // Pit/Race mode (0 = Race, 1 = Pit). Race = (normal operating) mode. Pit = (reduced power) mode. When PIT is set, it overrides all other power settings
630 UINT8 power; // VTX Power (0 = Off, 1 = 1mw to 14mW, 2 = 15mW to 25mW, 3 = 26mW to 99mW, 4 = 100mW to 299mW, 5 = 300mW to 600mW, 6 = 601mW+, 7 = manual control)
631 UINT16 powerDec; // VTX Power as a decimal 1mw/unit
632 UINT8 region; // Region (0 = USA, 1 = EU, 0xFF = N/A)
633 UINT8 rfu[7]; // reserved
637 #define STRU_TELE_VTX_EMPTY_COUNT 7
638 #define STRU_TELE_VTX_EMPTY_VALUE 0xff
640 #define VTX_KEEPALIVE_TIME_OUT 2000000 // uS
642 static bool srxlFrameVTX(sbuf_t
*dst
, timeUs_t currentTimeUs
)
644 static timeUs_t lastTimeSentVtx
= 0;
645 static spektrumVtx_t vtxSent
;
648 collectVtxTmData(&vtx
);
650 if ((vtxDeviceType
!= VTXDEV_UNKNOWN
) && vtxDeviceType
!= VTXDEV_UNSUPPORTED
) {
651 convertVtxTmData(&vtx
);
653 if ((memcmp(&vtxSent
, &vtx
, sizeof(spektrumVtx_t
)) != 0) ||
654 ((currentTimeUs
- lastTimeSentVtx
) > VTX_KEEPALIVE_TIME_OUT
) ) {
655 // Fill in the VTX tm structure
656 sbufWriteU8(dst
, TELE_DEVICE_VTX
);
657 sbufWriteU8(dst
, SRXL_FRAMETYPE_SID
);
658 sbufWriteU8(dst
, vtx
.band
);
659 sbufWriteU8(dst
, vtx
.channel
);
660 sbufWriteU8(dst
, vtx
.pitMode
);
661 sbufWriteU8(dst
, vtx
.power
);
662 sbufWriteU16(dst
, vtx
.powerValue
);
663 sbufWriteU8(dst
, vtx
.region
);
665 sbufFill(dst
, STRU_TELE_VTX_EMPTY_VALUE
, STRU_TELE_VTX_EMPTY_COUNT
);
667 memcpy(&vtxSent
, &vtx
, sizeof(spektrumVtx_t
));
668 lastTimeSentVtx
= currentTimeUs
;
674 #endif // USE_SPEKTRUM_VTX_TELEMETRY && USE_SPEKTRUM_VTX_CONTROL && USE_VTX_COMMON
677 // Schedule array to decide how often each type of frame is sent
678 // The frames are scheduled in sets of 3 frames, 2 mandatory and 1 user frame.
679 // The user frame type is cycled for each set.
680 // Example. QOS, RPM,.CURRENT, QOS, RPM, TEXT. QOS, RPM, CURRENT, etc etc
682 #define SRXL_SCHEDULE_MANDATORY_COUNT 2 // Mandatory QOS and RPM sensors
684 #define SRXL_FP_MAH_COUNT 1
687 #define SRXL_GPS_LOC_COUNT 1
688 #define SRXL_GPS_STAT_COUNT 1
690 #define SRXL_GPS_LOC_COUNT 0
691 #define SRXL_GPS_STAT_COUNT 0
694 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
695 #define SRXL_SCHEDULE_CMS_COUNT 1
697 #define SRXL_SCHEDULE_CMS_COUNT 0
700 #if defined(USE_SPEKTRUM_VTX_TELEMETRY) && defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
701 #define SRXL_VTX_TM_COUNT 1
703 #define SRXL_VTX_TM_COUNT 0
706 #define SRXL_SCHEDULE_USER_COUNT (SRXL_FP_MAH_COUNT + SRXL_SCHEDULE_CMS_COUNT + SRXL_VTX_TM_COUNT + SRXL_GPS_LOC_COUNT + SRXL_GPS_STAT_COUNT)
707 #define SRXL_SCHEDULE_COUNT_MAX (SRXL_SCHEDULE_MANDATORY_COUNT + 1)
708 #define SRXL_TOTAL_COUNT (SRXL_SCHEDULE_MANDATORY_COUNT + SRXL_SCHEDULE_USER_COUNT)
710 typedef bool (*srxlScheduleFnPtr
)(sbuf_t
*dst
, timeUs_t currentTimeUs
);
712 const srxlScheduleFnPtr srxlScheduleFuncs
[SRXL_TOTAL_COUNT
] = {
713 /* must send srxlFrameQos, Rpm and then alternating items of our own */
716 srxlFrameFlightPackCurrent
,
721 #if defined(USE_SPEKTRUM_VTX_TELEMETRY) && defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
724 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
730 static void processSrxl(timeUs_t currentTimeUs
)
732 static uint8_t srxlScheduleIndex
= 0;
733 static uint8_t srxlScheduleUserIndex
= 0;
735 sbuf_t srxlPayloadBuf
;
736 sbuf_t
*dst
= &srxlPayloadBuf
;
737 srxlScheduleFnPtr srxlFnPtr
;
739 if (srxlScheduleIndex
< SRXL_SCHEDULE_MANDATORY_COUNT
) {
740 srxlFnPtr
= srxlScheduleFuncs
[srxlScheduleIndex
];
742 srxlFnPtr
= srxlScheduleFuncs
[srxlScheduleIndex
+ srxlScheduleUserIndex
];
743 srxlScheduleUserIndex
= (srxlScheduleUserIndex
+ 1) % SRXL_SCHEDULE_USER_COUNT
;
745 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
746 // Boost CMS performance by sending nothing else but CMS Text frames when in a CMS menu.
747 // Sideeffect, all other reports are still not sent if user leaves CMS without a proper EXIT.
749 (pCurrentDisplay
== &srxlDisplayPort
)) {
750 srxlFnPtr
= srxlFrameText
;
757 srxlInitializeFrame(dst
);
758 if (srxlFnPtr(dst
, currentTimeUs
)) {
762 srxlScheduleIndex
= (srxlScheduleIndex
+ 1) % SRXL_SCHEDULE_COUNT_MAX
;
765 void initSrxlTelemetry(void)
767 // check if there is a serial port open for SRXL telemetry (ie opened by the SRXL RX)
768 // and feature is enabled, if so, set SRXL telemetry enabled
769 if (srxlRxIsActive()) {
770 srxlTelemetryEnabled
= true;
772 #if defined(USE_SERIALRX_SRXL2)
773 } else if (srxl2RxIsActive()) {
774 srxlTelemetryEnabled
= true;
778 srxlTelemetryEnabled
= false;
782 #if defined(USE_SPEKTRUM_CMS_TELEMETRY)
783 if (srxlTelemetryEnabled
) {
784 srxlDisplayportRegister();
789 bool checkSrxlTelemetryState(void)
791 return srxlTelemetryEnabled
;
795 * Called periodically by the scheduler
797 void handleSrxlTelemetry(timeUs_t currentTimeUs
)
800 #if defined(USE_SERIALRX_SRXL2)
801 if (srxl2TelemetryRequested()) {
802 processSrxl(currentTimeUs
);
806 if (srxlTelemetryBufferEmpty()) {
807 processSrxl(currentTimeUs
);