Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / rx / sbus.c
blob985703876548ba3ced505f333d5ca33b85fcb854
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "platform.h"
25 #ifdef USE_SERIAL_RX
27 #include "build/debug.h"
29 #include "common/utils.h"
31 #include "drivers/time.h"
33 #include "io/serial.h"
35 #ifdef USE_TELEMETRY
36 #include "telemetry/telemetry.h"
37 #endif
39 #include "rx/rx.h"
40 #include "rx/sbus.h"
41 #include "rx/sbus_channels.h"
44 * Observations
46 * FrSky X8R
47 * time between frames: 6ms.
48 * time to send frame: 3ms.
50 * Futaba R6208SB/R6303SB
51 * time between frames: 11ms.
52 * time to send frame: 3ms.
54 typedef enum {
55 STATE_SBUS_SYNC = 0,
56 STATE_SBUS_PAYLOAD,
57 STATE_SBUS_WAIT_SYNC
58 } sbusDecoderState_e;
60 typedef struct sbusFrameData_s {
61 sbusDecoderState_e state;
62 volatile sbusFrame_t frame;
63 volatile bool frameDone;
64 uint8_t buffer[SBUS_FRAME_SIZE];
65 uint8_t position;
66 timeUs_t lastActivityTimeUs;
67 } sbusFrameData_t;
69 // Receive ISR callback
70 static void sbusDataReceive(uint16_t c, void *data)
72 sbusFrameData_t *sbusFrameData = data;
73 const timeUs_t currentTimeUs = micros();
74 const timeDelta_t timeSinceLastByteUs = cmpTimeUs(currentTimeUs, sbusFrameData->lastActivityTimeUs);
75 sbusFrameData->lastActivityTimeUs = currentTimeUs;
77 // Handle inter-frame gap. We dwell in STATE_SBUS_WAIT_SYNC state ignoring all incoming bytes until we get long enough quite period on the wire
78 if (sbusFrameData->state == STATE_SBUS_WAIT_SYNC && timeSinceLastByteUs >= rxConfig()->sbusSyncInterval) {
79 sbusFrameData->state = STATE_SBUS_SYNC;
82 switch (sbusFrameData->state) {
83 case STATE_SBUS_SYNC:
84 if (c == SBUS_FRAME_BEGIN_BYTE) {
85 sbusFrameData->position = 0;
86 sbusFrameData->buffer[sbusFrameData->position++] = (uint8_t)c;
87 sbusFrameData->state = STATE_SBUS_PAYLOAD;
89 break;
91 case STATE_SBUS_PAYLOAD:
92 sbusFrameData->buffer[sbusFrameData->position++] = (uint8_t)c;
94 if (sbusFrameData->position == SBUS_FRAME_SIZE) {
95 const sbusFrame_t * frame = (sbusFrame_t *)&sbusFrameData->buffer[0];
96 bool frameValid = false;
98 // Do some sanity check
99 switch (frame->endByte) {
100 case 0x00: // This is S.BUS 1
101 case 0x04: // S.BUS 2 receiver voltage
102 case 0x14: // S.BUS 2 GPS/baro
103 case 0x24: // Unknown SBUS2 data
104 case 0x34: // Unknown SBUS2 data
105 frameValid = true;
106 sbusFrameData->state = STATE_SBUS_WAIT_SYNC;
107 break;
109 default: // Failed end marker
110 sbusFrameData->state = STATE_SBUS_WAIT_SYNC;
111 break;
114 // Frame seems sane, pass data to decoder
115 if (!sbusFrameData->frameDone && frameValid) {
117 memcpy((void *)&sbusFrameData->frame, (void *)&sbusFrameData->buffer[0], SBUS_FRAME_SIZE);
118 sbusFrameData->frameDone = true;
121 break;
123 case STATE_SBUS_WAIT_SYNC:
124 // Stay at this state and do nothing. Exit will be handled before byte is processed if the
125 // inter-frame gap is long enough
126 break;
130 static uint8_t sbusFrameStatus(rxRuntimeConfig_t *rxRuntimeConfig)
132 sbusFrameData_t *sbusFrameData = rxRuntimeConfig->frameData;
134 if (!sbusFrameData->frameDone) {
135 return RX_FRAME_PENDING;
138 // Decode channel data and store return value
139 const uint8_t retValue = sbusChannelsDecode(rxRuntimeConfig, (void *)&sbusFrameData->frame.channels);
141 // Reset the frameDone flag - tell ISR that we're ready to receive next frame
142 sbusFrameData->frameDone = false;
144 // Calculate "virtual link quality based on packet loss metric"
145 if (retValue & RX_FRAME_COMPLETE) {
146 lqTrackerAccumulate(rxRuntimeConfig->lqTracker, ((retValue & RX_FRAME_DROPPED) || (retValue & RX_FRAME_FAILSAFE)) ? 0 : RSSI_MAX_VALUE);
149 return retValue;
152 static bool sbusInitEx(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig, uint32_t sbusBaudRate)
154 static uint16_t sbusChannelData[SBUS_MAX_CHANNEL];
155 static sbusFrameData_t sbusFrameData;
157 rxRuntimeConfig->channelData = sbusChannelData;
158 rxRuntimeConfig->frameData = &sbusFrameData;
160 sbusChannelsInit(rxRuntimeConfig);
162 rxRuntimeConfig->channelCount = SBUS_MAX_CHANNEL;
164 rxRuntimeConfig->rcFrameStatusFn = sbusFrameStatus;
166 const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_RX_SERIAL);
167 if (!portConfig) {
168 return false;
171 #ifdef USE_TELEMETRY
172 bool portShared = telemetryCheckRxPortShared(portConfig);
173 #else
174 bool portShared = false;
175 #endif
177 serialPort_t *sBusPort = openSerialPort(portConfig->identifier,
178 FUNCTION_RX_SERIAL,
179 sbusDataReceive,
180 &sbusFrameData,
181 sbusBaudRate,
182 portShared ? MODE_RXTX : MODE_RX,
183 SBUS_PORT_OPTIONS |
184 (rxConfig->serialrx_inverted ? 0 : SERIAL_INVERTED) |
185 (tristateWithDefaultOffIsActive(rxConfig->halfDuplex) ? SERIAL_BIDIR : 0)
188 #ifdef USE_TELEMETRY
189 if (portShared) {
190 telemetrySharedPort = sBusPort;
192 #endif
194 return sBusPort != NULL;
197 bool sbusInit(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig)
199 return sbusInitEx(rxConfig, rxRuntimeConfig, SBUS_BAUDRATE);
202 bool sbusInitFast(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig)
204 return sbusInitEx(rxConfig, rxRuntimeConfig, SBUS_BAUDRATE_FAST);
206 #endif