Skip to content

Commit 48ab1c2

Browse files
committed
RTC: using DTS node instead of board define, minor fixes
1 parent 102cd29 commit 48ab1c2

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

libraries/RTC/RTC.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Rtc::~Rtc(){}
99

1010
bool Rtc::setYear(int year)
1111
{
12+
if (year < 1970 || year > 2100) {
13+
return false;
14+
}
1215
int currentYear, month, day, hour, minute, second;
1316
int retVal = getTime(currentYear, month, day, hour, minute, second);
1417
if(retVal == 0)
@@ -25,6 +28,9 @@ bool Rtc::setYear(int year)
2528

2629
bool Rtc::setMonthOfYear(int month)
2730
{
31+
if (month < 1 || month > 12) {
32+
return false;
33+
}
2834
int year, currentMonth, day, hour, minute, second;
2935
int retVal = getTime(year, currentMonth, day, hour, minute, second);
3036
if(retVal == 0)
@@ -40,6 +46,9 @@ bool Rtc::setMonthOfYear(int month)
4046

4147
bool Rtc::setDayOfMonth(int day)
4248
{
49+
if (day < 1 || day > 31) {
50+
return false;
51+
}
4352
int year, month, currentDay, hour, minute, second;
4453
int retVal = getTime(year, month, currentDay, hour, minute, second);
4554
if(retVal == 0)
@@ -55,6 +64,9 @@ bool Rtc::setDayOfMonth(int day)
5564

5665
bool Rtc::setHour(int hour)
5766
{
67+
if (hour < 0 || hour > 23) {
68+
return false;
69+
}
5870
int year, month, day, currentHour, minute, second;
5971
int retVal = getTime(year, month, day, currentHour, minute, second);
6072
if(retVal == 0)
@@ -70,6 +82,9 @@ bool Rtc::setHour(int hour)
7082

7183
bool Rtc::setMinute(int minute)
7284
{
85+
if (minute < 0 || minute > 59) {
86+
return false;
87+
}
7388
int year, month, day, hour, currentMinute, second;
7489
int retVal = getTime(year, month, day, hour, currentMinute, second);
7590
if(retVal == 0)
@@ -85,6 +100,9 @@ bool Rtc::setMinute(int minute)
85100

86101
bool Rtc::setSecond(int second)
87102
{
103+
if (second < 0 || second > 59) {
104+
return false;
105+
}
88106
int year, month, day, hour, minute, currentSecond;
89107
int retVal = getTime(year, month, day, hour, minute, currentSecond);
90108
if(retVal == 0)
@@ -164,7 +182,7 @@ int Rtc::getSeconds()
164182
return -1;
165183
}
166184

167-
#if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA)
185+
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
168186
#define RTC_NODE DT_NODELABEL(rtc)
169187

170188
/**
@@ -176,6 +194,9 @@ int Rtc::getSeconds()
176194
Rtc::Rtc()
177195
{
178196
rtc_dev = DEVICE_DT_GET(RTC_NODE);
197+
if (!rtc_dev) {
198+
printk("RTC device not found in device tree\n");
199+
}
179200
}
180201

181202
/**
@@ -366,7 +387,7 @@ bool Rtc::isAlarmPending()
366387
/**
367388
* @brief Rtc callback function
368389
*
369-
* This alarm callback wrapper is needed to make the connection between C-style low level driver function callabacks
390+
* This alarm callback wrapper is needed to make the connection between C-style low level driver function callbacks
370391
* which need to be static and the objects derived from the RTC class;
371392
* You do not need to call this directly in the app
372393
*
@@ -444,11 +465,23 @@ int Rtc::getCalibration(int32_t &calibration)
444465
return rtc_get_calibration(rtc_dev, &calibration);
445466
}
446467

447-
#elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME)
468+
#elif DT_NODE_EXISTS(DT_NODELABEL(rtc2))
448469

449470
#define COUNTER_NODE DT_NODELABEL(rtc2)
450471
LOG_MODULE_REGISTER(Rtc);
451472

473+
// Utility functions - local scope only
474+
namespace {
475+
static const int days_in_month[] = {
476+
31, 28, 31, 30, 31, 30,
477+
31, 31, 30, 31, 30, 31
478+
};
479+
480+
static bool is_leap(int year) {
481+
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
482+
}
483+
} // namespace
484+
452485
/**
453486
* @brief Static alarm handler for the RTC driver.
454487
*
@@ -526,6 +559,7 @@ int Rtc::setTime(int year, int month, int day, int hour, int minute, int second)
526559

527560
uint32_t freq = counter_get_frequency(counter_dev);
528561
if (freq == 0) {
562+
LOG_ERR("Counter frequency is zero");
529563
return -1;
530564
}
531565

@@ -608,6 +642,8 @@ int Rtc::setAlarm(int year, int month, int day, int hour, int minute, int second
608642
int ret = counter_set_channel_alarm(counter_dev, 0, &alarm_cfg);
609643
if (ret != 0) {
610644
LOG_WRN("Failed to set alarm: %d", ret);
645+
user_callback = nullptr;
646+
user_data = nullptr;
611647
return ret;
612648
}
613649

@@ -619,22 +655,17 @@ int Rtc::setAlarm(int year, int month, int day, int hour, int minute, int second
619655
*
620656
* This function stops any active alarm and clears the registered callback
621657
* and user data.
658+
* @return 0 on success, negative error code otherwise.
622659
*/
623-
void Rtc::cancelAlarm()
660+
int Rtc::cancelAlarm()
624661
{
625-
counter_cancel_channel_alarm(counter_dev, 0);
662+
int ret = counter_cancel_channel_alarm(counter_dev, 0);
663+
if (ret != 0) {
664+
LOG_WRN("Failed to cancel alarm: %d", ret);
665+
}
626666
user_callback = nullptr;
627667
user_data = nullptr;
628-
}
629-
630-
// Utility functions
631-
static const int days_in_month[] = {
632-
31, 28, 31, 30, 31, 30,
633-
31, 31, 30, 31, 30, 31
634-
};
635-
636-
static bool is_leap(int year) {
637-
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
668+
return ret;
638669
}
639670

640671
/**

libraries/RTC/RTC.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <zephyr/kernel.h>
1818
#include <zephyr/device.h>
19+
#include <zephyr/devicetree.h>
1920
#include <zephyr/drivers/counter.h>
2021
#include <zephyr/drivers/rtc.h>
2122
#include <time.h>
@@ -216,7 +217,7 @@ class Rtc {
216217
*/
217218
int getTime(int &year, int &month, int &day, int &hour, int &minute, int &second);
218219

219-
#if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA)
220+
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
220221
/**
221222
* @brief Schedules an alarm for a specific time.
222223
*
@@ -239,7 +240,7 @@ class Rtc {
239240
*/
240241
int cancelAlarm();
241242

242-
#elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME)
243+
#elif DT_NODE_EXISTS(DT_NODELABEL(rtc2))
243244
/**
244245
* @brief Sets an RTC alarm for boards using the Zephyr counter driver.
245246
*
@@ -259,11 +260,12 @@ class Rtc {
259260

260261
/**
261262
* @brief Cancels an active alarm.
263+
* @return 0 on success, negative error code otherwise.
262264
*/
263-
void cancelAlarm();
265+
int cancelAlarm();
264266
#endif
265267

266-
#if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA)
268+
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
267269
/**
268270
* @brief Retrieves the currently configured alarm time.
269271
*
@@ -313,7 +315,7 @@ class Rtc {
313315
#endif
314316

315317
private:
316-
#if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA)
318+
#if DT_NODE_EXISTS(DT_NODELABEL(rtc))
317319
/** @brief Pointer to the Zephyr RTC device. */
318320
const struct device *rtc_dev;
319321

@@ -331,7 +333,7 @@ class Rtc {
331333

332334
uint16_t alarmId = 0; /**< Default alarm identifier. */
333335

334-
#elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME)
336+
#elif DT_NODE_EXISTS(DT_NODELABEL(rtc2))
335337
/** @brief Pointer to the Zephyr counter device used as RTC backend. */
336338
const struct device *counter_dev;
337339

0 commit comments

Comments
 (0)