|
| 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. |
| 6 | +
|
| 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. |
| 11 | +
|
| 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 | +// Compatible with ARES 6HPA transmitter |
| 16 | + |
| 17 | +#if defined(ARES_CC2500_INO) |
| 18 | + |
| 19 | +#include "iface_cc2500.h" |
| 20 | + |
| 21 | +//#define ARES_FORCE_ID |
| 22 | + |
| 23 | +#define ARES_COARSE 0 |
| 24 | + |
| 25 | +#define ARES_PACKET_LEN 17 |
| 26 | +#define ARES_NUM_FREQUENCIES 60 |
| 27 | + |
| 28 | +enum { |
| 29 | + ARES_START = 0x00, |
| 30 | + ARES_CALIB = 0x01, |
| 31 | + ARES_PREP = 0x02, |
| 32 | + ARES_DATA = 0x03, |
| 33 | +}; |
| 34 | + |
| 35 | +// CC2500 register init values captured from the ARES 6HPA transmitter |
| 36 | +const PROGMEM uint8_t ARES_init_values[] = { |
| 37 | + /* 00 */ 0x06, 0x2E, 0x2E, 0x07, 0x5A, 0x60, 0x30, 0x04, |
| 38 | + /* 08 */ 0x05, 0x00, 0x00, 0x06, 0x00, 0x5C, 0xB1, 0x3B + ARES_COARSE, |
| 39 | + /* 10 */ 0x6A, 0xF8, 0x03, 0x23, 0x7A, 0x44, 0x07, 0x30, |
| 40 | + /* 18 */ 0x18, 0x16, 0x6C, 0x43, 0x40, 0x91, 0x87, 0x6B, |
| 41 | + /* 20 */ 0xF8, 0x56, 0x10, 0xA9, 0x0A, 0x00, 0x11 |
| 42 | +}; |
| 43 | + |
| 44 | +// Fixed hopping sequence captured from the ARES 6HPA transmitter. |
| 45 | +// This is a permutation of 60 channel values spread across the band. |
| 46 | +static const PROGMEM uint8_t ARES_hop[] = { |
| 47 | + 0xB0, 0x6F, 0x1D, 0xB4, 0x74, 0x20, 0xB8, 0xD8, |
| 48 | + 0x24, 0xBC, 0xDC, 0x28, 0x48, 0xE0, 0x2C, 0x4C, |
| 49 | + 0xE4, 0x90, 0x50, 0xE8, 0x94, 0x54, 0xEC, 0x00, |
| 50 | + 0x98, 0x58, 0x04, 0x9B, 0x5C, 0x08, 0xA0, 0xC0, |
| 51 | + 0x0C, 0xA4, 0xC3, 0x10, 0x30, 0xC6, 0x14, 0x34, |
| 52 | + 0xCC, 0x78, 0x38, 0xD0, 0x7C, 0x3C, 0xD4, 0x80, |
| 53 | + 0x40, 0x60, 0x84, 0x44, 0x64, 0x88, 0xA8, 0x68, |
| 54 | + 0x8C, 0xAC, 0x6C, 0x18 |
| 55 | +}; |
| 56 | + |
| 57 | +static void __attribute__((unused)) ARES_CC2500_init() |
| 58 | +{ |
| 59 | + CC2500_Strobe(CC2500_SRES); |
| 60 | + delayMilliseconds(1); |
| 61 | + CC2500_Strobe(CC2500_SIDLE); |
| 62 | + |
| 63 | + for (uint8_t i = 0; i < 39; ++i) |
| 64 | + CC2500_WriteReg(i, pgm_read_byte_near(&ARES_init_values[i])); |
| 65 | + |
| 66 | + CC2500_WriteReg(CC2500_0C_FSCTRL0, option); |
| 67 | + prev_option = option; |
| 68 | + |
| 69 | + // Write PATABLE to max power (0xFF for all 8 entries) as captured |
| 70 | + for (uint8_t i = 0; i < 8; i++) |
| 71 | + CC2500_WriteReg(CC2500_3E_PATABLE, 0xFF); |
| 72 | + |
| 73 | + CC2500_SetTxRxMode(TX_EN); |
| 74 | + CC2500_SetPower(); |
| 75 | +} |
| 76 | + |
| 77 | +// Load hopping table |
| 78 | +static void __attribute__((unused)) ARES_RF_channels() |
| 79 | +{ |
| 80 | + for (uint8_t i = 0; i < ARES_NUM_FREQUENCIES; i++) |
| 81 | + hopping_frequency[i] = pgm_read_byte_near(&ARES_hop[i]); |
| 82 | +} |
| 83 | + |
| 84 | +static void __attribute__((unused)) ARES_tune_chan() |
| 85 | +{ |
| 86 | + CC2500_Strobe(CC2500_SIDLE); |
| 87 | + CC2500_WriteReg(CC2500_0A_CHANNR, hopping_frequency[hopping_frequency_no]); |
| 88 | + CC2500_Strobe(CC2500_SFTX); |
| 89 | + CC2500_Strobe(CC2500_SCAL); |
| 90 | +} |
| 91 | + |
| 92 | +static void __attribute__((unused)) ARES_change_chan_fast() |
| 93 | +{ |
| 94 | + CC2500_Strobe(CC2500_SIDLE); |
| 95 | + CC2500_WriteReg(CC2500_0A_CHANNR, hopping_frequency[hopping_frequency_no]); |
| 96 | + CC2500_WriteReg(CC2500_25_FSCAL1, calData[hopping_frequency_no]); |
| 97 | +} |
| 98 | + |
| 99 | +// Advance the hop counter: cycles through 0-58 with step, inserting 59 when wrapping through 0 |
| 100 | +static uint8_t __attribute__((unused)) ARES_next_counter(uint8_t current, uint8_t step) |
| 101 | +{ |
| 102 | + if (current == 59) |
| 103 | + return 0; |
| 104 | + uint8_t next = (current + step) % 59; |
| 105 | + if (next == 0) |
| 106 | + return 59; |
| 107 | + return next; |
| 108 | +} |
| 109 | + |
| 110 | +static void __attribute__((unused)) ARES_build_packet() |
| 111 | +{ |
| 112 | + // Length byte: 16 data bytes follow |
| 113 | + packet[0] = 0x10; |
| 114 | + |
| 115 | + // TX ID |
| 116 | + packet[1] = rx_tx_addr[1]; |
| 117 | + packet[2] = rx_tx_addr[2]; |
| 118 | + packet[3] = rx_tx_addr[3]; |
| 119 | + |
| 120 | + // 6 channels encoded as interleaved 12-bit values in bytes 4-12 |
| 121 | + uint16_t ch[6]; |
| 122 | + for (uint8_t i = 0; i < 6; i++) |
| 123 | + ch[i] = convert_channel_16b_nolimit(i, 1820, 3300, false); |
| 124 | + |
| 125 | + packet[4] = ch[0] >> 4; |
| 126 | + packet[5] = ((ch[0] & 0x0F) << 4) | (ch[1] & 0x0F); |
| 127 | + packet[6] = ch[1] >> 4; |
| 128 | + packet[7] = ch[2] >> 4; |
| 129 | + packet[8] = ((ch[2] & 0x0F) << 4) | (ch[3] & 0x0F); |
| 130 | + packet[9] = ch[3] >> 4; |
| 131 | + packet[10] = ch[4] >> 4; |
| 132 | + packet[11] = ((ch[4] & 0x0F) << 4) | (ch[5] & 0x0F); |
| 133 | + packet[12] = ch[5] >> 4; |
| 134 | + |
| 135 | + // Byte 16: counter step size (stored in crc, set to 1-58 in ARES_init) |
| 136 | + uint8_t step = crc; |
| 137 | + |
| 138 | + // Bytes 13-15: running counter with rotating bit 7 frame indicator |
| 139 | + // The counter cycles 0-58 with a step, inserting 59 before wrapping to 0 |
| 140 | + // Each group of 3 packets has 3 consecutive counter values |
| 141 | + // packet_count holds the current counter value |
| 142 | + uint8_t c0 = packet_count; |
| 143 | + uint8_t c1 = ARES_next_counter(c0, step); |
| 144 | + uint8_t c2 = ARES_next_counter(c1, step); |
| 145 | + |
| 146 | + // Frame indicator: each data frame is sent 3 times |
| 147 | + // bind_phase tracks position 0/1/2 within the group of 3 |
| 148 | + packet[13] = c0; |
| 149 | + packet[14] = c1; |
| 150 | + packet[15] = c2; |
| 151 | + packet[16] = step; |
| 152 | + |
| 153 | + // Set the rotating frame bit (bit 7) on one of bytes 13-15 |
| 154 | + switch (bind_phase) |
| 155 | + { |
| 156 | + case 0: |
| 157 | + packet[13] |= 0x80; |
| 158 | + break; |
| 159 | + case 1: |
| 160 | + packet[14] |= 0x80; |
| 161 | + break; |
| 162 | + case 2: |
| 163 | + packet[15] |= 0x80; |
| 164 | + break; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +static void __attribute__((unused)) ARES_send_packet() |
| 169 | +{ |
| 170 | + ARES_change_chan_fast(); |
| 171 | + CC2500_SetPower(); |
| 172 | + CC2500_WriteData(packet, ARES_PACKET_LEN); |
| 173 | +} |
| 174 | + |
| 175 | +#define ARES_PACKET_PERIOD 6670 // 6.67ms between packets |
| 176 | +#define ARES_PREP_TIMING 2000 |
| 177 | + |
| 178 | +uint16_t ARES_callback() |
| 179 | +{ |
| 180 | + switch(phase) |
| 181 | + { |
| 182 | + case ARES_START: |
| 183 | + ARES_CC2500_init(); |
| 184 | + hopping_frequency_no = 0; |
| 185 | + bind_phase = 0; |
| 186 | + ARES_tune_chan(); |
| 187 | + phase = ARES_CALIB; |
| 188 | + return ARES_PREP_TIMING; |
| 189 | + case ARES_CALIB: |
| 190 | + calData[hopping_frequency_no] = CC2500_ReadReg(CC2500_25_FSCAL1); |
| 191 | + hopping_frequency_no++; |
| 192 | + if (hopping_frequency_no < ARES_NUM_FREQUENCIES) |
| 193 | + ARES_tune_chan(); |
| 194 | + else |
| 195 | + { |
| 196 | + hopping_frequency_no = 0; |
| 197 | + phase = ARES_PREP; |
| 198 | + } |
| 199 | + return ARES_PREP_TIMING; |
| 200 | + case ARES_PREP: |
| 201 | + if (prev_option != option) |
| 202 | + { |
| 203 | + phase = ARES_START; |
| 204 | + return ARES_PREP_TIMING; |
| 205 | + } |
| 206 | + #ifdef MULTI_SYNC |
| 207 | + telemetry_set_input_sync(ARES_PACKET_PERIOD); |
| 208 | + #endif |
| 209 | + ARES_build_packet(); |
| 210 | + phase = ARES_DATA; |
| 211 | + // Fall through |
| 212 | + case ARES_DATA: |
| 213 | + ARES_send_packet(); |
| 214 | + hopping_frequency_no++; |
| 215 | + if (hopping_frequency_no >= ARES_NUM_FREQUENCIES) |
| 216 | + hopping_frequency_no = 0; |
| 217 | + bind_phase++; |
| 218 | + if (bind_phase >= 3) |
| 219 | + { |
| 220 | + bind_phase = 0; |
| 221 | + // Advance counter to start of next group |
| 222 | + uint8_t step = crc; |
| 223 | + packet_count = ARES_next_counter(packet_count, step); |
| 224 | + packet_count = ARES_next_counter(packet_count, step); |
| 225 | + packet_count = ARES_next_counter(packet_count, step); |
| 226 | + } |
| 227 | + phase = ARES_PREP; |
| 228 | + return ARES_PACKET_PERIOD; |
| 229 | + } |
| 230 | + return 0; |
| 231 | +} |
| 232 | + |
| 233 | +void ARES_init() |
| 234 | +{ |
| 235 | + BIND_DONE; // Autobind protocol - no TX-initiated bind phase |
| 236 | + ARES_RF_channels(); |
| 237 | + |
| 238 | + // rx_tx_addr[1] and [2] are already set from MProtocol_id by the framework |
| 239 | + // RX_num (0-63) in byte 3 provides model match |
| 240 | + rx_tx_addr[3] = RX_num; |
| 241 | + |
| 242 | + // Counter step and start from capture |
| 243 | + crc = 23; |
| 244 | + packet_count = 35; |
| 245 | + |
| 246 | + #ifdef ARES_FORCE_ID |
| 247 | + rx_tx_addr[1] = 0xDC; |
| 248 | + rx_tx_addr[2] = 0xCC; |
| 249 | + rx_tx_addr[3] = 0x00; |
| 250 | + #endif |
| 251 | + phase = ARES_START; |
| 252 | +} |
| 253 | + |
| 254 | +#endif |
0 commit comments