@@ -21,19 +21,30 @@ Multiprotocol is distributed in the hope that it will be useful,
2121// #define FORCE_XK2_ID
2222// #define FORCE_XK2_P10_ID
2323
24- #define XK2_RF_BIND_CHANNEL 71
24+ #define XK2_RF_BIND_CHANNEL 71
2525#define XK2_P10_RF_BIND_CHANNEL 69
26- #define XK2_PAYLOAD_SIZE 9
27- #define XK2_PACKET_PERIOD 4911
28- #define XK2_RF_NUM_CHANNELS 4
26+ #define XK2_PAYLOAD_SIZE 9
27+ #define XK2_PACKET_PERIOD 4911
28+ #define XK2_RF_NUM_CHANNELS 4
29+ #define XK2_WRITE_TIME 1000
2930
3031enum {
3132 XK2_BIND1,
3233 XK2_BIND2,
3334 XK2_DATA_PREP,
34- XK2_DATA
35+ XK2_DATA,
36+ XK2_RX,
3537};
3638
39+ static uint8_t __attribute__ ((unused)) XK2_checksum(uint8_t init)
40+ {
41+ for (uint8_t i=0 ; i<XK2_PAYLOAD_SIZE-1 ; i++)
42+ init += packet[i];
43+ if (sub_protocol == XK2_P10)
44+ init += 0x10 ;
45+ return init;
46+ }
47+
3748static void __attribute__ ((unused)) XK2_send_packet()
3849{
3950 static uint8_t trim_ch=0 ;
@@ -47,8 +58,6 @@ static void __attribute__((unused)) XK2_send_packet()
4758 // memcpy(&packet[4], rx_id , 3);
4859 // Unknown
4960 packet[7 ] = 0x00 ;
50- // Checksum seed
51- packet[8 ] = 0xC0 ;
5261 }
5362 else
5463 {
@@ -77,19 +86,13 @@ static void __attribute__((unused)) XK2_send_packet()
7786 packet[5 ] |= 0x10 ; // Gyro off (senior mode)
7887 else if (Channel_data[CH6] > CHANNEL_MIN_COMMAND)
7988 packet[5 ] |= 0x08 ; // 3D
80-
81- // Telemetry not received=00, Telemetry received=01 but sometimes switch to 1 even if telemetry is not there...
82- packet[6 ] = 0x00 ;
89+ // Requiest telemetry flag
90+ packet[6 ] = 0x01 ;
8391 // RXID checksum
8492 packet[7 ] = crc8; // Sum RX_ID[0..2]
85- // Checksum seed
86- packet[8 ] = num_ch; // Based on TX ID
8793 }
8894 // Checksum
89- for (uint8_t i=0 ; i<XK2_PAYLOAD_SIZE-1 ; i++)
90- packet[8 ] += packet[i];
91- if (sub_protocol == XK2_P10)
92- packet[8 ] += 0x10 ;
95+ packet[8 ] = XK2_checksum (IS_BIND_IN_PROGRESS ? 0xC0 : num_ch);
9396
9497 // Send
9598 XN297_SetFreqOffset ();
@@ -160,6 +163,8 @@ static void __attribute__((unused)) XK2_initialize_txid()
160163
161164uint16_t XK2_callback ()
162165{
166+ static bool rx = false ;
167+
163168 switch (phase)
164169 {
165170 case XK2_BIND1:
@@ -178,13 +183,8 @@ uint16_t XK2_callback()
178183 debug(" %02X",packet[i]);
179184 debugln("");
180185 #endif
181- crc8 = 0xBF ;
182- for (uint8_t i=0 ; i<XK2_PAYLOAD_SIZE-1 ; i++)
183- crc8 += packet[i];
184- if (sub_protocol == XK2_P10)
185- crc8 += 0x10 ;
186- if (crc8 != packet[8 ])
187- {
186+ if (XK2_checksum (0xBF ) != packet[8 ])
187+ {// Wrong checksum
188188 phase = XK2_BIND1;
189189 return 1000 ;
190190 }
@@ -209,23 +209,67 @@ uint16_t XK2_callback()
209209 XN297_SetTxRxMode (TXRX_OFF);
210210 XN297_SetTxRxMode (TX_EN);
211211 XN297_SetTXAddr (rx_tx_addr, 5 );
212+ #ifdef XK2_HUB_TELEMETRY
213+ XN297_SetRXAddr (rx_tx_addr, XK2_PAYLOAD_SIZE);
214+ #endif
212215 BIND_DONE;
213216 phase++;
214217 case XK2_DATA:
215218 #ifdef MULTI_SYNC
216219 telemetry_set_input_sync (XK2_PACKET_PERIOD);
217220 #endif
221+ #ifdef XK2_HUB_TELEMETRY
222+ rx = XN297_IsRX ();
223+ XN297_SetTxRxMode (TXRX_OFF);
224+ #endif
225+ XK2_send_packet ();
226+ #ifdef XK2_HUB_TELEMETRY
227+ if (rx)
228+ {
229+ XN297_ReadPayload (packet, XK2_PAYLOAD_SIZE);
230+ #if 0
231+ debug("RX");
232+ for(uint8_t i=0; i<XK2_PAYLOAD_SIZE; i++)
233+ debug(" %02X",packet[i]);
234+ debugln("");
235+ #endif
236+ if (XK2_checksum (0xCC ) == packet[8 ] && memcmp (packet, rx_tx_addr, 3 ) == 0 )
237+ {// Good checksum and TXID
238+ // packets: E5 20 F2 00 00 00 00 00 C3 -> E5 20 F2 80 00 00 00 00 43
239+ telemetry_link = 1 ;
240+ v_lipo1 = packet[3 ] ? 137 :162 ; // low voltage 7.1V
241+ }
242+ }
243+ #endif
218244 if (bind_counter)
219245 {
220246 bind_counter--;
221247 if (bind_counter == 0 )
222- {
223248 phase = XK2_DATA_PREP;
224- // phase = XK2_BIND1;
225- }
249+ break ;
226250 }
227- XK2_send_packet ();
251+ # ifndef XK2_HUB_TELEMETRY
228252 break ;
253+ #else
254+ phase++;
255+ return XK2_WRITE_TIME;
256+ default : // XK2_RX
257+ /* { // Wait for packet to be sent before switching to receive mode
258+ uint16_t start=(uint16_t)micros(), count=0;
259+ while ((uint16_t)((uint16_t)micros()-(uint16_t)start) < 500)
260+ {
261+ if(XN297_IsPacketSent())
262+ break;
263+ count++;
264+ }
265+ debugln("%d",count);
266+ }*/
267+ // Switch to RX
268+ XN297_SetTxRxMode (TXRX_OFF);
269+ XN297_SetTxRxMode (RX_EN);
270+ phase = XK2_DATA;
271+ return XK2_PACKET_PERIOD-XK2_WRITE_TIME;
272+ #endif
229273 }
230274 return XK2_PACKET_PERIOD;
231275}
@@ -242,6 +286,9 @@ void XK2_init()
242286 phase = XK2_DATA_PREP;
243287 bind_counter = 0 ;
244288 hopping_frequency_no = 0 ;
289+ #ifdef XK2_HUB_TELEMETRY
290+ RX_RSSI = 100 ; // Dummy value
291+ #endif
245292}
246293
247294#endif
0 commit comments