Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 22.44 KB | None | 0 0
  1. //#include <TimerOne.h>
  2.  
  3. /*
  4.  This project is free software: you can redistribute it and/or modify
  5.  it under the terms of the GNU General Public License as published by
  6.  the Free Software Foundation, either version 3 of the License, or
  7.  (at your option) any later version.
  8.  
  9.  Deviation is distributed in the hope that it will be useful,
  10.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  GNU General Public License for more details.
  13.  
  14.  You should have received a copy of the GNU General Public License
  15.  along with Deviation.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #include "TimerOne.h"
  19. #include "iface_nrf24l01.h"
  20.  
  21. #define ER9X
  22.  
  23. #define DEBUG
  24. //PINOUT
  25. #define PPM_pin 3//PPM -D3
  26. #define SDI_pin 5 //SDIO-D5 /MOSI
  27. #define SCLK_pin 4 //SCK-D4
  28. #define NRF_CSN_pin 2 //D2/CS
  29. #define SDO_pin 6//D6 MISO
  30. #define CE_pin 7//D7//CE pin  
  31.  
  32. //
  33. #define  NRF_CSN_on PORTD |= 0x04 //D2
  34. #define  NRF_CSN_off PORTD &= 0xFB //D2
  35. //
  36. #define  SCK_on PORTD |= 0x10//D4
  37. #define  SCK_off PORTD &= 0xEF//D4
  38. #define  SDI_on PORTD |= 0x20 //D5
  39. #define  SDI_off PORTD &= 0xDF //D5
  40. //
  41. #define  CE_off PORTD &=0x7F//D7
  42. #define  CE_on PORTD |=0x80;//D7
  43. //
  44. //
  45. #define  SDI_1 (PIND & 0x20) == 0x20 //D5
  46. #define  SDI_0 (PIND & 0x20) == 0x00 //D5
  47. #define  SDO_1 (PIND & 0x40) == 0x40//D6
  48. #define  SDO_0 (PIND & 0x40) == 0x00//
  49. //
  50. #define RED_LED_pin A3//C3
  51. #define LED_ON  PORTC |= _BV(3);
  52. #define LED_OFF  PORTC &= ~_BV(3);
  53. //
  54. #define NOP() __asm__ __volatile__("nop")
  55. #define PPM_MAX 2000
  56. #define PPM_MIN 1000
  57. #define PPM_THROW 1000//PPM max-PPMmin
  58.  
  59. /*
  60. http://www.deviationtx.com/
  61.       TX address        Channel Sequence
  62. S1    3B B6 00 00 A2    15 35 1D 3D
  63.  
  64. */
  65.  
  66. #define SYMAX_BIND_COUNT 345   // 1.5 seconds
  67. #define FIRST_PACKET_DELAY  12000
  68. #define PACKET_PERIOD        4000     // Timeout for callback in uSec
  69. #define SYMAX_INITIAL_WAIT   500
  70.  
  71. #define SYMAX_FLAG_FLIP      0x01
  72. //#define SYMAX_FLAG_RATES     0x02
  73. #define SYMAX_FLAG_VIDEO     0x02
  74. #define SYMAX_FLAG_PICTURE   0x04
  75. #define SYMAX_PAYLOADSIZE 10       // receive data pipes set to this size, but unused
  76. #define MAX_PACKET_SIZE 16   // X11,X12,X5C-1 10-byte, X5C 16-byte
  77.  
  78.  
  79. static uint8_t packet[MAX_PACKET_SIZE];
  80. static uint8_t packet_size;
  81. static uint16_t counter;
  82. static uint32_t packet_counter;
  83. //static uint8_t tx_power;
  84. static uint8_t throttle, rudder, elevator, aileron, flags;
  85. static uint8_t rx_tx_addr[5];
  86. static uint8_t multimedia = 0;
  87. const uint8_t VideoBit = 7;
  88. const uint8_t PictureBit = 7;
  89. volatile uint16_t Servo_data[8];
  90. uint8_t mode_select = 0;
  91. byte scale;
  92.  
  93.  
  94. enum {
  95.   MOODE_SYMAX11,
  96.   MODE_SYMAX5C
  97. };
  98.  
  99. #if defined(ER9X)
  100. #define PPM_MAX 255//2000//TURNIGY/ER9x
  101. #define PPM_MIN 0 //1000
  102. #define PPM_THROW 1000//PPM max-PPMmin
  103.  
  104. enum chan_order {
  105.   AILERON = 0,
  106.   ELEVATOR,
  107.   THROTTLE,
  108.   RUDDER,
  109.   AUX1,
  110.   AUX2,
  111.   AUX3,
  112.   AUX4
  113. };
  114. #endif
  115.  
  116.  
  117. // frequency channel management
  118. #define MAX_RF_CHANNELS    17
  119. static uint8_t current_chan;
  120. static uint8_t chans[MAX_RF_CHANNELS];
  121. static uint8_t num_rf_channels;
  122. static uint8_t phase;
  123. bool partial_read = false;
  124. int index = 0;
  125. enum {
  126.   SYMAX_INIT1 = 0,
  127.   SYMAX_BIND2,
  128.   SYMAX_BIND3,
  129.   SYMAX_DATA
  130. };
  131. uint8_t data_rx_tx_addr[] = {0x3b,0xb6,0x00,0x00,0xa2};//<<--- is ok
  132. //uint8_t data_rx_tx_addr[] = {0x3b,0xb6,0x00,0x00,0xa2};//<<--- is ok
  133. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x00,0x00,0xa2};
  134. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x03,0x00,0xa2};//<<----  is ok
  135. //uint8_t data_rx_tx_addr[] = {0x46,0x18,0x00,0x00,0xa2};
  136. //-------------
  137. //uint8_t data_rx_tx_addr[] = {0x3b,0xb6,0x00,0x00,0xa2};//<<--- is ok COM9 //#6
  138. //uint8_t data_rx_tx_addr[] = {0x3b,0xb6,0x03,0x00,0xa2};//<<--- is ok COM11//#3
  139. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x02,0x00,0xa2};//<<--- is ok 90% COM12-com5 //#1
  140. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x00,0x00,0xa2};//<<--- is ok 100% COM10
  141. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x05,0x00,0xa2};//<<--- is ok 90%    //#4
  142. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x04,0x00,0xa2};//<<--- is ok 90%
  143. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x07,0x00,0xa2};//<<--- is ok 90%//
  144.  
  145. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x03,0x00,0xa2};//<<----  is ok
  146. //uint8_t data_rx_tx_addr[] = {0x46,0x18,0x01,0x00,0xa2};//<<--- is ok family 2 90%  COM3 //#5
  147. //uint8_t data_rx_tx_addr[] = {0x46,0x18,0x03,0x00,0xa2};//<<--- is ok family 2 90%  COM6
  148. //uint8_t data_rx_tx_addr[] = {0x46,0x18,0x04,0x00,0xa2};//<<--- is ok family 2 90%  COM7 //#2
  149. //
  150. //uint8_t data_rx_tx_addr[] = {0x9A,0xe9,0x07,0x00,0xa2};//<<--- is ok 90%
  151.  
  152. // Bit vector from bit position
  153. #define BV(bit) (1 << bit)
  154. unsigned long wait_until = 0;
  155. unsigned long lastWait = 0;
  156. void  setup()
  157. {
  158.   mode_select = MODE_SYMAX5C; //MODE_SYMAX5C;
  159.   pinMode(RED_LED_pin, OUTPUT);
  160.   //RF module pins
  161.   pinMode(PPM_pin, INPUT);//PPM input
  162.   pinMode(SDI_pin, OUTPUT);//MOSI
  163.   pinMode(SDO_pin, INPUT); //MISO
  164.   pinMode(SCLK_pin, OUTPUT);//SCLK
  165.   //pinMode(CS_pin, OUTPUT);//CS
  166.   pinMode(CE_pin, OUTPUT); //CE
  167.   pinMode(NRF_CSN_pin, OUTPUT);
  168.   pinMode (11, OUTPUT);
  169.   digitalWrite(11, LOW);
  170.   pinMode (12, OUTPUT); //Yes
  171.   pinMode(13, OUTPUT); //No
  172.   digitalWrite(13, HIGH);
  173.  
  174.  
  175.   //CS_on;//start CS high
  176.   NRF_CSN_on;
  177.   SDI_on;//start SDIO high
  178.   SCK_off;//start sck low
  179.   CE_on;
  180.   //
  181.   for (int i = 0; i < 8; i++) {
  182.     Servo_data[i] = 0 ; //1500;
  183.   }
  184.   Servo_data[THROTTLE] = 255 ; // -127 in sign and magnitude format
  185.   //
  186. #if F_CPU == 16000000
  187.   scale = 2;
  188. #elif F_CPU == 8000000
  189.   scale = 1;
  190. #endif
  191. #if defined(DEBUG) || defined(DEBUG2)
  192.   Serial.begin(115200); // For debuging//115200
  193.   //Serial.println("Midelic's nRF24L01");
  194.   //Serial.print("Testing SPI to NRF24L01+ ");
  195.   //if(NRF24L01_ReadReg(NRF24L01_0F_RX_ADDR_P5) == 0xC6) Serial.println(" ... passed");
  196.   //else Serial.println(" ... FAILED");
  197.   //NRF24L01_spi_test(); // 10 minute spi test.
  198. #endif
  199.   packet_counter = 0;
  200.   flags = 0;
  201.   symax_init();
  202.   phase = SYMAX_INIT1;//phase =0
  203.   //PROTOCOL_SetBindState(BIND_COUNT * PACKET_PERIOD / 1000);
  204.   //CLOCK_StartTimer(INITIAL_WAIT, symax_callback);
  205.   delayMicroseconds(SYMAX_INITIAL_WAIT);
  206.   //_delay_ms(SYMAX_INITIAL_WAIT);
  207.  
  208.   Timer1.initialize( 4000 );
  209.   Timer1.attachInterrupt( read_serial);//read_ppm );
  210.   wait_until = 0;
  211. }
  212.  
  213.  
  214.  
  215. void loop() {
  216.   //call actual function & take care of delays
  217.   while (micros() < wait_until);
  218.   wait_until = micros() + symax_callback();
  219.   //Serial.println(wait_until-lastWait);
  220.   lastWait = wait_until;
  221.  
  222.  
  223. }
  224.  
  225.  
  226. uint16_t symax_callback()
  227. {
  228.  
  229.   switch (phase) {
  230.     case SYMAX_INIT1:
  231.       symax_init1();
  232.       phase = SYMAX_BIND2;
  233.       //
  234.       return FIRST_PACKET_DELAY;
  235.       break;
  236.  
  237.     case SYMAX_BIND2:
  238.       counter = SYMAX_BIND_COUNT;
  239.       phase = SYMAX_BIND3;
  240.       SYMAX_send_packet(1);
  241.       break;
  242.  
  243.     case SYMAX_BIND3:
  244.       if (counter == 0) {
  245.         symax_init2();
  246.         phase = SYMAX_DATA;
  247.         //PROTOCOL_SetBindState(0);
  248.         //MUSIC_Play(MUSIC_DONE_BINDING);
  249.         LED_ON;
  250.       } else {
  251.         SYMAX_send_packet(1);
  252.         counter -= 1;
  253.       }
  254.       break;
  255.  
  256.     case SYMAX_DATA:
  257.       SYMAX_send_packet(0);
  258.       //return SYMAX_INITIAL_WAIT;
  259.       break;
  260.   }
  261.   return PACKET_PERIOD;
  262. }
  263.  
  264. static uint8_t checksum(uint8_t *data)
  265. {
  266.   uint8_t sum = data[0];
  267.  
  268.   for (int i = 1; i < packet_size - 1; i++)
  269.     if (mode_select == MODE_SYMAX5C)
  270.       sum += data[i];
  271.     else
  272.       sum ^= data[i];
  273.  
  274.   return sum + (mode_select == MODE_SYMAX5C ? 0 : 0x55);
  275. }
  276.  
  277.  
  278. //#define BABS(X) (((X) < 0) ? -(uint8_t)(X) : (X))
  279. // Channel values are sign + magnitude 8bit values
  280.  
  281. /*
  282. static uint8_t SYMAX_convert_channel(uint8_t num)
  283. {
  284.     //s32 ch = Channels[num];
  285.     //if (ch < CHAN_MIN_VALUE) {
  286.     //    ch = CHAN_MIN_VALUE;
  287.     ///} else if (ch > CHAN_MAX_VALUE) {
  288.     //    ch = CHAN_MAX_VALUE;
  289.    /// }
  290.     //return (uint8_t) ((ch < 0 ? 0x80 : 0) | BABS(ch * 127 / CHAN_MAX_VALUE));
  291.     return (uint8_t) (map(Servo_data[num],PPM_MIN,PPM_MAX,0,255));
  292. }
  293. */
  294.  
  295. static void SYMAX_read_controls(uint8_t* throttle, uint8_t* rudder, uint8_t* elevator, uint8_t* aileron, uint8_t* flags)
  296. {
  297.   // Protocol is registered AETRF, that is
  298.   // Aileron is channel 1, Elevator - 2, Throttle - 3, Rudder - 4, Flip control - 5
  299.  
  300.   *aileron  = convert_channel(AILERON); //rool
  301.   *elevator = convert_channel(ELEVATOR);//pitch
  302.   *throttle = convert_channel(THROTTLE);
  303.   *throttle = *throttle & 0x80 ? 0xff - *throttle : 0x80 + *throttle;
  304.   *rudder   = convert_channel(RUDDER);//yaw
  305.  
  306.   // Channel 5
  307.   if (Servo_data[AUX1] <= 1500)
  308.     *flags &= ~SYMAX_FLAG_FLIP;
  309.   else
  310.     *flags |= SYMAX_FLAG_FLIP;
  311.  
  312.   // Channel 6
  313.   //if (Servo_data[AUX2] <= 1500)
  314.   //    *flags &= ~SYMAX_FLAG_RATES;
  315.   // else
  316.   //    *flags |= SYMAX_FLAG_RATES;
  317.  
  318.   // Channel 6
  319.   if (Servo_data[AUX2] <= 128)
  320.     // *flags &= ~SYMAX_FLAG_PICTURE;
  321.     *flags &= ~SYMAX_FLAG_PICTURE;
  322.   else
  323.     *flags |= SYMAX_FLAG_PICTURE;
  324.  
  325.   // Channel 7
  326.   if (Servo_data[AUX3] <= 128)
  327.     *flags &= ~SYMAX_FLAG_VIDEO;
  328.   else
  329.     *flags |= SYMAX_FLAG_VIDEO;
  330.   //    dbgprintf("ail %5d, ele %5d, thr %5d, rud %5d, flags 0x%x\n",
  331.   //            *aileron, *elevator, *throttle, *rudder, *flags);
  332. }
  333.  
  334.  
  335. #define X5C_CHAN2TRIM(X) ((((X) & 0x80 ? 0xff - (X) : 0x80 + (X)) >> 2) + 0x20)
  336.  
  337. static void build_packet_x5c(uint8_t bind)
  338. {
  339.   if (bind) {
  340.     memset(packet, 0, packet_size);
  341.     packet[7] = 0xae;
  342.     packet[8] = 0xa9;
  343.     packet[14] = 0xc0;
  344.     packet[15] = 0x17;
  345.   } else {
  346.     SYMAX_read_controls(&throttle, &rudder, &elevator, &aileron, &flags);
  347.  
  348.     packet[0] = throttle;
  349.     packet[1] = rudder;
  350.     packet[2] = elevator ^ 0x80;  // reversed from default
  351.     packet[3] = aileron;
  352.     packet[4] = X5C_CHAN2TRIM(rudder ^ 0x80);// drive trims for extra control range
  353.     packet[5] = X5C_CHAN2TRIM(elevator);
  354.     packet[6] = X5C_CHAN2TRIM(aileron ^ 0x80);
  355.     packet[7] = 0xae;
  356.     packet[8] = 0xa9;
  357.     packet[9] = 0x00;
  358.     packet[10] = 0x00;
  359.     packet[11] = 0x00;
  360.     packet[12] = 0x00;
  361.     packet[13] = 0x00;
  362.     packet[14] = (flags & SYMAX_FLAG_VIDEO   ? 0x10 : 0x00)
  363.                  | (flags & SYMAX_FLAG_PICTURE ? 0x08 : 0x00)
  364.                  | (flags & SYMAX_FLAG_FLIP    ? 0x01 : 0x00)
  365.                  | 0x04;// (flags & SYMAX_FLAG_RATES   ? 0x04 : 0x00);
  366.     packet[15] = checksum(packet);
  367.   }
  368. }
  369.  
  370.  
  371. static void build_packet(uint8_t bind) {
  372.   if (bind) {
  373.     packet[0] = rx_tx_addr[4];
  374.     packet[1] = rx_tx_addr[3];
  375.     packet[2] = rx_tx_addr[2];
  376.     packet[3] = rx_tx_addr[1];
  377.     packet[4] = rx_tx_addr[0];
  378.     packet[5] = 0xaa;
  379.     packet[6] = 0xaa;
  380.     packet[7] = 0xaa;
  381.     packet[8] = 0x00;
  382.     // packet[9] = 0xda;
  383.     packet[9] = checksum(packet);
  384.   } else {
  385.     SYMAX_read_controls(&throttle, &rudder, &elevator, &aileron, &flags);
  386.     if (flags & SYMAX_FLAG_PICTURE)
  387.       digitalWrite(12, HIGH);
  388.     else
  389.       digitalWrite(12, LOW);
  390.     //Serial.println(throttle);
  391.     packet[0] = throttle;
  392.     packet[1] = elevator;
  393.     packet[2] = rudder;
  394.     packet[3] = aileron;
  395.     packet[4] = (flags & SYMAX_FLAG_VIDEO   ? 0x80 : 0x00) | (flags & SYMAX_FLAG_PICTURE ? 0x40 : 0x00);
  396.     packet[5] = (elevator >> 2) | 0xc0; //always high rates (bit 7 is rate control) (flags & SYMAX_FLAG_RATES ? 0x80 : 0x00) | 0x40;  // use trims to extend controls
  397.     packet[6] = (rudder >> 2) | (flags & SYMAX_FLAG_FLIP ? 0x40 : 0x00);
  398.     packet[7] = aileron >> 2;
  399.     packet[8] = 0x00;
  400.     packet[9] = checksum(packet);
  401.   }
  402. }
  403.  
  404.  
  405. static void SYMAX_send_packet(uint8_t bind)
  406. {
  407.   if (mode_select == MODE_SYMAX5C)
  408.     build_packet_x5c(bind);
  409.   else
  410.     build_packet(bind);
  411.  
  412.   // clear packet status bits and TX FIFO
  413.   NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70);
  414.   NRF24L01_WriteReg(NRF24L01_00_CONFIG, 0x2e);
  415.   NRF24L01_WriteReg(NRF24L01_05_RF_CH, chans[current_chan]);
  416.   NRF24L01_FlushTx();
  417.  
  418.   NRF24L01_WritePayload(packet, packet_size);
  419.  
  420.   if (packet_counter++ % 2) {   // use each channel twice
  421.     current_chan = (current_chan + 1) % num_rf_channels;
  422.   }
  423.  
  424.   // Check and adjust transmission power. We do this after
  425.   // transmission to not bother with timeout after power
  426.   // settings change -  we have plenty of time until next
  427.   // packet.
  428.   NRF24L01_SetPower(7);
  429.  
  430. }
  431.  
  432.  
  433.  
  434. static void symax_init()
  435. {
  436.   byte SYMAX_bind_rx_tx_addr[] = {0xab, 0xac, 0xad, 0xae, 0xaf};
  437.   byte bind_rx_tx_addr_x5c[] = {0x6d, 0x6a, 0x73, 0x73, 0x73};
  438.  
  439.   NRF24L01_Initialize();
  440.  
  441.  
  442.   NRF24L01_ReadReg(NRF24L01_07_STATUS);
  443.   NRF24L01_WriteReg(NRF24L01_00_CONFIG, BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO));
  444.   NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x00);      // No Auto Acknoledgement
  445.   NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x3F);  // Enable all data pipes (even though not used?)
  446.   NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x03);   // 5-byte RX/TX address
  447.   NRF24L01_WriteReg(NRF24L01_04_SETUP_RETR, 0xff); // 4mS retransmit t/o, 15 tries (retries w/o AA?)
  448.   NRF24L01_WriteReg(NRF24L01_05_RF_CH, 0x08);
  449.  
  450.   if (mode_select == MODE_SYMAX5C) {
  451.     NRF24L01_SetBitrate(NRF24L01_BR_1M);
  452.     packet_size = 16;
  453.   } else {
  454.     NRF24L01_SetBitrate(NRF24L01_BR_250K);
  455.     packet_size = 10;
  456.   }
  457.  
  458.   NRF24L01_SetPower(4);
  459.   NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70);     // Clear data ready, data sent, and retransmit
  460.   NRF24L01_WriteReg(NRF24L01_08_OBSERVE_TX, 0x00);
  461.   NRF24L01_WriteReg(NRF24L01_09_CD, 0x00);
  462.   NRF24L01_WriteReg(NRF24L01_0C_RX_ADDR_P2, 0xC3); // LSB byte of pipe 2 receive address
  463.   NRF24L01_WriteReg(NRF24L01_0D_RX_ADDR_P3, 0xC4);
  464.   NRF24L01_WriteReg(NRF24L01_0E_RX_ADDR_P4, 0xC5);
  465.   NRF24L01_WriteReg(NRF24L01_0F_RX_ADDR_P5, 0xC6);
  466.   NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, SYMAX_PAYLOADSIZE);   // bytes of data payload for pipe 1
  467.   NRF24L01_WriteReg(NRF24L01_12_RX_PW_P1, SYMAX_PAYLOADSIZE);
  468.   NRF24L01_WriteReg(NRF24L01_13_RX_PW_P2, SYMAX_PAYLOADSIZE);
  469.   NRF24L01_WriteReg(NRF24L01_14_RX_PW_P3, SYMAX_PAYLOADSIZE);
  470.   NRF24L01_WriteReg(NRF24L01_15_RX_PW_P4, SYMAX_PAYLOADSIZE);
  471.   NRF24L01_WriteReg(NRF24L01_16_RX_PW_P5, SYMAX_PAYLOADSIZE);
  472.   NRF24L01_WriteReg(NRF24L01_17_FIFO_STATUS, 0x00); // Just in case, no real bits to write here
  473.  
  474.   NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR, mode_select == MODE_SYMAX5C ? bind_rx_tx_addr_x5c : SYMAX_bind_rx_tx_addr, 5);
  475.  
  476.   NRF24L01_ReadReg(NRF24L01_07_STATUS);
  477.  
  478.   // Check for Beken BK2421/BK2423 chip
  479.   // It is done by using Beken specific activate code, 0x53
  480.   // and checking that status register changed appropriately
  481.   // There is no harm to run it on nRF24L01 because following
  482.   // closing activate command changes state back even if it
  483.   // does something on nRF24L01
  484.   NRF24L01_Activate(0x53); // magic for BK2421 bank switch
  485.   //dbgprintf("Trying to switch banks\n");
  486.   if (NRF24L01_ReadReg(NRF24L01_07_STATUS) & 0x80) {
  487.     //dbgprintf("BK2421 detected\n");
  488.     // Beken registers don't have such nice names, so we just mention
  489.     // them by their numbers
  490.     // It's all magic, eavesdropped from real transfer and not even from the
  491.     // data sheet - it has slightly different values
  492.     NRF24L01_WriteRegisterMulti(0x00, (uint8_t *) "\x40\x4B\x01\xE2", 4);
  493.     NRF24L01_WriteRegisterMulti(0x01, (uint8_t *) "\xC0\x4B\x00\x00", 4);
  494.     NRF24L01_WriteRegisterMulti(0x02, (uint8_t *) "\xD0\xFC\x8C\x02", 4);
  495.     NRF24L01_WriteRegisterMulti(0x03, (uint8_t *) "\x99\x00\x39\x21", 4);
  496.     NRF24L01_WriteRegisterMulti(0x04, (uint8_t *) "\xF9\x96\x82\x1B", 4);
  497.     NRF24L01_WriteRegisterMulti(0x05, (uint8_t *) "\x24\x06\x7F\xA6", 4);
  498.     NRF24L01_WriteRegisterMulti(0x06, (uint8_t *) "\x00\x00\x00\x00", 4);
  499.     NRF24L01_WriteRegisterMulti(0x07, (uint8_t *) "\x00\x00\x00\x00", 4);
  500.     NRF24L01_WriteRegisterMulti(0x08, (uint8_t *) "\x00\x00\x00\x00", 4);
  501.     NRF24L01_WriteRegisterMulti(0x09, (uint8_t *) "\x00\x00\x00\x00", 4);
  502.     NRF24L01_WriteRegisterMulti(0x0A, (uint8_t *) "\x00\x00\x00\x00", 4);
  503.     NRF24L01_WriteRegisterMulti(0x0B, (uint8_t *) "\x00\x00\x00\x00", 4);
  504.     NRF24L01_WriteRegisterMulti(0x0C, (uint8_t *) "\x00\x12\x73\x00", 4);
  505.     NRF24L01_WriteRegisterMulti(0x0D, (uint8_t *) "\x46\xB4\x80\x00", 4);
  506.     NRF24L01_WriteRegisterMulti(0x0E, (uint8_t *) "\x41\x10\x04\x82\x20\x08\x08\xF2\x7D\xEF\xFF", 11);
  507.     NRF24L01_WriteRegisterMulti(0x04, (uint8_t *) "\xFF\x96\x82\x1B", 4);
  508.     NRF24L01_WriteRegisterMulti(0x04, (uint8_t *) "\xF9\x96\x82\x1B", 4);
  509.   } else {
  510.     //dbgprintf("nRF24L01 detected\n");
  511.   }
  512.   NRF24L01_Activate(0x53); // switch bank back
  513.  
  514.   NRF24L01_FlushTx();
  515.   NRF24L01_ReadReg(NRF24L01_07_STATUS);
  516.   NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x0e);
  517.   //      NRF24L01_WriteReg(NRF24L01_07_STATUS,0x0e| (1 << NRF24L01_07_RX_DR) | (1 << NRF24L01_07_TX_DS)   );//    //reset the flag(s)| (1 << NRF24L01_07_MAX_RT)
  518.   NRF24L01_ReadReg(NRF24L01_00_CONFIG);
  519.   NRF24L01_WriteReg(NRF24L01_00_CONFIG, 0x0c);
  520.   NRF24L01_WriteReg(NRF24L01_00_CONFIG, 0x0e);  // power on
  521.  
  522.  
  523.  
  524. }
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531. static void symax_init1()
  532. {
  533.   // write a strange first packet to RF channel 8 ...
  534.   uint8_t first_packet[] = {0xf9, 0x96, 0x82, 0x1b, 0x20, 0x08, 0x08, 0xf2, 0x7d, 0xef, 0xff, 0x00, 0x00, 0x00, 0x00};
  535.   uint8_t chans_bind[] = {0x4b, 0x30, 0x40, 0x2e};
  536.   uint8_t chans_bind_x5c[] = {0x27, 0x1b, 0x39, 0x28, 0x24, 0x22, 0x2e, 0x36,
  537.                               0x19, 0x21, 0x29, 0x14, 0x1e, 0x12, 0x2d, 0x18
  538.                              };
  539.  
  540.  
  541.   //Serial.println("symax_init1");
  542.  
  543.   NRF24L01_FlushTx();
  544.   NRF24L01_WriteReg(NRF24L01_05_RF_CH, 0x08);
  545.   NRF24L01_WritePayload(first_packet, 15);
  546.  
  547.   if (mode_select == MODE_SYMAX5C) {
  548.     num_rf_channels = sizeof(chans_bind_x5c);
  549.     memcpy(chans, chans_bind_x5c, num_rf_channels);
  550.   } else {
  551.     //initialize_rx_tx_addr();   // make info available for bind packets
  552.     memcpy(rx_tx_addr, data_rx_tx_addr, 5);   // make info available for bind packets
  553.     num_rf_channels = sizeof(chans_bind);
  554.     memcpy(chans, chans_bind, num_rf_channels);
  555.   }
  556.   current_chan = 0;
  557.   packet_counter = 0;
  558. }
  559.  
  560.  
  561. static void initialize_rx_tx_addr()
  562. {
  563.   //u32 lfsr = 0xb2c54a2ful;
  564.   /*
  565.       if (Model_fixed_id) {
  566.          for (u8 i = 0, j = 0; i < sizeof(Model_fixed_id); ++i, j += 8)
  567.              rand32_r(&lfsr, (Model.fixed_id >> j) & 0xff);
  568.       }
  569.       */
  570.   /*
  571.   // Pump zero bytes for LFSR to diverge more
  572.   for (u8 i = 0; i < sizeof(lfsr); ++i) rand32_r(&lfsr, 0);
  573.  
  574.   rx_tx_addr[4] = 0xa2;
  575.   for (u8 i = 0; i < sizeof(rx_tx_addr)-1; ++i) {
  576.       rx_tx_addr[i] = lfsr & 0xff;
  577.       rand32_r(&lfsr, i);
  578.   }
  579.   */
  580. }
  581. /*
  582.       TX address        Channel Sequence
  583. S1    3B B6 00 00 A2    15 35 1D 3D
  584. D1    9A E9 02 00 A2    14 34 1C 3C
  585. D2    46 18 00 00 A2    11 21 31 41
  586. */
  587.  
  588. // channels determined by last byte of tx address
  589. static void set_channels(uint8_t address) {
  590.   static const uint8_t start_chans_1[] = {0x0a, 0x1a, 0x2a, 0x3a};
  591.   static const uint8_t start_chans_2[] = {0x2a, 0x0a, 0x42, 0x22};
  592.   static const uint8_t start_chans_3[] = {0x1a, 0x3a, 0x12, 0x32};
  593.   static const uint8_t start_chans_4[] = {0x15, 0x35, 0x1d, 0x3d};
  594.   static const uint8_t start_chans_5[] = {0x14, 0x34, 0x1c, 0x3c};
  595.   static const uint8_t start_chans_6[] = {0x11, 0x21, 0x31, 0x41};
  596.  
  597.   uint8_t laddress = address & 0x1f;
  598.   Serial.print("laddress:");  Serial.println(laddress);
  599.   uint8_t i;
  600.   uint32_t *pchans = (uint32_t *)chans;   // avoid compiler warning
  601.  
  602.   num_rf_channels = 4;
  603.  
  604.   if (laddress < 0x10) {
  605.     if (laddress == 6) laddress = 7;
  606.     for (i = 0; i < num_rf_channels; i++) {
  607.       //Serial.println("start_chans_1");
  608.       chans[i] = start_chans_1[i] + laddress;
  609.       //      chans[i] = start_chans_6[i] + laddress;
  610.     }
  611.   } else if (laddress < 0x18) {
  612.     for (i = 0; i < num_rf_channels; i++) {
  613.       //Serial.println("start_chans_2");
  614.       chans[i] = start_chans_2[i] + (laddress & 0x07);
  615.     }
  616.     if (laddress == 0x16) {
  617.       chans[0] += 1;
  618.       chans[1] += 1;
  619.     }
  620.   } else if (laddress < 0x1e) {
  621.     //Serial.println("start_chans_3");
  622.     for (i = 0; i < num_rf_channels; i++) {
  623.       chans[i] = start_chans_3[i] + (laddress & 0x07);
  624.       //chans[i] = start_chans_5[i] ;
  625.       //Serial.println(chans[i]);
  626.     }
  627.   } else if (laddress == 0x1e) {
  628.     *pchans = 0x38184121;
  629.   } else {
  630.     *pchans = 0x39194121;
  631.   }
  632. }
  633.  
  634.  
  635. static void symax_init2()
  636. {
  637.   // uint8_t chans_data[] = {0x1d, 0x3d, 0x15, 0x35};
  638.   uint8_t chans_data_x5c[] = {0x1d, 0x2f, 0x26, 0x3d, 0x15, 0x2b, 0x25, 0x24,
  639.                               0x27, 0x2c, 0x1c, 0x3e, 0x39, 0x2d, 0x22
  640.                              };
  641.   //Serial.println("symax_init2");
  642.   if (mode_select == MODE_SYMAX5C) {
  643.     num_rf_channels = sizeof(chans_data_x5c);
  644.     memcpy(chans, chans_data_x5c, num_rf_channels);
  645.   } else {
  646.     //num_rf_channels = sizeof(chans_data);
  647.     //memcpy(chans, chans_data, num_rf_channels);
  648.     set_channels(rx_tx_addr[0]);
  649.     NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR, rx_tx_addr, 5);
  650.   }
  651.   current_chan = 0;
  652.   packet_counter = 0;
  653. }
  654.  
  655. uint8_t convert_channel(uint8_t num) {
  656.  
  657.   if (Servo_data[num] < PPM_MIN) {
  658.     Servo_data[num] = PPM_MIN;
  659.   } else if (Servo_data[num] > PPM_MAX) {
  660.     Servo_data[num] = PPM_MAX;
  661.   }
  662.   return (uint8_t) (map(Servo_data[num], PPM_MIN, PPM_MAX, 0, 255));
  663. }
  664.  
  665. void read_ppm() {
  666.   //Serial.println("read_ppm");
  667.   static unsigned int pulse;
  668.   static unsigned long counterPPM;
  669.   static byte chan;
  670.   counterPPM = TCNT1;
  671.   //TCNT1 = 0;
  672.   if (counterPPM < 510 * scale) {
  673.     pulse = counterPPM;
  674.   }
  675.   else if (counterPPM > 1910 * scale) {
  676.     chan = 0;
  677.   }
  678.   else {
  679.     Servo_data[chan] = (counterPPM + pulse) / scale;
  680.     Servo_data[chan] = constrain(Servo_data[chan], PPM_MIN, PPM_MAX);
  681.     chan++;
  682.   }
  683. }
  684.  
  685. /*
  686.       serial_sync = Serial.read();
  687.       if (serial_sync == SYNC_BYTE)
  688.       {
  689.         for (int i = 0; i < NUM_ACTIVE_CHANNELS; i++)
  690.         {
  691.           while (!Serial.available())
  692.             ;
  693.  
  694.           input_byte = Serial.read();
  695.           input_value = CALC_CHANNEL_VALUE(input_byte);
  696.           frame_sync += (channel_ppm[i] - input_value);
  697.           channel_ppm[i] = input_value;
  698.         }
  699.       }
  700. */
  701. void read_serial()
  702. {
  703.   // Serial.println("read");
  704.   int  result = 0;
  705.   while (Serial.available() >= 8)
  706.   {
  707.     digitalWrite(12, HIGH);
  708.     result == 4;
  709.     int input_value;
  710.     // Serial.print("received: ");
  711.     byte number = Serial.read();
  712.     if (number == 255 )   {
  713.       result == 3;
  714.       for (int i = 0; i < 7; i++) {
  715.         // while (!Serial.available())
  716.         //   {delayMicroseconds(10);}
  717.         number =        Serial.read();
  718.         Servo_data[i] = number;
  719.         // Serial.print(Servo_data[i]);
  720.         // Serial.print('\n');
  721.       }
  722.       // Serial.print("===\n");
  723.     }
  724.   }
  725.   digitalWrite(12, LOW);
  726.  
  727. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement