1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _RTC_H_
10 #define _RTC_H_
11 
12 #define SECOND_TO_MICROSECOND (1000000ULL)
13 
14 /*!
15  * @addtogroup RTC_Adapter
16  * @{
17  */
18 
19 /************************************************************************************
20 *************************************************************************************
21 * Include
22 *************************************************************************************
23 ***********************************************************************************/
24 
25 /************************************************************************************
26 *************************************************************************************
27 * Public types
28 *************************************************************************************
29 ************************************************************************************/
30 
31 /*! @brief Hal rtc status. */
32 typedef enum _hal_rtc_status
33 {
34     kStatus_HAL_RtcSuccess = kStatus_Success, /*!< Success */
35     kStatus_HAL_RtcFail    = kStatus_Fail,    /*!< Failure*/
36 } hal_rtc_status_t;
37 
38 typedef enum _hal_rtc_interrupts
39 {
40     kHAL_RTC_AlarmInterrupt    = 1U << 0U,
41     kHAL_RTC_RolloverInterrupt = 1U << 1U,
42 } hal_rtc_interrupts_t;
43 
44 typedef enum _hal_rtc_status_flags
45 {
46     kHAL_RTC_AlarmInterruptFlag    = 1U << 0U,
47     kHAL_RTC_RolloverInterruptFlag = 1U << 1U,
48 } hal_rtc_status_flags_t;
49 
50 /*! @brief Hal rtc handle size. */
51 #define HAL_RTC_HANDLE_SIZE (8U)
52 
53 /*!
54  * @brief Defines the RTC handle
55  *
56  * This macro is used to define a 4 byte aligned rtc handle.
57  * Then use "(hal_rtc_handle_t)name" to get the rtc handle.
58  *
59  * The macro should be global and could be optional. You could also define RTC handle by yourself.
60  *
61  * This is an example,
62  * @code
63  *   HAL_RTC_HANDLE_DEFINE(rtcHandle);
64  * @endcode
65  *
66  * @param name The name string of the RTC handle.
67  */
68 #define HAL_RTC_HANDLE_DEFINE(name) uint32_t name[(HAL_RTC_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]
69 
70 /*! @brief Hal rtc handle. */
71 typedef void *hal_rtc_handle_t;
72 
73 /*! @brief The callback function of rtc */
74 typedef void (*hal_rtc_callback_t)(void *param);
75 
76 /*
77  * @brief   Enable/Disable RTC API
78  * VALID RANGE: TRUE/FALSE
79  */
80 #ifndef RTC_FUNCTIONS_ENABLE
81 #define RTC_FUNCTIONS_ENABLE (0)
82 #endif
83 
84 #ifndef RTC_LEGACY_FUNCTION_PROTOTYPE
85 #define RTC_LEGACY_FUNCTION_PROTOTYPE (0)
86 #endif
87 
88 /************************************************************************************
89 *************************************************************************************
90 * Public prototypes
91 *************************************************************************************
92 ************************************************************************************/
93 #if (defined(RTC_LEGACY_FUNCTION_PROTOTYPE) && (RTC_LEGACY_FUNCTION_PROTOTYPE > 0))
94 
95 /*! -------------------------------------------------------------------------
96  * @brief   initialize the RTC part of the timer module
97  *---------------------------------------------------------------------------*/
98 void HAL_RTCInit(void);
99 
100 /*! -------------------------------------------------------------------------
101  * @brief   Returns the absolute time at the moment of the call.
102  * @return  Absolute time at the moment of the call in microseconds.
103  *---------------------------------------------------------------------------*/
104 uint64_t HAL_RTCGetTimestamp(void);
105 
106 #endif
107 
108 #if (defined(RTC_FUNCTIONS_ENABLE) && (RTC_FUNCTIONS_ENABLE > 0))
109 
110 /*! -------------------------------------------------------------------------
111  * @brief     Sets the absolute time.
112  * @param[in] microseconds
113  *---------------------------------------------------------------------------*/
114 void HAL_RTCSetTime(uint64_t microseconds);
115 
116 /*! -------------------------------------------------------------------------
117  * @brief     Sets the alarm absolute time in microseconds.
118  * @param[in] microseconds Time in microseconds for the alarm.
119  * @param[in] callback function pointer.
120  * @param[in] param Parameter for callback.
121  *---------------------------------------------------------------------------*/
122 void HAL_RTCSetAlarm(uint64_t microseconds, hal_rtc_callback_t callback, void *param);
123 
124 /*! -------------------------------------------------------------------------
125  * @brief     Sets the alarm relative time in seconds.
126  * @param[in] seconds number of seconds until the alarm.
127  * @param[in] callback function pointer.
128  * @param[in] param Parameter for callback.
129  *---------------------------------------------------------------------------*/
130 void HAL_RTCSetAlarmRelative(uint32_t seconds, hal_rtc_callback_t callback, void *param);
131 #endif /* RTC_FUNCTIONS_ENABLE */
132 /*! @}*/
133 
134 /*! -------------------------------------------------------------------------
135  * @brief   initialize the RTC part of the timer module
136  *---------------------------------------------------------------------------*/
137 hal_rtc_status_t HAL_RtcInit(hal_rtc_handle_t halRtcHandle, uint8_t instance);
138 
139 /*! -------------------------------------------------------------------------
140  * @brief   DeInitialize the RTC part of the timer module
141  *---------------------------------------------------------------------------*/
142 void HAL_RtcDeinit(hal_rtc_handle_t halRtcHandle);
143 
144 /*! -------------------------------------------------------------------------
145  * @brief     Set the absolute time.
146  * @param[in] microseconds
147  *---------------------------------------------------------------------------*/
148 hal_rtc_status_t HAL_RtcSetTime(hal_rtc_handle_t halRtcHandle, uint64_t microseconds);
149 
150 /*! -------------------------------------------------------------------------
151  * @brief     Get the absolute time.
152  * @param[in] microseconds
153  *---------------------------------------------------------------------------*/
154 uint64_t HAL_RtcGetTime(hal_rtc_handle_t halRtcHandle);
155 
156 /*! -------------------------------------------------------------------------
157  * @brief     Set the alarm absolute time in microseconds.
158  * @param[in] microseconds Time in microseconds for the alarm.
159  *---------------------------------------------------------------------------*/
160 hal_rtc_status_t HAL_RtcSetAlarm(hal_rtc_handle_t halRtcHandle, uint64_t microseconds);
161 
162 /*! -------------------------------------------------------------------------
163  * @brief     Get the alarm absolute time in microseconds.
164  *---------------------------------------------------------------------------*/
165 uint64_t HAL_RtcGetAlarm(hal_rtc_handle_t halRtcHandle);
166 
167 uint32_t HAL_RtcGetStatusFlags(hal_rtc_handle_t halRtcHandle);
168 
169 void HAL_RtcClearStatusFlags(hal_rtc_handle_t halRtcHandle, uint32_t flags);
170 
171 uint32_t HAL_RtcGetEnabledInterrupts(hal_rtc_handle_t halRtcHandle);
172 
173 void HAL_RtcEnableInterrupts(hal_rtc_handle_t halRtcHandle, uint32_t flags);
174 
175 void HAL_RtcDisableInterrupts(hal_rtc_handle_t halRtcHandle, uint32_t flags);
176 
177 /*! @}*/
178 
179 #endif /* _RTC_H_ */
180