|
| 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 | +#if defined(H36_NRF24L01_INO) |
| 16 | + |
| 17 | +#include "iface_xn297.h" |
| 18 | + |
| 19 | +#define FORCE_H36_ORIGINAL_ID |
| 20 | + |
| 21 | +#define H36_PAYLOAD_SIZE 13 |
| 22 | +#define H36_RF_NUM_CHANNELS 4 |
| 23 | +#define H36_BIND_PACKET_PERIOD 10285 |
| 24 | +#define H36_BIND_COUNT 648 //3sec |
| 25 | + |
| 26 | +enum { |
| 27 | + H36_DATA1=0, |
| 28 | + H36_DATA2, |
| 29 | + H36_DATA3, |
| 30 | + H36_DATA4, |
| 31 | +}; |
| 32 | + |
| 33 | +static void __attribute__((unused)) H36_send_packet() |
| 34 | +{ |
| 35 | + if(IS_BIND_DONE && phase == H36_DATA1) |
| 36 | + { |
| 37 | + hopping_frequency_no++; |
| 38 | + hopping_frequency_no&=3; |
| 39 | + XN297_Hopping(hopping_frequency_no); |
| 40 | + } |
| 41 | + |
| 42 | + packet[0] = 0x2E; // constant? |
| 43 | + memcpy(&packet[2],rx_tx_addr,3); |
| 44 | + if(IS_BIND_IN_PROGRESS) |
| 45 | + {//Bind |
| 46 | + memcpy(&packet[5],hopping_frequency,4); |
| 47 | + memset(&packet[9], 0x00, 3); |
| 48 | + packet[12] = 0xED; // constant? |
| 49 | + bind_counter--; |
| 50 | + if(bind_counter == 0) |
| 51 | + BIND_DONE; |
| 52 | + } |
| 53 | + else |
| 54 | + {//Normal |
| 55 | + packet[5] = convert_channel_8b(THROTTLE); |
| 56 | + packet[6] = convert_channel_8b(RUDDER); |
| 57 | + packet[7] = convert_channel_8b(ELEVATOR); |
| 58 | + packet[8] = convert_channel_8b(AILERON); |
| 59 | + packet[9] = GET_FLAG(CH6_SW, 0x02) //Headless |
| 60 | + |GET_FLAG(CH7_SW, 0x04); //RTH(temporary) |
| 61 | + packet[10] = 0x20; //Trim A centered(0x20) |
| 62 | + packet[11] = CH5_SW?0x60:0x20; //Flip(0x40)|Trim E centered(0x20) |
| 63 | + packet[12] = 0xA0; //High(0x80)/Low(0x40) rates|Trim R centered(0x20)? |
| 64 | + } |
| 65 | + //crc |
| 66 | + packet[1]=0xAA; |
| 67 | + for(uint8_t i=5;i<12;i++) |
| 68 | + packet[1] ^= packet[i]; |
| 69 | + //Send |
| 70 | + XN297_SetPower(); |
| 71 | + XN297_SetTxRxMode(TX_EN); |
| 72 | + XN297_WritePayload(packet, H36_PAYLOAD_SIZE); |
| 73 | + #ifdef DEBUG_SERIAL |
| 74 | + debug("H%d P",hopping_frequency_no); |
| 75 | + for(uint8_t i=0; i < H36_PAYLOAD_SIZE; i++) |
| 76 | + debug(" %02X", packet[i]); |
| 77 | + debugln(); |
| 78 | + #endif |
| 79 | +} |
| 80 | + |
| 81 | +static void __attribute__((unused)) H36_initialize_txid() |
| 82 | +{ |
| 83 | + rx_tx_addr[0] = rx_tx_addr[3]; |
| 84 | + calc_fh_channels(4); |
| 85 | + #ifdef FORCE_H36_ORIGINAL_ID |
| 86 | + memcpy(rx_tx_addr,(uint8_t *)"\x00\x11\x00",3); |
| 87 | + memcpy(hopping_frequency,(uint8_t *)"\x36\x3A\x31\x2B",4); //54, 58, 49, 43 |
| 88 | + #endif |
| 89 | +} |
| 90 | + |
| 91 | +static void __attribute__((unused)) H36_RF_init() |
| 92 | +{ |
| 93 | + XN297_Configure(XN297_CRCEN, XN297_SCRAMBLED, XN297_1M); |
| 94 | + XN297_SetTXAddr((uint8_t*)"\xCC\x6C\x47\x90\x53", 5); |
| 95 | + XN297_RFChannel(50); //Bind channel |
| 96 | +} |
| 97 | + |
| 98 | +uint16_t H36_callback() |
| 99 | +{ |
| 100 | + H36_send_packet(); |
| 101 | + switch(phase) |
| 102 | + { |
| 103 | + case H36_DATA1: |
| 104 | + phase++; |
| 105 | + return 1830; |
| 106 | + case H36_DATA2: |
| 107 | + case H36_DATA3: |
| 108 | + phase++; |
| 109 | + return 3085; |
| 110 | + default://DATA4 |
| 111 | + #ifdef MULTI_SYNC |
| 112 | + telemetry_set_input_sync(18500); |
| 113 | + #endif |
| 114 | + phase = H36_DATA1; |
| 115 | + break; |
| 116 | + } |
| 117 | + return 10500; |
| 118 | +} |
| 119 | + |
| 120 | +void H36_init() |
| 121 | +{ |
| 122 | + BIND_IN_PROGRESS; // Autobind protocol |
| 123 | + H36_initialize_txid(); |
| 124 | + H36_RF_init(); |
| 125 | + phase = H36_DATA1; |
| 126 | + hopping_frequency_no = 0; |
| 127 | + bind_counter = H36_BIND_COUNT; |
| 128 | +} |
| 129 | +#endif |
0 commit comments