1 /*
2 * Copyright 2023 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7 #ifndef FSL_WAKETIMER_H_
8 #define FSL_WAKETIMER_H_
9
10 #include "fsl_common.h"
11
12 /*!
13 * @addtogroup waketimer
14 * @{
15 */
16
17 /*! @file*/
18
19 /*******************************************************************************
20 * Definitions
21 ******************************************************************************/
22
23 /*! @name Driver version */
24 /*! @{ */
25 /*! @brief WAKETIMER driver version. */
26 #define FSL_WAKETIMER_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
27 /*! @} */
28
29 /*!
30 * @brief WAKETIMER status flags.
31 */
32 enum _waketimer_status_flags
33 {
34 kWAKETIMER_WakeFlag =
35 (WAKETIMER_WAKE_TIMER_CTRL_WAKE_FLAG_MASK), /*!< Wake Timer Status Flag, sets wake timer has timed out. */
36 };
37
38 /*!
39 * @brief Define interrupt switchers of the module.
40 */
41 enum _waketimer_interrupt_enable
42 {
43 kWAKETIMER_WakeInterruptEnable = WAKETIMER_WAKE_TIMER_CTRL_INTR_EN_MASK, /*!< Generate interrupt
44 requests when WAKE_FLAG is asserted. */
45 };
46
47 /*! @brief waketimer callback function. */
48 typedef void (*waketimer_callback_t)(void);
49
50 /*!
51 * @brief WAKETIMER configuration structure
52 *
53 * This structure holds the configuration settings for the WAKETIMER peripheral. To initialize this
54 * structure to reasonable defaults, call the WAKETIMER_GetDefaultConfig() function and pass a
55 * pointer to the configuration structure instance.
56 *
57 * The configuration structure can be made constant so as to reside in flash.
58 */
59 typedef struct _waketimer_config
60 {
61 bool enableOSCDivide; /*!< true: Enable OSC Divide.
62 false: Disable OSC Divide. */
63 bool enableInterrupt; /*!< true: Enable interrupt.
64 false: Disable interrupt. */
65 waketimer_callback_t callback; /*!< timer countdown callback. */
66 } waketimer_config_t;
67
68 /*******************************************************************************
69 * API
70 ******************************************************************************/
71
72 #if defined(__cplusplus)
73 extern "C" {
74 #endif /* _cplusplus */
75
76 /*!
77 * @name Initialization and deinitialization
78 * @{
79 */
80
81 /*!
82 * @brief Initializes an WAKETIMER
83 *
84 * This function initializes the WAKETIMER.
85 *
86 * @param base WAKETIMER peripheral base address.
87 * @param config Pointer to the user configuration structure.
88 */
89 void WAKETIMER_Init(WAKETIMER_Type *base, const waketimer_config_t *config);
90
91 /*!
92 * @brief Deinitializes a WAKETIMER instance.
93 *
94 * This function deinitialize the WAKETIMER.
95 *
96 * @param base WAKETIMER peripheral base address.
97 */
98 void WAKETIMER_Deinit(WAKETIMER_Type *base);
99
100 /*!
101 * @brief Fills in the WAKETIMER configuration structure with the default settings.
102 *
103 * The default values are:
104 * @code
105 * config->enableInterrupt = true;
106 * config->enableOSCDivide = true;
107 * config->callback = NULL;
108 * @endcode
109 * @param config Pointer to the user configuration structure.
110 */
111 void WAKETIMER_GetDefaultConfig(waketimer_config_t *config);
112
113 /*! @}*/
114
115 /*!
116 * @name Interrupt Interface
117 * @{
118 */
119
120 /*!
121 * @brief Enables the selected WAKETIMER interrupts.
122 *
123 * @param base WAKETIMER peripheral base address
124 * @param mask Mask value for interrupt events. See to #_waketimer_interrupt_enable
125 */
126 void WAKETIMER_EnableInterrupts(WAKETIMER_Type *base, uint32_t mask);
127
128 /*!
129 * @brief Enables the selected WAKETIMER interrupts.
130 *
131 * @param base WAKETIMER peripheral base address
132 * @param mask Mask value for interrupt events. See to #_waketimer_interrupt_enable
133 */
134 void WAKETIMER_DisableInterrupts(WAKETIMER_Type *base, uint32_t mask);
135
136 /*!
137 * @brief Clear Status Interrupt Flag.
138 *
139 * This clears intrrupt status flag.
140 * Currently, only match interrupt flag can be cleared.
141 *
142 * @param base WAKETIMER peripheral base address.
143 * @param mask Mask value for flags to be cleared. See to #_waketimer_status_flags.
144 * @return none
145 */
146 void WAKETIMER_ClearStatusFlags(WAKETIMER_Type *base, uint32_t mask);
147
148 /*!
149 * @brief Receive noticification when waketime countdown.
150 *
151 * If the interrupt for the waketime countdown is enabled, then a callback can be registered
152 * which will be invoked when the event is triggered
153 *
154 * @param base WAKETIMER peripheral base address
155 * @param callback Function to invoke when the event is triggered
156 */
157 void WAKETIMER_SetCallback(WAKETIMER_Type *base, waketimer_callback_t callback);
158
159 /*! @}*/
160
161 /*!
162 * @name Timer Start and Stop
163 * @{
164 */
165
166 /*!
167 * @brief Halt and clear timer counter.
168 *
169 * This halt and clear timer counter.
170 *
171 * @param base WAKETIMER peripheral base address.
172 * @return none
173 */
WAKETIMER_HaltTimer(WAKETIMER_Type * base)174 static inline void WAKETIMER_HaltTimer(WAKETIMER_Type *base)
175 {
176 base->WAKE_TIMER_CTRL |= WAKETIMER_WAKE_TIMER_CTRL_CLR_WAKE_TIMER_MASK;
177 }
178
179 /*!
180 * @brief Set timer counter.
181 *
182 * This set the timer counter and start the timer countdown.
183 *
184 * @param base WAKETIMER peripheral base address.
185 * @param value countdown value.
186 * @return none
187 */
WAKETIMER_StartTimer(WAKETIMER_Type * base,uint32_t value)188 static inline void WAKETIMER_StartTimer(WAKETIMER_Type *base, uint32_t value)
189 {
190 base->WAKE_TIMER_CNT = value;
191 }
192
193 /*!
194 * @brief Get current timer count value from WAKETIMER.
195 *
196 * This function will get a decimal timer count value.
197 * The RAW value of timer count is gray code format, will be translated to decimal data internally.
198 *
199 * @param base WAKETIMER peripheral base address.
200 * @return Value of WAKETIMER which will be formated to decimal value.
201 */
202 uint32_t WAKETIMER_GetCurrentTimerValue(WAKETIMER_Type *base);
203
204 /*! @}*/
205
206 #if defined(__cplusplus)
207 }
208 #endif
209
210 /*! @}*/
211
212 #endif /* FSL_WAKETIMER_H_ */
213