[SITL] Enable telemetry: LTM and MAVLink (#8940)
[inav.git] / src / main / telemetry / srxl.c
blob8e01e47ef8ec784a844059f6cc7764ddf0cea722
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
25 #include "platform.h"
27 #if defined(USE_TELEMETRY_SRXL)
29 #include "build/version.h"
31 #include "cms/cms.h"
32 #include "io/displayport_srxl.h"
34 #include "common/crc.h"
35 #include "common/streambuf.h"
36 #include "common/utils.h"
38 #include "config/feature.h"
40 #include "io/gps.h"
41 #include "io/serial.h"
43 //#include "config/config.h"
44 #include "fc/config.h"
45 #include "fc/rc_controls.h"
46 #include "fc/runtime_config.h"
48 #include "flight/imu.h"
49 #include "flight/mixer.h"
51 #include "io/gps.h"
53 //#include "pg/motor.h"
55 #include "rx/rx.h"
56 #include "rx/spektrum.h"
57 #include "rx/srxl2.h"
59 #include "sensors/battery.h"
60 //#include "sensors/adcinternal.h"
61 #include "sensors/esc_sensor.h"
63 #include "telemetry/telemetry.h"
64 #include "telemetry/srxl.h"
66 #include "drivers/vtx_common.h"
67 //#include "drivers/dshot.h"
69 #include "io/vtx_tramp.h"
70 #include "io/vtx_smartaudio.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)
91 if (srxl2) {
92 #if defined(USE_SERIALRX_SRXL2)
93 srxl2InitializeFrame(dst);
94 #endif
95 } else {
96 dst->ptr = srxlFrame;
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)
107 if (srxl2) {
108 #if defined(USE_SERIALRX_SRXL2)
109 srxl2FinalizeFrame(dst);
110 #endif
111 } else {
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).
126 typedef struct
128 UINT8 identifier; // Source device = 0x7F
129 UINT8 sID; // Secondary ID
130 UINT16 A;
131 UINT16 B;
132 UINT16 L;
133 UINT16 R;
134 UINT16 F;
135 UINT16 H;
136 UINT16 rxVoltage; // Volts, 0.01V increments
137 } STRU_TELE_QOS;
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.
153 return true;
158 typedef struct
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
168 } STRU_TELE_RPM;
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)
186 uint32_t rpm = 0;
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) {
192 rpm = escData->rpm;
194 #endif
196 #if defined(USE_DSHOT_TELEMETRY)
197 if (useDshotTelemetry) {
198 uint16_t motors = getMotorCount();
200 if (motors > 0) {
201 for (int motor = 0; motor < motors; motor++) {
202 rpm += getDshotTelemetry(motor);
204 rpm = 100.0f / (motorConfig()->motorPoleCount / 2.0f) * rpm; // convert erpm freq to RPM.
205 rpm /= motors; // Average combined rpm
208 #endif
210 if (rpm > SPEKTRUM_MIN_RPM && rpm < SPEKTRUM_MAX_RPM) {
211 period_us = MICROSEC_PER_MINUTE / rpm; // revs/minute -> microSeconds
212 } else {
213 period_us = MICROSEC_PER_MINUTE / SPEKTRUM_MIN_RPM;
216 return period_us;
217 #else
218 return SPEKTRUM_RPM_UNUSED;
219 #endif
222 bool srxlFrameRpm(sbuf_t *dst, timeUs_t currentTimeUs)
224 int16_t coreTemp = SPEKTRUM_TEMP_UNUSED;
225 #if defined(USE_ADC_INTERNAL)
226 coreTemp = getCoreTemperatureCelsius();
227 coreTemp = coreTemp * 9 / 5 + 32; // C -> F
228 #endif
230 UNUSED(currentTimeUs);
232 sbufWriteU8(dst, SRXL_FRAMETYPE_TELE_RPM);
233 sbufWriteU8(dst, SRXL_FRAMETYPE_SID);
234 sbufWriteU16BigEndian(dst, getMotorAveragePeriod()); // pulse leading edges
235 if (telemetryConfig()->report_cell_voltage) {
236 sbufWriteU16BigEndian(dst, getBatteryAverageCellVoltage()); // Cell voltage is in units of 0.01V
237 } else {
238 sbufWriteU16BigEndian(dst, getBatteryVoltage()); // vbat is in units of 0.01V
240 sbufWriteU16BigEndian(dst, coreTemp); // temperature
241 sbufFill(dst, STRU_TELE_RPM_EMPTY_FIELDS_VALUE, STRU_TELE_RPM_EMPTY_FIELDS_COUNT);
243 // Mandatory frame, send it unconditionally.
244 return true;
247 #if defined(USE_GPS)
249 // From Frsky implementation
250 static void GPStoDDDMM_MMMM(int32_t mwiigps, gpsCoordinateDDDMMmmmm_t *result)
252 int32_t absgps, deg, min;
253 absgps = ABS(mwiigps);
254 deg = absgps / GPS_DEGREES_DIVIDER;
255 absgps = (absgps - deg * GPS_DEGREES_DIVIDER) * 60; // absgps = Minutes left * 10^7
256 min = absgps / GPS_DEGREES_DIVIDER; // minutes left
257 result->dddmm = deg * 100 + min;
258 result->mmmm = (absgps - min * GPS_DEGREES_DIVIDER) / 1000;
261 // BCD conversion
262 static uint32_t dec2bcd(uint16_t dec)
264 uint32_t result = 0;
265 uint8_t counter = 0;
267 while (dec) {
268 result |= (dec % 10) << counter * 4;
269 counter++;
270 dec /= 10;
272 return result;
276 typedef struct
278 UINT8 identifier; // Source device = 0x16
279 UINT8 sID; // Secondary ID
280 UINT16 altitudeLow; // BCD, meters, format 3.1 (Low order of altitude)
281 UINT32 latitude; // BCD, format 4.4, Degrees * 100 + minutes, less than 100 degrees
282 UINT32 longitude; // BCD, format 4.4 , Degrees * 100 + minutes, flag indicates > 99 degrees
283 UINT16 course; // BCD, 3.1
284 UINT8 HDOP; // BCD, format 1.1
285 UINT8 GPSflags; // see definitions below
286 } STRU_TELE_GPS_LOC;
289 // GPS flags definitions
290 #define GPS_FLAGS_IS_NORTH_BIT 0x01
291 #define GPS_FLAGS_IS_EAST_BIT 0x02
292 #define GPS_FLAGS_LONGITUDE_GREATER_99_BIT 0x04
293 #define GPS_FLAGS_GPS_FIX_VALID_BIT 0x08
294 #define GPS_FLAGS_GPS_DATA_RECEIVED_BIT 0x10
295 #define GPS_FLAGS_3D_FIX_BIT 0x20
296 #define GPS_FLAGS_NEGATIVE_ALT_BIT 0x80
298 bool srxlFrameGpsLoc(sbuf_t *dst, timeUs_t currentTimeUs)
300 UNUSED(currentTimeUs);
301 gpsCoordinateDDDMMmmmm_t coordinate;
302 uint32_t latitudeBcd, longitudeBcd, altitudeLo;
303 uint16_t altitudeLoBcd, groundCourseBcd, hdop;
304 uint8_t hdopBcd, gpsFlags;
306 if (!feature(FEATURE_GPS) || !STATE(GPS_FIX) || gpsSol.numSat < 6) {
307 return false;
310 // lattitude
311 GPStoDDDMM_MMMM(gpsSol.llh.lat, &coordinate);
312 latitudeBcd = (dec2bcd(coordinate.dddmm) << 16) | dec2bcd(coordinate.mmmm);
314 // longitude
315 GPStoDDDMM_MMMM(gpsSol.llh.lon, &coordinate);
316 longitudeBcd = (dec2bcd(coordinate.dddmm) << 16) | dec2bcd(coordinate.mmmm);
318 // altitude (low order)
319 altitudeLo = ABS(gpsSol.llh.alt) / 10;
320 altitudeLoBcd = dec2bcd(altitudeLo % 100000);
322 // Ground course
323 groundCourseBcd = dec2bcd(gpsSol.groundCourse);
325 // HDOP
326 hdop = gpsSol.hdop / 10;
327 hdop = (hdop > 99) ? 99 : hdop;
328 hdopBcd = dec2bcd(hdop);
330 // flags
331 gpsFlags = GPS_FLAGS_GPS_DATA_RECEIVED_BIT | GPS_FLAGS_GPS_FIX_VALID_BIT | GPS_FLAGS_3D_FIX_BIT;
332 gpsFlags |= (gpsSol.llh.lat > 0) ? GPS_FLAGS_IS_NORTH_BIT : 0;
333 gpsFlags |= (gpsSol.llh.lon > 0) ? GPS_FLAGS_IS_EAST_BIT : 0;
334 gpsFlags |= (gpsSol.llh.alt < 0) ? GPS_FLAGS_NEGATIVE_ALT_BIT : 0;
335 gpsFlags |= (gpsSol.llh.lon / GPS_DEGREES_DIVIDER > 99) ? GPS_FLAGS_LONGITUDE_GREATER_99_BIT : 0;
337 // SRXL frame
338 sbufWriteU8(dst, SRXL_FRAMETYPE_GPS_LOC);
339 sbufWriteU8(dst, SRXL_FRAMETYPE_SID);
340 sbufWriteU16(dst, altitudeLoBcd);
341 sbufWriteU32(dst, latitudeBcd);
342 sbufWriteU32(dst, longitudeBcd);
343 sbufWriteU16(dst, groundCourseBcd);
344 sbufWriteU8(dst, hdopBcd);
345 sbufWriteU8(dst, gpsFlags);
347 return true;
351 typedef struct
353 UINT8 identifier; // Source device = 0x17
354 UINT8 sID; // Secondary ID
355 UINT16 speed; // BCD, knots, format 3.1
356 UINT32 UTC; // BCD, format HH:MM:SS.S, format 6.1
357 UINT8 numSats; // BCD, 0-99
358 UINT8 altitudeHigh; // BCD, meters, format 2.0 (High bits alt)
359 } STRU_TELE_GPS_STAT;
362 #define STRU_TELE_GPS_STAT_EMPTY_FIELDS_VALUE 0xff
363 #define STRU_TELE_GPS_STAT_EMPTY_FIELDS_COUNT 6
364 #define SPEKTRUM_TIME_UNKNOWN 0xFFFFFFFF
366 bool srxlFrameGpsStat(sbuf_t *dst, timeUs_t currentTimeUs)
368 UNUSED(currentTimeUs);
369 uint32_t timeBcd;
370 uint16_t speedKnotsBcd, speedTmp;
371 uint8_t numSatBcd, altitudeHighBcd;
372 bool timeProvided = false;
374 if (!feature(FEATURE_GPS) || !STATE(GPS_FIX) || gpsSol.numSat < 6) {
375 return false;
378 // Number of sats and altitude (high bits)
379 numSatBcd = (gpsSol.numSat > 99) ? dec2bcd(99) : dec2bcd(gpsSol.numSat);
380 altitudeHighBcd = dec2bcd(gpsSol.llh.alt / 100000);
382 // Speed (knots)
383 speedTmp = gpsSol.groundSpeed * 1944 / 1000;
384 speedKnotsBcd = (speedTmp > 9999) ? dec2bcd(9999) : dec2bcd(speedTmp);
386 #ifdef USE_RTC_TIME
387 dateTime_t dt;
388 // RTC
389 if (rtcHasTime()) {
390 rtcGetDateTime(&dt);
391 timeBcd = dec2bcd(dt.hours);
392 timeBcd = timeBcd << 8;
393 timeBcd = timeBcd | dec2bcd(dt.minutes);
394 timeBcd = timeBcd << 8;
395 timeBcd = timeBcd | dec2bcd(dt.seconds);
396 timeBcd = timeBcd << 4;
397 timeBcd = timeBcd | dec2bcd(dt.millis / 100);
398 timeProvided = true;
400 #endif
401 timeBcd = (timeProvided) ? timeBcd : SPEKTRUM_TIME_UNKNOWN;
403 // SRXL frame
404 sbufWriteU8(dst, SRXL_FRAMETYPE_GPS_STAT);
405 sbufWriteU8(dst, SRXL_FRAMETYPE_SID);
406 sbufWriteU16(dst, speedKnotsBcd);
407 sbufWriteU32(dst, timeBcd);
408 sbufWriteU8(dst, numSatBcd);
409 sbufWriteU8(dst, altitudeHighBcd);
410 sbufFill(dst, STRU_TELE_GPS_STAT_EMPTY_FIELDS_VALUE, STRU_TELE_GPS_STAT_EMPTY_FIELDS_COUNT);
412 return true;
415 #endif
418 typedef struct
420 UINT8 identifier; // Source device = 0x34
421 UINT8 sID; // Secondary ID
422 INT16 current_A; // Instantaneous current, 0.1A (0-3276.8A)
423 INT16 chargeUsed_A; // Integrated mAh used, 1mAh (0-32.766Ah)
424 UINT16 temp_A; // Temperature, 0.1C (0-150C, 0x7FFF indicates not populated)
425 INT16 current_B; // Instantaneous current, 0.1A (0-3276.8A)
426 INT16 chargeUsed_B; // Integrated mAh used, 1mAh (0-32.766Ah)
427 UINT16 temp_B; // Temperature, 0.1C (0-150C, 0x7FFF indicates not populated)
428 UINT16 spare; // Not used
429 } STRU_TELE_FP_MAH;
431 #define STRU_TELE_FP_EMPTY_FIELDS_COUNT 2
432 #define STRU_TELE_FP_EMPTY_FIELDS_VALUE 0xff
434 #define SPEKTRUM_AMPS_UNUSED 0x7fff
435 #define SPEKTRUM_AMPH_UNUSED 0x7fff
437 #define FP_MAH_KEEPALIVE_TIME_OUT 2000000 // 2s
439 bool srxlFrameFlightPackCurrent(sbuf_t *dst, timeUs_t currentTimeUs)
441 uint16_t amps = getAmperage() / 10;
442 uint16_t mah = getMAhDrawn();
443 static uint16_t sentAmps;
444 static uint16_t sentMah;
445 static timeUs_t lastTimeSentFPmAh = 0;
447 timeUs_t keepAlive = currentTimeUs - lastTimeSentFPmAh;
449 if ( amps != sentAmps ||
450 mah != sentMah ||
451 keepAlive > FP_MAH_KEEPALIVE_TIME_OUT ) {
453 sbufWriteU8(dst, SRXL_FRAMETYPE_TELE_FP_MAH);
454 sbufWriteU8(dst, SRXL_FRAMETYPE_SID);
455 sbufWriteU16(dst, amps);
456 sbufWriteU16(dst, mah);
457 sbufWriteU16(dst, SPEKTRUM_TEMP_UNUSED); // temp A
458 sbufWriteU16(dst, SPEKTRUM_AMPS_UNUSED); // Amps B
459 sbufWriteU16(dst, SPEKTRUM_AMPH_UNUSED); // mAH B
460 sbufWriteU16(dst, SPEKTRUM_TEMP_UNUSED); // temp B
462 sbufFill(dst, STRU_TELE_FP_EMPTY_FIELDS_VALUE, STRU_TELE_FP_EMPTY_FIELDS_COUNT);
464 sentAmps = amps;
465 sentMah = mah;
466 lastTimeSentFPmAh = currentTimeUs;
467 return true;
469 return false;
472 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
474 // Betaflight CMS using Spektrum Tx telemetry TEXT_GEN sensor as display.
476 #define SPEKTRUM_SRXL_DEVICE_TEXTGEN (0x0C) // Text Generator
477 #define SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS (9) // Text Generator ROWS
478 #define SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS (13) // Text Generator COLS
481 typedef struct
483 UINT8 identifier;
484 UINT8 sID; // Secondary ID
485 UINT8 lineNumber; // Line number to display (0 = title, 1-8 for general, 254 = Refresh backlight, 255 = Erase all text on screen)
486 char text[13]; // 0-terminated text when < 13 chars
487 } STRU_SPEKTRUM_SRXL_TEXTGEN;
490 #if ( SPEKTRUM_SRXL_TEXTGEN_BUFFER_COLS > SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS )
491 static char srxlTextBuff[SPEKTRUM_SRXL_TEXTGEN_BUFFER_ROWS][SPEKTRUM_SRXL_TEXTGEN_BUFFER_COLS];
492 static bool lineSent[SPEKTRUM_SRXL_TEXTGEN_BUFFER_ROWS];
493 #else
494 static char srxlTextBuff[SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS][SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS];
495 static bool lineSent[SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS];
496 #endif
498 //**************************************************************************
499 // API Running in external client task context. E.g. in the CMS task
500 int spektrumTmTextGenPutChar(uint8_t col, uint8_t row, char c)
502 if (row < SPEKTRUM_SRXL_TEXTGEN_BUFFER_ROWS && col < SPEKTRUM_SRXL_TEXTGEN_BUFFER_COLS) {
503 // Only update and force a tm transmision if something has actually changed.
504 if (srxlTextBuff[row][col] != c) {
505 srxlTextBuff[row][col] = c;
506 lineSent[row] = false;
509 return 0;
511 //**************************************************************************
513 bool srxlFrameText(sbuf_t *dst, timeUs_t currentTimeUs)
515 UNUSED(currentTimeUs);
516 static uint8_t lineNo = 0;
517 int lineCount = 0;
519 // Skip already sent lines...
520 while (lineSent[lineNo] &&
521 lineCount < SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS) {
522 lineNo = (lineNo + 1) % SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS;
523 lineCount++;
526 sbufWriteU8(dst, SPEKTRUM_SRXL_DEVICE_TEXTGEN);
527 sbufWriteU8(dst, SRXL_FRAMETYPE_SID);
528 sbufWriteU8(dst, lineNo);
529 sbufWriteData(dst, srxlTextBuff[lineNo], SPEKTRUM_SRXL_DEVICE_TEXTGEN_COLS);
531 lineSent[lineNo] = true;
532 lineNo = (lineNo + 1) % SPEKTRUM_SRXL_DEVICE_TEXTGEN_ROWS;
534 // Always send something, Always one user frame after the two mandatory frames
535 // I.e. All of the three frame prep routines QOS, RPM, TEXT should always return true
536 // too keep the "Waltz" sequence intact.
537 return true;
539 #endif
541 #if defined(USE_SPEKTRUM_VTX_TELEMETRY) && defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
543 static uint8_t vtxDeviceType;
545 static void collectVtxTmData(spektrumVtx_t * vtx)
547 const vtxDevice_t *vtxDevice = vtxCommonDevice();
548 vtxDeviceType = vtxCommonGetDeviceType(vtxDevice);
550 // Collect all data from VTX, if VTX is ready
551 unsigned vtxStatus;
552 if (vtxDevice == NULL || !(vtxCommonGetBandAndChannel(vtxDevice, &vtx->band, &vtx->channel) &&
553 vtxCommonGetStatus(vtxDevice, &vtxStatus) &&
554 vtxCommonGetPowerIndex(vtxDevice, &vtx->power)) )
556 vtx->band = 0;
557 vtx->channel = 0;
558 vtx->power = 0;
559 vtx->pitMode = 0;
560 } else {
561 vtx->pitMode = (vtxStatus & VTX_STATUS_PIT_MODE) ? 1 : 0;
564 vtx->powerValue = 0;
565 #ifdef USE_SPEKTRUM_REGION_CODES
566 vtx->region = SpektrumRegion;
567 #else
568 vtx->region = SPEKTRUM_VTX_REGION_NONE;
569 #endif
572 // Reverse lookup, device power index to Spektrum power range index.
573 static void convertVtxPower(spektrumVtx_t * vtx)
575 uint8_t const * powerIndexTable = NULL;
577 vtxCommonLookupPowerValue(vtxCommonDevice(), vtx->power, &vtx->powerValue);
578 switch (vtxDeviceType) {
580 #if defined(USE_VTX_TRAMP)
581 case VTXDEV_TRAMP:
582 powerIndexTable = vtxTrampPi;
583 break;
584 #endif
585 #if defined(USE_VTX_SMARTAUDIO)
586 case VTXDEV_SMARTAUDIO:
587 powerIndexTable = vtxSaPi;
588 break;
589 #endif
590 #if defined(USE_VTX_RTC6705)
591 case VTXDEV_RTC6705:
592 powerIndexTable = vtxRTC6705Pi;
593 break;
594 #endif
596 case VTXDEV_UNKNOWN:
597 case VTXDEV_UNSUPPORTED:
598 default:
599 break;
603 if (powerIndexTable != NULL) {
604 for (int i = 0; i < SPEKTRUM_VTX_POWER_COUNT; i++)
605 if (powerIndexTable[i] >= vtx->power) {
606 vtx->power = i; // Translate device power index to Spektrum power index.
607 break;
612 static void convertVtxTmData(spektrumVtx_t * vtx)
614 // Convert from internal band indexes to Spektrum indexes
615 for (int i = 0; i < SPEKTRUM_VTX_BAND_COUNT; i++) {
616 if (spek2commonBand[i] == vtx->band) {
617 vtx->band = i;
618 break;
622 // De-bump channel no 1 based interally, 0-based in Spektrum.
623 vtx->channel--;
625 // Convert Power index to Spektrum ranges, different per brand.
626 convertVtxPower(vtx);
630 typedef struct
632 UINT8 identifier;
633 UINT8 sID; // Secondary ID
634 UINT8 band; // VTX Band (0 = Fatshark, 1 = Raceband, 2 = E, 3 = B, 4 = A, 5-7 = Reserved)
635 UINT8 channel; // VTX Channel (0-7)
636 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
637 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)
638 UINT16 powerDec; // VTX Power as a decimal 1mw/unit
639 UINT8 region; // Region (0 = USA, 1 = EU, 0xFF = N/A)
640 UINT8 rfu[7]; // reserved
641 } STRU_TELE_VTX;
644 #define STRU_TELE_VTX_EMPTY_COUNT 7
645 #define STRU_TELE_VTX_EMPTY_VALUE 0xff
647 #define VTX_KEEPALIVE_TIME_OUT 2000000 // uS
649 static bool srxlFrameVTX(sbuf_t *dst, timeUs_t currentTimeUs)
651 static timeUs_t lastTimeSentVtx = 0;
652 static spektrumVtx_t vtxSent;
654 spektrumVtx_t vtx;
655 collectVtxTmData(&vtx);
657 if ((vtxDeviceType != VTXDEV_UNKNOWN) && vtxDeviceType != VTXDEV_UNSUPPORTED) {
658 convertVtxTmData(&vtx);
660 if ((memcmp(&vtxSent, &vtx, sizeof(spektrumVtx_t)) != 0) ||
661 ((currentTimeUs - lastTimeSentVtx) > VTX_KEEPALIVE_TIME_OUT) ) {
662 // Fill in the VTX tm structure
663 sbufWriteU8(dst, TELE_DEVICE_VTX);
664 sbufWriteU8(dst, SRXL_FRAMETYPE_SID);
665 sbufWriteU8(dst, vtx.band);
666 sbufWriteU8(dst, vtx.channel);
667 sbufWriteU8(dst, vtx.pitMode);
668 sbufWriteU8(dst, vtx.power);
669 sbufWriteU16(dst, vtx.powerValue);
670 sbufWriteU8(dst, vtx.region);
672 sbufFill(dst, STRU_TELE_VTX_EMPTY_VALUE, STRU_TELE_VTX_EMPTY_COUNT);
674 memcpy(&vtxSent, &vtx, sizeof(spektrumVtx_t));
675 lastTimeSentVtx = currentTimeUs;
676 return true;
679 return false;
681 #endif // USE_SPEKTRUM_VTX_TELEMETRY && USE_SPEKTRUM_VTX_CONTROL && USE_VTX_COMMON
684 // Schedule array to decide how often each type of frame is sent
685 // The frames are scheduled in sets of 3 frames, 2 mandatory and 1 user frame.
686 // The user frame type is cycled for each set.
687 // Example. QOS, RPM,.CURRENT, QOS, RPM, TEXT. QOS, RPM, CURRENT, etc etc
689 #define SRXL_SCHEDULE_MANDATORY_COUNT 2 // Mandatory QOS and RPM sensors
691 #define SRXL_FP_MAH_COUNT 1
693 #if defined(USE_GPS)
694 #define SRXL_GPS_LOC_COUNT 1
695 #define SRXL_GPS_STAT_COUNT 1
696 #else
697 #define SRXL_GPS_LOC_COUNT 0
698 #define SRXL_GPS_STAT_COUNT 0
699 #endif
701 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
702 #define SRXL_SCHEDULE_CMS_COUNT 1
703 #else
704 #define SRXL_SCHEDULE_CMS_COUNT 0
705 #endif
707 #if defined(USE_SPEKTRUM_VTX_TELEMETRY) && defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
708 #define SRXL_VTX_TM_COUNT 1
709 #else
710 #define SRXL_VTX_TM_COUNT 0
711 #endif
713 #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)
714 #define SRXL_SCHEDULE_COUNT_MAX (SRXL_SCHEDULE_MANDATORY_COUNT + 1)
715 #define SRXL_TOTAL_COUNT (SRXL_SCHEDULE_MANDATORY_COUNT + SRXL_SCHEDULE_USER_COUNT)
717 typedef bool (*srxlScheduleFnPtr)(sbuf_t *dst, timeUs_t currentTimeUs);
719 const srxlScheduleFnPtr srxlScheduleFuncs[SRXL_TOTAL_COUNT] = {
720 /* must send srxlFrameQos, Rpm and then alternating items of our own */
721 srxlFrameQos,
722 srxlFrameRpm,
723 srxlFrameFlightPackCurrent,
724 #if defined(USE_GPS)
725 srxlFrameGpsStat,
726 srxlFrameGpsLoc,
727 #endif
728 #if defined(USE_SPEKTRUM_VTX_TELEMETRY) && defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
729 srxlFrameVTX,
730 #endif
731 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
732 srxlFrameText,
733 #endif
737 static void processSrxl(timeUs_t currentTimeUs)
739 static uint8_t srxlScheduleIndex = 0;
740 static uint8_t srxlScheduleUserIndex = 0;
742 sbuf_t srxlPayloadBuf;
743 sbuf_t *dst = &srxlPayloadBuf;
744 srxlScheduleFnPtr srxlFnPtr;
746 if (srxlScheduleIndex < SRXL_SCHEDULE_MANDATORY_COUNT) {
747 srxlFnPtr = srxlScheduleFuncs[srxlScheduleIndex];
748 } else {
749 srxlFnPtr = srxlScheduleFuncs[srxlScheduleIndex + srxlScheduleUserIndex];
750 srxlScheduleUserIndex = (srxlScheduleUserIndex + 1) % SRXL_SCHEDULE_USER_COUNT;
752 #if defined (USE_SPEKTRUM_CMS_TELEMETRY) && defined (USE_CMS)
753 // Boost CMS performance by sending nothing else but CMS Text frames when in a CMS menu.
754 // Sideeffect, all other reports are still not sent if user leaves CMS without a proper EXIT.
755 if (cmsInMenu &&
756 (cmsDisplayPortGetCurrent() == &srxlDisplayPort)) {
757 srxlFnPtr = srxlFrameText;
759 #endif
763 if (srxlFnPtr) {
764 srxlInitializeFrame(dst);
765 if (srxlFnPtr(dst, currentTimeUs)) {
766 srxlFinalize(dst);
769 srxlScheduleIndex = (srxlScheduleIndex + 1) % SRXL_SCHEDULE_COUNT_MAX;
772 void initSrxlTelemetry(void)
774 // check if there is a serial port open for SRXL telemetry (ie opened by the SRXL RX)
775 // and feature is enabled, if so, set SRXL telemetry enabled
776 if (srxlRxIsActive()) {
777 srxlTelemetryEnabled = true;
778 srxl2 = false;
779 #if defined(USE_SERIALRX_SRXL2)
780 } else if (srxl2RxIsActive()) {
781 srxlTelemetryEnabled = true;
782 srxl2 = true;
783 #endif
784 } else {
785 srxlTelemetryEnabled = false;
786 srxl2 = false;
790 bool checkSrxlTelemetryState(void)
792 return srxlTelemetryEnabled;
796 * Called periodically by the scheduler
798 void handleSrxlTelemetry(timeUs_t currentTimeUs)
800 if (srxl2) {
801 #if defined(USE_SERIALRX_SRXL2)
802 if (srxl2TelemetryRequested()) {
803 processSrxl(currentTimeUs);
805 #endif
806 } else {
807 if (srxlTelemetryBufferEmpty()) {
808 processSrxl(currentTimeUs);
812 #endif