Code cleaning (XMEGA)

This commit is contained in:
pascallanger 2016-09-01 17:41:24 +02:00
parent ee27535b82
commit 043a8336e5
3 changed files with 398 additions and 461 deletions

View File

@ -146,6 +146,27 @@ struct PPM_Parameters
uint8_t option; uint8_t option;
}; };
//*******************
//*** Timer ***
//*******************
#ifdef XMEGA
#define TIFR1 TCC1.INTFLAGS
#define OCF1A_bm TC1_CCAIF_bm
#define OCR1A TCC1.CCA
#define TCNT1 TCC1.CNT
#define USARTC0.DATA UDR0
#define OCF1B_bm TC1_CCBIF_bm
#define OCR1B TCC1.CCB
#define TCC1.INTCTRLB TIMSK1
#define SET_TIMSK1_OCIE1B TIMSK1 = (TIMSK1 & 0xF3) | 0x04
#define CLR_TIMSK1_OCIE1B TIMSK1 &= 0xF3
#else
#define OCF1A_bm _BV(OCF1A)
#define OCF1B_bm _BV(OCF1B)
#define SET_TIMSK1_OCIE1B TIMSK1 |= _BV(OCIE1B)
#define CLR_TIMSK1_OCIE1B TIMSK1 &=~_BV(OCIE1B)
#endif
//******************* //*******************
//*** Pinouts *** //*** Pinouts ***
//******************* //*******************

View File

@ -29,6 +29,10 @@
#include "_Config.h" #include "_Config.h"
#include "TX_Def.h" #include "TX_Def.h"
#ifdef XMEGA
#undef ENABLE_PPM // Disable PPM for orange module
#endif
//Global constants/variables //Global constants/variables
uint32_t MProtocol_id;//tx id, uint32_t MProtocol_id;//tx id,
uint32_t MProtocol_id_master; uint32_t MProtocol_id_master;
@ -135,6 +139,7 @@ void_function_t remote_callback = 0;
void setup() void setup()
{ {
#ifdef XMEGA #ifdef XMEGA
// General pinout
PORTD.OUTSET = 0x17 ; PORTD.OUTSET = 0x17 ;
PORTD.DIRSET = 0xB2 ; PORTD.DIRSET = 0xB2 ;
PORTD.DIRCLR = 0x4D ; PORTD.DIRCLR = 0x4D ;
@ -147,6 +152,15 @@ void setup()
for ( uint8_t count = 0 ; count < 20 ; count += 1 ) for ( uint8_t count = 0 ; count < 20 ; count += 1 )
asm("nop") ; asm("nop") ;
PORTE.OUTCLR = 0x01 ; PORTE.OUTCLR = 0x01 ;
// Timer1 config
// TCC1 16-bit timer, clocked at 0.5uS
EVSYS.CH3MUX = 0x80 + 0x04 ; // Prescaler of 16
TCC1.CTRLB = 0; TCC1.CTRLC = 0; TCC1.CTRLD = 0; TCC1.CTRLE = 0;
TCC1.INTCTRLA = 0; TIMSK1 = 0;
TCC1.PER = 0xFFFF ;
TCNT1 = 0 ;
TCC1.CTRLA = 0x0B ; // Event3 (prescale of 16)
#else #else
// General pinout // General pinout
DDRD = _BV(A7105_CS_pin)|_BV(SDI_pin)|_BV(SCLK_pin)|_BV( CC25_CSN_pin);//pin output DDRD = _BV(A7105_CS_pin)|_BV(SDI_pin)|_BV(SCLK_pin)|_BV( CC25_CSN_pin);//pin output
@ -154,6 +168,13 @@ void setup()
DDRB = _BV(NRF_CSN_pin)|_BV(CYRF_CSN_pin); //pin output DDRB = _BV(NRF_CSN_pin)|_BV(CYRF_CSN_pin); //pin output
PORTB = _BV(2)|_BV(3)|_BV(4)|_BV(BIND_pin); //pullup on dial (D10=PB2,D11=PB3,D12=PB4) and bind button PORTB = _BV(2)|_BV(3)|_BV(4)|_BV(BIND_pin); //pullup on dial (D10=PB2,D11=PB3,D12=PB4) and bind button
PORTC = _BV(0); //pullup on dial (A0=PC0) PORTC = _BV(0); //pullup on dial (A0=PC0)
#ifdef DEBUG_TX
TX_SET_OUTPUT;
#endif
// Timer1 config
TCCR1A = 0;
TCCR1B = (1 << CS11); //prescaler8, set timer1 to increment every 0.5us(16Mhz) and start timer
#endif #endif
// Set Chip selects // Set Chip selects
@ -165,25 +186,6 @@ void setup()
SDI_on; SDI_on;
SCK_off; SCK_off;
//#ifdef XMEGA
// // SPI enable, master, prescale of 16
// SPID.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_PRESCALER0_bm ;
//#endif
// Timer1 config
#ifdef XMEGA
// TCC1 16-bit timer, clocked at 0.5uS
EVSYS.CH3MUX = 0x80 + 0x04 ; // Prescaler of 16
TCC1.CTRLB = 0; TCC1.CTRLC = 0; TCC1.CTRLD = 0; TCC1.CTRLE = 0;
TCC1.INTCTRLA = 0; TCC1.INTCTRLB = 0;
TCC1.PER = 0xFFFF ;
TCC1.CNT = 0 ;
TCC1.CTRLA = 0x0B ; // Event3 (prescale of 16)
#else
TCCR1A = 0;
TCCR1B = (1 << CS11); //prescaler8, set timer1 to increment every 0.5us(16Mhz) and start timer
#endif
// Set servos positions // Set servos positions
for(uint8_t i=0;i<NUM_CHN;i++) for(uint8_t i=0;i<NUM_CHN;i++)
Servo_data[i]=1500; Servo_data[i]=1500;
@ -201,8 +203,8 @@ void setup()
// Read status of mode select binary switch // Read status of mode select binary switch
// after this mode_select will be one of {0000, 0001, ..., 1111} // after this mode_select will be one of {0000, 0001, ..., 1111}
#ifdef XMEGA #ifndef ENABLE_PPM
mode_select = MODE_SERIAL ; // only serial mode mode_select = MODE_SERIAL ; // force serial mode
#else #else
mode_select=0x0F - ( ( (PINB>>2)&0x07 ) | ( (PINC<<3)&0x08) );//encoder dip switches 1,2,4,8=>B2,B3,B4,C0 mode_select=0x0F - ( ( (PINB>>2)&0x07 ) | ( (PINC<<3)&0x08) );//encoder dip switches 1,2,4,8=>B2,B3,B4,C0
#endif #endif
@ -235,11 +237,9 @@ void setup()
protocol_init(); protocol_init();
#ifndef XMEGA
//Configure PPM interrupt //Configure PPM interrupt
EICRA |=_BV(ISC11); // The rising edge of INT1 pin D3 generates an interrupt request EICRA |=_BV(ISC11); // The rising edge of INT1 pin D3 generates an interrupt request
EIMSK |= _BV(INT1); // INT1 interrupt enable EIMSK |= _BV(INT1); // INT1 interrupt enable
#endif
#if defined(TELEMETRY) #if defined(TELEMETRY)
PPM_Telemetry_serial_init(); // Configure serial for telemetry PPM_Telemetry_serial_init(); // Configure serial for telemetry
@ -275,25 +275,14 @@ void loop()
} }
while(remote_callback==0); while(remote_callback==0);
} }
#ifdef XMEGA if( (TIFR1 & OCF1A_bm) != 0)
if( (TCC1.INTFLAGS & TC1_CCAIF_bm) != 0)
{
cli(); // Disable global int due to RW of 16 bits registers
TCC1.CCA = TCC1.CNT ; // Callback should already have been called... Use "now" as new sync point.
sei(); // Enable global int
}
else
while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait before callback
#else
if( (TIFR1 & _BV(OCF1A)) != 0)
{ {
cli(); // Disable global int due to RW of 16 bits registers cli(); // Disable global int due to RW of 16 bits registers
OCR1A=TCNT1; // Callback should already have been called... Use "now" as new sync point. OCR1A=TCNT1; // Callback should already have been called... Use "now" as new sync point.
sei(); // Enable global int sei(); // Enable global int
} }
else else
while((TIFR1 & _BV(OCF1A)) == 0); // Wait before callback while((TIFR1 & OCF1A_bm) == 0); // Wait before callback
#endif
do do
{ {
TX_ON; TX_ON;
@ -306,41 +295,22 @@ void loop()
while(next_callback>4000) while(next_callback>4000)
{ // start to wait here as much as we can... { // start to wait here as much as we can...
next_callback-=2000; // We will wait below for 2ms next_callback-=2000; // We will wait below for 2ms
#ifdef XMEGA
cli(); // Disable global int due to RW of 16 bits registers
TCC1.CCA +=2000*2; // set compare A for callback
TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A=callback flag
sei(); // enable global int
Update_All();
if(IS_CHANGE_PROTOCOL_FLAG_on)
break; // Protocol has been changed
while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait 2ms...
#else
cli(); // Disable global int due to RW of 16 bits registers cli(); // Disable global int due to RW of 16 bits registers
OCR1A += 2000*2 ; // set compare A for callback OCR1A += 2000*2 ; // set compare A for callback
TIFR1=_BV(OCF1A); // clear compare A=callback flag TIFR1=OCF1A_bm; // clear compare A=callback flag
sei(); // enable global int sei(); // enable global int
Update_All(); Update_All();
if(IS_CHANGE_PROTOCOL_FLAG_on) if(IS_CHANGE_PROTOCOL_FLAG_on)
break; // Protocol has been changed break; // Protocol has been changed
while((TIFR1 & _BV(OCF1A)) == 0); // wait 2ms... while((TIFR1 & OCF1A_bm) == 0); // wait 2ms...
#endif
} }
// at this point we have a maximum of 4ms in next_callback // at this point we have a maximum of 4ms in next_callback
next_callback *= 2 ; next_callback *= 2 ;
#ifdef XMEGA
cli(); // Disable global int due to RW of 16 bits registers
TCC1.CCA +=next_callback; // set compare A for callback
TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A=callback flag
diff=TCC1.CCA-TCC1.CNT; // compare timer and comparator
sei(); // enable global int
#else
cli(); // Disable global int due to RW of 16 bits registers cli(); // Disable global int due to RW of 16 bits registers
OCR1A+= next_callback ; // set compare A for callback OCR1A+= next_callback ; // set compare A for callback
TIFR1=_BV(OCF1A); // clear compare A=callback flag TIFR1=OCF1A_bm; // clear compare A=callback flag
diff=OCR1A-TCNT1; // compare timer and comparator diff=OCR1A-TCNT1; // compare timer and comparator
sei(); // enable global int sei(); // enable global int
#endif
} }
while(diff&0x8000); // Callback did not took more than requested time for next callback while(diff&0x8000); // Callback did not took more than requested time for next callback
// so we can launch Update_All before next callback // so we can launch Update_All before next callback
@ -349,10 +319,6 @@ void loop()
void Update_All() void Update_All()
{ {
TX_ON;
NOP();
TX_OFF;
#ifdef ENABLE_SERIAL #ifdef ENABLE_SERIAL
if(mode_select==MODE_SERIAL && IS_RX_FLAG_on) // Serial mode and something has been received if(mode_select==MODE_SERIAL && IS_RX_FLAG_on) // Serial mode and something has been received
{ {
@ -389,9 +355,6 @@ TX_OFF;
if( (protocol==MODE_FRSKY) || (protocol==MODE_HUBSAN) || (protocol==MODE_FRSKYX) || (protocol==MODE_DSM) ) if( (protocol==MODE_FRSKY) || (protocol==MODE_HUBSAN) || (protocol==MODE_FRSKYX) || (protocol==MODE_DSM) )
TelemetryUpdate(); TelemetryUpdate();
#endif #endif
TX_ON;
NOP();
TX_OFF;
} }
// Update Servo_AUX flags based on servo AUX positions // Update Servo_AUX flags based on servo AUX positions
@ -447,9 +410,11 @@ inline void tx_resume()
#ifdef XMEGA #ifdef XMEGA
USARTC0.CTRLA = (USARTC0.CTRLA & 0xFC) | 0x01 ; // Resume telemetry by enabling transmitter interrupt USARTC0.CTRLA = (USARTC0.CTRLA & 0xFC) | 0x01 ; // Resume telemetry by enabling transmitter interrupt
#else #else
#ifndef BASH_SERIAL
UCSR0B |= _BV(UDRIE0); // Resume telemetry by enabling transmitter interrupt UCSR0B |= _BV(UDRIE0); // Resume telemetry by enabling transmitter interrupt
#endif #endif
#endif #endif
#endif
} }
// Protocol start // Protocol start
@ -671,15 +636,9 @@ static void protocol_init()
next_callback-=temp<<10; // between 2-3ms left at this stage next_callback-=temp<<10; // between 2-3ms left at this stage
} }
cli(); // disable global int cli(); // disable global int
#ifdef XMEGA
TCC1.CCA = TCC1.CNT + next_callback*2; // set compare A for callback
sei(); // enable global int
TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A flag
#else
OCR1A = TCNT1 + next_callback*2;// set compare A for callback OCR1A = TCNT1 + next_callback*2;// set compare A for callback
sei(); // enable global int sei(); // enable global int
TIFR1=_BV(OCF1A); // clear compare A flag TIFR1 = OCF1A_bm ; // clear compare A flag
#endif
BIND_BUTTON_FLAG_off; // do not bind/reset id anymore even if protocol change BIND_BUTTON_FLAG_off; // do not bind/reset id anymore even if protocol change
} }
@ -835,7 +794,6 @@ uint16_t limit_channel_100(uint8_t ch)
void Mprotocol_serial_init() void Mprotocol_serial_init()
{ {
#ifdef XMEGA #ifdef XMEGA
PORTC.OUTSET = 0x08 ; PORTC.OUTSET = 0x08 ;
PORTC.DIRSET = 0x08 ; PORTC.DIRSET = 0x08 ;
@ -845,7 +803,7 @@ void Mprotocol_serial_init()
USARTC0.CTRLB = 0x18 ; USARTC0.CTRLB = 0x18 ;
USARTC0.CTRLA = (USARTC0.CTRLA & 0xCF) | 0x10 ; USARTC0.CTRLA = (USARTC0.CTRLA & 0xCF) | 0x10 ;
USARTC0.CTRLC = 0x2B ; USARTC0.CTRLC = 0x2B ;
USARTC0.DATA ; UDR0 ;
#ifdef INVERT_TELEMETRY #ifdef INVERT_TELEMETRY
PORTC.PIN3CTRL |= 0x40 ; PORTC.PIN3CTRL |= 0x40 ;
#endif #endif
@ -860,9 +818,7 @@ void Mprotocol_serial_init()
UDR0; UDR0;
//enable reception and RC complete interrupt //enable reception and RC complete interrupt
UCSR0B = _BV(RXEN0)|_BV(RXCIE0);//rx enable and interrupt UCSR0B = _BV(RXEN0)|_BV(RXCIE0);//rx enable and interrupt
#ifdef DEBUG_TX #ifndef DEBUG_TX
TX_SET_OUTPUT;
#else
#if defined(TELEMETRY) #if defined(TELEMETRY)
initTXSerial( SPEED_100K ) ; initTXSerial( SPEED_100K ) ;
#endif //TELEMETRY #endif //TELEMETRY
@ -1076,11 +1032,7 @@ ISR(INT1_vect, ISR_NOBLOCK)
static uint16_t Prev_TCNT1=0; static uint16_t Prev_TCNT1=0;
uint16_t Cur_TCNT1; uint16_t Cur_TCNT1;
#ifdef XMEGA
Cur_TCNT1 = TCC1.CNT - Prev_TCNT1 ; // Capture current Timer1 value
#else
Cur_TCNT1 = TCNT1 - Prev_TCNT1 ; // Capture current Timer1 value Cur_TCNT1 = TCNT1 - Prev_TCNT1 ; // Capture current Timer1 value
#endif
if(Cur_TCNT1<1000) if(Cur_TCNT1<1000)
chan=-1; // bad frame chan=-1; // bad frame
else else
@ -1120,34 +1072,20 @@ ISR(USART_RX_vect)
if(idx==0||discard_frame==1) if(idx==0||discard_frame==1)
{ // Let's try to sync at this point { // Let's try to sync at this point
idx=0;discard_frame=0; idx=0;discard_frame=0;
#ifdef XMEGA
if(USARTC0.DATA==0x55) // If 1st byte is 0x55 it looks ok
#else
if(UDR0==0x55) // If 1st byte is 0x55 it looks ok if(UDR0==0x55) // If 1st byte is 0x55 it looks ok
#endif
{ {
TX_RX_PAUSE_on; TX_RX_PAUSE_on;
tx_pause(); tx_pause();
#ifdef XMEGA OCR1B = TCNT1+(6500L) ; // Full message should be received within timer of 3250us
TCC1.CCB = TCC1.CNT+(6500L) ; // Full message should be received within timer of 3250us TIFR1 = OCF1B_bm ; // clear OCR1B match flag
TCC1.INTFLAGS = TC1_CCBIF_bm ; // clear OCR1B match flag SET_TIMSK1_OCIE1B ; // enable interrupt on compare B match
TCC1.INTCTRLB = (TCC1.INTCTRLB & 0xF3) | 0x04 ; // enable interrupt on compare B match
#else
OCR1B=TCNT1+6500L; // Full message should be received within timer of 3250us
TIFR1=_BV(OCF1B); // clear OCR1B match flag
TIMSK1 |=_BV(OCIE1B); // enable interrupt on compare B match
#endif
idx++; idx++;
} }
} }
else else
{ {
RX_MISSED_BUFF_off; // if rx_buff was good it's not anymore... RX_MISSED_BUFF_off; // if rx_buff was good it's not anymore...
#ifdef XMEGA
rx_buff[(idx++)-1]=USARTC0.DATA; // Store received byte
#else
rx_buff[(idx++)-1]=UDR0; // Store received byte rx_buff[(idx++)-1]=UDR0; // Store received byte
#endif
if(idx>RXBUFFER_SIZE) if(idx>RXBUFFER_SIZE)
{ // A full frame has been received { // A full frame has been received
if(!IS_RX_DONOTUPDTAE_on) if(!IS_RX_DONOTUPDTAE_on)
@ -1163,20 +1101,12 @@ ISR(USART_RX_vect)
} }
else else
{ {
#ifdef XMEGA
idx = USARTC0.DATA; // Dummy read
#else
idx=UDR0; // Dummy read idx=UDR0; // Dummy read
#endif
discard_frame=1; // Error encountered discard full frame... discard_frame=1; // Error encountered discard full frame...
} }
if(discard_frame==1) if(discard_frame==1)
{ {
#ifdef XMEGA CLR_TIMSK1_OCIE1B; // Disable interrupt on compare B match
TCC1.INTCTRLB &=0xF3; // Disable interrupt on compare B match
#else
TIMSK1 &=~_BV(OCIE1B); // Disable interrupt on compare B match
#endif
TX_RX_PAUSE_off; TX_RX_PAUSE_off;
tx_resume(); tx_resume();
} }
@ -1190,16 +1120,11 @@ ISR(USART_RX_vect)
#ifdef XMEGA #ifdef XMEGA
ISR(TCC1_CCB_vect) ISR(TCC1_CCB_vect)
#else #else
//ISR(TIMER1_COMPB_vect)
ISR(TIMER1_COMPB_vect, ISR_NOBLOCK ) ISR(TIMER1_COMPB_vect, ISR_NOBLOCK )
#endif #endif
{ // Timer1 compare B interrupt { // Timer1 compare B interrupt
discard_frame=1; discard_frame=1;
#ifdef XMEGA CLR_TIMSK1_OCIE1B; // Disable interrupt on compare B match
TCC1.INTCTRLB &=0xF3; // Disable interrupt on compare B match
#else
TIMSK1 &=~_BV(OCIE1B); // Disable interrupt on compare B match
#endif
tx_resume(); tx_resume();
} }
#endif //ENABLE_SERIAL #endif //ENABLE_SERIAL

View File

@ -1,7 +1,7 @@
//************************************* //**************************
// FrSky Telemetry serial code * // Telemetry serial code *
// By Midelic on RCGroups * // By Midelic on RCGroups *
//************************************* //**************************
#if defined TELEMETRY #if defined TELEMETRY
@ -30,7 +30,6 @@ uint8_t frame[18];
#ifdef BASH_SERIAL #ifdef BASH_SERIAL
// For bit-bashed serial output // For bit-bashed serial output
struct t_serial_bash struct t_serial_bash
{ {
uint8_t head ; uint8_t head ;
@ -540,18 +539,10 @@ ISR(USART_UDRE_vect)
{ {
if(++tx_tail>=TXBUFFER_SIZE)//head if(++tx_tail>=TXBUFFER_SIZE)//head
tx_tail=0; tx_tail=0;
#ifdef XMEGA
USARTC0.DATA = tx_buff[tx_tail] ;
#else
UDR0=tx_buff[tx_tail]; UDR0=tx_buff[tx_tail];
#endif
} }
if (tx_tail == tx_head) if (tx_tail == tx_head)
#ifdef XMEGA tx_pause(); // Check if all data is transmitted . if yes disable transmitter UDRE interrupt
USARTC0.CTRLA &= ~0x03 ;
#else
UCSR0B &= ~(1<<UDRIE0); // Check if all data is transmitted . if yes disable transmitter UDRE interrupt
#endif
} }
#else //BASH_SERIAL #else //BASH_SERIAL