42 #include <px4_platform_common/px4_config.h> 43 #include <board_config.h> 44 #include <px4_platform_common/defines.h> 56 #if defined(__PX4_NUTTX) 57 #include <nuttx/arch.h> 58 #define dsm_udelay(arg) up_udelay(arg) 60 #define dsm_udelay(arg) px4_usleep(arg) 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);
119 *channel = (raw >> shift) & 0xf;
121 uint16_t data_mask = (1 << shift) - 1;
122 *value = raw & data_mask;
137 static uint32_t cs10 = 0;
138 static uint32_t cs11 = 0;
139 static unsigned samples = 0;
154 uint16_t raw = (dp[0] << 8) | dp[1];
155 unsigned channel, value;
159 cs10 |= (1 << channel);
163 cs11 |= (1 << channel);
172 printf(
"dsm guess format: samples: %d %s\n", samples,
173 (reset) ?
"RESET" :
"");
192 static uint32_t masks[] = {
201 unsigned votes10 = 0;
202 unsigned votes11 = 0;
204 for (
unsigned i = 0; i < (
sizeof(masks) /
sizeof(masks[0])); i++) {
206 if (cs10 == masks[i]) {
210 if (cs11 == masks[i]) {
215 if ((votes11 == 1) && (votes10 == 0)) {
218 printf(
"DSM: 11-bit format\n");
223 if ((votes10 == 1) && (votes11 == 0)) {
226 printf(
"DSM: 10-bit format\n");
233 printf(
"DSM: format detect fail, 10: 0x%08x %d 11: 0x%08x %d\n", cs10, votes10, cs11, votes11);
242 #ifdef SPEKTRUM_POWER 244 SPEKTRUM_POWER(
true);
255 cfsetspeed(&t, 115200);
256 t.c_cflag &= ~(CSTOPB | PARENB);
257 tcsetattr(fd, TCSANOW, &t);
297 dsm_fd = open(device, O_RDWR | O_NONBLOCK);
322 #if defined(SPEKTRUM_POWER) 330 dsm_bind(uint16_t cmd,
int pulses)
341 SPEKTRUM_POWER(
false);
347 SPEKTRUM_POWER(
true);
354 SPEKTRUM_RX_AS_GPIO_OUTPUT();
360 for (
int i = 0; i < pulses; i++) {
372 SPEKTRUM_RX_AS_UART();
389 int8_t *rssi_percent)
424 int8_t dbm = (int8_t)dsm_frame[0];
461 uint16_t raw = (dp[0] << 8) | dp[1];
462 unsigned channel, value;
475 if (channel >= max_values) {
480 if (channel >= *num_values) {
481 *num_values = channel + 1;
503 value = ((((int)value - 1024) * 1000) / 1700) + 1500;
528 values[channel] = value;
537 if (*num_values == 13) {
552 printf(
"PARSED PACKET\n");
556 for (
unsigned i = 0; i < *num_values; i++) {
558 if (values[i] < 600 || values[i] > 2400) {
560 printf(
"DSM: VALUE RANGE FAIL: %d: %d\n", (
int)i, (
int)values[i]);
593 dsm_input(
int fd, uint16_t *values, uint16_t *num_values,
bool *dsm_11_bit, uint8_t *n_bytes, uint8_t **bytes,
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)
647 bool decode_ret =
false;
655 for (
unsigned d = 0; d < len; d++) {
662 printf(
"DSM: RESET (BUF LIM)\n");
670 printf(
"DSM: RESET (PACKET LIM)\n");
676 printf(
"dsm state: %s%s, count: %d, val: %02x\n",
724 printf(
"UNKNOWN PROTO STATE");
741 printf(
"dsm_decode: %u: %u\n", i, values[i]);
static uint16_t dsm_chan_buf[DSM_MAX_CHANNEL_COUNT]
RC protocol definition for Spektrum RC.
static enum DSM_DECODE_STATE dsm_decode_state
static int dsm_fd
File handle to the DSM UART.
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.
static bool dsm_guess_format(bool reset)
Attempt to guess if receiving 10 or 11 bit channel values.
static unsigned dsm_partial_frame_count
Count of bytes received for current dsm frame.
int reset(enum LPS22HB_BUS busid)
Reset the driver.
uint8_t dsm_buf_t[DSM_FRAME_SIZE *2]
Namespace encapsulating all device framework classes, functions and data.
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)
__EXPORT rc_decode_buf_t rc_decode_buf
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.
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)
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
int dsm_init(const char *device)
Initialize the DSM receive functionality.
#define DSM_FRAME_CHANNELS
Max supported DSM channels per frame.
#define DSM_FRAME_SIZE
DSM frame size in bytes.
static unsigned dsm_frame_drops
Count of incomplete DSM frames.
#define DSM_MAX_CHANNEL_COUNT
Max channel count of any DSM RC.
static dsm_buf_t & dsm_buf
DSM_BUFFER_SIZE DSM dsm frame receive buffer.
static uint16_t dsm_chan_count
DSM channel count.
uint8_t dsm_frame_t[DSM_BUFFER_SIZE]
DSM dsm frame receive buffer.
static unsigned dsm_channel_shift
Channel resolution, 0=unknown, 1=10 bit, 2=11 bit.
__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.
static hrt_abstime dsm_last_frame_time
Timestamp for start of last valid dsm frame.
static hrt_abstime dsm_last_rx_time
Timestamp when we last received data.