-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootthunder.c
More file actions
104 lines (79 loc) · 2.45 KB
/
bootthunder.c
File metadata and controls
104 lines (79 loc) · 2.45 KB
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
#include <bitthunder.h>
#include <bt_config.h>
#include <lib/putc.h>
#include <fs/bt_fs.h>
void signal_booted() {
BT_GpioSetDirection(9, BT_GPIO_DIR_OUTPUT);
BT_GpioSet(9, BT_TRUE);
}
void uart_config(BT_HANDLE hUART) {
BT_UART_CONFIG oConfig;
oConfig.eMode = BT_UART_MODE_POLLED;
oConfig.ulBaudrate = 115200;
oConfig.ucParity = BT_UART_PARITY_NONE;
oConfig.ucStopBits = BT_UART_ONE_STOP_BIT;
oConfig.ucDataBits = BT_UART_8_DATABITS;
BT_UartSetConfiguration(hUART, &oConfig);
}
int main(void) {
BT_ERROR Error;
BT_HANDLE hUART0 = BT_DeviceOpen(BT_CONFIG_BOOTTHUNDER_SHELL_0_DEVICE, &Error);
uart_config(hUART0);
BT_UartEnable(hUART0);
#ifdef BT_CONFIG_BOOTTHUNDER_SHELL_1_ENABLE
BT_HANDLE hUART1 = BT_DeviceOpen("uart1", &Error);
uart_config(hUART1);
BT_UartEnable(hUART1);
#endif
#if (BT_CONFIG_LIB_PRINTF_SUPPORT_MULTIPLE_STDOUT)
BT_AddStdout(hUART0);
#ifdef BT_CONFIG_BOOTTHUNDER_SHELL_1_ENABLE
BT_AddStdout(hUART1);
#endif
#else
BT_SetStdout(hUART0);
#endif
BT_kPrint("BootThunder started...");
signal_booted();
BT_u32 timeout = 0;
BT_HANDLE hVolume00 = BT_DeviceOpen(BT_CONFIG_BOOTTHUNDER_BOOT_VOLUME, &Error);
while(!hVolume00) {
timeout += 5;
if(timeout >= 5000) {
goto fallback_shell;
}
BT_ThreadSleep(5);
hVolume00 = BT_DeviceOpen(BT_CONFIG_BOOTTHUNDER_BOOT_VOLUME, &Error);
}
BT_Mount(hVolume00, BT_CONFIG_BOOTTHUNDER_BOOT_MOUNTPOINT);
timeout = BT_CONFIG_BOOTTHUNDER_TIMEOUT;
do {
BT_kPrint("Press any key to cancel boot (%d seconds remain)...\r", timeout);
BT_ThreadSleep(1000);
BT_s32 c = BT_GetC(hUART0, BT_FILE_NON_BLOCK, &Error);
if(c >= 0) goto fallback_shell;
#ifdef BT_CONFIG_BOOTTHUNDER_SHELL_1_ENABLE
c = BT_GetC(hUART1, BT_FILE_NON_BLOCK, &Error);
if(c >= 0) goto fallback_shell;
#endif
} while (--timeout > 0);
BT_HANDLE hShell = BT_ShellCreate(BT_stdin, BT_stdout, "BootThunder>", 0, NULL);
Error = BT_ShellScript(hShell, BT_CONFIG_BOOTTHUNDER_BOOT_SCRIPT_PATH);
if(Error) {
BT_kPrint("Could not open shell script, or shell script failed.");
}
fallback_shell:
BT_kPrint("Starting fallback shell!");
BT_HANDLE hShell0 = BT_ShellCreate(hUART0, hUART0, "BootThunder>", BT_SHELL_FLAG_NON_BLOCK, NULL);
#ifdef BT_CONFIG_BOOTTHUNDER_SHELL_1_ENABLE
BT_HANDLE hShell1 = BT_ShellCreate(hUART1, hUART1, "BootThunder>", BT_SHELL_FLAG_NON_BLOCK, NULL);
#endif
while(1) {
BT_Shell(hShell0);
#ifdef BT_CONFIG_BOOTTHUNDER_SHELL_1_ENABLE
BT_Shell(hShell1);
#endif
BT_ThreadSleep(1);
}
return 0;
}