205 lines
5.6 KiB
Arduino
Raw Permalink Normal View History

2019-11-28 20:02:59 +01:00
/*
This project is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Multiprotocol is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Multiprotocol. If not, see <http://www.gnu.org/licenses/>.
*/
2019-11-29 02:06:58 +01:00
#if defined(BAYANG_RX_NRF24L01_INO)
2019-11-28 20:02:59 +01:00
#include "iface_xn297.h"
2019-11-28 20:02:59 +01:00
2019-11-29 02:06:58 +01:00
#define BAYANG_RX_PACKET_SIZE 15
#define BAYANG_RX_RF_NUM_CHANNELS 4
#define BAYANG_RX_RF_BIND_CHANNEL 0
#define BAYANG_RX_ADDRESS_LENGTH 5
enum {
BAYANG_RX_BIND = 0,
BAYANG_RX_DATA
};
static void __attribute__((unused)) Bayang_Rx_RF_init()
2019-11-28 20:02:59 +01:00
{
2019-11-29 02:06:58 +01:00
const uint8_t bind_address[BAYANG_RX_ADDRESS_LENGTH] = { 0,0,0,0,0 };
XN297_Configure(XN297_CRCEN, XN297_SCRAMBLED, XN297_1M);
2019-11-29 02:06:58 +01:00
XN297_SetTXAddr(bind_address, BAYANG_RX_ADDRESS_LENGTH);
XN297_SetRXAddr(bind_address, BAYANG_RX_PACKET_SIZE);
XN297_RFChannel(BAYANG_RX_RF_BIND_CHANNEL);
XN297_SetTxRxMode(TXRX_OFF);
XN297_SetTxRxMode(RX_EN);
2019-11-29 02:06:58 +01:00
}
static uint8_t __attribute__((unused)) Bayang_Rx_check_validity() {
2019-11-29 11:57:06 +01:00
uint8_t sum = packet[0];
for (uint8_t i = 1; i < BAYANG_RX_PACKET_SIZE - 1; i++)
2019-11-29 02:06:58 +01:00
sum += packet[i];
return sum == packet[14];
}
static void __attribute__((unused)) Bayang_Rx_build_telemetry_packet()
{
uint32_t bits = 0;
uint8_t bitsavailable = 0;
uint8_t idx = 0;
packet_in[idx++] = RX_LQI;
2019-11-30 12:11:08 +01:00
packet_in[idx++] = RX_LQI>>1; // no RSSI: 125..0
packet_in[idx++] = 0; // start channel
packet_in[idx++] = 10; // number of channels in packet
2019-11-29 02:06:58 +01:00
// convert & pack channels
2019-11-29 02:53:19 +01:00
for (uint8_t i = 0; i < packet_in[3]; i++) {
uint32_t val = CHANNEL_MIN_100;
2019-11-29 02:06:58 +01:00
if (i < 4) {
// AETR
//val = (((packet[4 + i * 2] & ~0x7C) << 8) | packet[5 + i * 2]) << 1;
val=packet[4 + i * 2]&0x03;
val=(val<<8)+packet[5 + i * 2];
val=((val+128)<<3)/5;
} else if (i == 4 || i == 5) {
val=packet[i==4?1:13];
val=((val+32)<<5)/5; // extra analog channel
} else if (((i == 6) && (packet[2] & 0x08)) || // flip
((i == 7) && (packet[2] & 0x01)) || // rth
((i == 8) && (packet[2] & 0x20)) || // picture
((i == 9) && (packet[2] & 0x10))) { // video
2019-11-29 02:06:58 +01:00
// set channel to 100% if feature is enabled
val = CHANNEL_MAX_100;
2019-11-29 02:06:58 +01:00
}
bits |= val << bitsavailable;
bitsavailable += 11;
while (bitsavailable >= 8) {
packet_in[idx++] = bits & 0xff;
bits >>= 8;
bitsavailable -= 8;
}
}
2019-11-28 20:02:59 +01:00
}
2021-02-09 18:23:33 +01:00
void BAYANG_RX_init()
2019-11-28 20:02:59 +01:00
{
2019-11-29 02:06:58 +01:00
uint8_t i;
Bayang_Rx_RF_init();
2019-11-29 02:06:58 +01:00
hopping_frequency_no = 0;
rx_data_started = false;
2019-11-30 12:11:08 +01:00
rx_data_received = false;
2019-11-29 02:06:58 +01:00
if (IS_BIND_IN_PROGRESS) {
phase = BAYANG_RX_BIND;
}
else {
uint16_t temp = BAYANG_RX_EEPROM_OFFSET;
for (i = 0; i < 5; i++)
rx_tx_addr[i] = eeprom_read_byte((EE_ADDR)temp++);
for (i = 0; i < BAYANG_RX_RF_NUM_CHANNELS; i++)
hopping_frequency[i] = eeprom_read_byte((EE_ADDR)temp++);
//XN297_HoppingCalib(BAYANG_RX_RF_NUM_CHANNELS);
2019-11-29 02:06:58 +01:00
XN297_SetTXAddr(rx_tx_addr, BAYANG_RX_ADDRESS_LENGTH);
XN297_SetRXAddr(rx_tx_addr, BAYANG_RX_PACKET_SIZE);
2019-11-29 02:06:58 +01:00
phase = BAYANG_RX_DATA;
}
2019-11-28 20:02:59 +01:00
}
2021-02-09 18:23:33 +01:00
uint16_t BAYANG_RX_callback()
2019-11-29 02:06:58 +01:00
{
2019-11-29 13:36:36 +01:00
uint8_t i;
2019-11-29 02:06:58 +01:00
static int8_t read_retry;
2019-11-28 20:02:59 +01:00
switch (phase)
{
case BAYANG_RX_BIND:
if(IS_BIND_DONE)
{
BAYANG_RX_init(); // Abort bind
break;
}
if ( XN297_IsRX() )
{
debugln("RX");
// data received from TX
if (XN297_ReadPayload(packet, BAYANG_RX_PACKET_SIZE) && ( packet[0] == 0xA4 || packet[0] == 0xA2 ) && Bayang_Rx_check_validity())
{
// store tx info into eeprom
uint16_t temp = BAYANG_RX_EEPROM_OFFSET;
for (i = 0; i < 5; i++) {
rx_tx_addr[i] = packet[i + 1];
eeprom_write_byte((EE_ADDR)temp++, rx_tx_addr[i]);
}
for (i = 0; i < 4; i++) {
hopping_frequency[i] = packet[i + 6];
eeprom_write_byte((EE_ADDR)temp++, hopping_frequency[i]);
}
//XN297_HoppingCalib(BAYANG_RX_RF_NUM_CHANNELS);
XN297_SetTXAddr(rx_tx_addr, BAYANG_RX_ADDRESS_LENGTH);
XN297_SetRXAddr(rx_tx_addr, BAYANG_RX_PACKET_SIZE);
BIND_DONE;
phase = BAYANG_RX_DATA;
2019-11-29 02:06:58 +01:00
}
XN297_SetTxRxMode(RX_EN);
2019-11-29 02:06:58 +01:00
}
break;
case BAYANG_RX_DATA:
if ( XN297_IsRX() ) {
if (XN297_ReadPayload(packet, BAYANG_RX_PACKET_SIZE) && packet[0] == 0xA5 && Bayang_Rx_check_validity()) {
if ((telemetry_link & 0x7F) == 0) {
Bayang_Rx_build_telemetry_packet();
telemetry_link = 1;
#ifdef SEND_CPPM
if(sub_protocol>0)
telemetry_link |= 0x80; // Disable telemetry output
#endif
}
rx_data_started = true;
rx_data_received = true;
read_retry = 8;
pps_counter++;
2019-11-29 02:06:58 +01:00
}
}
// packets per second
if (millis() - pps_timer >= 1000) {
pps_timer = millis();
debugln("%d pps", pps_counter);
RX_LQI = pps_counter >> 1;
pps_counter = 0;
}
// frequency hopping
if (read_retry++ >= 8) {
hopping_frequency_no++;
if (hopping_frequency_no >= BAYANG_RX_RF_NUM_CHANNELS)
hopping_frequency_no = 0;
XN297_Hopping(hopping_frequency_no);
XN297_SetTxRxMode(RX_EN);
if (rx_data_started)
{
if(rx_data_received)
{ // In sync
rx_data_received = false;
read_retry = 5;
return 1500;
}
else
{ // packet lost
read_retry = 0;
if(RX_LQI==0) // communication lost
rx_data_started=false;
}
2019-11-30 12:11:08 +01:00
}
else
read_retry = -16; // retry longer until first packet is caught
2019-11-30 12:11:08 +01:00
}
return 250;
2019-11-29 02:06:58 +01:00
}
return 1000;
}
#endif