]> mj.ucw.cz Git - home-hw.git/commitdiff
Protab: Timer OC
authorMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 21:43:34 +0000 (23:43 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 21:43:34 +0000 (23:43 +0200)
protab/timer3-oc/Makefile [new file with mode: 0644]
protab/timer3-oc/timer.c [new file with mode: 0644]

diff --git a/protab/timer3-oc/Makefile b/protab/timer3-oc/Makefile
new file mode 100644 (file)
index 0000000..9f34630
--- /dev/null
@@ -0,0 +1,6 @@
+ROOT=../..
+BINARY=timer
+OBJS=timer.o
+LIB_OBJS=
+
+include $(ROOT)/mk/bluepill.mk
diff --git a/protab/timer3-oc/timer.c b/protab/timer3-oc/timer.c
new file mode 100644 (file)
index 0000000..c7019e5
--- /dev/null
@@ -0,0 +1,37 @@
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/timer.h>
+
+int main(void)
+{
+       rcc_clock_setup_in_hse_8mhz_out_72mhz();
+       rcc_periph_clock_enable(RCC_GPIOB);
+       rcc_periph_clock_enable(RCC_GPIOC);
+       rcc_periph_clock_enable(RCC_TIM3);
+       rcc_periph_reset_pulse(RST_TIM3);       // XXX
+
+       // PC13 = BluePill LED
+       gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
+       gpio_set(GPIOC, GPIO13);
+
+       // PB0 = TIM3_CH3
+       gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO0);
+
+       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, 1999);
+       timer_update_on_overflow(TIM3);
+
+       timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM1);
+       timer_set_oc_value(TIM3, TIM_OC3, 500);
+       timer_set_oc_polarity_high(TIM3, TIM_OC3);
+       timer_enable_oc_output(TIM3, TIM_OC3);
+
+       timer_enable_counter(TIM3);
+
+       for (;;) {
+               asm volatile ("wfi");
+       }
+
+       return 0;
+}