]> mj.ucw.cz Git - home-hw.git/blob - protab/timer2-irq/timer.c
Merge branch 'master' of ssh://git.ucw.cz/home/mj/GIT/home-hw
[home-hw.git] / protab / timer2-irq / timer.c
1 #include <libopencm3/cm3/nvic.h>
2 #include <libopencm3/stm32/rcc.h>
3 #include <libopencm3/stm32/gpio.h>
4 #include <libopencm3/stm32/timer.h>
5
6 void tim3_isr(void)
7 {
8         if (TIM_SR(TIM3) & TIM_SR_UIF) {
9                 TIM_SR(TIM3) &= ~TIM_SR_UIF;
10                 gpio_toggle(GPIOC, GPIO13);
11         }
12 }
13
14 int main(void)
15 {
16         rcc_clock_setup_in_hse_8mhz_out_72mhz();
17         rcc_periph_clock_enable(RCC_GPIOC);
18         rcc_periph_clock_enable(RCC_TIM3);
19
20         // PC13 = BluePill LED
21         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
22
23         timer_set_prescaler(TIM3, 35999);       // 72 MHz / 36000 = 2 kHz
24         timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
25         timer_set_period(TIM3, 199);            // 200 ticks = 100 ms
26         timer_update_on_overflow(TIM3);
27         timer_enable_irq(TIM3, TIM_DIER_UIE);
28         nvic_enable_irq(NVIC_TIM3_IRQ);
29         timer_enable_counter(TIM3);
30
31         for (;;) {
32                 asm volatile ("wfi");
33         }
34
35         return 0;
36 }