Browse Source

Add blink_btn.

flabbergast 1 year ago
parent
commit
bd937bc2a0
5 changed files with 209 additions and 0 deletions
  1. 4
    0
      README.md
  2. 26
    0
      blink_btn/Makefile.f0
  3. 26
    0
      blink_btn/Makefile.l0
  4. 27
    0
      blink_btn/Makefile.l0btld
  5. 126
    0
      blink_btn/button.c

+ 4
- 0
README.md View File

@@ -6,6 +6,10 @@ They aren't too specific for the bat boards, except the usual pinout considerati
6 6
 
7 7
 ## Short summary for the examples
8 8
 
9
+### blink_btn
10
+
11
+The onboard LED blinks, holding the button slows the frequency somewhat. The clocks are set up to 48MHz on F072, 32MHz on L052 (max).
12
+
9 13
 ### usb_cdcacm
10 14
 
11 15
 The board presents itself as a serial port (CDC). It echoes back what it receives; toggling the LED after each received packet. Pressing the `PA1` button will print `hello, world` over this port.

+ 26
- 0
blink_btn/Makefile.f0 View File

@@ -0,0 +1,26 @@
1
+##
2
+## This file is part of the libopencm3 project.
3
+##
4
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
5
+##
6
+## This library is free software: you can redistribute it and/or modify
7
+## it under the terms of the GNU Lesser General Public License as published by
8
+## the Free Software Foundation, either version 3 of the License, or
9
+## (at your option) any later version.
10
+##
11
+## This library is distributed in the hope that it will be useful,
12
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+## GNU Lesser General Public License for more details.
15
+##
16
+## You should have received a copy of the GNU Lesser General Public License
17
+## along with this library.  If not, see <http://www.gnu.org/licenses/>.
18
+##
19
+
20
+BINARY = button
21
+
22
+OPENCM3_DIR=../libopencm3
23
+LDSCRIPT = $(OPENCM3_DIR)/lib/stm32/f0/stm32f07xzb.ld
24
+
25
+include ../common.f0.mk
26
+

+ 26
- 0
blink_btn/Makefile.l0 View File

@@ -0,0 +1,26 @@
1
+##
2
+## This file is part of the libopencm3 project.
3
+##
4
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
5
+##
6
+## This library is free software: you can redistribute it and/or modify
7
+## it under the terms of the GNU Lesser General Public License as published by
8
+## the Free Software Foundation, either version 3 of the License, or
9
+## (at your option) any later version.
10
+##
11
+## This library is distributed in the hope that it will be useful,
12
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+## GNU Lesser General Public License for more details.
15
+##
16
+## You should have received a copy of the GNU Lesser General Public License
17
+## along with this library.  If not, see <http://www.gnu.org/licenses/>.
18
+##
19
+
20
+BINARY = button
21
+
22
+OPENCM3_DIR=../libopencm3
23
+LDSCRIPT = $(OPENCM3_DIR)/lib/stm32/l0/stm32l0xx8.ld
24
+
25
+include ../common.l0.mk
26
+

+ 27
- 0
blink_btn/Makefile.l0btld View File

@@ -0,0 +1,27 @@
1
+##
2
+## This file is part of the libopencm3 project.
3
+##
4
+## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
5
+##
6
+## This library is free software: you can redistribute it and/or modify
7
+## it under the terms of the GNU Lesser General Public License as published by
8
+## the Free Software Foundation, either version 3 of the License, or
9
+## (at your option) any later version.
10
+##
11
+## This library is distributed in the hope that it will be useful,
12
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+## GNU Lesser General Public License for more details.
15
+##
16
+## You should have received a copy of the GNU Lesser General Public License
17
+## along with this library.  If not, see <http://www.gnu.org/licenses/>.
18
+##
19
+
20
+BINARY = button
21
+
22
+OPENCM3_DIR=../libopencm3
23
+LDSCRIPT = ../l052-bldr.ld
24
+DEFS += -DBOOTLOADER8K
25
+
26
+include ../common.l0.mk
27
+

+ 126
- 0
blink_btn/button.c View File

@@ -0,0 +1,126 @@
1
+/*
2
+ * This file is part of the libopencm3 project.
3
+ *
4
+ * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>,
5
+ * Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
6
+ * Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
7
+ *
8
+ * This library is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU Lesser General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This library is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public License
19
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+
22
+#include <libopencm3/stm32/rcc.h>
23
+#include <libopencm3/stm32/flash.h>
24
+#include <libopencm3/stm32/gpio.h>
25
+
26
+#define LED_GPIO GPIOA
27
+#define LED_PIN GPIO15
28
+#define LED_RCC_GPIO RCC_GPIOA
29
+#define BUTTON_GPIO GPIOA
30
+#define BUTTON_PIN GPIO1
31
+#define BUTTON_RCC_GPIO RCC_GPIOA
32
+
33
+/* Set the clock to max speed. */
34
+static void clock_setup(void)
35
+{
36
+#if defined(STM32F0)
37
+	rcc_clock_setup_in_hsi_out_48mhz();
38
+#elif defined(STM32L0)
39
+	/* After a reset, the system uses MSI@2.1MHz. */
40
+	/* end result: 32MHz PLLVCO from HSI16,
41
+	 *	 no system/periph clock divide.
42
+	 */
43
+
44
+	/* increase the latency to 1 wait state (we'll be speeding up) */
45
+	flash_set_ws(1);
46
+
47
+	/* turn on HSI16 */
48
+	rcc_osc_on(RCC_HSI16);
49
+	rcc_wait_for_osc_ready(RCC_HSI16);
50
+
51
+	/* run AHB, APB1, APB2 at full speed */
52
+	rcc_set_hpre(RCC_CFGR_HPRE_NODIV);
53
+	rcc_set_ppre1(RCC_CFGR_PPRE1_NODIV);
54
+	rcc_set_ppre2(RCC_CFGR_PPRE2_NODIV);
55
+
56
+	/* turn off PLL and wait for it to fully stop */
57
+	rcc_osc_off(RCC_PLL);
58
+	while (RCC_CR & RCC_CR_PLLRDY);
59
+
60
+	/* set PLL source to HSI16 */
61
+	RCC_CFGR &= ~(1<<16); // RCC_CFGR_PLLSRC
62
+
63
+	/* set up PLL */
64
+	rcc_set_pll_multiplier(RCC_CFGR_PLLMUL_MUL4);
65
+	rcc_set_pll_divider(RCC_CFGR_PLLDIV_DIV2);
66
+
67
+	/* turn on and switch to PLL */
68
+	rcc_osc_on(RCC_PLL);
69
+	rcc_wait_for_osc_ready(RCC_PLL);
70
+	rcc_set_sysclk_source(RCC_PLL);
71
+
72
+	rcc_ahb_frequency = 32000000;
73
+	rcc_apb1_frequency = 32000000;
74
+	rcc_apb2_frequency = 32000000;
75
+#else
76
+	#error "Implement a clock setup."
77
+#endif
78
+}
79
+
80
+static void gpio_setup(void)
81
+{
82
+	/* Enable clocks. */
83
+	rcc_periph_clock_enable(LED_RCC_GPIO);
84
+	rcc_periph_clock_enable(BUTTON_RCC_GPIO);
85
+
86
+	/* Set the LED pin to 'output push-pull'. */
87
+	gpio_mode_setup(LED_GPIO, GPIO_MODE_OUTPUT,
88
+			GPIO_PUPD_NONE, LED_PIN);
89
+
90
+	/* Set the button pin to 'input' (external pull-down). */
91
+	gpio_mode_setup(BUTTON_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BUTTON_PIN);
92
+}
93
+
94
+#define SCB_VTOR MMIO32(0xE000ED08)
95
+
96
+int main(void)
97
+{
98
+#if defined(BOOTLOADER8K)
99
+	SCB_VTOR = (uint32_t) 0x08002000;
100
+#endif
101
+
102
+	int i;
103
+
104
+	clock_setup();
105
+	gpio_setup();
106
+
107
+	/* Blink the LED on the board. */
108
+	while (1) {
109
+		gpio_toggle(LED_GPIO, LED_PIN);
110
+
111
+		/* Upon button press, blink more slowly. */
112
+		if (gpio_get(BUTTON_GPIO, BUTTON_PIN)) {
113
+			for (i = 0; i < 2000000; i++) {	/* Wait a bit. */
114
+				__asm__("nop");
115
+			}
116
+		}
117
+
118
+		for (i = 0; i < 2000000; i++) {		/* Wait a bit. */
119
+			__asm__("nop");
120
+		}
121
+	}
122
+
123
+	return 0;
124
+}
125
+
126
+// vim: tabstop=4:shiftwidth=4:noexpandtab

Loading…
Cancel
Save