forked from mmoskal/uf2-stm32f103
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdapboot.c
122 lines (92 loc) · 3.05 KB
/
dapboot.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2016, Devan Lai
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice
* appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <libopencm3/cm3/vector.h>
#include "dapboot.h"
#include "target.h"
#include "usb_conf.h"
#include "config.h"
#include "ghostfat.h"
#include <libopencm3/usb/msc.h>
static inline void __set_MSP(uint32_t topOfMainStack) {
asm("msr msp, %0" : : "r" (topOfMainStack));
}
bool validate_application(void) {
if ((*(volatile uint32_t *)APP_BASE_ADDRESS & 0x2FFE0000) == 0x20000000) {
return true;
}
return false;
}
static void jump_to_application(void) __attribute__ ((noreturn));
static void jump_to_application(void) {
vector_table_t* app_vector_table = (vector_table_t*)APP_BASE_ADDRESS;
/* Use the application's vector table */
target_relocate_vector_table();
/* Do any necessary early setup for the application */
//target_pre_main();
/* Initialize the application's stack pointer */
__set_MSP((uint32_t)(app_vector_table->initial_sp_value));
/* Jump to the application entry point */
app_vector_table->reset();
while (1);
}
uint32_t msTimer;
extern int msc_started;
int main(void) {
bool appValid = validate_application();
if (appValid && target_get_force_app()) {
jump_to_application();
return 0;
}
/* Setup clocks */
target_clock_setup();
#ifdef DEVICE_DMESG
trace_setup();
#endif
/* Initialize GPIO/LEDs if needed */
target_gpio_setup();
if (target_get_force_bootloader() || !appValid) {
usbd_device* usbd_dev = usb_setup();
usb_msc_init(usbd_dev, 0x82, 64, 0x01, 64, "A", "MSC",
"1", BIN_NUM_BLOCKS, read_block, write_block);
int cycleCount = 0;
int br = 500;
int d = 1;
while (1) {
cycleCount++;
target_set_led(cycleCount < br);
if (cycleCount >= 700) {
msTimer++;
cycleCount = 0;
br += d;
if (br > 700)
d = -2;
else if (br < 10)
d = 2;
ghostfat_1ms();
if (appValid && !msc_started && msTimer > 3000) {
target_manifest_app();
}
}
usbd_poll(usbd_dev);
}
} else {
jump_to_application();
}
return 0;
}