AP_RCProtocol_SUMD.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 SUMD_MAX_CHANNELS 32
  21. #define SUMD_FRAME_MAXLEN 40
  22. class AP_RCProtocol_SUMD : public AP_RCProtocol_Backend {
  23. public:
  24. AP_RCProtocol_SUMD(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {}
  25. void process_pulse(uint32_t width_s0, uint32_t width_s1) override;
  26. void process_byte(uint8_t byte, uint32_t baudrate) override;
  27. private:
  28. void _process_byte(uint32_t timestamp_us, uint8_t byte);
  29. static uint8_t sumd_crc8(uint8_t crc, uint8_t value);
  30. #pragma pack(push, 1)
  31. typedef struct {
  32. uint8_t header; ///< 0xA8 for a valid packet
  33. uint8_t status; ///< 0x01 valid and live SUMD data frame / 0x00 = SUMH / 0x81 = Failsafe
  34. uint8_t length; ///< Channels
  35. uint8_t sumd_data[(SUMD_MAX_CHANNELS+1) * 2]; ///< ChannelData (High Byte/ Low Byte)
  36. uint8_t crc16_high; ///< High Byte of 16 Bit CRC
  37. uint8_t crc16_low; ///< Low Byte of 16 Bit CRC
  38. uint8_t telemetry; ///< Telemetry request
  39. uint8_t crc8; ///< SUMH CRC8
  40. } ReceiverFcPacketHoTT;
  41. #pragma pack(pop)
  42. enum SUMD_DECODE_STATE {
  43. SUMD_DECODE_STATE_UNSYNCED = 0,
  44. SUMD_DECODE_STATE_GOT_HEADER,
  45. SUMD_DECODE_STATE_GOT_STATE,
  46. SUMD_DECODE_STATE_GOT_LEN,
  47. SUMD_DECODE_STATE_GOT_DATA,
  48. SUMD_DECODE_STATE_GOT_CRC,
  49. SUMD_DECODE_STATE_GOT_CRC16_BYTE_1,
  50. SUMD_DECODE_STATE_GOT_CRC16_BYTE_2
  51. };
  52. enum SUMD_DECODE_STATE _decode_state = SUMD_DECODE_STATE_UNSYNCED;
  53. uint8_t _rxlen;
  54. ReceiverFcPacketHoTT _rxpacket;
  55. uint8_t _crc8 = 0x00;
  56. uint16_t _crc16 = 0x0000;
  57. bool _sumd = true;
  58. bool _crcOK = false;
  59. uint32_t last_packet_us;
  60. SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1};
  61. };