Skip to content

Commit bdf5c70

Browse files
authored
Separate board headers from globals (speeduino#1338)
* Break *circular* header dependencies: globals.h -> board_avr2560.h -> globals.h 1. Move board header selection into board_selector.h 2. Remove BOARD_H - board_definition.h should include the correct header 3. Put board specific definitions in the board specific header files. * Push board specific definitions out of board_definition.h and into board headers * Remove unecessary forward declares * Push board specific RTC init into board cpp files * Push Serial init into board cpp files, since there is Teensy 4.1 specific code * Add boardInitPins() into each board cpp files, move Teensy 4.1 specific code into it.
1 parent ee3519f commit bdf5c70

26 files changed

+689
-730
lines changed

speeduino/SD_logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "globals.h"
2-
#include BOARD_H
2+
#include "board_definition.h"
33

44
#ifdef SD_LOGGING
55
#include <SPI.h>

speeduino/acc_mc33810.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <SPI.h>
55
#include "port_pin.h"
66
#include "globals.h"
7-
#include BOARD_H //Note that this is not a real file, it is defined in globals.h.
7+
#include "board_definition.h"
88

99
extern PORT_TYPE mc33810_1_pin_port;
1010
extern PINMASK_TYPE mc33810_1_pin_mask;

speeduino/auxiliaries.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef AUX_H
22
#define AUX_H
33

4-
#include BOARD_H //Note that this is not a real file, it is defined in globals.h.
4+
#include "board_definition.h"
55

66
#include <SimplyAtomic.h>
77
#include "port_pin.h"

speeduino/board_avr2560.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include "globals.h"
1+
#include "board_definition.h"
2+
23
#if defined(CORE_AVR)
3-
#include "board_avr2560.h"
4+
#include "globals.h"
45
#include "auxiliaries.h"
56
#include "comms_secondary.h"
67
#include "idle.h"
@@ -18,7 +19,7 @@
1819
#define TIMER_MODE_CTC ((1<<WGM01)|(0<<WGM00))
1920
#define TIMER_MODE_FASTPWM ((1<<WGM01)|(1<<WGM00))
2021

21-
void initBoard(void)
22+
void initBoard(uint32_t baudRate)
2223
{
2324
/*
2425
***********************************************************************************************************
@@ -93,6 +94,7 @@ void initBoard(void)
9394
TCCR4B = TIMER_PRESCALER_64; //Timer4 Control Reg B: Timer Prescaler set to 64.
9495
TIFR4 = (1 << OCF4A) | (1<<OCF4B) | (1<<OCF4C) | (1<<TOV4) | (1<<ICF4); //Clear the compare flags, overflow flag and external input flag bits
9596

97+
Serial.begin(baudRate);
9698
}
9799

98100
/*
@@ -123,4 +125,14 @@ uint8_t getSystemTemp()
123125
return 0;
124126
}
125127

126-
#endif //CORE_AVR
128+
void boardInitRTC(void)
129+
{
130+
// Do nothing
131+
}
132+
133+
void boardInitPins(void)
134+
{
135+
// Do nothing
136+
}
137+
138+
#endif //CORE_AVR

speeduino/board_avr2560.h

Lines changed: 86 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#ifndef AVR2560_H
2-
#define AVR2560_H
1+
#pragma once
32

4-
#include "globals.h"
5-
#if defined(CORE_AVR)
3+
/** DO NOT INCLUDE DIRECTLY - should be included via board_definition.h */
4+
5+
#define CORE_AVR
66

77
#include <avr/interrupt.h>
88
#include <avr/io.h>
@@ -11,71 +11,80 @@
1111
***********************************************************************************************************
1212
* General
1313
*/
14-
#define COMPARE_TYPE uint16_t
15-
#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
16-
#define FPU_MAX_SIZE 0 //Size of the FPU buffer. 0 means no FPU.
17-
#ifdef USE_SPI_EEPROM
18-
#define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
19-
typedef uint16_t eeprom_address_t;
20-
#else
21-
#define EEPROM_LIB_H <EEPROM.h>
22-
typedef int eeprom_address_t;
23-
#endif
24-
#ifdef PLATFORMIO
25-
#define RTC_LIB_H <TimeLib.h>
26-
#else
27-
#define RTC_LIB_H <Time.h>
28-
#endif
29-
void initBoard(void);
30-
uint16_t freeRam(void);
31-
void doSystemReset(void);
32-
void jumpToBootloader(void);
33-
uint8_t getSystemTemp();
34-
35-
#define pinIsReserved(pin) ( ((pin) == 0) ) //Forbidden pins like USB on other boards
14+
#define BOARD_MAX_DIGITAL_PINS 54 //digital pins +1
15+
#define BOARD_MAX_IO_PINS 70 //digital pins + analog channels + 1
16+
#define BOARD_MAX_ADC_PINS 15 //Number of analog pins
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+
28+
#define COMPARE_TYPE uint16_t
29+
#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
30+
#define FPU_MAX_SIZE 0 //Size of the FPU buffer. 0 means no FPU.
31+
#ifdef USE_SPI_EEPROM
32+
#define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
33+
typedef uint16_t eeprom_address_t;
34+
#else
35+
#define EEPROM_LIB_H <EEPROM.h>
36+
typedef int eeprom_address_t;
37+
#endif
38+
#ifdef PLATFORMIO
39+
#define RTC_LIB_H <TimeLib.h>
40+
#else
41+
#define RTC_LIB_H <Time.h>
42+
#endif
43+
44+
#define pinIsReserved(pin) ( ((pin) == 0) ) //Forbidden pins like USB on other boards
3645

3746
/*
3847
***********************************************************************************************************
3948
* Schedules
4049
*/
4150
//Refer to svn.savannah.nongnu.org/viewvc/trunk/avr-libc/include/avr/iomxx0_1.h?root=avr-libc&view=markup
42-
#define FUEL1_COUNTER TCNT3
43-
#define FUEL2_COUNTER TCNT3
44-
#define FUEL3_COUNTER TCNT3
45-
#define FUEL4_COUNTER TCNT4
46-
#define FUEL5_COUNTER TCNT4
47-
#define FUEL6_COUNTER TCNT4 //Replaces ignition 4
48-
#define FUEL7_COUNTER TCNT5 //Replaces ignition 3
49-
#define FUEL8_COUNTER TCNT5 //Replaces ignition 2
50-
51-
#define IGN1_COUNTER TCNT5
52-
#define IGN2_COUNTER TCNT5
53-
#define IGN3_COUNTER TCNT5
54-
#define IGN4_COUNTER TCNT4
55-
#define IGN5_COUNTER TCNT4
56-
#define IGN6_COUNTER TCNT4 //Replaces injector 4
57-
#define IGN7_COUNTER TCNT3 //Replaces injector 3
58-
#define IGN8_COUNTER TCNT3 //Replaces injector 2
59-
60-
#define FUEL1_COMPARE OCR3A
61-
#define FUEL2_COMPARE OCR3B
62-
#define FUEL3_COMPARE OCR3C
63-
#define FUEL4_COMPARE OCR4B //Replaces ignition 6
64-
#define FUEL5_COMPARE OCR4C //Replaces ignition 5
65-
#define FUEL6_COMPARE OCR4A //Replaces ignition 4
66-
#define FUEL7_COMPARE OCR5C //Replaces ignition 3
67-
#define FUEL8_COMPARE OCR5B //Replaces ignition 2
68-
69-
#define IGN1_COMPARE OCR5A
70-
#define IGN2_COMPARE OCR5B
71-
#define IGN3_COMPARE OCR5C
72-
#define IGN4_COMPARE OCR4A //Replaces injector 6
73-
#define IGN5_COMPARE OCR4C //Replaces injector 5
74-
#define IGN6_COMPARE OCR4B //Replaces injector 4
75-
#define IGN7_COMPARE OCR3C //Replaces injector 3
76-
#define IGN8_COMPARE OCR3B //Replaces injector 2
77-
78-
//Note that the interrupt flag is reset BEFORE the interrupt is enabled
51+
#define FUEL1_COUNTER TCNT3
52+
#define FUEL2_COUNTER TCNT3
53+
#define FUEL3_COUNTER TCNT3
54+
#define FUEL4_COUNTER TCNT4
55+
#define FUEL5_COUNTER TCNT4
56+
#define FUEL6_COUNTER TCNT4 //Replaces ignition 4
57+
#define FUEL7_COUNTER TCNT5 //Replaces ignition 3
58+
#define FUEL8_COUNTER TCNT5 //Replaces ignition 2
59+
60+
#define IGN1_COUNTER TCNT5
61+
#define IGN2_COUNTER TCNT5
62+
#define IGN3_COUNTER TCNT5
63+
#define IGN4_COUNTER TCNT4
64+
#define IGN5_COUNTER TCNT4
65+
#define IGN6_COUNTER TCNT4 //Replaces injector 4
66+
#define IGN7_COUNTER TCNT3 //Replaces injector 3
67+
#define IGN8_COUNTER TCNT3 //Replaces injector 2
68+
69+
#define FUEL1_COMPARE OCR3A
70+
#define FUEL2_COMPARE OCR3B
71+
#define FUEL3_COMPARE OCR3C
72+
#define FUEL4_COMPARE OCR4B //Replaces ignition 6
73+
#define FUEL5_COMPARE OCR4C //Replaces ignition 5
74+
#define FUEL6_COMPARE OCR4A //Replaces ignition 4
75+
#define FUEL7_COMPARE OCR5C //Replaces ignition 3
76+
#define FUEL8_COMPARE OCR5B //Replaces ignition 2
77+
78+
#define IGN1_COMPARE OCR5A
79+
#define IGN2_COMPARE OCR5B
80+
#define IGN3_COMPARE OCR5C
81+
#define IGN4_COMPARE OCR4A //Replaces injector 6
82+
#define IGN5_COMPARE OCR4C //Replaces injector 5
83+
#define IGN6_COMPARE OCR4B //Replaces injector 4
84+
#define IGN7_COMPARE OCR3C //Replaces injector 3
85+
#define IGN8_COMPARE OCR3B //Replaces injector 2
86+
87+
//Note that the interrupt flag is reset BEFORE the interrupt is enabled
7988
static inline void FUEL1_TIMER_ENABLE(void) { TIFR3 |= (1<<OCF3A) ; TIMSK3 |= (1 << OCIE3A); } //Turn on the A compare unit (ie turn on the interrupt)
8089
static inline void FUEL2_TIMER_ENABLE(void) { TIFR3 |= (1<<OCF3B); TIMSK3 |= (1 << OCIE3B); } //Turn on the B compare unit (ie turn on the interrupt)
8190
static inline void FUEL3_TIMER_ENABLE(void) { TIFR3 |= (1<<OCF3C); TIMSK3 |= (1 << OCIE3C); } //Turn on the C compare unit (ie turn on the interrupt)
@@ -113,38 +122,35 @@ static inline void IGN6_TIMER_DISABLE(void) { TIMSK4 &= ~(1 << OCIE4B); } //Repl
113122
static inline void IGN7_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3C); } //Replaces injector 3
114123
static inline void IGN8_TIMER_DISABLE(void) { TIMSK3 &= ~(1 << OCIE3B); } //Replaces injector 2
115124

116-
#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)
117-
#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
125+
#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)
126+
#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
118127

119128
/*
120129
***********************************************************************************************************
121130
* Auxiliaries
122131
*/
123-
#define ENABLE_BOOST_TIMER() TIMSK1 |= (1 << OCIE1A)
124-
#define DISABLE_BOOST_TIMER() TIMSK1 &= ~(1 << OCIE1A)
125-
#define ENABLE_VVT_TIMER() TIMSK1 |= (1 << OCIE1B)
126-
#define DISABLE_VVT_TIMER() TIMSK1 &= ~(1 << OCIE1B)
132+
#define ENABLE_BOOST_TIMER() TIMSK1 |= (1 << OCIE1A)
133+
#define DISABLE_BOOST_TIMER() TIMSK1 &= ~(1 << OCIE1A)
134+
#define ENABLE_VVT_TIMER() TIMSK1 |= (1 << OCIE1B)
135+
#define DISABLE_VVT_TIMER() TIMSK1 &= ~(1 << OCIE1B)
127136

128-
#define BOOST_TIMER_COMPARE OCR1A
129-
#define BOOST_TIMER_COUNTER TCNT1
130-
#define VVT_TIMER_COMPARE OCR1B
131-
#define VVT_TIMER_COUNTER TCNT1
137+
#define BOOST_TIMER_COMPARE OCR1A
138+
#define BOOST_TIMER_COUNTER TCNT1
139+
#define VVT_TIMER_COMPARE OCR1B
140+
#define VVT_TIMER_COUNTER TCNT1
132141

133142
/*
134143
***********************************************************************************************************
135144
* Idle
136145
*/
137-
#define IDLE_COUNTER TCNT1
138-
#define IDLE_COMPARE OCR1C
146+
#define IDLE_COUNTER TCNT1
147+
#define IDLE_COMPARE OCR1C
139148

140-
#define IDLE_TIMER_ENABLE() TIMSK1 |= (1 << OCIE1C)
141-
#define IDLE_TIMER_DISABLE() TIMSK1 &= ~(1 << OCIE1C)
149+
#define IDLE_TIMER_ENABLE() TIMSK1 |= (1 << OCIE1C)
150+
#define IDLE_TIMER_DISABLE() TIMSK1 &= ~(1 << OCIE1C)
142151

143152
/*
144153
***********************************************************************************************************
145154
* CAN / Second serial
146155
*/
147156
#define SECONDARY_SERIAL_T HardwareSerial
148-
149-
#endif //CORE_AVR
150-
#endif //AVR2560_H

speeduino/board_definition.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
3+
/** @file
4+
* @brief Inclusion of board specific header files and board related definitions.
5+
*
6+
* @note This file should be named "board.h", but one of the STM32 Arduino implementations
7+
* has a <board.h> include. Which picks up *this file* instead of the intended file :-(
8+
*/
9+
10+
#include <stdint.h>
11+
#include <Arduino.h>
12+
13+
/**
14+
* @brief Initialise the board, including USB comms
15+
*
16+
* This is called after the tune is loaded from EEPROM, but before pins are assigned.
17+
*
18+
* @param baudRate The Serial comms baud rate
19+
*/
20+
void initBoard(uint32_t baudRate);
21+
22+
/**
23+
* @brief Pin specific initialisation (optional - can be empty)
24+
*
25+
* This is called *after* the pins are assigned and therefore after initBoard()
26+
*/
27+
void boardInitPins(void);
28+
29+
/** @brief Calculate free RAM for display in TunerStudio */
30+
uint16_t freeRam(void);
31+
32+
/** @brief Reset the board (optional) */
33+
void doSystemReset(void);
34+
35+
/** @brief Trigger the boot loader (optional) */
36+
void jumpToBootloader(void);
37+
38+
/** @brief Get the board temp for display in TunerStudio (optional) */
39+
uint8_t getSystemTemp(void);
40+
41+
// Include a specific header for a board.
42+
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
43+
#include "board_avr2560.h"
44+
#elif defined(CORE_TEENSY)
45+
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
46+
#include "board_teensy35.h"
47+
#elif defined(__IMXRT1062__)
48+
#include "board_teensy41.h"
49+
#endif
50+
#elif defined(STM32_MCU_SERIES) || defined(ARDUINO_ARCH_STM32) || defined(STM32)
51+
#include "board_stm32_official.h"
52+
#elif defined(__SAME51J19A__)
53+
#include "board_same51.h"
54+
// Allow external injection of the board definition via compiler flags
55+
#elif defined(EXTERNAL_BOARD_H)
56+
#include EXTERNAL_BOARD_H
57+
#else
58+
#error Incorrect board selected. Please select the correct board (Usually Mega 2560) and upload again
59+
#endif
60+
61+
#if defined(RTC_ENABLED)
62+
/** @brief Board specific RTC system initialisation (optional) */
63+
void boardInitRTC(void);
64+
#endif

speeduino/board_same51.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#include "board_definition.h"
2+
13
#if defined(CORE_SAME51)
2-
#include "globals.h"
3-
#include "auxiliaries.h"
44

5-
void initBoard()
5+
void initBoard(uint32_t baudRate)
66
{
77
/*
88
***********************************************************************************************************
@@ -28,6 +28,8 @@ void initBoard()
2828
***********************************************************************************************************
2929
* Schedules
3030
*/
31+
32+
Serial.begin(baudRate);
3133
}
3234

3335
uint16_t freeRam()
@@ -38,4 +40,15 @@ uint16_t freeRam()
3840
void doSystemReset() { return; }
3941
void jumpToBootloader() { return; }
4042

43+
void boardInitRTC(void)
44+
{
45+
// Do nothing
46+
}
47+
48+
49+
void boardInitPins(void)
50+
{
51+
// Do nothing
52+
}
53+
4154
#endif

0 commit comments

Comments
 (0)