Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / Skyartec_cc2500.ino
blobe82870c42c8285c4a01150c284673fa898e20e1d
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(SKYARTEC_CC2500_INO)
18 #include "iface_cc2500.h"
20 //#define SKYARTEC_FORCE_ID
22 #define SKYARTEC_COARSE                         0x00
23 #define SKYARTEC_TX_ADDR                        rx_tx_addr[1]
24 #define SKYARTEC_TX_CHANNEL                     rx_tx_addr[0]
26 enum {
27     SKYARTEC_PKT1 = 0,
28     SKYARTEC_SLEEP1, 
29     SKYARTEC_PKT2,
30     SKYARTEC_SLEEP2, 
31     SKYARTEC_PKT3,
32     SKYARTEC_SLEEP3, 
33     SKYARTEC_PKT4,
34     SKYARTEC_SLEEP4, 
35     SKYARTEC_PKT5,
36     SKYARTEC_SLEEP5, 
37     SKYARTEC_PKT6,
38     SKYARTEC_LAST,
41 const PROGMEM uint8_t SKYARTEC_init_values[] = {
42   /* 04 */ 0x13, 0x18, 0xFF, 0x05,
43   /* 08 */ 0x05, 0x43, 0xCD, 0x09, 0x00, 0x5D, 0x93, 0xB1 + SKYARTEC_COARSE,
44   /* 10 */ 0x2D, 0x20, 0x73, 0x22, 0xF8, 0x50, 0x07, 0x30,
45   /* 18 */ 0x18, 0x1D, 0x1C, 0xC7, 0x00, 0xB2, 0x87, 0x6B,
46   /* 20 */ 0xF8, 0xB6, 0x10, 0xEA, 0x0A, 0x00, 0x11, 0x41,
47   /* 28 */ 0x00, 0x59, 0x7F, 0x3F, 0x88, 0x31, 0x0B
50 static void __attribute__((unused)) SKYARTEC_rf_init()
52         CC2500_Strobe(CC2500_SIDLE);
54         for (uint8_t i = 4; i <= 0x2E; ++i)
55                 CC2500_WriteReg(i, pgm_read_byte_near(&SKYARTEC_init_values[i-4]));
56         
57         CC2500_WriteReg(CC2500_0C_FSCTRL0, option);
59         CC2500_SetTxRxMode(TX_EN);
60         CC2500_SetPower();
61         CC2500_Strobe(CC2500_SFTX);
62         CC2500_Strobe(CC2500_SFRX);
63         CC2500_Strobe(CC2500_SXOFF);
64         CC2500_Strobe(CC2500_SIDLE);
67 static void __attribute__((unused)) SKYARTEC_send_data_packet()
69         //13 c5 01 0259 0168 0000 0259 030c 021a 0489 f3 7e 0a
70         //header
71         packet[0] = 0x13;                               //Length
72         packet[1] = SKYARTEC_TX_ADDR;   //Tx Addr?
73         packet[2] = 0x01;                               //???
74         //channels
75         for(uint8_t i = 0; i < 7; i++)
76         {
77                 uint16_t value = convert_channel_16b_limit(CH_AETR[i],0x000,0x500);
78                 packet[3+2*i] = value >> 8;
79                 packet[4+2*i] = value & 0xff;
80         }
81         //checks
82     uint8_t xor1 = 0;
83     for(uint8_t i = 3; i <= 14; i++)
84         xor1 ^= packet[i];
85     packet[18] = xor1;
86     xor1 ^= packet[15];
87     xor1 ^= packet[16];
88     packet[17] = xor1;
89     packet[19] = packet[3] + packet[5] + packet[7] + packet[9] + packet[11] + packet[13];
91         CC2500_WriteReg(CC2500_04_SYNC1,        rx_tx_addr[3]);
92         CC2500_WriteReg(CC2500_05_SYNC0,        rx_tx_addr[2]);
93         CC2500_WriteReg(CC2500_09_ADDR,         SKYARTEC_TX_ADDR);
94         CC2500_WriteReg(CC2500_0A_CHANNR,       SKYARTEC_TX_CHANNEL);
95         CC2500_WriteData(packet, 20);
98 static void __attribute__((unused)) SKYARTEC_send_bind_packet()
100         //0b 7d 01 01 b2 c5 4a 2f 00 00 c5 d6
101         packet[0] = 0x0b;       //Length
102         packet[1] = 0x7d;
103         packet[2] = 0x01;
104         packet[3] = 0x01;
105         packet[4] = rx_tx_addr[0];
106         packet[5] = rx_tx_addr[1];
107         packet[6] = rx_tx_addr[2];
108         packet[7] = rx_tx_addr[3];
109         packet[8] = 0x00;
110         packet[9] = 0x00;
111         packet[10] = SKYARTEC_TX_ADDR;
112         uint8_t xor1 = 0;
113         for(uint8_t i = 3; i < 11; i++)
114                 xor1 ^= packet[i];
115         packet[11] = xor1;
116         CC2500_WriteReg(CC2500_04_SYNC1,        0x7d);
117         CC2500_WriteReg(CC2500_05_SYNC0,        0x7d);
118         CC2500_WriteReg(CC2500_09_ADDR,         0x7d);
119         CC2500_WriteReg(CC2500_0A_CHANNR,       0x7d);
120         CC2500_WriteData(packet, 12);
123 uint16_t SKYARTEC_callback()
125         if (phase & 0x01)
126         {
127                 CC2500_Strobe(CC2500_SIDLE);
128                 if (phase == SKYARTEC_LAST)
129                 {
130                         CC2500_SetPower();
131                         // Tune frequency if it has been changed
132                         CC2500_SetFreqOffset();
133                         phase = SKYARTEC_PKT1;
134                 }
135                 else
136                         phase++;
137                 return 3000;
138         }
139         if (phase == SKYARTEC_PKT1 && bind_counter)
140         {
141                 SKYARTEC_send_bind_packet();
142                 bind_counter--;
143                 if(bind_counter == 0)
144                         BIND_DONE;
145         }
146         else
147         {
148                 #ifdef MULTI_SYNC
149                         telemetry_set_input_sync(6000);
150                 #endif
151                 SKYARTEC_send_data_packet();
152         }
153         phase++;
154         return 3000;
157 void SKYARTEC_init()
159     SKYARTEC_rf_init();
161         #ifdef SKYARTEC_FORCE_ID
162                 memset(rx_tx_addr,0x00,4);
163         #endif
164         if(rx_tx_addr[0]==0) rx_tx_addr[0]=0xB2;
165         if(rx_tx_addr[1]==0) rx_tx_addr[1]=0xC5;
166         if(rx_tx_addr[2]==0) rx_tx_addr[2]=0x4A;
167         if(rx_tx_addr[3]==0) rx_tx_addr[3]=0x2F;
169         bind_counter = 250;
170         phase = SKYARTEC_PKT1;
173 #endif