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;
107 uint32_t spekChannelData
[SPEKTRUM_MAX_SUPPORTED_CHANNEL_COUNT
];
109 static uint8_t spektrumFrameStatus(rxRuntimeState_t
*rxRuntimeState
)
111 UNUSED(rxRuntimeState
);
113 #if defined(USE_TELEMETRY_SRXL)
114 static timeUs_t telemetryFrameRequestedUs
= 0;
116 timeUs_t currentTimeUs
= micros();
119 uint8_t result
= RX_FRAME_PENDING
;
121 if (rcFrameComplete
) {
122 rcFrameComplete
= false;
124 #if defined(USE_SPEKTRUM_REAL_RSSI) || defined(USE_SPEKTRUM_FAKE_RSSI)
125 spektrumHandleRSSI(spekFrame
);
128 // Get the VTX control bytes in a frame
129 uint32_t vtxControl
= ((spekFrame
[SPEKTRUM_VTX_CONTROL_1
] << 24) |
130 (spekFrame
[SPEKTRUM_VTX_CONTROL_2
] << 16) |
131 (spekFrame
[SPEKTRUM_VTX_CONTROL_3
] << 8) |
132 (spekFrame
[SPEKTRUM_VTX_CONTROL_4
] << 0) );
134 int8_t spektrumRcDataSize
;
135 // Handle VTX control frame.
136 if ((vtxControl
& SPEKTRUM_VTX_CONTROL_FRAME_MASK
) == SPEKTRUM_VTX_CONTROL_FRAME
&&
137 (spekFrame
[2] & 0x80) == 0 ) {
138 #if defined(USE_SPEKTRUM_VTX_CONTROL) && defined(USE_VTX_COMMON)
139 spektrumHandleVtxControl(vtxControl
);
141 spektrumRcDataSize
= SPEK_FRAME_SIZE
- SPEKTRUM_VTX_CONTROL_SIZE
;
143 spektrumRcDataSize
= SPEK_FRAME_SIZE
;
146 // Get the RC control channel inputs
147 for (int b
= 3; b
< spektrumRcDataSize
; b
+= 2) {
148 const uint8_t spekChannel
= 0x0F & (spekFrame
[b
- 1] >> spek_chan_shift
);
149 if (spekChannel
< rxRuntimeStatePtr
->channelCount
&& spekChannel
< SPEKTRUM_MAX_SUPPORTED_CHANNEL_COUNT
) {
150 if (rssi_channel
== 0 || spekChannel
!= rssi_channel
) {
151 spekChannelData
[spekChannel
] = ((uint32_t)(spekFrame
[b
- 1] & spek_chan_mask
) << 8) + spekFrame
[b
];
156 #if defined(USE_TELEMETRY_SRXL)
157 if (srxlEnabled
&& (spekFrame
[2] & 0x80) == 0) {
158 telemetryFrameRequestedUs
= currentTimeUs
;
161 result
= RX_FRAME_COMPLETE
;
164 #if defined(USE_TELEMETRY_SRXL)
165 if (telemetryBufLen
&& telemetryFrameRequestedUs
&& cmpTimeUs(currentTimeUs
, telemetryFrameRequestedUs
) >= SPEKTRUM_TELEMETRY_FRAME_DELAY_US
) {
166 telemetryFrameRequestedUs
= 0;
168 result
= (result
& ~RX_FRAME_PENDING
) | RX_FRAME_PROCESSING_REQUIRED
;
175 static float spektrumReadRawRC(const rxRuntimeState_t
*rxRuntimeState
, uint8_t chan
)
179 if (chan
>= rxRuntimeState
->channelCount
) {
184 data
= 0.5f
* (float)spekChannelData
[chan
] + 988; // 2048 mode
186 data
= spekChannelData
[chan
] + 988; // 1024 mode
192 #ifdef USE_SPEKTRUM_BIND
194 bool spekShouldBind(uint8_t spektrum_sat_bind
)
196 #ifdef USE_SPEKTRUM_BIND_PLUG
197 IO_t BindPlug
= IOGetByTag(rxConfig()->spektrum_bind_plug_ioTag
);
200 IOInit(BindPlug
, OWNER_RX_BIND
, 0);
201 IOConfigGPIO(BindPlug
, IOCFG_IPU
);
203 // Check status of bind plug and exit if not active
204 delayMicroseconds(10); // allow configuration to settle
205 if (IORead(BindPlug
)) {
209 #endif // USE_SPEKTRUM_BIND_PLUG
213 spektrum_sat_bind
== SPEKTRUM_SAT_BIND_DISABLED
||
214 spektrum_sat_bind
> SPEKTRUM_SAT_BIND_MAX
218 /* spektrumBind function ported from Baseflight. It's used to bind satellite receiver to TX.
219 * Function must be called immediately after startup so that we don't miss satellite bind window.
220 * Known parameters. Tested with DSMX satellite and DX8 radio. Framerate (11ms or 22ms) must be selected from TX.
221 * 9 = DSMX 11ms / DSMX 22ms
222 * 5 = DSM2 11ms 2048 / DSM2 22ms 1024
224 void spektrumBind(rxConfig_t
*rxConfig
)
226 if (!spekShouldBind(rxConfig
->spektrum_sat_bind
)) {
230 // Determine a pin to use
231 ioTag_t bindPin
= IO_TAG_NONE
;
233 if (rxConfig
->spektrum_bind_pin_override_ioTag
) {
234 bindPin
= rxConfig
->spektrum_bind_pin_override_ioTag
;
236 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
241 int index
= SERIAL_PORT_IDENTIFIER_TO_INDEX(portConfig
->identifier
);
242 ioTag_t txPin
= serialPinConfig()->ioTagTx
[index
];
243 ioTag_t rxPin
= serialPinConfig()->ioTagRx
[index
];
245 // Take care half-duplex case
246 switch (rxRuntimeState
.serialrxProvider
) {
248 #if defined(USE_TELEMETRY_SRXL)
249 if (featureIsEnabled(FEATURE_TELEMETRY
) && !telemetryCheckRxPortShared(portConfig
, rxRuntimeState
.serialrxProvider
)) {
253 #endif // USE_TELEMETRY_SRXL
256 bindPin
= rxConfig
->halfDuplex
? txPin
: rxPin
;
264 IO_t bindIO
= IOGetByTag(bindPin
);
266 IOInit(bindIO
, OWNER_RX_BIND
, 0);
267 IOConfigGPIO(bindIO
, IOCFG_OUT_PP
);
272 IOWrite(bindIO
, true);
274 // Bind window is around 20-140ms after powerup
278 for (int i
= 0; i
< rxConfig
->spektrum_sat_bind
; i
++) {
281 // RX line, drive low for 120us
282 IOWrite(bindIO
, false);
283 delayMicroseconds(120);
287 // RX line, drive high for 120us
288 IOWrite(bindIO
, true);
289 delayMicroseconds(120);
294 // Release the bind pin to avoid interference with an actual rx pin,
295 // when rxConfig->spektrum_bind_pin_override_ioTag is used.
296 // This happens when the bind pin is connected in parallel to the rx pin.
298 if (rxConfig
->spektrum_bind_pin_override_ioTag
) {
299 delay(50); // Keep it high for 50msec
300 IOConfigGPIO(bindIO
, IOCFG_IN_FLOATING
);
303 // 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
304 // Don't reset if hardware bind plug is present
305 // Reset only when autoreset is enabled
307 if (!rxConfig
->spektrum_bind_plug_ioTag
&& rxConfig
->spektrum_sat_bind_autoreset
== 1 && !isMPUSoftReset()) {
308 rxConfig
->spektrum_sat_bind
= 0;
309 saveConfigAndNotify();
312 #endif // USE_SPEKTRUM_BIND
314 #if defined(USE_TELEMETRY_SRXL)
315 static bool spektrumProcessFrame(const rxRuntimeState_t
*rxRuntimeState
)
317 UNUSED(rxRuntimeState
);
319 // if there is telemetry data to write
320 if (telemetryBufLen
> 0) {
321 serialWriteBuf(serialPort
, telemetryBuf
, telemetryBufLen
);
322 telemetryBufLen
= 0; // reset telemetry buffer
328 bool srxlTelemetryBufferEmpty()
330 if (telemetryBufLen
== 0) {
337 void srxlRxWriteTelemetryData(const void *data
, int len
)
339 len
= MIN(len
, (int)sizeof(telemetryBuf
));
340 memcpy(telemetryBuf
, data
, len
);
341 telemetryBufLen
= len
;
345 bool spektrumInit(const rxConfig_t
*rxConfig
, rxRuntimeState_t
*rxRuntimeState
)
347 rxRuntimeStatePtr
= rxRuntimeState
;
349 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
355 #if defined(USE_TELEMETRY_SRXL)
356 bool portShared
= telemetryCheckRxPortShared(portConfig
, rxRuntimeState
->serialrxProvider
);
358 bool portShared
= false;
361 switch (rxRuntimeState
->serialrxProvider
) {
366 #if defined(USE_TELEMETRY_SRXL)
367 srxlEnabled
= (featureIsEnabled(FEATURE_TELEMETRY
) && !portShared
);
370 case SERIALRX_SPEKTRUM2048
:
373 spek_chan_mask
= 0x07;
376 rxRuntimeState
->channelCount
= SPEKTRUM_2048_CHANNEL_COUNT
;
377 rxRuntimeState
->rxRefreshRate
= 11000;
379 case SERIALRX_SPEKTRUM1024
:
382 spek_chan_mask
= 0x03;
385 rxRuntimeState
->channelCount
= SPEKTRUM_1024_CHANNEL_COUNT
;
386 rxRuntimeState
->rxRefreshRate
= 22000;
390 rxRuntimeState
->rcReadRawFn
= spektrumReadRawRC
;
391 rxRuntimeState
->rcFrameStatusFn
= spektrumFrameStatus
;
392 rxRuntimeState
->rcFrameTimeUsFn
= rxFrameTimeUs
;
393 #if defined(USE_TELEMETRY_SRXL)
394 rxRuntimeState
->rcProcessFrameFn
= spektrumProcessFrame
;
397 serialPort
= openSerialPort(portConfig
->identifier
,
402 portShared
|| srxlEnabled
? MODE_RXTX
: MODE_RX
,
403 (rxConfig
->serialrx_inverted
? SERIAL_INVERTED
: 0) |
404 ((srxlEnabled
|| rxConfig
->halfDuplex
) ? SERIAL_BIDIR
: 0)
407 #if defined(USE_TELEMETRY_SRXL)
409 telemetrySharedPort
= serialPort
;
413 rssi_channel
= rxConfig
->rssi_channel
- 1; // -1 because rxConfig->rssi_channel is 1-based and rssi_channel is 0-based.
414 if (rssi_channel
>= rxRuntimeState
->channelCount
) {
418 return serialPort
!= NULL
;
421 bool srxlRxIsActive(void)
423 return serialPort
!= NULL
;