Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / BUMBLEB_ccnrf.ino
blobfe157a7b5f646167063c6b37cf9f07622e97ba14
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(BUMBLEB_CCNRF_INO)
18 #include "iface_xn297.h"
20 #define FORCE_BUMBLEB_ORIGINAL_ID
21 #define BUMBLEB_TELEM_DEBUG
23 #define BUMBLEB_PACKET_PERIOD   10200
24 #define BUMBLEB_RF_BIND_CHANNEL 42
25 #define BUMBLEB_RF_NUM_CHANNELS 2
26 #define BUMBLEB_PAYLOAD_SIZE    7
28 static void __attribute__((unused)) BUMBLEB_send_packet()
30         packet[6] = 0x00;
31         if(IS_BIND_IN_PROGRESS)
32         {
33                 packet[0] = rx_tx_addr[0];
34                 packet[1] = rx_tx_addr[1];
35                 packet[2] = 0x54;                        //???
36                 packet[3] = 0x58;                        //???
37                 hopping_frequency_no ^= 0x01;
38                 packet[4] = hopping_frequency[hopping_frequency_no];
39         }
40         else
41         {
42                 //hopping frequency
43                 XN297_Hopping(hopping_frequency_no);
44                 hopping_frequency_no ^= 0x01;
45                 packet[0] = 0x20
46                                         |GET_FLAG(CH6_SW,0x80);                         // High rate
47                 packet[1] = convert_channel_8b_limit_deadband(AILERON,0xBF,0xA0,0x81,40);       // Aileron: Max values:BD..A0..82
48                 if(packet[1] < 0xA0)
49                         packet[1] = 0x20 - packet[1];                           // Reverse low part of aileron
50                 packet[2] = convert_channel_8b(CH5)>>2;                 // 01..20..3F
51                 if(CH7_SW)                                                                              // Drive trim from aileron
52                 {
53                         uint8_t ch=convert_channel_8b(AILERON);
54                         if(ch > 0x5A && ch < 0x80-0x07)
55                                 packet[2] = ch - 0x5A;
56                         else if(ch < 0x5A)
57                         {
58                                 if(ch < 0x5A-0x20)
59                                         packet[2] = 0;
60                                 else
61                                         packet[2] = ch - (0x5A-0x20);
62                         }
63                         else if(packet[1] == 0x89)
64                                 packet[2] = 0x20;
65                         else if(ch > 0xA5)
66                         {
67                                 if(ch > 0xA9+0x1F)
68                                         packet[2] = 0x3F;
69                                 else
70                                         packet[2] = ch - 0x89;
71                         }
72                         else if(ch > 0xA5-0x1F)
73                                 packet[2] = ch - (0xA5-0x1F-0x20);
74                 }
75                 else
76                         packet[2] = convert_channel_8b(CH5)>>2;         // 01..20..3F
77                 packet[3] = convert_channel_8b(THROTTLE)>>2;    // 00..3F
78                 packet[4] = hopping_frequency[hopping_frequency_no];
79         }
81         packet[5] = packet[0];
82         for(uint8_t i=1;i<BUMBLEB_PAYLOAD_SIZE-2;i++)
83                 packet[5] += packet[i];
85         #if 0
86                 debug("P:");
87                 for(uint8_t i=0;i<BUMBLEB_PAYLOAD_SIZE;i++)
88                         debug(" %02X", packet[i]);
89                 debugln("");
90         #endif
92         XN297_SetPower();                                                                       // Set tx_power
93         XN297_SetFreqOffset();                                                          // Set frequency offset
94         XN297_SetTxRxMode(TX_EN);
95         XN297_WritePayload(packet, BUMBLEB_PAYLOAD_SIZE);
98 static void __attribute__((unused)) BUMBLEB_RF_init()
100         //Config CC2500
101         XN297_Configure(XN297_CRCEN, XN297_SCRAMBLED, XN297_250K);
102         XN297_SetTXAddr((uint8_t*)"\x55\x55\x55\x55\x55", 5);
103         XN297_HoppingCalib(BUMBLEB_RF_NUM_CHANNELS);            // Calibrate all channels
104         XN297_RFChannel(BUMBLEB_RF_BIND_CHANNEL);                       // Set bind channel
105         XN297_SetRXAddr(rx_tx_addr, BUMBLEB_PAYLOAD_SIZE);
108 static void __attribute__((unused)) BUMBLEB_initialize_txid()
110         calc_fh_channels(BUMBLEB_RF_NUM_CHANNELS);
111         rx_tx_addr[0] = rx_tx_addr[2];
112         rx_tx_addr[1] = rx_tx_addr[3];
113         #ifdef FORCE_BUMBLEB_ORIGINAL_ID
114                 rx_tx_addr[0] = 0x33;
115                 rx_tx_addr[1] = 0x65;
116                 hopping_frequency[0] = 2;
117                 hopping_frequency[1] = 40;
118         #endif
119         rx_tx_addr[2] = rx_tx_addr[3] = rx_tx_addr[4] = 0x55;
122 enum {
123         BUMBLEB_BIND            = 0x00,
124         BUMBLEB_BINDRX          = 0x01,
125         BUMBLEB_DATA            = 0x02,
128 #define BUMBLEB_WRITE_TIME 850
130 uint16_t BUMBLEB_callback()
132         bool rx;
133         switch(phase)
134         {
135                 case BUMBLEB_BIND:
136                         rx = XN297_IsRX();                              // Needed for the NRF24L01 since otherwise the bit gets cleared
138                         BUMBLEB_send_packet();
140                         if( rx )
141                         { // a packet has been received
142                                 #ifdef BUMBLEB_TELEM_DEBUG
143                                         debug("RX :");
144                                 #endif
145                                 if(XN297_ReadPayload(packet_in, BUMBLEB_PAYLOAD_SIZE))
146                                 { // packet with good CRC
147                                         #ifdef BUMBLEB_TELEM_DEBUG
148                                                 debug("OK :");
149                                                 for(uint8_t i=0;i<BUMBLEB_PAYLOAD_SIZE;i++)
150                                                         debug(" %02X",packet_in[i]);
151                                         #endif
152                                         // packet_in = 4F 71 55 52 58 61 AA
153                                         rx_tx_addr[2] = packet_in[0];
154                                         rx_tx_addr[3] = packet_in[1];
155                                         //rx_tx_addr[4] = packet_in[2]; // to test with other planes...
156                                         XN297_SetTXAddr(rx_tx_addr, 5);
157                                         BIND_DONE;
158                                         phase = BUMBLEB_DATA;
159                                         break;
160                                 }
161                         }
162                         phase++;
163                         return BUMBLEB_WRITE_TIME;
164                 case BUMBLEB_BINDRX:
165                         {
166                                 uint16_t start=(uint16_t)micros();
167                                 while ((uint16_t)((uint16_t)micros()-(uint16_t)start) < 500)
168                                 {
169                                         if(XN297_IsPacketSent())
170                                                 break;
171                                 }
172                         }
173                         XN297_SetTxRxMode(RX_EN);
174                         phase = BUMBLEB_BIND;
175                         return BUMBLEB_PACKET_PERIOD-BUMBLEB_WRITE_TIME;
176                 case BUMBLEB_DATA:
177                         #ifdef MULTI_SYNC
178                                 telemetry_set_input_sync(BUMBLEB_PACKET_PERIOD);
179                         #endif
180                         BUMBLEB_send_packet();
181                         break;
182         }
183         return BUMBLEB_PACKET_PERIOD;
186 void BUMBLEB_init()
188         BUMBLEB_initialize_txid();
189         BUMBLEB_RF_init();
190         hopping_frequency_no = 0;
191         
192         BIND_IN_PROGRESS;       // autobind protocol
193         phase = BUMBLEB_BIND;
196 #endif