1 /**************************************************************************//**
\r
5 * @brief EnHanced Input Capture Timer(ECAP) driver header file
\r
8 * Copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.
\r
9 *****************************************************************************/
\r
20 /** @addtogroup Standard_Driver Standard Driver
\r
24 /** @addtogroup ECAP_Driver ECAP Driver
\r
28 /** @addtogroup ECAP_EXPORTED_CONSTANTS ECAP Exported Constants
\r
32 #define ECAP_IC0 (0UL) /*!< ECAP IC0 Unit \hideinitializer */
\r
33 #define ECAP_IC1 (1UL) /*!< ECAP IC1 Unit \hideinitializer */
\r
34 #define ECAP_IC2 (2UL) /*!< ECAP IC2 Unit \hideinitializer */
\r
36 /*---------------------------------------------------------------------------------------------------------*/
\r
37 /* ECAP CTL0 constant definitions */
\r
38 /*---------------------------------------------------------------------------------------------------------*/
\r
39 #define ECAP_NOISE_FILTER_CLKDIV_1 (0UL<<ECAP_CTL0_NFCLKSEL_Pos) /*!< Noise filter clock divide by 1 \hideinitializer */
\r
40 #define ECAP_NOISE_FILTER_CLKDIV_2 (1UL<<ECAP_CTL0_NFCLKSEL_Pos) /*!< Noise filter clock divide by 2 \hideinitializer */
\r
41 #define ECAP_NOISE_FILTER_CLKDIV_4 (2UL<<ECAP_CTL0_NFCLKSEL_Pos) /*!< Noise filter clock divide by 4 \hideinitializer */
\r
42 #define ECAP_NOISE_FILTER_CLKDIV_16 (3UL<<ECAP_CTL0_NFCLKSEL_Pos) /*!< Noise filter clock divide by 16 \hideinitializer */
\r
43 #define ECAP_NOISE_FILTER_CLKDIV_32 (4UL<<ECAP_CTL0_NFCLKSEL_Pos) /*!< Noise filter clock divide by 32 \hideinitializer */
\r
44 #define ECAP_NOISE_FILTER_CLKDIV_64 (5UL<<ECAP_CTL0_NFCLKSEL_Pos) /*!< Noise filter clock divide by 64 \hideinitializer */
\r
47 #define ECAP_CAP_INPUT_SRC_FROM_IC (0UL) /*!< CAP input source from IC \hideinitializer */
\r
48 #define ECAP_CAP_INPUT_SRC_FROM_CH (2UL) /*!< CAP input source from CH of QEI \hideinitializer */
\r
50 #define ECAP_DISABLE_COMPARE (0UL<<ECAP_CTL0_CMPEN_Pos) /*!< Input capture compare and reload function disable \hideinitializer */
\r
51 #define ECAP_COMPARE_FUNCTION (1UL<<ECAP_CTL0_CMPEN_Pos) /*!< Input capture compare function \hideinitializer */
\r
52 /*---------------------------------------------------------------------------------------------------------*/
\r
53 /* ECAP CTL1 constant definitions */
\r
54 /*---------------------------------------------------------------------------------------------------------*/
\r
55 #define ECAP_RISING_EDGE (0UL<<ECAP_CTL1_EDGESEL0_Pos) /*!< ECAP capture rising edge selection \hideinitializer */
\r
56 #define ECAP_FALLING_EDGE (1UL<<ECAP_CTL1_EDGESEL0_Pos) /*!< ECAP capture falling edge selection \hideinitializer */
\r
57 #define ECAP_RISING_FALLING_EDGE (2UL<<ECAP_CTL1_EDGESEL0_Pos) /*!< ECAP capture either rising or falling edge selection \hideinitializer */
\r
59 #define ECAP_CAPTURE_TIMER_CLKDIV_1 (0UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 1 \hideinitializer */
\r
60 #define ECAP_CAPTURE_TIMER_CLKDIV_4 (1UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 4 \hideinitializer */
\r
61 #define ECAP_CAPTURE_TIMER_CLKDIV_16 (2UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 16 \hideinitializer */
\r
62 #define ECAP_CAPTURE_TIMER_CLKDIV_32 (3UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 32 \hideinitializer */
\r
63 #define ECAP_CAPTURE_TIMER_CLKDIV_64 (4UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 64 \hideinitializer */
\r
64 #define ECAP_CAPTURE_TIMER_CLKDIV_96 (5UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 96 \hideinitializer */
\r
65 #define ECAP_CAPTURE_TIMER_CLKDIV_112 (6UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 112 \hideinitializer */
\r
66 #define ECAP_CAPTURE_TIMER_CLKDIV_128 (7UL<<ECAP_CTL1_CLKSEL_Pos) /*!< ECAP capture timer clock divide by 128 \hideinitializer */
\r
68 #define ECAP_CAPTURE_TIMER_CLK_SRC_CAP_CLK (0UL<<ECAP_CTL1_CNTSRCSEL_Pos) /*!< ECAP capture timer/clock source from CAP_CLK \hideinitializer */
\r
69 #define ECAP_CAPTURE_TIMER_CLK_SRC_CAP0 (1UL<<ECAP_CTL1_CNTSRCSEL_Pos) /*!< ECAP capture timer/clock source from CAP0 \hideinitializer */
\r
70 #define ECAP_CAPTURE_TIMER_CLK_SRC_CAP1 (2UL<<ECAP_CTL1_CNTSRCSEL_Pos) /*!< ECAP capture timer/clock source from CAP1 \hideinitializer */
\r
71 #define ECAP_CAPTURE_TIMER_CLK_SRC_CAP2 (3UL<<ECAP_CTL1_CNTSRCSEL_Pos) /*!< ECAP capture timer/clock source from CAP2 \hideinitializer */
\r
73 /*@}*/ /* end of group ECAP_EXPORTED_CONSTANTS */
\r
75 /** @addtogroup ECAP_EXPORTED_FUNCTIONS ECAP Exported Functions
\r
80 * @brief This macro is used to select noise filter clock pre-divide number
\r
81 * @param[in] ecap Specify ECAP port
\r
82 * @param[in] u32ClkSel The noise filter clock divide number
\r
83 * - \ref ECAP_NOISE_FILTER_CLKDIV_1
\r
84 * - \ref ECAP_NOISE_FILTER_CLKDIV_2
\r
85 * - \ref ECAP_NOISE_FILTER_CLKDIV_4
\r
86 * - \ref ECAP_NOISE_FILTER_CLKDIV_16
\r
87 * - \ref ECAP_NOISE_FILTER_CLKDIV_32
\r
88 * - \ref ECAP_NOISE_FILTER_CLKDIV_64
\r
90 * @details This macro will set the sampling frequency of the noise filter cock.
\r
93 #define ECAP_SET_NOISE_FILTER_CLKDIV(ecap, u32ClkSel) ((ecap)->CTL0 = ((ecap)->CTL0 & ~ECAP_CTL0_NFCLKSEL_Msk)|(u32ClkSel))
\r
96 * @brief This macro is used to disable noise filter
\r
97 * @param[in] ecap Specify ECAP port
\r
99 * @details This macro will disable the noise filter of input capture.
\r
102 #define ECAP_NOISE_FILTER_DISABLE(ecap) ((ecap)->CTL0 |= ECAP_CTL0_CAPNFDIS_Msk)
\r
105 * @brief This macro is used to enable noise filter
\r
106 * @param[in] ecap Specify ECAP port
\r
107 * @param[in] u32ClkSel Select noise filter clock divide number
\r
108 * - \ref ECAP_NOISE_FILTER_CLKDIV_1
\r
109 * - \ref ECAP_NOISE_FILTER_CLKDIV_2
\r
110 * - \ref ECAP_NOISE_FILTER_CLKDIV_4
\r
111 * - \ref ECAP_NOISE_FILTER_CLKDIV_16
\r
112 * - \ref ECAP_NOISE_FILTER_CLKDIV_32
\r
113 * - \ref ECAP_NOISE_FILTER_CLKDIV_64
\r
115 * @details This macro will enable the noise filter of input capture and set noise filter clock divide.
\r
118 #define ECAP_NOISE_FILTER_ENABLE(ecap, u32ClkSel) ((ecap)->CTL0 = ((ecap)->CTL0 & ~(ECAP_CTL0_CAPNFDIS_Msk|ECAP_CTL0_NFCLKSEL_Msk))|(u32ClkSel))
\r
121 * @brief This macro is used to enable input channel unit
\r
122 * @param[in] ecap Specify ECAP port
\r
123 * @param[in] u32Mask The input channel mask
\r
124 * - \ref ECAP_CTL0_IC0EN_Msk
\r
125 * - \ref ECAP_CTL0_IC1EN_Msk
\r
126 * - \ref ECAP_CTL0_IC2EN_Msk
\r
128 * @details This macro will enable the input channel_n to input capture.
\r
131 #define ECAP_ENABLE_INPUT_CHANNEL(ecap, u32Mask) ((ecap)->CTL0 |= (u32Mask))
\r
134 * @brief This macro is used to disable input channel unit
\r
135 * @param[in] ecap Specify ECAP port
\r
136 * @param[in] u32Mask The input channel mask
\r
137 * - \ref ECAP_CTL0_IC0EN_Msk
\r
138 * - \ref ECAP_CTL0_IC1EN_Msk
\r
139 * - \ref ECAP_CTL0_IC2EN_Msk
\r
141 * @details This macro will disable the input channel_n to input capture.
\r
144 #define ECAP_DISABLE_INPUT_CHANNEL(ecap, u32Mask) ((ecap)->CTL0 &= ~(u32Mask))
\r
147 * @brief This macro is used to select input channel source
\r
148 * @param[in] ecap Specify ECAP port
\r
149 * @param[in] u32Index The input channel number
\r
153 * @param[in] u32Src The input source
\r
154 * - \ref ECAP_CAP_INPUT_SRC_FROM_IC
\r
155 * - \ref ECAP_CAP_INPUT_SRC_FROM_CH
\r
157 * @details This macro will select the input source from ICx, CHx.
\r
160 #define ECAP_SEL_INPUT_SRC(ecap, u32Index, u32Src) ((ecap)->CTL0 = ((ecap)->CTL0 & ~(ECAP_CTL0_CAPSEL0_Msk<<((u32Index)<<1)))|(((u32Src)<<ECAP_CTL0_CAPSEL0_Pos)<<((u32Index)<<1)))
\r
163 * @brief This macro is used to enable input channel interrupt
\r
164 * @param[in] ecap Specify ECAP port
\r
165 * @param[in] u32Mask The input channel mask
\r
166 * - \ref ECAP_CTL0_CAPIEN0_Msk
\r
167 * - \ref ECAP_CTL0_CAPIEN1_Msk
\r
168 * - \ref ECAP_CTL0_CAPIEN2_Msk
\r
170 * @details This macro will enable the input channel_n interrupt.
\r
173 #define ECAP_ENABLE_INT(ecap, u32Mask) ((ecap)->CTL0 |= (u32Mask))
\r
176 * @brief This macro is used to disable input channel interrupt
\r
177 * @param[in] ecap Specify ECAP port
\r
178 * @param[in] u32Mask The input channel mask
\r
183 * @details This macro will disable the input channel_n interrupt.
\r
186 #define ECAP_DISABLE_INT(ecap, u32Mask) ((ecap)->CTL0 &= ~(u32Mask))
\r
189 * @brief This macro is used to enable input channel overflow interrupt
\r
190 * @param[in] ecap Specify ECAP port
\r
192 * @details This macro will enable the input channel overflow interrupt.
\r
195 #define ECAP_ENABLE_OVF_INT(ecap) ((ecap)->CTL0 |= ECAP_CTL0_OVIEN_Msk)
\r
198 * @brief This macro is used to disable input channel overflow interrupt
\r
199 * @param[in] ecap Specify ECAP port
\r
201 * @details This macro will disable the input channel overflow interrupt.
\r
204 #define ECAP_DISABLE_OVF_INT(ecap) ((ecap)->CTL0 &= ~ECAP_CTL0_OVIEN_Msk)
\r
207 * @brief This macro is used to enable input channel compare-match interrupt
\r
208 * @param[in] ecap Specify ECAP port
\r
210 * @details This macro will enable the input channel compare-match interrupt.
\r
213 #define ECAP_ENABLE_CMP_MATCH_INT(ecap) ((ecap)->CTL0 |= ECAP_CTL0_CMPIEN_Msk)
\r
216 * @brief This macro is used to disable input channel compare-match interrupt
\r
217 * @param[in] ecap Specify ECAP port
\r
219 * @details This macro will disable the input channel compare-match interrupt.
\r
222 #define ECAP_DISABLE_CMP_MATCH_INT(ecap) ((ecap)->CTL0 &= ~ECAP_CTL0_CMPIEN_Msk)
\r
225 * @brief This macro is used to start capture counter
\r
226 * @param[in] ecap Specify ECAP port
\r
228 * @details This macro will start capture counter up-counting.
\r
231 #define ECAP_CNT_START(ecap) ((ecap)->CTL0 |= ECAP_CTL0_CNTEN_Msk)
\r
234 * @brief This macro is used to stop capture counter
\r
235 * @param[in] ecap Specify ECAP port
\r
237 * @details This macro will stop capture counter up-counting.
\r
240 #define ECAP_CNT_STOP(ecap) ((ecap)->CTL0 &= ~ECAP_CTL0_CNTEN_Msk)
\r
243 * @brief This macro is used to set event to clear capture counter
\r
244 * @param[in] ecap Specify ECAP port
\r
245 * @param[in] u32Event The input channel number
\r
246 * - \ref ECAP_CTL0_CMPCLREN_Msk
\r
247 * - \ref ECAP_CTL1_CAP0RLDEN_Msk
\r
248 * - \ref ECAP_CTL1_CAP1RLDEN_Msk
\r
249 * - \ref ECAP_CTL1_CAP2RLDEN_Msk
\r
250 * - \ref ECAP_CTL1_OVRLDEN_Msk
\r
253 * @details This macro will enable and select compare or capture event that can clear capture counter.
\r
256 #define ECAP_SET_CNT_CLEAR_EVENT(ecap, u32Event) do{ \
\r
257 if((u32Event) & ECAP_CTL0_CMPCLREN_Msk) \
\r
258 (ecap)->CTL0 |= ECAP_CTL0_CMPCLREN_Msk; \
\r
260 (ecap)->CTL0 &= ~ECAP_CTL0_CMPCLREN_Msk; \
\r
261 (ecap)->CTL1 = ((ecap)->CTL1 &~0xF00) | ((u32Event) & 0xF00); \
\r
265 * @brief This macro is used to enable compare function
\r
266 * @param[in] ecap Specify ECAP port
\r
268 * @details This macro will enable the compare function.
\r
271 #define ECAP_ENABLE_CMP(ecap) ((ecap)->CTL0 |= ECAP_CTL0_CMPEN_Msk)
\r
274 * @brief This macro is used to disable compare function
\r
275 * @param[in] ecap Specify ECAP port
\r
277 * @details This macro will disable the compare function.
\r
280 #define ECAP_DISABLE_CMP(ecap) ((ecap)->CTL0 &= ~ECAP_CTL0_CMPEN_Msk)
\r
283 * @brief This macro is used to enable input capture function.
\r
284 * @param[in] ecap Specify ECAP port
\r
286 * @details This macro will enable input capture timer/counter.
\r
289 #define ECAP_ENABLE_CNT(ecap) ((ecap)->CTL0 |= ECAP_CTL0_CAPEN_Msk)
\r
292 * @brief This macro is used to disable input capture function.
\r
293 * @param[in] ecap Specify ECAP port
\r
295 * @details This macro will disable input capture timer/counter.
\r
298 #define ECAP_DISABLE_CNT(ecap) ((ecap)->CTL0 &= ~ECAP_CTL0_CAPEN_Msk)
\r
301 * @brief This macro is used to select input channel edge detection
\r
302 * @param[in] ecap Specify ECAP port
\r
303 * @param[in] u32Index The input channel number
\r
307 * @param[in] u32Edge The input source
\r
308 * - \ref ECAP_RISING_EDGE
\r
309 * - \ref ECAP_FALLING_EDGE
\r
310 * - \ref ECAP_RISING_FALLING_EDGE
\r
312 * @details This macro will select input capture can detect falling edge, rising edge or either rising or falling edge change.
\r
315 #define ECAP_SEL_CAPTURE_EDGE(ecap, u32Index, u32Edge) ((ecap)->CTL1 = ((ecap)->CTL1 & ~(ECAP_CTL1_EDGESEL0_Msk<<((u32Index)<<1)))|((u32Edge)<<((u32Index)<<1)))
\r
318 * @brief This macro is used to select ECAP counter reload trigger source
\r
319 * @param[in] ecap Specify ECAP port
\r
320 * @param[in] u32TrigSrc The input source
\r
321 * - \ref ECAP_CTL1_CAP0RLDEN_Msk
\r
322 * - \ref ECAP_CTL1_CAP1RLDEN_Msk
\r
323 * - \ref ECAP_CTL1_CAP2RLDEN_Msk
\r
324 * - \ref ECAP_CTL1_OVRLDEN_Msk
\r
326 * @details This macro will select capture counter reload trigger source.
\r
329 #define ECAP_SEL_RELOAD_TRIG_SRC(ecap, u32TrigSrc) ((ecap)->CTL1 = ((ecap)->CTL1 & ~0xF00)|(u32TrigSrc))
\r
332 * @brief This macro is used to select capture timer clock divide.
\r
333 * @param[in] ecap Specify ECAP port
\r
334 * @param[in] u32Clkdiv The input source
\r
335 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_1
\r
336 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_4
\r
337 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_16
\r
338 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_32
\r
339 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_64
\r
340 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_96
\r
341 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_112
\r
342 * - \ref ECAP_CAPTURE_TIMER_CLKDIV_128
\r
344 * @details This macro will select capture timer clock has a pre-divider with eight divided option.
\r
347 #define ECAP_SEL_TIMER_CLK_DIV(ecap, u32Clkdiv) ((ecap)->CTL1 = ((ecap)->CTL1 & ~ECAP_CTL1_CLKSEL_Msk)|(u32Clkdiv))
\r
350 * @brief This macro is used to select capture timer/counter clock source
\r
351 * @param[in] ecap Specify ECAP port
\r
352 * @param[in] u32ClkSrc The input source
\r
353 * - \ref ECAP_CAPTURE_TIMER_CLK_SRC_CAP_CLK
\r
354 * - \ref ECAP_CAPTURE_TIMER_CLK_SRC_CAP0
\r
355 * - \ref ECAP_CAPTURE_TIMER_CLK_SRC_CAP1
\r
356 * - \ref ECAP_CAPTURE_TIMER_CLK_SRC_CAP2
\r
358 * @details This macro will select capture timer/clock clock source.
\r
361 #define ECAP_SEL_TIMER_CLK_SRC(ecap, u32ClkSrc) ((ecap)->CTL1 = ((ecap)->CTL1 & ~ECAP_CTL1_CNTSRCSEL_Msk)|(u32ClkSrc))
\r
364 * @brief This macro is used to read input capture status
\r
365 * @param[in] ecap Specify ECAP port
\r
366 * @return Input capture status flags
\r
367 * @details This macro will get the input capture interrupt status.
\r
370 #define ECAP_GET_INT_STATUS(ecap) ((ecap)->STATUS)
\r
373 * @brief This macro is used to get input channel interrupt flag
\r
374 * @param[in] ecap Specify ECAP port
\r
375 * @param[in] u32Mask The input channel mask
\r
376 * - \ref ECAP_STATUS_CAPTF0_Msk
\r
377 * - \ref ECAP_STATUS_CAPTF1_Msk
\r
378 * - \ref ECAP_STATUS_CAPTF2_Msk
\r
379 * - \ref ECAP_STATUS_CAPOVF_Msk
\r
380 * - \ref ECAP_STATUS_CAPCMPF_Msk
\r
382 * @details This macro will write 1 to get the input channel_n interrupt flag.
\r
385 #define ECAP_GET_CAPTURE_FLAG(ecap, u32Mask) (((ecap)->STATUS & (u32Mask))?1:0)
\r
388 * @brief This macro is used to clear input channel interrupt flag
\r
389 * @param[in] ecap Specify ECAP port
\r
390 * @param[in] u32Mask The input channel mask
\r
391 * - \ref ECAP_STATUS_CAPTF0_Msk
\r
392 * - \ref ECAP_STATUS_CAPTF1_Msk
\r
393 * - \ref ECAP_STATUS_CAPTF2_Msk
\r
394 * - \ref ECAP_STATUS_CAPOVF_Msk
\r
395 * - \ref ECAP_STATUS_CAPCMPF_Msk
\r
397 * @details This macro will write 1 to clear the input channel_n interrupt flag.
\r
400 #define ECAP_CLR_CAPTURE_FLAG(ecap, u32Mask) ((ecap)->STATUS = (u32Mask))
\r
403 * @brief This macro is used to set input capture counter value
\r
404 * @param[in] ecap Specify ECAP port
\r
405 * @param[in] u32Val Counter value
\r
407 * @details This macro will set a counter value of input capture.
\r
410 #define ECAP_SET_CNT_VALUE(ecap, u32Val) ((ecap)->CNT = (u32Val))
\r
413 * @brief This macro is used to get input capture counter value
\r
414 * @param[in] ecap Specify ECAP port
\r
415 * @return Capture counter value
\r
416 * @details This macro will get a counter value of input capture.
\r
419 #define ECAP_GET_CNT_VALUE(ecap) ((ecap)->CNT)
\r
422 * @brief This macro is used to get input capture counter hold value
\r
423 * @param[in] ecap Specify ECAP port
\r
424 * @param[in] u32Index The input channel number
\r
428 * @return Capture counter hold value
\r
429 * @details This macro will get a hold value of input capture channel_n.
\r
432 #define ECAP_GET_CNT_HOLD_VALUE(ecap, u32Index) (*(__IO uint32_t *) (&((ecap)->HLD0) + (u32Index)))
\r
435 * @brief This macro is used to set input capture counter compare value
\r
436 * @param[in] ecap Specify ECAP port
\r
437 * @param[in] u32Val Input capture compare value
\r
439 * @details This macro will set a compare value of input capture counter.
\r
442 #define ECAP_SET_CNT_CMP(ecap, u32Val) ((ecap)->CNTCMP = (u32Val))
\r
444 void ECAP_Open(ECAP_T* ecap, uint32_t u32FuncMask);
\r
445 void ECAP_Close(ECAP_T* ecap);
\r
446 void ECAP_EnableINT(ECAP_T* ecap, uint32_t u32Mask);
\r
447 void ECAP_DisableINT(ECAP_T* ecap, uint32_t u32Mask);
\r
448 /*@}*/ /* end of group ECAP_EXPORTED_FUNCTIONS */
\r
450 /*@}*/ /* end of group ECAP_Driver */
\r
452 /*@}*/ /* end of group Standard_Driver */
\r
458 #endif /*__ECAP_H__*/
\r
460 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
\r