Multi 5-in-1 initial support

This commit is contained in:
Pascal Langer
2020-06-06 01:57:52 +02:00
parent 0316c9eea9
commit 90b287f1f4
9 changed files with 162 additions and 161 deletions

View File

@@ -1,96 +1,79 @@
#if defined(FRSKYR9_SX1276_INO)
#include "iface_sx1276.h"
#define FREQ_MAP_SIZE 29
#define FREQ_MAP_SIZE 29
// TODO the channel spacing is equal, consider calculating the new channel instead of using lookup tables (first_chan + index * step)
uint8_t FrSkyR9_step = 1;
uint32_t FrSkyR9_freq_map[FREQ_MAP_SIZE];
static uint32_t FrSkyR9_freq_map_915[FREQ_MAP_SIZE] =
static void __attribute__((unused)) FrSkyR9_build_packet()
{
914472960,
914972672,
915472384,
915972096,
916471808,
916971520,
917471232,
917970944,
918470656,
918970368,
919470080,
919969792,
920469504,
920969216,
921468928,
921968640,
922468352,
922968064,
923467776,
923967488,
924467200,
924966912,
925466624,
925966336,
926466048,
926965760,
927465472,
//Header
packet[0] = 0x3C; // unknown but constant
// last two determined by FrSkyR9_step
0,
0
};
//ID
packet[1] = rx_tx_addr[3];
packet[2] = rx_tx_addr[2];
static uint32_t FrSkyR9_freq_map_868[FREQ_MAP_SIZE] =
{
859504640,
860004352,
860504064,
861003776,
861503488,
862003200,
862502912,
863002624,
863502336,
864002048,
864501760,
865001472,
865501184,
866000896,
866500608,
867000320,
867500032,
867999744,
868499456,
868999168,
869498880,
869998592,
870498304,
870998016,
871497728,
871997440,
872497152,
//Hopping
packet[3] = hopping_frequency_no; // current channel index
packet[4] = FrSkyR9_step; // step size and last 2 channels start index
// last two determined by FrSkyR9_step
0,
0
};
//RX number
packet[5] = RX_num; // receiver number from OpenTX
static uint8_t FrSkyR9_step = 1;
static uint32_t* FrSkyR9_freq_map = FrSkyR9_freq_map_915;
// Set packet[6]=failsafe, packet[7]=0?? and packet[8..19]=channels data
FrSkyX_channels(6);
//Bind
if(IS_BIND_IN_PROGRESS)
packet[6] = 0x41;
//SPort
packet[20] = 0x08; //FrSkyX_TX_Seq=8 at startup
packet[21] = 0x00; // length?
packet[22] = 0x00; // data1?
packet[23] = 0x00; // data2?
//CRC
uint16_t crc = FrSkyX_crc(packet, 24);
packet[24] = crc; // low byte
packet[25] = crc >> 8; // high byte
}
uint16_t initFrSkyR9()
{
set_rx_tx_addr(MProtocol_id_master);
if(sub_protocol & 0x01)
FrSkyR9_freq_map = FrSkyR9_freq_map_868;
else
FrSkyR9_freq_map = FrSkyR9_freq_map_915;
//FrSkyR9_step
FrSkyR9_step = 1 + (random(0xfefefefe) % 24);
FrSkyR9_freq_map[27] = FrSkyR9_freq_map[FrSkyR9_step];
FrSkyR9_freq_map[28] = FrSkyR9_freq_map[FrSkyR9_step+1];
debugln("Step=%d", FrSkyR9_step);
//Frequency table
uint32_t start_freq=914472960; //915
if(sub_protocol & 0x01)
start_freq=859504640; //868
for(uint8_t i=0;i<FREQ_MAP_SIZE-2;i++)
{
FrSkyR9_freq_map[i]=start_freq;
debugln("F%d=%lu", i, FrSkyR9_freq_map[i]);
start_freq+=0x7A000;
}
// Last two frequencies determined by FrSkyR9_step
FrSkyR9_freq_map[FREQ_MAP_SIZE-2] = FrSkyR9_freq_map[FrSkyR9_step];
debugln("F%d=%lu", FREQ_MAP_SIZE-2, FrSkyR9_freq_map[FREQ_MAP_SIZE-2]);
FrSkyR9_freq_map[FREQ_MAP_SIZE-1] = FrSkyR9_freq_map[FrSkyR9_step+1];
debugln("F%d=%lu", FREQ_MAP_SIZE-1, FrSkyR9_freq_map[FREQ_MAP_SIZE-1]);
hopping_frequency_no = 0;
//Set FrSkyFormat
if((sub_protocol & 0x02) == 0)
FrSkyFormat=0; // 16 channels
else
FrSkyFormat=1; // 8 channels
debugln("%dCH", FrSkyFormat&1 ? 8:16);
//SX1276 Init
SX1276_SetMode(true, false, SX1276_OPMODE_SLEEP);
SX1276_SetMode(true, false, SX1276_OPMODE_STDBY);
@@ -106,17 +89,16 @@ uint16_t initFrSkyR9()
SX1276_SetPreambleLength(9);
SX1276_SetDetectionThreshold(SX1276_MODEM_DETECTION_THRESHOLD_SF6);
SX1276_SetLna(1, true);
SX1276_SetHopPeriod(0); // 0 = disabled, we hope frequencies manually
SX1276_SetHopPeriod(0); // 0 = disabled, we hop frequencies manually
SX1276_SetPaDac(true);
SX1276_SetTxRxMode(TX_EN); // Set RF switch to TX
hopping_frequency_no = 0;
// TODO this can probably be shorter
return 20000; // start calling FrSkyR9_callback in 20 milliseconds
return 20000; // start calling FrSkyR9_callback in 20 milliseconds
}
uint16_t FrSkyR9_callback()
{
//Force standby
SX1276_SetMode(true, false, SX1276_OPMODE_STDBY);
//SX1276_WriteReg(SX1276_11_IRQFLAGSMASK, 0xbf); // use only RxDone interrupt
@@ -135,74 +117,28 @@ uint16_t FrSkyR9_callback()
// SX1276_WriteReg(SX1276_01_OPMODE, 0x81); // STDBY
//SX1276_WriteReg(SX1276_09_PACONFIG, 0xF0);
// SX1276_WriteReg(SX1276_09_PACONFIG, 0xF0);
//Set power
// max power: 15dBm (10.8 + 0.6 * MaxPower [dBm])
// output_power: 2 dBm (17-(15-OutputPower) (if pa_boost_pin == true))
SX1276_SetPaConfig(true, 7, 0);
SX1276_SetFrequency(FrSkyR9_freq_map[hopping_frequency_no]); // set current center frequency
delayMicroseconds(500);
packet[0] = 0x3C; // ????
packet[1] = rx_tx_addr[3]; // unique radio id
packet[2] = rx_tx_addr[2]; // unique radio id
packet[3] = hopping_frequency_no; // current channel index
packet[4] = FrSkyR9_step; // step size and last 2 channels start index
packet[5] = RX_num; // receiver number from OpenTX
// binding mode: 0x00 regular / 0x41 bind?
if(IS_BIND_IN_PROGRESS)
packet[6] = 0x41;
else
packet[6] = 0x00;
// TODO
packet[7] = 0x00; // fail safe related (looks like the same sequence of numbers as FrskyX protocol)
// two channel are spread over 3 bytes.
// each channel is 11 bit + 1 bit (msb) that states whether
// it's part of the upper channels (9-16) or lower (1-8) (0 - lower 1 - upper)
#define CH_POS 8
static uint8_t chan_start=0;
uint8_t chan_index = chan_start;
for(int i = 0; i < 12; i += 3)
{
// map channel values (0-2047) to (64-1984)
uint16_t ch1 = FrSkyX_scaleForPXX(chan_index);
uint16_t ch2 = FrSkyX_scaleForPXX(chan_index + 1);
packet[CH_POS + i] = ch1;
packet[CH_POS + i + 1] = (ch1 >> 8) | (ch2 << 4);
packet[CH_POS + i + 2] = (ch2 >> 4);
chan_index += 2;
}
if((sub_protocol & 0x02) == 0)
chan_start ^= 0x08; // Alternate between lower and upper when 16 channels is used
packet[20] = 0x08; // ????
packet[21] = 0x00; // ????
packet[22] = 0x00; // ????
packet[23] = 0x00; // ????
uint16_t crc = FrSkyX_crc(packet, 24);
packet[24] = crc; // low byte
packet[25] = crc >> 8; // high byte
SX1276_WritePayloadToFifo(packet, 26);
//Set frequency
hopping_frequency_no = (hopping_frequency_no + FrSkyR9_step) % FREQ_MAP_SIZE;
SX1276_SetFrequency(FrSkyR9_freq_map[hopping_frequency_no]); // set current center frequency
delayMicroseconds(500); //Frequency settle time
//Build packet
FrSkyR9_build_packet();
//Send
SX1276_WritePayloadToFifo(packet, 26);
SX1276_SetMode(true, false, SX1276_OPMODE_TX);
// need to clear RegIrqFlags?
return 19400;
return 20000;
}
#endif