From eabfd8b5c4544d2411e221689ab2385a4c317e6d Mon Sep 17 00:00:00 2001 From: pascallanger Date: Wed, 31 Aug 2016 10:26:27 +0200 Subject: [PATCH] Important changes of the scheduler and interrupts --- Multiprotocol/FrSkyX_cc2500.ino | 1 - Multiprotocol/Multiprotocol.h | 25 ++- Multiprotocol/Multiprotocol.ino | 367 ++++++++++++++++++-------------- Multiprotocol/Telemetry.ino | 9 +- 4 files changed, 233 insertions(+), 169 deletions(-) diff --git a/Multiprotocol/FrSkyX_cc2500.ino b/Multiprotocol/FrSkyX_cc2500.ino index ba5dfd5..3820432 100644 --- a/Multiprotocol/FrSkyX_cc2500.ino +++ b/Multiprotocol/FrSkyX_cc2500.ino @@ -252,7 +252,6 @@ uint16_t ReadFrSkyX() CC2500_WriteReg(CC2500_0C_FSCTRL0,option); // Frequency offset hack prev_option = option ; } - LED_ON; CC2500_SetTxRxMode(TX_EN); set_start(hopping_frequency_no); CC2500_SetPower(); diff --git a/Multiprotocol/Multiprotocol.h b/Multiprotocol/Multiprotocol.h index 76840fd..220bbc6 100644 --- a/Multiprotocol/Multiprotocol.h +++ b/Multiprotocol/Multiprotocol.h @@ -273,10 +273,17 @@ struct PPM_Parameters #endif // TX -#define TX_ON PORTD |= _BV(1) -#define TX_OFF PORTD &= ~_BV(1) -#define TX_TOGGLE PORTD ^= _BV(1) -#define TX_SET_OUTPUT DDRD |= _BV(1) +#ifdef DEBUG_TX + #define TX_ON PORTD |= _BV(1) + #define TX_OFF PORTD &= ~_BV(1) + #define TX_TOGGLE PORTD ^= _BV(1) + #define TX_SET_OUTPUT DDRD |= _BV(1) +#else + #define TX_ON + #define TX_OFF + #define TX_TOGGLE + #define TX_SET_OUTPUT +#endif // Macros #define NOP() __asm__ __volatile__("nop") @@ -329,6 +336,16 @@ struct PPM_Parameters #define RX_MISSED_BUFF_on protocol_flags2 |= _BV(2) #define IS_RX_MISSED_BUFF_on ( ( protocol_flags2 & _BV(2) ) !=0 ) +#define TX_MAIN_PAUSE_off protocol_flags2 &= ~_BV(3) +#define TX_MAIN_PAUSE_on protocol_flags2 |= _BV(3) +#define IS_TX_MAIN_PAUSE_on ( ( protocol_flags2 & _BV(3) ) !=0 ) + +#define TX_RX_PAUSE_off protocol_flags2 &= ~_BV(4) +#define TX_RX_PAUSE_on protocol_flags2 |= _BV(4) +#define IS_TX_RX_PAUSE_on ( ( protocol_flags2 & _BV(4) ) !=0 ) + +#define IS_TX_PAUSE_on ( ( protocol_flags2 & (_BV(4)|_BV(3)) ) !=0 ) + #define BLINK_BIND_TIME 100 #define BLINK_SERIAL_TIME 500 #define BLINK_BAD_PROTO_TIME_LOW 1000 diff --git a/Multiprotocol/Multiprotocol.ino b/Multiprotocol/Multiprotocol.ino index 2e4a37d..288af61 100644 --- a/Multiprotocol/Multiprotocol.ino +++ b/Multiprotocol/Multiprotocol.ino @@ -22,9 +22,8 @@ */ #include #include -#include "Multiprotocol.h" - //#define DEBUG_TX +#include "Multiprotocol.h" //Multiprotocol module configuration file #include "_Config.h" @@ -113,6 +112,7 @@ uint8_t prev_protocol=0; #define MAX_PKT 27 uint8_t pkt[MAX_PKT];//telemetry receiving packets #if defined(TELEMETRY) + uint8_t pass = 0; uint8_t pktt[MAX_PKT];//telemetry receiving packets #ifndef BASH_SERIAL volatile uint8_t tx_head=0; @@ -215,19 +215,8 @@ void setup() MProtocol_id_master=random_id(10,false); //Init RF modules - #ifdef CC2500_INSTALLED - CC2500_Reset(); - #endif - #ifdef A7105_INSTALLED - A7105_Reset(); - #endif - #ifdef CYRF6936_INSTALLED - CYRF_Reset(); - #endif - #ifdef NFR24L01_INSTALLED - NRF24L01_Reset(); - #endif - + modules_reset(); + #ifdef ENABLE_PPM //Protocol and interrupts initialization if(mode_select != MODE_SERIAL) @@ -271,8 +260,99 @@ void setup() } // Main +// Protocol scheduler void loop() +{ + uint16_t next_callback,diff=0xFFFF; + + while(1) + { + if(remote_callback==0 || diff>2*200) + { + do + { + Update_All(); + } + while(remote_callback==0); + } + #ifdef XMEGA + 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 & (1<4000) + { // start to wait here as much as we can... + 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 + OCR1A += 2000*2 ; // set compare A for callback + TIFR1=(1<4000) - { // start to wait here as much as we can... - 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 - while((TCC1.INTFLAGS & TC1_CCAIF_bm) == 0); // wait 2ms... -#else - cli(); // Disable global int due to RW of 16 bits registers - OCR1A += 2000*2 ; // set compare A for callback - TIFR1=(1<RXBUFFER_SIZE) { // A full frame has been received -#ifdef XMEGA - TCC1.INTCTRLB &=0xF3; // disable interrupt on compare B match -#else - TIMSK1 &=~(1<