rtc-reveil.c

STM32H7 Nucleo-144 (flagship: NUCLEO-H743ZI) · File : rtc-reveil.c · Language : C · Use case : Réveil périodique avec la RTC et mode veille bas courant

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 H7 board (STM32H743ZI, Cortex-M7 @ 480 MHz) 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

STM32CubeIDE project based on the Nucleo-H743ZI: drop the code into main.c, declare peripherals in the .ioc, then Run over ST-LINK. The ST-LINK serial monitor 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(GPIOB, GPIO_PIN_0);
    }
    HAL_Delay(1000);
  }
}

← Back to the board