Create release.yml
[betaflight.git] / src / main / rx / jetiexbus.c
blobc477deed7dd734589fc99637eb5da12d63b9fbaa
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/>.
22 * Authors:
23 * Thomas Miric - marv-t
25 * Jeti EX Bus Communication Protocol:
26 * http://www.jetimodel.com/en/show-file/642/
28 * JETI Telemetry Communication Protocol:
29 * http://www.jetimodel.com/en/show-file/26/
31 * Following restrictions:
32 * Communication speed: 125000 bps
33 * Number of channels: 16
35 * Connect as follows:
36 * Jeti EX Bus -> Serial TX (connect directly)
40 #include <stdbool.h>
41 #include <stdint.h>
43 #include "platform.h"
45 #ifdef USE_SERIALRX_JETIEXBUS
47 #include "build/build_config.h"
48 #include "build/debug.h"
50 #include "common/utils.h"
52 #include "drivers/time.h"
54 #include "io/serial.h"
56 #include "rx/rx.h"
57 #include "rx/jetiexbus.h"
61 // Serial driver for Jeti EX Bus receiver
63 #define JETIEXBUS_BAUDRATE 125000 // EX Bus 125000; EX Bus HS 250000 not supported
64 #define JETIEXBUS_OPTIONS (SERIAL_STOPBITS_1 | SERIAL_PARITY_NO)
65 #define JETIEXBUS_MIN_FRAME_GAP 1000
66 #define JETIEXBUS_CHANNEL_COUNT 16 // most Jeti TX transmit 16 channels
69 #define EXBUS_START_CHANNEL_FRAME (0x3E)
70 #define EXBUS_START_REQUEST_FRAME (0x3D)
71 #define EXBUS_JETIBOX_REQUEST (0x3B)
73 #define EXBUS_CHANNELDATA (0x3E03) // Frame contains Channel Data
74 #define EXBUS_CHANNELDATA_DATA_REQUEST (0x3E01) // Frame contains Channel Data, but with a request for data
75 #define EXBUS_TELEMETRY_REQUEST (0x3D01) // Frame is a request Frame
79 serialPort_t *jetiExBusPort;
81 uint32_t jetiTimeStampRequest = 0;
83 static uint8_t jetiExBusFramePosition;
84 static uint8_t jetiExBusFrameLength;
86 static uint8_t jetiExBusFrameState = EXBUS_STATE_ZERO;
87 uint8_t jetiExBusRequestState = EXBUS_STATE_ZERO;
89 // Use max values for ram areas
90 static uint8_t jetiExBusChannelFrame[EXBUS_MAX_CHANNEL_FRAME_SIZE];
91 uint8_t jetiExBusRequestFrame[EXBUS_MAX_REQUEST_FRAME_SIZE];
93 static uint16_t jetiExBusChannelData[JETIEXBUS_CHANNEL_COUNT];
95 // Jeti Ex Bus CRC calculations for a frame
96 uint16_t jetiExBusCalcCRC16(uint8_t *pt, uint8_t msgLen)
98 uint16_t crc16_data = 0;
99 uint8_t data=0;
101 for (uint8_t mlen = 0; mlen < msgLen; mlen++) {
102 data = pt[mlen] ^ ((uint8_t)(crc16_data) & (uint8_t)(0xFF));
103 data ^= data << 4;
104 crc16_data = ((((uint16_t)data << 8) | ((crc16_data & 0xFF00) >> 8))
105 ^ (uint8_t)(data >> 4)
106 ^ ((uint16_t)data << 3));
108 return(crc16_data);
111 void jetiExBusDecodeChannelFrame(uint8_t *exBusFrame)
113 uint16_t value;
114 uint8_t frameAddr;
116 // Decode header
117 switch (((uint16_t)exBusFrame[EXBUS_HEADER_SYNC] << 8) | ((uint16_t)exBusFrame[EXBUS_HEADER_REQ])) {
119 case EXBUS_CHANNELDATA_DATA_REQUEST: // not yet specified
120 case EXBUS_CHANNELDATA:
121 for (uint8_t i = 0; i < JETIEXBUS_CHANNEL_COUNT; i++) {
122 frameAddr = EXBUS_HEADER_LEN + i * 2;
123 value = ((uint16_t)exBusFrame[frameAddr + 1]) << 8;
124 value += (uint16_t)exBusFrame[frameAddr];
125 // Convert to internal format
126 jetiExBusChannelData[i] = value >> 3;
128 break;
132 void jetiExBusFrameReset(void)
134 jetiExBusFramePosition = 0;
135 jetiExBusFrameLength = EXBUS_MAX_CHANNEL_FRAME_SIZE;
139 supported:
140 0x3E 0x01 LEN Packet_ID 0x31 SUB_LEN Data_array CRC16 // Channel Data with telemetry request (2nd byte 0x01)
141 0x3E 0x03 LEN Packet_ID 0x31 SUB_LEN Data_array CRC16 // Channel Data forbids answering (2nd byte 0x03)
142 0x3D 0x01 0x08 Packet_ID 0x3A 0x00 CRC16 // Telemetry Request EX telemetry (5th byte 0x3A)
144 other messages - not supported:
145 0x3D 0x01 0x09 Packet_ID 0x3B 0x01 0xF0 CRC16 // Jetibox request (5th byte 0x3B)
149 // Receive ISR callback
150 static void jetiExBusDataReceive(uint16_t c, void *data)
152 UNUSED(data);
154 static timeUs_t jetiExBusTimeLast = 0;
155 static uint8_t *jetiExBusFrame;
156 const timeUs_t now = microsISR();
158 // Check if we shall reset frame position due to time
159 if (cmpTimeUs(now, jetiExBusTimeLast) > JETIEXBUS_MIN_FRAME_GAP) {
160 jetiExBusFrameReset();
161 jetiExBusFrameState = EXBUS_STATE_ZERO;
162 jetiExBusRequestState = EXBUS_STATE_ZERO;
164 jetiExBusTimeLast = now;
166 // Check if we shall start a frame?
167 if (jetiExBusFramePosition == 0) {
168 switch (c) {
169 case EXBUS_START_CHANNEL_FRAME:
170 jetiExBusFrameState = EXBUS_STATE_IN_PROGRESS;
171 jetiExBusFrame = jetiExBusChannelFrame;
172 break;
174 case EXBUS_START_REQUEST_FRAME:
175 jetiExBusRequestState = EXBUS_STATE_IN_PROGRESS;
176 jetiExBusFrame = jetiExBusRequestFrame;
177 break;
179 default:
180 return;
184 // Store in frame copy
185 jetiExBusFrame[jetiExBusFramePosition] = (uint8_t)c;
186 jetiExBusFramePosition++;
188 // Check the header for the message length
189 if (jetiExBusFramePosition == EXBUS_HEADER_LEN) {
191 if ((jetiExBusFrameState == EXBUS_STATE_IN_PROGRESS) && (jetiExBusFrame[EXBUS_HEADER_MSG_LEN] <= EXBUS_MAX_CHANNEL_FRAME_SIZE)) {
192 jetiExBusFrameLength = jetiExBusFrame[EXBUS_HEADER_MSG_LEN];
193 return;
196 if ((jetiExBusRequestState == EXBUS_STATE_IN_PROGRESS) && (jetiExBusFrame[EXBUS_HEADER_MSG_LEN] <= EXBUS_MAX_REQUEST_FRAME_SIZE)) {
197 jetiExBusFrameLength = jetiExBusFrame[EXBUS_HEADER_MSG_LEN];
198 return;
201 jetiExBusFrameReset(); // not a valid frame
202 jetiExBusFrameState = EXBUS_STATE_ZERO;
203 jetiExBusRequestState = EXBUS_STATE_ZERO;
204 return;
207 // Done?
208 if (jetiExBusFrameLength == jetiExBusFramePosition) {
209 if (jetiExBusFrameState == EXBUS_STATE_IN_PROGRESS)
210 jetiExBusFrameState = EXBUS_STATE_RECEIVED;
211 if (jetiExBusRequestState == EXBUS_STATE_IN_PROGRESS) {
212 jetiExBusRequestState = EXBUS_STATE_RECEIVED;
213 jetiTimeStampRequest = now;
216 jetiExBusFrameReset();
220 // Check if it is time to read a frame from the data...
221 static uint8_t jetiExBusFrameStatus(rxRuntimeState_t *rxRuntimeState)
223 UNUSED(rxRuntimeState);
225 uint8_t frameStatus = RX_FRAME_PENDING;
227 if (jetiExBusFrameState == EXBUS_STATE_RECEIVED) {
228 if (jetiExBusCalcCRC16(jetiExBusChannelFrame, jetiExBusChannelFrame[EXBUS_HEADER_MSG_LEN]) == 0) {
229 jetiExBusDecodeChannelFrame(jetiExBusChannelFrame);
230 frameStatus = RX_FRAME_COMPLETE;
231 rxRuntimeState->lastRcFrameTimeUs = jetiTimeStampRequest;
233 jetiExBusFrameState = EXBUS_STATE_ZERO;
236 return frameStatus;
239 static float jetiExBusReadRawRC(const rxRuntimeState_t *rxRuntimeState, uint8_t chan)
241 if (chan >= rxRuntimeState->channelCount)
242 return 0;
244 return (jetiExBusChannelData[chan]);
247 bool jetiExBusInit(const rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState)
249 UNUSED(rxConfig);
251 rxRuntimeState->channelCount = JETIEXBUS_CHANNEL_COUNT;
252 rxRuntimeState->rxRefreshRate = 5500;
254 rxRuntimeState->rcReadRawFn = jetiExBusReadRawRC;
255 rxRuntimeState->rcFrameStatusFn = jetiExBusFrameStatus;
256 rxRuntimeState->rcFrameTimeUsFn = rxFrameTimeUs;
258 jetiExBusFrameReset();
260 const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_RX_SERIAL);
262 if (!portConfig) {
263 return false;
266 jetiExBusPort = openSerialPort(portConfig->identifier,
267 FUNCTION_RX_SERIAL,
268 jetiExBusDataReceive,
269 NULL,
270 JETIEXBUS_BAUDRATE,
271 MODE_RXTX,
272 JETIEXBUS_OPTIONS | (rxConfig->serialrx_inverted ? SERIAL_INVERTED : 0) | SERIAL_BIDIR
274 return jetiExBusPort != NULL;
276 #endif // SERIAL_RX