]> mj.ucw.cz Git - home-hw.git/commitdiff
Protab: Button
authorMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 20:51:43 +0000 (22:51 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 30 Jul 2019 20:51:43 +0000 (22:51 +0200)
protab/button/Makefile [new file with mode: 0644]
protab/button/button.c [new file with mode: 0644]

diff --git a/protab/button/Makefile b/protab/button/Makefile
new file mode 100644 (file)
index 0000000..7e92deb
--- /dev/null
@@ -0,0 +1,6 @@
+ROOT=../..
+BINARY=button
+OBJS=button.o
+LIB_OBJS=
+
+include $(ROOT)/mk/bluepill.mk
diff --git a/protab/button/button.c b/protab/button/button.c
new file mode 100644 (file)
index 0000000..79cc5da
--- /dev/null
@@ -0,0 +1,30 @@
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+
+int main(void)
+{
+       rcc_clock_setup_in_hse_8mhz_out_72mhz();
+       rcc_periph_clock_enable(RCC_GPIOA);
+       rcc_periph_clock_enable(RCC_GPIOC);
+
+       // PC13 = BluePill LED
+       gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
+
+       // PA0 ... PA2 = LEDs
+       gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
+       gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
+       gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
+
+       // PA3 = button (pull-up)
+       gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO3);
+       gpio_set(GPIOA, GPIO3);
+
+       for (;;) {
+               if (gpio_get(GPIOA, GPIO3))
+                       gpio_set(GPIOA, GPIO0 | GPIO1 | GPIO2);
+               else
+                       gpio_clear(GPIOA, GPIO0 | GPIO1 | GPIO2);
+       }
+
+       return 0;
+}