Speeduino
Loading...
Searching...
No Matches
board_stm32_official.h
Go to the documentation of this file.
1#pragma once
2
5#include <Arduino.h>
6#include <HardwareTimer.h>
7#include <HardwareSerial.h>
8#include "STM32RTC.h"
9#include <SPI.h>
10
11#define CORE_STM32
12
13#ifndef PLATFORMIO
14#ifndef USBCON
15 #error "USBCON must be defined in boards.txt"
16#endif
17#ifndef USBD_USE_CDC
18 #error "USBD_USE_CDC must be defined in boards.txt"
19#endif
20#endif
21
22#if defined(STM32F1)
23 #include "stm32f1xx_ll_tim.h"
24#elif defined(STM32F3)
25 #include "stm32f3xx_ll_tim.h"
26#elif defined(STM32F4)
27 #include "stm32f4xx_ll_tim.h"
28 #if defined(STM32F407xx) && !defined(HAL_CAN_MODULE_ENABLED)
29 #warning "CAN module is not enabled. Internal CAN will NOT be available"
30 #endif
31#else /*Default should be STM32F4*/
32 #include "stm32f4xx_ll_tim.h"
33#endif
34
35/*
36***********************************************************************************************************
37* General
38*/
39
44using COMPARE_TYPE = uint16_t;
45
47constexpr uint32_t TIMER_RESOLUTION = 4U;
48
50static constexpr COMPARE_TYPE uS_TO_TIMER_COMPARE(uint32_t micros)
51{
52 // Faster than micros/TIMER_RESOLUTION
53 constexpr uint32_t SHIFT = TIMER_RESOLUTION/2U;
54 return (COMPARE_TYPE)(micros >> SHIFT);
55}
56
58static constexpr uint32_t ticksToMicros(COMPARE_TYPE ticks)
59{
60 return ticks * TIMER_RESOLUTION;
61}
62
63#define TS_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)
64#define FPU_MAX_SIZE 32 //Size of the FPU buffer. 0 means no FPU.
65constexpr uint16_t BLOCKING_FACTOR = 121;
66constexpr uint16_t TABLE_BLOCKING_FACTOR = 64;
67
68#ifndef word
69 #define word(h, l) (((h) << 8) | (l)) //word() function not defined for this platform in the main library
70#endif
71
72#if defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLUEPILL_F103CB) \
73 || defined(ARDUINO_BLACKPILL_F401CC) || defined(ARDUINO_BLACKPILL_F411CE)
74 //STM32 Pill boards
75 #ifndef NUM_DIGITAL_PINS
76 #define NUM_DIGITAL_PINS 35
77 #endif
78 #ifndef LED_BUILTIN
79 #define LED_BUILTIN PB1 //Maple Mini
80 #endif
81#elif defined(STM32F407xx)
82 #ifndef NUM_DIGITAL_PINS
83 #define NUM_DIGITAL_PINS 75
84 #endif
85#endif
86
87//Specific mode for Bluepill due to its small flash size. This disables a number of strings from being compiled into the flash
88#if defined(MCU_STM32F103C8) || defined(MCU_STM32F103CB)
89 #define SMALL_FLASH_MODE
90#endif
91
92#define BOARD_MAX_DIGITAL_PINS NUM_DIGITAL_PINS
93#define BOARD_MAX_IO_PINS NUM_DIGITAL_PINS
94#if __GNUC__ < 7 //Already included on GCC 7
95extern "C" char* sbrk(int incr); //Used to freeRam
96#endif
97#ifndef digitalPinToInterrupt
98inline uint32_t digitalPinToInterrupt(uint32_t Interrupt_pin) { return Interrupt_pin; } //This isn't included in the stm32duino libs (yet)
99#endif
100
101#if defined(USER_BTN)
102 #define EEPROM_RESET_PIN USER_BTN //onboard key0 for black STM32F407 boards and blackpills, keep pressed during boot to reset eeprom
103#endif
104
105#if defined(STM32F407xx)
106 //Comment out this to disable SD logging for STM32 if needed. Currently SD logging for STM32 is experimental feature for F407.
107 #define SD_LOGGING
108#endif
109
110#if defined(SD_LOGGING)
111 #define RTC_ENABLED
112 //SD logging with STM32 uses SD card in SPI mode, because used SD library doesn't support SDIO implementation. By default SPI3 is used that uses same pins as SDIO also, but in different order.
113 extern SPIClass SD_SPI; //SPI3_MOSI, SPI3_MISO, SPI3_SCK
114 #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(50), &SD_SPI)
115 //Alternatively same SPI bus can be used as there is for SPI flash. But this is not recommended due to slower speed and other possible problems.
116 //#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(50), &SPI_for_flash)
117#endif
118
119//When building for Black board Serial1 is instantiated,building generic STM32F4x7 has serial2 and serial 1 must be done here
120#if SERIAL_UART_INSTANCE==2
121HardwareSerial Serial1(PA10, PA9);
122#endif
123
124extern STM32RTC& rtc;
125
126#if defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLUEPILL_F103CB) \
127 || defined(ARDUINO_BLACKPILL_F401CC) || defined(ARDUINO_BLACKPILL_F411CE)
128 static inline bool pinIsReserved(uint8_t pin) {
129 return (pin == (uint8_t)PA11)
130 || (pin == (uint8_t)PA12)
131 || (pin == (uint8_t)PC14)
132 || (pin == (uint8_t)PC15)
133 ;
134 }
135
136 #ifndef PB11 //Hack for F4 BlackPills
137 #define PB11 PB10
138 #endif
139 //Hack to allow compilation on small STM boards
140 #ifndef A10
141 #define A10 PA0
142 #define A11 PA1
143 #define A12 PA2
144 #define A13 PA3
145 #define A14 PA4
146 #define A15 PA5
147 #endif
148#else
149 #ifdef USE_SPI_EEPROM
150 static inline bool pinIsReserved(uint8_t pin) {
151 return (pin == (uint8_t)PA11)
152 || (pin == (uint8_t)PA12)
153 || (pin == (uint8_t)PB3)
154 || (pin == (uint8_t)PB4)
155 || (pin == (uint8_t)USE_SPI_EEPROM)
156 ;
157 }
158 #else
159 static inline bool pinIsReserved(uint8_t pin) {
160 return (pin == (uint8_t)PA11)
161 || (pin == (uint8_t)PA12)
162 || (pin == (uint8_t)PB3)
163 || (pin == (uint8_t)PB4)
164 || (pin == (uint8_t)PB5)
165 || (pin == (uint8_t)PB0)
166 ;
167 }
168 #endif
169#endif
170
171#define PWM_FAN_AVAILABLE
172
173#ifndef LED_BUILTIN
174 #define LED_BUILTIN PA7
175#endif
176
177/*
178***********************************************************************************************************
179* EEPROM emulation
180*/
181#if defined(STM32F401xC)
182 #define SMALL_FLASH_MODE
183#endif
184
185#define RTC_LIB_H "STM32RTC.h"
186
187/*
188***********************************************************************************************************
189* Schedules
190* Timers Table for STM32F1
191* TIMER1 TIMER2 TIMER3 TIMER4
192* 1 - FAN 1 - INJ1 1 - IGN1 1 - oneMSInterval
193* 2 - BOOST 2 - INJ2 2 - IGN2 2 -
194* 3 - VVT 3 - INJ3 3 - IGN3 3 -
195* 4 - IDLE 4 - INJ4 4 - IGN4 4 -
196*
197* Timers Table for STM32F4
198* TIMER1 | TIMER2 | TIMER3 | TIMER4 | TIMER5 | TIMER11
199* 1 - FAN |1 - IGN1 |1 - INJ1 |1 - IGN5 |1 - INJ5 |1 - oneMSInterval
200* 2 - BOOST |2 - IGN2 |2 - INJ2 |2 - IGN6 |2 - INJ6 |
201* 3 - VVT |3 - IGN3 |3 - INJ3 |3 - IGN7 |3 - INJ7 |
202* 4 - IDLE |4 - IGN4 |4 - INJ4 |4 - IGN8 |4 - INJ8 |
203*/
204#if defined(STM32F407xx) //F407 can do 8x8 STM32F401/STM32F411 don't
205 #define INJ_CHANNELS 8
206 #define IGN_CHANNELS 8
207#else
208 #define INJ_CHANNELS 4
209 #define IGN_CHANNELS 5
210#endif
211#define FUEL1_COUNTER (TIM3)->CNT
212#define FUEL2_COUNTER (TIM3)->CNT
213#define FUEL3_COUNTER (TIM3)->CNT
214#define FUEL4_COUNTER (TIM3)->CNT
215
216#define FUEL1_COMPARE (TIM3)->CCR1
217#define FUEL2_COMPARE (TIM3)->CCR2
218#define FUEL3_COMPARE (TIM3)->CCR3
219#define FUEL4_COMPARE (TIM3)->CCR4
220
221#define IGN1_COUNTER (TIM2)->CNT
222#define IGN2_COUNTER (TIM2)->CNT
223#define IGN3_COUNTER (TIM2)->CNT
224#define IGN4_COUNTER (TIM2)->CNT
225
226#define IGN1_COMPARE (TIM2)->CCR1
227#define IGN2_COMPARE (TIM2)->CCR2
228#define IGN3_COMPARE (TIM2)->CCR3
229#define IGN4_COMPARE (TIM2)->CCR4
230
231#define FUEL5_COUNTER (TIM5)->CNT
232#define FUEL6_COUNTER (TIM5)->CNT
233#define FUEL7_COUNTER (TIM5)->CNT
234#define FUEL8_COUNTER (TIM5)->CNT
235
236#define FUEL5_COMPARE (TIM5)->CCR1
237#define FUEL6_COMPARE (TIM5)->CCR2
238#define FUEL7_COMPARE (TIM5)->CCR3
239#define FUEL8_COMPARE (TIM5)->CCR4
240
241#define IGN5_COUNTER (TIM4)->CNT
242#define IGN6_COUNTER (TIM4)->CNT
243#define IGN7_COUNTER (TIM4)->CNT
244#define IGN8_COUNTER (TIM4)->CNT
245
246#define IGN5_COMPARE (TIM4)->CCR1
247#define IGN6_COMPARE (TIM4)->CCR2
248#define IGN7_COMPARE (TIM4)->CCR3
249#define IGN8_COMPARE (TIM4)->CCR4
250
251
252static inline void FUEL1_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC1; (TIM3)->DIER |= TIM_DIER_CC1IE;}
253static inline void FUEL2_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC2; (TIM3)->DIER |= TIM_DIER_CC2IE;}
254static inline void FUEL3_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC3; (TIM3)->DIER |= TIM_DIER_CC3IE;}
255static inline void FUEL4_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC4; (TIM3)->DIER |= TIM_DIER_CC4IE;}
256
257static inline void FUEL1_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC1IE;}
258static inline void FUEL2_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC2IE;}
259static inline void FUEL3_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC3IE;}
260static inline void FUEL4_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC4IE;}
261
262static inline void IGN1_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC1; (TIM2)->DIER |= TIM_DIER_CC1IE;}
263static inline void IGN2_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC2; (TIM2)->DIER |= TIM_DIER_CC2IE;}
264static inline void IGN3_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC3; (TIM2)->DIER |= TIM_DIER_CC3IE;}
265static inline void IGN4_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC4; (TIM2)->DIER |= TIM_DIER_CC4IE;}
266
267static inline void IGN1_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC1IE;}
268static inline void IGN2_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC2IE;}
269static inline void IGN3_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC3IE;}
270static inline void IGN4_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC4IE;}
271
272static inline void FUEL5_TIMER_ENABLE(void) {(TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->SR = ~TIM_FLAG_CC1; (TIM5)->DIER |= TIM_DIER_CC1IE;}
273static inline void FUEL6_TIMER_ENABLE(void) {(TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->SR = ~TIM_FLAG_CC2; (TIM5)->DIER |= TIM_DIER_CC2IE;}
274static inline void FUEL7_TIMER_ENABLE(void) {(TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->SR = ~TIM_FLAG_CC3; (TIM5)->DIER |= TIM_DIER_CC3IE;}
275static inline void FUEL8_TIMER_ENABLE(void) {(TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->CR1 |= TIM_CR1_CEN; (TIM5)->SR = ~TIM_FLAG_CC4; (TIM5)->DIER |= TIM_DIER_CC4IE;}
276
277static inline void FUEL5_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC1IE;}
278static inline void FUEL6_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC2IE;}
279static inline void FUEL7_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC3IE;}
280static inline void FUEL8_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC4IE;}
281
282static inline void IGN5_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC1; (TIM4)->DIER |= TIM_DIER_CC1IE;}
283static inline void IGN6_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC2; (TIM4)->DIER |= TIM_DIER_CC2IE;}
284static inline void IGN7_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC3; (TIM4)->DIER |= TIM_DIER_CC3IE;}
285static inline void IGN8_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC4; (TIM4)->DIER |= TIM_DIER_CC4IE;}
286
287static inline void IGN5_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC1IE;}
288static inline void IGN6_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC2IE;}
289static inline void IGN7_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC3IE;}
290static inline void IGN8_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC4IE;}
291
292
293/*
294***********************************************************************************************************
295* Auxiliaries
296*/
297#define ENABLE_BOOST_TIMER() (TIM1)->SR = ~TIM_FLAG_CC2; (TIM1)->DIER |= TIM_DIER_CC2IE; (TIM1)->CR1 |= TIM_CR1_CEN;
298#define DISABLE_BOOST_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC2IE
299
300#define ENABLE_VVT_TIMER() (TIM1)->SR = ~TIM_FLAG_CC3; (TIM1)->DIER |= TIM_DIER_CC3IE; (TIM1)->CR1 |= TIM_CR1_CEN;
301#define DISABLE_VVT_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC3IE
302
303#define ENABLE_FAN_TIMER() (TIM1)->SR = ~TIM_FLAG_CC1; (TIM1)->DIER |= TIM_DIER_CC1IE; (TIM1)->CR1 |= TIM_CR1_CEN;
304#define DISABLE_FAN_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC1IE
305
306#define BOOST_TIMER_COMPARE (TIM1)->CCR2
307#define BOOST_TIMER_COUNTER (TIM1)->CNT
308#define VVT_TIMER_COMPARE (TIM1)->CCR3
309#define VVT_TIMER_COUNTER (TIM1)->CNT
310#define FAN_TIMER_COMPARE (TIM1)->CCR1
311#define FAN_TIMER_COUNTER (TIM1)->CNT
312
313/*
314***********************************************************************************************************
315* Idle
316*/
317#define IDLE_COUNTER (TIM1)->CNT
318#define IDLE_COMPARE (TIM1)->CCR4
319
320#define IDLE_TIMER_ENABLE() (TIM1)->SR = ~TIM_FLAG_CC4; (TIM1)->DIER |= TIM_DIER_CC4IE; (TIM1)->CR1 |= TIM_CR1_CEN;
321#define IDLE_TIMER_DISABLE() (TIM1)->DIER &= ~TIM_DIER_CC4IE
322
323
324/*
325***********************************************************************************************************
326* CAN / Second serial
327*/
328#if defined(HAL_CAN_MODULE_ENABLED)
329#define NATIVE_CAN_AVAILABLE
330#include <src/STM32_CAN/STM32_CAN.h>
331//This activates CAN1 interface on STM32, but it's named as Can0, because that's how Teensy implementation is done
332extern STM32_CAN Can0;
333#endif
334
335#if defined(STM32GENERIC) // STM32GENERIC core
336 #define SECONDARY_SERIAL_T SerialUART
337#else //libmaple core aka STM32DUINO
338 #define SECONDARY_SERIAL_T HardwareSerial
339#endif
340
341class inputPin_t;
342using boardInputPin_t = inputPin_t;
343class outputPin_t;
344using boardOutputPin_t = outputPin_t;
345
347#if NUM_ANALOG_INPUTS==10
348constexpr uint8_t ANALOG_PINS[NUM_ANALOG_INPUTS-1] = { _ANALOG_PINS_A0_A8 };
349#else
350constexpr uint8_t ANALOG_PINS[NUM_ANALOG_INPUTS-1] = { _ANALOG_PINS_A0_A14 };
351#endif
352
357constexpr uint8_t SERIAL_BUFFER_THRESHOLD = 0U;
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:32
fastInputPin_t boardInputPin_t
Definition board_avr2560.h:174
fastOutputPin_t boardOutputPin_t
Definition board_avr2560.h:176
static void IGN1_TIMER_ENABLE(void)
Definition board_stm32_official.h:262
static void IGN3_TIMER_DISABLE(void)
Definition board_stm32_official.h:269
static void IGN1_TIMER_DISABLE(void)
Definition board_stm32_official.h:267
static void FUEL2_TIMER_ENABLE(void)
Definition board_stm32_official.h:253
static void FUEL1_TIMER_DISABLE(void)
Definition board_stm32_official.h:257
static void FUEL4_TIMER_DISABLE(void)
Definition board_stm32_official.h:260
static void IGN3_TIMER_ENABLE(void)
Definition board_stm32_official.h:264
constexpr uint8_t ANALOG_PINS[NUM_ANALOG_INPUTS-1]
Analog pin mapping.
Definition board_stm32_official.h:350
static void FUEL4_TIMER_ENABLE(void)
Definition board_stm32_official.h:255
static void FUEL5_TIMER_DISABLE(void)
Definition board_stm32_official.h:277
static void FUEL2_TIMER_DISABLE(void)
Definition board_stm32_official.h:258
static void FUEL3_TIMER_DISABLE(void)
Definition board_stm32_official.h:259
static void IGN5_TIMER_ENABLE(void)
Definition board_stm32_official.h:282
static bool pinIsReserved(uint8_t pin)
Definition board_stm32_official.h:159
static void FUEL7_TIMER_DISABLE(void)
Definition board_stm32_official.h:279
uint32_t digitalPinToInterrupt(uint32_t Interrupt_pin)
Definition board_stm32_official.h:98
constexpr uint16_t BLOCKING_FACTOR
Definition board_stm32_official.h:65
static void FUEL7_TIMER_ENABLE(void)
Definition board_stm32_official.h:274
static void FUEL3_TIMER_ENABLE(void)
Definition board_stm32_official.h:254
static void FUEL6_TIMER_ENABLE(void)
Definition board_stm32_official.h:273
static void IGN6_TIMER_DISABLE(void)
Definition board_stm32_official.h:288
static constexpr uint32_t ticksToMicros(COMPARE_TYPE ticks)
Convert timer ticks to µS.
Definition board_stm32_official.h:58
static void FUEL6_TIMER_DISABLE(void)
Definition board_stm32_official.h:278
static void IGN7_TIMER_DISABLE(void)
Definition board_stm32_official.h:289
static void IGN2_TIMER_DISABLE(void)
Definition board_stm32_official.h:268
constexpr uint32_t TIMER_RESOLUTION
The timer tick length in µS.
Definition board_stm32_official.h:47
static void IGN8_TIMER_DISABLE(void)
Definition board_stm32_official.h:290
static void IGN6_TIMER_ENABLE(void)
Definition board_stm32_official.h:283
static void IGN2_TIMER_ENABLE(void)
Definition board_stm32_official.h:263
STM32RTC & rtc
static void IGN8_TIMER_ENABLE(void)
Definition board_stm32_official.h:285
static void FUEL8_TIMER_DISABLE(void)
Definition board_stm32_official.h:280
static void IGN4_TIMER_ENABLE(void)
Definition board_stm32_official.h:265
static void IGN7_TIMER_ENABLE(void)
Definition board_stm32_official.h:284
static void IGN5_TIMER_DISABLE(void)
Definition board_stm32_official.h:287
static void IGN4_TIMER_DISABLE(void)
Definition board_stm32_official.h:270
constexpr uint8_t SERIAL_BUFFER_THRESHOLD
When the serial buffer is filled to greater than this threshold value, the serial processing operatio...
Definition board_stm32_official.h:357
static void FUEL1_TIMER_ENABLE(void)
Definition board_stm32_official.h:252
char * sbrk(int incr)
static void FUEL8_TIMER_ENABLE(void)
Definition board_stm32_official.h:275
static void FUEL5_TIMER_ENABLE(void)
Definition board_stm32_official.h:272
constexpr uint16_t TABLE_BLOCKING_FACTOR
Definition board_stm32_official.h:66
static constexpr COMPARE_TYPE uS_TO_TIMER_COMPARE(uint32_t micros)
Converts a given number of uS into the required number of timer ticks until that time has passed.
Definition board_stm32_official.h:50