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