]> mj.ucw.cz Git - home-hw.git/blob - protab/timer4-pwm/timer.c
Protab: Use USB boot loader in all examples
[home-hw.git] / protab / timer4-pwm / timer.c
1 #include <libopencm3/stm32/rcc.h>
2 #include <libopencm3/stm32/gpio.h>
3 #include <libopencm3/stm32/timer.h>
4
5 int main(void)
6 {
7         rcc_clock_setup_in_hse_8mhz_out_72mhz();
8         rcc_periph_clock_enable(RCC_GPIOB);
9         rcc_periph_clock_enable(RCC_GPIOC);
10         rcc_periph_clock_enable(RCC_TIM3);
11         rcc_periph_reset_pulse(RST_TIM3);       // XXX
12
13         // PC13 = BluePill LED
14         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
15         gpio_set(GPIOC, GPIO13);
16
17         // PB0 = TIM3_CH3
18         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO0);
19
20         timer_set_prescaler(TIM3, 1);           // 72 MHz / 2 = 36 MHz
21         timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
22         timer_set_period(TIM3, 999);            // 36 MHz / 1000 = 36 kHz
23         timer_update_on_overflow(TIM3);
24
25         timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM1);
26         timer_set_oc_value(TIM3, TIM_OC3, 150);
27         timer_set_oc_polarity_high(TIM3, TIM_OC3);
28         timer_enable_oc_output(TIM3, TIM_OC3);
29
30         timer_enable_counter(TIM3);
31
32         for (;;) {
33                 asm volatile ("wfi");
34         }
35
36         return 0;
37 }