Removed excess trailing spaces before new lines on licenses.
[betaflight.git] / src / main / rx / jetiexbus.c
blob2ce081f0fdc880bbd0569f80d9d522cb3061ff49
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 RX (connect directly)
37 * Serial TX -> Resistor(2k4) ->Serial RX
38 * In jeti pdf it is different, but if the resistor breaks, the receiver continues to operate.
42 #include <stdbool.h>
43 #include <stdint.h>
45 #include "platform.h"
47 #ifdef USE_SERIAL_RX
49 #include "build/build_config.h"
50 #include "build/debug.h"
52 #include "common/utils.h"
54 #include "drivers/time.h"
56 #include "io/serial.h"
58 #include "rx/rx.h"
59 #include "rx/jetiexbus.h"
63 // Serial driver for Jeti EX Bus receiver
65 #define JETIEXBUS_BAUDRATE 125000 // EX Bus 125000; EX Bus HS 250000 not supported
66 #define JETIEXBUS_OPTIONS (SERIAL_STOPBITS_1 | SERIAL_PARITY_NO)
67 #define JETIEXBUS_MIN_FRAME_GAP 1000
68 #define JETIEXBUS_CHANNEL_COUNT 16 // most Jeti TX transmit 16 channels
71 #define EXBUS_START_CHANNEL_FRAME (0x3E)
72 #define EXBUS_START_REQUEST_FRAME (0x3D)
73 #define EXBUS_JETIBOX_REQUEST (0x3B)
75 #define EXBUS_CHANNELDATA (0x3E03) // Frame contains Channel Data
76 #define EXBUS_CHANNELDATA_DATA_REQUEST (0x3E01) // Frame contains Channel Data, but with a request for data
77 #define EXBUS_TELEMETRY_REQUEST (0x3D01) // Frame is a request Frame
81 serialPort_t *jetiExBusPort;
83 uint32_t jetiTimeStampRequest = 0;
85 static uint8_t jetiExBusFramePosition;
86 static uint8_t jetiExBusFrameLength;
88 static uint8_t jetiExBusFrameState = EXBUS_STATE_ZERO;
89 uint8_t jetiExBusRequestState = EXBUS_STATE_ZERO;
91 // Use max values for ram areas
92 static uint8_t jetiExBusChannelFrame[EXBUS_MAX_CHANNEL_FRAME_SIZE];
93 uint8_t jetiExBusRequestFrame[EXBUS_MAX_REQUEST_FRAME_SIZE];
95 static uint16_t jetiExBusChannelData[JETIEXBUS_CHANNEL_COUNT];
98 // Jeti Ex Bus CRC calculations for a frame
99 uint16_t jetiExBusCalcCRC16(uint8_t *pt, uint8_t msgLen)
101 uint16_t crc16_data = 0;
102 uint8_t data=0;
104 for (uint8_t mlen = 0; mlen < msgLen; mlen++) {
105 data = pt[mlen] ^ ((uint8_t)(crc16_data) & (uint8_t)(0xFF));
106 data ^= data << 4;
107 crc16_data = ((((uint16_t)data << 8) | ((crc16_data & 0xFF00) >> 8))
108 ^ (uint8_t)(data >> 4)
109 ^ ((uint16_t)data << 3));
111 return(crc16_data);
114 void jetiExBusDecodeChannelFrame(uint8_t *exBusFrame)
116 uint16_t value;
117 uint8_t frameAddr;
119 // Decode header
120 switch (((uint16_t)exBusFrame[EXBUS_HEADER_SYNC] << 8) | ((uint16_t)exBusFrame[EXBUS_HEADER_REQ])) {
122 case EXBUS_CHANNELDATA_DATA_REQUEST: // not yet specified
123 case EXBUS_CHANNELDATA:
124 for (uint8_t i = 0; i < JETIEXBUS_CHANNEL_COUNT; i++) {
125 frameAddr = EXBUS_HEADER_LEN + i * 2;
126 value = ((uint16_t)exBusFrame[frameAddr + 1]) << 8;
127 value += (uint16_t)exBusFrame[frameAddr];
128 // Convert to internal format
129 jetiExBusChannelData[i] = value >> 3;
131 break;
135 void jetiExBusFrameReset(void)
137 jetiExBusFramePosition = 0;
138 jetiExBusFrameLength = EXBUS_MAX_CHANNEL_FRAME_SIZE;
142 supported:
143 0x3E 0x01 LEN Packet_ID 0x31 SUB_LEN Data_array CRC16 // Channel Data with telemetry request (2nd byte 0x01)
144 0x3E 0x03 LEN Packet_ID 0x31 SUB_LEN Data_array CRC16 // Channel Data forbids answering (2nd byte 0x03)
145 0x3D 0x01 0x08 Packet_ID 0x3A 0x00 CRC16 // Telemetry Request EX telemetry (5th byte 0x3A)
147 other messages - not supported:
148 0x3D 0x01 0x09 Packet_ID 0x3B 0x01 0xF0 CRC16 // Jetibox request (5th byte 0x3B)
152 // Receive ISR callback
153 static void jetiExBusDataReceive(uint16_t c, void *data)
155 UNUSED(data);
157 uint32_t now;
158 static uint32_t jetiExBusTimeLast = 0;
159 static int32_t jetiExBusTimeInterval;
161 static uint8_t *jetiExBusFrame;
163 // Check if we shall reset frame position due to time
164 now = micros();
166 jetiExBusTimeInterval = now - jetiExBusTimeLast;
167 jetiExBusTimeLast = now;
169 if (jetiExBusTimeInterval > JETIEXBUS_MIN_FRAME_GAP) {
170 jetiExBusFrameReset();
171 jetiExBusFrameState = EXBUS_STATE_ZERO;
172 jetiExBusRequestState = EXBUS_STATE_ZERO;
175 // Check if we shall start a frame?
176 if (jetiExBusFramePosition == 0) {
177 switch (c) {
178 case EXBUS_START_CHANNEL_FRAME:
179 jetiExBusFrameState = EXBUS_STATE_IN_PROGRESS;
180 jetiExBusFrame = jetiExBusChannelFrame;
181 break;
183 case EXBUS_START_REQUEST_FRAME:
184 jetiExBusRequestState = EXBUS_STATE_IN_PROGRESS;
185 jetiExBusFrame = jetiExBusRequestFrame;
186 break;
188 default:
189 return;
193 // Store in frame copy
194 jetiExBusFrame[jetiExBusFramePosition] = (uint8_t)c;
195 jetiExBusFramePosition++;
197 // Check the header for the message length
198 if (jetiExBusFramePosition == EXBUS_HEADER_LEN) {
200 if ((jetiExBusFrameState == EXBUS_STATE_IN_PROGRESS) && (jetiExBusFrame[EXBUS_HEADER_MSG_LEN] <= EXBUS_MAX_CHANNEL_FRAME_SIZE)) {
201 jetiExBusFrameLength = jetiExBusFrame[EXBUS_HEADER_MSG_LEN];
202 return;
205 if ((jetiExBusRequestState == EXBUS_STATE_IN_PROGRESS) && (jetiExBusFrame[EXBUS_HEADER_MSG_LEN] <= EXBUS_MAX_REQUEST_FRAME_SIZE)) {
206 jetiExBusFrameLength = jetiExBusFrame[EXBUS_HEADER_MSG_LEN];
207 return;
210 jetiExBusFrameReset(); // not a valid frame
211 jetiExBusFrameState = EXBUS_STATE_ZERO;
212 jetiExBusRequestState = EXBUS_STATE_ZERO;
213 return;
216 // Done?
217 if (jetiExBusFrameLength == jetiExBusFramePosition) {
218 if (jetiExBusFrameState == EXBUS_STATE_IN_PROGRESS)
219 jetiExBusFrameState = EXBUS_STATE_RECEIVED;
220 if (jetiExBusRequestState == EXBUS_STATE_IN_PROGRESS) {
221 jetiExBusRequestState = EXBUS_STATE_RECEIVED;
222 jetiTimeStampRequest = micros();
225 jetiExBusFrameReset();
229 // Check if it is time to read a frame from the data...
230 static uint8_t jetiExBusFrameStatus(rxRuntimeConfig_t *rxRuntimeConfig)
232 UNUSED(rxRuntimeConfig);
234 if (jetiExBusFrameState != EXBUS_STATE_RECEIVED)
235 return RX_FRAME_PENDING;
237 if (jetiExBusCalcCRC16(jetiExBusChannelFrame, jetiExBusChannelFrame[EXBUS_HEADER_MSG_LEN]) == 0) {
238 jetiExBusDecodeChannelFrame(jetiExBusChannelFrame);
239 jetiExBusFrameState = EXBUS_STATE_ZERO;
240 return RX_FRAME_COMPLETE;
241 } else {
242 jetiExBusFrameState = EXBUS_STATE_ZERO;
243 return RX_FRAME_PENDING;
247 static uint16_t jetiExBusReadRawRC(const rxRuntimeConfig_t *rxRuntimeConfig, uint8_t chan)
249 if (chan >= rxRuntimeConfig->channelCount)
250 return 0;
252 return (jetiExBusChannelData[chan]);
255 bool jetiExBusInit(const rxConfig_t *rxConfig, rxRuntimeConfig_t *rxRuntimeConfig)
257 UNUSED(rxConfig);
259 rxRuntimeConfig->channelCount = JETIEXBUS_CHANNEL_COUNT;
260 rxRuntimeConfig->rxRefreshRate = 5500;
262 rxRuntimeConfig->rcReadRawFn = jetiExBusReadRawRC;
263 rxRuntimeConfig->rcFrameStatusFn = jetiExBusFrameStatus;
265 jetiExBusFrameReset();
267 const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_RX_SERIAL);
269 if (!portConfig) {
270 return false;
273 jetiExBusPort = openSerialPort(portConfig->identifier,
274 FUNCTION_RX_SERIAL,
275 jetiExBusDataReceive,
276 NULL,
277 JETIEXBUS_BAUDRATE,
278 MODE_RXTX,
279 JETIEXBUS_OPTIONS | (rxConfig->serialrx_inverted ? SERIAL_INVERTED : 0) | (rxConfig->halfDuplex ? SERIAL_BIDIR : 0)
281 serialSetMode(jetiExBusPort, MODE_RX);
282 return jetiExBusPort != NULL;
284 #endif // SERIAL_RX