Skip to content

Commit a1543ea

Browse files
FRASTMfpistm
authored andcommitted
feat: add new API to setAlarmTime with subsecond
Add new API to set alarm Time with subseconds which is a nb of milliseconds. Similar to setAlarmSubSeconds. Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 9e2f974 commit a1543ea

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ _Get the RTC handle_
158158

159159
* **`RTC_HandleTypeDef *RTC_GetHandle(void)`**
160160

161+
_binary and mix modes_
162+
163+
Some STM32 RTC have a binary mode with 32-bit free-running counter
164+
in addition to their BCD mode for calendar (for example stm32wl55).
165+
Combined with BCD this is the MIX mode. Only using the binary counter is the BIN mode.
166+
Three RTC functional modes are available:
167+
- `STM32RTC::MODE_BCD`
168+
- `STM32RTC::MODE_MIX`
169+
- `STM32RTC::MODE_BIN`
170+
171+
* **`Binary_Mode getBinaryMode(void);`**
172+
* **`void setBinaryMode(Binary_Mode mode);`**
173+
174+
175+
Any API using the Subsecond parameter is expressed in milliseconds
176+
whatever the RTC input clock. This parameter is [0..999] in MIX or BCD mode
177+
and [0..0xFFFFFFFF] in BIN mode. In this configuration, time and date registers
178+
are not used by the RTC.
161179

162180
Refer to the Arduino RTC documentation for the other functions
163181
http://arduino.cc/en/Reference/RTC

src/STM32RTC.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ STM32RTC::Binary_Mode STM32RTC::getBinaryMode(void)
154154
*/
155155
void STM32RTC::setBinaryMode(Binary_Mode mode)
156156
{
157+
#if defined(RTC_BINARY_NONE)
157158
_mode = mode;
159+
#else
160+
#warning "only BCD mode is supported"
161+
UNUSED(mode);
162+
_mode = MODE_BCD;
163+
#endif /* RTC_BINARY_NONE */
158164
}
159165

160166
/**
@@ -340,7 +346,7 @@ void STM32RTC::standbyMode(void)
340346

341347
/**
342348
* @brief get RTC subseconds.
343-
* @retval return the current subseconds from the RTC.
349+
* @retval return the current milliseconds from the RTC.
344350
*/
345351
uint32_t STM32RTC::getSubSeconds(void)
346352
{
@@ -388,7 +394,7 @@ uint8_t STM32RTC::getHours(AM_PM *period)
388394
* @param hours: pointer to the current hours
389395
* @param minutes: pointer to the current minutes
390396
* @param seconds: pointer to the current seconds
391-
* @param subSeconds: pointer to the current subSeconds
397+
* @param subSeconds: pointer to the current subSeconds (in milliseconds)
392398
* @param period: optional (default: nullptr)
393399
* pointer to the current hour period set in the RTC: AM or PM
394400
* @retval none
@@ -835,20 +841,30 @@ void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year
835841

836842
/**
837843
* @brief set RTC alarm subseconds.
838-
* @param subseconds: 0-999 (in ms)
844+
* @param subseconds: 0-999 (in ms) or 32bit nb of milliseconds in BIN mode
839845
* @param name: optional (default: ALARM_A)
840846
* ALARM_A or ALARM_B if exists
841847
* @retval none
842848
*/
843849
void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds, Alarm name)
844850
{
845-
if (subSeconds < 1000) {
851+
#ifndef RTC_ALARM_B
852+
UNUSED(name);
853+
#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) {
846864
#ifdef RTC_ALARM_B
847865
if (name == ALARM_B) {
848866
_alarmBSubSeconds = subSeconds;
849867
} else
850-
#else
851-
UNUSED(name);
852868
#endif
853869
{
854870
_alarmSubSeconds = subSeconds;
@@ -931,7 +947,7 @@ void STM32RTC::setAlarmHours(uint8_t hours, AM_PM period, Alarm name)
931947
if (_format == HOUR_12) {
932948
_alarmBPeriod = period;
933949
}
934-
}
950+
} else
935951
#else
936952
UNUSED(name);
937953
#endif
@@ -944,6 +960,18 @@ void STM32RTC::setAlarmHours(uint8_t hours, AM_PM period, Alarm name)
944960
}
945961
}
946962

963+
964+
/**
965+
* @brief set RTC alarm time.
966+
* @param subSeconds: 0-999 ms or 32bit nb of milliseconds in BIN mode
967+
* @param name: ALARM_A or ALARM_B if exists
968+
* @retval none
969+
*/
970+
void STM32RTC::setAlarmTime(uint32_t subSeconds, Alarm name)
971+
{
972+
setAlarmTime(0, 0, 0, subSeconds, AM, name);
973+
}
974+
947975
/**
948976
* @brief set RTC alarm time.
949977
* @param hours: 0-23
@@ -962,7 +990,7 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Ala
962990
* @param hours: 0-23
963991
* @param minutes: 0-59
964992
* @param seconds: 0-59
965-
* @param subSeconds: 0-999
993+
* @param subSeconds: 0-999 ms or 32bit nb of milliseconds in BIN mode
966994
* @param name: ALARM_A or ALARM_B if exists
967995
* @retval none
968996
*/
@@ -973,10 +1001,10 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uin
9731001

9741002
/**
9751003
* @brief set RTC alarm time.
976-
* @param hours: 0-23
977-
* @param minutes: 0-59
978-
* @param seconds: 0-59
979-
* @param subSeconds: 0-999 (optional)
1004+
* @param hours: 0-23 (not used in BIN mode)
1005+
* @param minutes: 0-59 (not used in BIN mode)
1006+
* @param seconds: 0-59 (not used in BIN mode)
1007+
* @param subSeconds: 0-999 ms (optional) or 32bit nb of milliseconds in BIN mode
9801008
* @param period: hour format AM or PM (optional)
9811009
* @param name: optional (default: ALARM_A)
9821010
* ALARM_A or ALARM_B if exists

src/STM32RTC.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class STM32RTC {
8686
};
8787

8888
enum Binary_Mode : uint8_t {
89-
MODE_BCD = MODE_BINARY_NONE, /* not used */
89+
MODE_BCD = MODE_BINARY_NONE,
9090
MODE_BIN = MODE_BINARY_ONLY,
9191
MODE_MIX = MODE_BINARY_MIX
9292
};
@@ -208,6 +208,7 @@ class STM32RTC {
208208
void setAlarmMinutes(uint8_t minutes, Alarm name = ALARM_A);
209209
void setAlarmHours(uint8_t hours, Alarm name);
210210
void setAlarmHours(uint8_t hours, AM_PM period = AM, Alarm name = ALARM_A);
211+
void setAlarmTime(uint32_t subSeconds, Alarm name = ALARM_A);
211212
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Alarm name);
212213
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Alarm name);
213214
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds = 0, AM_PM period = AM, Alarm name = ALARM_A);

0 commit comments

Comments
 (0)