Configuration of PWM for idle, boost and vvt for Teensy4.1 (Take2) (#929)

* Fix VSS not resetting to zero

Reinstated car stop check to avoid  speed not going back to zero.

* Better logic for gear detection

Previous gear detection logic had gears constantly lagging 1 shift behind.

* Update sensors.h

* Update sensors.ino

* Update sensors.ino

* Update sensors.h

* Update board_teensy41.h

Pit timers implementation for idle, boost and vvt

* Update board_teensy41.ino

Pit timers implementation for idle, boost and vvt

* Update board_teensy41.h

Error in fuel5-8 timer disable now corrected

* Update auxiliaries.ino

Enable vvt PWM for Teensy 4.1

* Update idle.ino

* Update board_teensy41.h

For some reason the PIT_LDVAL compares were not showing on  the previous PR

* Update auxiliaries.ino
This commit is contained in:
rafolg 2022-09-15 09:58:37 +10:00 committed by GitHub
parent 659eb8ec0f
commit a40971dde1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 21 deletions

View File

@ -417,8 +417,10 @@ void initialiseAuxPWM()
#if defined(CORE_AVR)
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. Note that the frequency is divided by 2 coming from TS to allow for up to 512hz
#elif defined(CORE_TEENSY)
#elif defined(CORE_TEENSY35)
vvt_pwm_max_count = 1000000L / (32 * configPage6.vvtFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle. Note that the frequency is divided by 2 coming from TS to allow for up to 512hz
#elif defined(CORE_TEENSY41)
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. Note that the frequency is divided by 2 coming fro TS to allow for up to 512hz
#endif
if(configPage6.vvtMode == VVT_MODE_CLOSED_LOOP)
@ -449,8 +451,10 @@ void initialiseAuxPWM()
// config wmi pwm output to use vvt output
#if defined(CORE_AVR)
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. Note that the frequency is divided by 2 coming from TS to allow for up to 512hz
#elif defined(CORE_TEENSY)
#elif defined(CORE_TEENSY35)
vvt_pwm_max_count = 1000000L / (32 * configPage6.vvtFreq * 2); //Converts the frequency in Hz to the number of ticks (at 16uS) it takes to complete 1 cycle. Note that the frequency is divided by 2 coming from TS to allow for up to 512hz
#elif defined(CORE_TEENSY41)
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. Note that the frequency is divided by 2 coming from TS to allow for up to 512hz
#endif
BIT_CLEAR(currentStatus.status4, BIT_STATUS4_WMI_EMPTY);
currentStatus.wmiPW = 0;
@ -1012,13 +1016,21 @@ void boostDisable()
{
if (boost_pwm_state == true)
{
#if defined(CORE_TEENSY41) //PIT TIMERS count down and have opposite effect on PWM
BOOST_PIN_HIGH();
#else
BOOST_PIN_LOW(); // Switch pin to low
#endif
SET_COMPARE(BOOST_TIMER_COMPARE, BOOST_TIMER_COUNTER + (boost_pwm_max_count - boost_pwm_cur_value) );
boost_pwm_state = false;
}
else
{
#if defined(CORE_TEENSY41) //PIT TIMERS count down and have opposite effect on PWM
BOOST_PIN_LOW();
#else
BOOST_PIN_HIGH(); // Switch pin high
#endif
SET_COMPARE(BOOST_TIMER_COMPARE, BOOST_TIMER_COUNTER + boost_pwm_target_value);
boost_pwm_cur_value = boost_pwm_target_value;
boost_pwm_state = true;
@ -1036,12 +1048,20 @@ void boostDisable()
{
if( (vvt1_pwm_value > 0) && (vvt1_max_pwm == false) ) //Don't toggle if at 0%
{
#if defined(CORE_TEENSY41)
VVT1_PIN_OFF();
#else
VVT1_PIN_ON();
#endif
vvt1_pwm_state = true;
}
if( (vvt2_pwm_value > 0) && (vvt2_max_pwm == false) ) //Don't toggle if at 0%
{
#if defined(CORE_TEENSY41)
VVT2_PIN_OFF();
#else
VVT2_PIN_ON();
#endif
vvt2_pwm_state = true;
}
@ -1068,7 +1088,11 @@ void boostDisable()
{
if(vvt1_pwm_value < (long)vvt_pwm_max_count) //Don't toggle if at 100%
{
#if defined(CORE_TEENSY41)
VVT1_PIN_ON();
#else
VVT1_PIN_OFF();
#endif
vvt1_pwm_state = false;
vvt1_max_pwm = false;
}
@ -1085,7 +1109,11 @@ void boostDisable()
{
if(vvt2_pwm_value < (long)vvt_pwm_max_count) //Don't toggle if at 100%
{
#if defined(CORE_TEENSY41)
VVT2_PIN_ON();
#else
VVT2_PIN_OFF();
#endif
vvt2_pwm_state = false;
vvt2_max_pwm = false;
}
@ -1102,7 +1130,11 @@ void boostDisable()
{
if(vvt1_pwm_value < (long)vvt_pwm_max_count) //Don't toggle if at 100%
{
#if defined(CORE_TEENSY41)
VVT1_PIN_ON();
#else
VVT1_PIN_OFF();
#endif
vvt1_pwm_state = false;
vvt1_max_pwm = false;
SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt1_pwm_cur_value) );
@ -1110,7 +1142,11 @@ void boostDisable()
else { vvt1_max_pwm = true; }
if(vvt2_pwm_value < (long)vvt_pwm_max_count) //Don't toggle if at 100%
{
#if defined(CORE_TEENSY41)
VVT2_PIN_ON();
#else
VVT2_PIN_OFF();
#endif
vvt2_pwm_state = false;
vvt2_max_pwm = false;
SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt2_pwm_cur_value) );

View File

@ -92,10 +92,10 @@
#define FUEL2_TIMER_DISABLE() TMR1_CSCTRL1 &= ~TMR_CSCTRL_TCF1EN
#define FUEL3_TIMER_DISABLE() TMR1_CSCTRL2 &= ~TMR_CSCTRL_TCF1EN
#define FUEL4_TIMER_DISABLE() TMR1_CSCTRL3 &= ~TMR_CSCTRL_TCF1EN
#define FUEL5_TIMER_DISABLE() TMR1_CSCTRL0 &= ~TMR_CSCTRL_TCF1EN
#define FUEL6_TIMER_DISABLE() TMR1_CSCTRL1 &= ~TMR_CSCTRL_TCF1EN
#define FUEL7_TIMER_DISABLE() TMR1_CSCTRL2 &= ~TMR_CSCTRL_TCF1EN
#define FUEL8_TIMER_DISABLE() TMR1_CSCTRL3 &= ~TMR_CSCTRL_TCF1EN
#define FUEL5_TIMER_DISABLE() TMR3_CSCTRL0 &= ~TMR_CSCTRL_TCF1EN
#define FUEL6_TIMER_DISABLE() TMR3_CSCTRL1 &= ~TMR_CSCTRL_TCF1EN
#define FUEL7_TIMER_DISABLE() TMR3_CSCTRL2 &= ~TMR_CSCTRL_TCF1EN
#define FUEL8_TIMER_DISABLE() TMR3_CSCTRL3 &= ~TMR_CSCTRL_TCF1EN
#define IGN1_TIMER_ENABLE() TMR2_CSCTRL0 |= TMR_CSCTRL_TCF1EN
#define IGN2_TIMER_ENABLE() TMR2_CSCTRL1 |= TMR_CSCTRL_TCF1EN
@ -129,12 +129,13 @@
***********************************************************************************************************
* Auxiliaries
*/
#define ENABLE_BOOST_TIMER() TMR3_CSCTRL0 |= TMR_CSCTRL_TCF1EN
#define DISABLE_BOOST_TIMER() TMR3_CSCTRL0 &= ~TMR_CSCTRL_TCF1EN
#define ENABLE_BOOST_TIMER() PIT_TCTRL1 |= PIT_TCTRL_TEN
#define DISABLE_BOOST_TIMER() PIT_TCTRL1 &= ~PIT_TCTRL_TEN
#define ENABLE_VVT_TIMER() TMR3_CSCTRL0 |= TMR_CSCTRL_TCF2EN
#define DISABLE_VVT_TIMER() TMR3_CSCTRL0 &= ~TMR_CSCTRL_TCF2EN
#define ENABLE_VVT_TIMER() PIT_TCTRL2 |= PIT_TCTRL_TEN
#define DISABLE_VVT_TIMER() PIT_TCTRL2 &= ~PIT_TCTRL_TEN
//Ran out of timers, this most likely won't work
#define ENABLE_FAN_TIMER() TMR3_CSCTRL1 |= TMR_CSCTRL_TCF2EN
#define DISABLE_FAN_TIMER() TMR3_CSCTRL1 &= ~TMR_CSCTRL_TCF2EN
@ -142,13 +143,11 @@
#define BOOST_TIMER_COUNTER 0
#define VVT_TIMER_COMPARE PIT_LDVAL2
#define VVT_TIMER_COUNTER 0
//these probaply need to be PIT_LDVAL something???
#define FAN_TIMER_COMPARE TMR3_COMP22
#define FAN_TIMER_COUNTER TMR3_CNTR1
void boostInterrupt();
void vvtInterrupt();
/*
***********************************************************************************************************
* Idle
@ -156,10 +155,8 @@
#define IDLE_COUNTER 0
#define IDLE_COMPARE PIT_LDVAL0
#define IDLE_TIMER_ENABLE() TMR3_CSCTRL1 |= TMR_CSCTRL_TCF1EN
#define IDLE_TIMER_DISABLE() TMR3_CSCTRL1 &= ~TMR_CSCTRL_TCF1EN
void idleInterrupt();
#define IDLE_TIMER_ENABLE() PIT_TCTRL0 |= PIT_TCTRL_TEN
#define IDLE_TIMER_DISABLE() PIT_TCTRL0 &= ~PIT_TCTRL_TEN
/*
***********************************************************************************************************

View File

@ -40,6 +40,7 @@ void initBoard()
PIT_TCTRL0 = 0;
PIT_TCTRL0 |= PIT_TCTRL_TIE; // enable Timer 1 interrupts
PIT_TCTRL0 |= PIT_TCTRL_TEN; // start Timer 1
PIT_LDVAL0 = 1; //1 * 2uS = 2uS
}
/*
@ -59,6 +60,20 @@ void initBoard()
***********************************************************************************************************
* Auxiliaries
*/
if (configPage6.boostEnabled == 1)
{
PIT_TCTRL1 = 0;
PIT_TCTRL1 |= PIT_TCTRL_TIE; // enable Timer 2 interrupts
PIT_TCTRL1 |= PIT_TCTRL_TEN; // start Timer 2
PIT_LDVAL1 = 1; //1 * 2uS = 2uS
}
if (configPage6.vvtEnabled == 1)
{
PIT_TCTRL2 = 0;
PIT_TCTRL2 |= PIT_TCTRL_TIE; // enable Timer 3 interrupts
PIT_TCTRL2 |= PIT_TCTRL_TEN; // start Timer 3
PIT_LDVAL2 = 1; //1 * 2uS = 2uS
}
//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 frequencies up to 511Hz
@ -190,10 +205,10 @@ void PIT_isr()
bool interrupt3 = (PIT_TFLG2 & PIT_TFLG_TIF);
bool interrupt4 = (PIT_TFLG3 & PIT_TFLG_TIF);
if(interrupt1) { PIT_TFLG0 = 1;}
else if(interrupt2) { PIT_TFLG1 = 1;}
else if(interrupt3) { PIT_TFLG2 = 1;}
else if(interrupt4) { oneMSInterval(); PIT_TFLG3 = 1;}
if(interrupt1) { idleInterrupt(); PIT_TFLG0 = 1;}
else if(interrupt2) { boostInterrupt(); PIT_TFLG1 = 1;}
else if(interrupt3) { vvtInterrupt(); PIT_TFLG2 = 1;}
else if(interrupt4) { oneMSInterval(); PIT_TFLG3 = 1;}
}
void TMR1_isr(void)

View File

@ -851,14 +851,24 @@ void idleInterrupt() //Most ARM chips can simply call a function
if (configPage6.iacPWMdir == 0)
{
//Normal direction
#if defined (CORE_TEENSY41) //PIT TIMERS count down and have opposite effect on PWM
IDLE_PIN_HIGH();
if(configPage6.iacChannels == 1) { IDLE2_PIN_LOW(); }
#else
IDLE_PIN_LOW(); // Switch pin to low (1 pin mode)
if(configPage6.iacChannels == 1) { IDLE2_PIN_HIGH(); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1
#endif
}
else
{
//Reversed direction
#if defined (CORE_TEENSY41) //PIT TIMERS count down and have opposite effect on PWM
IDLE_PIN_LOW();
if(configPage6.iacChannels == 1) { IDLE2_PIN_HIGH(); }
#else
IDLE_PIN_HIGH(); // Switch pin high
if(configPage6.iacChannels == 1) { IDLE2_PIN_LOW(); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1
#endif
}
SET_COMPARE(IDLE_COMPARE, IDLE_COUNTER + (idle_pwm_max_count - idle_pwm_cur_value) );
idle_pwm_state = false;
@ -868,14 +878,24 @@ void idleInterrupt() //Most ARM chips can simply call a function
if (configPage6.iacPWMdir == 0)
{
//Normal direction
#if defined (CORE_TEENSY41) //PIT TIMERS count down and have opposite effect on PWM
IDLE_PIN_LOW();
if(configPage6.iacChannels == 1) { IDLE2_PIN_HIGH(); }
#else
IDLE_PIN_HIGH(); // Switch pin high
if(configPage6.iacChannels == 1) { IDLE2_PIN_LOW(); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1
#endif
}
else
{
//Reversed direction
#if defined (CORE_TEENSY41) //PIT TIMERS count down and have opposite effect on PWM
IDLE_PIN_HIGH();
if(configPage6.iacChannels == 1) { IDLE2_PIN_LOW(); }
#else
IDLE_PIN_LOW(); // Switch pin to low (1 pin mode)
if(configPage6.iacChannels == 1) { IDLE2_PIN_HIGH(); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1
#endif
}
SET_COMPARE(IDLE_COMPARE, IDLE_COUNTER + idle_pwm_target_value);
idle_pwm_cur_value = idle_pwm_target_value;