Skip to content

Commit 6b84a27

Browse files
Added a button press example for l4
1 parent 4691efb commit 6b84a27

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
##
2+
## This file is part of the libopencm3 project.
3+
##
4+
## Copyright (C) 2009 Uwe Hermann <[email protected]>
5+
## Copyright (C) 2016 Benjamin Levine <[email protected]>
6+
##
7+
## This library is free software: you can redistribute it and/or modify
8+
## it under the terms of the GNU Lesser General Public License as published by
9+
## the Free Software Foundation, either version 3 of the License, or
10+
## (at your option) any later version.
11+
##
12+
## This library is distributed in the hope that it will be useful,
13+
## but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
## GNU Lesser General Public License for more details.
16+
##
17+
## You should have received a copy of the GNU Lesser General Public License
18+
## along with this library. If not, see <http://www.gnu.org/licenses/>.
19+
##
20+
21+
BINARY = button
22+
23+
LDSCRIPT = ../nucleo-l476rg.ld
24+
25+
include ../../Makefile.include
26+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# README
2+
3+
This example blinks the GREEN LED on the ST Nucleo STM32L476RG eval board.
4+
5+
When you press the 'USER' button, the blinking is faster.
6+
7+
## Board connections
8+
9+
*none required*
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of the libopencm3 project.
3+
*
4+
* Copyright (C) 2009 Uwe Hermann <[email protected]>,
5+
* Copyright (C) 2010 Piotr Esden-Tempski <[email protected]>
6+
* Copyright (C) 2011 Stephen Caudle <[email protected]>
7+
* Copyright (C) 2016 Benjamin Levine <[email protected]>
8+
*
9+
* This library is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
#include <libopencm3/stm32/rcc.h>
24+
#include <libopencm3/stm32/gpio.h>
25+
26+
uint16_t exti_line_state;
27+
28+
/* Set STM32 to 48 MHz. */
29+
static void clock_setup(void)
30+
{
31+
rcc_clock_setup_pll(&rcc_clock_config[RCC_CLOCK_VRANGE1_HSI16_PLL_48MHZ]);
32+
}
33+
34+
static void gpio_setup(void)
35+
{
36+
/* Enable GPIOA clock. */
37+
rcc_periph_clock_enable(RCC_GPIOA);
38+
39+
/* Set GPIO5 (in GPIO port A) to 'output push-pull'. */
40+
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5);
41+
}
42+
43+
static void button_setup(void)
44+
{
45+
/* Enable GPIOC clock. */
46+
rcc_periph_clock_enable(RCC_GPIOC);
47+
48+
/* Set GPIO13 (in GPIO port C) to 'input open-drain'. */
49+
gpio_mode_setup(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO13);
50+
}
51+
52+
int main(void)
53+
{
54+
int i;
55+
56+
clock_setup();
57+
button_setup();
58+
gpio_setup();
59+
60+
/* Blink the LED (PA5) on the board. */
61+
while (1) {
62+
gpio_toggle(GPIOA, GPIO5);
63+
64+
/* Upon button press, blink more quickly. */
65+
exti_line_state = GPIOC_IDR;
66+
if ((exti_line_state & (1 << 13)) != 0) {
67+
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
68+
__asm__("nop");
69+
}
70+
}
71+
72+
for (i = 0; i < 500000; i++) { /* Wait a bit. */
73+
__asm__("nop");
74+
}
75+
}
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)