123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- #include <stdint.h>
- #include <stdio.h>
- #include "dsm.h"
- #define DSM_FRAME_SIZE 16
- #define DSM_FRAME_CHANNELS 7
- static uint64_t dsm_last_frame_time;
- static unsigned dsm_channel_shift;
- #ifdef DEBUG
- # define debug(fmt, args...) printf(fmt "\n", ##args)
- #else
- # define debug(fmt, args...) do {} while(0)
- #endif
- static bool
- dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value)
- {
- if (raw == 0xffff)
- return false;
- *channel = (raw >> shift) & 0xf;
- uint16_t data_mask = (1 << shift) - 1;
- *value = raw & data_mask;
-
- return true;
- }
- static void
- dsm_guess_format(bool reset, const uint8_t dsm_frame[16])
- {
- static uint32_t cs10;
- static uint32_t cs11;
- static unsigned samples;
-
- if (reset) {
- cs10 = 0;
- cs11 = 0;
- samples = 0;
- dsm_channel_shift = 0;
- return;
- }
-
- for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
- const uint8_t *dp = &dsm_frame[2 + (2 * i)];
- uint16_t raw = (dp[0] << 8) | dp[1];
- unsigned channel, value;
-
- if (dsm_decode_channel(raw, 10, &channel, &value) && (channel < 31))
- cs10 |= (1 << channel);
- if (dsm_decode_channel(raw, 11, &channel, &value) && (channel < 31))
- cs11 |= (1 << channel);
-
- }
-
- if (samples++ < 5)
- return;
-
- static uint32_t masks[] = {
- 0x3f,
- 0x7f,
- 0xff,
- 0x1ff,
- 0x3ff,
- 0x1fff,
- 0x3fff
- };
- unsigned votes10 = 0;
- unsigned votes11 = 0;
- for (unsigned i = 0; i < sizeof(masks)/sizeof(masks[0]); i++) {
- if (cs10 == masks[i])
- votes10++;
- if (cs11 == masks[i])
- votes11++;
- }
- if ((votes11 == 1) && (votes10 == 0)) {
- dsm_channel_shift = 11;
- debug("DSM: 11-bit format");
- return;
- }
- if ((votes10 == 1) && (votes11 == 0)) {
- dsm_channel_shift = 10;
- debug("DSM: 10-bit format");
- return;
- }
-
- debug("DSM: format detect fail, 10: 0x%08x %u 11: 0x%08x %u", cs10, votes10, cs11, votes11);
- dsm_guess_format(true, dsm_frame);
- }
- bool
- dsm_decode(uint64_t frame_time, const uint8_t dsm_frame[16], uint16_t *values, uint16_t *num_values, uint16_t max_values)
- {
-
-
- if (((frame_time - dsm_last_frame_time) > 1000000) && (dsm_channel_shift != 0))
- dsm_guess_format(true, dsm_frame);
-
- dsm_last_frame_time = frame_time;
-
- if (dsm_channel_shift == 0) {
- dsm_guess_format(false, dsm_frame);
- return false;
- }
-
- for (unsigned i = 0; i < DSM_FRAME_CHANNELS; i++) {
- const uint8_t *dp = &dsm_frame[2 + (2 * i)];
- uint16_t raw = (dp[0] << 8) | dp[1];
- unsigned channel, value;
- if (!dsm_decode_channel(raw, dsm_channel_shift, &channel, &value))
- continue;
-
- if (channel >= max_values)
- continue;
-
- if (channel >= *num_values)
- *num_values = channel + 1;
-
- if (dsm_channel_shift == 10)
- value *= 2;
-
-
- value = ((((int)value - 1024) * 1000) / 1700) + 1500;
-
- switch (channel) {
- case 0:
- channel = 2;
- break;
- case 1:
- channel = 0;
- break;
- case 2:
- channel = 1;
- default:
- break;
- }
- values[channel] = value;
- }
-
- if (*num_values == 13)
- *num_values = 12;
- #if 0
- if (dsm_channel_shift == 11) {
-
- *num_values |= 0x8000;
- }
- #endif
-
- return true;
- }
- #if defined(TEST_MAIN_PROGRAM) || defined(TEST_HEX_STRING)
- static uint8_t dsm_partial_frame_count;
- static uint8_t dsm_frame[DSM_FRAME_SIZE];
- static enum DSM_DECODE_STATE {
- DSM_DECODE_STATE_DESYNC = 0,
- DSM_DECODE_STATE_SYNC
- } dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- static uint64_t dsm_last_rx_time;
- static uint16_t dsm_chan_count;
- static uint16_t dsm_frame_drops;
- static bool
- dsm_parse(uint64_t now, uint8_t *frame, unsigned len, uint16_t *values,
- uint16_t *num_values, bool *dsm_11_bit, unsigned *frame_drops, uint16_t max_channels)
- {
-
- bool decode_ret = false;
-
- for (unsigned d = 0; d < len; d++) {
-
- if (dsm_partial_frame_count == sizeof(dsm_frame) / sizeof(dsm_frame[0])) {
- dsm_partial_frame_count = 0;
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- #ifdef DSM_DEBUG
- printf("DSM: RESET (BUF LIM)\n");
- #endif
- }
- if (dsm_partial_frame_count == DSM_FRAME_SIZE) {
- dsm_partial_frame_count = 0;
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- #ifdef DSM_DEBUG
- printf("DSM: RESET (PACKET LIM)\n");
- #endif
- }
- #ifdef DSM_DEBUG
- #if 1
- printf("dsm state: %s%s, count: %d, val: %02x\n",
- (dsm_decode_state == DSM_DECODE_STATE_DESYNC) ? "DSM_DECODE_STATE_DESYNC" : "",
- (dsm_decode_state == DSM_DECODE_STATE_SYNC) ? "DSM_DECODE_STATE_SYNC" : "",
- dsm_partial_frame_count,
- (unsigned)frame[d]);
- #endif
- #endif
- switch (dsm_decode_state) {
- case DSM_DECODE_STATE_DESYNC:
-
- if ((now - dsm_last_rx_time) > 5000) {
- printf("resync %u\n", dsm_partial_frame_count);
- dsm_decode_state = DSM_DECODE_STATE_SYNC;
- dsm_partial_frame_count = 0;
- dsm_chan_count = 0;
- dsm_frame[dsm_partial_frame_count++] = frame[d];
- }
- break;
- case DSM_DECODE_STATE_SYNC: {
- dsm_frame[dsm_partial_frame_count++] = frame[d];
-
- if (dsm_partial_frame_count < DSM_FRAME_SIZE) {
- break;
- }
-
- decode_ret = dsm_decode(now, dsm_frame, values, &dsm_chan_count, max_channels);
- #if 1
- printf("%u %u: ", ((unsigned)(now/1000)) % 1000000, len);
- for (uint8_t i=0; i<DSM_FRAME_SIZE; i++) {
- printf("%02x ", (unsigned)dsm_frame[i]);
- }
- printf("\n");
- #endif
-
-
-
- dsm_partial_frame_count = 0;
-
- if (decode_ret == false) {
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- dsm_frame_drops++;
- printf("drop ");
- for (uint8_t i=0; i<DSM_FRAME_SIZE; i++) {
- printf("%02x ", (unsigned)dsm_frame[i]);
- }
- printf("\n");
- }
- }
- break;
- default:
- #ifdef DSM_DEBUG
- printf("UNKNOWN PROTO STATE");
- #endif
- decode_ret = false;
- }
- }
- if (frame_drops) {
- *frame_drops = dsm_frame_drops;
- }
- if (decode_ret) {
- *num_values = dsm_chan_count;
- }
- dsm_last_rx_time = now;
-
- return decode_ret;
- }
- #endif
- #ifdef TEST_MAIN_PROGRAM
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <time.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <termios.h>
- #include <string.h>
- static uint64_t micros64(void)
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return 1.0e6*((ts.tv_sec + (ts.tv_nsec*1.0e-9)));
- }
- int main(int argc, const char *argv[])
- {
- int fd = open(argv[1], O_RDONLY|O_CLOEXEC);
- if (fd == -1) {
- perror(argv[1]);
- exit(1);
- }
- struct termios options;
- tcgetattr(fd, &options);
- cfsetispeed(&options, B115200);
- cfsetospeed(&options, B115200);
- options.c_cflag &= ~(PARENB|CSTOPB|CSIZE);
- options.c_cflag |= CS8;
- options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
- options.c_iflag &= ~(IXON|IXOFF|IXANY);
- options.c_oflag &= ~OPOST;
- if (tcsetattr(fd, TCSANOW, &options) != 0) {
- perror("tcsetattr");
- exit(1);
- }
- tcflush(fd, TCIOFLUSH);
- uint16_t values[18];
- memset(values, 0, sizeof(values));
- while (true) {
- uint8_t b[16];
- uint16_t num_values = 0;
- fd_set fds;
- struct timeval tv;
-
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
- tv.tv_sec = 1;
- tv.tv_usec = 0;
-
- if (select(fd+1, &fds, nullptr, nullptr, &tv) != 1) {
- break;
- }
- ssize_t nread;
- if ((nread = read(fd, b, sizeof(b))) < 1) {
- break;
- }
- bool dsm_11_bit;
- unsigned frame_drops;
-
- if (dsm_parse(micros64(), b, nread, values, &num_values, &dsm_11_bit, &frame_drops, 18)) {
- #if 1
- printf("%u: ", num_values);
- for (uint8_t i=0; i<num_values; i++) {
- printf("%u:%4u ", i+1, values[i]);
- }
- printf("\n");
- #endif
- }
- }
- }
- #elif defined(TEST_HEX_STRING)
- #include <string.h>
- int main(int argc, const char *argv[])
- {
- uint8_t b[16];
- uint64_t t = 0;
- for (uint8_t i=1; i<argc; i++) {
- unsigned v;
- if (sscanf(argv[i], "%02x", &v) != 1 || v > 255) {
- printf("Bad hex value at %u : %s\n", (unsigned)i, argv[i]);
- return 1;
- }
- b[i-1] = v;
- }
- uint16_t values[18];
- memset(values, 0, sizeof(values));
-
- while (true) {
- uint16_t num_values = 0;
- bool dsm_11_bit;
- unsigned frame_drops;
- t += 11000;
-
- if (dsm_parse(t, b, sizeof(b), values, &num_values, &dsm_11_bit, &frame_drops, 18)) {
- #if 1
- printf("%u: ", num_values);
- for (uint8_t i=0; i<num_values; i++) {
- printf("%u:%4u ", i+1, values[i]);
- }
- printf("\n");
- #endif
- }
- }
- }
- #endif
|