|
| 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 | + |
| 16 | +#if defined(KYOSHO2_NRF24L01_INO) |
| 17 | + |
| 18 | +#include "iface_nrf24l01.h" |
| 19 | + |
| 20 | +#define KYOSHO2_PACKET_PERIOD 1120 // 1600 for bind, let's see |
| 21 | +#define KYOSHO2_BIND_COUNT 2000 // about 3sec |
| 22 | +#define KYOSHO2_BIND_CHANNEL 0x50 |
| 23 | +#define KYOSHO2_PAYLOAD_SIZE 28 |
| 24 | +#define KYOSHO2_RF_CHANNELS 15 |
| 25 | +#define KYOSHO2_START_RF_CHANNEL 0x13 // No idea where it comes from... ID or unknown bytes during the bind? |
| 26 | +#define KYOSHO2_NUM_CHANNEL 10 // Only 4 on the dumps but there is space for 10 channels in the payload... |
| 27 | + |
| 28 | +#define FORCE_KYOSHO2_ID |
| 29 | + |
| 30 | +bool KYOSHO2_resend; |
| 31 | +// |
| 32 | +static void __attribute__((unused)) KYOSHO2_send_packet() |
| 33 | +{ |
| 34 | + if(KYOSHO2_resend == true) |
| 35 | + { |
| 36 | + NRF24L01_Strobe(NRF24L01_E3_REUSE_TX_PL); |
| 37 | + if(IS_BIND_DONE) |
| 38 | + KYOSHO2_resend = false; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + memset(packet,0x00,KYOSHO2_PAYLOAD_SIZE); |
| 43 | + |
| 44 | + if(IS_BIND_IN_PROGRESS) |
| 45 | + { |
| 46 | + memcpy(packet, (uint8_t*)"\x01\x02\x05\x08\x1A\x2B\x3C\x4D", 8); // unknown bytes, parameters on how to build the rf channels? |
| 47 | + memcpy(&packet[8], rx_tx_addr, 4); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + memcpy(packet, rx_tx_addr, 4); |
| 52 | + //Hopp |
| 53 | + packet[6] = hopping_frequency_no + KYOSHO2_START_RF_CHANNEL; |
| 54 | + packet[7] = hopping_frequency[hopping_frequency_no]; |
| 55 | + NRF24L01_WriteReg(NRF24L01_05_RF_CH, packet[6+(rf_ch_num&0x01)]); |
| 56 | + rf_ch_num++; |
| 57 | + //Channels |
| 58 | + uint16_t temp; |
| 59 | + for (uint8_t i = 0; i< KYOSHO2_NUM_CHANNEL; i++) |
| 60 | + { |
| 61 | + temp=convert_channel_16b_limit(i,0,0x3FF); |
| 62 | + packet[8+i*2] = temp >> 8; |
| 63 | + packet[9+i*2] = temp; |
| 64 | + } |
| 65 | + } |
| 66 | + //Send |
| 67 | + NRF24L01_WriteReg(NRF24L01_07_STATUS, (_BV(NRF24L01_07_TX_DS) | _BV(NRF24L01_07_MAX_RT))); // Reset flags |
| 68 | + NRF24L01_FlushTx(); |
| 69 | + NRF24L01_WritePayload(packet,KYOSHO2_PAYLOAD_SIZE); |
| 70 | + NRF24L01_SetPower(); |
| 71 | + KYOSHO2_resend = true; |
| 72 | + |
| 73 | + #if 0 |
| 74 | + for(uint8_t i=0;i<KYOSHO2_PAYLOAD_SIZE;i++) |
| 75 | + debug("%02X ", packet[i]); |
| 76 | + debugln(""); |
| 77 | + #endif |
| 78 | +} |
| 79 | + |
| 80 | +static void __attribute__((unused)) KYOSHO2_RF_init() |
| 81 | +{ |
| 82 | + NRF24L01_Initialize(); |
| 83 | + NRF24L01_WriteReg(NRF24L01_05_RF_CH, KYOSHO2_BIND_CHANNEL); |
| 84 | + NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR, (uint8_t*)"\x69\x53\x10\xAC\xEF", 5); |
| 85 | +} |
| 86 | + |
| 87 | +static void __attribute__((unused)) KYOSHO2_initialize_tx_id() |
| 88 | +{ |
| 89 | + hopping_frequency_no = rx_tx_addr[3]%KYOSHO2_RF_CHANNELS; |
| 90 | + hopping_frequency[0] = 0x4A; |
| 91 | + #ifdef FORCE_KYOSHO2_ID |
| 92 | + memcpy(rx_tx_addr, (uint8_t*)"\x0A\xBD\x31\xDF", 4); |
| 93 | + hopping_frequency[0] = 0x4A; // No idea where it comes from... ID or unknown bytes during the bind? |
| 94 | + #endif |
| 95 | + for(uint8_t i=1;i<KYOSHO2_RF_CHANNELS;i++) |
| 96 | + { |
| 97 | + if(hopping_frequency[i-1]+5 < 0x50) |
| 98 | + hopping_frequency[i] = hopping_frequency[i-1]+5; |
| 99 | + else |
| 100 | + hopping_frequency[i] = hopping_frequency[i-1]-0x21; |
| 101 | + } |
| 102 | + #if 0 |
| 103 | + for(uint8_t i=0;i<KYOSHO2_RF_CHANNELS;i++) |
| 104 | + debugln("1:%02X, 2: %02X", i + KYOSHO2_START_RF_CHANNEL, hopping_frequency[i]); |
| 105 | + debugln("Selected 1:%02X, 2: %02X", hopping_frequency_no + KYOSHO2_START_RF_CHANNEL, hopping_frequency[hopping_frequency_no]); |
| 106 | + #endif |
| 107 | +} |
| 108 | + |
| 109 | +uint16_t KYOSHO2_callback() |
| 110 | +{ |
| 111 | + #ifdef MULTI_SYNC |
| 112 | + telemetry_set_input_sync(KYOSHO2_PACKET_PERIOD); |
| 113 | + #endif |
| 114 | + if(bind_counter) |
| 115 | + if(--bind_counter==0) |
| 116 | + { |
| 117 | + BIND_DONE; |
| 118 | + KYOSHO2_resend = false; |
| 119 | + } |
| 120 | + KYOSHO2_send_packet(); |
| 121 | + return KYOSHO2_PACKET_PERIOD; |
| 122 | +} |
| 123 | + |
| 124 | +void KYOSHO2_init() |
| 125 | +{ |
| 126 | + KYOSHO2_initialize_tx_id(); |
| 127 | + KYOSHO2_RF_init(); |
| 128 | + rf_ch_num = 0; |
| 129 | + |
| 130 | + if(IS_BIND_IN_PROGRESS) |
| 131 | + bind_counter = KYOSHO2_BIND_COUNT; |
| 132 | + else |
| 133 | + bind_counter = 0; |
| 134 | + KYOSHO2_resend = false; |
| 135 | +} |
| 136 | + |
| 137 | +#endif |
0 commit comments