/*
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 .
*/
#if defined(WFLYRF_A7105_INO)
#include "iface_a7105.h"
#define WFLYRF_FORCE_ID
//WFLYRF constants & variables
#define WFLYRF_BIND_COUNT 2500
#define WFLYRF_PACKET_SIZE 32
enum{
WFLYRF_BIND,
WFLYRF_DATA,
WFLYRF_PLL_TX,
WFLYRF_RX,
};
static void __attribute__((unused)) WFLYRF_send_bind_packet()
{
//Header
packet[0] = 0x0F; // Bind packet
//ID
packet[1] = rx_tx_addr[3];
packet[2] = rx_tx_addr[2];
packet[3] = rx_tx_addr[1];
//Unknown
packet[4] = 0x00;
packet[5] = 0x01;
//Freq
packet[6] = (hopping_frequency_no<<1)+0x0E;
rf_ch_num = (hopping_frequency_no<<2)+0x2C;
hopping_frequency_no++; // not sure which frequencies are used since the dump only goes from 0x0E to 0x2C and stops...
if(hopping_frequency_no > 0x1A) hopping_frequency_no=0x00;
//Unknown
memset(&packet[7],0x00,25);
//Debug
#if 0
debug("ch=%02X P=",rf_ch_num);
for(uint8_t i=0; i>8;
//RF channel
int8_t prev = rf_ch_num & 0x1F;
rf_ch_num = (pseudo ^ (pseudo >> 7)) & 0x57;
if(rf_ch_num & 0x10)
{
rf_ch_num |= 0x08;
rf_ch_num &= 0x4F;
}
if(rf_ch_num & 0x40)
{
rf_ch_num |= 0x10;
rf_ch_num &= 0x1F;
}
rf_ch_num ^= rx_tx_addr[3] & 0x1F;
if(abs((int8_t)rf_ch_num-prev) <= 9)
{
if(high_bit)
rf_ch_num |= 0x20;
}
else
if(!high_bit)
rf_ch_num |= 0x20;
//Partial ID
packet[3] = rx_tx_addr[3];
packet[4] = rx_tx_addr[2] & 0x1F; //not sure... could be 0x1F down to 0x03
//10 channels
for(uint8_t i = 0; i < 5; i++)
{
uint16_t temp=convert_channel_16b_nolimit(i*2 , 0x0000, 0x0FFF); // need to check channels min/max...
packet[5 + i*3] = temp&0xFF; // low byte
packet[7 + i*3] = (temp>>8)&0x0F; // high byte
temp=convert_channel_16b_nolimit(i*2+1, 0x0000, 0x0FFF); // need to check channels min/max...
packet[6 + i*3] = temp&0xFF; // low byte
packet[7 + i*3] |= (temp>>4)&0xF0; // high byte
}
//Unknown
memset(&packet[20],0x00,12);
//Debug
#if 0
debug("ch=%02X,%02X P=",rf_ch_num,(rf_ch_num<<1)+0x10);
for(uint8_t i=0; i>1; // Looks to be the RX RSSI value direct from A7105
// Read TX RSSI
TX_RSSI=255-A7105_ReadReg(A7105_1D_RSSI_THOLD);
telemetry_counter++; // LQI counter
telemetry_link=1;
if(telemetry_lost)
{
telemetry_lost = 0;
packet_count = 100;
telemetry_counter = 100;
}
}
#endif
#define WFLYRF_PACKET_PERIOD 3600 //3600
#define WFLYRF_BUFFER_TIME 1500 //1500
#define WFLYRF_WRITE_TIME 800 //942
uint16_t ReadWFLYRF()
{
uint16_t start;
#ifdef WFLYRF_HUB_TELEMETRY
uint8_t status;
#endif
#ifndef FORCE_WFLYRF_TUNING
A7105_AdjustLOBaseFreq(1);
#endif
switch(phase)
{
case WFLYRF_BIND:
bind_counter--;
if (bind_counter == 0)
{
BIND_DONE;
A7105_WriteID(MProtocol_id);
rf_ch_num = 0;
phase++; // WFLYRF_DATA
}
WFLYRF_send_bind_packet();
return WFLYRF_PACKET_PERIOD;
case WFLYRF_DATA:
#ifdef MULTI_SYNC
telemetry_set_input_sync(WFLYRF_PACKET_PERIOD);
#endif
//Build data packet
WFLYRF_build_packet();
//Fill the TX buffer without sending
A7105_WriteReg(A7105_03_FIFOI, 0x1F);
A7105_CSN_off;
SPI_Write(A7105_RST_WRPTR);
SPI_Write(A7105_05_FIFO_DATA);
for (uint8_t i = 0; i < WFLYRF_PACKET_SIZE; i++)
SPI_Write(packet[i]);
A7105_CSN_on;
#ifdef WFLYRF_HUB_TELEMETRY
//LQI calculation
packet_count++;
if(packet_count>=100)
{
packet_count=0;
TX_LQI=telemetry_counter;
if(telemetry_counter==0)
telemetry_lost = 1;
telemetry_counter = 0;
}
#endif
phase++; // WFLYRF_PLL_TX
return WFLYRF_BUFFER_TIME;
case WFLYRF_PLL_TX:
#ifdef WFLYRF_HUB_TELEMETRY
//Check RX status
status=A7105_ReadReg(A7105_00_MODE);
//debugln("S:%02X", status);
#endif
//PLL
A7105_Strobe(A7105_PLL);
#ifdef WFLYRF_HUB_TELEMETRY
//Read incoming packet even if bad/not present to not change too much the TX timing, might want to reorg the code...
A7105_ReadData(WFLYRF_PACKET_SIZE);
//Read telemetry
if((status & 0x21)==0)
{ // Packet received and CRC OK
//Debug
#if 1
debug("T:");
for(uint8_t i=0; i