From 5fab2a1c6bd030f2b7d41cf7956bc3fdb31b60ad Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 24 Sep 2020 18:34:57 +1200 Subject: [PATCH 01/12] RTC Seconds Interrupt Implement built-in RTC Seconds Interrupt --- src/STM32RTC.cpp | 20 ++++++++++++++++++++ src/STM32RTC.h | 3 +++ src/rtc.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ src/rtc.h | 3 +++ 4 files changed, 75 insertions(+) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index b95a6ea..4659f9f 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -220,6 +220,26 @@ void STM32RTC::detachInterrupt(void) detachAlarmCallback(); } + +/** + * @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(); +} + // Kept for compatibility. Use STM32LowPower library. void STM32RTC::standbyMode(void) { diff --git a/src/STM32RTC.h b/src/STM32RTC.h index 0fd0cc3..0ff3888 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -124,6 +124,9 @@ class STM32RTC { void attachInterrupt(voidFuncPtr callback, void *data = nullptr); void detachInterrupt(void); + + void attachSecondsInterrupt(voidFuncPtr callback); + void detachSecondsInterrupt(void); // Kept for compatibility: use STM32LowPower library. void standbyMode(); diff --git a/src/rtc.c b/src/rtc.c index 8a2ae3b..158263d 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,53 @@ 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; + HAL_RTCEx_SetSecond_IT(&RtcHandle); + HAL_NVIC_EnableIRQ(RTC_IRQn); +} + +/** + * @brief Detach Seconds interrupt callback. + * @param None + * @retval None + */ +void detachSecondsIrqCallback(void) +{ + HAL_RTCEx_DeactivateSecond(&RtcHandle); + RTCSecondsIrqCallback = NULL; +} + +/** + * @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); +} + #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 From 5eb0784e090cd45d625c4c3597cd46c47b27a19f Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 24 Sep 2020 18:47:44 +1200 Subject: [PATCH 02/12] stm32f1xx only Only enable Seconds interrupt on stm32f1xx --- src/STM32RTC.cpp | 3 ++- src/STM32RTC.h | 4 +++- src/rtc.c | 2 ++ src/rtc.h | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 4659f9f..98da3c0 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -220,7 +220,7 @@ void STM32RTC::detachInterrupt(void) detachAlarmCallback(); } - +#if defined(STM32F1xx) /** * @brief attach a callback to the RTC Seconds interrupt. * @param callback: pointer to the callback @@ -239,6 +239,7 @@ 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 0ff3888..db5c45c 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -124,9 +124,11 @@ class STM32RTC { void attachInterrupt(voidFuncPtr callback, void *data = nullptr); void detachInterrupt(void); - + +#if defined(STM32F1xx) 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 158263d..5dbd875 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -713,6 +713,7 @@ void RTC_Alarm_IRQHandler(void) HAL_RTC_AlarmIRQHandler(&RtcHandle); } +#if defined(STM32F1xx) /** * @brief Attach Seconds interrupt callback. * @param func: pointer to the callback @@ -759,6 +760,7 @@ void RTC_IRQHandler(void) { HAL_RTCEx_RTCIRQHandler(&RtcHandle); } +#endif #ifdef __cplusplus } diff --git a/src/rtc.h b/src/rtc.h index acb3339..348984d 100644 --- a/src/rtc.h +++ b/src/rtc.h @@ -165,8 +165,10 @@ void RTC_GetAlarm(uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *secon void attachAlarmCallback(voidCallbackPtr func, void *data); void detachAlarmCallback(void); +#if defined(STM32F1xx) void attachSecondsIrqCallback(voidCallbackPtr func); void detachSecondsIrqCallback(); +#endif #ifdef __cplusplus } From 8603c4894b9b55732e1777c05ec9439caac182f6 Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 24 Sep 2020 20:08:18 +1200 Subject: [PATCH 03/12] Version check Only available on core version > 1.9.0 --- src/STM32RTC.cpp | 2 +- src/STM32RTC.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 98da3c0..898a322 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -220,7 +220,7 @@ void STM32RTC::detachInterrupt(void) detachAlarmCallback(); } -#if defined(STM32F1xx) +#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 diff --git a/src/STM32RTC.h b/src/STM32RTC.h index db5c45c..d5e7d12 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -125,7 +125,7 @@ class STM32RTC { void attachInterrupt(voidFuncPtr callback, void *data = nullptr); void detachInterrupt(void); -#if defined(STM32F1xx) +#if defined(STM32F1xx) && defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) void attachSecondsInterrupt(voidFuncPtr callback); void detachSecondsInterrupt(void); #endif From 18a17db65774af0eb3e752552a57e9c938108661 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 1 Nov 2020 10:24:01 +1300 Subject: [PATCH 04/12] Other variants Disable check for STM32F1XX variants --- src/rtc.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/rtc.h | 4 ++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/rtc.c b/src/rtc.c index 5dbd875..5a063ac 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -713,7 +713,6 @@ void RTC_Alarm_IRQHandler(void) HAL_RTC_AlarmIRQHandler(&RtcHandle); } -#if defined(STM32F1xx) /** * @brief Attach Seconds interrupt callback. * @param func: pointer to the callback @@ -722,8 +721,36 @@ void RTC_Alarm_IRQHandler(void) 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); +#endif /* USE_TIMEOUT */ + } + + /* 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 } /** @@ -733,10 +760,15 @@ void attachSecondsIrqCallback(voidCallbackPtr func) */ 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 @@ -760,6 +792,20 @@ 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 diff --git a/src/rtc.h b/src/rtc.h index 348984d..9cf8aca 100644 --- a/src/rtc.h +++ b/src/rtc.h @@ -86,6 +86,8 @@ typedef void(*voidCallbackPtr)(void *); #endif + + #define HSE_RTC_MAX 1000000U #if !defined(STM32F1xx) @@ -165,10 +167,8 @@ void RTC_GetAlarm(uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *secon void attachAlarmCallback(voidCallbackPtr func, void *data); void detachAlarmCallback(void); -#if defined(STM32F1xx) void attachSecondsIrqCallback(voidCallbackPtr func); void detachSecondsIrqCallback(); -#endif #ifdef __cplusplus } From f949a4b21f43c42058bc35f9f5028e03e8e07c40 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 1 Nov 2020 11:04:49 +1300 Subject: [PATCH 05/12] Update rtc.c --- src/rtc.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/rtc.c b/src/rtc.c index 5a063ac..daf8d50 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -729,28 +729,26 @@ void attachSecondsIrqCallback(voidCallbackPtr func) /* 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); -#endif /* USE_TIMEOUT */ - } - + //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); + //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); + //LL_RTC_WAKEUP_Enable(RTC); + //LL_RTC_EnableIT_WUT(RTC); + //LL_RTC_ClearFlag_WUT(RTC); /* Enable RTC registers write protection */ - LL_RTC_EnableWriteProtection(RTC); + //LL_RTC_EnableWriteProtection(RTC); /* LSI_CLOCK, HSI_CLOCK, LSE_CLOCK, HSE_CLOCK*/ -#endif +#endif /* USE_TIMEOUT */ } /** From afe6ae6d909f98f973383aadadc193fca70c711e Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 25 Jan 2021 21:51:47 +1300 Subject: [PATCH 06/12] Update src/rtc.c Co-authored-by: Frederic Pillon --- src/rtc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rtc.c b/src/rtc.c index daf8d50..d1a99da 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -732,7 +732,7 @@ void attachSecondsIrqCallback(voidCallbackPtr func) //LL_RTC_DisableWriteProtection(RTC); /* Setting the Wakeup time to 1 s - If LL_RTC_WAKEUPCLOCK_CKSPRE is selected, the frequency is 1Hz, + 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); From c46f377d8d32a578e3a88b2613d1c0cdded6abcd Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 25 Jan 2021 21:51:56 +1300 Subject: [PATCH 07/12] Update src/rtc.c Co-authored-by: Frederic Pillon --- src/rtc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rtc.c b/src/rtc.c index d1a99da..d113ff4 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -736,7 +736,6 @@ void attachSecondsIrqCallback(voidCallbackPtr func) 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); From c09f19e4395c34c7a4eb5c94cc0a29c9d0707892 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 25 Jan 2021 21:52:04 +1300 Subject: [PATCH 08/12] Update src/rtc.c Co-authored-by: Frederic Pillon --- src/rtc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rtc.c b/src/rtc.c index d113ff4..a289b3d 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -740,7 +740,6 @@ void attachSecondsIrqCallback(voidCallbackPtr func) //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, From d6b610a7b6d23d9e691229a1e5ea65a1ddbbff8b Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 25 Jan 2021 21:52:12 +1300 Subject: [PATCH 09/12] Update src/rtc.c Co-authored-by: Frederic Pillon --- src/rtc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rtc.c b/src/rtc.c index a289b3d..9dd8105 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -742,10 +742,10 @@ void attachSecondsIrqCallback(voidCallbackPtr func) //LL_RTC_ClearFlag_WUT(RTC); /* Enable RTC registers write protection */ //LL_RTC_EnableWriteProtection(RTC); - /* LSI_CLOCK, - HSI_CLOCK, - LSE_CLOCK, - HSE_CLOCK*/ + /* LSI_CLOCK, + HSI_CLOCK, + LSE_CLOCK, + HSE_CLOCK*/ #endif /* USE_TIMEOUT */ } From 1abca475a5493ceec7f56a0246945e588917adc0 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 25 Jan 2021 21:52:23 +1300 Subject: [PATCH 10/12] Update src/rtc.c Co-authored-by: Frederic Pillon --- src/rtc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rtc.c b/src/rtc.c index 9dd8105..184e152 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -794,7 +794,7 @@ void RTC_IRQHandler(void) * @param htim Timer handle * @retval None */ -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { UNUSED(htim); From bde51598f38c1c14fbe07e01ac1ab1fe5dd0c70d Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 25 Jan 2021 21:52:30 +1300 Subject: [PATCH 11/12] Update src/rtc.h Co-authored-by: Frederic Pillon --- src/rtc.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/rtc.h b/src/rtc.h index 9cf8aca..acb3339 100644 --- a/src/rtc.h +++ b/src/rtc.h @@ -86,8 +86,6 @@ typedef void(*voidCallbackPtr)(void *); #endif - - #define HSE_RTC_MAX 1000000U #if !defined(STM32F1xx) From a47cdbab2c7ee7f214ebfbf06b2bcbe7c0658317 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 24 Feb 2021 12:20:21 +1300 Subject: [PATCH 12/12] Create .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore 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