From 76937ea566ca3236c41fe7588107d362b5848317 Mon Sep 17 00:00:00 2001 From: bsongis Date: Wed, 28 Jan 2015 23:22:04 +0100 Subject: [PATCH] Fixes #1968 - I ported the last changes from Damjan in the same time. Please review (I push it anyway as there was a freeze before on startup when radio in Slave mode) --- radio/src/opentx.cpp | 2 +- radio/src/pulses/ppm_arm.cpp | 25 +++---- radio/src/targets/taranis/board_taranis.h | 5 ++ radio/src/targets/taranis/pulses_driver.cpp | 70 ++++++++++++++------ radio/src/targets/taranis/trainer_driver.cpp | 39 ++++++----- 5 files changed, 86 insertions(+), 55 deletions(-) diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 6c33a17c3..073bbf47f 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -2593,7 +2593,7 @@ void opentxInit(OPENTX_INIT_ARGS) uart3Init(g_eeGeneral.uart3Mode, MODEL_TELEMETRY_PROTOCOL()); #endif -#if defined(CPUARM) && !defined(SIMU) +#if defined(PCBSKY9X) && !defined(SIMU) init_trainer_capture(); #endif diff --git a/radio/src/pulses/ppm_arm.cpp b/radio/src/pulses/ppm_arm.cpp index 65b0ce3f1..38613f3e6 100644 --- a/radio/src/pulses/ppm_arm.cpp +++ b/radio/src/pulses/ppm_arm.cpp @@ -55,6 +55,7 @@ void setupPulsesPPM(unsigned int port) // Don't enable interru uint32_t lastCh = min(NUM_CHNOUT, firstCh + 8 + g_model.moduleData[port].channelsCount); #if defined(PCBSKY9X) + // TODO move register stuff to driver register Pwm *pwmptr = PWM; uint32_t pwmCh = (port == EXTERNAL_MODULE ? 3 : 1); pwmptr->PWM_CH_NUM[pwmCh].PWM_CDTYUPD = (g_model.moduleData[port].ppmDelay * 50 + 300) * 2; //Stoplen *2 @@ -78,25 +79,19 @@ void setupPulsesPPM(unsigned int port) // Don't enable interru *(ptr + 1) = 0; #if defined(PCBTARANIS) + rest -= 1000; + uint32_t ppmDelay = (g_model.moduleData[port].ppmDelay * 50 + 300) * 2; + // set idle time, ppm delay and ppm polarity if (port == TRAINER_MODULE) { - TIM3->CCR2 = rest - 1000 ; // Update time - TIM3->CCR4 = (g_model.moduleData[port].ppmDelay*50+300)*2; - if (!g_model.moduleData[TRAINER_MODULE].ppmPulsePol) - TIM3->CCER |= TIM_CCER_CC4P; - else - TIM3->CCER &= ~TIM_CCER_CC4P; + set_trainer_ppm_parameters(rest, ppmDelay, !g_model.moduleData[TRAINER_MODULE].ppmPulsePol); // ppmPulsePol: 0 - positive, 1 - negative } else if (port == EXTERNAL_MODULE) { - TIM8->CCR2 = rest - 1000; // Update time - TIM8->CCR1 = (g_model.moduleData[port].ppmDelay*50+300)*2; - if(!g_model.moduleData[EXTERNAL_MODULE].ppmPulsePol) - TIM8->CCER |= TIM_CCER_CC1NP; - else - TIM8->CCER &= ~TIM_CCER_CC1NP; + set_external_ppm_parameters(rest, ppmDelay, !g_model.moduleData[EXTERNAL_MODULE].ppmPulsePol); } - else { - TIM1->CCR2 = rest - 1000; // Update time - TIM1->CCR3 = (g_model.moduleData[port].ppmDelay*50+300)*2; +#if defined(TARANIS_INTERNAL_PPM) + else if (port == INTERNAL_MODULE) { + set_internal_ppm_parameters(rest, ppmDelay, !g_model.moduleData[INTERNAL_MODULE].ppmPulsePol); } +#endif // #if defined(TARANIS_INTERNAL_PPM) #endif } diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index 13210f665..3dce1d90b 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -150,6 +150,10 @@ void init_no_pulses(uint32_t port); void disable_no_pulses(uint32_t port); void init_ppm( uint32_t module_index ); void disable_ppm( uint32_t module_index ); +void set_external_ppm_parameters(uint32_t idleTime, uint32_t delay, uint32_t positive); +#if defined(TARANIS_INTERNAL_PPM) + void set_internal_ppm_parameters(uint32_t idleTime, uint32_t delay, uint32_t positive); +#endif void init_pxx( uint32_t module_index ); void disable_pxx( uint32_t module_index ); void init_dsm2( uint32_t module_index ); @@ -158,6 +162,7 @@ void disable_dsm2( uint32_t module_index ); // Trainer driver void init_trainer_ppm(void); void stop_trainer_ppm(void); +void set_trainer_ppm_parameters(uint32_t idleTime, uint32_t delay, uint32_t positive); void init_trainer_capture(void); void stop_trainer_capture(void); void init_cppm_on_heartbeat_capture(void); diff --git a/radio/src/targets/taranis/pulses_driver.cpp b/radio/src/targets/taranis/pulses_driver.cpp index 11e761033..b75cc37cf 100644 --- a/radio/src/targets/taranis/pulses_driver.cpp +++ b/radio/src/targets/taranis/pulses_driver.cpp @@ -47,8 +47,10 @@ extern uint16_t dsm2Stream[400]; static void init_pa10_pxx( void ) ; static void disable_pa10_pxx( void ) ; -static void init_pa10_ppm( void ) ; -static void disable_pa10_ppm( void ) ; +#if defined(TARANIS_INTERNAL_PPM) + static void init_pa10_ppm( void ) ; + static void disable_pa10_ppm( void ) ; +#endif static void init_pa7_pxx( void ) ; static void disable_pa7_pxx( void ) ; #if defined(DSM2) @@ -96,20 +98,45 @@ void disable_dsm2(uint32_t port) void init_ppm(uint32_t port) { - if (port == INTERNAL_MODULE) - init_pa10_ppm(); // TODO needed? - else + if (port == EXTERNAL_MODULE) { init_pa7_ppm(); + } +#if defined(TARANIS_INTERNAL_PPM) + else if (port == INTERNAL_MODULE) { + init_pa10_ppm(); + } +#endif } void disable_ppm(uint32_t port) { - if (port == INTERNAL_MODULE) - disable_pa10_ppm(); // TODO needed? - else + if (port == EXTERNAL_MODULE) { disable_pa7_ppm(); + } +#if defined(TARANIS_INTERNAL_PPM) + else if (port == INTERNAL_MODULE) { + disable_pa10_ppm(); + } +#endif } +void set_external_ppm_parameters(uint32_t idleTime, uint32_t delay, uint32_t positive) +{ + TIM8->CCR2 = idleTime; + TIM8->CCR1 = delay; + // we are using complementary output so logic has to be reversed here + TIM8->CCER = TIM_CCER_CC1NE | (positive ? 0 : TIM_CCER_CC1NP); +} + +#if defined(TARANIS_INTERNAL_PPM) +void set_internal_ppm_parameters(uint32_t idleTime, uint32_t delay, uint32_t positive) +{ + TIM1->CCR2 = idleTime; + TIM1->CCR3 = delay; + TIM1->CCER = TIM_CCER_CC3E | (positive ? TIM_CCER_CC3P : 0); +} +#endif + void init_no_pulses(uint32_t port) { if (port == INTERNAL_MODULE) @@ -283,6 +310,7 @@ static void disable_pa10_pxx() INTERNAL_RF_OFF(); } +#if defined(TARANIS_INTERNAL_PPM) // PPM output // Timer 1, channel 1 on PA8 for prototype // Pin is AF1 function for timer 1 @@ -290,7 +318,6 @@ static void init_pa10_ppm() { INTERNAL_RF_ON(); // Timer1 - setupPulsesPPM(INTERNAL_MODULE) ; ppmStreamPtr[INTERNAL_MODULE] = ppmStream[INTERNAL_MODULE]; //RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN ; // Enable portA clock @@ -299,8 +326,12 @@ static void init_pa10_ppm() configure_pins( PIN_INTPPM_OUT, PIN_PERIPHERAL | PIN_PORTA | PIN_PER_1 | PIN_OS25 | PIN_PUSHPULL ) ; RCC->APB2ENR |= RCC_APB2ENR_TIM1EN ; // Enable clock - TIM1->CR1 &= ~TIM_CR1_CEN ; + + // setupPulsesPPM() is also configuring registers, + // so it has to be called after the peripheral is enabled + setupPulsesPPM(INTERNAL_MODULE) ; + TIM1->ARR = *ppmStreamPtr[INTERNAL_MODULE]++ ; TIM1->PSC = (PERI2_FREQUENCY * TIMER_MULT_APB2) / 2000000 - 1 ; // 0.5uS from 30MHz @@ -308,7 +339,6 @@ static void init_pa10_ppm() TIM1->CCMR2 = TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3M_2 ; // PWM mode 1 TIM1->CCMR1 = TIM_CCMR1_OC2PE ; // PWM mode 1 - TIM1->CCR3 = (g_model.moduleData[INTERNAL_MODULE].ppmDelay*50+300)*2; TIM1->BDTR = TIM_BDTR_MOE ; TIM1->EGR = 1 ; TIM1->DIER = TIM_DIER_UDE ; @@ -334,6 +364,7 @@ static void disable_pa10_ppm() INTERNAL_RF_OFF(); } +#endif // #if defined(TARANIS_INTERNAL_PPM) #if !defined(SIMU) extern "C" void TIM1_CC_IRQHandler() @@ -535,7 +566,6 @@ static void init_pa7_ppm() EXTERNAL_MODULE_ON(); // Timer1 - setupPulsesPPM(EXTERNAL_MODULE) ; ppmStreamPtr[EXTERNAL_MODULE] = ppmStream[EXTERNAL_MODULE]; RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN ; // Enable portA clock @@ -545,20 +575,16 @@ static void init_pa7_ppm() configure_pins( PIN_EXTPPM_OUT, PIN_PERIPHERAL | PIN_PORTA | PIN_PER_3 | PIN_OS25 | PIN_PUSHPULL ) ; #endif RCC->APB2ENR |= RCC_APB2ENR_TIM8EN ; // Enable clock - TIM8->CR1 &= ~TIM_CR1_CEN ; - + + // setupPulsesPPM() is also configuring registers, + // so it has to be called after the peripheral is enabled + setupPulsesPPM(EXTERNAL_MODULE) ; + TIM8->ARR = *ppmStreamPtr[EXTERNAL_MODULE]++ ; TIM8->PSC = (PERI2_FREQUENCY * TIMER_MULT_APB2) / 2000000 - 1 ; // 0.5uS from 30MHz -#if defined(REV3) - TIM8->CCER = TIM_CCER_CC1E ; -#else - TIM8->CCER = TIM_CCER_CC1NE; - if(g_model.moduleData[EXTERNAL_MODULE].ppmPulsePol) - TIM8->CCER |= TIM_CCER_CC1NP; -#endif + TIM8->CCMR1 = TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC2PE ; // PWM mode 1 - TIM8->CCR1 = (g_model.moduleData[EXTERNAL_MODULE].ppmDelay*50+300)*2; TIM8->BDTR = TIM_BDTR_MOE ; TIM8->EGR = 1 ; TIM8->DIER = TIM_DIER_UDE ; diff --git a/radio/src/targets/taranis/trainer_driver.cpp b/radio/src/targets/taranis/trainer_driver.cpp index d2394affa..267e07463 100644 --- a/radio/src/targets/taranis/trainer_driver.cpp +++ b/radio/src/targets/taranis/trainer_driver.cpp @@ -45,28 +45,27 @@ extern Fifo<32> sbusFifo; // Trainer PPM oputput PC9, Timer 3 channel 4, (Alternate Function 2) void init_trainer_ppm() { - setupTrainerPulses() ; TrainerPulsePtr = ppmStream[TRAINER_MODULE]; RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN ; // Enable portC clock configure_pins( PIN_TR_PPM_OUT, PIN_PERIPHERAL | PIN_PORTC | PIN_PER_2 | PIN_OS25 | PIN_PUSHPULL ) ; configure_pins( PIN_TR_PPM_IN, PIN_PORTA | PIN_INPUT ) ; RCC->APB1ENR |= RCC_APB1ENR_TIM3EN ; // Enable clock + TIM3->CR1 &= ~TIM_CR1_CEN ; + + // setupTrainerPulses() is also configuring registers, + // so it has to be called after the peripheral is enabled + setupTrainerPulses() ; TIM3->ARR = *TrainerPulsePtr++ ; TIM3->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 2000000 - 1 ; // 0.5uS - TIM3->CCER = TIM_CCER_CC4E ; - if(!g_model.moduleData[TRAINER_MODULE].ppmPulsePol) - TIM3->CCER |= TIM_CCER_CC4P; TIM3->CCMR2 = TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE ; // PWM mode 1 - TIM3->CCR4 = (g_model.moduleData[TRAINER_MODULE].ppmDelay*50+300)*2; TIM3->BDTR = TIM_BDTR_MOE ; TIM3->EGR = 1 ; - // TIM3->DIER = TIM_DIER_UDE ; TIM3->SR &= ~TIM_SR_UIF ; // Clear flag - TIM3->SR &= ~TIM_SR_CC2IF ; // Clear flag - TIM3->DIER |= TIM_DIER_CC2IE ; + TIM3->SR &= ~TIM_SR_CC1IF ; // Clear flag + TIM3->DIER |= TIM_DIER_CC1IE ; TIM3->DIER |= TIM_DIER_UIE ; TIM3->CR1 = TIM_CR1_CEN ; @@ -78,11 +77,18 @@ void init_trainer_ppm() void stop_trainer_ppm() { configure_pins( PIN_TR_PPM_OUT, PIN_INPUT | PIN_PORTC ) ; // Pin as input - TIM3->CR1 &= ~TIM_CR1_CEN ; // Stop counter TIM3->DIER = 0 ; // Stop Interrupt + TIM3->CR1 &= ~TIM_CR1_CEN ; // Stop counter NVIC_DisableIRQ(TIM3_IRQn) ; // Stop Interrupt } +void set_trainer_ppm_parameters(uint32_t idleTime, uint32_t delay, uint32_t positive) +{ + TIM3->CCR1 = idleTime; + TIM3->CCR4 = delay; + TIM3->CCER = TIM_CCER_CC4E | (positive ? TIM_CCER_CC4P : 0); +} + // Trainer capture, PC8, Timer 3 channel 3 void init_trainer_capture() { @@ -98,8 +104,8 @@ void init_trainer_capture() TIM3->SR &= ~TIM_SR_CC3IF & ~TIM_SR_CC2IF & ~TIM_SR_UIF ; // Clear flags TIM3->DIER |= TIM_DIER_CC3IE ; TIM3->CR1 = TIM_CR1_CEN ; - NVIC_SetPriority(TIM3_IRQn, 7); NVIC_EnableIRQ(TIM3_IRQn) ; + NVIC_SetPriority(TIM3_IRQn, 7); } void stop_trainer_capture() @@ -124,7 +130,7 @@ extern "C" void TIM3_IRQHandler() doCapture = true; } - if ( (TIM3->DIER & TIM_DIER_CC2IE ) && ( TIM3->SR & TIM_SR_CC2IF ) ) { + if ( ( TIM3->DIER & TIM_DIER_CC2IE ) && ( TIM3->SR & TIM_SR_CC2IF ) ) { // capture mode on heartbeat pin (external module) capture = TIM3->CCR2 ; doCapture = true ; @@ -153,11 +159,10 @@ extern "C" void TIM3_IRQHandler() } // PPM out compare interrupt - if ( ( TIM3->DIER & TIM_DIER_CC2IE ) && ( TIM3->SR & TIM_SR_CC2IF ) ) { + if ( ( TIM3->DIER & TIM_DIER_CC1IE ) && ( TIM3->SR & TIM_SR_CC1IF ) ) { // compare interrupt - TIM3->SR &= ~TIM_SR_CC2IF ; // Clear flag - TIM3->DIER &= ~TIM_DIER_CC2IE ; // stop this interrupt - TIM3->SR &= ~TIM_SR_CC2IF ; // Clear flag + TIM3->DIER &= ~TIM_DIER_CC1IE ; // stop this interrupt + TIM3->SR &= ~TIM_SR_CC1IF ; // Clear flag setupTrainerPulses() ; @@ -172,8 +177,8 @@ extern "C" void TIM3_IRQHandler() TIM3->SR &= ~TIM_SR_UIF ; // Clear flag TIM3->ARR = *TrainerPulsePtr++ ; if ( *TrainerPulsePtr == 0 ) { - TIM3->SR &= ~TIM_SR_CC2IF ; // Clear this flag - TIM3->DIER |= TIM_DIER_CC2IE ; // Enable this interrupt + TIM3->SR &= ~TIM_SR_CC1IF ; // Clear this flag + TIM3->DIER |= TIM_DIER_CC1IE ; // Enable this interrupt } } }