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/>.
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)
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);
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()
63 if(sub_protocol==ESKY_STD)
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;
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;
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
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
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
120 if (packet_count == 0)
121 for (uint8_t i = 0; i < 6; i++)
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)
127 if(sub_protocol==ESKY_STD)
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
135 if (packet_count > 6) packet_count = 0;
140 // Each data packet is repeated 14 times alternating between 2 channels
141 rf_ch = hopping_frequency[packet_count&1];
143 if(packet_count>14) packet_count=0;
144 packet[12] = hopping_frequency[6]; // end_byte
147 NRF24L01_WriteReg(NRF24L01_05_RF_CH, rf_ch);
149 NRF24L01_WritePayload(packet, ESKY_PAYLOAD_SIZE);
150 NRF24L01_SetPower(); //Keep transmit power updated
153 uint16_t ESKY_callback()
159 telemetry_set_input_sync(sub_protocol==ESKY_STD?ESKY_STD_PACKET_PERIOD*6:ESKY_ET4_TOTAL_PACKET_PERIOD);
162 if(sub_protocol==ESKY_ET4)
165 return ESKY_ET4_TOTAL_PACKET_PERIOD-ESKY_ET4_PACKET_PERIOD*13;
167 return ESKY_ET4_PACKET_PERIOD;
173 if (--bind_counter == 0)
175 ESKY_set_data_address();
179 return ESKY_STD_PACKET_PERIOD;
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)
194 rx_tx_addr[3] = 0xBB;