]> mj.ucw.cz Git - home-hw.git/commitdiff
Timer 1 & 2
authorMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 21:23:47 +0000 (23:23 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 21:23:47 +0000 (23:23 +0200)
protab/timer1-clock/Makefile [new file with mode: 0644]
protab/timer1-clock/timer.c [new file with mode: 0644]
protab/timer2-irq/Makefile [new file with mode: 0644]
protab/timer2-irq/timer.c [new file with mode: 0644]

diff --git a/protab/timer1-clock/Makefile b/protab/timer1-clock/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/timer1-clock/timer.c b/protab/timer1-clock/timer.c
new file mode 100644 (file)
index 0000000..1042e46
--- /dev/null
@@ -0,0 +1,34 @@
+#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;
+}
diff --git a/protab/timer2-irq/Makefile b/protab/timer2-irq/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/timer2-irq/timer.c b/protab/timer2-irq/timer.c
new file mode 100644 (file)
index 0000000..86ad521
--- /dev/null
@@ -0,0 +1,36 @@
+#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;
+}