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/>.
23 #ifdef USE_SERIALRX_SPEKTRUM
27 #include "common/maths.h"
29 #include "build/debug.h"
31 #include "drivers/io.h"
32 #include "drivers/io_impl.h"
33 #include "drivers/light_led.h"
34 #include "drivers/system.h"
35 #include "drivers/time.h"
36 #include "drivers/serial.h"
37 #include "drivers/serial_uart.h"
39 #include "io/serial.h"
41 #include "config/config.h"
43 #include "io/spektrum_rssi.h"
44 #include "io/spektrum_vtx_control.h"
46 #include "telemetry/telemetry.h"
47 #include "telemetry/srxl.h"
52 #include "rx/spektrum.h"
54 #include "config/feature.h"
56 // driver for spektrum satellite receiver / sbus
58 #define SPEKTRUM_TELEMETRY_FRAME_DELAY_US 1000 // Gap between received Rc frame and transmited TM frame
60 bool srxlEnabled
= false;
64 static uint8_t spek_chan_shift
;
65 static uint8_t spek_chan_mask
;
66 static bool rcFrameComplete
= false;
67 static bool spekHiRes
= false;
69 static volatile uint8_t spekFrame
[SPEK_FRAME_SIZE
];
71 static rxRuntimeState_t
*rxRuntimeStatePtr
;
72 static serialPort_t
*serialPort
;
74 #if defined(USE_TELEMETRY_SRXL)
75 static uint8_t telemetryBuf
[SRXL_FRAME_SIZE_MAX
];
76 static uint8_t telemetryBufLen
= 0;
79 // Receive ISR callback
80 static void spektrumDataReceive(uint16_t c
, void *data
)
82 rxRuntimeState_t
*const rxRuntimeState
= (rxRuntimeState_t
*const)data
;
84 static timeUs_t spekTimeLast
= 0;
85 static uint8_t spekFramePosition
= 0;
87 const timeUs_t now
= microsISR();
88 const timeUs_t spekTimeInterval
= cmpTimeUs(now
, spekTimeLast
);
91 if (spekTimeInterval
> SPEKTRUM_NEEDED_FRAME_INTERVAL
) {
92 spekFramePosition
= 0;
95 if (spekFramePosition
< SPEK_FRAME_SIZE
) {
96 spekFrame
[spekFramePosition
++] = (uint8_t)c
;
97 if (spekFramePosition
< SPEK_FRAME_SIZE
) {
98 rcFrameComplete
= false;
100 rxRuntimeState
->lastRcFrameTimeUs
= now
;
101 rcFrameComplete
= true;
106 uint32_t spekChannelData
[SPEKTRUM_MAX_SUPPORTED_CHANNEL_COUNT
];
108 static uint8_t spektrumFrameStatus(rxRuntimeState_t
*rxRuntimeState
)
110 UNUSED(rxRuntimeState
);
112 #if defined(USE_TELEMETRY_SRXL)
113 static timeUs_t telemetryFrameRequestedUs
= 0;
115 timeUs_t currentTimeUs
= micros();
118 uint8_t result
= RX_FRAME_PENDING
;
120 if (rcFrameComplete
) {
121 rcFrameComplete
= false;
123 #if defined(USE_SPEKTRUM_REAL_RSSI) || defined(USE_SPEKTRUM_VIRTUAL_RSSI)
124 spektrumHandleRSSI(spekFrame
);
127 // Get the VTX control bytes in a frame
128 uint32_t vtxControl
= ((spekFrame
[SPEKTRUM_VTX_CONTROL_1
] << 24) |
129 (spekFrame
[SPEKTRUM_VTX_CONTROL_2
] << 16) |
130 (spekFrame
[SPEKTRUM_VTX_CONTROL_3
] << 8) |
131 (spekFrame
[SPEKTRUM_VTX_CONTROL_4
] << 0) );
133 int8_t spektrumRcDataSize
;
134 // Handle VTX control frame.
135 if ((vtxControl
& SPEKTRUM_VTX_CONTROL_FRAME_MASK
) == SPEKTRUM_VTX_CONTROL_FRAME
&&
136 (spekFrame
[2] & 0x80) == 0 ) {
137 #if defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
138 spektrumHandleVtxControl(vtxControl
);
140 spektrumRcDataSize
= SPEK_FRAME_SIZE
- SPEKTRUM_VTX_CONTROL_SIZE
;
142 spektrumRcDataSize
= SPEK_FRAME_SIZE
;
145 // Get the RC control channel inputs
146 for (int b
= 3; b
< spektrumRcDataSize
; b
+= 2) {
147 const uint8_t spekChannel
= 0x0F & (spekFrame
[b
- 1] >> spek_chan_shift
);
148 if (spekChannel
< rxRuntimeStatePtr
->channelCount
&& spekChannel
< SPEKTRUM_MAX_SUPPORTED_CHANNEL_COUNT
) {
149 if (rssi_channel
== 0 || spekChannel
!= rssi_channel
) {
150 spekChannelData
[spekChannel
] = ((uint32_t)(spekFrame
[b
- 1] & spek_chan_mask
) << 8) + spekFrame
[b
];
155 #if defined(USE_TELEMETRY_SRXL)
156 if (srxlEnabled
&& (spekFrame
[2] & 0x80) == 0) {
157 telemetryFrameRequestedUs
= currentTimeUs
;
160 result
= RX_FRAME_COMPLETE
;
163 #if defined(USE_TELEMETRY_SRXL)
164 if (telemetryBufLen
&& telemetryFrameRequestedUs
&& cmpTimeUs(currentTimeUs
, telemetryFrameRequestedUs
) >= SPEKTRUM_TELEMETRY_FRAME_DELAY_US
) {
165 telemetryFrameRequestedUs
= 0;
167 result
= (result
& ~RX_FRAME_PENDING
) | RX_FRAME_PROCESSING_REQUIRED
;
174 static float spektrumReadRawRC(const rxRuntimeState_t
*rxRuntimeState
, uint8_t chan
)
178 if (chan
>= rxRuntimeState
->channelCount
) {
183 data
= 0.5f
* (float)spekChannelData
[chan
] + 988; // 2048 mode
185 data
= spekChannelData
[chan
] + 988; // 1024 mode
191 #ifdef USE_SPEKTRUM_BIND
193 bool spekShouldBind(uint8_t spektrum_sat_bind
)
195 #ifdef USE_SPEKTRUM_BIND_PLUG
196 IO_t BindPlug
= IOGetByTag(rxConfig()->spektrum_bind_plug_ioTag
);
199 IOInit(BindPlug
, OWNER_RX_BIND
, 0);
200 IOConfigGPIO(BindPlug
, IOCFG_IPU
);
202 // Check status of bind plug and exit if not active
203 delayMicroseconds(10); // allow configuration to settle
204 if (IORead(BindPlug
)) {
208 #endif // USE_SPEKTRUM_BIND_PLUG
212 spektrum_sat_bind
== SPEKTRUM_SAT_BIND_DISABLED
||
213 spektrum_sat_bind
> SPEKTRUM_SAT_BIND_MAX
217 /* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX.
218 * Function must be called immediately after startup so that we don't miss satellite bind window.
219 * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX.
220 * 9 = DSMX 11ms / DSMX 22ms
221 * 5 = DSM2 11ms 2048 / DSM2 22ms 1024
223 void spektrumBind(rxConfig_t
*rxConfig
)
225 if (!spekShouldBind(rxConfig
->spektrum_sat_bind
)) {
229 // Determine a pin to use
230 ioTag_t bindPin
= IO_TAG_NONE
;
232 if (rxConfig
->spektrum_bind_pin_override_ioTag
) {
233 bindPin
= rxConfig
->spektrum_bind_pin_override_ioTag
;
235 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
239 #if defined(USE_UART) || defined(USE_LPUART) || defined(USE_SOFTSERIAL)
240 const int resourceIndex
= serialResourceIndex(portConfig
->identifier
);
241 const ioTag_t txPin
= serialPinConfig()->ioTagTx
[resourceIndex
];
242 const ioTag_t rxPin
= serialPinConfig()->ioTagRx
[resourceIndex
];
244 // Take care half-duplex case
245 switch (rxRuntimeState
.serialrxProvider
) {
247 #if defined(USE_TELEMETRY_SRXL)
248 if (featureIsEnabled(FEATURE_TELEMETRY
) && !telemetryCheckRxPortShared(portConfig
, rxRuntimeState
.serialrxProvider
)) {
252 #endif // USE_TELEMETRY_SRXL
255 bindPin
= rxConfig
->halfDuplex
? txPin
: rxPin
;
263 IO_t bindIO
= IOGetByTag(bindPin
);
265 IOInit(bindIO
, OWNER_RX_BIND
, 0);
266 IOConfigGPIO(bindIO
, IOCFG_OUT_PP
);
271 IOWrite(bindIO
, true);
273 // Bind window is around 20-140ms after powerup
277 for (int i
= 0; i
< rxConfig
->spektrum_sat_bind
; i
++) {
280 // RX line, drive low for 120us
281 IOWrite(bindIO
, false);
282 delayMicroseconds(120);
286 // RX line, drive high for 120us
287 IOWrite(bindIO
, true);
288 delayMicroseconds(120);
292 // Release the bind pin to avoid interference with an actual rx pin,
293 // when rxConfig->spektrum_bind_pin_override_ioTag is used.
294 // This happens when the bind pin is connected in parallel to the rx pin.
296 if (rxConfig
->spektrum_bind_pin_override_ioTag
) {
297 delay(50); // Keep it high for 50msec
298 IOConfigGPIO(bindIO
, IOCFG_IN_FLOATING
);
301 // If we came here as a result of hard reset (power up, with spektrum_sat_bind set), then reset it back to zero and write config
302 // Don't reset if hardware bind plug is present
303 // Reset only when autoreset is enabled
305 if (!rxConfig
->spektrum_bind_plug_ioTag
&& rxConfig
->spektrum_sat_bind_autoreset
== 1 && !isMPUSoftReset()) {
306 rxConfig
->spektrum_sat_bind
= 0;
307 saveConfigAndNotify();
310 #endif // USE_SPEKTRUM_BIND
312 #if defined(USE_TELEMETRY_SRXL)
313 static bool spektrumProcessFrame(const rxRuntimeState_t
*rxRuntimeState
)
315 UNUSED(rxRuntimeState
);
317 // if there is telemetry data to write
318 if (telemetryBufLen
> 0) {
319 serialWriteBuf(serialPort
, telemetryBuf
, telemetryBufLen
);
320 telemetryBufLen
= 0; // reset telemetry buffer
326 bool srxlTelemetryBufferEmpty(void)
328 if (telemetryBufLen
== 0) {
335 void srxlRxWriteTelemetryData(const void *data
, int len
)
337 len
= MIN(len
, (int)sizeof(telemetryBuf
));
338 memcpy(telemetryBuf
, data
, len
);
339 telemetryBufLen
= len
;
343 bool spektrumInit(const rxConfig_t
*rxConfig
, rxRuntimeState_t
*rxRuntimeState
)
345 rxRuntimeStatePtr
= rxRuntimeState
;
347 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
353 #if defined(USE_TELEMETRY_SRXL)
354 bool portShared
= telemetryCheckRxPortShared(portConfig
, rxRuntimeState
->serialrxProvider
);
356 bool portShared
= false;
359 switch (rxRuntimeState
->serialrxProvider
) {
364 #if defined(USE_TELEMETRY_SRXL)
365 srxlEnabled
= (featureIsEnabled(FEATURE_TELEMETRY
) && !portShared
);
368 case SERIALRX_SPEKTRUM2048
:
371 spek_chan_mask
= 0x07;
374 rxRuntimeState
->channelCount
= SPEKTRUM_2048_CHANNEL_COUNT
;
376 case SERIALRX_SPEKTRUM1024
:
379 spek_chan_mask
= 0x03;
382 rxRuntimeState
->channelCount
= SPEKTRUM_1024_CHANNEL_COUNT
;
386 rxRuntimeState
->rcReadRawFn
= spektrumReadRawRC
;
387 rxRuntimeState
->rcFrameStatusFn
= spektrumFrameStatus
;
388 #if defined(USE_TELEMETRY_SRXL)
389 rxRuntimeState
->rcProcessFrameFn
= spektrumProcessFrame
;
392 serialPort
= openSerialPort(portConfig
->identifier
,
397 portShared
|| srxlEnabled
? MODE_RXTX
: MODE_RX
,
398 (rxConfig
->serialrx_inverted
? SERIAL_INVERTED
: 0) |
399 ((srxlEnabled
|| rxConfig
->halfDuplex
) ? SERIAL_BIDIR
: 0)
402 #if defined(USE_TELEMETRY_SRXL)
404 telemetrySharedPort
= serialPort
;
408 rssi_channel
= rxConfig
->rssi_channel
- 1; // -1 because rxConfig->rssi_channel is 1-based and rssi_channel is 0-based.
409 if (rssi_channel
>= rxRuntimeState
->channelCount
) {
413 return serialPort
!= NULL
;
416 bool srxlRxIsActive(void)
418 return serialPort
!= NULL
;