🛠 STM32CubeF4 et HAL : blink + UART printf générés pour la Nucleo-F401RE
Goals
Leave the Arduino abstraction for a session and build a STM32Cube HAL project for the Nucleo-F401RE. STM32CubeF4 is ST's official firmware package — CMSIS + HAL/LL drivers + board BSP + middleware + ready-made example projects — and it explicitly ships projects for the STM32F401RE-Nucleo. Code generation happens in STM32CubeMX; flashing goes through the on-board ST-LINK with STM32CubeProgrammer.
Hardware
- 1× Nucleo-F401RE + micro-USB cable
- PC with STM32CubeMX and STM32CubeIDE (or a Makefile toolchain); >
Steps
git clone --recursive https://github.com/STMicroelectronics/STM32CubeF4
- Clone the package recursively — it is a submodule aggregate:
- In CubeMX, select the Nucleo-F401RE board, enable USART2 (PA2/PA3, the ST-LINK VCP) in Asynchronous mode.
- Generate the code, then wire the blink + printf loop below into
main.c. - Build and flash over ST-LINK; open the ST-LINK COM port at 115200.
Code
/* main.c — Nucleo-F401RE, STM32Cube HAL: LD2 blink on PA5 + USART2 (ST-LINK VCP) */
#include "stm32f4xx_hal.h"
extern UART_HandleTypeDef huart2; // generated by CubeMX (PA2/PA3 -> ST-LINK VCP)
static GPIO_InitTypeDef gpio = {0};
int main(void)
{
HAL_Init; /* SysTick @1 ms, NVIC defaults */
SystemClock_Config; /* generated: HSI/PLL -> 84 MHz SYSCLK */
__HAL_RCC_GPIOA_CLK_ENABLE;
gpio.Pin = GPIO_PIN_5; /* LD2 (green user LED) = PA5 */
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &gpio);
char msg[48]; uint32_t n = 0;
while (1) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(250);
int len = snprintf(msg, sizeof msg,
"hello from Nucleo-F401RE #%lu\r\n", (unsigned long)++n);
HAL_UART_Transmit(&huart2, (uint8_t *)msg, len, HAL_MAX_DELAY);
HAL_Delay(750);
}
}
Expected result
LD2 blinks at 2 Hz and the ST-LINK COM port prints a numbered hello once per second. The generated SystemClock_Config runs the F401 at its default 84 MHz SYSCLK derived from HSI through the PLL. From here, the Projects/STM32F401RE-Nucleo tree in the repo is your cookbook: GPIO, ADC, USART, USB and SDIO examples maintained by ST.
Common mistakes
git clonewithout--recursive: half the drivers are missing and nothing compiles.- Looking for STemWin, TouchGFX or STM32_Audio on GitHub: licensed middleware is click-through only, from st.com with an account.
- Scripted st.com downloads fail (404 or dropped connections) — fetch UM1760, RM0368 and datasheets from a browser.
- Forgetting
HAL_Init: no SysTick, so everyHAL_Delayspins forever.
Other tutorials for this board
- Prise en main de la Nucleo-F401RE : ST-LINK, LED LD2 et alimentation
- Premier Blink Arduino sur la Nucleo-F401RE avec le core STM32duino
- Console série sur le ST-LINK VCP : printf, ADC 12 bits et piège du USB
- ADC 12 bits et PWM sur la Nucleo-F401RE : oversampling, millivolts et LED en fondu
- Zephyr RTOS sur la Nucleo-F401RE : west build, devicetree et west flash
- Bare-metal sur le STM32F401 : clignoter LD2 en écrivant les registres GPIO