Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / HS6200_EMU.ino
blob5ef74fe039fc3c10ec78ff6f3ef0b4e5531bbf11
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 #ifdef CYRF6936_INSTALLED
16 #include "iface_hs6200.h"
18 static bool HS6200_crc;
19 static uint16_t HS6200_crc_init;
20 static uint8_t HS6200_address_length, HS6200_tx_addr[5];
22 static void __attribute__((unused)) HS6200_Init(bool crc_en)
24         CYRF_GFSK1M_Init(32, 1);        //Dummy number of bytes for now
25         HS6200_crc = crc_en;
28 static void __attribute__((unused)) HS6200_SetTXAddr(const uint8_t* addr, uint8_t addr_len)
30         // precompute address crc
31         crc = 0xffff;
32         for(uint8_t i=0; i<addr_len; i++)
33                 crc16_update(addr[addr_len-1-i], 8);
34         HS6200_crc_init=crc;
35         memcpy(HS6200_tx_addr, addr, addr_len);
36         HS6200_address_length = addr_len;
39 static uint16_t __attribute__((unused)) HS6200_calc_crc(uint8_t* msg, uint8_t len)
41     uint8_t pos;
42     
43         crc = HS6200_crc_init;
44     // pcf + payload
45     for(pos=0; pos < len-1; pos++)
46         crc16_update(msg[pos], 8);
47     // last byte (1 bit only)
48     if(len > 0)
49         crc16_update(msg[pos+1], 1);
50     return crc;
53 static void __attribute__((unused)) HS6200_SendPayload(uint8_t* msg, uint8_t len)
55         static const uint8_t HS6200_scramble[] = { 0x80,0xf5,0x3b,0x0d,0x6d,0x2a,0xf9,0xbc,0x51,0x8e,0x4c,0xfd,0xc1,0x65,0xd0 }; // todo: find all 32 bytes ...
56         uint8_t payload[32];
57         const uint8_t no_ack = 1; // never ask for an ack
58         static uint8_t pid;
59         uint8_t pos = 0;
61         if(len > sizeof(HS6200_scramble))
62                 len = sizeof(HS6200_scramble);
64         // address
65         for(int8_t i=HS6200_address_length-1; i>=0; i--)
66                 payload[pos++] = HS6200_tx_addr[i];
68         // guard bytes
69         payload[pos++] = HS6200_tx_addr[0];
70         payload[pos++] = HS6200_tx_addr[0];
72         // packet control field
73         payload[pos++] = ((len & 0x3f) << 2) | (pid & 0x03);
74         payload[pos] = (no_ack & 0x01) << 7;
75         pid++;
77         // scrambled payload
78         if(len > 0)
79         {
80                 payload[pos++] |= (msg[0] ^ HS6200_scramble[0]) >> 1; 
81                 for(uint8_t i=1; i<len; i++)
82                         payload[pos++] = ((msg[i-1] ^ HS6200_scramble[i-1]) << 7) | ((msg[i] ^ HS6200_scramble[i]) >> 1);
83                 payload[pos] = (msg[len-1] ^ HS6200_scramble[len-1]) << 7; 
84         }
86         // crc
87         if(HS6200_crc)
88         {
89                 uint16_t crc = HS6200_calc_crc(&payload[HS6200_address_length+2], len+2);
90                 uint8_t hcrc = crc >> 8;
91                 uint8_t lcrc = crc & 0xff;
92                 payload[pos++] |= (hcrc >> 1);
93                 payload[pos++] = (hcrc << 7) | (lcrc >> 1);
94                 payload[pos++] = lcrc << 7;
95         }
97         #if 0
98                 debug("E:");
99                 for(uint8_t i=0; i<pos; i++)
100                         debug(" %02X",payload[i]);
101                 debugln("");
102         #endif
104         //CYRF wants LSB first
105         for(uint8_t i=0; i<pos; i++)
106                 payload[i]=bit_reverse(payload[i]);
107         //Send
108         CYRF_WriteRegister(CYRF_01_TX_LENGTH, pos);
109         CYRF_GFSK1M_SendPayload(payload, pos);
112 #endif