rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
bootloader_main.cpp
Go to the documentation of this file.
1
2#include "pch.h"
3#include "usbconsole.h"
4#include "hardware.h"
5
6#ifdef HW_HELLEN
7#include "hellen_all_meta.h"
8#endif // HW_HELLEN
9
10extern "C" {
11 #include "boot.h"
12 #include "flash.h"
13 #include "shared_params.h"
14}
15
16// used externaly by openblt_usb.cpp
18
22
23static const uint8_t maxWdRebootCounter = 10;
24
25#if (BOOT_COP_HOOKS_ENABLE > 0)
26extern "C" {
27 void CopInitHook(void);
28 void CopServiceHook(void);
29}
30
31// Functions for controlling the watchdog
32void CopInitHook(void) {
33 // Nothing to do...
34}
35
36void CopServiceHook(void) {
37 // We need to reset WDT here
38#if HAL_USE_WDG
39 wdgResetI(&WDGD1);
40#endif // HAL_USE_WDG
41}
42#endif // BOOT_COP_HOOKS_ENABLE
43
44PUBLIC_API_WEAK bool OpenBltIsBoardOk()
45{
46 return true;
47}
48
49class BlinkyThread : public chibios_rt::BaseStaticThread<256> {
50protected:
51 void main(void) override {
52 Gpio yellow = getWarningLedPin();
53 Gpio blue = getCommsLedPin();
54 Gpio green = getRunningLedPin();
55 Gpio red = LED_CRITICAL_ERROR_BRAIN_PIN;
56
57 efiSetPadMode("yellow", yellow, PAL_MODE_OUTPUT_PUSHPULL);
58 efiSetPadMode("blue", blue, PAL_MODE_OUTPUT_PUSHPULL);
59 efiSetPadMode("green", green, PAL_MODE_OUTPUT_PUSHPULL);
60 efiSetPadMode("red", red, PAL_MODE_OUTPUT_PUSHPULL);
61
62 auto yellowPort = getBrainPinPort(yellow);
63 auto yellowPin = getBrainPinIndex(yellow);
64 auto bluePort = getBrainPinPort(blue);
65 auto bluePin = getBrainPinIndex(blue);
66 auto greenPort = getBrainPinPort(green);
67 auto greenPin = getBrainPinIndex(green);
68 auto redPort = getBrainPinPort(red);
69 auto redPin = getBrainPinIndex(red);
70
71#ifdef BOOTLOADER_ENABLE_OUTPUT_PIN
72 {
73 ioportid_t en_port = getHwPort("blt-en-pin", BOOTLOADER_ENABLE_OUTPUT_PIN);
74 uint8_t en_pin = getHwPin("blt-en-pin", BOOTLOADER_ENABLE_OUTPUT_PIN);
75 palSetPadMode(en_port, en_pin, PAL_MODE_OUTPUT_PUSHPULL);
76 palWritePad(en_port, en_pin, 1);
77 }
78#endif // BOOTLOADER_ENABLE_OUTPUT_PIN
79
80#ifdef BOOTLOADER_ENABLE_OUTPUT_PIN2
81 {
82 ioportid_t en_port = getHwPort("blt-en-pin2", BOOTLOADER_ENABLE_OUTPUT_PIN2);
83 uint8_t en_pin = getHwPin("blt-en-pin2", BOOTLOADER_ENABLE_OUTPUT_PIN2);
84 palSetPadMode(en_port, en_pin, PAL_MODE_OUTPUT_PUSHPULL);
85 palWritePad(en_port, en_pin, 1);
86 }
87#endif // BOOTLOADER_ENABLE_OUTPUT_PIN2
88
89#ifdef BOOTLOADER_DISABLE_GREEN_LED
90 greenPort = NULL;
91#endif // BOOTLOADER_DISABLE_GREEN_LED
92
93 if (yellowPort) {
94 palSetPad(yellowPort, yellowPin);
95 }
96 if (bluePort) {
97 palSetPad(bluePort, bluePin);
98 }
99 if (greenPort) {
100 palSetPad(greenPort, greenPin);
101 }
102 if (redPort) {
103 if (wdReset) {
104 palClearPad(redPort, redPin);
105 } else {
106 palSetPad(redPort, redPin);
107 }
108 }
109
110 while (true) {
111 if (yellowPort) {
112 palTogglePad(yellowPort, yellowPin);
113 }
114 if (bluePort && OpenBltIsBoardOk()) {
115 palTogglePad(bluePort, bluePin);
116 }
117 if (greenPort) {
118 palTogglePad(greenPort, greenPin);
119 }
120 if (redPort && !wdReset) {
121 palTogglePad(redPort, redPin);
122 }
123 // blink 3 times faster if Dual Bank is not enabled
124 auto delay = isFlashDualBank() ? 125 : 40;
125 // blink faster if not in the waiting mode
126 chThdSleepMilliseconds(waitedLongerThanTimeout ? (delay * 2) : delay);
127 }
128 }
129};
130
131static BlinkyThread blinky;
132
134 uint8_t value = 0x00;
135 if (SharedParamsReadByIndex(0, &value) && (value == 0x01)) {
136 /* clear */
138 return BLT_TRUE;
139 }
140 return BLT_FALSE;
141}
142
144 uint8_t wd_counter = 0;
148 // One of watchdogs
149 SharedParamsReadByIndex(1, &wd_counter);
150 wd_counter++;
151 SharedParamsWriteByIndex(1, wd_counter);
152 wdReset = BLT_TRUE;
153 } else if ((resetCause == Reset_Cause_NRST_Pin) ||
155 // power on or NRST reset
156 // cleat WD counter
157 SharedParamsWriteByIndex(1, wd_counter);
158 }
159
160 return (wd_counter > maxWdRebootCounter);
161}
162
163int main(void) {
164 halInit();
165 chSysInit();
166
167 baseMCUInit();
168
169 // Init openblt shared params
173
174 // start the blinky thread
175 blinky.start(NORMALPRIO + 10);
176
177 // Init openblt itself
178 BootInit();
179
180 blt_bool wasConnected = BLT_FALSE;
181
182 while (true) {
183 BootTask();
184
185 // since BOOT_BACKDOOR_HOOKS_ENABLE==TRUE, BackDoorCheck() is not working
186 // so we have to manually check if we need to jump to the main firmware
187 if (ComIsConnected() == BLT_TRUE) {
188 // remember we had connection attempt
189 wasConnected = BLT_TRUE;
190 continue;
191 }
192 if (stayInBootloader || wasConnected)
193 continue;
194#if (BOOT_BACKDOOR_ENTRY_TIMEOUT_MS > 0)
195 blt_bool isTimeout = (TIME_I2MS(chVTGetSystemTime()) >= BOOT_BACKDOOR_ENTRY_TIMEOUT_MS);
196#else
197 blt_bool isTimeout = BLT_TRUE;
198#endif // BOOT_BACKDOOR_ENTRY_TIMEOUT_MS
199 if (isTimeout == BLT_TRUE) {
200 waitedLongerThanTimeout = BLT_TRUE;
202 }
203 }
204}
205
206// very basic version, supports on chip pins only (really only used for USB)
207void efiSetPadMode(const char* msg, brain_pin_e brainPin, iomode_t mode) {
208 ioportid_t port = getHwPort(msg, brainPin);
209 ioportmask_t pin = getHwPin(msg, brainPin);
210 /* paranoid */
211 if (!port) {
212 return;
213 }
214
215 palSetPadMode(port, pin, mode);
216}
Reset_Cause_t getMCUResetCause()
void baseMCUInit()
Gpio getWarningLedPin()
Gpio getCommsLedPin()
Gpio getRunningLedPin()
static blt_bool checkIfRebootIntoOpenBltRequested(void)
static blt_bool checkIfResetLoop(void)
void CopInitHook(void)
blt_bool stayInBootloader
static const uint8_t maxWdRebootCounter
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
PUBLIC_API_WEAK bool OpenBltIsBoardOk()
static blt_bool rebootLoop
static blt_bool wdReset
int main(void)
static blt_bool waitedLongerThanTimeout
void CopServiceHook(void)
static BlinkyThread blinky
void delay(void)
Definition board.c:12
ioportid_t getHwPort(const char *msg, brain_pin_e brainPin)
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
blt_bool isFlashDualBank(void)
uint32_t ioportmask_t
Digital I/O port sized unsigned type.
Definition hal_pal_lld.h:78
GPIO_TypeDef * ioportid_t
Port Identifier.
uint32_t iomode_t
Digital I/O modes.
Definition hal_pal_lld.h:83
Reset_Cause_t
Definition mpu_util.h:104
@ Reset_Cause_WWatchdog
Definition mpu_util.h:107
@ Reset_Cause_NRST_Pin
Definition mpu_util.h:109
@ Reset_Cause_POR
Definition mpu_util.h:110
@ Reset_Cause_IWatchdog
Definition mpu_util.h:106
void CpuStartUserProgram(void)
int getBrainPinIndex(Gpio brainPin)
ioportid_t getBrainPinPort(brain_pin_e brainPin)
bool SharedParamsWriteByIndex(uint32_t idx, uint8_t value)
Writes a data byte to the shared parameter buffer at the specified index.
void SharedParamsInit(void)
Initializes the shared RAM parameters module.
bool SharedParamsReadByIndex(uint32_t idx, uint8_t *value)
Reads a data byte from the shared parameter buffer at the specified index.
brain_pin_e pin
Definition stm32_adc.cpp:15
static const volatile Reset_Cause_t resetCause
unsigned char blt_bool
Definition types.h:46