@@ -22,17 +22,23 @@ Multiprotocol is distributed in the hope that it will be useful,
2222#define V761_PACKET_PERIOD 7060 // Timeout for callback in uSec
2323#define V761_INITIAL_WAIT 500
2424#define V761_PACKET_SIZE 8
25+ #define V761_RXPAYLOAD_SIZE 3
2526#define V761_BIND_COUNT 200
2627#define V761_BIND_FREQ 0x28
2728#define V761_RF_NUM_CHANNELS 3
2829#define TOPRC_BIND_FREQ 0x2A
2930#define TOPRC_PACKET_PERIOD 14120 // Timeout for callback in uSec
3031
32+ #define V761_WRITE_TIME 450
33+ // #define V761_TELEM_DEBUG
34+
3135enum
3236{
33- V761_BIND1 = 0 ,
34- V761_BIND2 ,
35- V761_DATA
37+ V761_BIND1 = 0 ,
38+ V761_BIND2 ,
39+ V761_RX_CHECK ,
40+ V761_DATA ,
41+ V761_RX
3642};
3743
3844static void __attribute__ ((unused)) V761_set_checksum()
@@ -70,8 +76,8 @@ static void __attribute__((unused)) V761_send_packet()
7076 if (hopping_frequency_no >= V761_RF_NUM_CHANNELS )
7177 {
7278 hopping_frequency_no = 0 ;
73- packet_count ++;
74- packet_count &= 0x03 ;
79+ packet_sent ++;
80+ packet_sent &= 0x03 ;
7581 }
7682
7783 packet[0 ] = convert_channel_8b (THROTTLE ); // Throttle
@@ -88,7 +94,7 @@ static void __attribute__((unused)) V761_send_packet()
8894 packet[3 ] = convert_channel_8b (AILERON )>>1 ; // Aileron
8995 }
9096
91- packet[5 ] = packet_count <<6 ; // 0X, 4X, 8X, CX
97+ packet[5 ] = packet_sent <<6 ; // 0X, 4X, 8X, CX
9298 packet[4 ] = 0x20 ; // Trims 00..20..40, 0X->20 4X->TrAil 8X->TrEle CX->TrRud
9399
94100 if (CH5_SW ) // Mode Expert Gyro off
@@ -180,53 +186,133 @@ static void __attribute__((unused)) V761_initialize_txid()
180186
181187uint16_t V761_callback ()
182188{
189+ static bool rx = false ;
190+ static uint8_t packet_telem = 0 ;
191+
183192 switch (phase)
184193 {
185194 case V761_BIND1 :
186195 if (bind_counter)
187196 bind_counter--;
188- packet_count ++;
197+ packet_sent ++;
189198 XN297_RFChannel (sub_protocol == V761_TOPRC ? TOPRC_BIND_FREQ : V761_BIND_FREQ );
190199 XN297_SetTXAddr (rx_id, 4 );
191200 V761_send_packet ();
192- if (packet_count >= 20 )
201+ if (packet_sent >= 20 )
193202 {
194- packet_count = 0 ;
203+ packet_sent = 0 ;
195204 phase = V761_BIND2 ;
196205 }
197206 return 15730 ;
198207 case V761_BIND2 :
199208 if (bind_counter)
200209 bind_counter--;
201- packet_count ++;
210+ packet_sent ++;
202211 XN297_Hopping (0 );
203212 XN297_SetTXAddr (rx_tx_addr, 4 );
204213 V761_send_packet ();
205214 if (bind_counter == 0 )
206215 {
207- packet_count = 0 ;
216+ packet_sent = 0 ;
208217 BIND_DONE ;
209218 phase = V761_DATA ;
219+ #ifdef V761_HUB_TELEMETRY
220+ XN297_SetRXAddr (rx_tx_addr, V761_RXPAYLOAD_SIZE );
221+ #endif
210222 }
211- else if (packet_count >= 20 )
223+ else if (packet_sent >= 20 )
212224 {
213- packet_count = 0 ;
225+ packet_sent = 0 ;
214226 phase = V761_BIND1 ;
215227 }
216228 return 15730 ;
229+ #ifdef V761_HUB_TELEMETRY
230+ case V761_RX_CHECK :
231+ rx = XN297_IsRX (); // Needed for the NRF24L01 since otherwise the bit gets cleared
232+ packet_count++;
233+ if (packet_count > 6 *21 ) // About 1 sec with no telemetry
234+ telemetry_lost = 1 ;
235+ if (packet_telem++ >= 100 )
236+ TX_LQI = packet_telem = 0 ;
237+ if (!telemetry_lost && !rx && (packet_count%22 ) == 0 )
238+ {// Should have received a telem packet but... Send telem to the radio to keep it alive
239+ TX_LQI ++;
240+ RX_RSSI = 100 - TX_LQI ;
241+ telemetry_link = 1 ;
242+ #ifdef V761_TELEM_DEBUG
243+ debugln (" Miss" );
244+ #endif
245+ }
246+ XN297_SetTxRxMode (TXRX_OFF );
247+ phase++;
248+ #endif
217249 case V761_DATA :
218250 #ifdef MULTI_SYNC
219251 telemetry_set_input_sync (packet_period);
220252 #endif
221253 V761_send_packet ();
254+ #ifdef V761_HUB_TELEMETRY
255+ if (hopping_frequency_no == 1 )
256+ { // Start RX on RF channel 0
257+ phase++;
258+ return V761_WRITE_TIME ;
259+ }
260+ if (rx)
261+ { // Check if a packet has been received
262+ #ifdef V761_TELEM_DEBUG
263+ debug (" RX " );
264+ #endif
265+ if (XN297_ReadPayload (packet_in, V761_RXPAYLOAD_SIZE ))
266+ { // packet with good CRC and length
267+ #ifdef V761_TELEM_DEBUG
268+ debug (" OK:" );
269+ for (uint8_t i=0 ;i<V761_RXPAYLOAD_SIZE ;i++)
270+ debug (" %02X" ,packet_in[i]);
271+ #endif
272+ // packet_in[] = AA 00 55 -> battery ok
273+ // packet_in[] = 55 00 AA -> low battery
274+ v_lipo1=packet_in[0 ] >> 1 ;
275+ RX_RSSI = 100 - TX_LQI ;
276+ telemetry_link = 1 ;
277+ telemetry_lost = 0 ;
278+ packet_count = 0 ;
279+ }
280+ #ifdef V761_TELEM_DEBUG
281+ else // Bad packet
282+ debug (" NOK" );
283+ debugln (" " );
284+ #endif
285+ rx = false ;
286+ }
222287 break ;
288+ case V761_RX :
289+ { // Wait for packet to be sent before switching to receive mode
290+ uint16_t start=(uint16_t )micros ();
291+ while ((uint16_t )((uint16_t )micros ()-(uint16_t )start) < 500 )
292+ {
293+ if (XN297_IsPacketSent ())
294+ break ;
295+ }
296+ }
297+ XN297_SetTxRxMode (RX_EN );
298+ phase = V761_RX_CHECK ;
299+ if (!telemetry_lost && packet_count%20 ==0 )
300+ { // Telemetry packets are sent every 21 packets but with a huge jitter from 4ms to 10+ms
301+ hopping_frequency_no++; // don't send next packet to ensure we have enough time to receive the telemetry
302+ return 2 * packet_period - V761_WRITE_TIME ;
303+ }
304+ return packet_period - V761_WRITE_TIME ;
305+ #else
306+ break ;
307+ #endif
223308 }
224309 return packet_period;
225310}
226311
227312void V761_init (void )
228313{
229314 V761_initialize_txid ();
315+ V761_RF_init ();
230316 if (sub_protocol == V761_TOPRC )
231317 {
232318 memcpy (rx_id,(uint8_t *)" \x20\x21\x05\x0A " ,4 );
@@ -246,12 +332,19 @@ void V761_init(void)
246332 else
247333 {
248334 XN297_SetTXAddr (rx_tx_addr, 4 );
335+ #ifdef V761_HUB_TELEMETRY
336+ XN297_SetRXAddr (rx_tx_addr, V761_RXPAYLOAD_SIZE );
337+ #endif
249338 phase = V761_DATA ;
250339 }
251340
252- V761_RF_init ();
253341 hopping_frequency_no = 0 ;
254- packet_count = 0 ;
342+ packet_sent = 0 ;
343+ #ifdef V761_HUB_TELEMETRY
344+ packet_count = 0 ;
345+ telemetry_lost = 1 ;
346+ #endif
347+
255348}
256349
257350#endif
0 commit comments