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