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