diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d398f9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +.vscode/arduino.json +.vscode/c_cpp_properties.json diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index b95a6ea..898a322 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -220,6 +220,27 @@ void STM32RTC::detachInterrupt(void) detachAlarmCallback(); } +#if defined(STM32F1xx) && defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) +/** + * @brief attach a callback to the RTC Seconds interrupt. + * @param callback: pointer to the callback + * @retval None + */ +void STM32RTC::attachSecondsInterrupt(voidFuncPtr callback) +{ + attachSecondsIrqCallback(callback); +} + +/** + * @brief detach the RTC Seconds callback. + * @retval None + */ +void STM32RTC::detachSecondsInterrupt(void) +{ + detachSecondsIrqCallback(); +} +#endif + // Kept for compatibility. Use STM32LowPower library. void STM32RTC::standbyMode(void) { diff --git a/src/STM32RTC.h b/src/STM32RTC.h index 0fd0cc3..d5e7d12 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -125,6 +125,11 @@ class STM32RTC { void attachInterrupt(voidFuncPtr callback, void *data = nullptr); void detachInterrupt(void); +#if defined(STM32F1xx) && defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) + void attachSecondsInterrupt(voidFuncPtr callback); + void detachSecondsInterrupt(void); +#endif + // Kept for compatibility: use STM32LowPower library. void standbyMode(); diff --git a/src/rtc.c b/src/rtc.c index 8a2ae3b..184e152 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -58,6 +58,7 @@ extern "C" { static RTC_HandleTypeDef RtcHandle = {0}; static voidCallbackPtr RTCUserCallback = NULL; static void *callbackUserData = NULL; +static voidCallbackPtr RTCSecondsIrqCallback = NULL; static sourceClock_t clkSrc = LSI_CLOCK; static uint8_t HSEDiv = 0; @@ -368,6 +369,7 @@ void RTC_DeInit(void) HAL_RTC_DeInit(&RtcHandle); RTCUserCallback = NULL; callbackUserData = NULL; + RTCSecondsIrqCallback = NULL; } /** @@ -711,6 +713,97 @@ void RTC_Alarm_IRQHandler(void) HAL_RTC_AlarmIRQHandler(&RtcHandle); } +/** + * @brief Attach Seconds interrupt callback. + * @param func: pointer to the callback + * @retval None + */ +void attachSecondsIrqCallback(voidCallbackPtr func) +{ + RTCSecondsIrqCallback = func; +#if defined(STM32F1xx) + /* The STM32F1xx series has a built in seconds interrupt so we can just enable that */ + HAL_RTCEx_SetSecond_IT(&RtcHandle); + HAL_NVIC_EnableIRQ(RTC_IRQn); +#else + /* All other variants don't have a Seconds interrupt, we will make use of the Periodic Wake Up Timer */ + /* Disable writeprotection so we can setup the periodic wakeup timer */ + //__HAL_RTC_WRITEPROTECTION_DISABLE(&RtcHandle); + //LL_RTC_DisableWriteProtection(RTC); + + /* Setting the Wakeup time to 1 s + If LL_RTC_WAKEUPCLOCK_CKSPRE is selected, the frequency is 1Hz, + this allows to get a wakeup time equal to 1 s if the counter is 0x0 */ + //LL_RTC_WAKEUP_SetAutoReload(RTC, 0); + //LL_RTC_WAKEUP_SetClock(RTC, LL_RTC_WAKEUPCLOCK_CKSPRE); + /* Enable wake up counter and wake up interrupt */ + //LL_RTC_WAKEUP_Enable(RTC); + //LL_RTC_EnableIT_WUT(RTC); + //LL_RTC_ClearFlag_WUT(RTC); + /* Enable RTC registers write protection */ + //LL_RTC_EnableWriteProtection(RTC); + /* LSI_CLOCK, + HSI_CLOCK, + LSE_CLOCK, + HSE_CLOCK*/ +#endif /* USE_TIMEOUT */ +} + +/** + * @brief Detach Seconds interrupt callback. + * @param None + * @retval None + */ +void detachSecondsIrqCallback(void) +{ +#if defined(STM32F1xx) + HAL_RTCEx_DeactivateSecond(&RtcHandle); +#else + +#endif + RTCSecondsIrqCallback = NULL; +} + +#if defined(STM32F1xx) +/** + * @brief Seconds interrupt callback. + * @param hrtc RTC handle + * @retval None + */ +void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc) +{ + UNUSED(hrtc); + + if (RTCSecondsIrqCallback != NULL) { + RTCSecondsIrqCallback(NULL); + } +} + +/** + * @brief This function handles RTC Seconds interrupt request. + * @param None + * @retval None + */ +void RTC_IRQHandler(void) +{ + HAL_RTCEx_RTCIRQHandler(&RtcHandle); +} +#else +/** + * @brief Periodic wakeup timer callback. + * @param htim Timer handle + * @retval None + */ +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) +{ + UNUSED(htim); + + if (RTCSecondsIrqCallback != NULL) { + RTCSecondsIrqCallback(NULL); + } +} +#endif + #ifdef __cplusplus } #endif diff --git a/src/rtc.h b/src/rtc.h index 4457a8b..acb3339 100644 --- a/src/rtc.h +++ b/src/rtc.h @@ -165,6 +165,9 @@ void RTC_GetAlarm(uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *secon void attachAlarmCallback(voidCallbackPtr func, void *data); void detachAlarmCallback(void); +void attachSecondsIrqCallback(voidCallbackPtr func); +void detachSecondsIrqCallback(); + #ifdef __cplusplus } #endif