clignotante.c
The reference blink in bare C with STM32 HAL: the board LED on pin 5 toggles every 500 ms thanks to the 1 ms HAL SysTick. The skeleton is the one generated by STM32CubeIDE, ready to compile.
Hardware
A Nucleo F4 board (STM32F401RE/F411RE, Cortex-M4) programmed via ST-LINK (USB), 3.3 V pins. None: the user LED (LD2 verte, broche PA5) is enough. The board is programmed via its USB connector (ST-LINK or bootloader).
Wiring
Nothing to wire: internal LED on GPIOA/GPIO_PIN_5, active HIGH.
How it works
HAL_Init() sets up SysTick (1 ms tick); HAL_GetTick() acts as a non-blocking clock. HAL_GPIO_WritePin drives the push-pull pin. __WFI() sleeps the CPU between ticks.
Running it
Create a STM32CubeIDE project (pick Nucleo-F401RE or F411RE), paste the code into main.c, configure peripherals in the .ioc, then Run. The ST-LINK serial terminal shows printf at 115200.
Variants
Create the project from the ST-provided .ioc (or an equivalent one), or use the LL library for leaner code.
clignotante.c
// Clignotement LED en STM32 HAL (projet genere par STM32CubeIDE/.ioc).
#include "main.h"
/* Broche de la LED utilisateur de la carte. */
#define LED_PORT GPIOA
#define LED_PIN GPIO_PIN_5
/* LED active a l'etat HIGH. */
uint32_t dernierBlink = 0;
uint8_t etat = 0;
int main(void) {
HAL_Init(); // SysTick 1 ms + priorites NVIC
SystemClock_Config(); // horloge generee depuis CubeMX
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef g = {0};
g.Pin = LED_PIN;
g.Mode = GPIO_MODE_OUTPUT_PP; // sortie push-pull
g.Pull = GPIO_NOPULL;
g.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &g);
for (;;) {
if (HAL_GetTick() - dernierBlink >= 500) { // 500 ms
dernierBlink = HAL_GetTick();
etat ^= 1;
HAL_GPIO_WritePin(LED_PORT, LED_PIN, etat ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
__WFI(); // attend l'interruption SysTick : consomme moins
}
}