New memory section types for DMA
[betaflight.git] / src / main / rx / sumd.c
blob0b99c620f2bb1fe9d4e5408048090e00f644a15f
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
25 #include "platform.h"
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"
37 #ifdef USE_TELEMETRY
38 #include "telemetry/telemetry.h"
39 #endif
41 #include "pg/rx.h"
43 #include "rx/rx.h"
44 #include "rx/sumd.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
57 #define SUMD_TIME_NEEDED_PER_FRAME 4000
59 #define SUMD_OFFSET_CHANNEL_1_HIGH 3
60 #define SUMD_OFFSET_CHANNEL_1_LOW 4
61 #define SUMD_BYTES_PER_CHANNEL 2
62 #define SUMD_SYNC_BYTE_INDEX 0
63 #define SUMD_CHANNEL_COUNT_INDEX 2
65 #define SUMD_HEADER_LENGTH 3
66 #define SUMD_CRC_LENGTH 2
68 #define SUMDV1_FRAME_STATE_OK 0x01
69 #define SUMDV3_FRAME_STATE_OK 0x03
70 #define SUMD_FRAME_STATE_FAILSAFE 0x81
72 static bool sumdFrameDone = false;
73 static uint16_t sumdChannels[MAX_SUPPORTED_RC_CHANNEL_COUNT];
74 static uint16_t crc;
76 static uint8_t sumd[SUMD_BUFFSIZE] = { 0, };
77 static uint8_t sumdChannelCount;
78 static timeUs_t lastFrameTimeUs = 0;
79 static timeUs_t lastRcFrameTimeUs = 0;
81 // Receive ISR callback
82 static void sumdDataReceive(uint16_t c, void *data)
84 UNUSED(data);
86 static timeUs_t sumdTimeLast;
87 static uint8_t sumdIndex;
89 const timeUs_t now = microsISR();
90 if (cmpTimeUs(now, sumdTimeLast) > SUMD_TIME_NEEDED_PER_FRAME) {
91 sumdIndex = 0;
93 sumdTimeLast = now;
95 if (sumdIndex == SUMD_SYNC_BYTE_INDEX) {
96 if (c != SUMD_SYNCBYTE) {
97 return;
98 } else {
99 sumdFrameDone = false; // lazy main loop didnt fetch the stuff
100 crc = 0;
102 } else if (sumdIndex == SUMD_CHANNEL_COUNT_INDEX) {
103 sumdChannelCount = (uint8_t)c;
106 if (sumdIndex < SUMD_BUFFSIZE) {
107 sumd[sumdIndex] = (uint8_t)c;
110 sumdIndex++;
111 if (sumdIndex <= sumdChannelCount * SUMD_BYTES_PER_CHANNEL + SUMD_HEADER_LENGTH) {
112 crc = crc16_ccitt(crc, (uint8_t)c);
113 } else if (sumdIndex == sumdChannelCount * SUMD_BYTES_PER_CHANNEL + SUMD_HEADER_LENGTH + SUMD_CRC_LENGTH) {
114 lastFrameTimeUs = now;
115 sumdIndex = 0;
116 sumdFrameDone = true;
120 static uint8_t sumdFrameStatus(rxRuntimeState_t *rxRuntimeState)
122 UNUSED(rxRuntimeState);
124 uint8_t frameStatus = RX_FRAME_PENDING;
126 if (!sumdFrameDone) {
127 return frameStatus;
130 sumdFrameDone = false;
132 // verify CRC
133 if (crc == ((sumd[SUMD_BYTES_PER_CHANNEL * sumdChannelCount + SUMD_OFFSET_CHANNEL_1_HIGH] << 8) |
134 (sumd[SUMD_BYTES_PER_CHANNEL * sumdChannelCount + SUMD_OFFSET_CHANNEL_1_LOW]))) {
136 switch (sumd[1]) {
137 case SUMD_FRAME_STATE_FAILSAFE:
138 frameStatus = RX_FRAME_COMPLETE | RX_FRAME_FAILSAFE;
139 break;
140 case SUMDV1_FRAME_STATE_OK:
141 case SUMDV3_FRAME_STATE_OK:
142 frameStatus = RX_FRAME_COMPLETE;
143 break;
146 if (frameStatus & RX_FRAME_COMPLETE) {
147 const unsigned channelsToProcess = MIN(sumdChannelCount, MAX_SUPPORTED_RC_CHANNEL_COUNT);
149 for (unsigned channelIndex = 0; channelIndex < channelsToProcess; channelIndex++) {
150 sumdChannels[channelIndex] = (
151 (sumd[SUMD_BYTES_PER_CHANNEL * channelIndex + SUMD_OFFSET_CHANNEL_1_HIGH] << 8) |
152 sumd[SUMD_BYTES_PER_CHANNEL * channelIndex + SUMD_OFFSET_CHANNEL_1_LOW]
158 if (!(frameStatus & (RX_FRAME_FAILSAFE | RX_FRAME_DROPPED))) {
159 lastRcFrameTimeUs = lastFrameTimeUs;
162 return frameStatus;
165 static float sumdReadRawRC(const rxRuntimeState_t *rxRuntimeState, uint8_t chan)
167 UNUSED(rxRuntimeState);
168 return (float)sumdChannels[chan] / 8;
171 static timeUs_t sumdFrameTimeUsFn(void)
173 return lastRcFrameTimeUs;
176 bool sumdInit(const rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState)
178 UNUSED(rxConfig);
180 rxRuntimeState->channelCount = MIN(SUMD_MAX_CHANNEL, MAX_SUPPORTED_RC_CHANNEL_COUNT);
181 rxRuntimeState->rxRefreshRate = 11000;
183 rxRuntimeState->rcReadRawFn = sumdReadRawRC;
184 rxRuntimeState->rcFrameStatusFn = sumdFrameStatus;
185 rxRuntimeState->rcFrameTimeUsFn = sumdFrameTimeUsFn;
187 const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_RX_SERIAL);
188 if (!portConfig) {
189 return false;
192 #ifdef USE_TELEMETRY
193 bool portShared = telemetryCheckRxPortShared(portConfig, rxRuntimeState->serialrxProvider);
194 #else
195 bool portShared = false;
196 #endif
198 serialPort_t *sumdPort = openSerialPort(portConfig->identifier,
199 FUNCTION_RX_SERIAL,
200 sumdDataReceive,
201 NULL,
202 SUMD_BAUDRATE,
203 portShared ? MODE_RXTX : MODE_RX,
204 (rxConfig->serialrx_inverted ? SERIAL_INVERTED : 0) | (rxConfig->halfDuplex ? SERIAL_BIDIR : 0)
207 #ifdef USE_TELEMETRY
208 if (portShared) {
209 telemetrySharedPort = sumdPort;
211 #endif
213 return sumdPort != NULL;
215 #endif