L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
rtc.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file rtc.c
5 * @brief This file provides code for the configuration
6 * of the RTC instances.
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "rtc.h"
22
23/* USER CODE BEGIN 0 */
24#include "stm32_hal_legacy.h"
25/* USER CODE END 0 */
26
27RTC_HandleTypeDef hrtc;
28
29/* RTC init function */
30void MX_RTC_Init(void)
31{
32
33 /* USER CODE BEGIN RTC_Init 0 */
34
35 /* USER CODE END RTC_Init 0 */
36
37 RTC_AlarmTypeDef sAlarm = {0};
38
39 /* USER CODE BEGIN RTC_Init 1 */
40
41 /* USER CODE END RTC_Init 1 */
42
43 /** Initialize RTC Only
44 */
45 hrtc.Instance = RTC;
46 hrtc.Init.AsynchPrediv = RTC_PREDIV_A;
47 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
48 hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
49 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
50 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
51 hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
52 hrtc.Init.BinMode = RTC_BINARY_ONLY;
53 if (HAL_RTC_Init(&hrtc) != HAL_OK)
54 {
56 }
57
58 /* USER CODE BEGIN Check_RTC_BKUP */
59
60 /* USER CODE END Check_RTC_BKUP */
61
62 /** Initialize RTC and set the Time and Date
63 */
64 if (HAL_RTCEx_SetSSRU_IT(&hrtc) != HAL_OK)
65 {
67 }
68
69 /** Enable the Alarm A
70 */
71 sAlarm.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO;
72 sAlarm.AlarmTime.SubSeconds = 0x0;
73 sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
74 sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDBINMASK_NONE;
75 sAlarm.Alarm = RTC_ALARM_A;
76 if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, 0) != HAL_OK)
77 {
79 }
80 /* USER CODE BEGIN RTC_Init 2 */
81
82
83 /* USER CODE END RTC_Init 2 */
84
85}
86
87void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
88{
89
90 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
91 if(rtcHandle->Instance==RTC)
92 {
93 /* USER CODE BEGIN RTC_MspInit 0 */
94
95 /* USER CODE END RTC_MspInit 0 */
96
97 /** Initializes the peripherals clocks
98 */
99 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
100 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
101
102 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
103 {
105 }
106
107 /* RTC clock enable */
108 __HAL_RCC_RTC_ENABLE();
109 __HAL_RCC_RTCAPB_CLK_ENABLE();
110
111 /* RTC interrupt Init */
112 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 1, 0);
113 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
114 /* USER CODE BEGIN RTC_MspInit 1 */
115
116 /* USER CODE END RTC_MspInit 1 */
117 }
118}
119
120void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
121{
122
123 if(rtcHandle->Instance==RTC)
124 {
125 /* USER CODE BEGIN RTC_MspDeInit 0 */
126
127 /* USER CODE END RTC_MspDeInit 0 */
128 /* Peripheral clock disable */
129 __HAL_RCC_RTC_DISABLE();
130 __HAL_RCC_RTCAPB_CLK_DISABLE();
131
132 /* RTC interrupt Deinit */
133 HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
134 /* USER CODE BEGIN RTC_MspDeInit 1 */
135
136 /* USER CODE END RTC_MspDeInit 1 */
137 }
138}
139
140/* USER CODE BEGIN 1 */
141
142/**
143 * @brief Configure RTC wakeup timer for 10 minutes
144 *
145 * Calculation for 10 minutes (600 seconds):
146 * - Using RTC_WAKEUPCLOCK_CK_SPRE_16BITS which provides a 1 Hz clock
147 * - This clock is derived from the RTC prescalers (ck_spre = 1 Hz)
148 * - Counter value for 10 minutes: 600 - 1 = 599
149 * - Maximum counter value is 0xFFFF (65535), which allows up to ~18.2 hours
150 * - So 10 minutes (600 seconds) is well within limits
151 */
152HAL_StatusTypeDef RTC_SetWakeupTimer_10Minutes(void)
153{
154 // Stop the wakeup timer first if it's running
155 HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
156
157 // Configure wakeup timer for 10 minutes (600 seconds)
158 // Using CK_SPRE (1 Hz clock from RTC)
159 // Counter value = desired_seconds - 1 = 600 - 1 = 599
160 // (The counter triggers when it reaches 0, so we count from 599 down to 0 = 600 ticks)
161 uint32_t wakeUp = 59;//599
162 if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, wakeUp, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, 0) != HAL_OK) // MT 10.1.2026 dal som 0
163 {
164 return HAL_ERROR;
165 }
166
167 return HAL_OK;
168}
169
170/* USER CODE END 1 */
void Error_Handler(void)
Default error handler called by HAL on unrecoverable errors. Disables interrupts and enters an infini...
Definition main.c:505
#define RTC_PREDIV_A
Definition main.h:138
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
Definition rtc.c:120
void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle)
Definition rtc.c:87
HAL_StatusTypeDef RTC_SetWakeupTimer_10Minutes(void)
Configure RTC wakeup timer for 10 minutes.
Definition rtc.c:152
void MX_RTC_Init(void)
Initialise the RTC peripheral with settings generated by STM32CubeMX. Configures the prescalers,...
Definition rtc.c:30
This file contains all the function prototypes for the rtc.c file.
RTC_HandleTypeDef hrtc
Definition rtc.c:27