20
20
#include " iface_nrf24l01.h"
21
21
22
22
#define CFLIE_BIND_COUNT 60
23
+ // #define CFLIE_USE_CRTP_RPYT
23
24
24
25
// =============================================================================
25
26
// CRTP (Crazy RealTime Protocol) Implementation
@@ -220,8 +221,8 @@ static uint8_t packet_ack()
220
221
221
222
static void set_rate_channel (uint8_t rate, uint8_t channel)
222
223
{
223
- NRF24L01_WriteReg (NRF24L01_05_RF_CH, channel); // Defined by model id
224
- NRF24L01_SetBitrate (rate); // Defined by model id
224
+ NRF24L01_WriteReg (NRF24L01_05_RF_CH, channel);
225
+ NRF24L01_SetBitrate (rate);
225
226
}
226
227
227
228
static void send_search_packet ()
@@ -232,23 +233,26 @@ static void send_search_packet()
232
233
NRF24L01_WriteReg (NRF24L01_07_STATUS, (BV (NRF24L01_07_TX_DS) | BV (NRF24L01_07_MAX_RT)));
233
234
NRF24L01_FlushTx ();
234
235
235
- if (rf_ch_num++ > 125 )
236
- {
237
- rf_ch_num = 0 ;
238
- switch (data_rate)
239
- {
240
- case NRF24L01_BR_250K:
241
- data_rate = NRF24L01_BR_1M;
242
- break ;
243
- case NRF24L01_BR_1M:
244
- data_rate = NRF24L01_BR_2M;
245
- break ;
246
- case NRF24L01_BR_2M:
247
- data_rate = NRF24L01_BR_250K;
248
- break ;
249
- }
250
- }
251
- set_rate_channel (data_rate, rf_ch_num);
236
+ if (sub_protocol == CFLIE_AUTO)
237
+ {
238
+ if (rf_ch_num++ > 125 )
239
+ {
240
+ rf_ch_num = 0 ;
241
+ switch (data_rate)
242
+ {
243
+ case NRF24L01_BR_250K:
244
+ data_rate = NRF24L01_BR_1M;
245
+ break ;
246
+ case NRF24L01_BR_1M:
247
+ data_rate = NRF24L01_BR_2M;
248
+ break ;
249
+ case NRF24L01_BR_2M:
250
+ data_rate = NRF24L01_BR_250K;
251
+ break ;
252
+ }
253
+ }
254
+ }
255
+ set_rate_channel (data_rate, rf_ch_num);
252
256
253
257
NRF24L01_WritePayload (buf, sizeof (buf));
254
258
@@ -355,7 +359,7 @@ static void send_crtp_rpyt_packet()
355
359
send_packet ();
356
360
}
357
361
358
- /* static void send_crtp_cppm_emu_packet()
362
+ static void send_crtp_cppm_emu_packet ()
359
363
{
360
364
struct CommanderPacketCppmEmu {
361
365
struct {
@@ -376,7 +380,7 @@ static void send_crtp_rpyt_packet()
376
380
377
381
// Make sure the number of aux channels in use is capped to MAX_CPPM_AUX_CHANNELS
378
382
// uint8_t numAuxChannels = Model.num_channels - 4;
379
- uint8_t numAuxChannels = 2; // TODO: Figure this out correctly
383
+ uint8_t numAuxChannels = 4 ;
380
384
if (numAuxChannels > MAX_CPPM_AUX_CHANNELS)
381
385
{
382
386
numAuxChannels = MAX_CPPM_AUX_CHANNELS;
@@ -385,16 +389,16 @@ static void send_crtp_rpyt_packet()
385
389
cpkt.hdr .numAuxChannels = numAuxChannels;
386
390
387
391
// Remap AETR to AERT (RPYT)
388
- cpkt.channelRoll = convert_channel_16b_limit(AILERON,1000, 2000);
392
+ cpkt.channelRoll = convert_channel_16b_limit (AILERON,2000 , 1000 );
389
393
cpkt.channelPitch = convert_channel_16b_limit (ELEVATOR,1000 ,2000 );
390
394
// Note: T & R Swapped:
391
395
cpkt.channelYaw = convert_channel_16b_limit (RUDDER, 1000 , 2000 );
392
396
cpkt.channelThrust = convert_channel_16b_limit (THROTTLE, 1000 , 2000 );
393
397
394
398
// Rescale the rest of the aux channels - RC channel 4 and up
395
- for (uint8_t i = 4 ; i < 14 ; i++)
399
+ for (uint8_t i = 0 ; i < 10 ; i++)
396
400
{
397
- cpkt.channelAux[i] = convert_channel_16b_limit(i, 1000, 2000 );
401
+ cpkt.channelAux [i] = convert_channel_16b_limit (i+ 4 , 2000 , 1000 );
398
402
}
399
403
400
404
// Total size of the commander packet is a 1-byte header, 4 2-byte channels and
@@ -409,25 +413,19 @@ static void send_crtp_rpyt_packet()
409
413
memcpy (&packet[2 ], (char *)&cpkt, commanderPacketSize); // Why not use sizeof(cpkt) here??
410
414
tx_payload_len = 2 + commanderPacketSize; // CRTP header, commander type, and packet
411
415
send_packet ();
412
- }*/
416
+ }
413
417
414
418
static void send_cmd_packet ()
415
419
{
416
- // TODO: Fix this so we can actually configure the packet type
417
- // switch(Model.proto_opts[PROTOOPTS_CRTP_MODE])
418
- // {
419
- // case CRTP_MODE_CPPM:
420
- // send_crtp_cppm_emu_packet();
421
- // break;
422
- // case CRTP_MODE_RPYT:
423
- // send_crtp_rpyt_packet();
424
- // break;
425
- // default:
426
- // send_crtp_rpyt_packet();
427
- // }
428
-
429
- // send_crtp_cppm_emu_packet(); // oh maAAAn
430
- send_crtp_rpyt_packet ();
420
+ #if defined(CFLIE_USE_CRTP_RPYT)
421
+ {
422
+ send_crtp_rpyt_packet ();
423
+ }
424
+ #else
425
+ {
426
+ send_crtp_cppm_emu_packet ();
427
+ }
428
+ #endif
431
429
}
432
430
433
431
// State machine for setting up CRTP logging
@@ -794,52 +792,36 @@ static uint8_t CFLIE_initialize_rx_tx_addr()
794
792
rx_tx_addr[3 ] =
795
793
rx_tx_addr[4 ] = 0xE7 ; // CFlie uses fixed address
796
794
797
- // if (Model.fixed_id) {
798
- // rf_ch_num = Model.fixed_id % 100;
799
- // switch (Model.fixed_id / 100) {
800
- // case 0:
801
- // data_rate = NRF24L01_BR_250K;
802
- // break;
803
- // case 1:
804
- // data_rate = NRF24L01_BR_1M;
805
- // break;
806
- // case 2:
807
- // data_rate = NRF24L01_BR_2M;
808
- // break;
809
- // default:
810
- // break;
811
- // }
812
-
813
- // if (Model.proto_opts[PROTOOPTS_TELEMETRY] == TELEM_ON_CRTPLOG) {
814
- // return CFLIE_INIT_CRTP_LOG;
815
- // } else {
816
- // return CFLIE_INIT_DATA;
817
- // }
818
- // } else {
819
- // data_rate = NRF24L01_BR_250K;
820
- // rf_ch_num = 10;
821
- // return CFLIE_INIT_SEARCH;
822
- // }
823
-
824
- // Default 1
825
- data_rate = NRF24L01_BR_1M;
826
- rf_ch_num = 10 ;
827
-
828
- // Default 2
829
- // data_rate = NRF24L01_BR_2M;
830
- // rf_ch_num = 110;
795
+ switch (sub_protocol) {
796
+ case CFLIE_2Mbps:
797
+ data_rate = NRF24L01_BR_2M;
798
+ rf_ch_num = option;
799
+ break ;
800
+ case CFLIE_1Mbps:
801
+ data_rate = NRF24L01_BR_1M;
802
+ rf_ch_num = option;
803
+ break ;
804
+ case CFLIE_250kbps:
805
+ data_rate = NRF24L01_BR_250K;
806
+ rf_ch_num = option;
807
+ break ;
808
+ default :
809
+ data_rate = NRF24L01_BR_2M;
810
+ rf_ch_num = 80 ;
811
+ }
812
+
831
813
return CFLIE_INIT_SEARCH;
832
814
}
833
815
834
816
void CFLIE_init (void )
835
817
{
836
818
BIND_IN_PROGRESS; // autobind protocol
837
819
838
- phase = CFLIE_initialize_rx_tx_addr ();
839
- crtp_log_setup_state = CFLIE_CRTP_LOG_SETUP_STATE_INIT;
840
- packet_count=0 ;
820
+ phase = CFLIE_initialize_rx_tx_addr ();
821
+ crtp_log_setup_state = CFLIE_CRTP_LOG_SETUP_STATE_INIT;
822
+ packet_count=0 ;
841
823
842
- CFLIE_RF_init ();
824
+ CFLIE_RF_init ();
843
825
}
844
826
845
- #endif
827
+ #endif
0 commit comments