1 /**************************************************************************//**
2  * @file     qei.c
3  * @version  V3.00
4  * $Revision: 2 $
5  * $Date: 17/09/20 9:33a $
6  * @brief    Quadrature Encoder Interface (QEI) driver source file
7  *
8  * @copyright SPDX-License-Identifier: Apache-2.0
9  * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
10  *****************************************************************************/
11 #include "NuMicro.h"
12 
13 
14 /** @addtogroup Standard_Driver Standard Driver
15   @{
16 */
17 
18 /** @addtogroup QEI_Driver QEI Driver
19   @{
20 */
21 
22 /** @addtogroup QEI_EXPORTED_FUNCTIONS QEI Exported Functions
23   @{
24 */
25 
26 /**
27   * @brief      Close QEI function
28   * @param[in]  qei         The pointer of the specified QEI module.
29   * @return     None
30   * @details    This function reset QEI configuration and stop QEI counting.
31   */
QEI_Close(QEI_T * qei)32 void QEI_Close(QEI_T* qei)
33 {
34     /* Reset QEI configuration */
35     qei->CTL = 0UL;
36 }
37 
38 /**
39   * @brief      Disable QEI interrupt
40   * @param[in]  qei         The pointer of the specified QEI module.
41   * @param[in]  u32IntSel   Interrupt type selection.
42   *                         - \ref QEI_CTL_DIRIEN_Msk   : Direction change interrupt
43   *                         - \ref QEI_CTL_OVUNIEN_Msk  : Counter overflow or underflow interrupt
44   *                         - \ref QEI_CTL_CMPIEN_Msk   : Compare-match interrupt
45   *                         - \ref QEI_CTL_IDXIEN_Msk   : Index detected interrupt
46   * @return     None
47   * @details    This function disable QEI specified interrupt.
48   */
QEI_DisableInt(QEI_T * qei,uint32_t u32IntSel)49 void QEI_DisableInt(QEI_T* qei, uint32_t u32IntSel)
50 {
51     /* Disable QEI specified interrupt */
52     QEI_DISABLE_INT(qei, u32IntSel);
53 
54     /* Disable NVIC QEI IRQ */
55     if((qei == QEI0) || (qei == QEI0_NS))
56     {
57         NVIC_DisableIRQ(QEI0_IRQn);
58     }
59     else
60     {
61         NVIC_DisableIRQ(QEI1_IRQn);
62     }
63 }
64 
65 /**
66   * @brief      Enable QEI interrupt
67   * @param[in]  qei         The pointer of the specified QEI module.
68   * @param[in]  u32IntSel   Interrupt type selection.
69   *                         - \ref QEI_CTL_DIRIEN_Msk   : Direction change interrupt
70   *                         - \ref QEI_CTL_OVUNIEN_Msk  : Counter overflow or underflow interrupt
71   *                         - \ref QEI_CTL_CMPIEN_Msk   : Compare-match interrupt
72   *                         - \ref QEI_CTL_IDXIEN_Msk   : Index detected interrupt
73   * @return     None
74   * @details    This function enable QEI specified interrupt.
75   */
QEI_EnableInt(QEI_T * qei,uint32_t u32IntSel)76 void QEI_EnableInt(QEI_T* qei, uint32_t u32IntSel)
77 {
78     /* Enable QEI specified interrupt */
79     QEI_ENABLE_INT(qei, u32IntSel);
80 
81     /* Enable NVIC QEI IRQ */
82     if((qei == QEI0) || (qei == QEI0_NS))
83     {
84         NVIC_EnableIRQ(QEI0_IRQn);
85     }
86     else
87     {
88         NVIC_EnableIRQ(QEI1_IRQn);
89     }
90 }
91 
92 /**
93   * @brief      Open QEI in specified mode and enable input
94   * @param[in]  qei         The pointer of the specified QEI module.
95   * @param[in]  u32Mode     QEI counting mode.
96   *                         - \ref QEI_CTL_X4_FREE_COUNTING_MODE
97   *                         - \ref QEI_CTL_X2_FREE_COUNTING_MODE
98   *                         - \ref QEI_CTL_X4_COMPARE_COUNTING_MODE
99   *                         - \ref QEI_CTL_X2_COMPARE_COUNTING_MODE
100   * @param[in]  u32Value    The counter maximum value in compare-counting mode.
101   * @return     None
102   * @details    This function set QEI in specified mode and enable input.
103   */
QEI_Open(QEI_T * qei,uint32_t u32Mode,uint32_t u32Value)104 void QEI_Open(QEI_T* qei, uint32_t u32Mode, uint32_t u32Value)
105 {
106     /* Set QEI function configuration */
107     /* Set QEI counting mode */
108     /* Enable IDX, QEA and QEB input to QEI controller */
109     qei->CTL = (qei->CTL & (~QEI_CTL_MODE_Msk)) | ((u32Mode) | QEI_CTL_CHAEN_Msk | QEI_CTL_CHBEN_Msk | QEI_CTL_IDXEN_Msk);
110 
111     /* Set QEI maximum count value in in compare-counting mode */
112     qei->CNTMAX = u32Value;
113 }
114 
115 /**
116   * @brief      Start QEI function
117   * @param[in]  qei     The pointer of the specified QEI module.
118   * @return     None
119   * @details    This function enable QEI function and start QEI counting.
120   */
QEI_Start(QEI_T * qei)121 void QEI_Start(QEI_T* qei)
122 {
123     /* Enable QEI controller function */
124     qei->CTL |= QEI_CTL_QEIEN_Msk;
125 }
126 
127 /**
128   * @brief      Stop QEI function
129   * @param[in]  qei     The pointer of the specified QEI module.
130   * @return     None
131   * @details    This function disable QEI function and stop QEI counting.
132   */
QEI_Stop(QEI_T * qei)133 void QEI_Stop(QEI_T* qei)
134 {
135     /* Disable QEI controller function */
136     qei->CTL &= (~QEI_CTL_QEIEN_Msk);
137 }
138 
139 
140 /**@}*/ /* end of group QEI_EXPORTED_FUNCTIONS */
141 
142 /**@}*/ /* end of group QEI_Driver */
143 
144 /**@}*/ /* end of group Standard_Driver */
145