123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- #include "AP_RCProtocol_DSM.h"
- extern const AP_HAL::HAL& hal;
- #ifdef DSM_DEBUG
- # define debug(fmt, args...) printf(fmt "\n", ##args)
- #else
- # define debug(fmt, args...) do {} while(0)
- #endif
- #define DSM_FRAME_SIZE 16
- #define DSM_FRAME_CHANNELS 7
- void AP_RCProtocol_DSM::process_pulse(uint32_t width_s0, uint32_t width_s1)
- {
- uint8_t b;
- if (ss.process_pulse(width_s0, width_s1, b)) {
- _process_byte(ss.get_byte_timestamp_us()/1000U, b);
- }
- }
- bool AP_RCProtocol_DSM::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;
- }
- void AP_RCProtocol_DSM::dsm_guess_format(bool reset, const uint8_t dsm_frame[16])
- {
-
- if (reset) {
- cs10 = 0;
- cs11 = 0;
- samples = 0;
- 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 const 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)) {
- channel_shift = 11;
- debug("DSM: 11-bit format");
- return;
- }
- if ((votes10 == 1) && (votes11 == 0)) {
- 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 AP_RCProtocol_DSM::dsm_decode(uint32_t frame_time_ms, const uint8_t dsm_frame[16],
- uint16_t *values, uint16_t *num_values, uint16_t max_values)
- {
-
- if (((frame_time_ms - last_frame_time_ms) > 200U) && (channel_shift != 0)) {
- dsm_guess_format(true, dsm_frame);
- }
-
- last_frame_time_ms = frame_time_ms;
-
- if (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, channel_shift, &channel, &value)) {
- continue;
- }
-
- if (channel >= max_values) {
- continue;
- }
-
- if (channel >= *num_values) {
- *num_values = channel + 1;
- }
-
- if (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 (channel_shift == 11) {
-
- *num_values |= 0x8000;
- }
- #endif
-
- return true;
- }
- void AP_RCProtocol_DSM::start_bind(void)
- {
- bind_state = BIND_STATE1;
- }
- void AP_RCProtocol_DSM::update(void)
- {
- #if defined(HAL_GPIO_SPEKTRUM_PWR) && defined(HAL_GPIO_SPEKTRUM_RC)
- switch (bind_state) {
- case BIND_STATE_NONE:
- break;
- case BIND_STATE1:
- hal.gpio->write(HAL_GPIO_SPEKTRUM_PWR, !HAL_SPEKTRUM_PWR_ENABLED);
- hal.gpio->pinMode(HAL_GPIO_SPEKTRUM_RC, 1);
- hal.gpio->write(HAL_GPIO_SPEKTRUM_RC, 1);
- bind_last_ms = AP_HAL::millis();
- bind_state = BIND_STATE2;
- break;
- case BIND_STATE2: {
- uint32_t now = AP_HAL::millis();
- if (now - bind_last_ms > 500) {
- hal.gpio->write(HAL_GPIO_SPEKTRUM_PWR, HAL_SPEKTRUM_PWR_ENABLED);
- bind_last_ms = now;
- bind_state = BIND_STATE3;
- }
- break;
- }
- case BIND_STATE3: {
- uint32_t now = AP_HAL::millis();
- if (now - bind_last_ms > 72) {
-
-
- const uint8_t num_pulses = 9;
- for (uint8_t i=0; i<num_pulses; i++) {
- hal.scheduler->delay_microseconds(120);
- hal.gpio->write(HAL_GPIO_SPEKTRUM_RC, 0);
- hal.scheduler->delay_microseconds(120);
- hal.gpio->write(HAL_GPIO_SPEKTRUM_RC, 1);
- }
- bind_last_ms = now;
- bind_state = BIND_STATE4;
- }
- break;
- }
- case BIND_STATE4: {
- uint32_t now = AP_HAL::millis();
- if (now - bind_last_ms > 50) {
- hal.gpio->pinMode(HAL_GPIO_SPEKTRUM_RC, 0);
- bind_state = BIND_STATE_NONE;
- }
- break;
- }
- }
- #endif
- }
- bool AP_RCProtocol_DSM::dsm_parse_byte(uint32_t frame_time_ms, uint8_t b, uint16_t *values,
- uint16_t *num_values, uint16_t max_channels)
- {
-
- bool decode_ret = false;
-
- if (byte_input.ofs == sizeof(byte_input.buf) / sizeof(byte_input.buf[0])) {
- byte_input.ofs = 0;
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- debug("DSM: RESET (BUF LIM)\n");
- }
- if (byte_input.ofs == DSM_FRAME_SIZE) {
- byte_input.ofs = 0;
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- debug("DSM: RESET (PACKET LIM)\n");
- }
- #ifdef DSM_DEBUG
- debug("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" : "",
- byte_input.ofs,
- (unsigned)b);
- #endif
- switch (dsm_decode_state) {
- case DSM_DECODE_STATE_DESYNC:
-
- if ((frame_time_ms - last_rx_time_ms) >= 5) {
- dsm_decode_state = DSM_DECODE_STATE_SYNC;
- byte_input.ofs = 0;
- byte_input.buf[byte_input.ofs++] = b;
- }
- break;
- case DSM_DECODE_STATE_SYNC: {
- if ((frame_time_ms - last_rx_time_ms) >= 5 && byte_input.ofs > 0) {
- byte_input.ofs = 0;
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- break;
- }
- byte_input.buf[byte_input.ofs++] = b;
-
- if (byte_input.ofs < DSM_FRAME_SIZE) {
- break;
- }
-
- decode_ret = dsm_decode(frame_time_ms, byte_input.buf, values, &chan_count, max_channels);
-
- byte_input.ofs = 0;
-
- if (decode_ret == false) {
- dsm_decode_state = DSM_DECODE_STATE_DESYNC;
- }
- break;
- }
- default:
- debug("UNKNOWN PROTO STATE");
- decode_ret = false;
- }
- if (decode_ret) {
- *num_values = chan_count;
- }
- last_rx_time_ms = frame_time_ms;
-
- return decode_ret;
- }
- void AP_RCProtocol_DSM::_process_byte(uint32_t timestamp_ms, uint8_t b)
- {
- uint16_t v[AP_DSM_MAX_CHANNELS];
- uint16_t nchan;
- memcpy(v, last_values, sizeof(v));
- if (dsm_parse_byte(timestamp_ms, b, v, &nchan, AP_DSM_MAX_CHANNELS)) {
- memcpy(last_values, v, sizeof(v));
- if (nchan >= MIN_RCIN_CHANNELS) {
- add_input(nchan, last_values, false);
- }
- }
- }
- void AP_RCProtocol_DSM::process_byte(uint8_t b, uint32_t baudrate)
- {
- if (baudrate != 115200) {
- return;
- }
- _process_byte(AP_HAL::millis(), b);
- }
|