1 /*
2 * Copyright 2018 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7 #ifndef _FSL_WKT_H_
8 #define _FSL_WKT_H_
9
10 #include "fsl_common.h"
11
12 /*!
13 * @addtogroup wkt
14 * @{
15 */
16
17 /*! @file */
18
19 /*******************************************************************************
20 * Definitions
21 ******************************************************************************/
22
23 /*! @name Driver version */
24 /*@{*/
25 #define FSL_WKT_DRIVER_VERSION (MAKE_VERSION(2, 0, 2)) /*!< Version 2.0.2 */
26 /*@}*/
27
28 /*! @brief Describes WKT clock source. */
29 typedef enum _wkt_clock_source
30 {
31 kWKT_DividedFROClockSource = 0U, /*!< WKT clock sourced from the divided FRO clock */
32 kWKT_LowPowerClockSource = 1U, /*!< WKT clock sourced from the Low power clock
33 Use this clock, LPOSCEN bit of DPDCTRL register must be enabled */
34 kWKT_ExternalClockSource = 2U, /*!< WKT clock sourced from the Low power clock
35 Use this clock, WAKECLKPAD_DISABLE bit of DPDCTRL register must be enabled */
36 } wkt_clock_source_t;
37
38 /*! @brief Describes WKT configuration structure. */
39 typedef struct _wkt_config
40 {
41 wkt_clock_source_t clockSource; /*!< External or internal clock source select */
42 } wkt_config_t;
43
44 /*! @brief List of WKT flags */
45 typedef enum _wkt_status_flags
46 {
47 kWKT_AlarmFlag = WKT_CTRL_ALARMFLAG_MASK, /*!< Alarm flag */
48 } wkt_status_flags_t;
49
50 /*******************************************************************************
51 * API
52 ******************************************************************************/
53
54 #if defined(__cplusplus)
55 extern "C" {
56 #endif
57
58 /*!
59 * @name Initialization and deinitialization
60 * @{
61 */
62
63 /*!
64 * @brief Ungates the WKT clock and configures the peripheral for basic operation.
65 *
66 * @note This API should be called at the beginning of the application using the WKT driver.
67 *
68 * @param base WKT peripheral base address
69 * @param config Pointer to user's WKT config structure.
70 */
71 void WKT_Init(WKT_Type *base, const wkt_config_t *config);
72
73 /*!
74 * @brief Gate the WKT clock
75 *
76 * @param base WKT peripheral base address
77 */
78 void WKT_Deinit(WKT_Type *base);
79
80 /*!
81 * @brief Initializes the WKT configuration structure.
82 *
83 * This function initializes the WKT configuration structure to default values. The default
84 * values are as follows.
85 * @code
86 * config->clockSource = kWKT_DividedFROClockSource;
87 * @endcode
88 *
89 * @param config Pointer to the WKT configuration structure.
90 * @see wkt_config_t
91 */
WKT_GetDefaultConfig(wkt_config_t * config)92 static inline void WKT_GetDefaultConfig(wkt_config_t *config)
93 {
94 assert(config);
95
96 /* Select divided FRO clock. */
97 config->clockSource = kWKT_DividedFROClockSource;
98 }
99
100 /*! @}*/
101
102 /*!
103 * @name Read the counter value.
104 * @{
105 */
106
107 /*!
108 * @brief Read actual WKT counter value.
109 *
110 * @param base WKT peripheral base address
111 */
WKT_GetCounterValue(WKT_Type * base)112 static inline uint32_t WKT_GetCounterValue(WKT_Type *base)
113 {
114 /* Get the counter. */
115 return (base->COUNT);
116 }
117
118 /*! @}*/
119
120 /*!
121 * @name Status Interface
122 * @{
123 */
124
125 /*!
126 * @brief Gets the WKT status flags
127 *
128 * @param base WKT peripheral base address
129 *
130 * @return The status flags. This is the logical OR of members of the
131 * enumeration ::wkt_status_flags_t
132 */
WKT_GetStatusFlags(WKT_Type * base)133 static inline uint32_t WKT_GetStatusFlags(WKT_Type *base)
134 {
135 return (base->CTRL & WKT_CTRL_ALARMFLAG_MASK);
136 }
137
138 /*!
139 * @brief Clears the WKT status flags.
140 *
141 * @param base WKT peripheral base address
142 * @param mask The status flags to clear. This is a logical OR of members of the
143 * enumeration ::wkt_status_flags_t
144 */
WKT_ClearStatusFlags(WKT_Type * base,uint32_t mask)145 static inline void WKT_ClearStatusFlags(WKT_Type *base, uint32_t mask)
146 {
147 base->CTRL |= mask;
148 }
149
150 /*! @}*/
151
152 /*!
153 * @name Timer Start and Stop
154 * @{
155 */
156
157 /*!
158 * @brief Starts the timer counting.
159 *
160 * After calling this function, timer loads a count value, counts down to 0, then stops.
161 *
162 * @note User can call the utility macros provided in fsl_common.h to convert to ticks
163 * Do not write to Counter register while the counting is in progress
164 *
165 * @param base WKT peripheral base address.
166 * @param count The value to be loaded into the WKT Count register
167 */
WKT_StartTimer(WKT_Type * base,uint32_t count)168 static inline void WKT_StartTimer(WKT_Type *base, uint32_t count)
169 {
170 /* Set the start count value */
171 base->COUNT = count;
172 }
173
174 /*!
175 * @brief Stops the timer counting.
176 *
177 * This function Clears the counter and stops the timer from counting.
178 *
179 * @param base WKT peripheral base address
180 */
WKT_StopTimer(WKT_Type * base)181 static inline void WKT_StopTimer(WKT_Type *base)
182 {
183 /* Clear the counter and stop the timer immediately */
184 base->CTRL = (base->CTRL & (~WKT_CTRL_ALARMFLAG_MASK)) | WKT_CTRL_CLEARCTR_MASK;
185 }
186
187 /*! @}*/
188
189 #if defined(__cplusplus)
190 }
191 #endif
192
193 /*! @}*/
194
195 #endif /* _FSL_WKT_H_ */
196