Speeduino
Loading...
Searching...
No Matches
board_teensy41.h
Go to the documentation of this file.
1#pragma once
2
5#include <Arduino.h>
6
7#define CORE_TEENSY41
8
9/*
10***********************************************************************************************************
11* General
12*/
14#define COMPARE_TYPE uint16_t
15#define SERIAL_BUFFER_SIZE 517 //Size of the serial buffer used by new comms protocol. For SD transfers this must be at least 512 + 1 (flag) + 4 (sector)
16#define FPU_MAX_SIZE 32 //Size of the FPU buffer. 0 means no FPU.
17#define BOARD_MAX_DIGITAL_PINS 54
18#define BOARD_MAX_IO_PINS 54
19#define BOARD_MAX_ADC_PINS 17 //Number of analog pins
20#define EEPROM_LIB_H <EEPROM.h>
21typedef int eeprom_address_t;
22#define RTC_ENABLED
23#define SD_LOGGING //SD logging enabled by default for Teensy 4.1 as it has the slot built in
24#define RTC_LIB_H "TimeLib.h"
25#define SD_CONFIG SdioConfig(FIFO_SDIO) //Set Teensy to use SDIO in FIFO mode. This is the fastest SD mode on Teensy as it offloads most of the writes
26
27//#define PWM_FAN_AVAILABLE
28#define pinIsReserved(pin) ( ((pin) == 0) || ((pin) == 42) || ((pin) == 43) || ((pin) == 44) || ((pin) == 45) || ((pin) == 46) || ((pin) == 47) || pinIsSerial((pin)) ) //Forbidden pins like USB
29
30#define INJ_CHANNELS 8
31#define IGN_CHANNELS 8
32
33/*
34***********************************************************************************************************
35* Schedules
36*/
37/*
38https://github.com/luni64/TeensyTimerTool/wiki/Supported-Timers#pit---periodic-timer
39https://github.com/luni64/TeensyTimerTool/wiki/Configuration#clock-setting-for-the-gpt-and-pit-timers
40The Quad timer (TMR) provides 4 timers each with 4 usable compare channels. The down compare and alternating compares are not usable
41FUEL 1-4: TMR1
42IGN 1-4 : TMR2
43FUEL 5-8: TMR3
44IGN 5-8 : TMR4
45*/
46#define FUEL1_COUNTER TMR1_CNTR0
47#define FUEL2_COUNTER TMR1_CNTR1
48#define FUEL3_COUNTER TMR1_CNTR2
49#define FUEL4_COUNTER TMR1_CNTR3
50#define FUEL5_COUNTER TMR3_CNTR0
51#define FUEL6_COUNTER TMR3_CNTR1
52#define FUEL7_COUNTER TMR3_CNTR2
53#define FUEL8_COUNTER TMR3_CNTR3
54
55#define IGN1_COUNTER TMR2_CNTR0
56#define IGN2_COUNTER TMR2_CNTR1
57#define IGN3_COUNTER TMR2_CNTR2
58#define IGN4_COUNTER TMR2_CNTR3
59#define IGN5_COUNTER TMR4_CNTR0
60#define IGN6_COUNTER TMR4_CNTR1
61#define IGN7_COUNTER TMR4_CNTR2
62#define IGN8_COUNTER TMR4_CNTR3
63
64#define FUEL1_COMPARE TMR1_COMP10
65#define FUEL2_COMPARE TMR1_COMP11
66#define FUEL3_COMPARE TMR1_COMP12
67#define FUEL4_COMPARE TMR1_COMP13
68#define FUEL5_COMPARE TMR3_COMP10
69#define FUEL6_COMPARE TMR3_COMP11
70#define FUEL7_COMPARE TMR3_COMP12
71#define FUEL8_COMPARE TMR3_COMP13
72
73#define IGN1_COMPARE TMR2_COMP10
74#define IGN2_COMPARE TMR2_COMP11
75#define IGN3_COMPARE TMR2_COMP12
76#define IGN4_COMPARE TMR2_COMP13
77#define IGN5_COMPARE TMR4_COMP10
78#define IGN6_COMPARE TMR4_COMP11
79#define IGN7_COMPARE TMR4_COMP12
80#define IGN8_COMPARE TMR4_COMP13
81
82static inline void FUEL1_TIMER_ENABLE(void) {TMR1_CSCTRL0 &= ~TMR_CSCTRL_TCF1; TMR1_CSCTRL0 |= TMR_CSCTRL_TCF1EN;} //Write 1 to the TCFIEN (Channel Interrupt Enable) bit of channel 0 Status/Control
90
91static inline void FUEL1_TIMER_DISABLE(void) {TMR1_CSCTRL0 &= ~TMR_CSCTRL_TCF1EN;} //Write 0 to the TCFIEN (Channel Interrupt Enable) bit of channel 0 Status/Control
99
108
117
118// Need to handle this dynamically in the future for other frequencies
119//#define TMR_PRESCALE 128
120//#define MAX_TIMER_PERIOD ((65535U * 1000000ULL) / (F_BUS_ACTUAL / TMR_PRESCALE)) //55923 @ 600Mhz.
121#if F_CPU == 600000000
122 //Bus Clock is 150Mhz @ 600 Mhz CPU.
123 #define MAX_TIMER_PERIOD 55923UL //Time per tick = 0.8533333
124 #define uS_TO_TIMER_COMPARE(uS) (((uS) * 75UL) >> 6) //Converts a given number of uS into the required number of timer ticks until that time has passed.
125#elif F_CPU == 528000000
126 //Bus Clock is 132Mhz @ 528 Mhz CPU.
127 #define MAX_TIMER_PERIOD 63549UL //Time per tick = 0.96969696
128 #define uS_TO_TIMER_COMPARE(uS) (((uS) * 66UL) >> 6) //Converts a given number of uS into the required number of timer ticks until that time has passed.
129#elif F_CPU == 450000000
130 //Bus Clock is 150Mhz @ 450 Mhz CPU.
131 #define MAX_TIMER_PERIOD 55923UL //Time per tick = 0.8533333
132 #define uS_TO_TIMER_COMPARE(uS) (((uS) * 75UL) >> 6) //Converts a given number of uS into the required number of timer ticks until that time has passed.
133#elif F_CPU == 396000000
134 //Bus Clock is 132Mhz @ 396 Mhz CPU.
135 #define MAX_TIMER_PERIOD 63549UL //Time per tick = 0.96969696
136 #define uS_TO_TIMER_COMPARE(uS) (((uS) * 66UL) >> 6) //Converts a given number of uS into the required number of timer ticks until that time has passed.
137#elif F_CPU == 150000000
138 //Bus Clock is 75Mhz @ 150 Mhz CPU.
139 #define MAX_TIMER_PERIOD 111846UL //Time per tick = 1.706666
140 #define uS_TO_TIMER_COMPARE(uS) (((uS) * 75UL) >> 7) //Converts a given number of uS into the required number of timer ticks until that time has passed.
141#else
142 #error Unsupported CPU frequency.
143#endif
144/*
145To calculate the above uS_TO_TIMER_COMPARE
146Choose number of bit of precision. Eg: 6
147Divide 2^6 by the time per tick (0.853333) = 75
148Multiply and bitshift back by the precision: (uS * 75) >> 6
149*/
150
151
152/*
153***********************************************************************************************************
154* Auxiliaries
155*/
156#define ENABLE_BOOST_TIMER() PIT_TCTRL1 |= PIT_TCTRL_TEN
157#define DISABLE_BOOST_TIMER() PIT_TCTRL1 &= ~PIT_TCTRL_TEN
158
159#define ENABLE_VVT_TIMER() PIT_TCTRL2 |= PIT_TCTRL_TEN
160#define DISABLE_VVT_TIMER() PIT_TCTRL2 &= ~PIT_TCTRL_TEN
161
162//Ran out of timers, this most likely won't work. This should be possible to implement with the GPT timer.
163#define ENABLE_FAN_TIMER() TMR3_CSCTRL1 |= TMR_CSCTRL_TCF2EN
164#define DISABLE_FAN_TIMER() TMR3_CSCTRL1 &= ~TMR_CSCTRL_TCF2EN
165
166#define BOOST_TIMER_COMPARE PIT_LDVAL1
167#define BOOST_TIMER_COUNTER 0
168#define VVT_TIMER_COMPARE PIT_LDVAL2
169#define VVT_TIMER_COUNTER 0
170
171//these probaply need to be PIT_LDVAL something???
172#define FAN_TIMER_COMPARE TMR3_COMP22
173#define FAN_TIMER_COUNTER TMR3_CNTR1
174
175/*
176***********************************************************************************************************
177* Idle
178*/
179#define IDLE_COUNTER 0
180#define IDLE_COMPARE PIT_LDVAL0
181
182#define IDLE_TIMER_ENABLE() PIT_TCTRL0 |= PIT_TCTRL_TEN
183#define IDLE_TIMER_DISABLE() PIT_TCTRL0 &= ~PIT_TCTRL_TEN
184
185/*
186***********************************************************************************************************
187* CAN / Second serial
188*/
189#define SECONDARY_SERIAL_T HardwareSerial
190
191#include <FlexCAN_T4.h>
192#define NATIVE_CAN_AVAILABLE //Disable for now as it causes lockup
static void IGN1_TIMER_ENABLE(void)
Definition board_teensy41.h:100
static void IGN3_TIMER_DISABLE(void)
Definition board_teensy41.h:111
static void IGN1_TIMER_DISABLE(void)
Definition board_teensy41.h:109
static void FUEL2_TIMER_ENABLE(void)
Definition board_teensy41.h:83
static void FUEL1_TIMER_DISABLE(void)
Definition board_teensy41.h:91
static void FUEL4_TIMER_DISABLE(void)
Definition board_teensy41.h:94
static void IGN3_TIMER_ENABLE(void)
Definition board_teensy41.h:102
static void FUEL4_TIMER_ENABLE(void)
Definition board_teensy41.h:85
int eeprom_address_t
Definition board_teensy41.h:21
bool pinIsSerial(uint8_t)
static void FUEL5_TIMER_DISABLE(void)
Definition board_teensy41.h:95
static void FUEL2_TIMER_DISABLE(void)
Definition board_teensy41.h:92
static void FUEL3_TIMER_DISABLE(void)
Definition board_teensy41.h:93
static void IGN5_TIMER_ENABLE(void)
Definition board_teensy41.h:104
static void FUEL7_TIMER_DISABLE(void)
Definition board_teensy41.h:97
static void FUEL7_TIMER_ENABLE(void)
Definition board_teensy41.h:88
static void FUEL3_TIMER_ENABLE(void)
Definition board_teensy41.h:84
static void FUEL6_TIMER_ENABLE(void)
Definition board_teensy41.h:87
static void IGN6_TIMER_DISABLE(void)
Definition board_teensy41.h:114
static void FUEL6_TIMER_DISABLE(void)
Definition board_teensy41.h:96
static void IGN7_TIMER_DISABLE(void)
Definition board_teensy41.h:115
static void IGN2_TIMER_DISABLE(void)
Definition board_teensy41.h:110
static void IGN8_TIMER_DISABLE(void)
Definition board_teensy41.h:116
static void IGN6_TIMER_ENABLE(void)
Definition board_teensy41.h:105
static void IGN2_TIMER_ENABLE(void)
Definition board_teensy41.h:101
static void IGN8_TIMER_ENABLE(void)
Definition board_teensy41.h:107
static void FUEL8_TIMER_DISABLE(void)
Definition board_teensy41.h:98
static void IGN4_TIMER_ENABLE(void)
Definition board_teensy41.h:103
static void IGN7_TIMER_ENABLE(void)
Definition board_teensy41.h:106
static void IGN5_TIMER_DISABLE(void)
Definition board_teensy41.h:113
static void IGN4_TIMER_DISABLE(void)
Definition board_teensy41.h:112
static void FUEL1_TIMER_ENABLE(void)
Definition board_teensy41.h:82
static void FUEL8_TIMER_ENABLE(void)
Definition board_teensy41.h:89
static void FUEL5_TIMER_ENABLE(void)
Definition board_teensy41.h:86
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:348