rename USE_24CHANELS to USE_34CHANNELS
[inav.git] / src / main / rx / jetiexbus.c
blob9e3c04d2c12085dc90ad12d59b2ba1e193e7a7cd
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
67 #ifdef USE_34CHANNELS
68 #define JETIEXBUS_CHANNEL_COUNT 24
69 #else
70 #define JETIEXBUS_CHANNEL_COUNT 16
71 #endif
74 #define EXBUS_START_CHANNEL_FRAME (0x3E)
75 #define EXBUS_START_REQUEST_FRAME (0x3D)
76 #define EXBUS_JETIBOX_REQUEST (0x3B)
78 #define EXBUS_CHANNELDATA (0x3E03) // Frame contains Channel Data
79 #define EXBUS_CHANNELDATA_DATA_REQUEST (0x3E01) // Frame contains Channel Data, but with a request for data
80 #define EXBUS_TELEMETRY_REQUEST (0x3D01) // Frame is a request Frame
84 serialPort_t *jetiExBusPort;
86 uint32_t jetiTimeStampRequest = 0;
88 static uint8_t jetiExBusFramePosition;
89 static uint8_t jetiExBusFrameLength;
91 static uint8_t jetiExBusFrameState = EXBUS_STATE_ZERO;
92 uint8_t jetiExBusRequestState = EXBUS_STATE_ZERO;
94 // Use max values for ram areas
95 static uint8_t jetiExBusChannelFrame[EXBUS_MAX_CHANNEL_FRAME_SIZE];
96 uint8_t jetiExBusRequestFrame[EXBUS_MAX_REQUEST_FRAME_SIZE];
98 static uint16_t jetiExBusChannelData[JETIEXBUS_CHANNEL_COUNT];
100 // Jeti Ex Bus CRC calculations for a frame
101 uint16_t jetiExBusCalcCRC16(uint8_t *pt, uint8_t msgLen)
103 uint16_t crc16_data = 0;
104 uint8_t data=0;
106 for (uint8_t mlen = 0; mlen < msgLen; mlen++) {
107 data = pt[mlen] ^ ((uint8_t)(crc16_data) & (uint8_t)(0xFF));
108 data ^= data << 4;
109 crc16_data = ((((uint16_t)data << 8) | ((crc16_data & 0xFF00) >> 8))
110 ^ (uint8_t)(data >> 4)
111 ^ ((uint16_t)data << 3));
113 return(crc16_data);
116 void jetiExBusDecodeChannelFrame(uint8_t *exBusFrame)
118 uint16_t value;
119 uint8_t frameAddr;
121 // Decode header
122 switch (((uint16_t)exBusFrame[EXBUS_HEADER_SYNC] << 8) | ((uint16_t)exBusFrame[EXBUS_HEADER_REQ])) {
124 case EXBUS_CHANNELDATA_DATA_REQUEST: // not yet specified
125 case EXBUS_CHANNELDATA:
126 for (uint8_t i = 0; i < JETIEXBUS_CHANNEL_COUNT; i++) {
127 frameAddr = EXBUS_HEADER_LEN + i * 2;
128 value = ((uint16_t)exBusFrame[frameAddr + 1]) << 8;
129 value += (uint16_t)exBusFrame[frameAddr];
130 // Convert to internal format
131 jetiExBusChannelData[i] = value >> 3;
133 break;
137 void jetiExBusFrameReset(void)
139 jetiExBusFramePosition = 0;
140 jetiExBusFrameLength = EXBUS_MAX_CHANNEL_FRAME_SIZE;
144 supported:
145 0x3E 0x01 LEN Packet_ID 0x31 SUB_LEN Data_array CRC16 // Channel Data with telemetry request (2nd byte 0x01)
146 0x3E 0x03 LEN Packet_ID 0x31 SUB_LEN Data_array CRC16 // Channel Data forbids answering (2nd byte 0x03)
147 0x3D 0x01 0x08 Packet_ID 0x3A 0x00 CRC16 // Telemetry Request EX telemetry (5th byte 0x3A)
149 other messages - not supported:
150 0x3D 0x01 0x09 Packet_ID 0x3B 0x01 0xF0 CRC16 // Jetibox request (5th byte 0x3B)
154 // Receive ISR callback
155 static void jetiExBusDataReceive(uint16_t c, void *data)
157 UNUSED(data);
159 static timeUs_t jetiExBusTimeLast = 0;
160 static uint8_t *jetiExBusFrame;
161 static uint8_t jetiExBusFrameMaxSize;
162 const timeUs_t now = microsISR();
164 // Check if we shall reset frame position due to time
165 if (cmpTimeUs(now, jetiExBusTimeLast) > JETIEXBUS_MIN_FRAME_GAP) {
166 jetiExBusFrameReset();
167 jetiExBusFrameState = EXBUS_STATE_ZERO;
168 jetiExBusRequestState = EXBUS_STATE_ZERO;
170 jetiExBusTimeLast = now;
172 // Check if we shall start a frame?
173 if (jetiExBusFramePosition == 0) {
174 switch (c) {
175 case EXBUS_START_CHANNEL_FRAME:
176 jetiExBusFrameState = EXBUS_STATE_IN_PROGRESS;
177 jetiExBusFrame = jetiExBusChannelFrame;
178 jetiExBusFrameMaxSize = EXBUS_MAX_CHANNEL_FRAME_SIZE;
179 break;
181 case EXBUS_START_REQUEST_FRAME:
182 jetiExBusRequestState = EXBUS_STATE_IN_PROGRESS;
183 jetiExBusFrame = jetiExBusRequestFrame;
184 jetiExBusFrameMaxSize = EXBUS_MAX_CHANNEL_FRAME_SIZE;
185 break;
187 default:
188 return;
192 if (jetiExBusFramePosition == jetiExBusFrameMaxSize) {
193 // frame overrun
194 jetiExBusFrameReset();
195 jetiExBusFrameState = EXBUS_STATE_ZERO;
196 jetiExBusRequestState = EXBUS_STATE_ZERO;
198 return;
201 // Store in frame copy
202 jetiExBusFrame[jetiExBusFramePosition] = (uint8_t)c;
203 jetiExBusFramePosition++;
205 // Check the header for the message length
206 if (jetiExBusFramePosition == EXBUS_HEADER_LEN) {
208 if ((jetiExBusFrameState == EXBUS_STATE_IN_PROGRESS) && (jetiExBusFrame[EXBUS_HEADER_MSG_LEN] <= EXBUS_MAX_CHANNEL_FRAME_SIZE)) {
209 jetiExBusFrameLength = jetiExBusFrame[EXBUS_HEADER_MSG_LEN];
210 return;
213 if ((jetiExBusRequestState == EXBUS_STATE_IN_PROGRESS) && (jetiExBusFrame[EXBUS_HEADER_MSG_LEN] <= EXBUS_MAX_REQUEST_FRAME_SIZE)) {
214 jetiExBusFrameLength = jetiExBusFrame[EXBUS_HEADER_MSG_LEN];
215 return;
218 jetiExBusFrameReset(); // not a valid frame
219 jetiExBusFrameState = EXBUS_STATE_ZERO;
220 jetiExBusRequestState = EXBUS_STATE_ZERO;
221 return;
224 // Done?
225 if (jetiExBusFrameLength == jetiExBusFramePosition) {
226 if (jetiExBusFrameState == EXBUS_STATE_IN_PROGRESS)
227 jetiExBusFrameState = EXBUS_STATE_RECEIVED;
228 if (jetiExBusRequestState == EXBUS_STATE_IN_PROGRESS) {
229 jetiExBusRequestState = EXBUS_STATE_RECEIVED;
230 jetiTimeStampRequest = now;
233 jetiExBusFrameReset();
237 // Check if it is time to read a frame from the data...
238 static uint8_t jetiExBusFrameStatus(rxRuntimeConfig_t *rxRuntimeConfig)
240 UNUSED(rxRuntimeConfig);
242 uint8_t frameStatus = RX_FRAME_PENDING;
244 if (jetiExBusFrameState == EXBUS_STATE_RECEIVED) {
245 if (jetiExBusCalcCRC16(jetiExBusChannelFrame, jetiExBusChannelFrame[EXBUS_HEADER_MSG_LEN]) == 0) {
246 jetiExBusDecodeChannelFrame(jetiExBusChannelFrame);
247 frameStatus = RX_FRAME_COMPLETE;
249 jetiExBusFrameState = EXBUS_STATE_ZERO;
252 return frameStatus;
255 static uint16_t jetiExBusReadRawRC(const rxRuntimeConfig_t *rxRuntimeConfig, uint8_t chan)
257 if (chan >= rxRuntimeConfig->channelCount)
258 return 0;
260 return (jetiExBusChannelData[chan]);
263 bool jetiExBusInit(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig)
265 UNUSED(rxConfig);
267 rxRuntimeConfig->channelCount = JETIEXBUS_CHANNEL_COUNT;
268 rxRuntimeConfig->rcReadRawFn = jetiExBusReadRawRC;
269 rxRuntimeConfig->rcFrameStatusFn = jetiExBusFrameStatus;
271 jetiExBusFrameReset();
273 const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_RX_SERIAL);
275 if (!portConfig) {
276 return false;
279 jetiExBusPort = openSerialPort(portConfig->identifier,
280 FUNCTION_RX_SERIAL,
281 jetiExBusDataReceive,
282 NULL,
283 JETIEXBUS_BAUDRATE,
284 MODE_RXTX,
285 JETIEXBUS_OPTIONS | (rxConfig->serialrx_inverted ? SERIAL_INVERTED : 0) | SERIAL_BIDIR
287 return jetiExBusPort != NULL;
289 #endif // SERIAL_RX