@@ -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 */
194200Rtc::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
471485LOG_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 */
513527Rtc::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 */
528542bool 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, ¤t_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 */
0 commit comments