From 85d2243c6e6a99cfa7167e636a87f8f13e04de3f Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Wed, 23 Jan 2019 12:04:54 +1300 Subject: [PATCH] Move board specific code out of auxilliaries --- speeduino/auxiliaries.ino | 34 ---------------------------------- speeduino/board_avr2560.h | 5 +++++ speeduino/board_avr2560.ino | 26 ++++++++++++++++++++++++-- speeduino/board_stm32.h | 2 ++ speeduino/board_stm32.ino | 8 ++++++++ speeduino/board_teensy35.h | 3 +++ speeduino/board_teensy35.ino | 30 ++++++++++++++++++++++++++++-- speeduino/comms.ino | 16 ++++++++-------- 8 files changed, 78 insertions(+), 46 deletions(-) diff --git a/speeduino/auxiliaries.ino b/speeduino/auxiliaries.ino index 34856357..505a1c94 100644 --- a/speeduino/auxiliaries.ino +++ b/speeduino/auxiliaries.ino @@ -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); diff --git a/speeduino/board_avr2560.h b/speeduino/board_avr2560.h index fafd84d9..c54f916b 100644 --- a/speeduino/board_avr2560.h +++ b/speeduino/board_avr2560.h @@ -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 diff --git a/speeduino/board_avr2560.ino b/speeduino/board_avr2560.ino index 89cec65a..16cc5fe0 100644 --- a/speeduino/board_avr2560.ino +++ b/speeduino/board_avr2560.ino @@ -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) diff --git a/speeduino/board_stm32.h b/speeduino/board_stm32.h index 5fd523ff..cf61fe99 100644 --- a/speeduino/board_stm32.h +++ b/speeduino/board_stm32.h @@ -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(); diff --git a/speeduino/board_stm32.ino b/speeduino/board_stm32.ino index caa55cd9..30328136 100644 --- a/speeduino/board_stm32.ino +++ b/speeduino/board_stm32.ino @@ -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 /* *********************************************************************************************************** diff --git a/speeduino/board_teensy35.h b/speeduino/board_teensy35.h index 51c50948..280aa69e 100644 --- a/speeduino/board_teensy35.h +++ b/speeduino/board_teensy35.h @@ -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() /* *********************************************************************************************************** diff --git a/speeduino/board_teensy35.ino b/speeduino/board_teensy35.ino index e8b90a43..cd303697 100644 --- a/speeduino/board_teensy35.ino +++ b/speeduino/board_teensy35.ino @@ -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 diff --git a/speeduino/comms.ino b/speeduino/comms.ino index c0ad455e..e8816251 100644 --- a/speeduino/comms.ino +++ b/speeduino/comms.ino @@ -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