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


#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/flash.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);
void tim2_isr(void);
static void nvic_init(void);
static void timer_init(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);
}


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


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


static void timer_init(void)
{
	gpio_set(GPIOC, GPIO13);
	TIM_CNT(TIM2) = 1;               // set start value
	TIM_PSC(TIM2) = 1440;            // set prescaler
	TIM_ARR(TIM2) = 50000;           // interrupt value
	TIM_DIER(TIM2) |= TIM_DIER_UIE;  // update interrupt enable
	TIM_CR1(TIM2) |= TIM_CR1_CEN;    // enable timer
}