]> mj.ucw.cz Git - home-hw.git/commitdiff
Protab: PWM
authorMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 21:48:46 +0000 (23:48 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 21:48:46 +0000 (23:48 +0200)
protab/timer4-pwm/Makefile [new file with mode: 0644]
protab/timer4-pwm/timer.c [new file with mode: 0644]

diff --git a/protab/timer4-pwm/Makefile b/protab/timer4-pwm/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/timer4-pwm/timer.c b/protab/timer4-pwm/timer.c
new file mode 100644 (file)
index 0000000..77f5b43
--- /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, 1);           // 72 MHz / 2 = 36 MHz
+       timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
+       timer_set_period(TIM3, 999);            // 36 MHz / 1000 = 36 kHz
+       timer_update_on_overflow(TIM3);
+
+       timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM1);
+       timer_set_oc_value(TIM3, TIM_OC3, 50);
+       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;
+}