2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
27 #ifdef USE_SERIALRX_SUMD
29 #include "common/crc.h"
30 #include "common/utils.h"
31 #include "common/maths.h"
33 #include "drivers/time.h"
35 #include "io/serial.h"
38 #include "telemetry/telemetry.h"
46 // driver for SUMD receiver using UART2
48 // Support for SUMD and SUMD V3
49 // Tested with 16 channels, SUMD supports up to 16(*), SUMD V3 up to 32 (MZ-32) channels, but limit to MAX_SUPPORTED_RC_CHANNEL_COUNT (currently 8, BF 3.4)
50 // * According to the original SUMD V1 documentation, SUMD V1 already supports up to 32 Channels?!?
52 #define SUMD_SYNCBYTE 0xA8
53 #define SUMD_MAX_CHANNEL 32
54 #define SUMD_BUFFSIZE (SUMD_MAX_CHANNEL * 2 + 5) // 6 channels + 5 = 17 bytes for 6 channels
56 #define SUMD_BAUDRATE 115200
58 static bool sumdFrameDone
= false;
59 static uint16_t sumdChannels
[MAX_SUPPORTED_RC_CHANNEL_COUNT
];
62 static uint8_t sumd
[SUMD_BUFFSIZE
] = { 0, };
63 static uint8_t sumdChannelCount
;
65 // Receive ISR callback
66 static void sumdDataReceive(uint16_t c
, void *data
)
71 static uint32_t sumdTimeLast
;
72 static uint8_t sumdIndex
;
75 if ((sumdTime
- sumdTimeLast
) > 4000)
77 sumdTimeLast
= sumdTime
;
80 if (c
!= SUMD_SYNCBYTE
)
84 sumdFrameDone
= false; // lazy main loop didnt fetch the stuff
89 sumdChannelCount
= (uint8_t)c
;
90 if (sumdIndex
< SUMD_BUFFSIZE
)
91 sumd
[sumdIndex
] = (uint8_t)c
;
93 if (sumdIndex
< sumdChannelCount
* 2 + 4)
94 crc
= crc16_ccitt(crc
, (uint8_t)c
);
96 if (sumdIndex
== sumdChannelCount
* 2 + 5) {
102 #define SUMD_OFFSET_CHANNEL_1_HIGH 3
103 #define SUMD_OFFSET_CHANNEL_1_LOW 4
104 #define SUMD_BYTES_PER_CHANNEL 2
107 #define SUMDV1_FRAME_STATE_OK 0x01
108 #define SUMDV3_FRAME_STATE_OK 0x03
109 #define SUMD_FRAME_STATE_FAILSAFE 0x81
111 static uint8_t sumdFrameStatus(rxRuntimeConfig_t
*rxRuntimeConfig
)
113 UNUSED(rxRuntimeConfig
);
115 uint8_t frameStatus
= RX_FRAME_PENDING
;
117 if (!sumdFrameDone
) {
121 sumdFrameDone
= false;
124 if (crc
!= ((sumd
[SUMD_BYTES_PER_CHANNEL
* sumdChannelCount
+ SUMD_OFFSET_CHANNEL_1_HIGH
] << 8) |
125 (sumd
[SUMD_BYTES_PER_CHANNEL
* sumdChannelCount
+ SUMD_OFFSET_CHANNEL_1_LOW
])))
129 case SUMD_FRAME_STATE_FAILSAFE
:
130 frameStatus
= RX_FRAME_COMPLETE
| RX_FRAME_FAILSAFE
;
132 case SUMDV1_FRAME_STATE_OK
:
133 case SUMDV3_FRAME_STATE_OK
:
134 frameStatus
= RX_FRAME_COMPLETE
;
140 unsigned channelsToProcess
= MIN(sumdChannelCount
, MAX_SUPPORTED_RC_CHANNEL_COUNT
);
142 for (unsigned channelIndex
= 0; channelIndex
< channelsToProcess
; channelIndex
++) {
143 sumdChannels
[channelIndex
] = (
144 (sumd
[SUMD_BYTES_PER_CHANNEL
* channelIndex
+ SUMD_OFFSET_CHANNEL_1_HIGH
] << 8) |
145 sumd
[SUMD_BYTES_PER_CHANNEL
* channelIndex
+ SUMD_OFFSET_CHANNEL_1_LOW
]
151 static uint16_t sumdReadRawRC(const rxRuntimeConfig_t
*rxRuntimeConfig
, uint8_t chan
)
153 UNUSED(rxRuntimeConfig
);
154 return sumdChannels
[chan
] / 8;
157 bool sumdInit(const rxConfig_t
*rxConfig
, rxRuntimeConfig_t
*rxRuntimeConfig
)
161 rxRuntimeConfig
->channelCount
= MIN(SUMD_MAX_CHANNEL
, MAX_SUPPORTED_RC_CHANNEL_COUNT
);
162 rxRuntimeConfig
->rxRefreshRate
= 11000;
164 rxRuntimeConfig
->rcReadRawFn
= sumdReadRawRC
;
165 rxRuntimeConfig
->rcFrameStatusFn
= sumdFrameStatus
;
167 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
173 bool portShared
= telemetryCheckRxPortShared(portConfig
);
175 bool portShared
= false;
178 serialPort_t
*sumdPort
= openSerialPort(portConfig
->identifier
,
183 portShared
? MODE_RXTX
: MODE_RX
,
184 (rxConfig
->serialrx_inverted
? SERIAL_INVERTED
: 0) | (rxConfig
->halfDuplex
? SERIAL_BIDIR
: 0)
189 telemetrySharedPort
= sumdPort
;
193 return sumdPort
!= NULL
;