1 /***************************************************************************//**
2  * @file
3  * @brief SLEEPTIMER API definition.
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 /***************************************************************************//**
32  * @addtogroup sleeptimer Sleep Timer
33  * @{
34  ******************************************************************************/
35 
36 #ifndef SL_SLEEPTIMER_H
37 #define SL_SLEEPTIMER_H
38 
39 #include <stdint.h>
40 #include <stddef.h>
41 #include <stdbool.h>
42 #include "sl_status.h"
43 #include "sl_common.h"
44 
45 /// @cond DO_NOT_INCLUDE_WITH_DOXYGEN
46 #define SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG (0x01)
47 #define SL_SLEEPTIMER_ANY_FLAG                                  (0xFF)
48 
49 #define SLEEPTIMER_ENUM(name) typedef uint8_t name; enum name##_enum
50 
51 /// @endcond
52 
53 /// Timestamp, wall clock time in seconds.
54 typedef uint32_t sl_sleeptimer_timestamp_t;
55 
56 // Timestamp, 64 bits wall clock in seconds.
57 typedef uint64_t sl_sleeptimer_timestamp_64_t;     ///< sl sleeptimer timestamp 64 t
58 
59 /// Time zone offset from UTC(second).
60 typedef int32_t sl_sleeptimer_time_zone_offset_t;
61 
62 // Forward declaration
63 typedef struct sl_sleeptimer_timer_handle sl_sleeptimer_timer_handle_t;
64 
65 /***************************************************************************//**
66  * Typedef for the user supplied callback function which is called when
67  * a timer expires.
68  *
69  * @param handle The timer handle.
70  *
71  * @param data An extra parameter for the user application.
72  ******************************************************************************/
73 typedef void (*sl_sleeptimer_timer_callback_t)(sl_sleeptimer_timer_handle_t *handle, void *data);
74 
75 /// @brief Timer structure for sleeptimer
76 struct sl_sleeptimer_timer_handle {
77   void *callback_data;                     ///< User data to pass to callback function.
78   uint8_t priority;                        ///< Priority of timer.
79   uint16_t option_flags;                   ///< Option flags.
80   sl_sleeptimer_timer_handle_t *next;      ///< Pointer to next element in list.
81   sl_sleeptimer_timer_callback_t callback; ///< Function to call when timer expires.
82   uint32_t timeout_periodic;               ///< Periodic timeout.
83   uint32_t delta;                          ///< Delay relative to previous element in list.
84   uint32_t timeout_expected_tc;            ///< Expected tick count of the next timeout (only used for periodic timer).
85   uint16_t conversion_error;               ///< The error when converting ms to ticks (thousandths of ticks)
86   uint16_t accumulated_error;              ///< Accumulated conversion error (thousandths of ticks)
87 };
88 
89 /// @brief Month enum.
SLEEPTIMER_ENUM(sl_sleeptimer_month_t)90 SLEEPTIMER_ENUM(sl_sleeptimer_month_t) {
91   MONTH_JANUARY = 0,
92   MONTH_FEBRUARY = 1,
93   MONTH_MARCH   = 2,
94   MONTH_APRIL = 3,
95   MONTH_MAY = 4,
96   MONTH_JUNE = 5,
97   MONTH_JULY = 6,
98   MONTH_AUGUST = 7,
99   MONTH_SEPTEMBER = 8,
100   MONTH_OCTOBER = 9,
101   MONTH_NOVEMBER = 10,
102   MONTH_DECEMBER = 11,
103 };
104 
105 /// @brief Week Day enum.
SLEEPTIMER_ENUM(sl_sleeptimer_weekDay_t)106 SLEEPTIMER_ENUM(sl_sleeptimer_weekDay_t) {
107   DAY_SUNDAY = 0,
108   DAY_MONDAY = 1,
109   DAY_TUESDAY = 2,
110   DAY_WEDNESDAY = 3,
111   DAY_THURSDAY = 4,
112   DAY_FRIDAY = 5,
113   DAY_SATURDAY = 6,
114 };
115 
116 /// @brief Time and Date structure.
117 typedef  struct  time_date {
118   uint8_t sec;                                ///< Second (0-59)
119   uint8_t min;                                ///< Minute of month (0-59)
120   uint8_t hour;                               ///< Hour (0-23)
121   uint8_t month_day;                          ///< Day of month (1-31)
122   sl_sleeptimer_month_t month;                ///< Month (0-11)
123   uint16_t year;                              ///< Year, based on a 1900 Epoch.
124   sl_sleeptimer_weekDay_t day_of_week;        ///< Day of week (0-6)
125   uint16_t day_of_year;                       ///< Day of year (1-366)
126   sl_sleeptimer_time_zone_offset_t time_zone; ///< Offset, in seconds, from UTC
127 } sl_sleeptimer_date_t;
128 
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 
133 /***************************************************************************//**
134  * Initializes the Sleeptimer.
135  *
136  * @return 0 if successful. Error code otherwise.
137  ******************************************************************************/
138 sl_status_t sl_sleeptimer_init(void);
139 
140 /***************************************************************************//**
141  * Starts a 32 bits timer.
142  *
143  * @param handle Pointer to handle to timer.
144  * @param timeout Timer timeout, in timer ticks.
145  * @param callback Callback function that will be called when
146  *        initial/periodic timeout expires.
147  * @param callback_data Pointer to user data that will be passed to callback.
148  * @param priority Priority of callback. Useful in case multiple timer expire
149  *        at the same time. 0 = highest priority.
150  * @param option_flags Bit array of option flags for the timer.
151  *        Valid bit-wise OR of one or more of the following:
152  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
153  *        or 0 for not flags.
154  *
155  * @return 0 if successful. Error code otherwise.
156  ******************************************************************************/
157 sl_status_t sl_sleeptimer_start_timer(sl_sleeptimer_timer_handle_t *handle,
158                                       uint32_t timeout,
159                                       sl_sleeptimer_timer_callback_t callback,
160                                       void *callback_data,
161                                       uint8_t priority,
162                                       uint16_t option_flags);
163 
164 /***************************************************************************//**
165  * Restarts a 32 bits timer.
166  *
167  * @param handle Pointer to handle to timer.
168  * @param timeout Timer timeout, in timer ticks.
169  * @param callback Callback function that will be called when
170  *        initial/periodic timeout expires.
171  * @param callback_data Pointer to user data that will be passed to callback.
172  * @param priority Priority of callback. Useful in case multiple timer expire
173  *        at the same time. 0 = highest priority.
174  * @param option_flags Bit array of option flags for the timer.
175  *        Valid bit-wise OR of one or more of the following:
176  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
177  *        or 0 for not flags.
178  *
179  * @return 0 if successful. Error code otherwise.
180  ******************************************************************************/
181 sl_status_t sl_sleeptimer_restart_timer(sl_sleeptimer_timer_handle_t *handle,
182                                         uint32_t timeout,
183                                         sl_sleeptimer_timer_callback_t callback,
184                                         void *callback_data,
185                                         uint8_t priority,
186                                         uint16_t option_flags);
187 
188 /***************************************************************************//**
189  * Starts a 32 bits periodic timer.
190  *
191  * @param handle Pointer to handle to timer.
192  * @param timeout Timer periodic timeout, in timer ticks.
193  * @param callback Callback function that will be called when
194  *        initial/periodic timeout expires.
195  * @param callback_data Pointer to user data that will be passed to callback.
196  * @param priority Priority of callback. Useful in case multiple timer expire
197  *        at the same time. 0 = highest priority.
198  * @param option_flags Bit array of option flags for the timer.
199  *        Valid bit-wise OR of one or more of the following:
200  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
201  *        or 0 for not flags.
202  *
203  * @return 0 if successful. Error code otherwise.
204  ******************************************************************************/
205 sl_status_t sl_sleeptimer_start_periodic_timer(sl_sleeptimer_timer_handle_t *handle,
206                                                uint32_t timeout,
207                                                sl_sleeptimer_timer_callback_t callback,
208                                                void *callback_data,
209                                                uint8_t priority,
210                                                uint16_t option_flags);
211 
212 /***************************************************************************//**
213  * Restarts a 32 bits periodic timer.
214  *
215  * @param handle Pointer to handle to timer.
216  * @param timeout Timer periodic timeout, in timer ticks.
217  * @param callback Callback function that will be called when
218  *        initial/periodic timeout expires.
219  * @param callback_data Pointer to user data that will be passed to callback.
220  * @param priority Priority of callback. Useful in case multiple timer expire
221  *        at the same time. 0 = highest priority.
222  * @param option_flags Bit array of option flags for the timer.
223  *        Valid bit-wise OR of one or more of the following:
224  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
225  *        or 0 for not flags.
226  *
227  * @return 0 if successful. Error code otherwise.
228  ******************************************************************************/
229 sl_status_t sl_sleeptimer_restart_periodic_timer(sl_sleeptimer_timer_handle_t *handle,
230                                                  uint32_t timeout,
231                                                  sl_sleeptimer_timer_callback_t callback,
232                                                  void *callback_data,
233                                                  uint8_t priority,
234                                                  uint16_t option_flags);
235 
236 /***************************************************************************//**
237  * Stops a timer.
238  *
239  * @param handle Pointer to handle to timer.
240  *
241  * @return
242  ******************************************************************************/
243 sl_status_t sl_sleeptimer_stop_timer(sl_sleeptimer_timer_handle_t *handle);
244 
245 /***************************************************************************//**
246  * Gets the status of a timer.
247  *
248  * @param handle Pointer to handle to timer.
249  * @param running Pointer to the status of the timer.
250  *
251  * @note A non periodic timer is considered not running during its callback.
252  *
253  * @return 0 if successful. Error code otherwise.
254  ******************************************************************************/
255 sl_status_t sl_sleeptimer_is_timer_running(sl_sleeptimer_timer_handle_t *handle,
256                                            bool *running);
257 
258 /***************************************************************************//**
259  * Gets remaining time until timer expires.
260  *
261  * @param handle Pointer to handle to timer.
262  * @param time Time left in timer ticks.
263  *
264  * @return 0 if successful. Error code otherwise.
265  ******************************************************************************/
266 sl_status_t sl_sleeptimer_get_timer_time_remaining(sl_sleeptimer_timer_handle_t *handle,
267                                                    uint32_t *time);
268 
269 /**************************************************************************//**
270  * Gets the time remaining until the first timer with the matching set of flags
271  * expires.
272  *
273  * @param option_flags Set of flags to match:
274  *          - SL_SLEEPTIMER_ANY_TIMER_FLAG
275  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
276  *
277  * @param time_remaining Time left in timer ticks.
278  *
279  * @return 0 if successful. Error code otherwise.
280  *****************************************************************************/
281 sl_status_t sl_sleeptimer_get_remaining_time_of_first_timer(uint16_t option_flags,
282                                                             uint32_t *time_remaining);
283 
284 /***************************************************************************//**
285  * Gets current 32 bits global tick count.
286  *
287  * @return Current tick count.
288  ******************************************************************************/
289 uint32_t sl_sleeptimer_get_tick_count(void);
290 
291 /***************************************************************************//**
292  * Gets current 64 bits global tick count.
293  *
294  * @return Current tick count.
295  ******************************************************************************/
296 uint64_t sl_sleeptimer_get_tick_count64(void);
297 
298 /***************************************************************************//**
299  * Get timer frequency.
300  *
301  * @return 0 if successful. Error code otherwise.
302  ******************************************************************************/
303 uint32_t sl_sleeptimer_get_timer_frequency(void);
304 
305 /***************************************************************************//**
306  * Converts a Unix timestamp into a date.
307  *
308  * @param time 32 bit Unix timestamp to convert.
309  * @param time_zone Offset from UTC in second.
310  * @param date Pointer to converted date.
311  *
312  * @note Time is in Standard Time.
313  *
314  * @note Function definition is accessible only when
315  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
316  *
317  * @return 0 if successful. Error code otherwise.
318  ******************************************************************************/
319 sl_status_t sl_sleeptimer_convert_time_to_date(sl_sleeptimer_timestamp_t time,
320                                                sl_sleeptimer_time_zone_offset_t time_zone,
321                                                sl_sleeptimer_date_t *date);
322 
323 /***************************************************************************//**
324  * Converts a 64 bit Unix timestamp into a date.
325  *
326  * @param time 64 bit Unix timestamp to convert.
327  * @param time_zone Offset from UTC in second.
328  * @param date Pointer to converted date.
329  *
330  * @note Time is in Standard Time.
331  *
332  * @note Function definition is accessible only when
333  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
334  *
335  * @return 0 if successful. Error code otherwise.
336  ******************************************************************************/
337 sl_status_t sl_sleeptimer_convert_time_to_date_64(sl_sleeptimer_timestamp_64_t time,
338                                                   sl_sleeptimer_time_zone_offset_t time_zone,
339                                                   sl_sleeptimer_date_t *date);
340 
341 /***************************************************************************//**
342  * Converts a date into a Unix timestamp.
343  *
344  * @param date Pointer to date to convert.
345  * @param time Pointer to converted 32 bit Unix timestamp.
346  *
347  * @return 0 if successful. Error code otherwise.
348  *
349  * @note Dates are based on the Unix time representation.
350  *       Range of dates supported :
351  *          - January 1, 1970, 00:00:00 to January 19, 2038, 03:14:00
352  *
353  * @note Function definition is accessible only when
354  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
355  ******************************************************************************/
356 sl_status_t sl_sleeptimer_convert_date_to_time(sl_sleeptimer_date_t *date,
357                                                sl_sleeptimer_timestamp_t *time);
358 
359 /***************************************************************************//**
360  * Converts a date into a 64 bit timestamp.
361  *
362  * @param date Pointer to date to convert.
363  * @param time Pointer to converted 64 bit Unix timestamp.
364  *
365  * @return 0 if successful. Error code otherwise.
366  *
367  * @note Dates are based on the 64 bit Unix time representation.
368  *       Range of dates supported :
369  *          - January 1, 1900, 00:00:00 to  December 31, 11899 23:59:59.
370  *
371  * @note Function definition is accessible only when
372  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
373  ******************************************************************************/
374 sl_status_t sl_sleeptimer_convert_date_to_time_64(sl_sleeptimer_date_t *date,
375                                                   sl_sleeptimer_timestamp_64_t *time);
376 
377 /***************************************************************************//**
378  * Convert date to string.
379  *
380  * @param str Output string.
381  * @param size Size of the input array.
382  * @param format The format specification character.
383  * @param date Pointer to date structure.
384  *
385  * @return 0 if error. Number of character in the output string.
386  *
387  * @note Refer strftime() from UNIX.
388  *       http://man7.org/linux/man-pages/man3/strftime.3.html
389  *
390  * @note Function definition is accessible only when
391  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
392  ******************************************************************************/
393 uint32_t sl_sleeptimer_convert_date_to_str(char *str,
394                                            size_t size,
395                                            const uint8_t *format,
396                                            sl_sleeptimer_date_t *date);
397 
398 /***************************************************************************//**
399  * Sets time zone offset.
400  *
401  * @param  offset  Time zone offset, in seconds.
402  *
403  * @note Function definition is accessible only when
404  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
405  *
406  ******************************************************************************/
407 void sl_sleeptimer_set_tz(sl_sleeptimer_time_zone_offset_t offset);
408 
409 /***************************************************************************//**
410  * Gets time zone offset.
411  *
412  * @return Time zone offset, in seconds.
413  ******************************************************************************/
414 sl_sleeptimer_time_zone_offset_t sl_sleeptimer_get_tz(void);
415 
416 /***************************************************************************//**
417  * Retrieves current 32 bit time.
418  *
419  * @note Function definition is accessible only when
420  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
421  *
422  * @return Current timestamps in Unix format.
423  ******************************************************************************/
424 sl_sleeptimer_timestamp_t sl_sleeptimer_get_time(void);
425 
426 /***************************************************************************//**
427  * Retrieves current 64 bit time.
428  *
429  * @note Function definition is accessible only when
430  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
431  *
432  * @return Current timestamps in Unix format.
433  ******************************************************************************/
434 sl_sleeptimer_timestamp_64_t sl_sleeptimer_get_time_64(void);
435 
436 /***************************************************************************//**
437  * Sets current time.
438  *
439  * @param time timestamp structure to set.
440  *
441  * @note Function definition is accessible only when
442  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
443  *
444  * @return 0 if successful. Error code otherwise.
445  ******************************************************************************/
446 sl_status_t sl_sleeptimer_set_time(sl_sleeptimer_timestamp_t time);
447 
448 /***************************************************************************//**
449  * Sets current time.
450  *
451  * @param time timestamp structure to set.
452  *
453  * @note Function definition is accessible only when
454  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
455  *
456  * @return 0 if successful. Error code otherwise.
457  ******************************************************************************/
458 sl_status_t sl_sleeptimer_set_time_64(sl_sleeptimer_timestamp_64_t time);
459 
460 /***************************************************************************//**
461  * Gets current date.
462  *
463  * @param date Pointer to a sl_sleeptimer_date_t structure.
464  *
465  * @note Time is in Standard Time.
466  *
467  * @note Function definition is accessible only when
468  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
469  *
470  * @return 0 if successful. Error code otherwise.
471  ******************************************************************************/
472 sl_status_t sl_sleeptimer_get_datetime(sl_sleeptimer_date_t *date);
473 
474 /***************************************************************************//**
475  * Sets current time, in date format.
476  *
477  * @param date Pointer to current date.
478  *
479  * @note Function definition is accessible only when
480  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
481  *
482  * @return 0 if successful. Error code otherwise.
483  ******************************************************************************/
484 sl_status_t sl_sleeptimer_set_datetime(sl_sleeptimer_date_t *date);
485 
486 /***************************************************************************//**
487  * Builds a date time structure based on the provided parameters,
488  * where the maximum supported date is 10:14:07 PM 01/18/2038.
489  *
490  * @param date Pointer to the structure to be populated.
491  * @param year Current year. May be provided based on a 0 Epoch or a 1900 Epoch.
492  * @param month Months since January. Expected value: 0-11.
493  * @param month_day Day of the month. Expected value: 1-31.
494  * @param hour Hours since midnight. Expected value: 0-23.
495  * @param min Minutes after the hour. Expected value: 0-59.
496  * @param sec Seconds after the minute. Expected value: 0-59.
497  * @param tzOffset Offset, in seconds, from UTC.
498  *
499  * @note Function definition is accessible only when
500  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
501  *
502  * @return 0 if successful. Error code otherwise.
503  ******************************************************************************/
504 sl_status_t sl_sleeptimer_build_datetime(sl_sleeptimer_date_t *date,
505                                          uint16_t year,
506                                          sl_sleeptimer_month_t month,
507                                          uint8_t month_day,
508                                          uint8_t hour,
509                                          uint8_t min,
510                                          uint8_t sec,
511                                          sl_sleeptimer_time_zone_offset_t tzOffset);
512 
513 /***************************************************************************//**
514  * Builds a date time structure based on the provided parameters,
515  * where the maximum supported date is 11:59:59 PM 12/31/11899.
516  *
517  * @param date Pointer to the structure to be populated.
518  * @param year Current year based on 0 Epoch.
519  * @param month Months since January. Expected value: 0-11.
520  * @param month_day Day of the month. Expected value: 1-31.
521  * @param hour Hours since midnight. Expected value: 0-23.
522  * @param min Minutes after the hour. Expected value: 0-59.
523  * @param sec Seconds after the minute. Expected value: 0-59.
524  * @param tzOffset Offset, in seconds, from UTC.
525  *
526  * @note Resulting date structure's year will be based on 1900 epoch
527  *
528  * @note Function definition is accessible only when
529  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
530  *
531  * @return 0 if successful. Error code otherwise.
532  ******************************************************************************/
533 sl_status_t sl_sleeptimer_build_datetime_64(sl_sleeptimer_date_t *date,
534                                             uint16_t year,
535                                             sl_sleeptimer_month_t month,
536                                             uint8_t month_day,
537                                             uint8_t hour,
538                                             uint8_t min,
539                                             uint8_t sec,
540                                             sl_sleeptimer_time_zone_offset_t tzOffset);
541 
542 /***************************************************************************//**
543  * Converts Unix timestamp into NTP timestamp.
544  *
545  * @param time Unix timestamp.
546  * @param ntp_time Pointer to NTP Timestamp.
547  *
548  * @note Unix timestamp range supported : 0x0 to 0x7C55 817F
549  *       ie. January 1, 1970, 00:00:00 to February 07, 2036, 06:28:15
550  *
551  * @note Function definition is accessible only when
552  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
553  *
554  * @return 0 if successful. Error code otherwise.
555  ******************************************************************************/
556 sl_status_t sl_sleeptimer_convert_unix_time_to_ntp(sl_sleeptimer_timestamp_t time,
557                                                    uint32_t *ntp_time);
558 
559 /***************************************************************************//**
560  * Converts NTP timestamp into Unix timestamp.
561  *
562  * @param ntp_time NTP Timestamp.
563  * @param time Pointer to Unix timestamp.
564  *
565  * @note NTP timestamp range supported : 0x83AA 7E80 to 0xFFFF FFFF
566  *       ie. January 1, 1970, 00:00:00 to February 07, 2036, 06:28:15
567  *
568  * @note Function definition is accessible only when
569  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
570  *
571  * @return 0 if successful. Error code otherwise.
572  ******************************************************************************/
573 sl_status_t sl_sleeptimer_convert_ntp_time_to_unix(uint32_t ntp_time,
574                                                    sl_sleeptimer_timestamp_t *time);
575 
576 /***************************************************************************//**
577  * Converts Unix timestamp into Zigbee timestamp.
578  *
579  * @param time Unix timestamp.
580  *
581  * @param zigbee_time Pointer to NTP Timestamp.
582  *
583  * @note Unix timestamp range supported : 0x386D 4380 to 0x7FFF FFFF
584  *       ie. January 1, 2000, 00:00:0 to January 19, 2038, 03:14:00
585  *
586  * @note Function definition is accessible only when
587  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
588  *
589  * @return 0 if successful. Error code otherwise.
590  ******************************************************************************/
591 sl_status_t sl_sleeptimer_convert_unix_time_to_zigbee(sl_sleeptimer_timestamp_t time,
592                                                       uint32_t *zigbee_time);
593 
594 /***************************************************************************//**
595  * Converts Zigbee timestamp into Unix timestamp.
596  *
597  * @param zigbee_time NTP Timestamp.
598  * @param time Pointer to Unix timestamp.
599  *
600  * @note ZIGBEE timestamp range supported : 0x0 to 0x4792 BC7F
601  *        ie. January 1, 2000, 00:00:00 to January 19, 2038, 03:14:00
602  *
603  * @note Function definition is accessible only when
604  *       SL_SLEEPTIMER_WALLCLOCK_CONFIG is set to 1.
605  *
606  * @return 0 if successful. Error code otherwise.
607  ******************************************************************************/
608 sl_status_t sl_sleeptimer_convert_zigbee_time_to_unix(uint32_t zigbee_time,
609                                                       sl_sleeptimer_timestamp_t *time);
610 
611 /***************************************************************************//**
612  * Calculates offset for time zone after UTC-0.
613  *
614  * @param hours Number of hours from UTC-0.
615  * @param minutes Number of minutes from UTC-0.
616  *
617  * @return The time zone offset in seconds.
618  ******************************************************************************/
sl_sleeptimer_set_tz_ahead_utc(uint8_t hours,uint8_t minutes)619 __STATIC_INLINE sl_sleeptimer_time_zone_offset_t sl_sleeptimer_set_tz_ahead_utc(uint8_t hours,
620                                                                                 uint8_t minutes)
621 {
622   return ((hours * 3600u) + (minutes * 60u));
623 }
624 
625 /***************************************************************************//**
626  * Calculates offset for time zone before UTC-0.
627  *
628  * @param hours Number of hours to UTC-0.
629  * @param minutes Number of minutes to UTC-0.
630  *
631  * @return The time zone offset in seconds.
632  ******************************************************************************/
sl_sleeptimer_set_tz_behind_utc(uint8_t hours,uint8_t minutes)633 __STATIC_INLINE sl_sleeptimer_time_zone_offset_t sl_sleeptimer_set_tz_behind_utc(uint8_t hours,
634                                                                                  uint8_t minutes)
635 {
636   return -(sl_sleeptimer_time_zone_offset_t)((hours * 3600u) + (minutes * 60u));
637 }
638 
639 /***************************************************************************//**
640  * Active delay.
641  *
642  * @param time_ms Delay duration in milliseconds.
643  ******************************************************************************/
644 void sl_sleeptimer_delay_millisecond(uint16_t time_ms);
645 
646 /***************************************************************************//**
647  * Converts milliseconds in ticks.
648  *
649  * @param time_ms Number of milliseconds.
650  *
651  * @return Corresponding ticks number.
652  *
653  * @note The result is "rounded" to the superior tick number.
654  *       This function is light and cannot fail so it should be privilegied to
655  *       perform a millisecond to tick conversion.
656  ******************************************************************************/
657 uint32_t sl_sleeptimer_ms_to_tick(uint16_t time_ms);
658 
659 /***************************************************************************//**
660  * Converts 32-bits milliseconds in ticks.
661  *
662  * @param time_ms Number of milliseconds.
663  * @param tick Pointer to the converted tick number.
664  *
665  * @return 0 if successful. Error code otherwise.
666  *
667  * @note  The result is "rounded" to the superior tick number.
668  *        If possible the sl_sleeptimer_ms_to_tick() function should be used.
669  *
670  * @note  This function converts the delay expressed in milliseconds to timer
671  *        ticks (represented on 32 bits). This means that the value that can
672  *        be passed to the argument 'time_ms' is limited. The maximum
673  *        timeout value that can be passed to this function can be retrieved
674  *        by calling sl_sleeptimer_get_max_ms32_conversion().
675  *        If the value passed to 'time_ms' is too large,
676  *        SL_STATUS_INVALID_PARAMETER will be returned.
677  ******************************************************************************/
678 sl_status_t sl_sleeptimer_ms32_to_tick(uint32_t time_ms,
679                                        uint32_t *tick);
680 
681 /***************************************************************************//**
682  * Gets the maximum value that can be passed to the functions that have a
683  * 32-bits time or timeout argument expressed in milliseconds.
684  *
685  * @return Maximum time or timeout value in milliseconds.
686  ******************************************************************************/
687 uint32_t sl_sleeptimer_get_max_ms32_conversion(void);
688 
689 /***************************************************************************//**
690  * Converts ticks in milliseconds.
691  *
692  * @param tick Number of tick.
693  *
694  * @return Corresponding milliseconds number.
695  *
696  * @note The result is rounded to the inferior millisecond.
697  ******************************************************************************/
698 uint32_t sl_sleeptimer_tick_to_ms(uint32_t tick);
699 
700 /***************************************************************************//**
701  * Converts 64-bit ticks in milliseconds.
702  *
703  * @param tick Number of tick.
704  * @param ms Pointer to the converted milliseconds number.
705  *
706  * @return 0 if successful. Error code otherwise.
707  *
708  * @note The result is rounded to the inferior millisecond.
709  ******************************************************************************/
710 sl_status_t sl_sleeptimer_tick64_to_ms(uint64_t tick,
711                                        uint64_t *ms);
712 
713 /***************************************************************************//**
714  * Allow sleep after ISR exit.
715  *
716  * @return true if sleep is allowed after ISR exit. False otherwise.
717  ******************************************************************************/
718 bool sl_sleeptimer_is_power_manager_early_restore_timer_latest_to_expire(void);
719 
720 /**************************************************************************//**
721  * Starts a 32 bits timer.
722  *
723  * @param handle Pointer to handle to timer.
724  * @param timeout_ms Timer timeout, in milliseconds.
725  * @param callback Callback function that will be called when
726  *        initial/periodic timeout expires.
727  * @param callback_data Pointer to user data that will be passed to callback.
728  * @param priority Priority of callback. Useful in case multiple timer expire
729  *        at the same time. 0 = highest priority.
730  * @param option_flags Bit array of option flags for the timer.
731  *        Valid bit-wise OR of one or more of the following:
732  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
733  *        or 0 for not flags.
734  *
735  * @return 0 if successful. Error code otherwise.
736  *
737  * @note  This function converts the delay expressed in milliseconds to timer
738  *        ticks (represented on 32 bits). This means that the value that can
739  *        be passed to the argument 'timeout_ms' is limited. The maximum
740  *        timeout value that can be passed to this function can be retrieved
741  *        by calling sl_sleeptimer_get_max_ms32_conversion().
742  *        If the value passed to 'timeout_ms' is too large,
743  *        SL_STATUS_INVALID_PARAMETER will be returned.
744  *****************************************************************************/
sl_sleeptimer_start_timer_ms(sl_sleeptimer_timer_handle_t * handle,uint32_t timeout_ms,sl_sleeptimer_timer_callback_t callback,void * callback_data,uint8_t priority,uint16_t option_flags)745 __STATIC_INLINE sl_status_t sl_sleeptimer_start_timer_ms(sl_sleeptimer_timer_handle_t *handle,
746                                                          uint32_t timeout_ms,
747                                                          sl_sleeptimer_timer_callback_t callback,
748                                                          void *callback_data,
749                                                          uint8_t priority,
750                                                          uint16_t option_flags)
751 {
752   sl_status_t status;
753   uint32_t timeout_tick;
754 
755   status = sl_sleeptimer_ms32_to_tick(timeout_ms, &timeout_tick);
756   if (status != SL_STATUS_OK) {
757     return status;
758   }
759 
760   return sl_sleeptimer_start_timer(handle, timeout_tick, callback, callback_data, priority, option_flags);
761 }
762 
763 /**************************************************************************//**
764  * Restarts a 32 bits timer.
765  *
766  * @param handle Pointer to handle to timer.
767  * @param timeout_ms Timer timeout, in milliseconds.
768  * @param callback Callback function that will be called when
769  *        initial/periodic timeout expires.
770  * @param callback_data Pointer to user data that will be passed to callback.
771  * @param priority Priority of callback. Useful in case multiple timer expire
772  *        at the same time. 0 = highest priority.
773  * @param option_flags Bit array of option flags for the timer.
774  *        Valid bit-wise OR of one or more of the following:
775  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
776  *        or 0 for not flags.
777  *
778  * @return 0 if successful. Error code otherwise.
779  *
780  * @note  This function converts the delay expressed in milliseconds to timer
781  *        ticks (represented on 32 bits). This means that the value that can
782  *        be passed to the argument 'timeout_ms' is limited. The maximum
783  *        timeout value that can be passed to this function can be retrieved
784  *        by calling sl_sleeptimer_get_max_ms32_conversion().
785  *        If the value passed to 'timeout_ms' is too large,
786  *        SL_STATUS_INVALID_PARAMETER will be returned.
787  *****************************************************************************/
sl_sleeptimer_restart_timer_ms(sl_sleeptimer_timer_handle_t * handle,uint32_t timeout_ms,sl_sleeptimer_timer_callback_t callback,void * callback_data,uint8_t priority,uint16_t option_flags)788 __STATIC_INLINE sl_status_t sl_sleeptimer_restart_timer_ms(sl_sleeptimer_timer_handle_t *handle,
789                                                            uint32_t timeout_ms,
790                                                            sl_sleeptimer_timer_callback_t callback,
791                                                            void *callback_data,
792                                                            uint8_t priority,
793                                                            uint16_t option_flags)
794 {
795   sl_status_t status;
796   uint32_t timeout_tick;
797 
798   status = sl_sleeptimer_ms32_to_tick(timeout_ms, &timeout_tick);
799   if (status != SL_STATUS_OK) {
800     return status;
801   }
802 
803   return sl_sleeptimer_restart_timer(handle, timeout_tick, callback, callback_data, priority, option_flags);
804 }
805 
806 /***************************************************************************//**
807  * Starts a 32 bits periodic timer.
808  *
809  * @param handle Pointer to handle to timer.
810  * @param timeout_ms Timer periodic timeout, in milliseconds.
811  * @param callback Callback function that will be called when
812  *        initial/periodic timeout expires.
813  * @param callback_data Pointer to user data that will be passed to callback.
814  * @param priority Priority of callback. Useful in case multiple timer expire
815  *        at the same time. 0 = highest priority.
816  * @param option_flags Bit array of option flags for the timer.
817  *        Valid bit-wise OR of one or more of the following:
818  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
819  *        or 0 for not flags.
820  *
821  * @return 0 if successful. Error code otherwise.
822  *
823  * @note  This function converts the delay expressed in milliseconds to timer
824  *        ticks (represented on 32 bits). This means that the value that can
825  *        be passed to the argument 'timeout_ms' is limited. The maximum
826  *        timeout value that can be passed to this function can be retrieved
827  *        by calling sl_sleeptimer_get_max_ms32_conversion().
828  *        If the value passed to 'timeout_ms' is too large,
829  *        SL_STATUS_INVALID_PARAMETER will be returned.
830  ******************************************************************************/
831 sl_status_t sl_sleeptimer_start_periodic_timer_ms(sl_sleeptimer_timer_handle_t *handle,
832                                                   uint32_t timeout_ms,
833                                                   sl_sleeptimer_timer_callback_t callback,
834                                                   void *callback_data,
835                                                   uint8_t priority,
836                                                   uint16_t option_flags);
837 
838 /***************************************************************************//**
839  * Restarts a 32 bits periodic timer.
840  *
841  * @param handle Pointer to handle to timer.
842  * @param timeout_ms Timer periodic timeout, in milliseconds.
843  * @param callback Callback function that will be called when
844  *        initial/periodic timeout expires.
845  * @param callback_data Pointer to user data that will be passed to callback.
846  * @param priority Priority of callback. Useful in case multiple timer expire
847  *        at the same time. 0 = highest priority.
848  * @param option_flags Bit array of option flags for the timer.
849  *        Valid bit-wise OR of one or more of the following:
850  *          - SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG
851  *        or 0 for not flags.
852  *
853  * @return 0 if successful. Error code otherwise.
854  *
855  * @note  This function converts the delay expressed in milliseconds to timer
856  *        ticks (represented on 32 bits). This means that the value that can
857  *        be passed to the argument 'timeout_ms' is limited. The maximum
858  *        timeout value that can be passed to this function can be retrieved
859  *        by calling sl_sleeptimer_get_max_ms32_conversion().
860  *        If the value passed to 'timeout_ms' is too large,
861  *        SL_STATUS_INVALID_PARAMETER will be returned.
862  ******************************************************************************/
863 sl_status_t sl_sleeptimer_restart_periodic_timer_ms(sl_sleeptimer_timer_handle_t *handle,
864                                                     uint32_t timeout_ms,
865                                                     sl_sleeptimer_timer_callback_t callback,
866                                                     void *callback_data,
867                                                     uint8_t priority,
868                                                     uint16_t option_flags);
869 
870 /***************************************************************************//**
871  * @brief
872  *   Gets the precision (in PPM) of the sleeptimer's clock.
873  *
874  * @return
875  *   Clock accuracy, in PPM.
876  ******************************************************************************/
877 uint16_t sl_sleeptimer_get_clock_accuracy(void);
878 
879 #ifdef __cplusplus
880 }
881 #endif
882 
883 /** @} (end addtogroup sleeptimer) */
884 
885 /* *INDENT-OFF* */
886 /* THE REST OF THE FILE IS DOCUMENTATION ONLY! */
887 /// @addtogroup sleeptimer Sleep Timer
888 /// @{
889 ///
890 ///   @details
891 ///   Sleep Timer can be used for creating timers which are tightly integrated with power management.
892 ///   The Power Manager requires precision timing to have all clocks ready on time, so that wakeup
893 ///   happens a little bit earlier to prepare the system to be ready at the right time.
894 ///   Sleep Timer uses one Hardware Timer and creates multiple software timer instances. It is important
895 ///   to note that when sleeptimer is used with WTIMER/TIMER, the MCU cannot go to EM2 energy mode
896 ///   because WTIMER/TIMER uses a high frequency clock source which is not retained in low energy mode.
897 ///
898 ///   The sleeptimer.c and sleeptimer.h source files for the SLEEPTIMER device driver library are in the
899 ///   service/sleeptimer folder.
900 ///
901 ///   @n @section sleeptimer_intro Introduction
902 ///
903 ///   The Sleeptimer driver provides software timers, delays, timekeeping and date functionalities using a low-frequency real-time clock peripheral.
904 ///
905 ///   All Silicon Labs microcontrollers equipped with the RTC or RTCC peripheral are currently supported. Only one instance of this driver can be initialized by the application.
906 ///
907 ///   @n @section sleeptimer_functionalities_overview Functionalities overview
908 ///
909 ///   @n @subsection software_timers Software Timers
910 ///
911 ///   This functionality allows the user to create periodic and one shot timers. A user callback can be associated with a timer and is called when the timer expires.
912 ///
913 ///   Timer structures must be allocated by the user. The function is called from within an interrupt handler with interrupts enabled.
914 ///
915 ///   @n @subsection timekeeping Timekeeping
916 ///
917 ///   A 64-bits tick counter is accessible through the @li uint64_t sl_sleeptimer_get_tick_count64(void) API. It keeps the tick count since the initialization of the driver
918 ///
919 ///   The `SL_SLEEPTIMER_WALLCLOCK_CONFIG` configuration enables a UNIX timestamp (seconds count since January 1, 1970, 00:00:00).
920 ///
921 ///   This timestamp can be retrieved/modified using the following API:
922 ///
923 ///   @li sl_sleeptimer_timestamp_t sl_sleeptimer_get_time(void);
924 ///   @li sl_status_t sl_sleeptimer_set_time(sl_sleeptimer_timestamp_t time);
925 ///
926 ///   Convenience conversion functions are provided to convert UNIX timestamp to/from NTP and Zigbee cluster format :
927 ///
928 ///   @li sl_status_t sl_sleeptimer_convert_unix_time_to_ntp(sl_sleeptimer_timestamp_t time, uint32_t *ntp_time);
929 ///   @li sl_status_t sl_sleeptimer_convert_ntp_time_to_unix(uint32_t ntp_time, sl_sleeptimer_timestamp_t *time);
930 ///   @li sl_status_t sl_sleeptimer_convert_unix_time_to_zigbee(sl_sleeptimer_timestamp_t time, uint32_t *zigbee_time);
931 ///   @li sl_status_t sl_sleeptimer_convert_zigbee_time_to_unix(uint32_t zigbee_time, sl_sleeptimer_timestamp_t *time);
932 ///
933 ///   @n @subsection date Date
934 ///
935 ///   The previously described internal timestamp can also be retrieved/modified in a date format sl_sleeptimer_date_t.
936 ///
937 ///   @n <b>API :</b> @n
938 ///
939 ///   @li sl_status_t sl_sleeptimer_get_datetime(sl_sleeptimer_date_t *date);
940 ///   @li sl_status_t sl_sleeptimer_set_datetime(sl_sleeptimer_date_t *date);
941 ///
942 ///   @n @subsection frequency_setup Frequency Setup and Tick Count
943 ///
944 ///   This driver works with a configurable time unit called tick.
945 ///
946 ///   The frequency of the ticks is based on the clock source and the internal frequency divider.
947 ///
948 ///   WTIMER/TIMER peripherals uses high frequency oscillator. To have a reasonable tick frequency, divider is set to maximum value (1024).
949 ///
950 ///   One of the following clock sources must be enabled before initializing the sleeptimer:
951 ///
952 ///   @li LFXO: external crystal oscillator. Typically running at 32.768 kHz.
953 ///   @li LFRCO: internal oscillator running at 32.768 kHz
954 ///   @li ULFRCO: Ultra low-frequency oscillator running at 1.000 kHz
955 ///   @li HFXO: High Frequency Crystal Oscillator at 39 Mhz. HFXO is only needed when Sleeptimer runs on TIMER or WTIMER.
956 ///
957 ///   The frequency divider is selected with the `SL_SLEEPTIMER_FREQ_DIVIDER` configuration. Its value must be a power of two within the range of 1 to 32. The number of ticks per second (sleeptimer frequency) is dictated by the following formula:
958 ///
959 ///   Tick (seconds) = 1 / (clock_frequency / frequency_divider)
960 ///
961 ///   The highest resolution for a tick is 30.5 us. It is achieved with a 32.768 kHz clock and a divider of 1.
962 ///
963 ///   @n @section sleeptimer_getting_started Getting Started
964 ///
965 ///   @n @subsection  clock_selection Clock Selection
966 ///
967 ///   The sleeptimer relies on the hardware timer to operate. The hardware timer peripheral must be properly clocked from the application. Selecting the appropriate timer is crucial for design considerations. Each timer can potentially be used as a sleeptimer and is also available to the user. However, note that if a timer is used by the sleeptimer, it can't be used by the application and vice versa.
968 ///
969 ///   For WTIMER/TIMER peripherals, the user must select the appropriate oscillator if it is not the default wanted clock source.
970 ///
971 ///   When WTIMER/TIMER is selected, sleeptimer uses channel 0 and it is not possible to use other channels of the same instance for other purposes.
972 ///
973 ///   @n @subsection  Clock Selection in a Project without Micrium OS
974 ///
975 ///   When RTC, RTCC, or BURTC is selected, the clock source for the peripheral must be configured and enabled in the application before initializing the sleeptimer module or any communication stacks. Most of the time, it consists in enabling the desired oscillators and setting up the clock source for the peripheral, like in the following example:
976 ///
977 ///   @code{.c}
978 ///   CMU_ClockSelectSet(cmuClock_LFE, cmuSelect_LFRCO);
979 ///   CMU_ClockEnable(cmuClock_RTCC, true);
980 ///   @endcode
981 ///
982 ///   @n @subsection  clock_branch_select Clock Branch Select
983 ///
984 ///   | Clock  | Enum                    | Description                       | Frequency |
985 ///   |--------|-------------------------|-----------------------------------|-----------|
986 ///   | LFXO   | <b>cmuSelect_LFXO</b>   | Low-frequency crystal oscillator  |32.768 Khz |
987 ///   | LFRCO  | <b>cmuSelect_LFRCO</b>  | Low-frequency RC oscillator       |32.768 Khz |
988 ///   | ULFRCO | <b>cmuSelect_ULFRCO</b> | Ultra low-frequency RC oscillator |1 Khz      |
989 ///
990 ///   @n @subsection  timer_clock_enable Timer Clock Enable
991 ///
992 ///   | Module             | Enum                  | Description                                        |
993 ///   |--------------------|-----------------------|----------------------------------------------------|
994 ///   | RTCC               | <b>cmuClock_RTCC</b>  | Real-time counter and calendar clock (LF E branch) |
995 ///   | RTC                | <b>cmuClock_RTC</b>   | Real time counter clock (LF A branch)              |
996 ///   | BURTC              | <b>cmuClock_BURTC</b> | BURTC clock (EM4 Group A branch)                   |
997 ///
998 ///   When the Radio internal RTC (PRORTC) is selected, it is not necessary to configure the clock source for the peripheral. However, it is important to enable the desired oscillator before initializing the sleeptimer module or any communication stacks. The best oscillator available (LFXO being the first choice) will be used by the sleeptimer at initalization. The following example shows how the desired oscilator should be enabled:
999 ///
1000 ///   @code{.c}
1001 ///   CMU_OscillatorEnable(cmuSelect_LFXO, true, true);
1002 ///   @endcode
1003 ///
1004 ///   @n @subsection  clock_micrium_os Clock Selection in a Project with Micrium OS
1005 ///
1006 ///   When Micrium OS is used, a BSP (all instances) is provided that sets up some parts of the clock tree. The sleeptimer clock source will be enabled by this bsp. However, the desired oscillator remains configurable from the file <b>bsp_cfg.h</b>.
1007 ///
1008 ///   The configuration `BSP_LF_CLK_SEL` determines which oscillator will be used by the sleeptimer's hardware timer peripheral. It can take the following values:
1009 ///
1010 ///   | Config                   | Description                       | Frequency |
1011 ///   |--------------------------|-----------------------------------|-----------|
1012 ///   | <b>BSP_LF_CLK_LFXO</b>   | Low-frequency crystal oscillator  |32.768 Khz |
1013 ///   | <b>BSP_LF_CLK_LFRCO</b>  | Low-frequency RC oscillator       |32.768 Khz |
1014 ///   | <b>BSP_LF_CLK_ULFRCO</b> | Ultra low-frequency RC oscillator |1 Khz      |
1015 ///
1016 ///   @n @section sleeptimer_conf Configuration Options
1017 ///
1018 ///   `SL_SLEEPTIMER_PERIPHERAL` can be set to one of the following values:
1019 ///
1020 ///   | Config                            | Description                                                                                          |
1021 ///   | --------------------------------- |------------------------------------------------------------------------------------------------------|
1022 ///   | `SL_SLEEPTIMER_PERIPHERAL_DEFAULT`| Selects either RTC or RTCC, depending of what is available on the platform.                          |
1023 ///   | `SL_SLEEPTIMER_PERIPHERAL_RTCC`   | Selects RTCC                                                                                         |
1024 ///   | `SL_SLEEPTIMER_PERIPHERAL_RTC`    | Selects RTC                                                                                          |
1025 ///   | `SL_SLEEPTIMER_PERIPHERAL_PRORTC` | Selects Internal radio RTC. Available only on EFR32XG13, EFR32XG14, EFR32XG21 and EFR32XG22 families.|
1026 ///   | `SL_SLEEPTIMER_PERIPHERAL_BURTC`  | Selects BURTC. Not available on Series 0 devices.                                                    |
1027 ///
1028 ///   `SL_SLEEPTIMER_WALLCLOCK_CONFIG` must be set to 1 to enable timestamp and date functionnalities.
1029 ///
1030 ///   `SL_SLEEPTIMER_FREQ_DIVIDER` must be a power of 2 within the range 1 to 32. When `SL_SLEEPTIMER_PERIPHERAL` is set to `SL_SLEEPTIMER_PERIPHERAL_PRORTC`, `SL_SLEEPTIMER_FREQ_DIVIDER` must be set to 1.
1031 ///
1032 ///   `SL_SLEEPTIMER_PRORTC_HAL_OWNS_IRQ_HANDLER` is only meaningful when `SL_SLEEPTIMER_PERIPHERAL` is set to `SL_SLEEPTIMER_PERIPHERAL_PRORTC`. Set to 1 if no communication stack is used in your project. Otherwise, must be set to 0.
1033 ///
1034 ///   @n @section sleeptimer_api The API
1035 ///
1036 ///   This section contains brief descriptions of the API functions. For
1037 ///   more information about input and output parameters and return values,
1038 ///   click on the hyperlinked function names. Most functions return an error
1039 ///   code, `SL_STATUS_OK` is returned on success,
1040 ///   see sl_status.h for other error codes.
1041 ///
1042 ///   The application code must include the @em sl_sleeptimer.h header file.
1043 ///
1044 ///   All API functions can be called from within interrupt handlers.
1045 ///
1046 ///   @ref sl_sleeptimer_init() @n
1047 ///    These functions initialize the sleeptimer driver. Typically,
1048 ///     sl_sleeptimer_init() is called once in the startup code.
1049 ///
1050 ///   @ref sl_sleeptimer_start_timer() @n
1051 ///    Start a one shot 32 bits timer. When a timer expires, a user-supplied callback function
1052 ///    is called. A pointer to this function is passed to
1053 ///     sl_sleeptimer_start_timer(). See @ref callback for
1054 ///    details of the callback prototype.
1055 ///
1056 ///   @ref sl_sleeptimer_restart_timer() @n
1057 ///    Restart a one shot 32 bits timer. When a timer expires, a user-supplied callback function
1058 ///    is called. A pointer to this function is passed to
1059 ///     sl_sleeptimer_start_timer(). See @ref callback for
1060 ///    details of the callback prototype.
1061 ///
1062 ///   @ref sl_sleeptimer_start_periodic_timer() @n
1063 ///    Start a periodic 32 bits timer. When a timer expires, a user-supplied callback function
1064 ///    is called. A pointer to this function is passed to
1065 ///     sl_sleeptimer_start_timer(). See @ref callback for
1066 ///    details of the callback prototype.
1067 ///
1068 ///   @ref sl_sleeptimer_restart_periodic_timer() @n
1069 ///    Restart a periodic 32 bits timer. When a timer expires, a user-supplied callback function
1070 ///    is called. A pointer to this function is passed to
1071 ///     sl_sleeptimer_start_timer(). See @ref callback for
1072 ///    details of the callback prototype.
1073 ///
1074 ///   @ref sl_sleeptimer_stop_timer() @n
1075 ///    Stop a timer.
1076 ///
1077 ///   @ref sl_sleeptimer_get_timer_time_remaining() @n
1078 ///    Get the time remaining before the timer expires.
1079 ///
1080 ///   @ref sl_sleeptimer_delay_millisecond() @n
1081 ///    Delay for the given number of milliseconds. This is an "active wait" delay function.
1082 ///
1083 ///   @ref sl_sleeptimer_is_timer_running() @n
1084 ///    Check if a timer is running.
1085 ///
1086 ///   @ref sl_sleeptimer_get_time(), @ref sl_sleeptimer_set_time() @n
1087 ///    Get or set wallclock time.
1088 ///
1089 ///   @ref sl_sleeptimer_ms_to_tick(), @ref sl_sleeptimer_ms32_to_tick(),
1090 ///   @ref sl_sleeptimer_tick_to_ms(), @ref sl_sleeptimer_tick64_to_ms() @n
1091 ///    Convert between milliseconds and RTC/RTCC
1092 ///    counter ticks.
1093 ///
1094 ///   @n @anchor callback <b>The timer expiry callback function:</b> @n
1095 ///   The callback function, prototyped as @ref sl_sleeptimer_timer_callback_t(), is called from
1096 ///   within the RTC peripheral interrupt handler on timer expiration.
1097 ///    sl_sleeptimer_timer_callback_t(sl_sleeptimer_timer_handle_t *handle, void *data)
1098 ///
1099 ///   @n @section sleeptimer_example Example
1100 ///   @code{.c}
1101 ///#include "sl_sleeptimer.h"
1102 ///
1103 ///void my_timer_callback(sl_sleeptimer_timer_handle_t *handle, void *data)
1104 ///{
1105 ///  //Code executed when the timer expire.
1106 ///}
1107 ///
1108 ///int start_timer(void)
1109 ///{
1110 ///  sl_status_t status;
1111 ///  sl_sleeptimer_timer_handle_t my_timer;
1112 ///  uint32_t timer_timeout = 300;
1113 ///
1114 ///  // We assume the sleeptimer is initialized properly
1115 ///
1116 ///  status = sl_sleeptimer_start_timer(&my_timer,
1117 ///                                     timer_timeout,
1118 ///                                     my_timer_callback,
1119 ///                                     (void *)NULL,
1120 ///                                     0,
1121 ///                                     0);
1122 ///  if(status != SL_STATUS_OK) {
1123 ///    return -1;
1124 ///  }
1125 ///  return 1;
1126 ///}
1127 ///   @endcode
1128 ///
1129 /// @} (end addtogroup sleeptimer)
1130 
1131 #endif // SL_SLEEPTIMER_H
1132