AP_RCProtocol_SRXL.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #pragma once
  18. #include "AP_RCProtocol.h"
  19. #include "SoftSerial.h"
  20. #define SRXL_MIN_FRAMESPACE_US 8000U /* Minumum space between srxl frames in us (applies to all variants) */
  21. #define SRXL_MAX_CHANNELS 20U /* Maximum number of channels from srxl datastream */
  22. /* Variant specific SRXL datastream characteristics */
  23. /* Framelength in byte */
  24. #define SRXL_FRAMELEN_V1 27U /* Framelength with header in byte for: Mpx SRXLv1 or XBUS Mode B */
  25. #define SRXL_FRAMELEN_V2 35U /* Framelength with header in byte for: Mpx SRXLv2 */
  26. #define SRXL_FRAMELEN_V5 18U /* Framelength with header in byte for Spk AR7700 etc. */
  27. #define SRXL_FRAMELEN_MAX 35U /* maximum possible framelengh */
  28. /* Headerbyte */
  29. #define SRXL_HEADER_V1 0xA1U /* Headerbyte for: Mpx SRXLv1 or XBUS Mode B */
  30. #define SRXL_HEADER_V2 0xA2U /* Headerbyte for: Mpx SRXLv2 */
  31. #define SRXL_HEADER_V5 0xA5U /* Headerbyte for: Spk AR7700 etc. */
  32. #define SRXL_HEADER_NOT_IMPL 0xFFU /* Headerbyte for non impemented srxl header*/
  33. class AP_RCProtocol_SRXL : public AP_RCProtocol_Backend {
  34. public:
  35. AP_RCProtocol_SRXL(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {}
  36. void process_pulse(uint32_t width_s0, uint32_t width_s1) override;
  37. void process_byte(uint8_t byte, uint32_t baudrate) override;
  38. private:
  39. void _process_byte(uint32_t timestamp_us, uint8_t byte);
  40. int srxl_channels_get_v1v2(uint16_t max_values, uint8_t *num_values, uint16_t *values, bool *failsafe_state);
  41. int srxl_channels_get_v5(uint16_t max_values, uint8_t *num_values, uint16_t *values, bool *failsafe_state);
  42. uint8_t buffer[SRXL_FRAMELEN_MAX]; /* buffer for raw srxl frame data in correct order --> buffer[0]=byte0 buffer[1]=byte1 */
  43. uint8_t buflen; /* length in number of bytes of received srxl dataframe in buffer */
  44. uint32_t last_data_us; /* timespan since last received data in us */
  45. uint16_t channels[SRXL_MAX_CHANNELS] = {0}; /* buffer for extracted RC channel data as pulsewidth in microseconds */
  46. uint16_t max_channels = 0;
  47. enum {
  48. STATE_IDLE, /* do nothing */
  49. STATE_NEW, /* get header of frame + prepare for frame reception + begin new crc cycle */
  50. STATE_COLLECT /* collect RC channel data from frame + concurrently calc crc over payload data + extract channel information */
  51. };
  52. uint8_t frame_header = 0U; /* Frame header from SRXL datastream */
  53. uint8_t frame_len_full = 0U; /* Length in number of bytes of full srxl datastream */
  54. uint8_t decode_state = STATE_IDLE; /* Current state of SRXL frame decoding */
  55. uint8_t decode_state_next = STATE_IDLE; /* State of frame decoding thatwill be applied when the next byte from dataframe drops in */
  56. uint16_t crc_fmu = 0U; /* CRC calculated over payload from srxl datastream on this machine */
  57. uint16_t crc_receiver = 0U; /* CRC extracted from srxl datastream */
  58. SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1};
  59. };