Skip to content

Commit b2a05c7

Browse files
committed
support a seconds interrupt based on the RTC feature
The stm32F1 device has specific one-second interrupt. For stm32 devices that do not have a seconds interrupt the RTC WakeUp is programmed to interrupt the system on a one-second period. The HAL_RTCEx_WakeUpTimerEventCallback is called instead keeping the same naming RTCSecondsIrqCallback. Some stm32F0 mcus do not support the wakeUp feature. Signed-off-by: Francois Ramu <[email protected]>
1 parent 9b7890d commit b2a05c7

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

src/rtc.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern "C" {
5858
static RTC_HandleTypeDef RtcHandle = {0};
5959
static voidCallbackPtr RTCUserCallback = NULL;
6060
static void *callbackUserData = NULL;
61+
static voidCallbackPtr RTCSecondsIrqCallback = NULL;
6162

6263
static sourceClock_t clkSrc = LSI_CLOCK;
6364
static uint8_t HSEDiv = 0;
@@ -388,6 +389,7 @@ void RTC_DeInit(void)
388389
HAL_RTC_DeInit(&RtcHandle);
389390
RTCUserCallback = NULL;
390391
callbackUserData = NULL;
392+
RTCSecondsIrqCallback = NULL;
391393
}
392394

393395
/**
@@ -731,6 +733,107 @@ void RTC_Alarm_IRQHandler(void)
731733
HAL_RTC_AlarmIRQHandler(&RtcHandle);
732734
}
733735

736+
#ifdef ONESECOND_IRQn
737+
/**
738+
* @brief Attach Seconds interrupt callback.
739+
* @note stm32F1 has a second interrupt capability
740+
* other MCUs map this on their WakeUp feature
741+
* @param func: pointer to the callback
742+
* @retval None
743+
*/
744+
void attachSecondsIrqCallback(voidCallbackPtr func)
745+
{
746+
#if defined(STM32F1xx)
747+
/* callback called on Seconds interrupt */
748+
RTCSecondsIrqCallback = func;
749+
750+
HAL_RTCEx_SetSecond_IT(&RtcHandle);
751+
__HAL_RTC_SECOND_CLEAR_FLAG(&RtcHandle, RTC_FLAG_SEC);
752+
#else
753+
/* callback called on wakeUp interrupt for One-Second purpose*/
754+
RTCSecondsIrqCallback = func;
755+
756+
/* for MCUs using the wakeup feature : irq each second */
757+
#if defined(RTC_WUTR_WUTOCLR)
758+
HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, 0);
759+
#else
760+
HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
761+
#endif /* RTC_WUTR_WUTOCLR */
762+
763+
#endif /* STM32F1xx */
764+
/* enable the IRQ that will trig the one-second interrupt */
765+
HAL_NVIC_EnableIRQ(ONESECOND_IRQn);
766+
}
767+
768+
/**
769+
* @brief Detach Seconds interrupt callback.
770+
* @param None
771+
* @retval None
772+
*/
773+
void detachSecondsIrqCallback(void)
774+
{
775+
#if defined(STM32F1xx)
776+
HAL_RTCEx_DeactivateSecond(&RtcHandle);
777+
#else
778+
/* for MCUs using the wakeup feature : do not deactivate the WakeUp
779+
as it might be used for another reason than the One-Second purpose */
780+
// HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
781+
#endif /* STM32F1xx */
782+
RTCSecondsIrqCallback = NULL;
783+
}
784+
785+
#if defined(STM32F1xx)
786+
/**
787+
* @brief Seconds interrupt callback.
788+
* @param hrtc RTC handle
789+
* @retval None
790+
*/
791+
void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
792+
{
793+
UNUSED(hrtc);
794+
795+
if (RTCSecondsIrqCallback != NULL) {
796+
RTCSecondsIrqCallback(NULL);
797+
}
798+
}
799+
800+
/**
801+
* @brief This function handles RTC Seconds interrupt request.
802+
* @param None
803+
* @retval None
804+
*/
805+
void RTC_IRQHandler(void)
806+
{
807+
HAL_RTCEx_RTCIRQHandler(&RtcHandle);
808+
}
809+
810+
#else
811+
/**
812+
* @brief WakeUp event mapping the Seconds interrupt callback.
813+
* @param hrtc RTC handle
814+
* @retval None
815+
*/
816+
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
817+
{
818+
UNUSED(hrtc);
819+
820+
if (RTCSecondsIrqCallback != NULL) {
821+
RTCSecondsIrqCallback(NULL);
822+
}
823+
}
824+
825+
/**
826+
* @brief This function handles RTC Seconds through wakeup interrupt request.
827+
* @param None
828+
* @retval None
829+
*/
830+
void RTC_WKUP_IRQHandler(void)
831+
{
832+
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
833+
}
834+
#endif /* STM32F1xx */
835+
#endif /* ONESECOND_IRQn */
836+
734837
#ifdef __cplusplus
735838
}
736839
#endif

src/rtc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ typedef void(*voidCallbackPtr)(void *);
109109
#define RTC_Alarm_IRQHandler RTC_TAMP_IRQHandler
110110
#endif
111111

112+
/* mapping the IRQn for the one-second interrupt depending on the soc */
113+
#if defined(STM32F1xx) || (defined(STM32F0xx) && defined(RTC_CR_WUTE)) || \
114+
defined(STM32L0xx) || defined(STM32L5xx) || defined(STM32U5xx) || \
115+
defined(STM32WLE4xx)
116+
// specific WakeUp interrupt
117+
#define ONESECOND_IRQn RTC_IRQn
118+
#elif defined(STM32MP1xx)
119+
// global RTC interrupt
120+
#define ONESECOND_IRQn RTC_WKUP_ALARM_IRQn
121+
#elif defined(STM32G0xx)
122+
// global RTC/TAMP interrupt
123+
#define ONESECOND_IRQn RTC_TAMP_IRQn
124+
#elif defined(STM32WL54xx)|| defined(STM32WL55xx)
125+
// global RTC/LSS interrupt
126+
#define ONESECOND_IRQn RTC_LSECSS_IRQn
127+
#elif defined(RTC_CR_WUTE)
128+
// specific WakeUp interrupt (including M4 cpu of the STM32WLE5xx)
129+
#define ONESECOND_IRQn RTC_WKUP_IRQn
130+
#else
131+
// no One-Second IRQ available for the series
132+
#endif /* STM32F1xx || etc */
133+
112134
#if defined(STM32F1xx) && !defined(IS_RTC_WEEKDAY)
113135
/* Compensate missing HAL definition */
114136
#define IS_RTC_WEEKDAY(WEEKDAY) (((WEEKDAY) == RTC_WEEKDAY_MONDAY) || \
@@ -161,6 +183,10 @@ void RTC_StopAlarm(void);
161183
void RTC_GetAlarm(uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period, uint8_t *mask);
162184
void attachAlarmCallback(voidCallbackPtr func, void *data);
163185
void detachAlarmCallback(void);
186+
#ifdef ONESECOND_IRQn
187+
void attachSecondsIrqCallback(voidCallbackPtr func);
188+
void detachSecondsIrqCallback(void);
189+
#endif /* ONESECOND_IRQn */
164190

165191
#ifdef __cplusplus
166192
}

0 commit comments

Comments
 (0)