Speeduino
Loading...
Searching...
No Matches
board_avr2560.h
Go to the documentation of this file.
1#pragma once
2
5#define CORE_AVR
6
7#include <stdint.h>
8#include <avr/interrupt.h>
9#include <avr/io.h>
10
11/*
12***********************************************************************************************************
13* General
14*/
15#define BOARD_MAX_DIGITAL_PINS 54 //digital pins +1
16#define BOARD_MAX_IO_PINS 70 //digital pins + analog channels + 1
17#define BOARD_MAX_ADC_PINS 15 //Number of analog pins
18#ifndef LED_BUILTIN
19 #define LED_BUILTIN 13
20#endif
21
22#ifndef INJ_CHANNELS
23 #define INJ_CHANNELS 4
24#endif
25#ifndef IGN_CHANNELS
26 #define IGN_CHANNELS 5
27#endif
28
33using COMPARE_TYPE = uint16_t;
34
35namespace {
37 constexpr uint32_t TICK_RESOLUTION = 4U;
38}
39
41static constexpr COMPARE_TYPE uS_TO_TIMER_COMPARE(uint32_t micros)
42{
43 // Faster than micros/TICK_RESOLUTION
44 constexpr uint32_t SHIFT = TICK_RESOLUTION/2U;
45 return (COMPARE_TYPE)(micros >> SHIFT);
46}
47
49static constexpr uint32_t ticksToMicros(COMPARE_TYPE ticks)
50{
51 return ticks * TICK_RESOLUTION;
52}
53
54#define TS_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
55#define FPU_MAX_SIZE 0 //Size of the FPU buffer. 0 means no FPU.
56#ifdef USE_SPI_EEPROM
57 #define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
58 class SPI_EEPROM_Class;
59 using EEPROM_t = SPI_EEPROM_Class;
60#else
61 #define EEPROM_LIB_H <EEPROM.h>
62 class EEPROMClass;
63 using EEPROM_t = EEPROMClass;
64#endif
65#ifdef PLATFORMIO
66 #define RTC_LIB_H <TimeLib.h>
67#else
68 #define RTC_LIB_H <Time.h>
69#endif
70constexpr uint16_t BLOCKING_FACTOR = 121;
71constexpr uint16_t TABLE_BLOCKING_FACTOR = 64;
72static inline bool pinIsReserved(uint8_t pin) { return pin==0U; } //Forbidden pins like USB on other boards
73
74/*
75***********************************************************************************************************
76* Schedules
77*/
78 //Refer to svn.savannah.nongnu.org/viewvc/trunk/avr-libc/include/avr/iomxx0_1.h?root=avr-libc&view=markup
79#define FUEL1_COUNTER TCNT3
80#define FUEL2_COUNTER TCNT3
81#define FUEL3_COUNTER TCNT3
82#define FUEL4_COUNTER TCNT4
83#define FUEL5_COUNTER TCNT4
84#define FUEL6_COUNTER TCNT4 //Replaces ignition 4
85#define FUEL7_COUNTER TCNT5 //Replaces ignition 3
86#define FUEL8_COUNTER TCNT5 //Replaces ignition 2
87
88#define IGN1_COUNTER TCNT5
89#define IGN2_COUNTER TCNT5
90#define IGN3_COUNTER TCNT5
91#define IGN4_COUNTER TCNT4
92#define IGN5_COUNTER TCNT4
93#define IGN6_COUNTER TCNT4 //Replaces injector 4
94#define IGN7_COUNTER TCNT3 //Replaces injector 3
95#define IGN8_COUNTER TCNT3 //Replaces injector 2
96
97#define FUEL1_COMPARE OCR3A
98#define FUEL2_COMPARE OCR3B
99#define FUEL3_COMPARE OCR3C
100#define FUEL4_COMPARE OCR4B //Replaces ignition 6
101#define FUEL5_COMPARE OCR4C //Replaces ignition 5
102#define FUEL6_COMPARE OCR4A //Replaces ignition 4
103#define FUEL7_COMPARE OCR5C //Replaces ignition 3
104#define FUEL8_COMPARE OCR5B //Replaces ignition 2
105
106#define IGN1_COMPARE OCR5A
107#define IGN2_COMPARE OCR5B
108#define IGN3_COMPARE OCR5C
109#define IGN4_COMPARE OCR4A //Replaces injector 6
110#define IGN5_COMPARE OCR4C //Replaces injector 5
111#define IGN6_COMPARE OCR4B //Replaces injector 4
112#define IGN7_COMPARE OCR3C //Replaces injector 3
113#define IGN8_COMPARE OCR3B //Replaces injector 2
114
115//Note that the interrupt flag is reset BEFORE the interrupt is enabled
116static inline void FUEL1_TIMER_ENABLE(void) { TIFR3 |= (1<<OCF3A) ; TIMSK3 |= (1 << OCIE3A); } //Turn on the A compare unit (ie turn on the interrupt)
117static inline void FUEL2_TIMER_ENABLE(void) { TIFR3 |= (1<<OCF3B); TIMSK3 |= (1 << OCIE3B); } //Turn on the B compare unit (ie turn on the interrupt)
118static inline void FUEL3_TIMER_ENABLE(void) { TIFR3 |= (1<<OCF3C); TIMSK3 |= (1 << OCIE3C); } //Turn on the C compare unit (ie turn on the interrupt)
119static inline void FUEL4_TIMER_ENABLE(void) { TIFR4 |= (1<<OCF4B); TIMSK4 |= (1 << OCIE4B); } //Turn on the B compare unit (ie turn on the interrupt)
120static inline void FUEL5_TIMER_ENABLE(void) { TIFR4 |= (1<<OCF4C); TIMSK4 |= (1 << OCIE4C); } //Turn on the C compare unit (ie turn on the interrupt)
121static inline void FUEL6_TIMER_ENABLE(void) { TIFR4 |= (1<<OCF4A); TIMSK4 |= (1 << OCIE4A); } //Turn on the A compare unit (ie turn on the interrupt)
122static inline void FUEL7_TIMER_ENABLE(void) { TIFR5 |= (1<<OCF5C); TIMSK5 |= (1 << OCIE5C); } //
123static inline void FUEL8_TIMER_ENABLE(void) { TIFR5 |= (1<<OCF5B); TIMSK5 |= (1 << OCIE5B); } //
124
125static inline void FUEL1_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3A); } // //Turn off this output compare unit
126static inline void FUEL2_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3B); } // //Turn off this output compare unit
127static inline void FUEL3_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3C); } // //Turn off this output compare unit
128static inline void FUEL4_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4B); }
129static inline void FUEL5_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4C); } // //
130static inline void FUEL6_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4A); } // //
131static inline void FUEL7_TIMER_DISABLE(void) { TIMSK5 &= ~(1 << OCIE5C); } // //
132static inline void FUEL8_TIMER_DISABLE(void) { TIMSK5 &= ~(1 << OCIE5B); } //
133
134 //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.
135static inline void IGN1_TIMER_ENABLE(void) { TIFR5 |= (1<<OCF5A); TIMSK5 |= (1 << OCIE5A); } //Turn on the A compare unit (ie turn on the interrupt)
136static inline void IGN2_TIMER_ENABLE(void) { TIFR5 |= (1<<OCF5B); TIMSK5 |= (1 << OCIE5B); }//Turn on the B compare unit (ie turn on the interrupt)
137static inline void IGN3_TIMER_ENABLE(void) { TIFR5 |= (1<<OCF5C); TIMSK5 |= (1 << OCIE5C); }//Turn on the C compare unit (ie turn on the interrupt)
138static inline void IGN4_TIMER_ENABLE(void) { TIFR4 |= (1<<OCF4A); TIMSK4 |= (1 << OCIE4A); }//Turn on the A compare unit (ie turn on the interrupt)
139static inline void IGN5_TIMER_ENABLE(void) { TIFR4 |= (1<<OCF4C); TIMSK4 |= (1 << OCIE4C); } //Turn on the A compare unit (ie turn on the interrupt)
140static inline void IGN6_TIMER_ENABLE(void) { TIFR4 |= (1<<OCF4B); TIMSK4 |= (1 << OCIE4B); } //Replaces injector 4
141static inline void IGN7_TIMER_ENABLE(void) { TIMSK3 |= (1 << OCIE3C); }//Replaces injector 3
142static inline void IGN8_TIMER_ENABLE(void) { TIMSK3 |= (1 << OCIE3B); } //Replaces injector 2
143
144static inline void IGN1_TIMER_DISABLE(void) { TIMSK5 &= ~(1 << OCIE5A); } //Turn off this output compare unit
145static inline void IGN2_TIMER_DISABLE(void) { TIMSK5 &= ~(1 << OCIE5B); } //Turn off this output compare unit
146static inline void IGN3_TIMER_DISABLE(void) { TIMSK5 &= ~(1 << OCIE5C); } //Turn off this output compare unit
147static inline void IGN4_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4A); } //Turn off this output compare unit
148static inline void IGN5_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4C); } //Turn off this output compare unit
149static inline void IGN6_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4B); } //Replaces injector 4
150static inline void IGN7_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3C); } //Replaces injector 3
151static inline void IGN8_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3B); } //Replaces injector 2
152
153/*
154***********************************************************************************************************
155* Auxiliaries
156*/
157#define ENABLE_BOOST_TIMER() TIMSK1 |= (1 << OCIE1A)
158#define DISABLE_BOOST_TIMER() TIMSK1 &= ~(1 << OCIE1A)
159#define ENABLE_VVT_TIMER() TIMSK1 |= (1 << OCIE1B)
160#define DISABLE_VVT_TIMER() TIMSK1 &= ~(1 << OCIE1B)
161
162#define BOOST_TIMER_COMPARE OCR1A
163#define BOOST_TIMER_COUNTER TCNT1
164#define VVT_TIMER_COMPARE OCR1B
165#define VVT_TIMER_COUNTER TCNT1
166
167/*
168***********************************************************************************************************
169* Idle
170*/
171#define IDLE_COUNTER TCNT1
172#define IDLE_COMPARE OCR1C
173
174#define IDLE_TIMER_ENABLE() TIMSK1 |= (1 << OCIE1C)
175#define IDLE_TIMER_DISABLE() TIMSK1 &= ~(1 << OCIE1C)
176
177/*
178***********************************************************************************************************
179* CAN / Second serial
180*/
181#define SECONDARY_SERIAL_T HardwareSerial
static void IGN1_TIMER_ENABLE(void)
Definition board_avr2560.h:135
static void IGN3_TIMER_DISABLE(void)
Definition board_avr2560.h:146
static void IGN1_TIMER_DISABLE(void)
Definition board_avr2560.h:144
EEPROMClass EEPROM_t
Definition board_avr2560.h:63
static void FUEL2_TIMER_ENABLE(void)
Definition board_avr2560.h:117
static void FUEL1_TIMER_DISABLE(void)
Definition board_avr2560.h:125
static void FUEL4_TIMER_DISABLE(void)
Definition board_avr2560.h:128
static void IGN3_TIMER_ENABLE(void)
Definition board_avr2560.h:137
static void FUEL4_TIMER_ENABLE(void)
Definition board_avr2560.h:119
static void FUEL5_TIMER_DISABLE(void)
Definition board_avr2560.h:129
static void FUEL2_TIMER_DISABLE(void)
Definition board_avr2560.h:126
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:33
static void FUEL3_TIMER_DISABLE(void)
Definition board_avr2560.h:127
static void IGN5_TIMER_ENABLE(void)
Definition board_avr2560.h:139
static bool pinIsReserved(uint8_t pin)
Definition board_avr2560.h:72
static void FUEL7_TIMER_DISABLE(void)
Definition board_avr2560.h:131
constexpr uint16_t BLOCKING_FACTOR
Definition board_avr2560.h:70
static void FUEL7_TIMER_ENABLE(void)
Definition board_avr2560.h:122
static void FUEL3_TIMER_ENABLE(void)
Definition board_avr2560.h:118
static void FUEL6_TIMER_ENABLE(void)
Definition board_avr2560.h:121
static void IGN6_TIMER_DISABLE(void)
Definition board_avr2560.h:149
static constexpr uint32_t ticksToMicros(COMPARE_TYPE ticks)
Convert timer ticks to µS.
Definition board_avr2560.h:49
static void FUEL6_TIMER_DISABLE(void)
Definition board_avr2560.h:130
static void IGN7_TIMER_DISABLE(void)
Definition board_avr2560.h:150
static void IGN2_TIMER_DISABLE(void)
Definition board_avr2560.h:145
static void IGN8_TIMER_DISABLE(void)
Definition board_avr2560.h:151
static void IGN6_TIMER_ENABLE(void)
Definition board_avr2560.h:140
static void IGN2_TIMER_ENABLE(void)
Definition board_avr2560.h:136
static void IGN8_TIMER_ENABLE(void)
Definition board_avr2560.h:142
static void FUEL8_TIMER_DISABLE(void)
Definition board_avr2560.h:132
static void IGN4_TIMER_ENABLE(void)
Definition board_avr2560.h:138
static void IGN7_TIMER_ENABLE(void)
Definition board_avr2560.h:141
static void IGN5_TIMER_DISABLE(void)
Definition board_avr2560.h:148
static void IGN4_TIMER_DISABLE(void)
Definition board_avr2560.h:147
static void FUEL1_TIMER_ENABLE(void)
Definition board_avr2560.h:116
static void FUEL8_TIMER_ENABLE(void)
Definition board_avr2560.h:123
static void FUEL5_TIMER_ENABLE(void)
Definition board_avr2560.h:120
constexpr uint16_t TABLE_BLOCKING_FACTOR
Definition board_avr2560.h:71
static constexpr COMPARE_TYPE uS_TO_TIMER_COMPARE(uint32_t micros)
Convert µS to timer ticks.
Definition board_avr2560.h:41