Speeduino
Loading...
Searching...
No Matches
board_stm32_official.h
Go to the documentation of this file.
1#ifndef STM32OFFICIAL_H
2#define STM32OFFICIAL_H
3#include <Arduino.h>
4#if defined(STM32_CORE_VERSION_MAJOR)
5#include <HardwareTimer.h>
6#include <HardwareSerial.h>
7#include "STM32RTC.h"
8#include <SPI.h>
9
10#ifndef PLATFORMIO
11 #ifndef USBCON
12 #error "USBCON must be defined in boards.txt"
13 #endif
14 #ifndef USBD_USE_CDC
15 #error "USBD_USE_CDC must be defined in boards.txt"
16 #endif
17#endif
18
19#if defined(STM32F1)
20 #include "stm32f1xx_ll_tim.h"
21#elif defined(STM32F3)
22 #include "stm32f3xx_ll_tim.h"
23#elif defined(STM32F4)
24 #include "stm32f4xx_ll_tim.h"
25 #if defined(STM32F407xx) && !defined(HAL_CAN_MODULE_ENABLED)
26 #warning "CAN module is not enabled. Internal CAN will NOT be available"
27 #endif
28#else /*Default should be STM32F4*/
29 #include "stm32f4xx_ll_tim.h"
30#endif
31/*
32***********************************************************************************************************
33* General
34*/
35#define COMPARE_TYPE uint16_t
36#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)
37#define FPU_MAX_SIZE 32 //Size of the FPU buffer. 0 means no FPU.
38#define TIMER_RESOLUTION 4
39
40//Select one for EEPROM,the default is EEPROM emulation on internal flash.
41//#define SRAM_AS_EEPROM /*Use 4K battery backed SRAM, requires a 3V continuous source (like battery) connected to Vbat pin */
42//#define USE_SPI_EEPROM PB0 /*Use M25Qxx SPI flash on BlackF407VE*/
43//#define FRAM_AS_EEPROM /*Use FRAM like FM25xxx, MB85RSxxx or any SPI compatible */
44
45#ifndef word
46 #define word(h, l) ((h << 8) | l) //word() function not defined for this platform in the main library
47#endif
48
49#if defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLUEPILL_F103CB) \
50 || defined(ARDUINO_BLACKPILL_F401CC) || defined(ARDUINO_BLACKPILL_F411CE)
51 //STM32 Pill boards
52 #ifndef NUM_DIGITAL_PINS
53 #define NUM_DIGITAL_PINS 35
54 #endif
55 #ifndef LED_BUILTIN
56 #define LED_BUILTIN PB1 //Maple Mini
57 #endif
58#elif defined(STM32F407xx)
59 #ifndef NUM_DIGITAL_PINS
60 #define NUM_DIGITAL_PINS 75
61 #endif
62#endif
63
64//Specific mode for Bluepill due to its small flash size. This disables a number of strings from being compiled into the flash
65#if defined(MCU_STM32F103C8) || defined(MCU_STM32F103CB)
66 #define SMALL_FLASH_MODE
67#endif
68
69#define BOARD_MAX_DIGITAL_PINS NUM_DIGITAL_PINS
70#define BOARD_MAX_IO_PINS NUM_DIGITAL_PINS
71#if __GNUC__ < 7 //Already included on GCC 7
72extern "C" char* sbrk(int incr); //Used to freeRam
73#endif
74#ifndef digitalPinToInterrupt
75inline uint32_t digitalPinToInterrupt(uint32_t Interrupt_pin) { return Interrupt_pin; } //This isn't included in the stm32duino libs (yet)
76#endif
77
78#if defined(USER_BTN)
79 #define EEPROM_RESET_PIN USER_BTN //onboard key0 for black STM32F407 boards and blackpills, keep pressed during boot to reset eeprom
80#endif
81
82#if defined(STM32F407xx)
83 //Comment out this to disable SD logging for STM32 if needed. Currently SD logging for STM32 is experimental feature for F407.
84 #define SD_LOGGING
85#endif
86
87#if defined(SD_LOGGING)
88 #define RTC_ENABLED
89 //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.
90 extern SPIClass SD_SPI; //SPI3_MOSI, SPI3_MISO, SPI3_SCK
91 #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(50), &SD_SPI)
92 //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.
93 //#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(50), &SPI_for_flash)
94#endif
95
96//When building for Black board Serial1 is instantiated,building generic STM32F4x7 has serial2 and serial 1 must be done here
97#if SERIAL_UART_INSTANCE==2
99#endif
100
101extern STM32RTC& rtc;
102
103void initBoard();
105void doSystemReset();
106void jumpToBootloader();
108
109#if defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLUEPILL_F103CB) \
110 || defined(ARDUINO_BLACKPILL_F401CC) || defined(ARDUINO_BLACKPILL_F411CE)
111 #define pinIsReserved(pin) ( ((pin) == PA11) || ((pin) == PA12) || ((pin) == PC14) || ((pin) == PC15) )
112
113 #ifndef PB11 //Hack for F4 BlackPills
114 #define PB11 PB10
115 #endif
116 //Hack to allow compilation on small STM boards
117 #ifndef A10
118 #define A10 PA0
119 #define A11 PA1
120 #define A12 PA2
121 #define A13 PA3
122 #define A14 PA4
123 #define A15 PA5
124 #endif
125#else
126 #ifdef USE_SPI_EEPROM
127 #define pinIsReserved(pin) ( ((pin) == PA11) || ((pin) == PA12) || ((pin) == PB3) || ((pin) == PB4) || ((pin) == PB5) || ((pin) == USE_SPI_EEPROM) ) //Forbidden pins like USB
128 #else
129 #define pinIsReserved(pin) ( ((pin) == PA11) || ((pin) == PA12) || ((pin) == PB3) || ((pin) == PB4) || ((pin) == PB5) || ((pin) == PB0) ) //Forbidden pins like USB
130 #endif
131#endif
132
133#define PWM_FAN_AVAILABLE
134
135#ifndef LED_BUILTIN
136 #define LED_BUILTIN PA7
137#endif
138
139/*
140***********************************************************************************************************
141* EEPROM emulation
142*/
143#if defined(SRAM_AS_EEPROM)
144 #define EEPROM_LIB_H "src/BackupSram/BackupSramAsEEPROM.h"
146 #include EEPROM_LIB_H
148
149#elif defined(USE_SPI_EEPROM)
150 #define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
152 #include EEPROM_LIB_H
153 extern SPIClass SPI_for_flash; //SPI1_MOSI, SPI1_MISO, SPI1_SCK
154
155 //windbond W25Q16 SPI flash EEPROM emulation
159
160#elif defined(FRAM_AS_EEPROM) //https://github.com/VitorBoss/FRAM
161 #define EEPROM_LIB_H "src/FRAM/Fram.h"
163 #include EEPROM_LIB_H
164 #if defined(STM32F407xx)
165 extern FramClass EEPROM; /*(mosi, miso, sclk, ssel, clockspeed) 31/01/2020*/
166 #else
167 extern FramClass EEPROM; //Blue/Black Pills
168 #endif
169
170#else //default case, internal flash as EEPROM
171 #define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
173 #include EEPROM_LIB_H
175 #if defined(STM32F401xC)
176 #define SMALL_FLASH_MODE
177 #endif
178#endif
179
180
181#define RTC_LIB_H "STM32RTC.h"
182
183/*
184***********************************************************************************************************
185* Schedules
186* Timers Table for STM32F1
187* TIMER1 TIMER2 TIMER3 TIMER4
188* 1 - FAN 1 - INJ1 1 - IGN1 1 - oneMSInterval
189* 2 - BOOST 2 - INJ2 2 - IGN2 2 -
190* 3 - VVT 3 - INJ3 3 - IGN3 3 -
191* 4 - IDLE 4 - INJ4 4 - IGN4 4 -
192*
193* Timers Table for STM32F4
194* TIMER1 | TIMER2 | TIMER3 | TIMER4 | TIMER5 | TIMER11
195* 1 - FAN |1 - IGN1 |1 - INJ1 |1 - IGN5 |1 - INJ5 |1 - oneMSInterval
196* 2 - BOOST |2 - IGN2 |2 - INJ2 |2 - IGN6 |2 - INJ6 |
197* 3 - VVT |3 - IGN3 |3 - INJ3 |3 - IGN7 |3 - INJ7 |
198* 4 - IDLE |4 - IGN4 |4 - INJ4 |4 - IGN8 |4 - INJ8 |
199*/
200#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)
201#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
202
203#define FUEL1_COUNTER (TIM3)->CNT
204#define FUEL2_COUNTER (TIM3)->CNT
205#define FUEL3_COUNTER (TIM3)->CNT
206#define FUEL4_COUNTER (TIM3)->CNT
207
208#define FUEL1_COMPARE (TIM3)->CCR1
209#define FUEL2_COMPARE (TIM3)->CCR2
210#define FUEL3_COMPARE (TIM3)->CCR3
211#define FUEL4_COMPARE (TIM3)->CCR4
212
213#define IGN1_COUNTER (TIM2)->CNT
214#define IGN2_COUNTER (TIM2)->CNT
215#define IGN3_COUNTER (TIM2)->CNT
216#define IGN4_COUNTER (TIM2)->CNT
217
218#define IGN1_COMPARE (TIM2)->CCR1
219#define IGN2_COMPARE (TIM2)->CCR2
220#define IGN3_COMPARE (TIM2)->CCR3
221#define IGN4_COMPARE (TIM2)->CCR4
222
223#define FUEL5_COUNTER (TIM5)->CNT
224#define FUEL6_COUNTER (TIM5)->CNT
225#define FUEL7_COUNTER (TIM5)->CNT
226#define FUEL8_COUNTER (TIM5)->CNT
227
228#define FUEL5_COMPARE (TIM5)->CCR1
229#define FUEL6_COMPARE (TIM5)->CCR2
230#define FUEL7_COMPARE (TIM5)->CCR3
231#define FUEL8_COMPARE (TIM5)->CCR4
232
233#define IGN5_COUNTER (TIM4)->CNT
234#define IGN6_COUNTER (TIM4)->CNT
235#define IGN7_COUNTER (TIM4)->CNT
236#define IGN8_COUNTER (TIM4)->CNT
237
238#define IGN5_COMPARE (TIM4)->CCR1
239#define IGN6_COMPARE (TIM4)->CCR2
240#define IGN7_COMPARE (TIM4)->CCR3
241#define IGN8_COMPARE (TIM4)->CCR4
242
243
244static inline void FUEL1_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC1; (TIM3)->DIER |= TIM_DIER_CC1IE;}
245static inline void FUEL2_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC2; (TIM3)->DIER |= TIM_DIER_CC2IE;}
246static inline void FUEL3_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC3; (TIM3)->DIER |= TIM_DIER_CC3IE;}
247static inline void FUEL4_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC4; (TIM3)->DIER |= TIM_DIER_CC4IE;}
248
249static inline void FUEL1_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC1IE;}
250static inline void FUEL2_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC2IE;}
251static inline void FUEL3_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC3IE;}
252static inline void FUEL4_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC4IE;}
253
254static inline void IGN1_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC1; (TIM2)->DIER |= TIM_DIER_CC1IE;}
255static inline void IGN2_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC2; (TIM2)->DIER |= TIM_DIER_CC2IE;}
256static inline void IGN3_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC3; (TIM2)->DIER |= TIM_DIER_CC3IE;}
257static inline void IGN4_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC4; (TIM2)->DIER |= TIM_DIER_CC4IE;}
258
259static inline void IGN1_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC1IE;}
260static inline void IGN2_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC2IE;}
261static inline void IGN3_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC3IE;}
262static inline void IGN4_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC4IE;}
263
264static 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;}
265static 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;}
266static 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;}
267static 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;}
268
269static inline void FUEL5_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC1IE;}
270static inline void FUEL6_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC2IE;}
271static inline void FUEL7_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC3IE;}
272static inline void FUEL8_TIMER_DISABLE(void) {(TIM5)->DIER &= ~TIM_DIER_CC4IE;}
273
274static inline void IGN5_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC1; (TIM4)->DIER |= TIM_DIER_CC1IE;}
275static inline void IGN6_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC2; (TIM4)->DIER |= TIM_DIER_CC2IE;}
276static inline void IGN7_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC3; (TIM4)->DIER |= TIM_DIER_CC3IE;}
277static inline void IGN8_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC4; (TIM4)->DIER |= TIM_DIER_CC4IE;}
278
279static inline void IGN5_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC1IE;}
280static inline void IGN6_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC2IE;}
281static inline void IGN7_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC3IE;}
282static inline void IGN8_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC4IE;}
283
284
285/*
286***********************************************************************************************************
287* Auxiliaries
288*/
289#define ENABLE_BOOST_TIMER() (TIM1)->SR = ~TIM_FLAG_CC2; (TIM1)->DIER |= TIM_DIER_CC2IE; (TIM1)->CR1 |= TIM_CR1_CEN;
290#define DISABLE_BOOST_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC2IE
291
292#define ENABLE_VVT_TIMER() (TIM1)->SR = ~TIM_FLAG_CC3; (TIM1)->DIER |= TIM_DIER_CC3IE; (TIM1)->CR1 |= TIM_CR1_CEN;
293#define DISABLE_VVT_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC3IE
294
295#define ENABLE_FAN_TIMER() (TIM1)->SR = ~TIM_FLAG_CC1; (TIM1)->DIER |= TIM_DIER_CC1IE; (TIM1)->CR1 |= TIM_CR1_CEN;
296#define DISABLE_FAN_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC1IE
297
298#define BOOST_TIMER_COMPARE (TIM1)->CCR2
299#define BOOST_TIMER_COUNTER (TIM1)->CNT
300#define VVT_TIMER_COMPARE (TIM1)->CCR3
301#define VVT_TIMER_COUNTER (TIM1)->CNT
302#define FAN_TIMER_COMPARE (TIM1)->CCR1
303#define FAN_TIMER_COUNTER (TIM1)->CNT
304
305/*
306***********************************************************************************************************
307* Idle
308*/
309#define IDLE_COUNTER (TIM1)->CNT
310#define IDLE_COMPARE (TIM1)->CCR4
311
312#define IDLE_TIMER_ENABLE() (TIM1)->SR = ~TIM_FLAG_CC4; (TIM1)->DIER |= TIM_DIER_CC4IE; (TIM1)->CR1 |= TIM_CR1_CEN;
313#define IDLE_TIMER_DISABLE() (TIM1)->DIER &= ~TIM_DIER_CC4IE
314
315/*
316***********************************************************************************************************
317* Timers
318*/
319
320extern HardwareTimer Timer1;
321extern HardwareTimer Timer2;
322extern HardwareTimer Timer3;
323extern HardwareTimer Timer4;
324#if !defined(ARDUINO_BLUEPILL_F103C8) && !defined(ARDUINO_BLUEPILL_F103CB) //F103 just have 4 timers
325extern HardwareTimer Timer5;
326#if defined(TIM11)
328#elif defined(TIM7)
330#endif
331#endif
332
333#if ((STM32_CORE_VERSION_MINOR<=8) & (STM32_CORE_VERSION_MAJOR==1))
340#if (INJ_CHANNELS >= 5)
342#endif
343#if (INJ_CHANNELS >= 6)
345#endif
346#if (INJ_CHANNELS >= 7)
348#endif
349#if (INJ_CHANNELS >= 8)
351#endif
359#if (IGN_CHANNELS >= 5)
361#endif
362#if (IGN_CHANNELS >= 6)
364#endif
365#if (IGN_CHANNELS >= 7)
367#endif
368#if (IGN_CHANNELS >= 8)
370#endif
371#endif //End core<=1.8
372
373/*
374***********************************************************************************************************
375* CAN / Second serial
376*/
377#if defined(HAL_CAN_MODULE_ENABLED)
378#define NATIVE_CAN_AVAILABLE
379#include <src/STM32_CAN/STM32_CAN.h>
380//This activates CAN1 interface on STM32, but it's named as Can0, because that's how Teensy implementation is done
381extern STM32_CAN Can0;
382#endif
383
384#if defined(STM32GENERIC) // STM32GENERIC core
385 #define SECONDARY_SERIAL_T SerialUART
386#else //libmaple core aka STM32DUINO
387 #define SECONDARY_SERIAL_T HardwareSerial
388#endif
389
390#endif //CORE_STM32
391#endif //STM32_H
void vvtInterrupt(void)
Definition auxiliaries.cpp:1150
void boostInterrupt(void)
Definition auxiliaries.cpp:1120
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:348
void idleInterrupt(void)
Definition idle.cpp:761
void fuelSchedule1Interrupt()
Definition scheduler.cpp:370
void fuelSchedule3Interrupt()
Definition scheduler.cpp:390
void fuelSchedule2Interrupt()
Definition scheduler.cpp:380
void fuelSchedule4Interrupt()
Definition scheduler.cpp:400
void ignitionSchedule1Interrupt(void)
Definition scheduler.cpp:493
void oneMSInterval(void)
Definition timers.cpp:76