Move board specific code out of auxilliaries

This commit is contained in:
Josh Stewart 2019-01-23 12:04:54 +13:00
parent f4c0513c2f
commit 85d2243c6e
8 changed files with 78 additions and 46 deletions

View File

@ -56,32 +56,6 @@ void fanControl()
void initialiseAuxPWM()
{
#if defined(CORE_AVR)
TCCR1B = 0x00; //Disbale Timer1 while we set it up
TCNT1 = 0; //Reset Timer Count
TIFR1 = 0x00; //Timer1 INT Flag Reg: Clear Timer Overflow Flag
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)
//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
//NVIC_ENABLE_IRQ(IRQ_FTM1);
#endif
boost_pin_port = portOutputRegister(digitalPinToPort(pinBoost));
boost_pin_mask = digitalPinToBitMask(pinBoost);
vvt_pin_port = portOutputRegister(digitalPinToPort(pinVVT_1));
@ -100,14 +74,6 @@ void initialiseAuxPWM()
else { pinMode(configPage10.n2o_arming_pin, INPUT); }
}
#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
ENABLE_VVT_TIMER(); //Turn on the B compare unit (ie turn on the interrupt)
boostPID.SetOutputLimits(configPage2.boostMinDuty, configPage2.boostMaxDuty);

View File

@ -21,6 +21,11 @@
#define micros_safe() micros() //If the timer5 method is not used, the micros_safe() macro is simply an alias for the normal micros()
#endif
//Mega 2561 MCU does not have a serial3 available.
#if not defined(__AVR_ATmega2561__)
#define USE_SERIAL3
#endif
/*
***********************************************************************************************************
* Schedules

View File

@ -1,6 +1,8 @@
#include "globals.h"
#if defined(CORE_AVR)
#include "globals.h"
#include "auxiliaries.h"
void initBoard()
{
@ -9,14 +11,34 @@ void initBoard()
* General
*/
configPage9.intcan_available = 0; // AVR devices do NOT have internal canbus
/*
***********************************************************************************************************
* Auxilliaries
*/
//PWM used by the Boost and VVT outputs
TCCR1B = 0x00; //Disbale Timer1 while we set it up
TCNT1 = 0; //Reset Timer Count
TIFR1 = 0x00; //Timer1 INT Flag Reg: Clear Timer Overflow Flag
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
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
}
uint16_t freeRam()
{
extern int __heap_start, *__brkval;
int currentVal;
uint16_t v;
return (uint16_t) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
if(__brkval == 0) { currentVal = (int) &__heap_start; }
else { currentVal = (int) __brkval; }
//return (uint16_t) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); // cppcheck-suppress misra-c2012-12.1
return (uint16_t) &v - currentVal; //cppcheck-suppress misra-c2012-11.4
}
#if defined(TIMER5_MICROS)

View File

@ -9,6 +9,8 @@
* General
*/
#define PORT_TYPE uint8_t
#define micros_safe() micros() //timer5 method is not used on anything but AVR, the micros_safe() macro is simply an alias for the normal micros()
#define USE_SERIAL3
void initBoard();
uint16_t freeRam();

View File

@ -1,4 +1,5 @@
#include "globals.h"
#include "auxiliaries.h"
#if defined(CORE_STM32)
#include "board_stm32.h"
@ -42,6 +43,13 @@ void initBoard()
#endif
pinMode(LED_BUILTIN, OUTPUT); //Visual WDT
/*
***********************************************************************************************************
* Auxilliaries
*/
//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
/*
***********************************************************************************************************

View File

@ -11,6 +11,9 @@
#define PORT_TYPE uint8_t //Size of the port variables
#define BOARD_DIGITAL_GPIO_PINS 34
#define BOARD_NR_GPIO_PINS 34
#define USE_SERIAL3
#define micros_safe() micros() //timer5 method is not used on anything but AVR, the micros_safe() macro is simply an alias for the normal micros()
/*
***********************************************************************************************************

View File

@ -1,6 +1,7 @@
#include "globals.h"
#if defined(CORE_TEENSY)
#include "globals.h"
#include "auxiliaries.h"
void initBoard()
{
@ -85,6 +86,31 @@ void initBoard()
//Uses the PIT timer on Teensy.
lowResTimer.begin(oneMSInterval, 1000);
/*
***********************************************************************************************************
* Auxilliaries
*/
//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
//NVIC_ENABLE_IRQ(IRQ_FTM1);
//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
/*
***********************************************************************************************************
* Schedules

View File

@ -418,16 +418,16 @@ void sendValues(uint16_t offset, uint16_t packetLength, byte cmd, byte portNum)
if (portNum == 3)
{
//CAN serial
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)|| defined(CORE_STM32) || defined (CORE_TEENSY) //ATmega2561 does not have Serial3
#if defined(USE_SERIAL3)
if (offset == 0)
{
CANSerial.write("A"); //confirm cmd type
}
{
CANSerial.write("A"); //confirm cmd type
}
else
{
CANSerial.write("r"); //confirm cmd type
CANSerial.write(cmd);
}
{
CANSerial.write("r"); //confirm cmd type
CANSerial.write(cmd);
}
#endif
}
else