From d279ef7490555d0dfe648231dd2c480ad65dca54 Mon Sep 17 00:00:00 2001 From: midelic Date: Fri, 2 Sep 2016 22:08:09 +0100 Subject: [PATCH] XMEGA /STM32 cleaning add layer --- Multiprotocol/Multiprotocol.ino | 322 +++++----------------------- Multiprotocol/Multiprotocol_STM32.h | 26 ++- Multiprotocol/SPI.ino | 4 +- Multiprotocol/Telemetry.ino | 11 +- Multiprotocol/_Config.h | 24 ++- 5 files changed, 97 insertions(+), 290 deletions(-) diff --git a/Multiprotocol/Multiprotocol.ino b/Multiprotocol/Multiprotocol.ino index e07fa13..f3b17ad 100644 --- a/Multiprotocol/Multiprotocol.ino +++ b/Multiprotocol/Multiprotocol.ino @@ -20,26 +20,14 @@ You should have received a copy of the GNU General Public License along with Multiprotocol. If not, see . */ -#define STM32_board -//#undef __cplusplus -#if defined STM32_board -#include "Multiprotocol_STM32.h" -#include -#include -#include -#include -uint16_t OCR1A = 0; -uint16_t TCNT1=0; -HardwareTimer timer(2); -#else -#include -#include -#include "Multiprotocol.h" -#endif + #include #include "_Config.h" #include "TX_Def.h" +#ifdef STM32_board +HardwareTimer timer(2); +#endif #ifdef XMEGA #undef ENABLE_PPM // Disable PPM for orange module @@ -65,9 +53,6 @@ uint16_t Servo_data[NUM_CHN]; uint8_t Servo_AUX; uint16_t servo_max_100,servo_min_100,servo_max_125,servo_min_125; -#ifndef STM32_board -uint16_t servo_max_100,servo_min_100,servo_max_125,servo_min_125; -#endif // Protocol variables uint8_t cyrfmfg_id[6];//for dsm2 and devo uint8_t rx_tx_addr[5]; @@ -214,7 +199,7 @@ void setup() start_timer2();//0.5us #else DDRD = (1<SR & TIMER_SR_CC1IF)!=0){ cli(); @@ -368,16 +343,15 @@ void loop() else while((TIMER2_BASE->SR & TIMER_SR_CC1IF )==0);//walit till compare match #else - if( (TIFR1 & (1<4000) { // start to wait here as much as we can... - next_callback-=2000; - - #ifdef XMEGA - cli(); // disable global int - 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 - + next_callback-=2000; #if defined STM32_board cli(); OCR1A+=2000*2;// clear compare A=callback flag - TIMER2_BASE->CCR1=OCR1A; - TCNT1 = TIMER2_BASE->CNT; TIMER2_BASE->SR &= ~TIMER_SR_CC1IF; //clear compare Flag sei(); Update_All(); if(IS_CHANGE_PROTOCOL_FLAG_on) break; // Protocol has been changed - while((TIMER2_BASE->SR &TIMER_SR_CC1IF)==0);//2ms wait + while((TIMER2_BASE->SR & TIMER_SR_CC1IF)==0);//2ms wait #else cli(); OCR1A+=2000*2; // clear compare A=callback flag - TIFR1=(1<CCR1 = OCR1A; - TCNT1 = TIMER2_BASE->CNT; TIMER2_BASE->SR &= ~TIMER_SR_CC1IF;//clear compare Flag write zero diff=OCR1A-TCNT1; // compare timer and comparator sei(); #else cli(); OCR1A+=next_callback; // set compare A for callback - TIFR1=(1<SR & TIMER_SR_CC1IF)!=0){ - cli(); - OCR1A = TIMER2_BASE->CNT; - TIMER2_BASE->CCR1=OCR1A; - sei(); - } - else - while((TIMER2_BASE->SR & TIMER_SR_CC1IF )==0);//walit till compare match - #else - if( (TIFR1 & (1<4000) - { // start to wait here as much as we can... - next_callback=next_callback-2000; - - #ifdef XMEGA - cli(); // disable global int - TCC1.CCA +=2000*2; // set compare A for callback - TCC1.INTFLAGS = TC1_CCAIF_bm ; // clear compare A=callback flag - sei(); // enable global int - while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait 2ms... - #else - - #if defined STM32_board - cli(); - OCR1A+=2000*2;// clear compare A=callback flag - TIMER2_BASE->CCR1=OCR1A; - TCNT1 = TIMER2_BASE->CNT; - TIMER2_BASE->SR &= ~TIMER_SR_CC1IF; //clear compare Flag - sei(); - while((TIMER2_BASE->SR &TIMER_SR_CC1IF)==0);//2ms wait - #else - cli(); - OCR1A+=2000*2; // clear compare A=callback flag - TIFR1=(1<CCR1 = OCR1A; - TCNT1 = TIMER2_BASE->CNT; - TIMER2_BASE->SR &= ~TIMER_SR_CC1IF;//clear compare Flag write zero - diff=OCR1A-TCNT1; // compare timer and comparator - sei(); - #else - cli(); - OCR1A+=next_callback; // set compare A for callback - TIFR1=(1<32000) { // next_callback should not be more than 32767 so we will wait here... uint16_t temp=(next_callback>>10)-2; - delay(temp); + delayMilliseconds(temp); next_callback-=temp<<10; // between 2-3ms left at this stage } - #ifdef XMEGA - cli(); // disable global int - 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 #if defined STM32_board - cli(); // disable global int - TCNT1 = TIMER2_BASE->CNT; + cli(); // disable global int OCR1A=TCNT1+next_callback*2; - TIMER2_BASE->CCR1 = OCR1A; sei(); TIMER2_BASE->SR &= ~TIMER_SR_CC1IF;//clear compare Flag write zero #else cli(); // disable global int - OCR1A=TCNT1+next_callback*2; // set compare A for callback + OCR1A = TCNT1 + next_callback*2; // set compare A for callback sei(); // enable global int - TIFR1=(1<CR1 &= ~ USART_CR1_RXNEIE;//disable - #else UCSR0B &= ~(1<CR1 |= USART_CR1_RXNEIE ;//disable - #else UCSR0B |= (1<CNT; - Cur_TCNT1=time-Prev_TCNT1; // Capture current Timer1 value - #else Cur_TCNT1=TCNT1-Prev_TCNT1; // Capture current Timer1 value - #endif - #endif + if(Cur_TCNT1<1000) chan=-1; // bad frame else @@ -1409,9 +1231,7 @@ extern "C" { #ifdef XMEGA if((USARTC0.STATUS & 0x1C)==0) // Check frame error, data overrun and parity error #else - #ifndef STM32_board UCSR0B &= ~_BV(RXCIE0) ; // RX interrupt disable - #endif sei(); #if defined STM32_board if(USART2_BASE->SR & USART_SR_RXNE) { @@ -1423,52 +1243,26 @@ extern "C" { { // received byte is ok to process if(idx==0||discard_frame==1) { // Let's try to sync at this point - idx=0;discard_frame=0; - #ifdef XMEGA - if(USARTC0.DATA==0x55) // If 1st byte is 0x55 it looks ok - - #else - #if defined STM32_board - if(USART2_BASE->DR==0x55) - #else + idx=0;discard_frame=0; if(UDR0==0x55) // If 1st byte is 0x55 it looks ok - #endif - #endif { - #ifdef XMEGA - TCC1.CCB = TCC1.CNT+(6500L) ; // Full message should be received within timer of 3250us - TCC1.INTFLAGS = TC1_CCBIF_bm ; // clear OCR1B match flag - TCC1.INTCTRLB = (TCC1.INTCTRLB & 0xF3) | 0x04 ; // enable interrupt on compare B match - - #else #if defined STM32_board - uint16_t OCR1B = TIMER2_BASE->CNT; - OCR1B +=6500L; + uint16_t OCR1B; + OCR1B =TCNT1+6500L; timer.setCompare(TIMER_CH2,OCR1B); timer.attachCompare2Interrupt(ISR_COMPB); #else OCR1B=TCNT1+6500L; // Full message should be received within timer of 3250us - TIFR1=(1<DR&0xff; // Store received byte - #else + RX_MISSED_BUFF_off; // if rx_buff was good it's not anymore... rx_buff[(idx++)-1]=UDR0; // Store received byte - #endif - #endif if(idx>RXBUFFER_SIZE) { // A full frame has been received @@ -1484,41 +1278,25 @@ extern "C" { } } else - { - #ifdef XMEGA - idx = USARTC0.DATA ; // Dummy read - #else - #if defined STM32_board - idx=USART2_BASE->DR&0xff; - #else + { idx=UDR0; // Dummy read - #endif - #endif discard_frame=1; // Error encountered discard full frame... } if(discard_frame==1) { - #ifdef XMEGA - TCC1.INTCTRLB &=0xF3; // disable interrupt on compare B match - #else #if defined STM32_board detachInterrupt(2);//disable interrupt on ch2 #else - TIMSK1 &=~(1<CC2500 MODE_J6PRO=22, // =>CYRF6936 MODE_FQ777=23, // =>NRF24L01 - MODE_ASSAN=24 // =>NRF24L01 + MODE_ASSAN=24, // =>NRF24L01 + MODE_FRSKY1 = 25 // =>CC2500 }; enum Flysky @@ -125,13 +126,29 @@ enum FRSKYX #define AUTOBIND 1 #define NO_AUTOBIND 0 - +struct PPM_Parameters +{ + uint8_t protocol : 5; + uint8_t sub_proto : 3; + uint8_t rx_num : 4; + uint8_t power : 1; + uint8_t autobind : 1; + uint8_t option; +}; //******************* //*** Pinouts *** //******************* #if defined STM32_board + +#define OCR1A TIMER2_BASE->CCR1 +#define TCNT1 TIMER2_BASE->CNT +#define UDR0 USART2_BASE->DR +#define UCSR0B USART2_BASE->CR1 +#define RXCIE0 USART_CR1_RXNEIE_BIT +#define TXCIE0 USART_CR1_TXEIE_BIT +//#define TIFR1 TIMER2_BASE->SR //******************** #define BIND_pin PA0 #define LED_pin PA1 @@ -172,6 +189,9 @@ enum FRSKYX #define CS_on digitalWrite(CS_pin,HIGH) #define CS_off digitalWrite(CS_pin,LOW) +#define NRF_CE_on +#define NRF_CE_off + #define SCK_on digitalWrite(SCK_pin,HIGH) #define SCK_off digitalWrite(SCK_pin,LOW) @@ -511,4 +531,4 @@ Serial: 100000 Baud 8e2 _ xxxx xxxx p -- 1843 +100% 2047 +125% Channels bits are concatenated to fit in 22 bytes like in SBUS protocol -*/ +*/ diff --git a/Multiprotocol/SPI.ino b/Multiprotocol/SPI.ino index ee080f9..e2602e4 100644 --- a/Multiprotocol/SPI.ino +++ b/Multiprotocol/SPI.ino @@ -5,7 +5,7 @@ based on arduino maple library */ - +#ifdef STM32_board SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port @@ -20,7 +20,7 @@ void initSPI2() { SPI_2.setClockDivider(SPI_CLOCK_DIV8); //// Slow speed (36 / 8 = 4.5 MHz SPI_2 speed) } - +#endif #ifdef XMEGA #define XNOP() NOP() diff --git a/Multiprotocol/Telemetry.ino b/Multiprotocol/Telemetry.ino index c96a190..c8b83c4 100644 --- a/Multiprotocol/Telemetry.ino +++ b/Multiprotocol/Telemetry.ino @@ -540,27 +540,19 @@ void initTXSerial( uint8_t speed) { if(++tx_tail>=TXBUFFER_SIZE)//head tx_tail=0; - #ifdef XMEGA - USARTC0.DATA = tx_buff[tx_tail] ; - #else #if defined STM32_board USART3_BASE->DR=tx_buff[tx_tail];//clears TXE bit #else UDR0=tx_buff[tx_tail]; #endif - #endif } if (tx_tail == tx_head) - #ifdef XMEGA - USARTC0.CTRLA &= ~0x03 ; - #else #if defined STM32_board USART3_BASE->CR1 &= ~USART_CR1_TXEIE;//disable interrupt } #else - UCSR0B &= ~(1<. */ + + +/**Board selection**/ +#define STM32_board +//#define XMEGA +/*******************/ +#ifdef STM32_board +//#undef __cplusplus +#include "Multiprotocol_STM32.h" +#include +#include +#include +#include +#else +#include +#include +#include "Multiprotocol.h" +#endif + + /** Multiprotocol module configuration file ***/ /*******************/ @@ -124,7 +144,7 @@ #define PPM_MIN_125 1000 // 125% #endif - +/* struct PPM_Parameters { uint8_t protocol : 5; @@ -134,7 +154,7 @@ struct PPM_Parameters uint8_t autobind : 1; uint8_t option; }; - +*/ //Update this table to set which protocol and all associated settings are called for the corresponding dial number const PPM_Parameters PPM_prot[15]= { // Dial Protocol Sub protocol RX_Num Power Auto Bind Option