PX4 Firmware
PX4 Autopilot Software http://px4.io
dsm.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 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 /**
35  * @file dsm.cpp
36  *
37  * Serial protocol decoder for the Spektrum DSM* family of protocols.
38  *
39  * Decodes into the global PPM buffer and updates accordingly.
40  */
41 
42 #include <px4_platform_common/px4_config.h>
43 #include <board_config.h>
44 #include <px4_platform_common/defines.h>
45 
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <termios.h>
49 #include <string.h>
50 
51 #include "dsm.h"
52 #include "spektrum_rssi.h"
53 #include "common_rc.h"
54 #include <drivers/drv_hrt.h>
55 
56 #if defined(__PX4_NUTTX)
57 #include <nuttx/arch.h>
58 #define dsm_udelay(arg) up_udelay(arg)
59 #else
60 #define dsm_udelay(arg) px4_usleep(arg)
61 #endif
62 
63 // #define DSM_DEBUG
64 
65 static enum DSM_DECODE_STATE {
69 
70 static int dsm_fd = -1; /**< File handle to the DSM UART */
71 static hrt_abstime dsm_last_rx_time; /**< Timestamp when we last received data */
72 static hrt_abstime dsm_last_frame_time; /**< Timestamp for start of last valid dsm frame */
73 static dsm_frame_t &dsm_frame = rc_decode_buf.dsm.frame; /**< DSM_BUFFER_SIZE DSM dsm frame receive buffer */
74 static dsm_buf_t &dsm_buf = rc_decode_buf.dsm.buf; /**< DSM_BUFFER_SIZE DSM dsm frame receive buffer */
75 
77 static unsigned dsm_partial_frame_count; /**< Count of bytes received for current dsm frame */
78 static unsigned dsm_channel_shift = 0; /**< Channel resolution, 0=unknown, 1=10 bit, 2=11 bit */
79 static unsigned dsm_frame_drops = 0; /**< Count of incomplete DSM frames */
80 static uint16_t dsm_chan_count = 0; /**< DSM channel count */
81 
82 static bool
83 dsm_decode(hrt_abstime frame_time, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, unsigned max_values,
84  int8_t *rssi_percent);
85 
86 /**
87  * Attempt to decode a single channel raw channel datum
88  *
89  * The DSM* protocol doesn't provide any explicit framing,
90  * so we detect dsm frame boundaries by the inter-dsm frame delay.
91  *
92  * The minimum dsm frame spacing is 11ms; with 16 bytes at 115200bps
93  * dsm frame transmission time is ~1.4ms.
94  *
95  * We expect to only be called when bytes arrive for processing,
96  * and if an interval of more than 5ms passes between calls,
97  * the first byte we read will be the first byte of a dsm frame.
98  *
99  * In the case where byte(s) are dropped from a dsm frame, this also
100  * provides a degree of protection. Of course, it would be better
101  * if we didn't drop bytes...
102  *
103  * Upon receiving a full dsm frame we attempt to decode it
104  *
105  * @param[in] raw 16 bit raw channel value from dsm frame
106  * @param[in] shift position of channel number in raw data
107  * @param[out] channel pointer to returned channel number
108  * @param[out] value pointer to returned channel value
109  * @return true=raw value successfully decoded
110  */
111 static bool
112 dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value)
113 {
114 
115  if (raw == 0xffff) {
116  return false;
117  }
118 
119  *channel = (raw >> shift) & 0xf;
120 
121  uint16_t data_mask = (1 << shift) - 1;
122  *value = raw & data_mask;
123 
124  //debug("DSM: %d 0x%04x -> %d %d", shift, raw, *channel, *value);
125 
126  return true;
127 }
128 
129 /**
130  * Attempt to guess if receiving 10 or 11 bit channel values
131  *
132  * @param[in] reset true=reset the 10/11 bit state to unknown
133  */
134 static bool
136 {
137  static uint32_t cs10 = 0;
138  static uint32_t cs11 = 0;
139  static unsigned samples = 0;
140 
141  /* reset the 10/11 bit sniffed channel masks */
142  if (reset) {
143  cs10 = 0;
144  cs11 = 0;
145  samples = 0;
146  dsm_channel_shift = 0;
147  return false;
148  }
149 
150  /* scan the channels in the current dsm_frame in both 10- and 11-bit mode */
151  for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
152 
153  uint8_t *dp = &dsm_frame[2 + (2 * i)];
154  uint16_t raw = (dp[0] << 8) | dp[1];
155  unsigned channel, value;
156 
157  /* if the channel decodes, remember the assigned number */
158  if (dsm_decode_channel(raw, 10, &channel, &value) && (channel < 31)) {
159  cs10 |= (1 << channel);
160  }
161 
162  if (dsm_decode_channel(raw, 11, &channel, &value) && (channel < 31)) {
163  cs11 |= (1 << channel);
164  }
165 
166  /* XXX if we cared, we could look for the phase bit here to decide 1 vs. 2-dsm_frame format */
167  }
168 
169  samples++;
170 
171 #ifdef DSM_DEBUG
172  printf("dsm guess format: samples: %d %s\n", samples,
173  (reset) ? "RESET" : "");
174 #endif
175 
176  /* wait until we have seen plenty of frames - 5 should normally be enough */
177  if (samples < 5) {
178  return false;
179  }
180 
181  /*
182  * Iterate the set of sensible sniffed channel sets and see whether
183  * decoding in 10 or 11-bit mode has yielded anything we recognize.
184  *
185  * XXX Note that due to what seem to be bugs in the DSM2 high-resolution
186  * stream, we may want to sniff for longer in some cases when we think we
187  * are talking to a DSM2 receiver in high-resolution mode (so that we can
188  * reject it, ideally).
189  * See e.g. http://git.openpilot.org/cru/OPReview-116 for a discussion
190  * of this issue.
191  */
192  static uint32_t masks[] = {
193  0x3f, /* 6 channels (DX6) */
194  0x7f, /* 7 channels (DX7) */
195  0xff, /* 8 channels (DX8) */
196  0x1ff, /* 9 channels (DX9, etc.) */
197  0x3ff, /* 10 channels (DX10) */
198  0x1fff, /* 13 channels (DX10t) */
199  0x3fff /* 18 channels (DX10) */
200  };
201  unsigned votes10 = 0;
202  unsigned votes11 = 0;
203 
204  for (unsigned i = 0; i < (sizeof(masks) / sizeof(masks[0])); i++) {
205 
206  if (cs10 == masks[i]) {
207  votes10++;
208  }
209 
210  if (cs11 == masks[i]) {
211  votes11++;
212  }
213  }
214 
215  if ((votes11 == 1) && (votes10 == 0)) {
216  dsm_channel_shift = 11;
217 #ifdef DSM_DEBUG
218  printf("DSM: 11-bit format\n");
219 #endif
220  return true;
221  }
222 
223  if ((votes10 == 1) && (votes11 == 0)) {
224  dsm_channel_shift = 10;
225 #ifdef DSM_DEBUG
226  printf("DSM: 10-bit format\n");
227 #endif
228  return true;
229  }
230 
231  /* call ourselves to reset our state ... we have to try again */
232 #ifdef DSM_DEBUG
233  printf("DSM: format detect fail, 10: 0x%08x %d 11: 0x%08x %d\n", cs10, votes10, cs11, votes11);
234 #endif
235  dsm_guess_format(true);
236  return false;
237 }
238 
239 int
241 {
242 #ifdef SPEKTRUM_POWER
243  // enable power on DSM connector
244  SPEKTRUM_POWER(true);
245 #endif
246 
247  int ret = -1;
248 
249  if (fd >= 0) {
250 
251  struct termios t;
252 
253  /* 115200bps, no parity, one stop bit */
254  tcgetattr(fd, &t);
255  cfsetspeed(&t, 115200);
256  t.c_cflag &= ~(CSTOPB | PARENB);
257  tcsetattr(fd, TCSANOW, &t);
258 
259  /* initialise the decoder */
262 
263  /* reset the format detector */
264  dsm_guess_format(true);
265 
266  ret = 0;
267  }
268 
269  return ret;
270 }
271 
272 void
274 {
275  dsm_channel_shift = 0;
276  dsm_frame_drops = 0;
277  dsm_chan_count = 0;
279 
280  for (unsigned i = 0; i < DSM_MAX_CHANNEL_COUNT; i++) {
281  dsm_chan_buf[i] = 0;
282  }
283 }
284 
285 /**
286  * Initialize the DSM receive functionality
287  *
288  * Open the UART for receiving DSM frames and configure it appropriately
289  *
290  * @param[in] device Device name of DSM UART
291  */
292 int
293 dsm_init(const char *device)
294 {
295 
296  if (dsm_fd < 0) {
297  dsm_fd = open(device, O_RDWR | O_NONBLOCK);
298  }
299 
300  dsm_proto_init();
301 
302  int ret = dsm_config(dsm_fd);
303 
304  if (!ret) {
305  return dsm_fd;
306 
307  } else {
308  return -1;
309  }
310 }
311 
312 void
314 {
315  if (dsm_fd >= 0) {
316  close(dsm_fd);
317  }
318 
319  dsm_fd = -1;
320 }
321 
322 #if defined(SPEKTRUM_POWER)
323 /**
324  * Handle DSM satellite receiver bind mode handler
325  *
326  * @param[in] cmd commands - dsm_bind_power_down, dsm_bind_power_up, dsm_bind_set_rx_out, dsm_bind_send_pulses, dsm_bind_reinit_uart
327  * @param[in] pulses Number of pulses for dsm_bind_send_pulses command
328  */
329 void
330 dsm_bind(uint16_t cmd, int pulses)
331 {
332  if (dsm_fd < 0) {
333  return;
334  }
335 
336  switch (cmd) {
337 
339 
340  /*power down DSM satellite*/
341  SPEKTRUM_POWER(false);
342  break;
343 
345 
346  /*power up DSM satellite*/
347  SPEKTRUM_POWER(true);
348  dsm_guess_format(true);
349  break;
350 
352 
353  /*Set UART RX pin to active output mode*/
354  SPEKTRUM_RX_AS_GPIO_OUTPUT();
355  break;
356 
358 
359  /*Pulse RX pin a number of times*/
360  for (int i = 0; i < pulses; i++) {
361  dsm_udelay(120);
362  SPEKTRUM_OUT(false);
363  dsm_udelay(120);
364  SPEKTRUM_OUT(true);
365  }
366 
367  break;
368 
370 
371  /*Restore USART RX pin to RS232 receive mode*/
372  SPEKTRUM_RX_AS_UART();
373  break;
374 
375  }
376 }
377 #endif
378 
379 /**
380  * Decode the entire dsm frame (all contained channels)
381  *
382  * @param[in] frame_time timestamp when this dsm frame was received. Used to detect RX loss in order to reset 10/11 bit guess.
383  * @param[out] values pointer to per channel array of decoded values
384  * @param[out] num_values pointer to number of raw channel values returned
385  * @return true=DSM frame successfully decoded, false=no update
386  */
387 bool
388 dsm_decode(hrt_abstime frame_time, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, unsigned max_values,
389  int8_t *rssi_percent)
390 {
391  /*
392  debug("DSM dsm_frame %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x",
393  dsm_frame[0], dsm_frame[1], dsm_frame[2], dsm_frame[3], dsm_frame[4], dsm_frame[5], dsm_frame[6], dsm_frame[7],
394  dsm_frame[8], dsm_frame[9], dsm_frame[10], dsm_frame[11], dsm_frame[12], dsm_frame[13], dsm_frame[14], dsm_frame[15]);
395  */
396  /*
397  * If we have lost signal for at least a second, reset the
398  * format guessing heuristic.
399  */
400  if (((frame_time - dsm_last_frame_time) > 1000000) && (dsm_channel_shift != 0)) {
401  dsm_guess_format(true);
402  }
403 
404  /* if we don't know the dsm_frame format, update the guessing state machine */
405  if (dsm_channel_shift == 0) {
406  if (!dsm_guess_format(false)) {
407  return false;
408  }
409  }
410 
411  /*
412  * The first byte represents the rssi in dBm on telemetry receivers with updated
413  * firmware, or fades on others. If the value is less than zero, it's rssi.
414  * We have other ways to detect bad link metrics, so ignore positive values,
415  * but rssi dBm is a useful value.
416  */
417 
418  if (rssi_percent) {
419  if (((int8_t *)dsm_frame)[0] < 0) {
420  /*
421  * RSSI is a signed integer between -42dBm and -92dBm
422  * If signal is lost, the value is -128
423  */
424  int8_t dbm = (int8_t)dsm_frame[0];
425 
426  if (dbm == -128) {
427  *rssi_percent = 0;
428 
429  } else {
430  *rssi_percent = spek_dbm_to_percent(dbm);
431  }
432 
433  } else {
434  /* if we don't know the rssi, anything over 100 will invalidate it */
435  *rssi_percent = 127;
436  }
437  }
438 
439  /*
440  * The second byte indicates the protocol and frame rate. We have a
441  * guessing state machine, so we don't need to use this. At any rate,
442  * these are the allowable values:
443  *
444  * 0x01 22MS 1024 DSM2
445  * 0x12 11MS 2048 DSM2
446  * 0xa2 22MS 2048 DSMX
447  * 0xb2 11MS 2048 DSMX
448  */
449 
450  /*
451  * Each channel is a 16-bit unsigned value containing either a 10-
452  * or 11-bit channel value and a 4-bit channel number, shifted
453  * either 10 or 11 bits. The MSB may also be set to indicate the
454  * second dsm_frame in variants of the protocol where more than
455  * seven channels are being transmitted.
456  */
457 
458  for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
459 
460  uint8_t *dp = &dsm_frame[2 + (2 * i)];
461  uint16_t raw = (dp[0] << 8) | dp[1];
462  unsigned channel, value;
463 
464  if (!dsm_decode_channel(raw, dsm_channel_shift, &channel, &value)) {
465  continue;
466  }
467 
468  /* reset bit guessing state machine if the channel index is out of bounds */
469  if (channel > DSM_MAX_CHANNEL_COUNT) {
470  dsm_guess_format(true);
471  return false;
472  }
473 
474  /* ignore channels out of range */
475  if (channel >= max_values) {
476  continue;
477  }
478 
479  /* update the decoded channel count */
480  if (channel >= *num_values) {
481  *num_values = channel + 1;
482  }
483 
484  /* convert 0-1024 / 0-2048 values to 1000-2000 ppm encoding. */
485  if (dsm_channel_shift == 10) {
486  value *= 2;
487  }
488 
489  /*
490  * Spektrum scaling is special. There are these basic considerations
491  *
492  * * Midpoint is 1520 us
493  * * 100% travel channels are +- 400 us
494  *
495  * We obey the original Spektrum scaling (so a default setup will scale from
496  * 1100 - 1900 us), but we do not obey the weird 1520 us center point
497  * and instead (correctly) center the center around 1500 us. This is in order
498  * to get something useful without requiring the user to calibrate on a digital
499  * link for no reason.
500  */
501 
502  /* scaled integer for decent accuracy while staying efficient */
503  value = ((((int)value - 1024) * 1000) / 1700) + 1500;
504 
505  /*
506  * Store the decoded channel into the R/C input buffer, taking into
507  * account the different ideas about channel assignement that we have.
508  *
509  * Specifically, the first four channels in rc_channel_data are roll, pitch, thrust, yaw,
510  * but the first four channels from the DSM receiver are thrust, roll, pitch, yaw.
511  */
512  switch (channel) {
513  case 0:
514  channel = 2;
515  break;
516 
517  case 1:
518  channel = 0;
519  break;
520 
521  case 2:
522  channel = 1;
523 
524  default:
525  break;
526  }
527 
528  values[channel] = value;
529  }
530 
531  /*
532  * Spektrum likes to send junk in higher channel numbers to fill
533  * their packets. We don't know about a 13 channel model in their TX
534  * lines, so if we get a channel count of 13, we'll return 12 (the last
535  * data index that is stable).
536  */
537  if (*num_values == 13) {
538  *num_values = 12;
539  }
540 
541  /* Set the 11-bit data indicator */
542  *dsm_11_bit = (dsm_channel_shift == 11);
543 
544  /* we have received something we think is a dsm_frame */
545  dsm_last_frame_time = frame_time;
546 
547  /*
548  * XXX Note that we may be in failsafe here; we need to work out how to detect that.
549  */
550 
551 #ifdef DSM_DEBUG
552  printf("PARSED PACKET\n");
553 #endif
554 
555  /* check all values */
556  for (unsigned i = 0; i < *num_values; i++) {
557  /* if the value is unrealistic, fail the parsing entirely */
558  if (values[i] < 600 || values[i] > 2400) {
559 #ifdef DSM_DEBUG
560  printf("DSM: VALUE RANGE FAIL: %d: %d\n", (int)i, (int)values[i]);
561 #endif
562  *num_values = 0;
563  return false;
564  }
565  }
566 
567  return true;
568 }
569 
570 /**
571  * Called periodically to check for input data from the DSM UART
572  *
573  * The DSM* protocol doesn't provide any explicit framing,
574  * so we detect dsm frame boundaries by the inter-dsm frame delay.
575  * The minimum dsm frame spacing is 11ms; with 16 bytes at 115200bps
576  * dsm frame transmission time is ~1.4ms.
577  * We expect to only be called when bytes arrive for processing,
578  * and if an interval of more than 5ms passes between calls,
579  * the first byte we read will be the first byte of a dsm frame.
580  * In the case where byte(s) are dropped from a dsm frame, this also
581  * provides a degree of protection. Of course, it would be better
582  * if we didn't drop bytes...
583  * Upon receiving a full dsm frame we attempt to decode it.
584  *
585  * @param[out] values pointer to per channel array of decoded values
586  * @param[out] num_values pointer to number of raw channel values returned, high order bit 0:10 bit data, 1:11 bit data
587  * @param[out] n_butes number of bytes read
588  * @param[out] bytes pointer to the buffer of read bytes
589  * @param[out] rssi value in percent, if supported, or 127
590  * @return true=decoded raw channel values updated, false=no update
591  */
592 bool
593 dsm_input(int fd, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, uint8_t *n_bytes, uint8_t **bytes,
594  int8_t *rssi,
595  unsigned max_values)
596 {
597  int ret = 1;
598  hrt_abstime now;
599 
600  /*
601  * The S.BUS protocol doesn't provide reliable framing,
602  * so we detect frame boundaries by the inter-frame delay.
603  *
604  * The minimum frame spacing is 7ms; with 25 bytes at 100000bps
605  * frame transmission time is ~2ms.
606  *
607  * We expect to only be called when bytes arrive for processing,
608  * and if an interval of more than 3ms passes between calls,
609  * the first byte we read will be the first byte of a frame.
610  *
611  * In the case where byte(s) are dropped from a frame, this also
612  * provides a degree of protection. Of course, it would be better
613  * if we didn't drop bytes...
614  */
615  now = hrt_absolute_time();
616 
617  /*
618  * Fetch bytes, but no more than we would need to complete
619  * a complete frame.
620  */
621 
622  ret = read(fd, &dsm_buf[0], sizeof(dsm_buf) / sizeof(dsm_buf[0]));
623 
624  /* if the read failed for any reason, just give up here */
625  if (ret < 1) {
626  return false;
627 
628  } else {
629  *n_bytes = ret;
630  *bytes = &dsm_buf[0];
631  }
632 
633  /*
634  * Try to decode something with what we got
635  */
636  return dsm_parse(now, &dsm_buf[0], ret, values, num_values, dsm_11_bit, &dsm_frame_drops, rssi, max_values);
637 }
638 
639 bool
640 dsm_parse(const uint64_t now, const uint8_t *frame, const unsigned len, uint16_t *values,
641  uint16_t *num_values, bool *dsm_11_bit, unsigned *frame_drops, int8_t *rssi_percent, uint16_t max_channels)
642 {
643 
644  /* this is set by the decoding state machine and will default to false
645  * once everything that was decodable has been decoded.
646  */
647  bool decode_ret = false;
648 
649  /* ensure there can be no overflows */
650  if (max_channels > sizeof(dsm_chan_buf) / sizeof(dsm_chan_buf[0])) {
651  max_channels = sizeof(dsm_chan_buf) / sizeof(dsm_chan_buf[0]);
652  }
653 
654  /* keep decoding until we have consumed the buffer */
655  for (unsigned d = 0; d < len; d++) {
656 
657  /* overflow check */
658  if (dsm_partial_frame_count == sizeof(dsm_frame) / sizeof(dsm_frame[0])) {
661 #ifdef DSM_DEBUG
662  printf("DSM: RESET (BUF LIM)\n");
663 #endif
664  }
665 
669 #ifdef DSM_DEBUG
670  printf("DSM: RESET (PACKET LIM)\n");
671 #endif
672  }
673 
674 #ifdef DSM_DEBUG
675 #if 1
676  printf("dsm state: %s%s, count: %d, val: %02x\n",
677  (dsm_decode_state == DSM_DECODE_STATE_DESYNC) ? "DSM_DECODE_STATE_DESYNC" : "",
678  (dsm_decode_state == DSM_DECODE_STATE_SYNC) ? "DSM_DECODE_STATE_SYNC" : "",
680  (unsigned)frame[d]);
681 #endif
682 #endif
683 
684  switch (dsm_decode_state) {
686 
687  /* we are de-synced and only interested in the frame marker */
688  if ((now - dsm_last_rx_time) > 5000) {
691  dsm_chan_count = 0;
692  dsm_frame[dsm_partial_frame_count++] = frame[d];
693  }
694 
695  break;
696 
697  case DSM_DECODE_STATE_SYNC: {
698  dsm_frame[dsm_partial_frame_count++] = frame[d];
699 
700  /* decode whatever we got and expect */
702  break;
703  }
704 
705  /*
706  * Great, it looks like we might have a frame. Go ahead and
707  * decode it.
708  */
709  decode_ret = dsm_decode(now, &dsm_chan_buf[0], &dsm_chan_count, dsm_11_bit, max_channels, rssi_percent);
710 
711  /* we consumed the partial frame, reset */
713 
714  /* if decoding failed, set proto to desync */
715  if (!decode_ret) {
717  dsm_frame_drops++;
718  }
719  }
720  break;
721 
722  default:
723 #ifdef DSM_DEBUG
724  printf("UNKNOWN PROTO STATE");
725 #endif
726  decode_ret = false;
727  }
728  }
729 
730  if (frame_drops) {
731  *frame_drops = dsm_frame_drops;
732  }
733 
734  if (decode_ret) {
735  *num_values = dsm_chan_count;
736 
737  memcpy(&values[0], &dsm_chan_buf[0], dsm_chan_count * sizeof(dsm_chan_buf[0]));
738 #ifdef DSM_DEBUG
739 
740  for (unsigned i = 0; i < dsm_chan_count; i++) {
741  printf("dsm_decode: %u: %u\n", i, values[i]);
742  }
743 
744 #endif
745  }
746 
747  dsm_last_rx_time = now;
748 
749  /* return false as default */
750  return decode_ret;
751 }
static uint16_t dsm_chan_buf[DSM_MAX_CHANNEL_COUNT]
Definition: dsm.cpp:76
dsm_frame_t frame
Definition: dsm.h:62
RC protocol definition for Spektrum RC.
static enum DSM_DECODE_STATE dsm_decode_state
static int dsm_fd
File handle to the DSM UART.
Definition: dsm.cpp:70
bool dsm_input(int fd, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, uint8_t *n_bytes, uint8_t **bytes, int8_t *rssi, unsigned max_values)
Called periodically to check for input data from the DSM UART.
Definition: dsm.cpp:593
static bool dsm_guess_format(bool reset)
Attempt to guess if receiving 10 or 11 bit channel values.
Definition: dsm.cpp:135
dsm_decode_t dsm
Definition: common_rc.h:16
static unsigned dsm_partial_frame_count
Count of bytes received for current dsm frame.
Definition: dsm.cpp:77
int reset(enum LPS22HB_BUS busid)
Reset the driver.
uint8_t dsm_buf_t[DSM_FRAME_SIZE *2]
Definition: dsm.h:59
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
High-resolution timer with callouts and timekeeping.
static bool dsm_decode(hrt_abstime frame_time, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, unsigned max_values, int8_t *rssi_percent)
Decode the entire dsm frame (all contained channels)
Definition: dsm.cpp:388
__EXPORT rc_decode_buf_t rc_decode_buf
Definition: common_rc.cpp:4
void dsm_deinit()
Definition: dsm.cpp:313
static void read(bootloader_app_shared_t *pshared)
static bool dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value)
Attempt to decode a single channel raw channel datum.
Definition: dsm.cpp:112
RSSI dBm to percentage conversion for Spektrum telemetry receivers.
#define dsm_udelay(arg)
Definition: dsm.cpp:60
bool dsm_parse(const uint64_t now, const uint8_t *frame, const unsigned len, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, unsigned *frame_drops, int8_t *rssi_percent, uint16_t max_channels)
Definition: dsm.cpp:640
void dsm_proto_init()
Definition: dsm.cpp:273
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
int fd
Definition: dataman.cpp:146
int dsm_init(const char *device)
Initialize the DSM receive functionality.
Definition: dsm.cpp:293
#define DSM_FRAME_CHANNELS
Max supported DSM channels per frame.
Definition: dsm.h:52
#define DSM_FRAME_SIZE
DSM frame size in bytes.
Definition: dsm.h:51
static unsigned dsm_frame_drops
Count of incomplete DSM frames.
Definition: dsm.cpp:79
#define DSM_MAX_CHANNEL_COUNT
Max channel count of any DSM RC.
Definition: dsm.h:53
dsm_buf_t buf
Definition: dsm.h:63
static dsm_buf_t & dsm_buf
DSM_BUFFER_SIZE DSM dsm frame receive buffer.
Definition: dsm.cpp:74
static uint16_t dsm_chan_count
DSM channel count.
Definition: dsm.cpp:80
DSM_DECODE_STATE
Definition: dsm.cpp:65
uint8_t dsm_frame_t[DSM_BUFFER_SIZE]
DSM dsm frame receive buffer.
Definition: dsm.h:58
int dsm_config(int fd)
Definition: dsm.cpp:240
static unsigned dsm_channel_shift
Channel resolution, 0=unknown, 1=10 bit, 2=11 bit.
Definition: dsm.cpp:78
constexpr int8_t spek_dbm_to_percent(int8_t dbm)
Definition: spektrum_rssi.h:79
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
static dsm_frame_t & dsm_frame
DSM_BUFFER_SIZE DSM dsm frame receive buffer.
Definition: dsm.cpp:73
static hrt_abstime dsm_last_frame_time
Timestamp for start of last valid dsm frame.
Definition: dsm.cpp:72
static hrt_abstime dsm_last_rx_time
Timestamp when we last received data.
Definition: dsm.cpp:71