Timer configs for boost and vvt on Teensy

This commit is contained in:
Josh Stewart 2018-04-03 17:47:33 +10:00
parent 915c6b9107
commit 6df7e4c345
2 changed files with 62 additions and 6 deletions

View File

@ -39,7 +39,21 @@ void initialiseAuxPWM()
TCCR1A = 0x00; //Timer1 Control Reg A: Wave Gen Mode normal (Simply counts up from 0 to 65535 (16-bit int)
TCCR1B = (1 << CS12); //Timer1 Control Reg B: Timer Prescaler set to 256. 1 tick = 16uS. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg
#elif defined(CORE_TEENSY)
//REALLY NEED TO DO THIS!
//FlexTimer 1 is used for boost and VVT. There are 8 channels on this module
FTM1_MODE |= FTM_MODE_WPDIS; // Write Protection Disable
FTM1_MODE |= FTM_MODE_FTMEN; //Flex Timer module enable
FTM1_MODE |= FTM_MODE_INIT;
FTM1_SC |= FTM_SC_CLKS(0b1); // Set internal clocked
FTM1_SC |= FTM_SC_PS(0b111); //Set prescaler to 128 (2.1333uS tick time)
//Enable each compare channel individually
FTM1_C0SC &= ~FTM_CSC_MSB; //According to Pg 965 of the K64 datasheet, this should not be needed as MSB is reset to 0 upon reset, but the channel interrupt fails to fire without it
FTM1_C0SC |= FTM_CSC_MSA; //Enable Compare mode
FTM1_C0SC |= FTM_CSC_CHIE; //Enable channel compare interrupt
FTM1_C1SC &= ~FTM_CSC_MSB; //According to Pg 965 of the K64 datasheet, this should not be needed as MSB is reset to 0 upon reset, but the channel interrupt fails to fire without it
FTM1_C1SC |= FTM_CSC_MSA; //Enable Compare mode
FTM1_C1SC |= FTM_CSC_CHIE; //Enable channel compare interrupt
#endif
boost_pin_port = portOutputRegister(digitalPinToPort(pinBoost));
@ -47,14 +61,13 @@ void initialiseAuxPWM()
vvt_pin_port = portOutputRegister(digitalPinToPort(pinVVT_1));
vvt_pin_mask = digitalPinToBitMask(pinVVT_1);
#if defined(CORE_STM32) //2uS resolution Min 8Hz, Max 5KHz
boost_pwm_max_count = 1000000L / (configPage6.boostFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle. The x2 is there because the frequency is stored at half value (in a byte) to allow freqneucies up to 511Hz
vvt_pwm_max_count = 1000000L / (configPage6.vvtFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle
#if defined(CORE_STM32) || defined(CORE_TEENSY) //2uS resolution Min 8Hz, Max 5KHz
boost_pwm_max_count = 1000000L / (2 * configPage6.boostFreq * 2); //Converts the frequency in Hz to the number of ticks (at 2uS) it takes to complete 1 cycle. The x2 is there because the frequency is stored at half value (in a byte) to allow freqneucies up to 511Hz
vvt_pwm_max_count = 1000000L / (2 * configPage6.vvtFreq * 2); //Converts the frequency in Hz to the number of ticks (at 2uS) it takes to complete 1 cycle
#else
boost_pwm_max_count = 1000000L / (16 * configPage6.boostFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle. The x2 is there because the frequency is stored at half value (in a byte) to allow freqneucies up to 511Hz
vvt_pwm_max_count = 1000000L / (16 * configPage6.vvtFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle
#endif
//TIMSK1 |= (1 << OCIE1A); <---- Not required as compare A is turned on when needed by boost control
ENABLE_VVT_TIMER(); //Turn on the B compare unit (ie turn on the interrupt)
boostPID.SetOutputLimits(configPage2.boostMinDuty, configPage2.boostMaxDuty);
@ -126,7 +139,7 @@ void boostControl()
}
}
else { // Disable timer channel and zero the flex boost correction status
DISABLE_BOOST_TIMER();
DISABLE_BOOST_TIMER();
currentStatus.flexBoostCorrection = 0;
}
@ -214,3 +227,17 @@ void boostDisable()
vvt_pwm_state = true;
}
}
#if defined(CORE_TEENSY)
void ftm1_isr(void)
{
//FTM1 only has 2 compare channels
//Use separate variables for each test to ensure conversion to bool
bool interrupt1 = (FTM1_C0SC & FTM_CSC_CHF);
bool interrupt2 = (FTM1_C1SC & FTM_CSC_CHF);
if(interrupt1) { FTM1_C0SC &= ~FTM_CSC_CHF; boostInterrupt(); }
else if(interrupt2) { FTM1_C1SC &= ~FTM_CSC_CHF; vvtInterrupt(); }
}
#endif

View File

@ -1344,5 +1344,34 @@ void ftm0_isr(void)
else if(interrupt7) { FTM0_C6SC &= ~FTM_CSC_CHF; ignitionSchedule3Interrupt(); }
else if(interrupt8) { FTM0_C7SC &= ~FTM_CSC_CHF; ignitionSchedule4Interrupt(); }
}
void ftm3_isr(void)
{
#if (INJ_CHANNELS >= 5)
if(bool(FTM3_C0SC & FTM_CSC_CHF)) { FTM3_C0SC &= ~FTM_CSC_CHF; fuelSchedule5Interrupt(); }
#endif
#if (INJ_CHANNELS >= 6)
else if(bool(FTM3_C1SC & FTM_CSC_CHF)) { FTM3_C1SC &= ~FTM_CSC_CHF; fuelSchedule6Interrupt(); }
#endif
#if (INJ_CHANNELS >= 7)
else if(bool(FTM3_C2SC & FTM_CSC_CHF)) { FTM3_C2SC &= ~FTM_CSC_CHF; fuelSchedule7Interrupt(); }
#endif
#if (INJ_CHANNELS >= 8)
else if(bool(FTM3_C3SC & FTM_CSC_CHF)) { FTM3_C3SC &= ~FTM_CSC_CHF; fuelSchedule8Interrupt(); }
#endif
#if (IGN_CHANNELS >= 5)
if(bool(FTM3_C4SC & FTM_CSC_CHF)) { FTM3_C4SC &= ~FTM_CSC_CHF; ignitionSchedule5Interrupt(); }
#endif
#if (IGN_CHANNELS >= 6)
else if(bool(FTM3_C5SC & FTM_CSC_CHF)) { FTM3_C5SC &= ~FTM_CSC_CHF; ignitionSchedule6Interrupt(); }
#endif
#if (IGN_CHANNELS >= 7)
else if(bool(FTM3_C6SC & FTM_CSC_CHF)) { FTM3_C6SC &= ~FTM_CSC_CHF; ignitionSchedule7Interrupt(); }
#endif
#if (IGN_CHANNELS >= 8)
else if(bool(FTM3_C7SC & FTM_CSC_CHF)) { FTM3_C7SC &= ~FTM_CSC_CHF; ignitionSchedule8Interrupt(); }
#endif
}
#endif