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