-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboot_main.cpp
106 lines (86 loc) · 2.56 KB
/
boot_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <dispatcher.h>
#include <sys_tick.h>
#include <watchdog.h>
#include "bootloader.h"
#include "boot_protocol.h"
using namespace Bootloader;
BootloaderApp bootloader;
BootloaderProtocol bootprotocol{bootloader};
#if defined(_DEBUG) && _DEBUG
Mcucpp::basic_ostream<DebugDevice> cout;
#endif
extern "C" void Reset_Handler();
__attribute__((section(".isr_vectors_orig"), used)) void (*const g_pfnVectors_orig[])(void) =
{
(void (*)(void))((unsigned long)&_estack),
Reset_Handler};
extern void (*const g_pfnVectors[])(void);
void Blink()
{
Led::Toggle();
GetCurrentDispatcher().SetTimer(100, Blink);
}
int main()
{
bootloader.SetVectTable(&g_pfnVectors[0]);
Portb::Enable();
Porta::Enable();
Portc::Enable();
Portd::Enable();
Porta::SetConfiguration(0x1fff, Porta::In);
Portb::SetConfiguration(0xffff, Portb::In);
Portc::SetConfiguration(0xffff, Portc::In);
Portd::SetConfiguration(0xffff, Portd::In);
Porta::SetPullUp(0x1fff, Porta::PullDown);
Portb::SetPullUp(0xffff, Portb::PullDown);
Portc::SetPullUp(0xffff, Portc::PullDown);
Portd::SetPullUp(0xffff, Portd::PullDown);
Led::Port::Enable();
TxPin::Port::Enable();
RxPin::Port::Enable();
DePin::Port::Enable();
#if defined(_DEBUG) && _DEBUG
Clock::HsiClock::Enable();
DebugDeviceClock::Set(UsartClockSrc::Hsi);
DebugDevice::Init(115200);
DebugDevice::SelectTxRxPins<DebugTxPin, DebugRxPin>();
cout << "Bootloader started\r\n";
#endif
Led::SetConfiguration(Led::Port::Out);
Led::Set();
Watchdog::Start(2000);
bootloader.InitBootData();
SysTickTimer::Init(1);
SysTickTimer::EnableInterrupt();
GetCurrentDispatcher().SetTimerFunc(&GetTickCount);
if (!bootprotocol.Init())
{
#if defined(_DEBUG) && _DEBUG
cout << "Init Failed\r\n";
#endif
bootloader.Exit();
}
Blink();
GetCurrentDispatcher().SetTimer(BootStartTimeout, []()
{ bootloader.Exit(); });
do
{
while (!bootloader.IsDone())
{
GetCurrentDispatcher().Poll();
Watchdog::Reset();
}
#if defined(_DEBUG) && _DEBUG
cout << "Try running app\r\n";
#endif
if(!bootloader.RunApplication())
{
#if defined(_DEBUG) && _DEBUG
cout << "No entry point found\r\n";
#endif
}
bootloader.Connected();
} while (true);
bootloader.Reset();
return 0;
}