Skip to content

Commit 1524cb8

Browse files
authored
Merge pull request #103 from fpistm/SSRU_management
SSRU management
2 parents 3ce53c4 + dccec10 commit 1524cb8

File tree

6 files changed

+115
-63
lines changed

6 files changed

+115
-63
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. Manage interrupt (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

+26-10
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
{
@@ -851,16 +876,7 @@ void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds, Alarm name)
851876
#ifndef RTC_ALARM_B
852877
UNUSED(name);
853878
#endif
854-
if (_mode == MODE_BIN) {
855-
#ifdef RTC_ALARM_B
856-
if (name == ALARM_B) {
857-
_alarmBSubSeconds = subSeconds;
858-
} else
859-
#endif
860-
{
861-
_alarmSubSeconds = subSeconds;
862-
}
863-
} else if (subSeconds < 1000) {
879+
if ((_mode == MODE_BIN) || (subSeconds < 1000)) {
864880
#ifdef RTC_ALARM_B
865881
if (name == ALARM_B) {
866882
_alarmBSubSeconds = subSeconds;

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

+64-50
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ static void *callbackUserData = NULL;
6363
static voidCallbackPtr RTCUserCallbackB = NULL;
6464
static void *callbackUserDataB = NULL;
6565
#endif
66+
#ifdef ONESECOND_IRQn
6667
static voidCallbackPtr RTCSecondsIrqCallback = NULL;
67-
68+
#endif
69+
#ifdef STM32WLxx
70+
static voidCallbackPtr RTCSubSecondsUnderflowIrqCallback = NULL;
71+
#endif
6872
static sourceClock_t clkSrc = LSI_CLOCK;
6973
static uint32_t clkVal = LSI_VALUE;
7074
static uint8_t HSEDiv = 0;
@@ -97,55 +101,6 @@ static inline int _log2(int x)
97101
}
98102

99103
/* Exported functions --------------------------------------------------------*/
100-
101-
/* HAL MSP function used for RTC_Init */
102-
void HAL_RTC_MspInit(RTC_HandleTypeDef *rtcHandle)
103-
{
104-
#if defined(RTC_SCR_CSSRUF)
105-
if (rtcHandle->Instance == RTC) {
106-
/* In BINARY mode (MIX or ONLY), set the SSR Underflow interrupt */
107-
if (rtcHandle->Init.BinMode != RTC_BINARY_NONE) {
108-
#if defined(STM32WLxx)
109-
/* Only the STM32WLxx series has a TAMP_STAMP_LSECSS_SSRU_IRQn */
110-
if (HAL_RTCEx_SetSSRU_IT(rtcHandle) != HAL_OK) {
111-
Error_Handler();
112-
}
113-
/* Give init value for the RtcFeatures enable */
114-
rtcHandle->IsEnabled.RtcFeatures = 0;
115-
116-
/* RTC interrupt Init */
117-
HAL_NVIC_SetPriority(TAMP_STAMP_LSECSS_SSRU_IRQn, 0, 0);
118-
HAL_NVIC_EnableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
119-
#else
120-
/* The STM32U5, STM32H5, STM32L4plus have common RTC interrupt and a SSRU flag */
121-
__HAL_RTC_SSRU_ENABLE_IT(rtcHandle, RTC_IT_SSRU);
122-
#endif /* STM32WLxx */
123-
}
124-
}
125-
#else /* RTC_SCR_CSSRUF */
126-
UNUSED(rtcHandle);
127-
#endif /* RTC_SCR_CSSRUF */
128-
/* RTC_Alarm_IRQn is enabled when enabling Alarm */
129-
}
130-
131-
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *rtcHandle)
132-
{
133-
134-
if (rtcHandle->Instance == RTC) {
135-
/* Peripheral clock disable */
136-
__HAL_RCC_RTC_DISABLE();
137-
#ifdef __HAL_RCC_RTCAPB_CLK_DISABLE
138-
__HAL_RCC_RTCAPB_CLK_DISABLE();
139-
#endif
140-
/* RTC interrupt Deinit */
141-
#if defined(STM32WLxx)
142-
/* Only the STM32WLxx series has a TAMP_STAMP_LSECSS_SSRU_IRQn */
143-
HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
144-
#endif /* STM32WLxx */
145-
HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
146-
}
147-
}
148-
149104
/**
150105
* @brief Get pointer to RTC_HandleTypeDef
151106
* @param None
@@ -635,14 +590,31 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
635590
void RTC_DeInit(bool reset_cb)
636591
{
637592
HAL_RTC_DeInit(&RtcHandle);
593+
/* Peripheral clock disable */
594+
__HAL_RCC_RTC_DISABLE();
595+
#ifdef __HAL_RCC_RTCAPB_CLK_DISABLE
596+
__HAL_RCC_RTCAPB_CLK_DISABLE();
597+
#endif
598+
HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
599+
#ifdef ONESECOND_IRQn
600+
HAL_NVIC_DisableIRQ(ONESECOND_IRQn);
601+
#endif
602+
#ifdef STM32WLxx
603+
HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn);
604+
#endif
638605
if (reset_cb) {
639606
RTCUserCallback = NULL;
640607
callbackUserData = NULL;
641608
#ifdef RTC_ALARM_B
642609
RTCUserCallbackB = NULL;
643610
callbackUserDataB = NULL;
644611
#endif
612+
#ifdef ONESECOND_IRQn
645613
RTCSecondsIrqCallback = NULL;
614+
#endif
615+
#ifdef STM32WLxx
616+
RTCSubSecondsUnderflowIrqCallback = NULL;
617+
#endif
646618
}
647619
}
648620

@@ -1260,6 +1232,48 @@ void RTC_WKUP_IRQHandler(void)
12601232
#endif /* STM32F1xx */
12611233
#endif /* ONESECOND_IRQn */
12621234

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+
12631277
#if defined(STM32F1xx)
12641278
void RTC_StoreDate(void)
12651279
{

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)