Skip to content

Commit 57ae705

Browse files
committed
feat: add SubSecondinterrupt underflow api for STM32WLxx
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 82267c2 commit 57ae705

File tree

6 files changed

+101
-46
lines changed

6 files changed

+101
-46
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ by the RTC. Thus the getEpoch function is only to be called to get the subsecond
179179
(returned time_t is not valid). The setAlarmEpoch only uses the sub-second [0..0xFFFFFFFF]
180180
(time_t value is useless).
181181

182+
_SubSeconds underflow_
183+
184+
Only dor STM32WLxx. MAange interrrupt (SSRU) when SubSeconds register
185+
underflow. Used by STM32LoRaWAN.
186+
187+
* **`void attachSubSecondsUnderflowInterrupt(voidFuncPtr callback);`**
188+
* **`void detachSubSecondsUnderflowInterrupt(void);`**
189+
182190
Refer to the Arduino RTC documentation for the other functions
183191
http://arduino.cc/en/Reference/RTC
184192

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ attachInterrupt KEYWORD2
7272
detachInterrupt KEYWORD2
7373
attachSecondsInterrupt KEYWORD2
7474
detachSecondsInterrupt KEYWORD2
75+
attachSubSecondsUnderflowInterrupt KEYWORD2
76+
detachSubSecondsUnderflowInterrupt KEYWORD2
7577

7678
getClockSource KEYWORD2
7779
setClockSource KEYWORD2

src/STM32RTC.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,31 @@ void STM32RTC::detachSecondsInterrupt(void)
334334
}
335335

336336
#endif /* ONESECOND_IRQn */
337+
338+
#ifdef STM32WLxx
339+
/**
340+
* @brief attach a callback to the RTC SubSeconds underflow interrupt.
341+
* @note only for STM32WLxx
342+
* @param callback: pointer to the callback
343+
* @retval None
344+
*/
345+
void STM32RTC::attachSubSecondsUnderflowInterrupt(voidFuncPtr callback)
346+
{
347+
attachSubSecondsUnderflowIrqCallback(callback);
348+
}
349+
350+
/**
351+
* @brief detach the RTC SubSeconds underflow callback.
352+
* @retval None
353+
*/
354+
void STM32RTC::detachSubSecondsUnderflowInterrupt(void)
355+
{
356+
detachSubSecondsUnderflowIrqCallback();
357+
}
358+
359+
#endif /* STM32WLxx */
360+
361+
337362
// Kept for compatibility. Use STM32LowPower library.
338363
void STM32RTC::standbyMode(void)
339364
{

src/STM32RTC.h

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ class STM32RTC {
159159
void detachSecondsInterrupt(void);
160160

161161
#endif /* ONESECOND_IRQn */
162+
#ifdef STM32WLxx
163+
// STM32WLxx has a dedicated IRQ
164+
void attachSubSecondsUnderflowInterrupt(voidFuncPtr callback);
165+
void detachSubSecondsUnderflowInterrupt(void);
166+
#endif /* STM32WLxx */
162167
// Kept for compatibility: use STM32LowPower library.
163168
void standbyMode();
164169

src/rtc.c

+51-43
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ static void *callbackUserDataB = NULL;
6666
#ifdef ONESECOND_IRQn
6767
static voidCallbackPtr RTCSecondsIrqCallback = NULL;
6868
#endif
69+
#ifdef STM32WLxx
70+
static voidCallbackPtr RTCSubSecondsUnderflowIrqCallback = NULL;
71+
#endif
6972
static sourceClock_t clkSrc = LSI_CLOCK;
7073
static uint32_t clkVal = LSI_VALUE;
7174
static uint8_t HSEDiv = 0;
@@ -98,49 +101,6 @@ static inline int _log2(int x)
98101
}
99102

100103
/* Exported functions --------------------------------------------------------*/
101-
102-
/* HAL MSP function used for RTC_Init */
103-
void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle)
104-
{
105-
#if defined(RTC_SCR_CSSRUF)
106-
if (rtcHandle->Instance == RTC) {
107-
/* In BINARY mode (MIX or ONLY), set the SSR Underflow interrupt */
108-
if (rtcHandle->Init.BinMode != RTC_BINARY_NONE) {
109-
#if defined(STM32WLxx)
110-
/* Only the STM32WLxx series has a TAMP_STAMP_LSECSS_SSRU_IRQn */
111-
if (HAL_RTCEx_SetSSRU_IT(rtcHandle) != HAL_OK) {
112-
Error_Handler();
113-
}
114-
/* Give init value for the RtcFeatures enable */
115-
rtcHandle->IsEnabled.RtcFeatures = 0;
116-
117-
/* RTC interrupt Init */
118-
HAL_NVIC_SetPriority(TAMP_STAMP_LSECSS_SSRU_IRQn, 0, 0);
119-
HAL_NVIC_EnableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
120-
#else
121-
/* The STM32U5, STM32H5, STM32L4plus have common RTC interrupt and a SSRU flag */
122-
__HAL_RTC_SSRU_ENABLE_IT(rtcHandle, RTC_IT_SSRU);
123-
#endif /* STM32WLxx */
124-
}
125-
}
126-
#else /* RTC_SCR_CSSRUF */
127-
UNUSED(rtcHandle);
128-
#endif /* RTC_SCR_CSSRUF */
129-
/* RTC_Alarm_IRQn is enabled when enabling Alarm */
130-
}
131-
132-
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
133-
{
134-
135-
if (rtcHandle->Instance == RTC) {
136-
/* RTC interrupt Deinit */
137-
#if defined(STM32WLxx)
138-
/* Only the STM32WLxx series has a TAMP_STAMP_LSECSS_SSRU_IRQn */
139-
HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
140-
#endif /* STM32WLxx */
141-
}
142-
}
143-
144104
/**
145105
* @brief Get pointer to RTC_HandleTypeDef
146106
* @param None
@@ -638,6 +598,9 @@ void RTC_DeInit(bool reset_cb)
638598
HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
639599
#ifdef ONESECOND_IRQn
640600
HAL_NVIC_DisableIRQ(ONESECOND_IRQn);
601+
#endif
602+
#ifdef STM32WLxx
603+
HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
641604
#endif
642605
if (reset_cb) {
643606
RTCUserCallback = NULL;
@@ -648,6 +611,9 @@ void RTC_DeInit(bool reset_cb)
648611
#endif
649612
#ifdef ONESECOND_IRQn
650613
RTCSecondsIrqCallback = NULL;
614+
#endif
615+
#ifdef STM32WLxx
616+
RTCSubSecondsUnderflowIrqCallback = NULL;
651617
#endif
652618
}
653619
}
@@ -1266,6 +1232,48 @@ void RTC_WKUP_IRQHandler(void)
12661232
#endif /* STM32F1xx */
12671233
#endif /* ONESECOND_IRQn */
12681234

1235+
#ifdef STM32WLxx
1236+
/**
1237+
* @brief Attach SubSeconds underflow interrupt callback.
1238+
* @param func: pointer to the callback
1239+
* @retval None
1240+
*/
1241+
void attachSubSecondsUnderflowIrqCallback(voidCallbackPtr func)
1242+
{
1243+
/* Callback called on SSRU interrupt */
1244+
RTCSubSecondsUnderflowIrqCallback = func;
1245+
1246+
/* Enable the IRQ that will trig the one-second interrupt */
1247+
if (HAL_RTCEx_SetSSRU_IT(&RtcHandle) != HAL_OK) {
1248+
Error_Handler();
1249+
}
1250+
HAL_NVIC_SetPriority(TAMP_STAMP_LSECSS_SSRU_IRQn, RTC_IRQ_SSRU_PRIO, RTC_IRQ_SSRU_SUBPRIO);
1251+
HAL_NVIC_EnableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
1252+
}
1253+
1254+
/**
1255+
* @brief Detach SubSeconds underflow interrupt callback.
1256+
* @param None
1257+
* @retval None
1258+
*/
1259+
void detachSubSecondsUnderflowIrqCallback(void)
1260+
{
1261+
RTCSubSecondsUnderflowIrqCallback = NULL;
1262+
if (HAL_RTCEx_DeactivateSSRU(&RtcHandle) != HAL_OK) {
1263+
Error_Handler();
1264+
}
1265+
HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
1266+
}
1267+
1268+
void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
1269+
{
1270+
(void)hrtc;
1271+
if (RTCSubSecondsUnderflowIrqCallback != NULL) {
1272+
RTCSubSecondsUnderflowIrqCallback(NULL);
1273+
}
1274+
}
1275+
#endif /* STM32WLxx */
1276+
12691277
#if defined(STM32F1xx)
12701278
void RTC_StoreDate(void)
12711279
{

src/rtc.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ typedef void(*voidCallbackPtr)(void *);
108108
#ifndef RTC_IRQ_SUBPRIO
109109
#define RTC_IRQ_SUBPRIO 0
110110
#endif
111-
112-
111+
#ifndef RTC_IRQ_SSRU_PRIO
112+
#define RTC_IRQ_SSRU_PRIO 0
113+
#endif
114+
#ifndef RTC_IRQ_SSRU_SUBPRIO
115+
#define RTC_IRQ_SSRU_SUBPRIO 0
116+
#endif
113117
#define HSE_RTC_MAX 1000000U
114118

115119
#if !defined(STM32F1xx)
@@ -198,7 +202,10 @@ void detachAlarmCallback(alarm_t name);
198202
void attachSecondsIrqCallback(voidCallbackPtr func);
199203
void detachSecondsIrqCallback(void);
200204
#endif /* ONESECOND_IRQn */
201-
205+
#ifdef STM32WLxx
206+
void attachSubSecondsUnderflowIrqCallback(voidCallbackPtr func);
207+
void detachSubSecondsUnderflowIrqCallback(void);
208+
#endif /* STM32WLxx */
202209
#if defined(STM32F1xx)
203210
void RTC_StoreDate(void);
204211
#endif

0 commit comments

Comments
 (0)