// vim: shiftwidth=4 tabstop=4 noexpandtab #include #include #include #include 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 }