--- /dev/null
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/timer.h>
+
+static void delay_ms(unsigned int ms)
+{
+ timer_set_period(TIM3, 2*ms);
+ timer_enable_counter(TIM3);
+ while (TIM_CR1(TIM3) & TIM_CR1_CEN)
+ ;
+}
+
+int main(void)
+{
+ rcc_clock_setup_in_hse_8mhz_out_72mhz();
+ rcc_periph_clock_enable(RCC_GPIOC);
+ rcc_periph_clock_enable(RCC_TIM3);
+
+ // PC13 = BluePill LED
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
+
+ timer_set_prescaler(TIM3, 35999); // 72 MHz / 36000 = 2 kHz
+ timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_DOWN);
+ timer_one_shot_mode(TIM3);
+
+ for (;;) {
+ gpio_clear(GPIOC, GPIO13);
+ delay_ms(100);
+ gpio_set(GPIOC, GPIO13);
+ delay_ms(500);
+ }
+
+ return 0;
+}
--- /dev/null
+#include <libopencm3/cm3/nvic.h>
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/timer.h>
+
+void tim3_isr(void)
+{
+ if (TIM_SR(TIM3) & TIM_SR_UIF) {
+ TIM_SR(TIM3) &= ~TIM_SR_UIF;
+ gpio_toggle(GPIOC, GPIO13);
+ }
+}
+
+int main(void)
+{
+ rcc_clock_setup_in_hse_8mhz_out_72mhz();
+ rcc_periph_clock_enable(RCC_GPIOC);
+ rcc_periph_clock_enable(RCC_TIM3);
+
+ // PC13 = BluePill LED
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
+
+ timer_set_prescaler(TIM3, 35999); // 72 MHz / 36000 = 2 kHz
+ timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
+ timer_set_period(TIM3, 199); // 200 ticks = 100 ms
+ timer_update_on_overflow(TIM3);
+ timer_enable_irq(TIM3, TIM_DIER_UIE);
+ nvic_enable_irq(NVIC_TIM3_IRQ);
+ timer_enable_counter(TIM3);
+
+ for (;;) {
+ asm volatile ("wfi");
+ }
+
+ return 0;
+}