Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / ESky_nrf24l01.ino
blob8aa51c10f7ad1bc6c8823c086937ffcc1599aa20
1 /*
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/>.
14  */
15 // Last sync with hexfet new_protocols/esky_nrf24l01.c dated 2015-02-13
17 #if defined(ESKY_NRF24L01_INO)
19 #include "iface_nrf24l01.h"
21 //#define ESKY_ET4_FORCE_ID
23 #define ESKY_BIND_COUNT         1000
24 #define ESKY_STD_PACKET_PERIOD  3333
25 #define ESKY_ET4_PACKET_PERIOD  1190
26 #define ESKY_ET4_TOTAL_PACKET_PERIOD    20300
27 #define ESKY_ET4_BIND_PACKET_PERIOD     5000
28 #define ESKY_PAYLOAD_SIZE       13
29 #define ESKY_PACKET_CHKTIME     100 // Time to wait for packet to be sent (no ACK, so very short)
31 static void __attribute__((unused)) ESKY_set_data_address()
33         NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x02);     // 4-byte RX/TX address for regular packets
34         NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0, rx_tx_addr, 4);
35         NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR,    rx_tx_addr, 4);
38 static void __attribute__((unused)) ESKY_RF_init()
40         NRF24L01_Initialize();
42         if (IS_BIND_IN_PROGRESS)
43         {
44                 NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x01);     // 3-byte RX/TX address for bind packets
45                 NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0, (uint8_t*)"\x00\x00\x00", 3);
46                 NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR,    (uint8_t*)"\x00\x00\x00", 3);
47         }
48         else
49                 ESKY_set_data_address();
50         NRF24L01_WriteReg(NRF24L01_05_RF_CH, 50);              // Channel 50 for bind packets
51         NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, ESKY_PAYLOAD_SIZE);  // bytes of data payload for pipe 0
52         NRF24L01_WriteReg(NRF24L01_12_RX_PW_P1, ESKY_PAYLOAD_SIZE);
53         NRF24L01_WriteReg(NRF24L01_13_RX_PW_P2, ESKY_PAYLOAD_SIZE);
54         NRF24L01_WriteReg(NRF24L01_14_RX_PW_P3, ESKY_PAYLOAD_SIZE);
55         NRF24L01_WriteReg(NRF24L01_15_RX_PW_P4, ESKY_PAYLOAD_SIZE);
56         NRF24L01_WriteReg(NRF24L01_16_RX_PW_P5, ESKY_PAYLOAD_SIZE);
57         NRF24L01_WriteReg(NRF24L01_17_FIFO_STATUS, 0x00);      // Just in case, no real bits to write here
60 static void __attribute__((unused)) ESKY_TXID_init()
62         NRF24L01_FlushTx();
63         if(sub_protocol==ESKY_STD)
64         {
65                 uint16_t channel_ord = rx_tx_addr[0] % 74;
66                 hopping_frequency[12] = 10 + (uint8_t)channel_ord;      //channel_code
67                 uint8_t channel1, channel2;
68                 channel1 = 10 + (uint8_t)((37 + channel_ord*5) % 74);
69                 channel2 = 10 + (uint8_t)((     channel_ord*5) % 74) ;
71                 hopping_frequency[0] = channel1;
72                 hopping_frequency[1] = channel1;
73                 hopping_frequency[2] = channel1;
74                 hopping_frequency[3] = channel2;
75                 hopping_frequency[4] = channel2;
76                 hopping_frequency[5] = channel2;
78                 //end_bytes
79                 hopping_frequency[6] = 6;
80                 hopping_frequency[7] = channel1*2;
81                 hopping_frequency[8] = channel2*2;
82                 hopping_frequency[9] = 6;
83                 hopping_frequency[10] = channel1*2;
84                 hopping_frequency[11] = channel2*2;
85         }
86         else
87         { // ESKY_ET4
88                 hopping_frequency[0]  = 0x29;   //41
89                 hopping_frequency[1]  = 0x12;   //18
90                 hopping_frequency[6]  = 0x87;   //135 payload end byte
91                 hopping_frequency[12] = 0x84;   //132 indicates which channels to use
92         }
93                 
94         // Turn radio power on
95         NRF24L01_SetTxRxMode(TX_EN);
98 static void __attribute__((unused)) ESKY_send_packet(uint8_t bind)
100         uint8_t rf_ch = 50; // bind channel
101         if (bind)
102         {
103                 // Bind packet
104                 packet[0]  = rx_tx_addr[2];
105                 packet[1]  = rx_tx_addr[1];
106                 packet[2]  = rx_tx_addr[0];
107                 packet[3]  = hopping_frequency[12]; // channel_code encodes pair of channels to transmit on
108                 packet[4]  = 0x18;
109                 packet[5]  = 0x29;
110                 packet[6]  = 0;
111                 packet[7]  = 0;
112                 packet[8]  = 0;
113                 packet[9]  = 0;
114                 packet[10] = 0;
115                 packet[11] = 0;
116                 packet[12] = 0;
117         }
118         else
119         {
120                 if (packet_count == 0)
121                         for (uint8_t i = 0; i < 6; i++)
122                         {
123                                 uint16_t val=convert_channel_ppm(CH_AETR[i]);
124                                 packet[i*2]   = val>>8;         //high byte of servo timing(1000-2000us)
125                                 packet[i*2+1] = val&0xFF;       //low byte of servo timing(1000-2000us)
126                         }
127                 if(sub_protocol==ESKY_STD)
128                 {
129                         // Regular packet
130                         // Each data packet is repeated 3 times on one channel, and 3 times on another channel
131                         // For arithmetic simplicity, channels are repeated in rf_channels array
132                         rf_ch = hopping_frequency[packet_count];
133                         packet[12] = hopping_frequency[packet_count+6]; // end_bytes
134                         packet_count++;
135                         if (packet_count > 6) packet_count = 0;
136                 }
137                 else
138                 { // ESKY_ET4
139                         // Regular packet
140                         // Each data packet is repeated 14 times alternating between 2 channels
141                         rf_ch = hopping_frequency[packet_count&1];
142                         packet_count++;
143                         if(packet_count>14) packet_count=0;
144                         packet[12] = hopping_frequency[6];      // end_byte
145                 }
146         }
147         NRF24L01_WriteReg(NRF24L01_05_RF_CH, rf_ch);
148         NRF24L01_FlushTx();
149         NRF24L01_WritePayload(packet, ESKY_PAYLOAD_SIZE);
150         NRF24L01_SetPower();    //Keep transmit power updated
153 uint16_t ESKY_callback()
155         if(IS_BIND_DONE)
156         {
157                 #ifdef MULTI_SYNC
158                         if(packet_count==0)
159                                 telemetry_set_input_sync(sub_protocol==ESKY_STD?ESKY_STD_PACKET_PERIOD*6:ESKY_ET4_TOTAL_PACKET_PERIOD);
160                 #endif
161                 ESKY_send_packet(0);
162                 if(sub_protocol==ESKY_ET4)
163                 {
164                         if(packet_count==0)
165                                 return ESKY_ET4_TOTAL_PACKET_PERIOD-ESKY_ET4_PACKET_PERIOD*13;
166                         else
167                                 return ESKY_ET4_PACKET_PERIOD;
168                 }
169         }
170         else
171         {
172                 ESKY_send_packet(1);
173                 if (--bind_counter == 0)
174                 {
175                         ESKY_set_data_address();
176                         BIND_DONE;
177                 }
178         }
179         return ESKY_STD_PACKET_PERIOD;
182 void ESKY_init(void)
184         bind_counter = ESKY_BIND_COUNT;
185         rx_tx_addr[2] = rx_tx_addr[3];  // Model match
186         #ifdef ESKY_ET4_FORCE_ID
187           if(sub_protocol==ESKY_ET4)
188           {
189                   rx_tx_addr[0]=0x72;
190                   rx_tx_addr[1]=0xBB;
191                   rx_tx_addr[2]=0xCC;
192           }
193         #endif
194         rx_tx_addr[3] = 0xBB;
195         ESKY_RF_init();
196         ESKY_TXID_init();
197         packet_count=0;
200 #endif