-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbl.c
More file actions
82 lines (63 loc) · 1.43 KB
/
Copy pathbl.c
File metadata and controls
82 lines (63 loc) · 1.43 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
#include <string.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/flash.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/cortex.h>
#include <libopencm3/stm32/iwdg.h>
#include "timer.h"
#include "led.h"
#include "clock.h"
#include "tick.h"
#include "adc.h"
#include "usb.h"
#include "usb_dfu.h"
#include "bl.h"
void start_app(void)
{
/* Boot the application if it's valid. */
if ((*(volatile uint32_t *)ADDR_APP & 0x2FFE0000) == 0x20000000) {
cm_disable_interrupts();
/* Set vector table base address. */
SCB_VTOR = ADDR_APP & 0xFFFF;
/* Initialise master stack pointer. */
asm volatile("msr msp, %0"::"g" (*(volatile uint32_t *)ADDR_APP));
/* Jump to application. */
(*(void (**)())(ADDR_APP + 4))();
}
}
int main(void)
{
cm_disable_interrupts();
clock_setup();
timer_setup();
led_setup();
systick_setup();
adc_setup();
uint16_t adc = adc_read();
if (adc == 0xfff)
adc = 0xffe;
uint32_t voltage = (adc * 3300) / 0xfff;
voltage = voltage * (10000 + 2200) / 2200;
if (voltage > 9000)
start_app();
usb_setup(&dfu_cb);
cm_enable_interrupts();
iwdg_set_period_ms(1000);
iwdg_start();
while(1)
{
iwdg_reset();
if (dfu_wants_start_app())
start_app();
if (tick.flag_tick) {
tick.flag_tick = 0;
if (tick.flag_1000ms) {
tick.flag_1000ms = 0;
led4_blink();
if (tick.sec == 5 && !dfu_in_progress())
start_app();
}
}
}
}