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_SERIAL_RX)
30 #include "build/debug.h"
32 #include "common/maths.h"
33 #include "common/utils.h"
35 #include "drivers/time.h"
37 #include "io/serial.h"
40 #include "telemetry/telemetry.h"
41 #include "telemetry/smartport.h"
47 #include "rx/sbus_channels.h"
51 #define FPORT_TIME_NEEDED_PER_FRAME_US 3000
52 #define FPORT_MAX_TELEMETRY_RESPONSE_DELAY_US 2000
53 #define FPORT_MIN_TELEMETRY_RESPONSE_DELAY_US 500
54 #define FPORT_MAX_TELEMETRY_AGE_MS 500
57 #define FPORT_FRAME_MARKER 0x7E
59 #define FPORT_ESCAPE_CHAR 0x7D
60 #define FPORT_ESCAPE_MASK 0x20
62 #define FPORT_CRC_VALUE 0xFF
64 #define FPORT_BAUDRATE 115200
66 #define FPORT_PORT_OPTIONS (SERIAL_STOPBITS_1 | SERIAL_PARITY_NO)
69 DEBUG_FPORT_FRAME_INTERVAL
= 0,
70 DEBUG_FPORT_FRAME_ERRORS
,
71 DEBUG_FPORT_FRAME_LAST_ERROR
,
72 DEBUG_FPORT_TELEMETRY_DELAY
,
76 DEBUG_FPORT_NO_ERROR
= 0,
77 DEBUG_FPORT_ERROR_TIMEOUT
,
78 DEBUG_FPORT_ERROR_OVERSIZE
,
79 DEBUG_FPORT_ERROR_SIZE
,
80 DEBUG_FPORT_ERROR_CHECKSUM
,
81 DEBUG_FPORT_ERROR_TYPE
,
82 DEBUG_FPORT_ERROR_TYPE_SIZE
,
86 FPORT_FRAME_TYPE_CONTROL
= 0x00,
87 FPORT_FRAME_TYPE_TELEMETRY_REQUEST
= 0x01,
88 FPORT_FRAME_TYPE_TELEMETRY_RESPONSE
= 0x81,
93 FPORT_FRAME_ID_NULL
= 0x00, // (master/slave)
94 FPORT_FRAME_ID_DATA
= 0x10, // (master/slave)
95 FPORT_FRAME_ID_READ
= 0x30, // (master)
96 FPORT_FRAME_ID_WRITE
= 0x31, // (master)
97 FPORT_FRAME_ID_RESPONSE
= 0x32, // (slave)
100 typedef struct fportControlData_s
{
101 sbusChannels_t channels
;
103 } fportControlData_t
;
105 typedef union fportData_s
{
106 fportControlData_t controlData
;
107 smartPortPayload_t telemetryData
;
110 typedef struct fportFrame_s
{
115 static const smartPortPayload_t emptySmartPortFrame
= { .frameId
= 0, .valueId
= 0, .data
= 0 };
117 #define FPORT_REQUEST_FRAME_LENGTH sizeof(fportFrame_t)
118 #define FPORT_RESPONSE_FRAME_LENGTH (sizeof(uint8_t) + sizeof(smartPortPayload_t))
120 #define FPORT_FRAME_PAYLOAD_LENGTH_CONTROL (sizeof(uint8_t) + sizeof(fportControlData_t))
121 #define FPORT_FRAME_PAYLOAD_LENGTH_TELEMETRY_REQUEST (sizeof(uint8_t) + sizeof(smartPortPayload_t))
123 #define NUM_RX_BUFFERS 3
124 #define BUFFER_SIZE (FPORT_REQUEST_FRAME_LENGTH + 2 * sizeof(uint8_t))
126 typedef struct fportBuffer_s
{
127 uint8_t data
[BUFFER_SIZE
];
131 static fportBuffer_t rxBuffer
[NUM_RX_BUFFERS
];
133 static volatile uint8_t rxBufferWriteIndex
= 0;
134 static volatile uint8_t rxBufferReadIndex
= 0;
136 static volatile timeUs_t lastTelemetryFrameReceivedUs
;
137 static volatile bool clearToSend
= false;
139 static volatile uint8_t framePosition
= 0;
141 static smartPortPayload_t
*mspPayload
= NULL
;
142 static timeUs_t lastRcFrameReceivedMs
= 0;
144 static serialPort_t
*fportPort
;
145 static bool telemetryEnabled
= false;
147 static void reportFrameError(uint8_t errorReason
) {
148 static volatile uint16_t frameErrors
= 0;
150 DEBUG_SET(DEBUG_FPORT
, DEBUG_FPORT_FRAME_ERRORS
, ++frameErrors
);
151 DEBUG_SET(DEBUG_FPORT
, DEBUG_FPORT_FRAME_LAST_ERROR
, errorReason
);
154 // Receive ISR callback
155 static void fportDataReceive(uint16_t c
, void *data
)
159 static timeUs_t frameStartAt
= 0;
160 static bool escapedCharacter
= false;
161 static timeUs_t lastFrameReceivedUs
= 0;
162 static bool telemetryFrame
= false;
164 const timeUs_t currentTimeUs
= micros();
168 if (framePosition
> 1 && cmpTimeUs(currentTimeUs
, frameStartAt
) > FPORT_TIME_NEEDED_PER_FRAME_US
+ 500) {
169 reportFrameError(DEBUG_FPORT_ERROR_TIMEOUT
);
174 uint8_t val
= (uint8_t)c
;
176 if (val
== FPORT_FRAME_MARKER
) {
177 if (framePosition
> 1) {
178 const uint8_t nextWriteIndex
= (rxBufferWriteIndex
+ 1) % NUM_RX_BUFFERS
;
179 if (nextWriteIndex
!= rxBufferReadIndex
) {
180 rxBuffer
[rxBufferWriteIndex
].length
= framePosition
- 1;
181 rxBufferWriteIndex
= nextWriteIndex
;
184 if (telemetryFrame
) {
186 lastTelemetryFrameReceivedUs
= currentTimeUs
;
187 telemetryFrame
= false;
190 DEBUG_SET(DEBUG_FPORT
, DEBUG_FPORT_FRAME_INTERVAL
, currentTimeUs
- lastFrameReceivedUs
);
191 lastFrameReceivedUs
= currentTimeUs
;
193 escapedCharacter
= false;
196 frameStartAt
= currentTimeUs
;
198 } else if (framePosition
> 0) {
199 if (framePosition
>= BUFFER_SIZE
+ 1) {
202 reportFrameError(DEBUG_FPORT_ERROR_OVERSIZE
);
204 if (escapedCharacter
) {
205 val
= val
^ FPORT_ESCAPE_MASK
;
206 escapedCharacter
= false;
207 } else if (val
== FPORT_ESCAPE_CHAR
) {
208 escapedCharacter
= true;
213 if (framePosition
== 2 && val
== FPORT_FRAME_TYPE_TELEMETRY_REQUEST
) {
214 telemetryFrame
= true;
217 rxBuffer
[rxBufferWriteIndex
].data
[framePosition
- 1] = val
;
218 framePosition
= framePosition
+ 1;
223 #if defined(USE_TELEMETRY_SMARTPORT)
224 static void smartPortWriteFrameFport(const smartPortPayload_t
*payload
)
228 uint16_t checksum
= 0;
229 smartPortSendByte(FPORT_RESPONSE_FRAME_LENGTH
, &checksum
, fportPort
);
230 smartPortSendByte(FPORT_FRAME_TYPE_TELEMETRY_RESPONSE
, &checksum
, fportPort
);
231 smartPortWriteFrameSerial(payload
, fportPort
, checksum
);
235 static bool checkChecksum(uint8_t *data
, uint8_t length
)
237 uint16_t checksum
= 0;
238 for (unsigned i
= 0; i
< length
; i
++) {
239 checksum
= checksum
+ *(uint8_t *)(data
+ i
);
242 checksum
= (checksum
& 0xff) + (checksum
>> 8);
244 return checksum
== FPORT_CRC_VALUE
;
247 static uint8_t fportFrameStatus(rxRuntimeConfig_t
*rxRuntimeConfig
)
249 static smartPortPayload_t payloadBuffer
;
250 static bool hasTelemetryRequest
= false;
252 uint8_t result
= RX_FRAME_PENDING
;
254 if (rxBufferReadIndex
!= rxBufferWriteIndex
) {
255 uint8_t bufferLength
= rxBuffer
[rxBufferReadIndex
].length
;
256 uint8_t frameLength
= rxBuffer
[rxBufferReadIndex
].data
[0];
257 if (frameLength
!= bufferLength
- 2) {
258 reportFrameError(DEBUG_FPORT_ERROR_SIZE
);
260 if (!checkChecksum(&rxBuffer
[rxBufferReadIndex
].data
[0], bufferLength
)) {
261 reportFrameError(DEBUG_FPORT_ERROR_CHECKSUM
);
263 fportFrame_t
*frame
= (fportFrame_t
*)&rxBuffer
[rxBufferReadIndex
].data
[1];
265 switch (frame
->type
) {
266 case FPORT_FRAME_TYPE_CONTROL
:
267 if (frameLength
!= FPORT_FRAME_PAYLOAD_LENGTH_CONTROL
) {
268 reportFrameError(DEBUG_FPORT_ERROR_TYPE_SIZE
);
270 result
= sbusChannelsDecode(rxRuntimeConfig
, &frame
->data
.controlData
.channels
);
272 setRssi(scaleRange(frame
->data
.controlData
.rssi
, 0, 100, 0, 1024), RSSI_SOURCE_RX_PROTOCOL
);
274 lastRcFrameReceivedMs
= millis();
278 case FPORT_FRAME_TYPE_TELEMETRY_REQUEST
:
279 if (frameLength
!= FPORT_FRAME_PAYLOAD_LENGTH_TELEMETRY_REQUEST
) {
280 reportFrameError(DEBUG_FPORT_ERROR_TYPE_SIZE
);
282 #if defined(USE_TELEMETRY_SMARTPORT)
283 if (!telemetryEnabled
) {
287 switch(frame
->data
.telemetryData
.frameId
) {
288 case FPORT_FRAME_ID_NULL
:
289 case FPORT_FRAME_ID_DATA
: // never used
290 hasTelemetryRequest
= true;
293 case FPORT_FRAME_ID_READ
:
294 case FPORT_FRAME_ID_WRITE
: // never used
295 memcpy(&payloadBuffer
, &frame
->data
.telemetryData
, sizeof(smartPortPayload_t
));
296 mspPayload
= &payloadBuffer
;
308 reportFrameError(DEBUG_FPORT_ERROR_TYPE
);
316 rxBufferReadIndex
= (rxBufferReadIndex
+ 1) % NUM_RX_BUFFERS
;
319 if ((mspPayload
|| hasTelemetryRequest
) && cmpTimeUs(micros(), lastTelemetryFrameReceivedUs
) >= FPORT_MIN_TELEMETRY_RESPONSE_DELAY_US
) {
320 hasTelemetryRequest
= false;
322 result
= (result
& ~RX_FRAME_PENDING
) | RX_FRAME_PROCESSING_REQUIRED
;
325 if (lastRcFrameReceivedMs
&& ((millis() - lastRcFrameReceivedMs
) > FPORT_MAX_TELEMETRY_AGE_MS
)) {
326 setRssiDirect(0, RSSI_SOURCE_RX_PROTOCOL
);
327 lastRcFrameReceivedMs
= 0;
333 static bool fportProcessFrame(const rxRuntimeConfig_t
*rxRuntimeConfig
)
335 UNUSED(rxRuntimeConfig
);
337 #if defined(USE_TELEMETRY_SMARTPORT)
338 timeUs_t currentTimeUs
= micros();
339 if (cmpTimeUs(currentTimeUs
, lastTelemetryFrameReceivedUs
) > FPORT_MAX_TELEMETRY_RESPONSE_DELAY_US
) {
344 DEBUG_SET(DEBUG_FPORT
, DEBUG_FPORT_TELEMETRY_DELAY
, currentTimeUs
- lastTelemetryFrameReceivedUs
);
346 processSmartPortTelemetry(mspPayload
, &clearToSend
, NULL
);
349 smartPortWriteFrameFport(&emptySmartPortFrame
);
361 bool fportRxInit(const rxConfig_t
*rxConfig
, rxRuntimeConfig_t
*rxRuntimeConfig
)
363 static uint16_t sbusChannelData
[SBUS_MAX_CHANNEL
];
364 rxRuntimeConfig
->channelData
= sbusChannelData
;
365 sbusChannelsInit(rxConfig
, rxRuntimeConfig
);
367 rxRuntimeConfig
->channelCount
= SBUS_MAX_CHANNEL
;
368 rxRuntimeConfig
->rxRefreshRate
= 11000;
370 rxRuntimeConfig
->rcFrameStatusFn
= fportFrameStatus
;
371 rxRuntimeConfig
->rcProcessFrameFn
= fportProcessFrame
;
373 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
378 fportPort
= openSerialPort(portConfig
->identifier
,
384 FPORT_PORT_OPTIONS
| (rxConfig
->serialrx_inverted
? SERIAL_INVERTED
: 0) | (rxConfig
->halfDuplex
? SERIAL_BIDIR
: 0)
388 #if defined(USE_TELEMETRY_SMARTPORT)
389 telemetryEnabled
= initSmartPortTelemetryExternal(smartPortWriteFrameFport
);
392 if (rssiSource
== RSSI_SOURCE_NONE
) {
393 rssiSource
= RSSI_SOURCE_RX_PROTOCOL
;
397 return fportPort
!= NULL
;