1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_EWM_H_
9 #define _FSL_EWM_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup ewm
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  *******************************************************************************/
21 
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief EWM driver version 2.0.3. */
25 #define FSL_EWM_DRIVER_VERSION (MAKE_VERSION(2, 0, 3))
26 /*@}*/
27 
28 /*! @brief Describes EWM clock source. */
29 #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
30 typedef enum _ewm_lpo_clock_source
31 {
32     kEWM_LpoClockSource0 = 0U, /*!< EWM clock sourced from lpo_clk[0]*/
33     kEWM_LpoClockSource1 = 1U, /*!< EWM clock sourced from lpo_clk[1]*/
34     kEWM_LpoClockSource2 = 2U, /*!< EWM clock sourced from lpo_clk[2]*/
35     kEWM_LpoClockSource3 = 3U, /*!< EWM clock sourced from lpo_clk[3]*/
36 } ewm_lpo_clock_source_t;
37 #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT */
38 
39 /*!
40  * @brief Data structure for EWM configuration.
41  *
42  * This structure is used to configure the EWM.
43  */
44 typedef struct _ewm_config
45 {
46     bool enableEwm;           /*!< Enable EWM module */
47     bool enableEwmInput;      /*!< Enable EWM_in input */
48     bool setInputAssertLogic; /*!< EWM_in signal assertion state */
49     bool enableInterrupt;     /*!< Enable EWM interrupt */
50 #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
51     ewm_lpo_clock_source_t clockSource; /*!< Clock source select */
52 #endif                                  /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT */
53 #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
54     uint8_t prescaler;        /*!< Clock prescaler value */
55 #endif                        /* FSL_FEATURE_EWM_HAS_PRESCALER */
56     uint8_t compareLowValue;  /*!< Compare low-register value */
57     uint8_t compareHighValue; /*!< Compare high-register value */
58 } ewm_config_t;
59 
60 /*!
61  * @brief EWM interrupt configuration structure with default settings all disabled.
62  *
63  * This structure contains the settings for all of EWM interrupt configurations.
64  */
65 enum _ewm_interrupt_enable_t
66 {
67     kEWM_InterruptEnable = EWM_CTRL_INTEN_MASK, /*!< Enable the EWM to generate an interrupt*/
68 };
69 
70 /*!
71  * @brief EWM status flags.
72  *
73  * This structure contains the constants for the EWM status flags for use in the EWM functions.
74  */
75 enum _ewm_status_flags_t
76 {
77     kEWM_RunningFlag = EWM_CTRL_EWMEN_MASK, /*!< Running flag, set when EWM is enabled*/
78 };
79 
80 /*******************************************************************************
81  * API
82  *******************************************************************************/
83 
84 #if defined(__cplusplus)
85 extern "C" {
86 #endif /* __cplusplus */
87 
88 /*!
89  * @name EWM initialization and de-initialization
90  * @{
91  */
92 
93 /*!
94  * @brief Initializes the EWM peripheral.
95  *
96  * This function is used to initialize the EWM. After calling, the EWM
97  * runs immediately according to the configuration.
98  * Note that, except for the interrupt enable control bit, other control bits and registers are write once after a
99  * CPU reset. Modifying them more than once generates a bus transfer error.
100  *
101  * This is an example.
102  * @code
103  *   ewm_config_t config;
104  *   EWM_GetDefaultConfig(&config);
105  *   config.compareHighValue = 0xAAU;
106  *   EWM_Init(ewm_base,&config);
107  * @endcode
108  *
109  * @param base EWM peripheral base address
110  * @param config The configuration of the EWM
111  */
112 void EWM_Init(EWM_Type *base, const ewm_config_t *config);
113 
114 /*!
115  * @brief Deinitializes the EWM peripheral.
116  *
117  * This function is used to shut down the EWM.
118  *
119  * @param base EWM peripheral base address
120  */
121 void EWM_Deinit(EWM_Type *base);
122 
123 /*!
124  * @brief Initializes the EWM configuration structure.
125  *
126  * This function initializes the EWM configuration structure to default values. The default
127  * values are as follows.
128  * @code
129  *   ewmConfig->enableEwm = true;
130  *   ewmConfig->enableEwmInput = false;
131  *   ewmConfig->setInputAssertLogic = false;
132  *   ewmConfig->enableInterrupt = false;
133  *   ewmConfig->ewm_lpo_clock_source_t = kEWM_LpoClockSource0;
134  *   ewmConfig->prescaler = 0;
135  *   ewmConfig->compareLowValue = 0;
136  *   ewmConfig->compareHighValue = 0xFEU;
137  * @endcode
138  *
139  * @param config Pointer to the EWM configuration structure.
140  * @see ewm_config_t
141  */
142 void EWM_GetDefaultConfig(ewm_config_t *config);
143 
144 /* @} */
145 
146 /*!
147  * @name EWM functional Operation
148  * @{
149  */
150 
151 /*!
152  * @brief Enables the EWM interrupt.
153  *
154  * This function enables the EWM interrupt.
155  *
156  * @param base EWM peripheral base address
157  * @param mask The interrupts to enable
158  *        The parameter can be combination of the following source if defined
159  *        @arg kEWM_InterruptEnable
160  */
EWM_EnableInterrupts(EWM_Type * base,uint32_t mask)161 static inline void EWM_EnableInterrupts(EWM_Type *base, uint32_t mask)
162 {
163     base->CTRL |= (uint8_t)mask;
164 }
165 
166 /*!
167  * @brief Disables the EWM interrupt.
168  *
169  * This function enables the EWM interrupt.
170  *
171  * @param base EWM peripheral base address
172  * @param mask The interrupts to disable
173  *        The parameter can be combination of the following source if defined
174  *        @arg kEWM_InterruptEnable
175  */
EWM_DisableInterrupts(EWM_Type * base,uint32_t mask)176 static inline void EWM_DisableInterrupts(EWM_Type *base, uint32_t mask)
177 {
178     base->CTRL &= (uint8_t)(~mask);
179 }
180 
181 /*!
182  * @brief Gets all status flags.
183  *
184  * This function gets all status flags.
185  *
186  * This is an example for getting the running flag.
187  * @code
188  *   uint32_t status;
189  *   status = EWM_GetStatusFlags(ewm_base) & kEWM_RunningFlag;
190  * @endcode
191  * @param base        EWM peripheral base address
192  * @return            State of the status flag: asserted (true) or not-asserted (false).@see _ewm_status_flags_t
193  *                    - True: a related status flag has been set.
194  *                    - False: a related status flag is not set.
195  */
EWM_GetStatusFlags(EWM_Type * base)196 static inline uint32_t EWM_GetStatusFlags(EWM_Type *base)
197 {
198     return ((uint32_t)base->CTRL & EWM_CTRL_EWMEN_MASK);
199 }
200 
201 /*!
202  * @brief Services the EWM.
203  *
204  * This function resets the EWM counter to zero.
205  *
206  * @param base EWM peripheral base address
207  */
208 void EWM_Refresh(EWM_Type *base);
209 
210 /*@}*/
211 
212 #if defined(__cplusplus)
213 }
214 #endif /* __cplusplus */
215 
216 /*! @}*/
217 
218 #endif /* _FSL_EWM_H_ */
219