From 3988436770fec3ce2574665d1f88c741f9f86bff Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 30 Jul 2019 23:48:46 +0200 Subject: [PATCH] Protab: PWM --- protab/timer4-pwm/Makefile | 6 ++++++ protab/timer4-pwm/timer.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 protab/timer4-pwm/Makefile create mode 100644 protab/timer4-pwm/timer.c diff --git a/protab/timer4-pwm/Makefile b/protab/timer4-pwm/Makefile new file mode 100644 index 0000000..9f34630 --- /dev/null +++ b/protab/timer4-pwm/Makefile @@ -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 index 0000000..77f5b43 --- /dev/null +++ b/protab/timer4-pwm/timer.c @@ -0,0 +1,37 @@ +#include +#include +#include + +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; +} -- 2.39.2