summaryrefslogtreecommitdiff
path: root/src/main.c
blob: f8b803bf5e68c6b50aee85d73a8329f17694a44a (plain)
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
// vim: shiftwidth=4 tabstop=4 noexpandtab


#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>


static void clock_init(void);
static void gpio_init(void);
static void nvic_init(void);
static void timer_init(void);

void tim2_isr(void);


void main(void)
{
	clock_init();
	gpio_init();
	nvic_init();
	timer_init();

	while(1);  // wait forever
}


static void clock_init(void)
{
	rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
	rcc_periph_clock_enable(RCC_GPIOC);  // for PC13 blinking
	rcc_periph_clock_enable(RCC_TIM2);   // for timer / counter 2
}


static void gpio_init(void)
{
	gpio_set_mode(GPIOC,
				  GPIO_MODE_OUTPUT_50_MHZ,
				  GPIO_CNF_OUTPUT_PUSHPULL,
				  GPIO13);
}


static void nvic_init(void)
{
	nvic_enable_irq(NVIC_TIM2_IRQ);
	nvic_set_priority(NVIC_TIM2_IRQ, 1);
}


static void timer_init(void)
{
	// set LED on
	gpio_clear(GPIOC, GPIO13);

	// setup timer
	timer_set_counter(TIM2, 1);
	timer_set_prescaler(TIM2, 1440);
	timer_set_period(TIM2, 50000);
	timer_enable_irq(TIM2, TIM_DIER_UIE);  // set action to trigger interrupt
	timer_enable_counter(TIM2);
}


void tim2_isr(void)
{
	gpio_toggle(GPIOC, GPIO13);
	TIM_SR(TIM2) &= ~TIM_SR_UIF;  // clear interrrupt flag
}