Nucleo-F401RE: Serial commands
A command line on USART2, set up in registers: type commands in the serial monitor to switch LD2 and read the clock and uptime.
Communication · Beginner
In a real product: Lab instrument console (Industrial): Engineers set up and query the instrument by typing commands over a serial port.
On a Nucleo, the serial port is USART2 on PA2 (TX) and PA3 (RX). Those pins go to the ST-LINK chip at the top of the board, which passes the bytes on over USB, so the cable that programs the board is also its serial port.
This program sets USART2 up itself, collects what you type into a line, and answers a few commands.
What you'll learn
- Giving PA2 and PA3 to USART2 with
MODER(alternate function) andAFR(AF7) - Setting the baud rate in
USART2->BRRfrom the clock the USART runs on - Sending by waiting for
TXE, and receiving inUSART2_IRQHandlerinto a ring buffer so no byte is lost
How it works
The USART divides its clock, here APB1 at 16 MHz, by BRR to get the bit rate: 16 000 000 / 115 200 ≈ 139. The chip sends a byte when it is written to DR and sets TXE when it can take the next; __io_putchar() waits for it, and the C library's printf() calls __io_putchar() for each character.
A received byte sets RXNE, and reading DR takes it and clears the flag. The USART holds only one byte, so one that arrives before the last is read is lost (an overrun). While the loop is busy printing an answer it cannot read, so RXNEIE raises an interrupt for every byte instead, and USART2_IRQHandler puts it in a 64-byte ring buffer. The loop takes bytes out when it has time, adds them to line until a \r or \n ends it, and runs the command.
The handler only writes head and the loop only writes tail, so the two never step on each other: that is what makes a ring buffer with one writer at each end safe without turning interrupts off.
status prints SystemCoreClock: 16 MHz, the internal oscillator the chip starts on. Nothing here turns on the PLL.
Try this
- Add a
blinkcommand that flashes LD2 five times. - Turn
RXNEIEoff and pollRXNEin the loop, then paste two commands at once: one is garbled. - Run the core at 84 MHz from the PLL, then fix
BRRfor the new APB1 clock (42 MHz).
main.c
// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// A command line on USART2 (PA2 TX, PA3 RX, through the ST-LINK): led on, led off, status, help.
#include <stdio.h>
#include <string.h>
#include "stm32f4xx.h"
static volatile uint32_t ticks; // milliseconds since SysTick started
// Received bytes, from the interrupt to the loop: the handler writes head, the loop writes tail.
static volatile uint8_t rx[64];
static volatile unsigned head, tail;
void SysTick_Handler(void) {
ticks++;
}
void USART2_IRQHandler(void) {
if (USART2->SR & USART_SR_RXNE) {
uint8_t c = USART2->DR; // reading DR clears RXNE
unsigned next = (head + 1) % sizeof rx;
if (next != tail) { // full: drop the byte
rx[head] = c;
head = next;
}
}
}
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
static void help(void) {
printf("Commands: led on, led off, status, help\r\n");
}
static void run(const char *line) {
if (!strcmp(line, "led on")) {
GPIOA->BSRR = GPIO_BSRR_BS5;
printf("LD2 on\r\n");
} else if (!strcmp(line, "led off")) {
GPIOA->BSRR = GPIO_BSRR_BR5;
printf("LD2 off\r\n");
} else if (!strcmp(line, "status")) {
printf("Core clock: %lu Hz, up %lu s\r\n", (unsigned long)SystemCoreClock, (unsigned long)(ticks / 1000));
} else if (line[0]) {
help();
}
}
int main(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
// PA5 output (LD2); PA2 and PA3 alternate function, AF7: USART2 TX and RX.
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER5 | GPIO_MODER_MODER2 | GPIO_MODER_MODER3))
| GPIO_MODER_MODER5_0 | GPIO_MODER_MODER2_1 | GPIO_MODER_MODER3_1;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFFu << 8)) | (7u << 8) | (7u << 12);
USART2->BRR = SystemCoreClock / 115200; // 16 MHz HSI after reset
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_UE;
NVIC_EnableIRQ(USART2_IRQn);
SysTick->LOAD = SystemCoreClock / 1000 - 1; // 1 ms
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
printf("Nucleo-F401RE ready\r\n");
help();
char line[32];
unsigned len = 0;
while (1) {
if (tail == head) continue;
char c = (char)rx[tail];
tail = (tail + 1) % sizeof rx;
if (c == '\r' || c == '\n') {
line[len] = '\0';
run(line);
len = 0;
} else if (len < sizeof line - 1) {
line[len++] = c;
}
}
}