@@ -160,11 +160,24 @@ int ArduinoRTC::getSeconds()
160160#if defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA)
161161#define RTC_NODE DT_NODELABEL (rtc)
162162
163+ /* *
164+ * @brief RTC library constructor
165+ *
166+ * Constructor used with the STM32 microcontroller based boards (OPTA, PORTENTA H7, GIGA R1)
167+ *
168+ */
163169ArduinoRTC::ArduinoRTC()
164170{
165171 rtc_dev = DEVICE_DT_GET (RTC_NODE);
166172}
167173
174+ /* *
175+ * @brief RTC library classic begin() function
176+ *
177+ * Takes care of the underlying STM32 RTC HAL driver initalization
178+ *
179+ * @return bool operation result
180+ */
168181bool ArduinoRTC::begin ()
169182{
170183 if (!device_is_ready (rtc_dev)) {
@@ -178,6 +191,19 @@ bool ArduinoRTC::begin()
178191 return true ;
179192}
180193
194+ /* *
195+ * @brief RTC library setter function
196+ *
197+ * Used to set the time and data with a single function call
198+ *
199+ * @param int year to be set
200+ * @param int month to be set
201+ * @param int day to be set
202+ * @param int hour to be set
203+ * @param int minute to be set
204+ * @param int second to be set
205+ * @return int operation result
206+ */
181207int ArduinoRTC::setTime (int year, int month, int day, int hour, int minute, int second)
182208{
183209 struct rtc_time time = {
@@ -191,6 +217,18 @@ int ArduinoRTC::setTime(int year, int month, int day, int hour, int minute, int
191217 return rtc_set_time (rtc_dev, &time);
192218}
193219
220+ /* *
221+ * @brief RTC library getter
222+ *
223+ * Used to set the time and data with a single function call
224+ *
225+ * @param int reference to currently set year
226+ * @param int reference to currently set month
227+ * @param int reference to currently set day of month
228+ * @param int reference to currently set hour
229+ * @param int reference to currently set minute
230+ * @param int reference to currently set second
231+ */
194232int ArduinoRTC::getTime (int &year, int &month, int &day, int &hour, int &minute, int &second)
195233{
196234 struct rtc_time time;
@@ -207,8 +245,19 @@ int ArduinoRTC::getTime(int &year, int &month, int &day, int &hour, int &minute,
207245 return 0 ;
208246}
209247
210- // ------------------- ALARM API -------------------------
211-
248+ /* *
249+ * @brief RTC library setter
250+ *
251+ * Used to set the alarm time through a single function call
252+ *
253+ * @param int year to be set
254+ * @param int month to be set
255+ * @param int day to be set
256+ * @param int hour to be set
257+ * @param int minute to be set
258+ * @param int second to be set
259+ * @return int operation result
260+ */
212261int ArduinoRTC::setAlarm (int year, int month, int day, int hour, int minute, int second, RTCAlarmCallback cb, void *user_data)
213262{
214263 struct rtc_time alarm_time = {
@@ -240,6 +289,19 @@ int ArduinoRTC::setAlarm(int year, int month, int day, int hour, int minute, int
240289 return rtc_alarm_set_time (rtc_dev, alarmId, mask, &alarm_time);
241290}
242291
292+ /* *
293+ * @brief RTC library getter
294+ *
295+ * Used to get the time and date of the currently set alarm
296+ *
297+ * @param int reference to currently set year
298+ * @param int reference to currently set month
299+ * @param int reference to currently set day of month
300+ * @param int reference to currently set hour
301+ * @param int reference to currently set minute
302+ * @param int reference to currently set second
303+ * @return int operation result
304+ */
243305int ArduinoRTC::getAlarm (int &year, int &month, int &day, int &hour, int &minute, int &second)
244306{
245307 struct rtc_time alarm_time;
@@ -258,21 +320,44 @@ int ArduinoRTC::getAlarm(int &year, int &month, int &day, int &hour, int &minute
258320 return 0 ;
259321}
260322
323+ /* *
324+ * @brief RTC utility function
325+ *
326+ * Used to cancel a previously set alarm
327+ *
328+ * @return int operation result
329+ */
261330int ArduinoRTC::cancelAlarm ()
262331{
263332 // Seting time with value zero cancels it
264333 struct rtc_time dummy = {0 };
265334 return rtc_alarm_set_time (rtc_dev, alarmId, 0 , &dummy);
266335}
267336
337+ /* *
338+ * @brief RTC utility function
339+ *
340+ * Used to check if an alarm is currently set
341+ *
342+ * @return bool operation result, true for alarm pening, false otherwise
343+ */
268344bool ArduinoRTC::isAlarmPending ()
269345{
270346 int ret = rtc_alarm_is_pending (rtc_dev, alarmId);
271347 return ret > 0 ;
272348}
273349
274- // ------------------- CALLBACKS -------------------------
275-
350+ /* *
351+ * @brief RTC callback function
352+ *
353+ * This alarm callback wrapper is needed to make the connection between C-style low level driver function callabacks
354+ * which need to be static and the objects derived from the RTC class;
355+ * You do not need to call this direcly in the app
356+ *
357+ * @param const struct device *dev is the hardware device (not used at this level)
358+ * @param uint16_t id is an event/alarm identifier
359+ * @param void *user_data is a void pointer the user can set when registering the callback
360+ */
276361void ArduinoRTC::alarmCallbackWrapper (const struct device *dev, uint16_t id, void *user_data)
277362{
278363 ArduinoRTC *self = static_cast <ArduinoRTC *>(user_data);
@@ -281,6 +366,14 @@ void ArduinoRTC::alarmCallbackWrapper(const struct device *dev, uint16_t id, voi
281366 }
282367}
283368
369+ /* *
370+ * @brief RTC utility function
371+ *
372+ * This function allows for updating the user callback and the message passed to it
373+ *
374+ * @param RTCUpdateCallback cb is the new callback which will be called in the app when the event fires later on
375+ * @param void *user_data is a void pointer the user can set when registering the callback
376+ */
284377int ArduinoRTC::setUpdateCallback (RTCUpdateCallback cb, void *user_data)
285378{
286379 userUpdateCallback = cb;
@@ -289,6 +382,14 @@ int ArduinoRTC::setUpdateCallback(RTCUpdateCallback cb, void *user_data)
289382 return rtc_update_set_callback (rtc_dev, ArduinoRTC::updateCallbackWrapper, this );
290383}
291384
385+ /* *
386+ * @brief RTC utility function
387+ *
388+ * This function is a wrapper which allows for connecting the low level driver and the registered callback with the instance of the object we use in the application sketch
389+ *
390+ * @param const struct device *dev is the hardware device (not used at this level)
391+ * @param void *user_data is a void pointer the user can set when registering the callback
392+ */
292393void ArduinoRTC::updateCallbackWrapper (const struct device *dev, void *user_data)
293394{
294395 ArduinoRTC *self = static_cast <ArduinoRTC *>(user_data);
@@ -297,26 +398,53 @@ void ArduinoRTC::updateCallbackWrapper(const struct device *dev, void *user_data
297398 }
298399}
299400
300- // ------------------- CALIBRATION -------------------------
301-
401+ /* *
402+ * @brief Sets the RTC calibration value.
403+ *
404+ * This function writes a calibration value to the RTC hardware to adjust
405+ * timing accuracy. A positive or negative calibration factor may be applied,
406+ * depending on whether the clock is running fast or slow.
407+ *
408+ * @param calibration The calibration adjustment value in parts per million (ppm).
409+ * @return 0 if successful, or a negative error code on failure.
410+ */
302411int ArduinoRTC::setCalibration (int32_t calibration)
303412{
304413 return rtc_set_calibration (rtc_dev, calibration);
305414}
306415
416+
417+ /* *
418+ * @brief Retrieves the current RTC calibration value.
419+ *
420+ * This function reads the current calibration adjustment applied to the RTC.
421+ *
422+ * @param calibration Reference to an integer where the current calibration
423+ * value (in parts per million) will be stored.
424+ * @return 0 if successful, or a negative error code on failure.
425+ */
307426int ArduinoRTC::getCalibration (int32_t &calibration)
308427{
309428 return rtc_get_calibration (rtc_dev, &calibration);
310429}
311430
312431#elif defined(ARDUINO_NANO33BLE) || defined(ARDUINO_NICLA_SENSE_ME)
313432
314- // ------------------- NRF Boards --------------------------
315-
316433#define COUNTER_NODE DT_NODELABEL (rtc2)
317434LOG_MODULE_REGISTER(ArduinoRTC);
318435
319- // Static callback handler
436+ /* *
437+ * @brief Static alarm handler for the RTC driver.
438+ *
439+ * This function acts as a static wrapper between the hardware interrupt and
440+ * the user-defined callback. It retrieves the relevant `ArduinoRTC` instance
441+ * from the provided `user_data` and invokes the user’s callback function.
442+ *
443+ * @param dev Pointer to the RTC device structure (hardware driver).
444+ * @param chan_id The alarm channel identifier.
445+ * @param ticks The current tick count at the time of the alarm.
446+ * @param user_data Pointer to user-defined data (the ArduinoRTC instance).
447+ */
320448void ArduinoRTC::alarmHandler (const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)
321449{
322450 ArduinoRTC *rtc = static_cast <ArduinoRTC *>(user_data);
@@ -325,6 +453,12 @@ void ArduinoRTC::alarmHandler(const struct device *dev, uint8_t chan_id, uint32_
325453 }
326454}
327455
456+ /* *
457+ * @brief Constructs an ArduinoRTC object.
458+ *
459+ * Initializes internal variables, sets default offsets, and retrieves
460+ * the hardware RTC counter device.
461+ */
328462ArduinoRTC::ArduinoRTC ()
329463{
330464 counter_dev = DEVICE_DT_GET (COUNTER_NODE);
@@ -333,7 +467,13 @@ ArduinoRTC::ArduinoRTC()
333467 user_data = nullptr ;
334468}
335469
336- // Initialize the RTC
470+ /* *
471+ * @brief Initializes the RTC hardware interface.
472+ *
473+ * This function checks if the RTC device is ready and starts the counter.
474+ *
475+ * @return true if the device is ready and successfully started, false otherwise.
476+ */
337477bool ArduinoRTC::begin ()
338478{
339479 if (!device_is_ready (counter_dev)) {
@@ -344,6 +484,20 @@ bool ArduinoRTC::begin()
344484 return true ;
345485}
346486
487+ /* *
488+ * @brief Sets the current RTC time.
489+ *
490+ * This function converts a date and time into epoch format and adjusts the
491+ * internal time offset based on the current tick counter.
492+ *
493+ * @param year The current year (e.g., 2025).
494+ * @param month The current month (1–12).
495+ * @param day The day of the month (1–31).
496+ * @param hour The hour (0–23).
497+ * @param minute The minute (0–59).
498+ * @param second The second (0–59).
499+ * @return 0 if successful, or a negative error code if frequency or ticks are invalid.
500+ */
347501int ArduinoRTC::setTime (int year, int month, int day, int hour, int minute, int second)
348502{
349503 time_t target = datetimeToEpoch (year, month, day, hour, minute, second);
@@ -360,6 +514,20 @@ int ArduinoRTC::setTime(int year, int month, int day, int hour, int minute, int
360514 return 0 ;
361515}
362516
517+ /* *
518+ * @brief Retrieves the current RTC time.
519+ *
520+ * This function reads the current counter value, applies the time offset,
521+ * and converts the epoch time to a human-readable date and time.
522+ *
523+ * @param year Reference to store the current year.
524+ * @param month Reference to store the current month.
525+ * @param day Reference to store the current day.
526+ * @param hour Reference to store the current hour.
527+ * @param minute Reference to store the current minute.
528+ * @param second Reference to store the current second.
529+ * @return 0 if successful.
530+ */
363531int ArduinoRTC::getTime (int &year, int &month, int &day, int &hour, int &minute, int &second)
364532{
365533 uint32_t ticks;
@@ -372,6 +540,22 @@ int ArduinoRTC::getTime(int &year, int &month, int &day, int &hour, int &minute,
372540 return 0 ;
373541}
374542
543+ /* *
544+ * @brief Sets an RTC alarm.
545+ *
546+ * This function schedules an alarm for a specific date and time. When the
547+ * target time is reached, the registered user callback is invoked.
548+ *
549+ * @param year Target alarm year.
550+ * @param month Target alarm month.
551+ * @param day Target alarm day.
552+ * @param hour Target alarm hour.
553+ * @param minute Target alarm minute.
554+ * @param second Target alarm second.
555+ * @param callback Function pointer to the user-defined callback.
556+ * @param cb_user_data Pointer to user data passed to the callback when triggered.
557+ * @return 0 if successful, or a negative error code if the target time is invalid.
558+ */
375559int ArduinoRTC::setAlarm (int year, int month, int day, int hour, int minute, int second,
376560 void (*callback)(const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data),
377561 void *cb_user_data)
@@ -410,7 +594,12 @@ int ArduinoRTC::setAlarm(int year, int month, int day, int hour, int minute, int
410594 return 0 ;
411595}
412596
413- // Cancel alarm
597+ /* *
598+ * @brief Cancels the currently scheduled alarm.
599+ *
600+ * This function stops any active alarm and clears the registered callback
601+ * and user data.
602+ */
414603void ArduinoRTC::cancelAlarm ()
415604{
416605 counter_cancel_channel_alarm (counter_dev, 0 );
@@ -428,6 +617,20 @@ static bool is_leap(int year) {
428617 return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 ));
429618}
430619
620+ /* *
621+ * @brief Converts a date and time to Unix epoch seconds.
622+ *
623+ * This utility function calculates the number of seconds since
624+ * January 1, 1970 (the Unix epoch) for the provided date and time.
625+ *
626+ * @param year Year value (e.g., 2025).
627+ * @param month Month value (1–12).
628+ * @param day Day value (1–31).
629+ * @param hour Hour value (0–23).
630+ * @param minute Minute value (0–59).
631+ * @param second Second value (0–59).
632+ * @return Corresponding epoch time in seconds.
633+ */
431634time_t ArduinoRTC::datetimeToEpoch (int year, int month, int day, int hour, int minute, int second)
432635{
433636 int y = year;
@@ -450,6 +653,20 @@ time_t ArduinoRTC::datetimeToEpoch(int year, int month, int day, int hour, int m
450653 return (((days * 24 + hour) * 60 + minute) * 60 + second);
451654}
452655
656+ /* *
657+ * @brief Converts Unix epoch seconds to a date and time.
658+ *
659+ * This utility function breaks down a given epoch timestamp into a
660+ * human-readable calendar date and time.
661+ *
662+ * @param t Epoch time in seconds.
663+ * @param year Reference to store the year.
664+ * @param month Reference to store the month.
665+ * @param day Reference to store the day.
666+ * @param hour Reference to store the hour.
667+ * @param minute Reference to store the minute.
668+ * @param second Reference to store the second.
669+ */
453670void ArduinoRTC::epochToDatetime (time_t t, int &year, int &month, int &day, int &hour, int &minute, int &second)
454671{
455672 time_t seconds_in_day = t % 86400 ;
0 commit comments