Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / ASSAN_nrf24l01.ino
blobc64869da5c43e0b241cadd1d6c60f23ec135551f
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  */
16 #if defined(ASSAN_NRF24L01_INO)
18 #include "iface_nrf24l01.h"
20 #define ASSAN_PACKET_SIZE               20
21 #define ASSAN_RF_BIND_CHANNEL   0x03
22 #define ASSAN_ADDRESS_LENGTH    4
24 enum {
25     ASSAN_BIND0=0,
26     ASSAN_BIND1,
27     ASSAN_BIND2,
28     ASSAN_DATA0,
29     ASSAN_DATA1,
30     ASSAN_DATA2,
31     ASSAN_DATA3,
32     ASSAN_DATA4,
33     ASSAN_DATA5
36 void ASSAN_RF_init()
38     NRF24L01_Initialize();
39         //Specifics to ASSAN
40     NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x02);                      // 4 bytes rx/tx address
41         NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR,    (uint8_t *)"\x80\x80\x80\xB8", ASSAN_ADDRESS_LENGTH);       // Bind address
42         NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0, (uint8_t *)"\x80\x80\x80\xB8", ASSAN_ADDRESS_LENGTH);       // Bind address
43         NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, ASSAN_PACKET_SIZE);
46 void ASSAN_send_packet()
48         for(uint8_t i=0;i<8;i++)
49         {
50                 uint16_t val=Channel_data[i];
51                 val=((val<<2)+val)+(860<<3);                                    // PPM value <<3
52                 
53                 packet[2*i]=val>>8;
54                 packet[2*i+1]=val;
55         }
56         for(uint8_t i=0;i<ASSAN_ADDRESS_LENGTH;i++)
57                 packet[16+i]=packet[23-i];
58         NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70);            // Clear data ready, data sent, and retransmit
59         NRF24L01_FlushTx();
60         NRF24L01_WritePayload(packet, ASSAN_PACKET_SIZE);
63 uint16_t ASSAN_callback()
65         switch (phase)
66         {
67         // Bind
68                 case ASSAN_BIND0:
69                         //Config RX @1M
70                         NRF24L01_WriteReg(NRF24L01_05_RF_CH, ASSAN_RF_BIND_CHANNEL);
71                         NRF24L01_SetBitrate(NRF24L01_BR_1M);                                    // 1Mbps
72                         NRF24L01_SetTxRxMode(RX_EN);
73                         phase++;
74                 case ASSAN_BIND1:
75                         //Wait for receiver to send the frames
76                         if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR))
77                         { //Something has been received
78                                 NRF24L01_ReadPayload(packet, ASSAN_PACKET_SIZE);
79                                 if(packet[19]==0x13)
80                                 { //Last frame received
81                                         phase++;
82                                         //Switch to TX
83                                         NRF24L01_SetTxRxMode(TXRX_OFF);
84                                         NRF24L01_SetTxRxMode(TX_EN);
85                                         //Prepare bind packet
86                                         memset(packet,0x05,ASSAN_PACKET_SIZE-5);
87                                         packet[15]=0x99;
88                                         for(uint8_t i=0;i<ASSAN_ADDRESS_LENGTH;i++)
89                                                 packet[16+i]=packet[23-i];
90                                         packet_count=0;
91                                         delayMilliseconds(260);
92                                         return 10000;   // Wait 270ms in total...
93                                 }
94                         }
95                         return 1000;
96                 case ASSAN_BIND2:
97                         // Send 20 packets
98                         packet_count++;
99                         if(packet_count==20)
100                                 packet[15]=0x13;        // different value for last packet
101                         NRF24L01_WritePayload(packet, ASSAN_PACKET_SIZE);
102                         if(packet_count==20)
103                         {
104                                 phase++;
105                                 delayMilliseconds(2165);
106                         }
107                         return 22520;
108         // Normal operation
109                 case ASSAN_DATA0:
110                         // Bind Done
111                         BIND_DONE;
112                         NRF24L01_SetBitrate(NRF24L01_BR_250K);                                  // 250Kbps
113                         NRF24L01_SetTxRxMode(TXRX_OFF);
114                         NRF24L01_SetTxRxMode(TX_EN);
115                 case ASSAN_DATA1:
116                 case ASSAN_DATA4:
117                         // Change ID and RF channel
118                         NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR,packet+20+4*hopping_frequency_no, ASSAN_ADDRESS_LENGTH);
119                         NRF24L01_WriteReg(NRF24L01_05_RF_CH, hopping_frequency[hopping_frequency_no]);
120                         hopping_frequency_no^=0x01;
121                         NRF24L01_SetPower();
122                         phase=ASSAN_DATA2;
123                         return 2000;
124                 case ASSAN_DATA2:
125                         #ifdef MULTI_SYNC
126                                 telemetry_set_input_sync(12000);
127                         #endif
128                 case ASSAN_DATA3:
129                         ASSAN_send_packet();
130                         phase++;        // DATA 3 or 4
131                         return 5000;
132         }
133         return 0;
136 static void __attribute__((unused)) ASSAN_initialize_txid()
138 /*      //Renaud TXID with Freq=36 and alternate Freq 67 or 68 or 69 or 70 or 71 or 73 or 74 or 75 or 78 and may be more...
139         packet[23]=0x22;
140         packet[22]=0x37;
141         packet[21]=0xFA;
142         packet[20]=0x53; */
143         // Using packet[20..23] to store the ID1 and packet[24..27] to store the ID2
144         uint8_t freq=0,freq2;
145         for(uint8_t i=0;i<ASSAN_ADDRESS_LENGTH;i++)
146         {
147                 uint8_t temp=rx_tx_addr[i];
148                 packet[i+20]=temp;
149                 packet[i+24]=temp+1;
150                 freq+=temp;
151         }       
153         // Main frequency
154         freq=((freq%25)+2)<<1;
155         if(freq&0x02)   freq|=0x01;
156         hopping_frequency[0]=freq;
157         // Alternate frequency has some random
158         do
159         {
160                 freq2=random(0xfefefefe)%9;
161                 freq2+=freq*2-5;
162         }
163         while( (freq2>118) || (freq2<freq+1) || (freq2==2*freq) );
164         hopping_frequency[1]=freq2;
167 void ASSAN_init()
169         ASSAN_initialize_txid();
170         ASSAN_RF_init();
171         hopping_frequency_no = 0;
173         if(IS_BIND_IN_PROGRESS)
174                 phase=ASSAN_BIND0;
175         else 
176                 phase=ASSAN_DATA0;
179 #endif