Skip to content

Commit

Permalink
RTC Time and Alarm configuration in BIN mode (BINARY_ONLY)
Browse files Browse the repository at this point in the history
In BIN ONLY mode, the Time and Date registers are 0.
The Subsecond param in expressed in a nb of milliseconds
on a 32-bit value.

Signed-off-by: Francois Ramu <[email protected]>
  • Loading branch information
FRASTM committed Sep 21, 2023
1 parent 0e17344 commit 3b9a253
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
27 changes: 19 additions & 8 deletions src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void STM32RTC::standbyMode(void)

/**
* @brief get RTC subseconds.
* @retval return the current subseconds from the RTC.
* @retval return the current milliseconds from the RTC.
*/
uint32_t STM32RTC::getSubSeconds(void)
{
Expand Down Expand Up @@ -388,7 +388,7 @@ uint8_t STM32RTC::getHours(AM_PM *period)
* @param hours: pointer to the current hours
* @param minutes: pointer to the current minutes
* @param seconds: pointer to the current seconds
* @param subSeconds: pointer to the current subSeconds
* @param subSeconds: pointer to the current subSeconds 'in milliseconds)
* @param period: optional (default: nullptr)
* pointer to the current hour period set in the RTC: AM or PM
* @retval none
Expand Down Expand Up @@ -835,14 +835,25 @@ void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year

/**
* @brief set RTC alarm subseconds.
* @param subseconds: 0-999 (in ms)
* @param subseconds: 0-999 (in ms) or 32bit value in BIN mode
* @param name: optional (default: ALARM_A)
* ALARM_A or ALARM_B if exists
* @retval none
*/
void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds, Alarm name)
{
if (subSeconds < 1000) {
if (_mode == MODE_BIN) {
#ifdef RTC_ALARM_B
if (name == ALARM_B) {
_alarmBSubSeconds = subSeconds;
}
#else
UNUSED(name);
#endif
{
_alarmSubSeconds = subSeconds;
}
} else if (subSeconds < 1000) {
#ifdef RTC_ALARM_B
if (name == ALARM_B) {
_alarmBSubSeconds = subSeconds;
Expand Down Expand Up @@ -973,10 +984,10 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uin

/**
* @brief set RTC alarm time.
* @param hours: 0-23
* @param minutes: 0-59
* @param seconds: 0-59
* @param subSeconds: 0-999 (optional)
* @param hours: 0-23 (not used in BIN mode)
* @param minutes: 0-59 (not used in BIN mode)
* @param seconds: 0-59 (not used in BIN mode)
* @param subSeconds: 0-999 ms (optional) or 32bit nb of milliseconds in BIN mode
* @param period: hour format AM or PM (optional)
* @param name: optional (default: ALARM_A)
* ALARM_A or ALARM_B if exists
Expand Down
10 changes: 7 additions & 3 deletions src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,18 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
}
#if defined(RTC_SSR_SS)
if (subSeconds != NULL) {
if ((initMode == MODE_BINARY_MIX) || (initMode == MODE_BINARY_ONLY)) {
if (initMode == MODE_BINARY_MIX) {
/* The subsecond is the free-running downcounter, to be converted in milliseconds */
*subSeconds = (((UINT32_MAX - RTC_TimeStruct.SubSeconds + 1) & UINT32_MAX)
* 1000) / fqce_apre; /* give one more to compensate the 3.9ms uncertainty */
*subSeconds = *subSeconds % 1000; /* nb of milliseconds */
*subSeconds = *subSeconds % 1000; /* nb of milliseconds [0..999] */
/* predivAsync is 0x7F with LSE clock source */
} else if (initMode == MODE_BINARY_ONLY) {
/* The subsecond is the free-running downcounter, to be converted in milliseconds */
*subSeconds = (((UINT32_MAX - RTC_TimeStruct.SubSeconds + 1) & UINT32_MAX)
* 1000) / fqce_apre; /* give one more to compensate the 3.9ms uncertainty */
} else {
/* the subsecond register value is converted in millisec */
/* the subsecond register value is converted in millisec on 32bit*/
*subSeconds = ((predivSync - RTC_TimeStruct.SubSeconds) * 1000) / (predivSync + 1);
}
}
Expand Down

0 comments on commit 3b9a253

Please sign in to comment.