2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
24 #ifdef USE_SERIALRX_SUMD
26 #include "common/utils.h"
28 #include "drivers/time.h"
29 #include "drivers/serial.h"
30 #include "drivers/serial_uart.h"
32 #include "io/serial.h"
37 #include "telemetry/telemetry.h"
39 // driver for SUMD receiver using UART2
41 // FIXME test support for more than 8 channels, should probably work up to 12 channels
43 #define SUMD_SYNCBYTE 0xA8
44 #define SUMD_MAX_CHANNEL 16
45 #define SUMD_BUFFSIZE (SUMD_MAX_CHANNEL * 2 + 5) // 6 channels + 5 = 17 bytes for 6 channels
47 #define SUMD_BAUDRATE 115200
49 static bool sumdFrameDone
= false;
50 static uint16_t sumdChannels
[SUMD_MAX_CHANNEL
];
53 #define CRC_POLYNOME 0x1021
55 // CRC calculation, adds a 8 bit unsigned to 16 bit crc
56 static void CRC16(uint8_t value
)
60 crc
= crc
^ (int16_t)value
<< 8;
61 for (i
= 0; i
< 8; i
++) {
63 crc
= (crc
<< 1) ^ CRC_POLYNOME
;
69 static uint8_t sumd
[SUMD_BUFFSIZE
] = { 0, };
70 static uint8_t sumdChannelCount
;
72 // Receive ISR callback
73 static void sumdDataReceive(uint16_t c
, void *rxCallbackData
)
75 UNUSED(rxCallbackData
);
78 static timeUs_t sumdTimeLast
;
79 static uint8_t sumdIndex
;
82 if (cmpTimeUs(sumdTime
, sumdTimeLast
) > MS2US(4))
84 sumdTimeLast
= sumdTime
;
87 if (c
!= SUMD_SYNCBYTE
)
91 sumdFrameDone
= false; // lazy main loop didnt fetch the stuff
96 sumdChannelCount
= (uint8_t)c
;
97 if (sumdIndex
< SUMD_BUFFSIZE
)
98 sumd
[sumdIndex
] = (uint8_t)c
;
100 if (sumdIndex
< sumdChannelCount
* 2 + 4)
103 if (sumdIndex
== sumdChannelCount
* 2 + 5) {
105 sumdFrameDone
= true;
109 #define SUMD_OFFSET_CHANNEL_1_HIGH 3
110 #define SUMD_OFFSET_CHANNEL_1_LOW 4
111 #define SUMD_BYTES_PER_CHANNEL 2
114 #define SUMD_FRAME_STATE_OK 0x01
115 #define SUMD_FRAME_STATE_FAILSAFE 0x81
117 static uint8_t sumdFrameStatus(rxRuntimeConfig_t
*rxRuntimeConfig
)
119 UNUSED(rxRuntimeConfig
);
121 uint8_t channelIndex
;
123 uint8_t frameStatus
= RX_FRAME_PENDING
;
125 if (!sumdFrameDone
) {
129 sumdFrameDone
= false;
132 if (crc
!= ((sumd
[SUMD_BYTES_PER_CHANNEL
* sumdChannelCount
+ SUMD_OFFSET_CHANNEL_1_HIGH
] << 8) |
133 (sumd
[SUMD_BYTES_PER_CHANNEL
* sumdChannelCount
+ SUMD_OFFSET_CHANNEL_1_LOW
])))
137 case SUMD_FRAME_STATE_FAILSAFE
:
138 frameStatus
= RX_FRAME_COMPLETE
| RX_FRAME_FAILSAFE
;
140 case SUMD_FRAME_STATE_OK
:
141 frameStatus
= RX_FRAME_COMPLETE
;
147 if (sumdChannelCount
> SUMD_MAX_CHANNEL
)
148 sumdChannelCount
= SUMD_MAX_CHANNEL
;
150 for (channelIndex
= 0; channelIndex
< sumdChannelCount
; channelIndex
++) {
151 sumdChannels
[channelIndex
] = (
152 (sumd
[SUMD_BYTES_PER_CHANNEL
* channelIndex
+ SUMD_OFFSET_CHANNEL_1_HIGH
] << 8) |
153 sumd
[SUMD_BYTES_PER_CHANNEL
* channelIndex
+ SUMD_OFFSET_CHANNEL_1_LOW
]
159 static uint16_t sumdReadRawRC(const rxRuntimeConfig_t
*rxRuntimeConfig
, uint8_t chan
)
161 UNUSED(rxRuntimeConfig
);
162 return sumdChannels
[chan
] / 8;
165 bool sumdInit(const rxConfig_t
*rxConfig
, rxRuntimeConfig_t
*rxRuntimeConfig
)
169 rxRuntimeConfig
->channelCount
= SUMD_MAX_CHANNEL
;
170 rxRuntimeConfig
->rcReadRawFn
= sumdReadRawRC
;
171 rxRuntimeConfig
->rcFrameStatusFn
= sumdFrameStatus
;
173 const serialPortConfig_t
*portConfig
= findSerialPortConfig(FUNCTION_RX_SERIAL
);
179 bool portShared
= telemetryCheckRxPortShared(portConfig
);
181 bool portShared
= false;
184 serialPort_t
*sumdPort
= openSerialPort(portConfig
->identifier
,
189 portShared
? MODE_RXTX
: MODE_RX
,
190 SERIAL_NOT_INVERTED
| (tristateWithDefaultOffIsActive(rxConfig
->halfDuplex
) ? SERIAL_BIDIR
: 0)
195 telemetrySharedPort
= sumdPort
;
199 return sumdPort
!= NULL
;
201 #endif // USE_SERIALRX_SUMD