Line data Source code
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 : 18 : #pragma once 19 : 20 : #include "AP_RCProtocol_config.h" 21 : 22 : #if AP_RCPROTOCOL_SRXL_ENABLED 23 : 24 : #include "AP_RCProtocol.h" 25 : #include "SoftSerial.h" 26 : 27 : #define SRXL_MIN_FRAMESPACE_US 8000U /* Minumum space between srxl frames in us (applies to all variants) */ 28 : #define SRXL_MAX_CHANNELS 20U /* Maximum number of channels from srxl datastream */ 29 : 30 : /* Variant specific SRXL datastream characteristics */ 31 : /* Framelength in byte */ 32 : #define SRXL_FRAMELEN_V1 27U /* Framelength with header in byte for: Mpx SRXLv1 or XBUS Mode B */ 33 : #define SRXL_FRAMELEN_V2 35U /* Framelength with header in byte for: Mpx SRXLv2 */ 34 : #define SRXL_FRAMELEN_V5 18U /* Framelength with header in byte for Spk AR7700 etc. */ 35 : #define SRXL_FRAMELEN_MAX 35U /* maximum possible framelengh */ 36 : 37 : /* Headerbyte */ 38 : #define SRXL_HEADER_V1 0xA1U /* Headerbyte for: Mpx SRXLv1 or XBUS Mode B */ 39 : #define SRXL_HEADER_V2 0xA2U /* Headerbyte for: Mpx SRXLv2 */ 40 : #define SRXL_HEADER_V5 0xA5U /* Headerbyte for: Spk AR7700 etc. */ 41 : #define SRXL_HEADER_NOT_IMPL 0xFFU /* Headerbyte for non impemented srxl header*/ 42 : 43 : class AP_RCProtocol_SRXL : public AP_RCProtocol_Backend { 44 : public: 45 2094 : AP_RCProtocol_SRXL(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {} 46 : void process_pulse(uint32_t width_s0, uint32_t width_s1) override; 47 : void process_byte(uint8_t byte, uint32_t baudrate) override; 48 : private: 49 : void _process_byte(uint32_t timestamp_us, uint8_t byte); 50 : int srxl_channels_get_v1v2(uint16_t max_values, uint8_t *num_values, uint16_t *values, bool *failsafe_state); 51 : int srxl_channels_get_v5(uint16_t max_values, uint8_t *num_values, uint16_t *values, bool *failsafe_state); 52 : uint8_t buffer[SRXL_FRAMELEN_MAX]; /* buffer for raw srxl frame data in correct order --> buffer[0]=byte0 buffer[1]=byte1 */ 53 : uint8_t buflen; /* length in number of bytes of received srxl dataframe in buffer */ 54 : uint32_t last_data_us; /* timespan since last received data in us */ 55 : uint16_t channels[SRXL_MAX_CHANNELS] = {0}; /* buffer for extracted RC channel data as pulsewidth in microseconds */ 56 : uint16_t max_channels = 0; 57 : enum { 58 : STATE_IDLE, /* do nothing */ 59 : STATE_NEW, /* get header of frame + prepare for frame reception + begin new crc cycle */ 60 : STATE_COLLECT /* collect RC channel data from frame + concurrently calc crc over payload data + extract channel information */ 61 : }; 62 : uint8_t frame_header = 0U; /* Frame header from SRXL datastream */ 63 : uint8_t frame_len_full = 0U; /* Length in number of bytes of full srxl datastream */ 64 : uint8_t decode_state = STATE_IDLE; /* Current state of SRXL frame decoding */ 65 : uint8_t decode_state_next = STATE_IDLE; /* State of frame decoding thatwill be applied when the next byte from dataframe drops in */ 66 : uint16_t crc_fmu = 0U; /* CRC calculated over payload from srxl datastream on this machine */ 67 : uint16_t crc_receiver = 0U; /* CRC extracted from srxl datastream */ 68 : 69 : SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1}; 70 : }; 71 : 72 : #endif // AP_RCPROTOCOL_SRXL_ENABLED