rtc-reveil.c

STM32F103 "Blue Pill" (STM32F103C8T6) · 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 Blue Pill STM32F103C8T6 (Cortex-M3 @ 72 MHz, 64 KB flash) powered by micro-USB or external 3.3 V; programmed over SWD or the serial bootloader. 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 (STM32F103C8) or Arduino (STM32 core): program over SWD (ST-LINK, DAPLink adapter) or the serial bootloader (BOOT0 to 1). Note: PC13 LED is active LOW.

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(GPIOC, GPIO_PIN_13);
    }
    HAL_Delay(1000);
  }
}

← Back to the board