Skip to content

Commit 528fd72

Browse files
committed
chore: add HAL v2 support
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 5c58716 commit 528fd72

9 files changed

Lines changed: 667 additions & 145 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ To know if a time has already been set use:
144144
### Since STM32RTC version higher than 1.3.4
145145
_Second alarm (Alarm B)_
146146
147-
Some STM32 RTC have a second alarm named `RTC_ALARM_B`.
147+
Some STM32 RTC have a second alarm named `ALARM_B`.
148148
It is possible to use it thanks all alarm API with an extra argument:
149149
- `STM32RTC::ALARM_A`
150150
- `STM32RTC::ALARM_B`
@@ -158,7 +158,8 @@ It is possible to use it thanks all alarm API with an extra argument:
158158

159159
### Since STM32RTC version higher than 1.3.7
160160
_Get the RTC handle_
161-
161+
> [!NOTE]
162+
> Only available for the STM32 HAL driver version 1
162163
* **`RTC_HandleTypeDef *RTC_GetHandle(void)`**
163164

164165
_binary and mix modes_

examples/RTCReset/RTCReset.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef struct {
3737
} cb_data_t;
3838

3939
static cb_data_t atime = { 2222, true };
40-
#ifdef RTC_ALARM_B
40+
#if defined(ALARM_B_AVAILABLE)
4141
static cb_data_t btime = { 3333, false };
4242
#endif
4343
static byte seconds = 0;
@@ -107,7 +107,7 @@ void setup() {
107107

108108
// In any case attach a callback to the RTC alarm interrupt.
109109
rtc.attachInterrupt(alarmMatch, &atime);
110-
#ifdef RTC_ALARM_B
110+
#ifdef ALARM_B_AVAILABLE
111111
rtc.attachInterrupt(alarmMatch, &btime, STM32RTC::ALARM_B);
112112
#endif
113113
rtc.begin(); // Initialize RTC 24H format
@@ -120,7 +120,7 @@ void setup() {
120120
rtc.setAlarmDay(day);
121121
rtc.setAlarmTime(hours, minutes, seconds + 5, 567);
122122
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
123-
#ifdef RTC_ALARM_B
123+
#ifdef ALARM_B_AVAILABLE
124124
// ALARM_B
125125
rtc.setAlarmDay(day, STM32RTC::ALARM_B);
126126
rtc.setAlarmTime(hours, minutes, seconds + 5, 567, STM32RTC::ALARM_B);
@@ -144,7 +144,7 @@ void setup() {
144144
Serial.printf("Alarm A was enabled and restored\n");
145145
}
146146
}
147-
#ifdef RTC_ALARM_B
147+
#ifdef ALARM_B_AVAILABLE
148148
// ALARM_B
149149
if (rtc.isAlarmEnabled(STM32RTC::ALARM_B)) {
150150
rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_B);
@@ -168,7 +168,7 @@ void setup() {
168168
rtc.setAlarmTime(hours, minutes, seconds + 5, 567);
169169
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
170170
}
171-
#ifdef RTC_ALARM_B
171+
#ifdef ALARM_B_AVAILABLE
172172
bool alarmB = rtc.isAlarmEnabled(STM32RTC::ALARM_B);
173173
Serial.printf("Alarm B enable status: %s\n", (alarmB) ? "True" : "False");
174174
if (!alarmB) {
@@ -187,7 +187,7 @@ void loop() {
187187
Serial.printf("\n%02d/%02d/%02d %02d:%02d:%02d.%03d\n", rtc.getDay(), rtc.getMonth(), rtc.getYear(), hours, minutes, seconds, subSeconds);
188188
// Print current alarm configuration
189189
Serial.printf("Alarm A: %02d %02d:%02d:%02d.%03d\n", rtc.getAlarmDay(), rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds(), rtc.getAlarmSubSeconds());
190-
#ifdef RTC_ALARM_B
190+
#ifdef ALARM_B_AVAILABLE
191191
Serial.printf("Alarm B: %02d %02d:%02d:%02d.%03d\n", rtc.getAlarmDay(STM32RTC::ALARM_B), rtc.getAlarmHours(STM32RTC::ALARM_B), rtc.getAlarmMinutes(STM32RTC::ALARM_B), rtc.getAlarmSeconds(STM32RTC::ALARM_B), rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
192192
#endif
193193
delay(1000);
@@ -227,7 +227,7 @@ void alarmMatch(void* data) {
227227
Serial.printf("\t\t\tAlarm A Match %i\n", ++alarmMatch_counter);
228228
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
229229
}
230-
#ifdef RTC_ALARM_B
230+
#ifdef ALARM_B_AVAILABLE
231231
else {
232232
Serial.printf("\t\t\tAlarm B Match %i\n", ++alarmMatchB_counter);
233233
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms, STM32RTC::ALARM_B);

examples/advancedRTCAlarm/advancedRTCAlarm.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ STM32RTC& rtc = STM32RTC::getInstance();
2525

2626
/* Declare it volatile since it's incremented inside an interrupt */
2727
volatile int alarmMatch_counter = 0;
28-
#ifdef RTC_ALARM_B
28+
#if defined(ALARM_B_AVAILABLE)
2929
volatile int alarmBMatch_counter = 0;
3030
#endif
3131

@@ -64,7 +64,7 @@ void setup()
6464
rtc.setAlarmTime(16, 0, 10, 567);
6565
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
6666

67-
#ifdef RTC_ALARM_B
67+
#if defined(ALARM_B_AVAILABLE)
6868
rtc.attachInterrupt(alarmBMatch, STM32RTC::ALARM_B);
6969
rtc.setAlarmDay(day, STM32RTC::ALARM_B);
7070
rtc.setAlarmTime(16, 0, 11, 567, STM32RTC::ALARM_B);
@@ -110,7 +110,7 @@ void alarmMatch(void *data)
110110
rtc.setAlarmEpoch(epoc + sec, STM32RTC::MATCH_SS, epoc_ms);
111111
}
112112

113-
#ifdef RTC_ALARM_B
113+
#if defined(ALARM_B_AVAILABLE)
114114
void alarmBMatch(void *data)
115115
{
116116
(void)data;

examples/bin_onlyRTCAlarm/bin_onlyRTCAlarm.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void setup()
4949
rtc.enableAlarm(rtc.MATCH_SUBSEC);
5050
Serial.printf("Set Alarm A in 12s (at %d ms)\r\n", rtc.getAlarmSubSeconds());
5151

52-
#ifdef RTC_ALARM_B
52+
#if defined(ALARM_B_AVAILABLE)
5353
/* Program ALARM B in 600ms ms from now (keep timeout < 1000ms) */
5454
timeout = rtc.getSubSeconds() + 600;
5555

@@ -58,7 +58,7 @@ void setup()
5858
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
5959
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 600,
6060
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
61-
#endif /* RTC_ALARM_B */
61+
#endif /* ALARM_B_AVAILABLE */
6262

6363
}
6464

@@ -74,12 +74,12 @@ void alarmAMatch(void *data)
7474
Serial.printf("Alarm A Match at %d ms \r\n", rtc.getSubSeconds());
7575
}
7676

77-
#ifdef RTC_ALARM_B
77+
#if defined(ALARM_B_AVAILABLE)
7878
void alarmBMatch(void *data)
7979
{
8080
UNUSED(data);
8181
rtc.disableAlarm(STM32RTC::ALARM_B); /* Else it will trig again */
8282
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
8383
}
84-
#endif /* RTC_ALARM_B */
84+
#endif /* ALARM_B_AVAILABLE */
8585

examples/mixRTCAlarm/mixRTCAlarm.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void setup()
6464
Serial.printf("Set Alarm A in 12s (at %02d:%02d:%02d)\r\n",
6565
rtc.getAlarmHours(), rtc.getAlarmMinutes(), rtc.getAlarmSeconds());
6666

67-
#ifdef RTC_ALARM_B
67+
#if defined(ALARM_B_AVAILABLE)
6868
/* Program ALARM B in 600ms ms from now (keep timeout < 1000ms) */
6969
timeout = rtc.getSubSeconds() + 600;
7070

@@ -73,7 +73,7 @@ void setup()
7373
rtc.enableAlarm(rtc.MATCH_SUBSEC, STM32RTC::ALARM_B);
7474
Serial.printf("Set Alarm B (in %d ms) at %d ms\r\n", 600,
7575
rtc.getAlarmSubSeconds(STM32RTC::ALARM_B));
76-
#endif /* RTC_ALARM_B */
76+
#endif /* ALARM_B_AVAILABLE */
7777
}
7878

7979
void loop()
@@ -89,11 +89,11 @@ void alarmAMatch(void *data)
8989
rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
9090
}
9191

92-
#ifdef RTC_ALARM_B
92+
#if defined(ALARM_B_AVAILABLE)
9393
void alarmBMatch(void *data)
9494
{
9595
UNUSED(data);
9696
rtc.disableAlarm(STM32RTC::ALARM_B);
9797
Serial.printf("Alarm B Match at %d ms\r\n", rtc.getSubSeconds());
9898
}
99-
#endif /* RTC_ALARM_B */
99+
#endif /* ALARM_B_AVAILABLE */

src/STM32RTC.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void STM32RTC::begin(bool resetTime, Hour_Format format)
9696
_alarmSubSeconds = _subSeconds;
9797
_alarmPeriod = _hoursPeriod;
9898
}
99-
#ifdef RTC_ALARM_B
99+
#if defined(ALARM_B_AVAILABLE)
100100
syncAlarmTime(ALARM_B);
101101
if (!IS_RTC_DATE(_alarmBDay)) {
102102
// Use current time to init alarm members,
@@ -173,13 +173,13 @@ STM32RTC::Binary_Mode STM32RTC::getBinaryMode(void)
173173
*/
174174
void STM32RTC::setBinaryMode(Binary_Mode mode)
175175
{
176-
#if defined(RTC_BINARY_NONE)
176+
#if defined(LL_RTC_BINARY_NONE)
177177
_mode = mode;
178178
#else
179179
#warning "only BCD mode is supported"
180180
UNUSED(mode);
181181
_mode = MODE_BCD;
182-
#endif /* RTC_BINARY_NONE */
182+
#endif /* LL_RTC_BINARY_NONE */
183183
}
184184

185185
/**
@@ -224,7 +224,7 @@ void STM32RTC::setPrediv(uint32_t predivA, uint32_t predivS)
224224
*/
225225
void STM32RTC::enableAlarm(Alarm_Match match, Alarm name)
226226
{
227-
#ifdef RTC_ALARM_B
227+
#if defined(ALARM_B_AVAILABLE)
228228
if (name == ALARM_B) {
229229
_alarmBMatch = match;
230230
} else
@@ -236,7 +236,7 @@ void STM32RTC::enableAlarm(Alarm_Match match, Alarm name)
236236
}
237237
switch (match) {
238238
case MATCH_OFF:
239-
#ifdef RTC_ALARM_B
239+
#if defined(ALARM_B_AVAILABLE)
240240
if (name == ALARM_B) {
241241
RTC_StopAlarm(::ALARM_B);
242242
} else
@@ -247,7 +247,7 @@ void STM32RTC::enableAlarm(Alarm_Match match, Alarm name)
247247
break;
248248
case MATCH_SUBSEC:
249249
/* force _alarmday to 0 to go to the right alarm config in MIX mode */
250-
#ifdef RTC_ALARM_B
250+
#if defined(ALARM_B_AVAILABLE)
251251
if (name == ALARM_B) {
252252
RTC_StartAlarm(::ALARM_B, 0, 0, 0, 0,
253253
_alarmBSubSeconds, (_alarmBPeriod == AM) ? HOUR_AM : HOUR_PM,
@@ -266,7 +266,7 @@ void STM32RTC::enableAlarm(Alarm_Match match, Alarm name)
266266
case MATCH_HHMMSS:
267267
case MATCH_MMSS:
268268
case MATCH_SS:
269-
#ifdef RTC_ALARM_B
269+
#if defined(ALARM_B_AVAILABLE)
270270
if (name == ALARM_B) {
271271
RTC_StartAlarm(::ALARM_B, _alarmBDay, _alarmBHours, _alarmBMinutes, _alarmBSeconds,
272272
_alarmBSubSeconds, (_alarmBPeriod == AM) ? HOUR_AM : HOUR_PM,
@@ -538,7 +538,7 @@ uint32_t STM32RTC::getAlarmSubSeconds(Alarm name)
538538
{
539539
uint32_t alarmSubSeconds = 0;
540540
syncAlarmTime(name);
541-
#ifdef RTC_ALARM_B
541+
#if defined(ALARM_B_AVAILABLE)
542542
if (name == ALARM_B) {
543543
alarmSubSeconds = _alarmBSubSeconds;
544544
} else
@@ -559,7 +559,7 @@ uint8_t STM32RTC::getAlarmSeconds(Alarm name)
559559
{
560560
uint8_t alarmSeconds = 0;
561561
syncAlarmTime(name);
562-
#ifdef RTC_ALARM_B
562+
#if defined(ALARM_B_AVAILABLE)
563563
if (name == ALARM_B) {
564564
alarmSeconds = _alarmBSeconds;
565565
} else
@@ -580,7 +580,7 @@ uint8_t STM32RTC::getAlarmMinutes(Alarm name)
580580
{
581581
uint8_t alarmMinutes = 0;
582582
syncAlarmTime(name);
583-
#ifdef RTC_ALARM_B
583+
#if defined(ALARM_B_AVAILABLE)
584584
if (name == ALARM_B) {
585585
alarmMinutes = _alarmBMinutes;
586586
} else
@@ -614,7 +614,7 @@ uint8_t STM32RTC::getAlarmHours(AM_PM *period, Alarm name)
614614
{
615615
uint8_t alarmHours = 0;
616616
syncAlarmTime(name);
617-
#ifdef RTC_ALARM_B
617+
#if defined(ALARM_B_AVAILABLE)
618618
if (name == ALARM_B) {
619619
if (period != nullptr) {
620620
*period = _alarmBPeriod;
@@ -641,7 +641,7 @@ uint8_t STM32RTC::getAlarmDay(Alarm name)
641641
{
642642
uint8_t alarmDay = 0;
643643
syncAlarmTime(name);
644-
#ifdef RTC_ALARM_B
644+
#if defined(ALARM_B_AVAILABLE)
645645
if (name == ALARM_B) {
646646
alarmDay = _alarmBDay;
647647
} else
@@ -892,11 +892,11 @@ void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year
892892
*/
893893
void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds, Alarm name)
894894
{
895-
#ifndef RTC_ALARM_B
895+
#if !defined(RTC_ALARM_B) && !defined(USE_HALV2_DRIVER)
896896
UNUSED(name);
897897
#endif
898898
if ((_mode == MODE_BIN) || (subSeconds < 1000)) {
899-
#ifdef RTC_ALARM_B
899+
#if defined(ALARM_B_AVAILABLE)
900900
if (name == ALARM_B) {
901901
_alarmBSubSeconds = subSeconds;
902902
} else
@@ -917,7 +917,7 @@ void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds, Alarm name)
917917
void STM32RTC::setAlarmSeconds(uint8_t seconds, Alarm name)
918918
{
919919
if (seconds < 60) {
920-
#ifdef RTC_ALARM_B
920+
#if defined(ALARM_B_AVAILABLE)
921921
if (name == ALARM_B) {
922922
_alarmBSeconds = seconds;
923923
} else
@@ -940,7 +940,7 @@ void STM32RTC::setAlarmSeconds(uint8_t seconds, Alarm name)
940940
void STM32RTC::setAlarmMinutes(uint8_t minutes, Alarm name)
941941
{
942942
if (minutes < 60) {
943-
#ifdef RTC_ALARM_B
943+
#if defined(ALARM_B_AVAILABLE)
944944
if (name == ALARM_B) {
945945
_alarmBMinutes = minutes;
946946
} else
@@ -976,7 +976,7 @@ void STM32RTC::setAlarmHours(uint8_t hours, Alarm name)
976976
void STM32RTC::setAlarmHours(uint8_t hours, AM_PM period, Alarm name)
977977
{
978978
if (hours < 24) {
979-
#ifdef RTC_ALARM_B
979+
#if defined(ALARM_B_AVAILABLE)
980980
if (name == ALARM_B) {
981981
_alarmBHours = hours;
982982
if (_format == HOUR_12) {
@@ -1063,7 +1063,7 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uin
10631063
void STM32RTC::setAlarmDay(uint8_t day, Alarm name)
10641064
{
10651065
if ((day >= 1) && (day <= 31)) {
1066-
#ifdef RTC_ALARM_B
1066+
#if defined(ALARM_B_AVAILABLE)
10671067
if (name == ALARM_B) {
10681068
_alarmBDay = day;
10691069
}
@@ -1193,7 +1193,7 @@ time_t STM32RTC::getAlarmEpoch(uint32_t *subSeconds, Alarm name)
11931193
tm.tm_year = _year + EPOCH_TIME_YEAR_OFF;
11941194
tm.tm_mon = _month - 1;
11951195
syncAlarmTime(name);
1196-
#ifdef RTC_ALARM_B
1196+
#if defined(ALARM_B_AVAILABLE)
11971197
if (name == ALARM_B) {
11981198
tm.tm_mday = _alarmBDay;
11991199
tm.tm_hour = _alarmBHours;
@@ -1274,7 +1274,11 @@ void STM32RTC::setEpoch(time_t ts, uint32_t subSeconds)
12741274
_month = tmp->tm_mon + 1;
12751275
_day = tmp->tm_mday;
12761276
if (tmp->tm_wday == 0) {
1277+
#if defined(USE_HALV2_DRIVER)
1278+
_wday = HAL_RTC_WEEKDAY_SUNDAY;
1279+
#else
12771280
_wday = RTC_WEEKDAY_SUNDAY;
1281+
#endif
12781282
} else {
12791283
_wday = tmp->tm_wday;
12801284
}
@@ -1303,7 +1307,10 @@ void STM32RTC::setY2kEpoch(time_t ts)
13031307
*/
13041308
void STM32RTC::configForLowPower(Source_Clock source)
13051309
{
1306-
#if defined(HAL_PWR_MODULE_ENABLED)
1310+
#if defined(HAL_PWR_MODULE_ENABLED) || (defined(USE_HAL_PWR_MODULE) && (USE_HAL_PWR_MODULE == 1))
1311+
#if defined(USE_HALV2_DRIVER)
1312+
HAL_RCC_LP_RTCAPB_EnableClockInStopMode();
1313+
#else
13071314
#ifdef __HAL_RCC_RTCAPB_CLKAM_ENABLE
13081315
__HAL_RCC_RTCAPB_CLKAM_ENABLE();
13091316
#endif
@@ -1319,6 +1326,7 @@ void STM32RTC::configForLowPower(Source_Clock source)
13191326
#if defined(PWR_WAKEUP_PIN7_HIGH_3)
13201327
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN7_HIGH_3);
13211328
#endif
1329+
#endif /* USE_HALV2_DRIVER */
13221330
setClockSource(source);
13231331
begin();
13241332

@@ -1373,7 +1381,7 @@ void STM32RTC::syncAlarmTime(Alarm name)
13731381
{
13741382
hourAM_PM_t p = HOUR_AM;
13751383
uint8_t match;
1376-
#ifdef RTC_ALARM_B
1384+
#if defined(ALARM_B_AVAILABLE)
13771385
if (name == ALARM_B) {
13781386
RTC_GetAlarm(::ALARM_B, &_alarmBDay, &_alarmBHours, &_alarmBMinutes, &_alarmBSeconds,
13791387
&_alarmBSubSeconds, &p, &match);
@@ -1395,7 +1403,7 @@ void STM32RTC::syncAlarmTime(Alarm name)
13951403
case MATCH_HHMMSS:
13961404
case MATCH_MMSS:
13971405
case MATCH_SS:
1398-
#ifdef RTC_ALARM_B
1406+
#if defined(ALARM_B_AVAILABLE)
13991407
if (name == ALARM_B) {
14001408
_alarmBMatch = static_cast<Alarm_Match>(match);
14011409
} else
@@ -1405,7 +1413,7 @@ void STM32RTC::syncAlarmTime(Alarm name)
14051413
}
14061414
break;
14071415
default:
1408-
#ifdef RTC_ALARM_B
1416+
#if defined(ALARM_B_AVAILABLE)
14091417
if (name == ALARM_B) {
14101418
_alarmBMatch = MATCH_OFF;
14111419
} else

0 commit comments

Comments
 (0)