From f689348bd6908d33d53bd1faca40e2e970d1d333 Mon Sep 17 00:00:00 2001 From: philipp schoenberger Date: Wed, 3 Oct 2018 19:57:55 +0200 Subject: [PATCH] add timeer init --- Multiprotocol/Multiprotocol.ino | 121 ++++++++++++++++++++++++++------ Multiprotocol/Pins.h | 73 ++++++++++--------- Multiprotocol/inputs.ino | 28 ++++---- 3 files changed, 153 insertions(+), 69 deletions(-) diff --git a/Multiprotocol/Multiprotocol.ino b/Multiprotocol/Multiprotocol.ino index 3827bd4..5c2d225 100644 --- a/Multiprotocol/Multiprotocol.ino +++ b/Multiprotocol/Multiprotocol.ino @@ -163,6 +163,9 @@ void setup() NRF_CSN_output; #endif + // Timer1 config + TCCR1A = 0; + TCCR1B = (1 << CS11); //prescaler8, set timer1 to increment every 0.5us(16Mhz) and start timer // Random //random_init(); @@ -244,6 +247,9 @@ void setup() // Set default channels' value InitChannel(); InitPPM(); + // Update LED + LED_off; + LED_output; //Init RF modules modules_reset(); @@ -355,7 +361,7 @@ void setup() void loop() { uint16_t next_callback,diff=0xFFFF; - debugln("%s:%d",__func__, __LINE__); + while(1) { if(remote_callback==0 || IS_WAIT_BIND_on || diff>2*200) @@ -365,34 +371,83 @@ void loop() Update_All(); } while(remote_callback==0 || IS_WAIT_BIND_on); - } - + } + #ifndef STM32_BOARD + if( (TIFR1 & OCF1A_bm) != 0) + { + 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. + sei(); // Enable global int + } + else + while((TIFR1 & OCF1A_bm) == 0); // Wait before callback + #else + if((TIMER2_BASE->SR & TIMER_SR_CC1IF)!=0) + { + debugln("Callback miss"); + cli(); + OCR1A = TCNT1; + sei(); + } + else + while((TIMER2_BASE->SR & TIMER_SR_CC1IF )==0); // Wait before callback + #endif do { TX_MAIN_PAUSE_on; tx_pause(); - unsigned long last_call = millis(); if(IS_INPUT_SIGNAL_on && remote_callback!=0) next_callback=remote_callback(); else next_callback=2000; // No PPM/serial signal check again in 2ms... - - unsigned long update_start = millis(); - update_inputs(); - unsigned long update_end = millis(); - - //debugln("diff = %d", update_end - update_start); - //next_abs_call = last_call + next_callback; - - #if 0 - { // next_callback should not be more than 32767 so we will wait here... - uint16_t temp=(next_callback>>10)-2; - delayMilliseconds(temp); - next_callback-=temp<<10; // between 2-3ms left at this stage + +#if 0 + // 15 < 250 + // 20 250 250 + if (next_callback > 4000) { + next_callback-=2000; + Update_All(); + delayMicroseconds(2000); } - #else - delayMicroseconds(next_callback); - #endif + delayMicroseconds(next_callback*2); +#else + TX_MAIN_PAUSE_off; + tx_resume(); + while(next_callback>4000) + { // start to wait here as much as we can... + next_callback-=2000; // We will wait below for 2ms + cli(); // Disable global int due to RW of 16 bits registers + OCR1A += 2000 *2; // set compare A for callback + #ifndef STM32_BOARD + TIFR1=OCF1A_bm; // clear compare A=callback flag + #else + TIMER2_BASE->SR = 0x1E5F & ~TIMER_SR_CC1IF; // Clear Timer2/Comp1 interrupt flag + #endif + sei(); // enable global int + if(Update_All()) // Protocol changed? + { + next_callback=0; // Launch new protocol ASAP + break; + } + #ifndef STM32_BOARD + while((TIFR1 & OCF1A_bm) == 0); // wait 2ms... + #else + while((TIMER2_BASE->SR & TIMER_SR_CC1IF)==0);//2ms wait + #endif + } + // at this point we have a maximum of 4ms in next_callback + next_callback *= 2 ; + cli(); // Disable global int due to RW of 16 bits registers + OCR1A+= next_callback ; // set compare A for callback + #ifndef STM32_BOARD + TIFR1=OCF1A_bm; // clear compare A=callback flag + #else + TIMER2_BASE->SR = 0x1E5F & ~TIMER_SR_CC1IF; // Clear Timer2/Comp1 interrupt flag + #endif + diff=OCR1A-TCNT1; // compare timer and comparator + sei(); // enable global int +#endif + //debugln("next %d diff %lu (%lu to %lu) diff %x ",org_next_call, end__ - start_, end__, start_, diff); } while(diff&0x8000); // Callback did not took more than requested time for next callback // so we can launch Update_All before next callback @@ -401,8 +456,23 @@ void loop() uint8_t Update_All() { + #ifdef ENABLE_SERIAL + #ifdef CHECK_FOR_BOOTLOADER + if ( (mode_select==MODE_SERIAL) && (NotBootChecking == 0) ) + pollBoot() ; + else + #endif + if(mode_select==MODE_SERIAL && IS_RX_FLAG_on) // Serial mode and something has been received + { + update_serial_data(); // Update protocol and data + update_channels_aux(); + INPUT_SIGNAL_on; //valid signal received + last_signal=millis(); + } + #endif //ENABLE_SERIAL #ifdef ENABLE_PPM - INPUT_SIGNAL_on; + //INPUT_SIGNAL_on; + update_inputs(); if(mode_select!=MODE_SERIAL && IS_PPM_FLAG_on) // PPM mode and a full frame has been received { debugln("%s:%d",__func__, __LINE__); @@ -648,11 +718,20 @@ static void protocol_init() WAIT_BIND_off; CHANGE_PROTOCOL_FLAG_off; + if(next_callback>32000) { // next_callback should not be more than 32767 so we will wait here... uint16_t temp=(next_callback>>10)-2; delayMilliseconds(temp); next_callback-=temp<<10; // between 2-3ms left at this stage } + cli(); // disable global int + OCR1A = TCNT1 + next_callback*2; // set compare A for callback + #ifndef STM32_BOARD + TIFR1 = OCF1A_bm ; // clear compare A flag + #else + TIMER2_BASE->SR = 0x1E5F & ~TIMER_SR_CC1IF; // Clear Timer2/Comp1 interrupt flag + #endif + sei(); // enable global int BIND_BUTTON_FLAG_off; // do not bind/reset id anymore even if protocol change debugln("%s BIND_BUTTON_FLAG_off",__func__); diff --git a/Multiprotocol/Pins.h b/Multiprotocol/Pins.h index 2e1adbe..c0200a4 100644 --- a/Multiprotocol/Pins.h +++ b/Multiprotocol/Pins.h @@ -13,41 +13,48 @@ //******************* //*** Pinouts *** //******************* -#if 0 - // SDIO MOSI - #define SDI_pin 2 //D5 = PD5 - #define SDI_port PORTB - #define SDI_ipr PINB - #define SDI_ddr DDRB - #define SDI_on SDI_port |= _BV(SDI_pin) - #define SDI_off SDI_port &= ~_BV(SDI_pin) - #define SDI_1 (SDI_ipr & _BV(SDI_pin)) - #define SDI_0 (SDI_ipr & _BV(SDI_pin)) == 0x00 - #define SDI_input SDI_ddr &= ~_BV(SDI_pin) - #define SDI_output SDI_ddr |= _BV(SDI_pin) +#define LED_off +#define LED_on +#define LED_output +#define IS_LED_on false +#define LED_toggle +#if 1 - //SDO / MISO - #define SDO_pin 3 //D6 = PD6 - #define SDO_port PORTB - #define SDO_ipr PINB - #define SDO_1 (SDO_ipr & _BV(SDO_pin)) - #define SDO_0 (SDO_ipr & _BV(SDO_pin)) == 0x00 + // SDIO MOSI + #define SDI_pin 16 + #define SDI_on digitalWrite(SDI_pin, HIGH) + #define SDI_off digitalWrite(SDI_pin, LOW) + #define SDI_1 (digitalRead(SDI_pin) == HIGH) + #define SDI_0 (digitalRead(SDI_pin) == LOW) + #define SDI_input pinMode(SDI_pin, INPUT); + #define SDI_output pinMode(SDI_pin, OUTPUT); + + //SDO / MISO + #define SDO_pin 14 + #define SDO_on digitalWrite(SDO_pin, HIGH) + #define SDO_off digitalWrite(SDO_pin, LOW) + #define SDO_1 (digitalRead(SDO_pin) == HIGH) + #define SDO_0 (digitalRead(SDO_pin) == LOW) + #define SDO_input pinMode(SDO_pin, INPUT); + #define SDO_output pinMode(SDO_pin, OUTPUT); - // SCLK - #define SCLK_port PORTB - #define SCLK_ddr DDRB - #define SCLK_pin 1 //D4 = PD4 - #define SCLK_output SCLK_ddr |= _BV(SCLK_pin) - #define SCLK_on SCLK_port |= _BV(SCLK_pin) - #define SCLK_off SCLK_port &= ~_BV(SCLK_pin) - - // Chip select CC2500 - #define CC25_CSN_pin 7 //D7 = PD7 - #define CC25_CSN_port PORTE - #define CC25_CSN_ddr DDRE - #define CC25_CSN_output CC25_CSN_ddr |= _BV(CC25_CSN_pin) - #define CC25_CSN_on CC25_CSN_port |= _BV(CC25_CSN_pin) - #define CC25_CSN_off CC25_CSN_port &= ~_BV(CC25_CSN_pin) + // SCLK + #define SCLK_pin 15 + #define SCLK_on digitalWrite(SCLK_pin, HIGH) + #define SCLK_off digitalWrite(SCLK_pin, LOW) + #define SCLK_1 (digitalRead(SCLK_pin) == HIGH) + #define SCLK_0 (digitalRead(SCLK_pin) == LOW) + #define SCLK_input pinMode(SCLK_pin, INPUT); + #define SCLK_output pinMode(SCLK_pin, OUTPUT); + + // Chip select CC2500 + #define CC25_CSN_pin 7 + #define CC25_CSN_on digitalWrite(CC25_CSN_pin, HIGH) + #define CC25_CSN_off digitalWrite(CC25_CSN_pin, LOW) + #define CC25_CSN_1 (digitalRead(CC25_CSN_pin) == HIGH) + #define CC25_CSN_0 (digitalRead(CC25_CSN_pin) == LOW) + #define CC25_CSN_input pinMode(CC25_CSN_pin, INPUT); + #define CC25_CSN_output pinMode(CC25_CSN_pin, OUTPUT); //******************* //*** Timer *** diff --git a/Multiprotocol/inputs.ino b/Multiprotocol/inputs.ino index 542b234..e04ffff 100644 --- a/Multiprotocol/inputs.ino +++ b/Multiprotocol/inputs.ino @@ -1,20 +1,18 @@ void update_inputs(void) { //analogRead() static int inc = 1; - #if 1 - if (inc > 0) { - if (Channel_data[THROTTLE] < CHANNEL_MIN_125 + 100) { - Channel_data[THROTTLE] += 1; - }else { - inc = -1; - } - }else { - if (Channel_data[THROTTLE] > CHANNEL_MIN_125) { - Channel_data[THROTTLE] -= 1; - }else { - inc = +1; - } - } -#endif + if (inc > 0) { + if (Channel_data[THROTTLE] < CHANNEL_MIN_125 + 100) { + Channel_data[THROTTLE] += 1; + }else { + inc = -1; + } + }else { + if (Channel_data[THROTTLE] > CHANNEL_MIN_125) { + Channel_data[THROTTLE] -= 1; + }else { + inc = +1; + } + } }