rtc-reveil.c
The RTC keeps time even with power removed (coin cell or supercap on the board): this program prints the clock continuously and fires a wake-up every 10 s, the basis of low-power periodic-wake stations.
Hardware
A Nucleo F4 board (STM32F401RE/F411RE, Cortex-M4) programmed via ST-LINK (USB), 3.3 V pins. Optional: coin cell or supercapacitor to keep the clock running off-power.
Wiring
No wiring. Enable the RTC in CubeMX (LSE 32.768 kHz if a crystal is fitted, else LSI) and set the start date/time.
How it works
HAL_RTC_GetTime/GetDate read the counters; the GetTime-then-GetDate order is required by the HAL internal lock. The wake-up can also use the WakeUp timer to exit Stop mode.
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
Switch to Stop mode with a WakeUp timer (current divided by ~100), or sync the time over UART (NMEA-like protocol).
rtc-reveil.c
// RTC : affichage de l'heure + reveveil toutes les 10 s.
#include "main.h"
#include <stdio.h>
extern RTC_HandleTypeDef hrtc;
extern UART_HandleTypeDef huart2;
int __io_putchar(int ch) {
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
RTC_TimeTypeDef t;
RTC_DateTypeDef d;
uint32_t dernierReveil = 0;
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_RTC_Init(); // horloge LSE/Lsi + date/heure initiales dans CubeMX
for (;;) {
HAL_RTC_GetTime(&hrtc, &t, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &d, RTC_FORMAT_BIN);
// NB: GetDate apres GetTime (obligation HAL pour le lock).
printf("%02u:%02u:%02u %02u/%02u/%02u\r\n",
t.Hours, t.Minutes, t.Seconds, d.Date, d.Month, d.Year);
// reveveil logiciel simple (ou RTC_Alarm / WakeUp timer)
if (HAL_GetTick() - dernierReveil >= 10000) {
dernierReveil = HAL_GetTick();
printf("--- REVEIL ---\r\n");
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
HAL_Delay(1000);
}
}