2019-01-18 02:46:23 -08:00
# ifndef AVR2560_H
# define AVR2560_H
2023-10-06 00:10:20 -07:00
# include "globals.h"
2019-01-18 02:46:23 -08:00
# if defined(CORE_AVR)
2019-01-20 20:50:50 -08:00
# include <avr/interrupt.h>
# include <avr/io.h>
2019-01-18 02:46:23 -08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* General
*/
# define PORT_TYPE uint8_t //Size of the port variables (Eg inj1_pin_port).
2019-03-03 14:15:57 -08:00
# define PINMASK_TYPE uint8_t
2019-08-20 22:15:19 -07:00
# define COMPARE_TYPE uint16_t
# define COUNTER_TYPE uint16_t
2022-01-08 22:05:05 -08:00
# define SERIAL_BUFFER_SIZE (256+7+1) //Size of the serial buffer used by new comms protocol. The largest single packet is the O2 calibration which is 256 bytes + 7 bytes of overhead
2022-08-09 19:12:42 -07:00
# define FPU_MAX_SIZE 0 //Size of the FPU buffer. 0 means no FPU.
2020-02-16 00:54:52 -08:00
# ifdef USE_SPI_EEPROM
# define EEPROM_LIB_H "src / SPIAsEEPROM / SPIAsEEPROM.h"
2021-09-13 23:51:26 -07:00
typedef uint16_t eeprom_address_t ;
2020-02-16 00:54:52 -08:00
# else
# define EEPROM_LIB_H <EEPROM.h>
2021-09-13 23:51:26 -07:00
typedef int eeprom_address_t ;
2020-02-16 00:54:52 -08:00
# endif
2021-12-19 03:58:14 -08:00
# ifdef PLATFORMIO
# define RTC_LIB_H <TimeLib.h>
# else
# define RTC_LIB_H <Time.h>
# endif
2022-11-05 15:43:29 -07:00
void initBoard ( void ) ;
uint16_t freeRam ( void ) ;
void doSystemReset ( void ) ;
void jumpToBootloader ( void ) ;
2019-01-18 02:46:23 -08:00
2019-01-21 15:04:21 -08:00
# if defined(TIMER5_MICROS)
2019-10-21 18:49:07 -07:00
/*#define micros() (((timer5_overflow_count << 16) + TCNT5) * 4) */ //Fast version of micros() that uses the 4uS tick of timer5. See timers.ino for the overflow ISR of timer5
2019-01-21 15:04:21 -08:00
# define millis() (ms_counter) //Replaces the standard millis() function with this macro. It is both faster and more accurate. See timers.ino for its counter increment.
static inline unsigned long micros_safe ( ) ; //A version of micros() that is interrupt safe
# else
# define micros_safe() micros() //If the timer5 method is not used, the micros_safe() macro is simply an alias for the normal micros()
# endif
2022-04-10 17:49:58 -07:00
# define pinIsReserved(pin) ( ((pin) == 0) ) //Forbidden pins like USB on other boards
2019-01-21 15:04:21 -08:00
2019-01-22 15:04:54 -08:00
//Mega 2561 MCU does not have a serial3 available.
# if not defined(__AVR_ATmega2561__)
# define USE_SERIAL3
# endif
2019-01-19 19:57:16 -08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Schedules
*/
2019-10-21 18:49:07 -07:00
//Refer to svn.savannah.nongnu.org/viewvc/trunk/avr-libc/include/avr/iomxx0_1.h?root=avr-libc&view=markup
2019-01-19 19:57:16 -08:00
# define FUEL1_COUNTER TCNT3
# define FUEL2_COUNTER TCNT3
# define FUEL3_COUNTER TCNT3
# define FUEL4_COUNTER TCNT4
2019-07-19 12:57:30 -07:00
# define FUEL5_COUNTER TCNT4
2019-01-19 19:57:16 -08:00
# define FUEL6_COUNTER TCNT4 //Replaces ignition 4
# define FUEL7_COUNTER TCNT5 //Replaces ignition 3
# define FUEL8_COUNTER TCNT5 //Replaces ignition 2
# define IGN1_COUNTER TCNT5
# define IGN2_COUNTER TCNT5
# define IGN3_COUNTER TCNT5
# define IGN4_COUNTER TCNT4
2019-07-19 12:57:30 -07:00
# define IGN5_COUNTER TCNT4
2019-01-19 19:57:16 -08:00
# define IGN6_COUNTER TCNT4 //Replaces injector 4
# define IGN7_COUNTER TCNT3 //Replaces injector 3
# define IGN8_COUNTER TCNT3 //Replaces injector 2
# define FUEL1_COMPARE OCR3A
# define FUEL2_COMPARE OCR3B
# define FUEL3_COMPARE OCR3C
2020-03-31 23:03:11 -07:00
# define FUEL4_COMPARE OCR4B //Replaces ignition 6
# define FUEL5_COMPARE OCR4C //Replaces ignition 5
# define FUEL6_COMPARE OCR4A //Replaces ignition 4
# define FUEL7_COMPARE OCR5C //Replaces ignition 3
# define FUEL8_COMPARE OCR5B //Replaces ignition 2
2019-01-19 19:57:16 -08:00
# define IGN1_COMPARE OCR5A
# define IGN2_COMPARE OCR5B
# define IGN3_COMPARE OCR5C
2020-03-31 23:03:11 -07:00
# define IGN4_COMPARE OCR4A //Replaces injector 6
# define IGN5_COMPARE OCR4C //Replaces injector 5
2019-01-19 19:57:16 -08:00
# define IGN6_COMPARE OCR4B //Replaces injector 4
# define IGN7_COMPARE OCR3C //Replaces injector 3
# define IGN8_COMPARE OCR3B //Replaces injector 2
2019-11-06 20:01:28 -08:00
//Note that the interrupt flag is reset BEFORE the interrupt is enabled
2023-10-06 14:25:26 -07:00
static inline void FUEL1_TIMER_ENABLE ( void ) { TIFR3 | = ( 1 < < OCF3A ) ; TIMSK3 | = ( 1 < < OCIE3A ) ; } //Turn on the A compare unit (ie turn on the interrupt)
static inline void FUEL2_TIMER_ENABLE ( void ) { TIFR3 | = ( 1 < < OCF3B ) ; TIMSK3 | = ( 1 < < OCIE3B ) ; } //Turn on the B compare unit (ie turn on the interrupt)
static inline void FUEL3_TIMER_ENABLE ( void ) { TIFR3 | = ( 1 < < OCF3C ) ; TIMSK3 | = ( 1 < < OCIE3C ) ; } //Turn on the C compare unit (ie turn on the interrupt)
static inline void FUEL4_TIMER_ENABLE ( void ) { TIFR4 | = ( 1 < < OCF4B ) ; TIMSK4 | = ( 1 < < OCIE4B ) ; } //Turn on the B compare unit (ie turn on the interrupt)
static inline void FUEL5_TIMER_ENABLE ( void ) { TIFR4 | = ( 1 < < OCF4C ) ; TIMSK4 | = ( 1 < < OCIE4C ) ; } //Turn on the C compare unit (ie turn on the interrupt)
static inline void FUEL6_TIMER_ENABLE ( void ) { TIFR4 | = ( 1 < < OCF4A ) ; TIMSK4 | = ( 1 < < OCIE4A ) ; } //Turn on the A compare unit (ie turn on the interrupt)
static inline void FUEL7_TIMER_ENABLE ( void ) { TIFR5 | = ( 1 < < OCF5C ) ; TIMSK5 | = ( 1 < < OCIE5C ) ; } //
static inline void FUEL8_TIMER_ENABLE ( void ) { TIFR5 | = ( 1 < < OCF5B ) ; TIMSK5 | = ( 1 < < OCIE5B ) ; } //
2019-01-19 19:57:16 -08:00
2023-10-06 14:25:26 -07:00
static inline void FUEL1_TIMER_DISABLE ( void ) { TIMSK3 & = ~ ( 1 < < OCIE3A ) ; } // //Turn off this output compare unit
static inline void FUEL2_TIMER_DISABLE ( void ) { TIMSK3 & = ~ ( 1 < < OCIE3B ) ; } // //Turn off this output compare unit
static inline void FUEL3_TIMER_DISABLE ( void ) { TIMSK3 & = ~ ( 1 < < OCIE3C ) ; } // //Turn off this output compare unit
static inline void FUEL4_TIMER_DISABLE ( void ) { TIMSK4 & = ~ ( 1 < < OCIE4B ) ; } ////Turn off this output compare unit
static inline void FUEL5_TIMER_DISABLE ( void ) { TIMSK4 & = ~ ( 1 < < OCIE4C ) ; } // //
static inline void FUEL6_TIMER_DISABLE ( void ) { TIMSK4 & = ~ ( 1 < < OCIE4A ) ; } // //
static inline void FUEL7_TIMER_DISABLE ( void ) { TIMSK5 & = ~ ( 1 < < OCIE5C ) ; } // //
static inline void FUEL8_TIMER_DISABLE ( void ) { TIMSK5 & = ~ ( 1 < < OCIE5B ) ; } //
2019-01-19 19:57:16 -08:00
2019-03-05 21:58:26 -08:00
//These have the TIFR5 bits set to 1 to clear the interrupt flag. This prevents a false interrupt being called the first time the channel is enabled.
2023-10-06 14:25:26 -07:00
static inline void IGN1_TIMER_ENABLE ( void ) { TIFR5 | = ( 1 < < OCF5A ) ; TIMSK5 | = ( 1 < < OCIE5A ) ; } //Turn on the A compare unit (ie turn on the interrupt)
static inline void IGN2_TIMER_ENABLE ( void ) { TIFR5 | = ( 1 < < OCF5B ) ; TIMSK5 | = ( 1 < < OCIE5B ) ; } //Turn on the B compare unit (ie turn on the interrupt)
static inline void IGN3_TIMER_ENABLE ( void ) { TIFR5 | = ( 1 < < OCF5C ) ; TIMSK5 | = ( 1 < < OCIE5C ) ; } //Turn on the C compare unit (ie turn on the interrupt)
static inline void IGN4_TIMER_ENABLE ( void ) { TIFR4 | = ( 1 < < OCF4A ) ; TIMSK4 | = ( 1 < < OCIE4A ) ; } //Turn on the A compare unit (ie turn on the interrupt)
static inline void IGN5_TIMER_ENABLE ( void ) { TIFR4 | = ( 1 < < OCF4C ) ; TIMSK4 | = ( 1 < < OCIE4C ) ; } //Turn on the A compare unit (ie turn on the interrupt)
static inline void IGN6_TIMER_ENABLE ( void ) { TIFR4 | = ( 1 < < OCF4B ) ; TIMSK4 | = ( 1 < < OCIE4B ) ; } //Replaces injector 4
static inline void IGN7_TIMER_ENABLE ( void ) { TIMSK3 | = ( 1 < < OCIE3C ) ; } //Replaces injector 3
static inline void IGN8_TIMER_ENABLE ( void ) { TIMSK3 | = ( 1 < < OCIE3B ) ; } //Replaces injector 2
2019-01-19 19:57:16 -08:00
2023-10-06 14:25:26 -07:00
static inline void IGN1_TIMER_DISABLE ( void ) { TIMSK5 & = ~ ( 1 < < OCIE5A ) ; } //Turn off this output compare unit
static inline void IGN2_TIMER_DISABLE ( void ) { TIMSK5 & = ~ ( 1 < < OCIE5B ) ; } //Turn off this output compare unit
static inline void IGN3_TIMER_DISABLE ( void ) { TIMSK5 & = ~ ( 1 < < OCIE5C ) ; } //Turn off this output compare unit
static inline void IGN4_TIMER_DISABLE ( void ) { TIMSK4 & = ~ ( 1 < < OCIE4A ) ; } //Turn off this output compare unit
static inline void IGN5_TIMER_DISABLE ( void ) { TIMSK4 & = ~ ( 1 < < OCIE4C ) ; } //Turn off this output compare unit
static inline void IGN6_TIMER_DISABLE ( void ) { TIMSK4 & = ~ ( 1 < < OCIE4B ) ; } //Replaces injector 4
static inline void IGN7_TIMER_DISABLE ( void ) { TIMSK3 & = ~ ( 1 < < OCIE3C ) ; } //Replaces injector 3
static inline void IGN8_TIMER_DISABLE ( void ) { TIMSK3 & = ~ ( 1 < < OCIE3B ) ; } //Replaces injector 2
2019-01-19 19:57:16 -08:00
# define MAX_TIMER_PERIOD 262140UL //The longest period of time (in uS) that the timer can permit (IN this case it is 65535 * 4, as each timer tick is 4uS)
2019-07-08 00:56:02 -07:00
# define uS_TO_TIMER_COMPARE(uS1) ((uS1) >> 2) //Converts a given number of uS into the required number of timer ticks until that time has passed
2019-01-19 19:57:16 -08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2022-04-10 17:49:58 -07:00
* Auxiliaries
2019-01-19 19:57:16 -08:00
*/
# define ENABLE_BOOST_TIMER() TIMSK1 |= (1 << OCIE1A)
# define DISABLE_BOOST_TIMER() TIMSK1 &= ~(1 << OCIE1A)
# define ENABLE_VVT_TIMER() TIMSK1 |= (1 << OCIE1B)
# define DISABLE_VVT_TIMER() TIMSK1 &= ~(1 << OCIE1B)
# define BOOST_TIMER_COMPARE OCR1A
# define BOOST_TIMER_COUNTER TCNT1
# define VVT_TIMER_COMPARE OCR1B
# define VVT_TIMER_COUNTER TCNT1
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Idle
*/
2019-07-19 12:57:30 -07:00
# define IDLE_COUNTER TCNT1
# define IDLE_COMPARE OCR1C
2019-01-19 19:57:16 -08:00
2019-07-19 12:57:30 -07:00
# define IDLE_TIMER_ENABLE() TIMSK1 |= (1 << OCIE1C)
# define IDLE_TIMER_DISABLE() TIMSK1 &= ~(1 << OCIE1C)
2019-01-19 19:57:16 -08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* CAN / Second serial
*/
2019-01-18 02:46:23 -08:00
# endif //CORE_AVR
# endif //AVR2560_H