Skip to content

Commit a8f2397

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. Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 8d7dae5 commit a8f2397

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

src/rtc.c

Lines changed: 100 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,104 @@ void RTC_Alarm_IRQHandler(void)
731733
HAL_RTC_AlarmIRQHandler(&RtcHandle);
732734
}
733735

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

src/rtc.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ 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(STM32L0xx) || defined(STM32L5x) || defined(STM32U5xx)
114+
// specific WakeUp interrupt
115+
#define ONESECOND_IRQn RTC_IRQn
116+
#elif defined(STM32MP1xx)
117+
// global RTC interrupt
118+
#define ONESECOND_IRQn RTC_WKUP_ALARM_IRQn
119+
#elif defined(STM32G0xx)
120+
// global RTC/TAMP interrupt
121+
#define ONESECOND_IRQn RTC_TAMP_IRQn
122+
#elif defined(STM32WLxx)
123+
// global RTC/LSS interrupt
124+
#define ONESECOND_IRQn RTC_LSECSS_IRQn
125+
#else
126+
// specific WakeUp interrupt
127+
#define ONESECOND_IRQn RTC_WKUP_IRQn
128+
#endif /* RTC_WKUP_IRQn */
129+
112130
#if defined(STM32F1xx) && !defined(IS_RTC_WEEKDAY)
113131
/* Compensate missing HAL definition */
114132
#define IS_RTC_WEEKDAY(WEEKDAY) (((WEEKDAY) == RTC_WEEKDAY_MONDAY) || \
@@ -161,6 +179,8 @@ void RTC_StopAlarm(void);
161179
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);
162180
void attachAlarmCallback(voidCallbackPtr func, void *data);
163181
void detachAlarmCallback(void);
182+
void attachSecondsIrqCallback(voidCallbackPtr func);
183+
void detachSecondsIrqCallback(void);
164184

165185
#ifdef __cplusplus
166186
}

0 commit comments

Comments
 (0)