Using RTC in FreeRTOS
This application note explains how to use the STM32H7 RTC in the FreeRTOS demo application.
1. Understanding RTC Interfaces
1.1. FreeRTOS RTC Implementation
The FreeRTOS BSP makes use of the STM32CubeH7 software component to provide a device driver for the STM32H7 RTC. The driver is configured (enabled / disabled) at the BSP build time, using the HAL_RTC_MODULE_ENABLED
configuration option, defined in the stm32h7xx_hal_conf.h
file.
1.2. FreeRTOS RTC C-Binding API
The RTC driver implements the following C-binding API:
Function | Description | Comments |
| ||
| Initialize the RTC according to the specified parameters in hrtc |
|
| ||
| De-initialize the RTC peripheral |
|
| ||
| Get RTC current date |
|
| ||
| Get RTC current time |
|
| ||
| Set RTC current date |
|
| ||
| Get RTC current time |
|
The RTC_HandleTypeDef
data structure used in the HAL_RTC_Init
interface has the following definition:
typedef struct
{
RTC_TypeDef * Instance; /* Register base address */
RTC_InitTypeDef Init; /* RTC required parameters */
HAL_LockTypeDef Lock; /* RTC locking object */
__IO HAL_RTCStateTypeDef State; /* Time communication state */
} RTC_HandleTypeDef;
The RTC_InitTypeDef
data structure used in RTC_HandleTypeDef
has the following definition:
typedef struct
{
uint32_t HourFormat; /* Specifies the RTC Hour Format. */
uint32_t AsynchPrediv; /* Specifies the RTC Asynchronous Predivider value. */
uint32_t SynchPrediv; /* Specifies the RTC Synchronous Predivider value. */
uint32_t OutPut; /* Specifies which signal will be routed to the
RTC output. */
uint32_t OutPutRemap; /* Specifies the remap for RTC output. */
uint32_t OutPutPolarity; /* Specifies the polarity of the output signal. */
uint32_t OutPutType; /* Specifies the RTC Output Pin mode. */
} RTC_InitTypeDef;
2. RTC CLI Command
The FreeRTOS application implements the following RTC related CLI command:
Command | Description | Comments |
| Get the RTC time and date |
|
| Set the RTC time and date |
|
3. Validating RTC Operation
Use the following step-wise procedure to validate the FreeRTOS RTC operation:
Install a 3V Lithium battery CR1220 (or CR1216, CR1225, or any equivalent) in the BT1 battery holder.
From the FreeRTOS Command Line Interface, set the RTC time and date to the current wall time:
CLI> rtc_set_date 2019:05:09:12:00 CLI>
Power off the board. Let it sit unpowered for at least 1 hour.
Power on the target board and let it boot to the FreeRTOS CLI.
Validate that the RTC time and date continue to match the current wall time: