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();
113
114#if defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLUEPILL_F103CB) \
115 || defined(ARDUINO_BLACKPILL_F401CC) || defined(ARDUINO_BLACKPILL_F411CE)
116 #define pinIsReserved(pin) ( ((pin) == PA11) || ((pin) == PA12) || ((pin) == PC14) || ((pin) == PC15) )
117
118 #ifndef PB11 //Hack for F4 BlackPills
119 #define PB11 PB10
120 #endif
121 //Hack to allow compilation on small STM boards
122 #ifndef A10
123 #define A10 PA0
124 #define A11 PA1
125 #define A12 PA2
126 #define A13 PA3
127 #define A14 PA4
128 #define A15 PA5
129 #endif
130#else
131 #ifdef USE_SPI_EEPROM
132 #define pinIsReserved(pin) ( ((pin) == PA11) || ((pin) == PA12) || ((pin) == PB3) || ((pin) == PB4) || ((pin) == PB5) || ((pin) == USE_SPI_EEPROM) ) //Forbidden pins like USB
133 #else
134 #define pinIsReserved(pin) ( ((pin) == PA11) || ((pin) == PA12) || ((pin) == PB3) || ((pin) == PB4) || ((pin) == PB5) || ((pin) == PB0) ) //Forbidden pins like USB
135 #endif
136#endif
137
138#define PWM_FAN_AVAILABLE
139
140#ifndef LED_BUILTIN
141 #define LED_BUILTIN PA7
142#endif
143
144/*
145***********************************************************************************************************
146* EEPROM emulation
147*/
148#if defined(SRAM_AS_EEPROM)
149 #define EEPROM_LIB_H "src/BackupSram/BackupSramAsEEPROM.h"
151 #include EEPROM_LIB_H
153
154#elif defined(USE_SPI_EEPROM)
155 #define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
157 #include EEPROM_LIB_H
158 extern SPIClass SPI_for_flash; //SPI1_MOSI, SPI1_MISO, SPI1_SCK
159
160 //windbond W25Q16 SPI flash EEPROM emulation
164
165#elif defined(FRAM_AS_EEPROM) //https://github.com/VitorBoss/FRAM
166 #define EEPROM_LIB_H "src/FRAM/Fram.h"
168 #include EEPROM_LIB_H
169 #if defined(STM32F407xx)
170 extern FramClass EEPROM; /*(mosi, miso, sclk, ssel, clockspeed) 31/01/2020*/
171 #else
172 extern FramClass EEPROM; //Blue/Black Pills
173 #endif
174
175#else //default case, internal flash as EEPROM
176 #define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h"
178 #include EEPROM_LIB_H
180 #if defined(STM32F401xC)
181 #define SMALL_FLASH_MODE
182 #endif
183#endif
184
185
186#define RTC_LIB_H "STM32RTC.h"
187
188/*
189***********************************************************************************************************
190* Schedules
191* Timers Table for STM32F1
192* TIMER1 TIMER2 TIMER3 TIMER4
193* 1 - FAN 1 - INJ1 1 - IGN1 1 - oneMSInterval
194* 2 - BOOST 2 - INJ2 2 - IGN2 2 -
195* 3 - VVT 3 - INJ3 3 - IGN3 3 -
196* 4 - IDLE 4 - INJ4 4 - IGN4 4 -
197*
198* Timers Table for STM32F4
199* TIMER1 | TIMER2 | TIMER3 | TIMER4 | TIMER5 | TIMER11
200* 1 - FAN |1 - IGN1 |1 - INJ1 |1 - IGN5 |1 - INJ5 |1 - oneMSInterval
201* 2 - BOOST |2 - IGN2 |2 - INJ2 |2 - IGN6 |2 - INJ6 |
202* 3 - VVT |3 - IGN3 |3 - INJ3 |3 - IGN7 |3 - INJ7 |
203* 4 - IDLE |4 - IGN4 |4 - INJ4 |4 - IGN8 |4 - INJ8 |
204*/
205#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)
206#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
207
208#define FUEL1_COUNTER (TIM3)->CNT
209#define FUEL2_COUNTER (TIM3)->CNT
210#define FUEL3_COUNTER (TIM3)->CNT
211#define FUEL4_COUNTER (TIM3)->CNT
212
213#define FUEL1_COMPARE (TIM3)->CCR1
214#define FUEL2_COMPARE (TIM3)->CCR2
215#define FUEL3_COMPARE (TIM3)->CCR3
216#define FUEL4_COMPARE (TIM3)->CCR4
217
218#define IGN1_COUNTER (TIM2)->CNT
219#define IGN2_COUNTER (TIM2)->CNT
220#define IGN3_COUNTER (TIM2)->CNT
221#define IGN4_COUNTER (TIM2)->CNT
222
223#define IGN1_COMPARE (TIM2)->CCR1
224#define IGN2_COMPARE (TIM2)->CCR2
225#define IGN3_COMPARE (TIM2)->CCR3
226#define IGN4_COMPARE (TIM2)->CCR4
227
228#define FUEL5_COUNTER (TIM5)->CNT
229#define FUEL6_COUNTER (TIM5)->CNT
230#define FUEL7_COUNTER (TIM5)->CNT
231#define FUEL8_COUNTER (TIM5)->CNT
232
233#define FUEL5_COMPARE (TIM5)->CCR1
234#define FUEL6_COMPARE (TIM5)->CCR2
235#define FUEL7_COMPARE (TIM5)->CCR3
236#define FUEL8_COMPARE (TIM5)->CCR4
237
238#define IGN5_COUNTER (TIM4)->CNT
239#define IGN6_COUNTER (TIM4)->CNT
240#define IGN7_COUNTER (TIM4)->CNT
241#define IGN8_COUNTER (TIM4)->CNT
242
243#define IGN5_COMPARE (TIM4)->CCR1
244#define IGN6_COMPARE (TIM4)->CCR2
245#define IGN7_COMPARE (TIM4)->CCR3
246#define IGN8_COMPARE (TIM4)->CCR4
247
248
249static inline void FUEL1_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC1; (TIM3)->DIER |= TIM_DIER_CC1IE;}
250static inline void FUEL2_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC2; (TIM3)->DIER |= TIM_DIER_CC2IE;}
251static inline void FUEL3_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC3; (TIM3)->DIER |= TIM_DIER_CC3IE;}
252static inline void FUEL4_TIMER_ENABLE(void) {(TIM3)->CR1 |= TIM_CR1_CEN; (TIM3)->SR = ~TIM_FLAG_CC4; (TIM3)->DIER |= TIM_DIER_CC4IE;}
253
254static inline void FUEL1_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC1IE;}
255static inline void FUEL2_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC2IE;}
256static inline void FUEL3_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC3IE;}
257static inline void FUEL4_TIMER_DISABLE(void) {(TIM3)->DIER &= ~TIM_DIER_CC4IE;}
258
259static inline void IGN1_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC1; (TIM2)->DIER |= TIM_DIER_CC1IE;}
260static inline void IGN2_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC2; (TIM2)->DIER |= TIM_DIER_CC2IE;}
261static inline void IGN3_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC3; (TIM2)->DIER |= TIM_DIER_CC3IE;}
262static inline void IGN4_TIMER_ENABLE(void) {(TIM2)->CR1 |= TIM_CR1_CEN; (TIM2)->SR = ~TIM_FLAG_CC4; (TIM2)->DIER |= TIM_DIER_CC4IE;}
263
264static inline void IGN1_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC1IE;}
265static inline void IGN2_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC2IE;}
266static inline void IGN3_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC3IE;}
267static inline void IGN4_TIMER_DISABLE(void) {(TIM2)->DIER &= ~TIM_DIER_CC4IE;}
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
279static inline void IGN5_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC1; (TIM4)->DIER |= TIM_DIER_CC1IE;}
280static inline void IGN6_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC2; (TIM4)->DIER |= TIM_DIER_CC2IE;}
281static inline void IGN7_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC3; (TIM4)->DIER |= TIM_DIER_CC3IE;}
282static inline void IGN8_TIMER_ENABLE(void) {(TIM4)->CR1 |= TIM_CR1_CEN; (TIM4)->SR = ~TIM_FLAG_CC4; (TIM4)->DIER |= TIM_DIER_CC4IE;}
283
284static inline void IGN5_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC1IE;}
285static inline void IGN6_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC2IE;}
286static inline void IGN7_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC3IE;}
287static inline void IGN8_TIMER_DISABLE(void) {(TIM4)->DIER &= ~TIM_DIER_CC4IE;}
288
289
290/*
291***********************************************************************************************************
292* Auxiliaries
293*/
294#define ENABLE_BOOST_TIMER() (TIM1)->SR = ~TIM_FLAG_CC2; (TIM1)->DIER |= TIM_DIER_CC2IE; (TIM1)->CR1 |= TIM_CR1_CEN;
295#define DISABLE_BOOST_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC2IE
296
297#define ENABLE_VVT_TIMER() (TIM1)->SR = ~TIM_FLAG_CC3; (TIM1)->DIER |= TIM_DIER_CC3IE; (TIM1)->CR1 |= TIM_CR1_CEN;
298#define DISABLE_VVT_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC3IE
299
300#define ENABLE_FAN_TIMER() (TIM1)->SR = ~TIM_FLAG_CC1; (TIM1)->DIER |= TIM_DIER_CC1IE; (TIM1)->CR1 |= TIM_CR1_CEN;
301#define DISABLE_FAN_TIMER() (TIM1)->DIER &= ~TIM_DIER_CC1IE
302
303#define BOOST_TIMER_COMPARE (TIM1)->CCR2
304#define BOOST_TIMER_COUNTER (TIM1)->CNT
305#define VVT_TIMER_COMPARE (TIM1)->CCR3
306#define VVT_TIMER_COUNTER (TIM1)->CNT
307#define FAN_TIMER_COMPARE (TIM1)->CCR1
308#define FAN_TIMER_COUNTER (TIM1)->CNT
309
310/*
311***********************************************************************************************************
312* Idle
313*/
314#define IDLE_COUNTER (TIM1)->CNT
315#define IDLE_COMPARE (TIM1)->CCR4
316
317#define IDLE_TIMER_ENABLE() (TIM1)->SR = ~TIM_FLAG_CC4; (TIM1)->DIER |= TIM_DIER_CC4IE; (TIM1)->CR1 |= TIM_CR1_CEN;
318#define IDLE_TIMER_DISABLE() (TIM1)->DIER &= ~TIM_DIER_CC4IE
319
320/*
321***********************************************************************************************************
322* Timers
323*/
324
325extern HardwareTimer Timer1;
326extern HardwareTimer Timer2;
327extern HardwareTimer Timer3;
328extern HardwareTimer Timer4;
329#if !defined(ARDUINO_BLUEPILL_F103C8) && !defined(ARDUINO_BLUEPILL_F103CB) //F103 just have 4 timers
330extern HardwareTimer Timer5;
331#if defined(TIM11)
333#elif defined(TIM7)
335#endif
336#endif
337
338#if ((STM32_CORE_VERSION_MINOR<=8) & (STM32_CORE_VERSION_MAJOR==1))
345#if (INJ_CHANNELS >= 5)
347#endif
348#if (INJ_CHANNELS >= 6)
350#endif
351#if (INJ_CHANNELS >= 7)
353#endif
354#if (INJ_CHANNELS >= 8)
356#endif
364#if (IGN_CHANNELS >= 5)
366#endif
367#if (IGN_CHANNELS >= 6)
369#endif
370#if (IGN_CHANNELS >= 7)
372#endif
373#if (IGN_CHANNELS >= 8)
375#endif
376#endif //End core<=1.8
377
378/*
379***********************************************************************************************************
380* CAN / Second serial
381*/
382#if defined(HAL_CAN_MODULE_ENABLED)
383#define NATIVE_CAN_AVAILABLE
384#include <src/STM32_CAN/STM32_CAN.h>
385//This activates CAN1 interface on STM32, but it's named as Can0, because that's how Teensy implementation is done
386extern STM32_CAN Can0;
387#endif
388
389#define secondarySerial_AVAILABLE
390#if defined(STM32GENERIC) // STM32GENERIC core
391 #define SECONDARY_SERIAL_T SerialUART
392#else //libmaple core aka STM32DUINO
393 #define SECONDARY_SERIAL_T HardwareSerial
394#endif
395
396#endif //CORE_STM32
397#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