PX4 Firmware
PX4 Autopilot Software http://px4.io
crsf.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #if 0 // enable non-verbose debugging
35 #define CRSF_DEBUG PX4_WARN
36 #else
37 #define CRSF_DEBUG(...)
38 #endif
39 
40 #if 0 // verbose debugging. Careful when enabling: it leads to too much output, causing dropped bytes
41 #define CRSF_VERBOSE PX4_WARN
42 #else
43 #define CRSF_VERBOSE(...)
44 #endif
45 
46 #include <drivers/drv_hrt.h>
47 #include <termios.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "crsf.h"
52 #include "common_rc.h"
53 
54 #define MIN(a,b) (((a)<(b))?(a):(b))
55 #define MAX(a,b) (((a)>(b))?(a):(b))
56 
57 
58 #define CRSF_BAUDRATE 420000
59 
60 #define CRSF_SYNC_BYTE 0xC8
61 
62 enum class crsf_frame_type_t : uint8_t {
63  gps = 0x02,
64  battery_sensor = 0x08,
65  link_statistics = 0x14,
66  rc_channels_packed = 0x16,
67  attitude = 0x1E,
68  flight_mode = 0x21,
69 
70  // Extended Header Frames, range: 0x28 to 0x96
71  device_ping = 0x28,
72  device_info = 0x29,
74  parameter_read = 0x2C,
75  parameter_write = 0x2D,
76  command = 0x32
77 };
78 
79 enum class crsf_payload_size_t : uint8_t {
80  gps = 15,
81  battery_sensor = 8,
82  link_statistics = 10,
83  rc_channels = 22, ///< 11 bits per channel * 16 channels = 22 bytes.
84  attitude = 6,
85 };
86 
87 
88 enum class crsf_address_t : uint8_t {
89  broadcast = 0x00,
90  usb = 0x10,
91  tbs_core_pnp_pro = 0x80,
92  reserved1 = 0x8A,
93  current_sensor = 0xC0,
94  gps = 0xC2,
95  tbs_blackbox = 0xC4,
96  flight_controller = 0xC8,
97  reserved2 = 0xCA,
98  race_tag = 0xCC,
99  radio_transmitter = 0xEA,
100  crsf_receiver = 0xEC,
101  crsf_transmitter = 0xEE
102 };
103 
104 #pragma pack(push, 1)
106  // 176 bits of data (11 bits per channel * 16 channels) = 22 bytes
107  unsigned chan0 : 11;
108  unsigned chan1 : 11;
109  unsigned chan2 : 11;
110  unsigned chan3 : 11;
111  unsigned chan4 : 11;
112  unsigned chan5 : 11;
113  unsigned chan6 : 11;
114  unsigned chan7 : 11;
115  unsigned chan8 : 11;
116  unsigned chan9 : 11;
117  unsigned chan10 : 11;
118  unsigned chan11 : 11;
119  unsigned chan12 : 11;
120  unsigned chan13 : 11;
121  unsigned chan14 : 11;
122  unsigned chan15 : 11;
123 };
124 
125 #pragma pack(pop)
126 
127 enum class crsf_parser_state_t : uint8_t {
128  unsynced = 0,
129  synced
130 };
131 
133 static unsigned current_frame_position = 0;
135 
136 /**
137  * parse the current crsf_frame buffer
138  */
139 static bool crsf_parse_buffer(uint16_t *values, uint16_t *num_values, uint16_t max_channels);
140 
141 static uint8_t crc8_dvb_s2(uint8_t crc, uint8_t a);
142 static uint8_t crc8_dvb_s2_buf(uint8_t *buf, int len);
143 uint8_t crsf_frame_CRC(const crsf_frame_t &frame);
144 
145 
146 int
147 crsf_config(int uart_fd)
148 {
149  struct termios t;
150 
151  /* no parity, one stop bit */
152  tcgetattr(uart_fd, &t);
153  cfsetspeed(&t, CRSF_BAUDRATE);
154  t.c_cflag &= ~(CSTOPB | PARENB);
155  return tcsetattr(uart_fd, TCSANOW, &t);
156 }
157 
158 /**
159  * Convert from RC to PWM value
160  * @param chan_value channel value in [172, 1811]
161  * @return PWM channel value in [1000, 2000]
162  */
163 static uint16_t convert_channel_value(unsigned chan_value);
164 
165 
166 bool crsf_parse(const uint64_t now, const uint8_t *frame, unsigned len, uint16_t *values,
167  uint16_t *num_values, uint16_t max_channels)
168 {
169  bool ret = false;
170  uint8_t *crsf_frame_ptr = (uint8_t *)&crsf_frame;
171 
172  while (len > 0) {
173 
174  // fill in the crsf_buffer, as much as we can
175  const unsigned current_len = MIN(len, sizeof(crsf_frame_t) - current_frame_position);
176  memcpy(crsf_frame_ptr + current_frame_position, frame, current_len);
177  current_frame_position += current_len;
178 
179  // protection to guarantee parsing progress
180  if (current_len == 0) {
181  CRSF_DEBUG("========== parser bug: no progress (%i) ===========", len);
182 
183  for (unsigned i = 0; i < current_frame_position; ++i) {
184  CRSF_DEBUG("crsf_frame_ptr[%i]: 0x%x", i, (int)crsf_frame_ptr[i]);
185  }
186 
187  // reset the parser
188  current_frame_position = 0;
189  parser_state = crsf_parser_state_t::unsynced;
190  return false;
191  }
192 
193  len -= current_len;
194  frame += current_len;
195 
196  if (crsf_parse_buffer(values, num_values, max_channels)) {
197  ret = true;
198  }
199  }
200 
201 
202  return ret;
203 }
204 
205 static uint8_t crc8_dvb_s2(uint8_t crc, uint8_t a)
206 {
207  crc ^= a;
208 
209  for (int i = 0; i < 8; ++i) {
210  if (crc & 0x80) {
211  crc = (crc << 1) ^ 0xD5;
212 
213  } else {
214  crc = crc << 1;
215  }
216  }
217 
218  return crc;
219 }
220 
221 static uint8_t crc8_dvb_s2_buf(uint8_t *buf, int len)
222 {
223  uint8_t crc = 0;
224 
225  for (int i = 0; i < len; ++i) {
226  crc = crc8_dvb_s2(crc, buf[i]);
227  }
228 
229  return crc;
230 }
231 
232 uint8_t crsf_frame_CRC(const crsf_frame_t &frame)
233 {
234  // CRC includes type and payload
235  uint8_t crc = crc8_dvb_s2(0, frame.type);
236 
237  for (int i = 0; i < frame.header.length - 2; ++i) {
238  crc = crc8_dvb_s2(crc, frame.payload[i]);
239  }
240 
241  return crc;
242 }
243 
244 static uint16_t convert_channel_value(unsigned chan_value)
245 {
246  /*
247  * RC PWM
248  * min 172 -> 988us
249  * mid 992 -> 1500us
250  * max 1811 -> 2012us
251  */
252  static constexpr float scale = (2012.f - 988.f) / (1811.f - 172.f);
253  static constexpr float offset = 988.f - 172.f * scale;
254  return (scale * chan_value) + offset;
255 }
256 
257 static bool crsf_parse_buffer(uint16_t *values, uint16_t *num_values, uint16_t max_channels)
258 {
259  uint8_t *crsf_frame_ptr = (uint8_t *)&crsf_frame;
260 
261  if (parser_state == crsf_parser_state_t::unsynced) {
262  // there is no sync byte, try to find an RC packet by searching for a matching frame length and type
263  for (unsigned i = 1; i < current_frame_position - 1; ++i) {
264  if (crsf_frame_ptr[i] == (uint8_t)crsf_payload_size_t::rc_channels + 2 &&
265  crsf_frame_ptr[i + 1] == (uint8_t)crsf_frame_type_t::rc_channels_packed) {
266  parser_state = crsf_parser_state_t::synced;
267  unsigned frame_offset = i - 1;
268  CRSF_VERBOSE("RC channels found at offset %i", frame_offset);
269 
270  // move the rest of the buffer to the beginning
271  if (frame_offset != 0) {
272  memmove(crsf_frame_ptr, crsf_frame_ptr + frame_offset, current_frame_position - frame_offset);
273  current_frame_position -= frame_offset;
274  }
275 
276  break;
277  }
278  }
279  }
280 
281  if (parser_state != crsf_parser_state_t::synced) {
282  if (current_frame_position >= sizeof(crsf_frame_t)) {
283  // discard most of the data, but keep the last 3 bytes (otherwise we could miss the frame start)
285 
286  for (unsigned i = 0; i < current_frame_position; ++i) {
287  crsf_frame_ptr[i] = crsf_frame_ptr[sizeof(crsf_frame_t) - current_frame_position + i];
288  }
289 
290  CRSF_VERBOSE("Discarding buffer");
291  }
292 
293  return false;
294  }
295 
296 
297  if (current_frame_position < 3) {
298  // wait until we have the header & type
299  return false;
300  }
301 
302  // Now we have at least the header and the type
303 
304  const unsigned current_frame_length = crsf_frame.header.length + sizeof(crsf_frame_header_t);
305 
306  if (current_frame_length > sizeof(crsf_frame_t) || current_frame_length < 4) {
307  // frame too long or bogus -> discard everything and go into unsynced state
309  parser_state = crsf_parser_state_t::unsynced;
310  CRSF_DEBUG("Frame too long/bogus (%i, type=%i) -> unsync", current_frame_length, crsf_frame.type);
311  return false;
312  }
313 
314  if (current_frame_position < current_frame_length) {
315  // we don't have the full frame yet -> wait for more data
316  CRSF_VERBOSE("waiting for more data (%i < %i)", current_frame_position, current_frame_length);
317  return false;
318  }
319 
320  bool ret = false;
321 
322  // Now we have the full frame
323 
324  if (crsf_frame.type == (uint8_t)crsf_frame_type_t::rc_channels_packed &&
325  crsf_frame.header.length == (uint8_t)crsf_payload_size_t::rc_channels + 2) {
326  const uint8_t crc = crsf_frame.payload[crsf_frame.header.length - 2];
327 
328  if (crc == crsf_frame_CRC(crsf_frame)) {
331  *num_values = MIN(max_channels, 16);
332 
333  if (max_channels > 0) { values[0] = convert_channel_value(rc_channels->chan0); }
334 
335  if (max_channels > 1) { values[1] = convert_channel_value(rc_channels->chan1); }
336 
337  if (max_channels > 2) { values[2] = convert_channel_value(rc_channels->chan2); }
338 
339  if (max_channels > 3) { values[3] = convert_channel_value(rc_channels->chan3); }
340 
341  if (max_channels > 4) { values[4] = convert_channel_value(rc_channels->chan4); }
342 
343  if (max_channels > 5) { values[5] = convert_channel_value(rc_channels->chan5); }
344 
345  if (max_channels > 6) { values[6] = convert_channel_value(rc_channels->chan6); }
346 
347  if (max_channels > 7) { values[7] = convert_channel_value(rc_channels->chan7); }
348 
349  if (max_channels > 8) { values[8] = convert_channel_value(rc_channels->chan8); }
350 
351  if (max_channels > 9) { values[9] = convert_channel_value(rc_channels->chan9); }
352 
353  if (max_channels > 10) { values[10] = convert_channel_value(rc_channels->chan10); }
354 
355  if (max_channels > 11) { values[11] = convert_channel_value(rc_channels->chan11); }
356 
357  if (max_channels > 12) { values[12] = convert_channel_value(rc_channels->chan12); }
358 
359  if (max_channels > 13) { values[13] = convert_channel_value(rc_channels->chan13); }
360 
361  if (max_channels > 14) { values[14] = convert_channel_value(rc_channels->chan14); }
362 
363  if (max_channels > 15) { values[15] = convert_channel_value(rc_channels->chan15); }
364 
365  CRSF_VERBOSE("Got Channels");
366 
367  ret = true;
368 
369  } else {
370  CRSF_DEBUG("CRC check failed");
371  }
372 
373  } else {
374  CRSF_DEBUG("Got Non-RC frame (len=%i, type=%i)", current_frame_length, crsf_frame.type);
375  // We could check the CRC here and reset the parser into unsynced state if it fails.
376  // But in practise it's robust even without that.
377  }
378 
379  // Either reset or move the rest of the buffer
380  if (current_frame_position > current_frame_length) {
381  CRSF_VERBOSE("Moving buffer (%i > %i)", current_frame_position, current_frame_length);
382  memmove(crsf_frame_ptr, crsf_frame_ptr + current_frame_length, current_frame_position - current_frame_length);
383  current_frame_position -= current_frame_length;
384 
385  } else {
387  }
388 
389  return ret;
390 }
391 
392 /**
393  * write an uint8_t value to a buffer at a given offset and increment the offset
394  */
395 static inline void write_uint8_t(uint8_t *buf, int &offset, uint8_t value)
396 {
397  buf[offset++] = value;
398 }
399 /**
400  * write an uint16_t value to a buffer at a given offset and increment the offset
401  */
402 static inline void write_uint16_t(uint8_t *buf, int &offset, uint16_t value)
403 {
404  // Big endian
405  buf[offset] = value >> 8;
406  buf[offset + 1] = value & 0xff;
407  offset += 2;
408 }
409 /**
410  * write an uint24_t value to a buffer at a given offset and increment the offset
411  */
412 static inline void write_uint24_t(uint8_t *buf, int &offset, int value)
413 {
414  // Big endian
415  buf[offset] = value >> 16;
416  buf[offset + 1] = (value >> 8) & 0xff;
417  buf[offset + 2] = value & 0xff;
418  offset += 3;
419 }
420 
421 /**
422  * write an int32_t value to a buffer at a given offset and increment the offset
423  */
424 static inline void write_int32_t(uint8_t *buf, int &offset, int32_t value)
425 {
426  // Big endian
427  buf[offset] = value >> 24;
428  buf[offset + 1] = (value >> 16) & 0xff;
429  buf[offset + 2] = (value >> 8) & 0xff;
430  buf[offset + 3] = value & 0xff;
431  offset += 4;
432 }
433 
434 static inline void write_frame_header(uint8_t *buf, int &offset, crsf_frame_type_t type, uint8_t payload_size)
435 {
436  write_uint8_t(buf, offset, CRSF_SYNC_BYTE); // this got changed from the address to the sync byte
437  write_uint8_t(buf, offset, payload_size + 2);
438  write_uint8_t(buf, offset, (uint8_t)type);
439 }
440 static inline void write_frame_crc(uint8_t *buf, int &offset, int buf_size)
441 {
442  // CRC does not include the address and length
443  write_uint8_t(buf, offset, crc8_dvb_s2_buf(buf + 2, buf_size - 3));
444 
445  // check correctness of buffer size (only needed during development)
446  //if (buf_size != offset) { PX4_ERR("frame size mismatch (%i != %i)", buf_size, offset); }
447 }
448 
449 bool crsf_send_telemetry_battery(int uart_fd, uint16_t voltage, uint16_t current, int fuel, uint8_t remaining)
450 {
451  uint8_t buf[(uint8_t)crsf_payload_size_t::battery_sensor + 4];
452  int offset = 0;
454  write_uint16_t(buf, offset, voltage);
455  write_uint16_t(buf, offset, current);
456  write_uint24_t(buf, offset, fuel);
457  write_uint8_t(buf, offset, remaining);
458  write_frame_crc(buf, offset, sizeof(buf));
459  return write(uart_fd, buf, offset) == offset;
460 }
461 
462 bool crsf_send_telemetry_gps(int uart_fd, int32_t latitude, int32_t longitude, uint16_t groundspeed,
463  uint16_t gps_heading, uint16_t altitude, uint8_t num_satellites)
464 {
465  uint8_t buf[(uint8_t)crsf_payload_size_t::gps + 4];
466  int offset = 0;
468  write_int32_t(buf, offset, latitude);
469  write_int32_t(buf, offset, longitude);
470  write_uint16_t(buf, offset, groundspeed);
471  write_uint16_t(buf, offset, gps_heading);
472  write_uint16_t(buf, offset, altitude);
473  write_uint8_t(buf, offset, num_satellites);
474  write_frame_crc(buf, offset, sizeof(buf));
475  return write(uart_fd, buf, offset) == offset;
476 }
477 
478 bool crsf_send_telemetry_attitude(int uart_fd, int16_t pitch, int16_t roll, int16_t yaw)
479 {
480  uint8_t buf[(uint8_t)crsf_payload_size_t::attitude + 4];
481  int offset = 0;
483  write_uint16_t(buf, offset, pitch);
484  write_uint16_t(buf, offset, roll);
485  write_uint16_t(buf, offset, yaw);
486  write_frame_crc(buf, offset, sizeof(buf));
487  return write(uart_fd, buf, offset) == offset;
488 }
489 
490 bool crsf_send_telemetry_flight_mode(int uart_fd, const char *flight_mode)
491 {
492  const int max_length = 16;
493  int length = strlen(flight_mode) + 1;
494 
495  if (length > max_length) {
496  length = max_length;
497  }
498 
499  uint8_t buf[max_length + 4];
500  int offset = 0;
502  memcpy(buf + offset, flight_mode, length);
503  offset += length;
504  buf[offset - 1] = 0; // ensure null-terminated string
505  write_frame_crc(buf, offset, length + 4);
506  return write(uart_fd, buf, offset) == offset;
507 }
static uint8_t crc8_dvb_s2_buf(uint8_t *buf, int len)
Definition: crsf.cpp:221
bool crsf_send_telemetry_gps(int uart_fd, int32_t latitude, int32_t longitude, uint16_t groundspeed, uint16_t gps_heading, uint16_t altitude, uint8_t num_satellites)
Send telemetry GPS information.
Definition: crsf.cpp:462
crsf_payload_size_t
Definition: crsf.cpp:79
bool crsf_send_telemetry_battery(int uart_fd, uint16_t voltage, uint16_t current, int fuel, uint8_t remaining)
Send telemetry battery information.
Definition: crsf.cpp:449
uint8_t payload[CRSF_PAYLOAD_SIZE_MAX+1]
payload data including 1 byte CRC at end
Definition: crsf.h:64
static void write_frame_crc(uint8_t *buf, int &offset, int buf_size)
Definition: crsf.cpp:440
static unsigned current_frame_position
Definition: crsf.cpp:133
#define CRSF_BAUDRATE
Definition: crsf.cpp:58
uint8_t length
length of crsf_frame_t (including CRC) minus sizeof(crsf_frame_header_t)
Definition: crsf.h:58
static uint8_t crc8_dvb_s2(uint8_t crc, uint8_t a)
Definition: crsf.cpp:205
bool crsf_parse(const uint64_t now, const uint8_t *frame, unsigned len, uint16_t *values, uint16_t *num_values, uint16_t max_channels)
Parse the CRSF protocol and extract RC channel data.
Definition: crsf.cpp:166
bool crsf_send_telemetry_attitude(int uart_fd, int16_t pitch, int16_t roll, int16_t yaw)
Send telemetry Attitude information.
Definition: crsf.cpp:478
uint8_t crsf_frame_CRC(const crsf_frame_t &frame)
Definition: crsf.cpp:232
High-resolution timer with callouts and timekeeping.
__EXPORT rc_decode_buf_t rc_decode_buf
Definition: common_rc.cpp:4
static void write_uint24_t(uint8_t *buf, int &offset, int value)
write an uint24_t value to a buffer at a given offset and increment the offset
Definition: crsf.cpp:412
static void write_frame_header(uint8_t *buf, int &offset, crsf_frame_type_t type, uint8_t payload_size)
Definition: crsf.cpp:434
int crsf_config(int uart_fd)
Configure an UART port to be used for CRSF.
Definition: crsf.cpp:147
static crsf_parser_state_t parser_state
Definition: crsf.cpp:134
static crsf_frame_t & crsf_frame
Definition: crsf.cpp:132
static void write_uint8_t(uint8_t *buf, int &offset, uint8_t value)
write an uint8_t value to a buffer at a given offset and increment the offset
Definition: crsf.cpp:395
static void write_uint16_t(uint8_t *buf, int &offset, uint16_t value)
write an uint16_t value to a buffer at a given offset and increment the offset
Definition: crsf.cpp:402
#define CRSF_VERBOSE(...)
Definition: crsf.cpp:43
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
#define CRSF_DEBUG(...)
Definition: crsf.cpp:37
#define CRSF_SYNC_BYTE
Definition: crsf.cpp:60
static void write(bootloader_app_shared_t *pshared)
static bool crsf_parse_buffer(uint16_t *values, uint16_t *num_values, uint16_t max_channels)
parse the current crsf_frame buffer
Definition: crsf.cpp:257
crsf_frame_header_t header
Definition: crsf.h:62
crsf_parser_state_t
Definition: crsf.cpp:127
static uint16_t convert_channel_value(unsigned chan_value)
Convert from RC to PWM value.
Definition: crsf.cpp:244
crsf_address_t
Definition: crsf.cpp:88
crsf_frame_t crsf_frame
Definition: common_rc.h:15
RC protocol definition for CSRF (TBS Crossfire).
#define MIN(a, b)
Definition: crsf.cpp:54
uint8_t type
Definition: crsf.h:63
11 bits per channel * 16 channels = 22 bytes.
bool crsf_send_telemetry_flight_mode(int uart_fd, const char *flight_mode)
Send telemetry Flight Mode information.
Definition: crsf.cpp:490
static void write_int32_t(uint8_t *buf, int &offset, int32_t value)
write an int32_t value to a buffer at a given offset and increment the offset
Definition: crsf.cpp:424
crsf_frame_type_t
Definition: crsf.cpp:62