|
| 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