Skip to content

[Enhancement] Implement RTC Seconds Interrupt for STM32F1xx #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

.vscode/arduino.json
.vscode/c_cpp_properties.json
21 changes: 21 additions & 0 deletions src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions src/STM32RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
93 changes: 93 additions & 0 deletions src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,6 +369,7 @@ void RTC_DeInit(void)
HAL_RTC_DeInit(&RtcHandle);
RTCUserCallback = NULL;
callbackUserData = NULL;
RTCSecondsIrqCallback = NULL;
}

/**
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down