|
| 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 | +#include <libopencm3/stm32/usart.h> |
| 25 | + |
| 26 | +static void clock_setup(void) |
| 27 | +{ |
| 28 | + /* Enable GPIOD clock for LED & USARTs. */ |
| 29 | + rcc_periph_clock_enable(RCC_GPIOA); |
| 30 | + |
| 31 | + /* Enable clocks for USART2. */ |
| 32 | + rcc_periph_clock_enable(RCC_USART2); |
| 33 | +} |
| 34 | + |
| 35 | +static void usart_setup(void) |
| 36 | +{ |
| 37 | + /* Setup USART2 parameters. */ |
| 38 | + usart_set_baudrate(USART2, 115200); |
| 39 | + usart_set_databits(USART2, 8); |
| 40 | + usart_set_stopbits(USART2, USART_STOPBITS_1); |
| 41 | + usart_set_mode(USART2, USART_MODE_TX); |
| 42 | + usart_set_parity(USART2, USART_PARITY_NONE); |
| 43 | + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); |
| 44 | + |
| 45 | + /* Finally enable the USART. */ |
| 46 | + usart_enable(USART2); |
| 47 | +} |
| 48 | + |
| 49 | +static void gpio_setup(void) |
| 50 | +{ |
| 51 | + /* Setup GPIO pin GPIO5 on GPIO port A for LED. */ |
| 52 | + gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5); |
| 53 | + |
| 54 | + /* Setup GPIO pins for USART2 transmit. */ |
| 55 | + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); |
| 56 | + |
| 57 | + /* Setup USART2 TX pin as alternate function. */ |
| 58 | + gpio_set_af(GPIOA, GPIO_AF7, GPIO2); |
| 59 | +} |
| 60 | + |
| 61 | +int main(void) |
| 62 | +{ |
| 63 | + int i, j = 0, c = 0; |
| 64 | + |
| 65 | + clock_setup(); |
| 66 | + gpio_setup(); |
| 67 | + usart_setup(); |
| 68 | + |
| 69 | + /* Blink the LED (PD12) on the board with every transmitted byte. */ |
| 70 | + while (1) { |
| 71 | + gpio_toggle(GPIOA, GPIO5); /* LED on/off */ |
| 72 | + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ |
| 73 | + c = (c == 9) ? 0 : c + 1; /* Increment c. */ |
| 74 | + if ((j++ % 80) == 0) { /* Newline after line full. */ |
| 75 | + usart_send_blocking(USART2, '\r'); |
| 76 | + usart_send_blocking(USART2, '\n'); |
| 77 | + } |
| 78 | + for (i = 0; i < 300000; i++) { /* Wait a bit. */ |
| 79 | + __asm__("NOP"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments