Skip to content

Commit 4837775

Browse files
ChuckMesden
authored andcommitted
Nucleo F411RE - Simple USART example
Same as the one for other boards
1 parent 8019726 commit 4837775

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-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) 2015 Chuck McManis <[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 = usart
22+
23+
LDSCRIPT = ../nucleo-f411re.ld
24+
25+
include ../../Makefile.include
26+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# README
2+
3+
This example program sends repeating sequence of characters "0123456789" on
4+
USART2 on the ST Nucleo F411RE eval board.
5+
6+
The sending is done in a blocking way.
7+
8+
Note that the Nucleo board exports the serial port as an ttyACM device on
9+
Linux or a COM port on Windows. On Linux, if you can locate the correct
10+
serial port as
11+
12+
/dev/serial/by-id/usb-STMicroelectronics_STM32_STLink_066CFF515056805087171825-if02
13+
14+
(note there will be differences based on the serial number of your board)
15+
this may also show up as /dev/ttyACM0 if it was the first serial port
16+
enumerated, if you have additional serial devices attached to the USB port
17+
of your system they may show up as additional ttyACM1/ttyACM2/... etc.
18+
19+
Connect to the port using `screen /dev/ttyACM0 115200` where you replace
20+
the serial port with the one for your system.
21+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)