Skip to content

Commit b67b33e

Browse files
committed
RTC: improved board mgm, fixed build wrn
1 parent 48ab1c2 commit b67b33e

File tree

10 files changed

+148
-117
lines changed

10 files changed

+148
-117
lines changed

libraries/RTC/RTC.cpp

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,14 @@ int Rtc::getSeconds()
182182
return -1;
183183
}
184184

185+
#ifdef CONFIG_RTC_STM32
186+
185187
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
186-
#define RTC_NODE DT_NODELABEL(rtc)
188+
#define RTC_NODE DT_NODELABEL(rtc)
189+
#else
190+
#define RTC_NODE DT_INVALID_NODE
191+
#warning "RTC node not found in devicetree"
192+
#endif
187193

188194
/**
189195
* @brief Rtc library constructor
@@ -193,7 +199,7 @@ int Rtc::getSeconds()
193199
*/
194200
Rtc::Rtc()
195201
{
196-
rtc_dev = DEVICE_DT_GET(RTC_NODE);
202+
rtc_dev = DEVICE_DT_GET_OR_NULL(RTC_NODE);
197203
if (!rtc_dev) {
198204
printk("RTC device not found in device tree\n");
199205
}
@@ -241,10 +247,10 @@ int Rtc::setTime(int year, int month, int day, int hour, int minute, int second)
241247
.tm_mday = day,
242248
.tm_mon = month - 1,
243249
.tm_year = year - 1900,
244-
.tm_wday = -1, /* unknown */
245-
.tm_yday = -1, /* unknown */
246-
.tm_isdst = -1, /* unknown */
247-
.tm_nsec = 0 /* unknown */
250+
.tm_wday = 0,
251+
.tm_yday = 0,
252+
.tm_isdst = -1, /**< Daylight saving time flag (Unknown = -1) */
253+
.tm_nsec = 0 /* Nanoseconds (Unknown = 0) */
248254
};
249255

250256
return rtc_set_time(rtc_dev, &time);
@@ -395,7 +401,9 @@ bool Rtc::isAlarmPending()
395401
* @param uint16_t id is an event/alarm identifier
396402
* @param void *user_data is a void pointer the user can set when registering the callback
397403
*/
398-
void Rtc::alarmCallbackWrapper(const struct device *dev, uint16_t id, void *user_data)
404+
void Rtc::alarmCallbackWrapper([[maybe_unused]] const struct device *dev,
405+
[[maybe_unused]] uint16_t id,
406+
void *user_data)
399407
{
400408
Rtc *self = static_cast<Rtc *>(user_data);
401409
if (self && self->userAlarmCallback) {
@@ -427,7 +435,8 @@ int Rtc::setUpdateCallback(RtcUpdateCallback cb, void *user_data)
427435
* @param const struct device *dev is the hardware device (not used at this level)
428436
* @param void *user_data is a void pointer the user can set when registering the callback
429437
*/
430-
void Rtc::updateCallbackWrapper(const struct device *dev, void *user_data)
438+
void Rtc::updateCallbackWrapper([[maybe_unused]] const struct device *dev,
439+
void *user_data)
431440
{
432441
Rtc *self = static_cast<Rtc *>(user_data);
433442
if (self && self->userUpdateCallback) {
@@ -465,9 +474,14 @@ int Rtc::getCalibration(int32_t &calibration)
465474
return rtc_get_calibration(rtc_dev, &calibration);
466475
}
467476

468-
#elif DT_NODE_EXISTS(DT_NODELABEL(rtc2))
477+
#else // For non-STM32 platforms, we use the generic counter API to implement RTC functionality
469478

479+
#if DT_NODE_EXISTS(DT_NODELABEL(rtc2))
470480
#define COUNTER_NODE DT_NODELABEL(rtc2)
481+
#else
482+
#warning "RTC node not found in devicetree"
483+
#define COUNTER_NODE DT_INVALID_NODE
484+
#endif
471485
LOG_MODULE_REGISTER(Rtc);
472486

473487
// Utility functions - local scope only
@@ -500,7 +514,7 @@ void Rtc::alarmHandler(const struct device *dev, uint8_t chan_id, uint32_t ticks
500514
if (rtc->user_callback) {
501515
rtc->user_callback(dev, chan_id, ticks, rtc->user_data);
502516
} else {
503-
LOG_WRN("Alarm triggered but no callback registered! Channel: %d, Ticks: %u", chan_id, ticks);
517+
printk("Alarm triggered but no callback registered! Channel: %d, Ticks: %u\n", chan_id, ticks);
504518
}
505519
}
506520

@@ -512,7 +526,7 @@ void Rtc::alarmHandler(const struct device *dev, uint8_t chan_id, uint32_t ticks
512526
*/
513527
Rtc::Rtc()
514528
{
515-
counter_dev = DEVICE_DT_GET(COUNTER_NODE);
529+
counter_dev = DEVICE_DT_GET_OR_NULL(COUNTER_NODE);
516530
timeOffset = 0;
517531
user_callback = nullptr;
518532
user_data = nullptr;
@@ -527,8 +541,12 @@ Rtc::Rtc()
527541
*/
528542
bool Rtc::begin()
529543
{
544+
if (!counter_dev) {
545+
printk("RTC counter device not found in device tree\n");
546+
return false;
547+
}
530548
if (!device_is_ready(counter_dev)) {
531-
LOG_ERR("RTC counter device not ready");
549+
printk("RTC counter device not ready\n");
532550
return false;
533551
}
534552

@@ -559,7 +577,7 @@ int Rtc::setTime(int year, int month, int day, int hour, int minute, int second)
559577

560578
uint32_t freq = counter_get_frequency(counter_dev);
561579
if (freq == 0) {
562-
LOG_ERR("Counter frequency is zero");
580+
printk("Counter frequency is zero\n");
563581
return -1;
564582
}
565583

@@ -618,6 +636,7 @@ int Rtc::setAlarm(int year, int month, int day, int hour, int minute, int second
618636
uint32_t current_ticks;
619637

620638
if (counter_get_value(counter_dev, &current_ticks) != 0) {
639+
printk("Failed to get current counter value\n");
621640
return -1;
622641
}
623642

@@ -641,7 +660,7 @@ int Rtc::setAlarm(int year, int month, int day, int hour, int minute, int second
641660

642661
int ret = counter_set_channel_alarm(counter_dev, 0, &alarm_cfg);
643662
if (ret != 0) {
644-
LOG_WRN("Failed to set alarm: %d", ret);
663+
printk("Failed to set alarm: %d\n", ret);
645664
user_callback = nullptr;
646665
user_data = nullptr;
647666
return ret;
@@ -661,13 +680,69 @@ int Rtc::cancelAlarm()
661680
{
662681
int ret = counter_cancel_channel_alarm(counter_dev, 0);
663682
if (ret != 0) {
664-
LOG_WRN("Failed to cancel alarm: %d", ret);
683+
printk("Failed to cancel alarm: %d\n", ret);
665684
}
666685
user_callback = nullptr;
667686
user_data = nullptr;
668687
return ret;
669688
}
670689

690+
/**
691+
* @brief Retrieves the currently configured alarm time.
692+
* This function is not supported and will return -1 to indicate this.
693+
*
694+
* @return -1 to indicate not supported.
695+
*/
696+
int Rtc::getAlarm([[maybe_unused]] int &year, [[maybe_unused]] int &month, [[maybe_unused]] int &day,
697+
[[maybe_unused]] int &hour, [[maybe_unused]] int &minute, [[maybe_unused]] int &second)
698+
{
699+
return -1;
700+
}
701+
702+
/**
703+
* @brief Checks whether an alarm is currently pending.
704+
* This function is not supported and will return false to indicate this.
705+
*
706+
* @return false to indicate not supported.
707+
*/
708+
bool Rtc::isAlarmPending()
709+
{
710+
return false;
711+
}
712+
713+
/**
714+
* @brief Registers an update callback function.
715+
* This function is not supported and will return -1 to indicate this.
716+
*
717+
* @return -1 to indicate not supported.
718+
*/
719+
int Rtc::setUpdateCallback([[maybe_unused]] RtcUpdateCallback cb, [[maybe_unused]] void *user_data)
720+
{
721+
return -1;
722+
}
723+
724+
/**
725+
* @brief Sets the Rtc calibration value.
726+
* This function is not supported and will return -1 to indicate this.
727+
*
728+
* @return -1 to indicate not supported.
729+
*/
730+
int Rtc::setCalibration([[maybe_unused]] int32_t calibration)
731+
{
732+
return -1;
733+
}
734+
735+
/**
736+
* @brief Retrieves the current Rtc calibration value.
737+
* This function is not supported and will return -1 to indicate this.
738+
*
739+
* @return -1 to indicate not supported.
740+
*/
741+
int Rtc::getCalibration([[maybe_unused]] int32_t &calibration)
742+
{
743+
return -1;
744+
}
745+
671746
/**
672747
* @brief Converts a date and time to Unix epoch seconds.
673748
*
@@ -756,4 +831,4 @@ void Rtc::epochToDatetime(time_t t, int &year, int &month, int &day, int &hour,
756831
month = m + 1;
757832
day = days + 1;
758833
}
759-
#endif
834+
#endif /* CONFIG_RTC_STM32*/

libraries/RTC/RTC.h

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class Rtc {
217217
*/
218218
int getTime(int &year, int &month, int &day, int &hour, int &minute, int &second);
219219

220-
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
220+
#ifdef CONFIG_RTC_STM32
221221
/**
222222
* @brief Schedules an alarm for a specific time.
223223
*
@@ -240,7 +240,7 @@ class Rtc {
240240
*/
241241
int cancelAlarm();
242242

243-
#elif DT_NODE_EXISTS(DT_NODELABEL(rtc2))
243+
#else
244244
/**
245245
* @brief Sets an RTC alarm for boards using the Zephyr counter driver.
246246
*
@@ -260,12 +260,53 @@ class Rtc {
260260

261261
/**
262262
* @brief Cancels an active alarm.
263+
*
263264
* @return 0 on success, negative error code otherwise.
264265
*/
265266
int cancelAlarm();
267+
268+
/**
269+
* @brief Retrieves the currently configured alarm time.
270+
*
271+
* @return -1 to indicate not supported.
272+
*/
273+
int getAlarm([[maybe_unused]] int &year, [[maybe_unused]] int &month, [[maybe_unused]] int &day,
274+
[[maybe_unused]] int &hour, [[maybe_unused]] int &minute, [[maybe_unused]] int &second);
275+
276+
/**
277+
* @brief Checks whether an alarm is currently pending.
278+
* This function is not supported and will return -1 to indicate this.
279+
*
280+
* @return false to indicate not supported.
281+
*/
282+
bool isAlarmPending();
283+
284+
/**
285+
* @brief Registers an update callback function.
286+
* This function is not supported and will return -1 to indicate this.
287+
*
288+
* @return -1 to indicate not supported.
289+
*/
290+
int setUpdateCallback([[maybe_unused]] RtcUpdateCallback cb, [[maybe_unused]] void *user_data);
291+
292+
/**
293+
* @brief Sets the Rtc calibration value.
294+
* This function is not supported and will return -1 to indicate this.
295+
*
296+
* @return -1 to indicate not supported.
297+
*/
298+
int setCalibration([[maybe_unused]] int32_t calibration);
299+
300+
/**
301+
* @brief Retrieves the current Rtc calibration value.
302+
* This function is not supported and will return -1 to indicate this.
303+
*
304+
* @return -1 to indicate not supported.
305+
*/
306+
int getCalibration([[maybe_unused]] int32_t &calibration);
266307
#endif
267308

268-
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
309+
#ifdef CONFIG_RTC_STM32
269310
/**
270311
* @brief Retrieves the currently configured alarm time.
271312
*
@@ -315,15 +356,15 @@ class Rtc {
315356
#endif
316357

317358
private:
318-
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
359+
#ifdef CONFIG_RTC_STM32
319360
/** @brief Pointer to the Zephyr RTC device. */
320361
const struct device *rtc_dev;
321362

322363
/** @brief Internal static wrapper for alarm callbacks. */
323-
static void alarmCallbackWrapper(const struct device *dev, uint16_t id, void *user_data);
364+
static void alarmCallbackWrapper([[maybe_unused]] const struct device *dev, [[maybe_unused]] uint16_t id, void *user_data);
324365

325366
/** @brief Internal static wrapper for update callbacks. */
326-
static void updateCallbackWrapper(const struct device *dev, void *user_data);
367+
static void updateCallbackWrapper([[maybe_unused]] const struct device *dev, void *user_data);
327368

328369
RtcAlarmCallback userAlarmCallback = nullptr; /**< User-registered alarm callback. */
329370
void *userAlarmCallbackData = nullptr; /**< User data for alarm callback. */
@@ -333,7 +374,7 @@ class Rtc {
333374

334375
uint16_t alarmId = 0; /**< Default alarm identifier. */
335376

336-
#elif DT_NODE_EXISTS(DT_NODELABEL(rtc2))
377+
#else
337378
/** @brief Pointer to the Zephyr counter device used as RTC backend. */
338379
const struct device *counter_dev;
339380

libraries/RTC/examples/AlarmRTC/AlarmRTC.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void setup() {
6868
void loop() {
6969
char printBuffer[30]; // declare a buffer of large enough size for the message we want to display
7070
int y, m, d, h, min, s;
71-
int status = rtc.getTime(y, m, d, h, min, s);
71+
rtc.getTime(y, m, d, h, min, s);
7272
// Because the print() and println() functions do not support formatted output directly, we can use this trick to prepare a buffer with the string we want to show
7373
sprintf(printBuffer, "Time is: %04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, min, s);
7474
Serial.println(printBuffer);

libraries/RTC/examples/CalibrationRTC/CalibrationRTC.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#include "RTC.h"
22
#include <stdio.h>
33

4-
// Doesn't work on the Opta for some reason.
5-
6-
74
Rtc rtc;
85

96
void setup() {
10-
char printBuffer[30];
7+
char printBuffer[64];
118
delay(1000);
129
Serial.begin(115200);
1310
if (!rtc.begin()) {
@@ -17,7 +14,7 @@ void setup() {
1714

1815
int calib = 0;
1916
if (rtc.getCalibration(calib) == 0) {
20-
sprintf(printBuffer, "Current calibration: %d\n", calib);
17+
snprintf(printBuffer, sizeof(printBuffer), "Current calibration: %d\n", calib);
2118
Serial.println(printBuffer);
2219
} else {
2320
Serial.println("Failed to get calibration");
@@ -28,7 +25,7 @@ void setup() {
2825
int32_t new_calib = calib + 1;
2926

3027
if (rtc.setCalibration(new_calib) == 0) {
31-
sprintf(printBuffer, "Calibration updated to: %d\n", new_calib);
28+
snprintf(printBuffer, sizeof(printBuffer), "Calibration updated to: %d\n", new_calib);
3229
Serial.println(printBuffer);
3330
} else {
3431
Serial.println("Failed to set calibration");

0 commit comments

Comments
 (0)