Code cleanup

This commit is contained in:
pascallanger 2019-09-30 20:53:10 +02:00
parent 821732bba9
commit c2404d4f0d
6 changed files with 22 additions and 333 deletions

View File

@ -184,27 +184,6 @@ static void __attribute__((unused)) FrSkyX_build_packet()
uint8_t limit = (sub_protocol & 2 ) ? 31 : 28 ;
for (uint8_t i=22;i<limit;i++)
packet[i]=0;
#if defined SPORT_POLLING
uint8_t idxs=0;
if(ok_to_send)
for (uint8_t i=23;i<limit;i++)
{//
if(sport_index==sport_idx)
{//no new data
ok_to_send=false;
break;
}
packet[i]=SportData[sport_index];
sport_index= (sport_index+1) & (MAX_SPORT_BUFFER-1);
idxs++;
}
packet[22]= idxs;
debug("SPort: ");
for(uint8_t i=0;i<idxs;i++)
debug("%02X ",packet[23+i]);
debugln("");
#endif // SPORT_POLLING
#ifdef SPORT_SEND
uint8_t nbr_bytes=0;
for (uint8_t i=23;i<limit;i++)
@ -327,12 +306,6 @@ uint16_t initFrSkyX()
//************************
FrSkyX_init();
#ifdef SPORT_POLLING
#ifdef INVERT_SERIAL
start_timer4() ;
#endif
#endif
//
if(IS_BIND_IN_PROGRESS)
{
state = FRSKY_BIND;

View File

@ -19,7 +19,7 @@
#define VERSION_MAJOR 1
#define VERSION_MINOR 3
#define VERSION_REVISION 0
#define VERSION_PATCH_LEVEL 1
#define VERSION_PATCH_LEVEL 2
//******************
// Protocols
@ -324,7 +324,7 @@ enum MultiPacketTypes
MULTI_TELEMETRY_AFHDS2A = 6,
MULTI_TELEMETRY_CONFIG = 7,
MULTI_TELEMETRY_SYNC = 8,
MULTI_TELEMETRY_SPORT_POLLING = 9,
MULTI_TELEMETRY_SPORT_BUFFER = 9,
MULTI_TELEMETRY_HITEC = 10,
MULTI_TELEMETRY_SCANNER = 11,
MULTI_TELEMETRY_AFHDS2A_AC = 12,
@ -591,9 +591,10 @@ Serial: 100000 Baud 8e2 _ xxxx xxxx p --
Stream[0] = 0x54 sub_protocol values are 32..63 Stream contains channels
Stream[0] = 0x57 sub_protocol values are 0..31 Stream contains failsafe
Stream[0] = 0x56 sub_protocol values are 32..63 Stream contains failsafe
Stream[0] |= 0x20 any of the above + 8 additional bytes at the end of the stream available for the current sub_protocol
header
Stream[1] = sub_protocol|BindBit|RangeCheckBit|AutoBindBit;
sub_protocol is 0..31 (bits 0..4), value should be added with 32 if Stream[0] = 0x54
sub_protocol is 0..31 (bits 0..4), value should be added with 32 if Stream[0] = 0x54 | 0x56
=> Reserved 0
Flysky 1
Hubsan 2

View File

@ -55,11 +55,7 @@
#include <SPI.h>
#include <EEPROM.h>
HardwareTimer HWTimer2(2);
#if defined SPORT_POLLING
#ifdef INVERT_TELEMETRY
HardwareTimer HWTimer4(4);
#endif
#endif
void PPM_decode();
void ISR_COMPB();
extern "C"
@ -210,13 +206,6 @@ uint8_t packet_in[TELEMETRY_BUFFER_SIZE];//telemetry receiving packets
uint8_t telemetry_link=0;
uint8_t telemetry_counter=0;
uint8_t telemetry_lost;
#ifdef SPORT_POLLING
#define MAX_SPORT_BUFFER 64
uint8_t SportData[MAX_SPORT_BUFFER];
bool ok_to_send = false;
uint8_t sport_idx = 0;
uint8_t sport_index = 0;
#endif
#ifdef SPORT_SEND
#define MAX_SPORT_BUFFER 64
uint8_t SportData[MAX_SPORT_BUFFER];
@ -902,9 +891,7 @@ inline void tx_resume()
{
#ifdef TELEMETRY
// Resume telemetry by enabling transmitter interrupt
#ifndef SPORT_POLLING
if(!IS_TX_PAUSE_on)
#endif
{
#ifdef ORANGE_TX
cli() ;
@ -1664,9 +1651,7 @@ void modules_reset()
USART2_BASE->CR1 |= USART_CR1_PCE_BIT;
}
usart3_begin(100000,SERIAL_8E2);
#ifndef SPORT_POLLING
USART3_BASE->CR1 &= ~ USART_CR1_RE; //disable receive
#endif
USART3_BASE->CR1 &= ~ USART_CR1_RE; //disable receive
USART2_BASE->CR1 &= ~ USART_CR1_TE; //disable transmit
#else
//ATMEGA328p

View File

@ -48,18 +48,6 @@ uint8_t RetrySequence ;
// Store for FrskyX telemetry
struct t_FrSkyX_RX_Frame FrSkyX_RX_Frames[4] ;
uint8_t FrSkyX_RX_NextFrame=0;
#ifdef SPORT_POLLING
uint8_t sport_rx_index[28] ;
uint8_t ukindex ;
uint8_t kindex ;
uint8_t TxData[2];
uint8_t SportIndexPolling;
uint8_t RxData[16] ;
volatile uint8_t RxIndex=0 ;
uint8_t sport_bytes=0;
uint8_t skipped_id;
uint8_t rx_counter=0;
#endif
#endif // SPORT_TELEMETRY
#if defined HUB_TELEMETRY
@ -90,14 +78,6 @@ static void multi_send_header(uint8_t type, uint8_t len)
static void multi_send_status()
{
#ifdef SPORT_POLLING
#ifdef INVERT_SERIAL
USART3_BASE->CR1 &= ~USART_CR1_TE ;
TX_INV_on; //activate inverter for both serial TX and RX signals
USART3_BASE->CR1 |= USART_CR1_TE ;
#endif
rx_pause();
#endif
multi_send_header(MULTI_TELEMETRY_STATUS, 5);
// Build flags
@ -538,7 +518,7 @@ packet_in[6]|(counter++)|00 01 02 03 04 05 06 07 08 09
0x34 0x0A 0xC3 0x56 0xF3
*/
#if defined SPORT_POLLING || defined MULTI_TELEMETRY
#if defined MULTI_TELEMETRY
const uint8_t PROGMEM Indices[] = { 0x00, 0xA1, 0x22, 0x83, 0xE4, 0x45,
0xC6, 0x67, 0x48, 0xE9, 0x6A, 0xCB,
0xAC, 0x0D, 0x8E, 0x2F, 0xD0, 0x71,
@ -568,11 +548,6 @@ const uint8_t PROGMEM Indices[] = { 0x00, 0xA1, 0x22, 0x83, 0xE4, 0x45,
void sportSend(uint8_t *p)
{
uint16_t crc_s = 0;
#if defined SPORT_POLLING && defined INVERT_SERIAL
USART3_BASE->CR1 &= ~USART_CR1_TE ;
TX_INV_on; //activate inverter for both serial TX and RX signals
USART3_BASE->CR1 |= USART_CR1_TE ;
#endif
Serial_write(START_STOP);//+9
Serial_write(p[0]) ;
for (uint8_t i = 1; i < 9; i++)
@ -595,198 +570,6 @@ const uint8_t PROGMEM Indices[] = { 0x00, 0xA1, 0x22, 0x83, 0xE4, 0x45,
}
#endif
#if defined SPORT_POLLING
uint8_t nextID()
{
uint8_t i ;
uint8_t poll_idx ;
if (phase)
{
poll_idx = 99 ;
for ( i = 0 ; i < 28 ; i++ )
{
if ( sport_rx_index[kindex] )
{
poll_idx = kindex ;
}
kindex++ ;
if ( kindex>= 28 )
{
kindex = 0 ;
phase = 0 ;
break ;
}
if ( poll_idx != 99 )
{
break ;
}
}
if ( poll_idx != 99 )
{
return poll_idx ;
}
}
if ( phase == 0 )
{
for ( i = 0 ; i < 28 ; i++ )
{
if ( sport_rx_index[ukindex] == 0 )
{
poll_idx = ukindex ;
phase = 1 ;
}
ukindex++;
if (ukindex >= 28 )
{
ukindex = 0 ;
}
if ( poll_idx != 99 )
{
return poll_idx ;
}
}
if ( poll_idx == 99 )
{
phase = 1 ;
return 0 ;
}
}
return poll_idx ;
}
#ifdef INVERT_SERIAL
void start_timer4()
{
TIMER4_BASE->PSC = 71; // 72-1;for 72 MHZ / 1.0sec/(71+1)
TIMER4_BASE->CCER = 0 ;
TIMER4_BASE->DIER = 0 ;
TIMER4_BASE->CCMR1 = 0 ;
TIMER4_BASE->CCMR1 = TIMER_CCMR1_OC1M ;
HWTimer4.attachInterrupt(TIMER_CH1, __irq_timer4); // Assign function to Timer2/Comp2 interrupt
nvic_irq_set_priority( NVIC_TIMER4, 14 ) ;
}
void stop_timer4()
{
TIMER5_BASE->CR1 = 0 ;
nvic_irq_disable( NVIC_TIMER4 ) ;
}
void __irq_timer4(void)
{
TIMER4_BASE->DIER = 0 ;
TIMER4_BASE->CR1 = 0 ;
TX_INV_on; //activate inverter for both serial TX and RX signals
}
#endif
void pollSport()
{
uint8_t pindex = nextID() ;
TxData[0] = START_STOP;
TxData[1] = pgm_read_byte_near(&Indices[pindex]) ;
if(!telemetry_lost && ((TxData[1] &0x1F)== skipped_id ||TxData[1]==0x98))
{//98 ID(RSSI/RxBat and SWR ) and ID's from sport telemetry
pindex = nextID() ;
TxData[1] = pgm_read_byte_near(&Indices[pindex]);
}
SportIndexPolling = pindex ;
RxIndex = 0;
#ifdef INVERT_SERIAL
USART3_BASE->CR1 &= ~USART_CR1_TE ;
TX_INV_on; //activate inverter for both serial TX and RX signals
USART3_BASE->CR1 |= USART_CR1_TE ;
#endif
#ifdef MULTI_TELEMETRY
multi_send_header(MULTI_TELEMETRY_SPORT_POLLING, 1);
#else
Serial_write(TxData[0]);
#endif
RxIndex=0;
Serial_write(TxData[1]);
USART3_BASE->CR1 |= USART_CR1_TCIE ;
#ifdef INVERT_SERIAL
TIMER4_BASE->CNT = 0 ;
TIMER4_BASE->CCR1 = 3000 ;
TIMER4_BASE->DIER = TIMER_DIER_CC1IE ;
TIMER4_BASE->CR1 = TIMER_CR1_CEN ;
#endif
}
bool checkSportPacket()
{
uint8_t *packet = RxData ;
uint16_t crc = 0 ;
if ( RxIndex < 8 )
return 0 ;
for ( uint8_t i = 0 ; i<8 ; i += 1 )
{
crc += packet[i];
crc += crc >> 8;
crc &= 0x00ff;
}
return (crc == 0x00ff) ;
}
uint8_t unstuff()
{
uint8_t i ;
uint8_t j ;
j = 0 ;
for ( i = 0 ; i < RxIndex ; i += 1 )
{
if ( RxData[i] == BYTESTUFF )
{
i += 1 ;
RxData[j] = RxData[i] ^ STUFF_MASK ; ;
}
else
RxData[j] = RxData[i] ;
j += 1 ;
}
return j ;
}
void processSportData(uint8_t *p)
{
RxIndex = unstuff() ;
uint8_t x=checkSportPacket() ;
if (x)
{
SportData[sport_idx]=0x7E;
sport_idx =(sport_idx+1) & (MAX_SPORT_BUFFER-1);
SportData[sport_idx]=TxData[1]&0x1F;
sport_idx =(sport_idx+1) & (MAX_SPORT_BUFFER-1);
for(uint8_t i=0;i<(RxIndex-1);i++)
{//no crc
if(p[i]==START_STOP || p[i]==BYTESTUFF)
{//stuff back
SportData[sport_idx]=BYTESTUFF;
sport_idx =(sport_idx+1) & (MAX_SPORT_BUFFER-1);
SportData[sport_idx]=p[i]^STUFF_MASK;
}
else
SportData[sport_idx]=p[i];
sport_idx =(sport_idx+1) & (MAX_SPORT_BUFFER-1);
}
sport_rx_index[SportIndexPolling] = 1 ;
ok_to_send=true;
RxIndex =0 ;
}
}
inline void rx_pause()
{
USART3_BASE->CR1 &= ~ USART_CR1_RXNEIE; //disable rx interrupt on USART3
}
inline void rx_resume()
{
USART3_BASE->CR1 |= USART_CR1_RXNEIE; //enable rx interrupt on USART3
}
#endif//end SPORT_POLLING
void sportIdle()
{
#if !defined MULTI_TELEMETRY
@ -796,18 +579,11 @@ void sportIdle()
void sportSendFrame()
{
#if defined SPORT_POLLING
rx_pause();
#endif
uint8_t i;
sport_counter = (sport_counter + 1) %36;
if(telemetry_lost)
{
#ifdef SPORT_POLLING
pollSport();
#else
sportIdle();
#endif
sportIdle();
return;
}
if(sport_counter<6)
@ -843,26 +619,19 @@ void sportSendFrame()
if(sport)
{
for (i=0;i<FRSKY_SPORT_PACKET_SIZE;i++)
frame[i]=pktx1[i];
sport -= 1 ;
#ifdef SPORT_POLLING
skipped_id=frame[0];
#endif
frame[i]=pktx1[i];
sport -- ;
if ( sport )
{
uint8_t j = sport * FRSKY_SPORT_PACKET_SIZE ;
for (i=0;i<j;i++)
pktx1[i] = pktx1[i+FRSKY_SPORT_PACKET_SIZE] ;
pktx1[i] = pktx1[i+FRSKY_SPORT_PACKET_SIZE] ;
}
break;
}
else
{
#ifdef SPORT_POLLING
pollSport();
#else
sportIdle();
#endif
sportIdle();
return;
}
}
@ -982,19 +751,6 @@ void TelemetryUpdate()
}
telemetry_link=0;
sportSendFrame();
/* uint32_t now = micros();
if ((now - last) > SPORT_TIME)
{
#if defined SPORT_POLLING
processSportData(RxData); //process arrived data before polling
#endif
sportSendFrame();
#ifdef STM32_BOARD
last=now;
#else
last += SPORT_TIME ;
#endif
}*/
}
#endif // SPORT_TELEMETRY
@ -1163,27 +919,6 @@ void TelemetryUpdate()
#endif
{ // Transmit interrupt
#ifdef STM32_BOARD
#ifdef SPORT_POLLING
if(USART3_BASE->SR & USART_SR_TC)
{
if ( USART3_BASE->CR1 & USART_CR1_TCIE )
{
USART3_BASE->CR1 &= ~USART_CR1_TCIE ;
TX_INV_off;
}
}
if(USART3_BASE->SR & USART_SR_RXNE)
{
USART3_BASE->SR &= ~USART_SR_RXNE;
if (RxIndex < 16 )
{
if(RxData[0]==TxData[0] && RxData[1]==TxData[1])
RxIndex=0;
RxData[RxIndex++] = USART3_BASE->DR & 0xFF ;
}
}
#endif
if(USART3_BASE->SR & USART_SR_TXE)
{
#endif
@ -1200,9 +935,6 @@ void TelemetryUpdate()
if (tx_tail == tx_head)
{
tx_pause(); // Check if all data is transmitted . if yes disable transmitter UDRE interrupt
#ifdef SPORT_POLLING
rx_resume();
#endif
}
#ifdef STM32_BOARD
}

View File

@ -246,7 +246,7 @@
#undef NCC1701_HUB_TELEMETRY
#undef HUB_TELEMETRY
#undef SPORT_TELEMETRY
#undef SPORT_POLLING
#undef SPORT_SEND
#undef DSM_TELEMETRY
#undef MULTI_STATUS
#undef MULTI_TELEMETRY
@ -294,13 +294,10 @@
#endif
#if not defined(FRSKYX_CC2500_INO)
#undef SPORT_TELEMETRY
#undef SPORT_POLLING
#undef SPORT_SEND
#endif
#if not defined (SPORT_TELEMETRY) || not defined (STM32_BOARD)
#undef SPORT_POLLING
#endif
#if defined SPORT_POLLING && not defined INVERT_TELEMETRY
#error SPORT_POLLING has been defined but not INVERT_TELEMETRY. They should be both enabled to work.
#if not defined (SPORT_TELEMETRY)
#undef SPORT_SEND
#endif
#if not defined(DSM_CYRF6936_INO)
#undef DSM_TELEMETRY
@ -308,10 +305,13 @@
#if not defined(DSM_TELEMETRY) && not defined(SPORT_TELEMETRY) && not defined(HUB_TELEMETRY) && not defined(HUBSAN_HUB_TELEMETRY) && not defined(BUGS_HUB_TELEMETRY) && not defined(NCC1701_HUB_TELEMETRY) && not defined(BAYANG_HUB_TELEMETRY) && not defined(CABELL_HUB_TELEMETRY) && not defined(AFHDS2A_HUB_TELEMETRY) && not defined(AFHDS2A_FW_TELEMETRY) && not defined(MULTI_TELEMETRY) && not defined(MULTI_STATUS) && not defined(HITEC_HUB_TELEMETRY) && not defined(HITEC_FW_TELEMETRY) && not defined(SCANNER_TELEMETRY) && not defined(FRSKYX_RX_TELEMETRY)
#undef TELEMETRY
#undef INVERT_TELEMETRY
#undef SPORT_POLLING
#endif
#endif
#ifdef SPORT_SEND
#define SERIAL_DATA_ENABLE
#endif
//Make sure TX is defined correctly
#ifndef AILERON
#error You must select a correct channel order.

View File

@ -287,11 +287,9 @@
#define SCANNER_TELEMETRY // Forward spectrum scanner data to TX
#define FRSKYX_RX_TELEMETRY // Forward channels data to TX
//SPORT_POLLING is an implementation of the same polling routine as XJT module for sport telemetry bidirectional communication.
//This is useful for passing sport control frames from TX to RX(ex: changing Betaflight PID or VTX channels on the fly using LUA scripts with OpentX).
//Using this feature requires to uncomment INVERT_TELEMETRY as this TX output on telemetry pin only inverted signal.
//SPORT_SEND: passing sport control frames from TX to RX(ex: SxR configuration, changing Betaflight PID or VTX channels on the fly using LUA scripts with OpentX).
//!!!! This is a work in progress!!! Do not enable unless you want to test and report
//#define SPORT_POLLING
//#define SPORT_SEND
/****************************/