2 This project is free software: you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation, either version 3 of the License, or
5 (at your option) any later version.
7 Multiprotocol is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with Multiprotocol. If not, see <http://www.gnu.org/licenses/>.
15 // Last sync with deviation main github branch
17 #if defined(SLT_CCNRF_INO)
19 #include "iface_nrf250k.h"
21 //#define SLT_Q200_FORCE_ID
23 // For code readability
24 #define SLT_PAYLOADSIZE_V1 7
25 #define SLT_PAYLOADSIZE_V1_4 5
26 #define SLT_PAYLOADSIZE_V2 11
27 #define SLT_NFREQCHANNELS 15
28 #define SLT_TXID_SIZE 4
29 #define SLT_BIND_CHANNEL 0x50
32 // flags going to packet[6] (Q200)
33 FLAG_Q200_FMODE = 0x20,
34 FLAG_Q200_VIDON = 0x10,
35 FLAG_Q200_FLIP = 0x08,
36 FLAG_Q200_VIDOFF= 0x04,
40 // flags going to packet[6] (MR100 & Q100)
41 FLAG_MR100_FMODE = 0x20,
42 FLAG_MR100_FLIP = 0x04,
43 FLAG_MR100_VIDEO = 0x02,
44 FLAG_MR100_PICTURE = 0x01,
56 static void __attribute__((unused)) SLT_RF_init()
59 NRF250K_SetTXAddr(rx_tx_addr, SLT_TXID_SIZE);
62 static void __attribute__((unused)) SLT_set_freq(void)
64 // Frequency hopping sequence generation
65 for (uint8_t i = 0; i < SLT_TXID_SIZE; ++i)
67 uint8_t next_i = (i+1) % SLT_TXID_SIZE; // is & 3 better than % 4 ?
68 uint8_t base = i < 2 ? 0x03 : 0x10;
69 hopping_frequency[i*4 + 0] = (rx_tx_addr[i] & 0x3f) + base;
70 hopping_frequency[i*4 + 1] = (rx_tx_addr[i] >> 2) + base;
71 hopping_frequency[i*4 + 2] = (rx_tx_addr[i] >> 4) + (rx_tx_addr[next_i] & 0x03)*0x10 + base;
72 hopping_frequency[i*4 + 3] = (rx_tx_addr[i] >> 6) + (rx_tx_addr[next_i] & 0x0f)*0x04 + base;
76 uint8_t max_freq=0x50; //V1 and V2
77 if(sub_protocol==Q200)
79 for (uint8_t i = 0; i < SLT_NFREQCHANNELS; ++i)
81 if(sub_protocol==Q200 && hopping_frequency[i] >= max_freq)
82 hopping_frequency[i] = hopping_frequency[i] - max_freq + 0x03;
87 for (uint8_t j = 0; j < i; ++j)
88 if (hopping_frequency[i] == hopping_frequency[j])
91 hopping_frequency[i] += 7;
92 if (hopping_frequency[i] >= max_freq)
93 hopping_frequency[i] = hopping_frequency[i] - max_freq + 0x03;
99 for (uint8_t i = 0; i < SLT_NFREQCHANNELS; ++i)
100 debug(" %02X", hopping_frequency[i]);
105 hopping_frequency[SLT_NFREQCHANNELS]=SLT_BIND_CHANNEL;
108 NRF250K_HoppingCalib(SLT_NFREQCHANNELS+1);
111 static void __attribute__((unused)) SLT_wait_radio()
114 while (!NRF250K_IsPacketSent());
118 static void __attribute__((unused)) SLT_send_packet(uint8_t len)
121 NRF250K_WritePayload(packet, len);
125 static void __attribute__((unused)) SLT_build_packet()
127 static uint8_t calib_counter=0;
129 // Set radio channel - once per packet batch
130 NRF250K_SetFreqOffset(); // Set frequency offset
131 NRF250K_Hopping(hopping_frequency_no);
132 if (++hopping_frequency_no >= SLT_NFREQCHANNELS)
133 hopping_frequency_no = 0;
135 // aileron, elevator, throttle, rudder, gear, pitch
136 uint8_t e = 0; // byte where extension 2 bits for every 10-bit channel are packed
137 for (uint8_t i = 0; i < 4; ++i)
139 uint16_t v = convert_channel_10b(sub_protocol != SLT_V1_4 ? CH_AETR[i] : i, false);
140 if(sub_protocol>SLT_V2 && (i==CH2 || i==CH3) && sub_protocol != SLT_V1_4)
141 v=1023-v; // reverse throttle and elevator channels for Q100/Q200/MR100 protocols
143 e = (e >> 2) | (uint8_t) ((v >> 2) & 0xC0);
145 // Extra bits for AETR
148 //->V1_4CH stops here
151 packet[5] = convert_channel_8b(CH5);
152 packet[6] = convert_channel_8b(CH6);
156 if(sub_protocol==Q200)
157 packet[6] = GET_FLAG(CH9_SW , FLAG_Q200_FMODE)
158 |GET_FLAG(CH10_SW, FLAG_Q200_FLIP)
159 |GET_FLAG(CH11_SW, FLAG_Q200_VIDON)
160 |GET_FLAG(CH12_SW, FLAG_Q200_VIDOFF);
161 else if(sub_protocol==MR100 || sub_protocol==Q100)
162 packet[6] = GET_FLAG(CH9_SW , FLAG_MR100_FMODE)
163 |GET_FLAG(CH10_SW, FLAG_MR100_FLIP)
164 |GET_FLAG(CH11_SW, FLAG_MR100_VIDEO) // Does not exist on the Q100 but...
165 |GET_FLAG(CH12_SW, FLAG_MR100_PICTURE); // Does not exist on the Q100 but...
166 packet[7]=convert_channel_8b(CH7);
167 packet[8]=convert_channel_8b(CH8);
168 packet[9]=0xAA; //normal mode for Q100/Q200, unknown for V2/MR100
169 packet[10]=0x00; //normal mode for Q100/Q200, unknown for V2/MR100
170 if((sub_protocol==Q100 || sub_protocol==Q200) && CH13_SW)
172 packet[9]=0x77; //enter calibration
173 if(calib_counter>=20 && calib_counter<=25) // 7 packets for Q100 / 3 packets for Q200
174 packet[10]=0x20; //launch calibration
176 if(calib_counter>250) calib_counter=250;
182 static void __attribute__((unused)) SLT_send_bind_packet()
185 NRF250K_Hopping(SLT_NFREQCHANNELS); //Bind channel
186 BIND_IN_PROGRESS; //Limit TX power to bind level
189 NRF250K_SetTXAddr((uint8_t *)"\x7E\xB8\x63\xA9", SLT_TXID_SIZE);
190 memcpy((void*)packet,(void*)rx_tx_addr,SLT_TXID_SIZE);
192 SLT_send_packet(SLT_TXID_SIZE);
194 SLT_send_packet(SLT_PAYLOADSIZE_V2);
197 #define SLT_TIMING_BUILD 1000
198 #define SLT_V1_TIMING_PACKET 1000
199 #define SLT_V1_4_TIMING_PACKET 1643
200 #define SLT_V2_TIMING_PACKET 2042
201 #define SLT_V1_TIMING_BIND2 1000
202 #define SLT_V2_TIMING_BIND1 6507
203 #define SLT_V2_TIMING_BIND2 2112
204 uint16_t SLT_callback()
209 //debugln_time("b ");
211 telemetry_set_input_sync(packet_period);
214 NRF250K_SetPower(); //Change power level
215 NRF250K_SetTXAddr(rx_tx_addr, SLT_TXID_SIZE);
217 return SLT_TIMING_BUILD;
221 SLT_send_packet(packet_length);
222 if(sub_protocol==SLT_V1)
223 return SLT_V1_TIMING_PACKET;
224 if(sub_protocol==SLT_V1_4)
226 phase++; //Packets are sent two times only
227 return SLT_V1_4_TIMING_PACKET;
230 return SLT_V2_TIMING_PACKET;
232 SLT_send_packet(packet_length);
233 if (++packet_count >= 100)
236 if(sub_protocol==SLT_V1||sub_protocol==SLT_V1_4)
239 return SLT_V1_TIMING_BIND2;
243 return SLT_V2_TIMING_BIND1;
246 {// Continue to send normal packets
248 if(sub_protocol==SLT_V1)
249 return 20000-SLT_TIMING_BUILD;
250 if(sub_protocol==SLT_V1_4)
251 return 18000-SLT_TIMING_BUILD-SLT_V1_4_TIMING_PACKET;
253 return 13730-SLT_TIMING_BUILD;
256 SLT_send_bind_packet();
258 return SLT_V2_TIMING_BIND2;
260 SLT_send_bind_packet();
262 if(sub_protocol==SLT_V1)
263 return 20000-SLT_TIMING_BUILD-SLT_V1_TIMING_BIND2;
264 if(sub_protocol==SLT_V1_4)
265 return 18000-SLT_TIMING_BUILD-SLT_V1_TIMING_BIND2-SLT_V1_4_TIMING_PACKET;
267 return 13730-SLT_TIMING_BUILD-SLT_V2_TIMING_BIND1-SLT_V2_TIMING_BIND2;
274 BIND_DONE; // Not a TX bind protocol
277 hopping_frequency_no = 0;
278 if(sub_protocol==Q200)
279 { //Q200: Force high part of the ID otherwise it won't bind
282 #ifdef SLT_Q200_FORCE_ID // ID taken from TX dumps
283 rx_tx_addr[0]=0x01;rx_tx_addr[1]=0x02;rx_tx_addr[2]=0x6A;rx_tx_addr[3]=0x31;
284 /* rx_tx_addr[0]=0x01;rx_tx_addr[1]=0x02;rx_tx_addr[2]=0x0B;rx_tx_addr[3]=0x57;*/
290 if(sub_protocol==SLT_V1)
292 packet_length = SLT_PAYLOADSIZE_V1;
294 packet_period = 20000+2*SLT_V1_TIMING_PACKET; //22ms
297 else if(sub_protocol==SLT_V1_4)
299 packet_length = SLT_PAYLOADSIZE_V1_4;
301 packet_period = 18000; //18ms
304 MProtocol_id = MProtocol_id_master ^ (1<<RX_num);
305 set_rx_tx_addr(MProtocol_id);
306 debugln("Try ID: %lx", MProtocol_id);
310 packet_length = SLT_PAYLOADSIZE_V2;
312 packet_period = 13730+2*SLT_V2_TIMING_PACKET; //~18ms
319 //268363 + 1643 / 15 = 18000