-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
82 lines (68 loc) · 2.28 KB
/
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
#include <errno.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/systick.h>
extern "C" {
void sys_tick_handler(void);
}
static void clock_setup() {
// First, let's ensure that our clock is running off the high-speed internal
// oscillator (HSI) at 48MHz.
rcc_clock_setup_in_hsi_out_48mhz();
// Since our LED is on GPIO bank A, we need to enable
// the peripheral clock to this GPIO bank in order to use it.
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
// In order to use our UART, we must enable the clock to it as well.
rcc_periph_clock_enable(RCC_USART1);
}
static void systick_setup() {
// Set the systick clock source to our main clock
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
// Clear the Current Value Register so that we start at 0
STK_CVR = 0;
// In order to trigger an interrupt every millisecond, we can set the reload
// value to be the speed of the processor / 1000 -1
systick_set_reload(rcc_ahb_frequency / 1000 - 1);
// Enable interrupts from the system tick clock
systick_interrupt_enable();
// Enable the system tick counter
systick_counter_enable();
}
// Storage for our monotonic system clock.
// Note that it needs to be volatile since we're modifying it from an interrupt.
static volatile uint64_t _millis = 0;
uint64_t millis() {
return _millis;
}
// This is our interrupt handler for the systick reload interrupt.
// The full list of interrupt services routines that can be implemented is
// listed in libopencm3/include/libopencm3/stm32/f0/nvic.h
void sys_tick_handler(void) {
// Increment our monotonic clock
_millis++;
}
/**
* Delay for a real number of milliseconds
*/
void delay(uint64_t duration) {
const uint64_t until = millis() + duration;
while (millis() < until);
}
static void gpio_setup() {
// Our test LED is connected to Port A pin 11, so let's set it as output
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO11);
}
int main() {
clock_setup();
systick_setup();
gpio_setup();
// Toggle the LED on and off forever
while (1) {
gpio_set(GPIOA, GPIO11);
delay(1000);
gpio_clear(GPIOA, GPIO11);
delay(1000);
}
return 0;
}