|
| 1 | +/* |
| 2 | + * This file is part of the libopencm3 project. |
| 3 | + * |
| 4 | + * Copyright (C) 2009 Uwe Hermann <[email protected]> |
| 5 | + * Copyright (C) 2011 Stephen Caudle <[email protected]> |
| 6 | + * Copyright (c) 2015 Chuck McManis <[email protected]> |
| 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/gpio.h> |
| 24 | + |
| 25 | +static void gpio_setup(void) |
| 26 | +{ |
| 27 | + /* Enable GPIOA clock. */ |
| 28 | + /* Manually: */ |
| 29 | + /*RCC_AHB1ENR |= RCC_AHB1ENR_IOPAEN; */ |
| 30 | + /* Using API functions: */ |
| 31 | + rcc_periph_clock_enable(RCC_GPIOA); |
| 32 | + |
| 33 | + /* Set GPIO5 (in GPIO port A) to 'output push-pull'. */ |
| 34 | + /* Manually: */ |
| 35 | + /*GPIOA_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((8 - 8) * 4) + 2)); */ |
| 36 | + /*GPIOA_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((8 - 8) * 4)); */ |
| 37 | + /* Using API functions: */ |
| 38 | + gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5); |
| 39 | +} |
| 40 | + |
| 41 | +int main(void) |
| 42 | +{ |
| 43 | + int i; |
| 44 | + |
| 45 | + gpio_setup(); |
| 46 | + |
| 47 | + /* Blink the LED (PC8) on the board. */ |
| 48 | + while (1) { |
| 49 | + /* Manually: */ |
| 50 | + /* GPIOA_BSRR = GPIO5; */ /* LED off */ |
| 51 | + /* for (i = 0; i < 1000000; i++) */ /* Wait a bit. */ |
| 52 | + /* __asm__("nop"); */ |
| 53 | + /* GPIOA_BRR = GPIO5; */ /* LED on */ |
| 54 | + /* for (i = 0; i < 1000000; i++) */ /* Wait a bit. */ |
| 55 | + /* __asm__("nop"); */ |
| 56 | + |
| 57 | + /* Using API functions gpio_set()/gpio_clear(): */ |
| 58 | + /* gpio_set(GPIOA, GPIO5); */ /* LED off */ |
| 59 | + /* for (i = 0; i < 1000000; i++) */ /* Wait a bit. */ |
| 60 | + /* __asm__("nop"); */ |
| 61 | + /* gpio_clear(GPIOA, GPIO5); */ /* LED on */ |
| 62 | + /* for (i = 0; i < 1000000; i++) */ /* Wait a bit. */ |
| 63 | + /* __asm__("nop"); */ |
| 64 | + |
| 65 | + /* Using API function gpio_toggle(): */ |
| 66 | + gpio_toggle(GPIOA, GPIO5); /* LED on/off */ |
| 67 | + for (i = 0; i < 1000000; i++) { /* Wait a bit. */ |
| 68 | + __asm__("nop"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
0 commit comments