summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/m460/StdDriver/src/nu_eqei.c
blob: e1c99cc2c91b2c19b505f34c5cf5129657b46cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**************************************************************************//**
 * @file     qei.c
 * @version  V3.00
 * @brief    Enhanced Quadrature Encoder Interface (EQEI) driver source file
 *
 * SPDX-License-Identifier: Apache-2.0
 * @copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "NuMicro.h"


/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup EQEI_Driver EQEI Driver
  @{
*/

/** @addtogroup EQEI_EXPORTED_FUNCTIONS EQEI Exported Functions
  @{
*/

/**
  * @brief      Close EQEI function
  * @param[in]  eqei         The pointer of the specified EQEI module.
  * @return     None
  * @details    This function reset EQEI configuration and stop EQEI counting.
  */
void EQEI_Close(EQEI_T *eqei)
{
    /* Reset EQEI configuration */
    eqei->CTL = (uint32_t)0;
}

/**
  * @brief      Disable EQEI interrupt
  * @param[in]  eqei         The pointer of the specified EQEI module.
  * @param[in]  u32IntSel   Interrupt type selection.
  *                         - \ref EQEI_CTL_DIRIEN_Msk   : Direction change interrupt
  *                         - \ref EQEI_CTL_OVUNIEN_Msk  : Counter overflow or underflow interrupt
  *                         - \ref EQEI_CTL_CMPIEN_Msk   : Compare-match interrupt
  *                         - \ref EQEI_CTL_IDXIEN_Msk   : Index detected interrupt
  * @return     None
  * @details    This function disable EQEI specified interrupt.
  */
void EQEI_DisableInt(EQEI_T *eqei, uint32_t u32IntSel)
{
    /* Disable EQEI specified interrupt */
    EQEI_DISABLE_INT(eqei, u32IntSel);

    /* Disable NVIC EQEI IRQ */
    if (eqei == (EQEI_T *)EQEI0)
    {
        NVIC_DisableIRQ((IRQn_Type)EQEI0_IRQn);
    }
    else if (eqei == (EQEI_T *)EQEI1)
    {
        NVIC_DisableIRQ((IRQn_Type)EQEI1_IRQn);
    }
    else if (eqei == (EQEI_T *)EQEI2)
    {
        NVIC_DisableIRQ((IRQn_Type)EQEI2_IRQn);
    }
    else
    {
        NVIC_DisableIRQ((IRQn_Type)EQEI3_IRQn);
    }
}

/**
  * @brief      Enable EQEI interrupt
  * @param[in]  eqei         The pointer of the specified EQEI module.
  * @param[in]  u32IntSel   Interrupt type selection.
  *                         - \ref EQEI_CTL_DIRIEN_Msk   : Direction change interrupt
  *                         - \ref EQEI_CTL_OVUNIEN_Msk  : Counter overflow or underflow interrupt
  *                         - \ref EQEI_CTL_CMPIEN_Msk   : Compare-match interrupt
  *                         - \ref EQEI_CTL_IDXIEN_Msk   : Index detected interrupt
  * @return     None
  * @details    This function enable EQEI specified interrupt.
  */
void EQEI_EnableInt(EQEI_T *eqei, uint32_t u32IntSel)
{
    /* Enable EQEI specified interrupt */
    EQEI_ENABLE_INT(eqei, u32IntSel);

    /* Enable NVIC EQEI IRQ */
    if (eqei == (EQEI_T *)EQEI0)
    {
        NVIC_EnableIRQ(EQEI0_IRQn);
    }
    else if (eqei == (EQEI_T *)EQEI1)
    {
        NVIC_EnableIRQ(EQEI1_IRQn);
    }
    else if (eqei == (EQEI_T *)EQEI2)
    {
        NVIC_EnableIRQ(EQEI2_IRQn);
    }
    else
    {
        NVIC_EnableIRQ(EQEI3_IRQn);
    }
}

/**
  * @brief      Open EQEI in specified mode and enable input
  * @param[in]  eqei         The pointer of the specified EQEI module.
  * @param[in]  u32Mode     EQEI counting mode.
  *                         - \ref EQEI_CTL_X4_FREE_COUNTING_MODE
  *                         - \ref EQEI_CTL_X2_FREE_COUNTING_MODE
  *                         - \ref EQEI_CTL_X4_COMPARE_COUNTING_MODE
  *                         - \ref EQEI_CTL_X2_COMPARE_COUNTING_MODE
  *                         - \ref EQEI_CTL_PHASE_COUNTING_MODE_TYPE1
  *                         - \ref EQEI_CTL_PHASE_COUNTING_MODE_TYPE2
  *                         - \ref EQEI_CTL_DIRECTIONAL_COUNTING_MODE
  * @param[in]  u32Value    The counter maximum value in compare-counting mode.
  * @return     None
  * @details    This function set EQEI in specified mode and enable input.
  */
void EQEI_Open(EQEI_T *eqei, uint32_t u32Mode, uint32_t u32Value)
{
    /* Set EQEI function configuration */
    /* Set EQEI counting mode */
    /* Enable IDX, QEA and QEB input to EQEI controller */
    eqei->CTL = (eqei->CTL & (~EQEI_CTL_MODE_Msk)) | ((u32Mode) | EQEI_CTL_CHAEN_Msk | EQEI_CTL_CHBEN_Msk | EQEI_CTL_IDXEN_Msk);

    /* Set EQEI maximum count value in in compare-counting mode */
    eqei->CNTMAX = u32Value;
}

/**
  * @brief      Start EQEI function
  * @param[in]  eqei     The pointer of the specified EQEI module.
  * @return     None
  * @details    This function enable EQEI function and start EQEI counting.
  */
void EQEI_Start(EQEI_T *eqei)
{
    /* Enable EQEI controller function */
    eqei->CTL |= EQEI_CTL_QEIEN_Msk;
}

/**
  * @brief      Stop EQEI function
  * @param[in]  eqei     The pointer of the specified EQEI module.
  * @return     None
  * @details    This function disable EQEI function and stop EQEI counting.
  */
void EQEI_Stop(EQEI_T *eqei)
{
    /* Disable EQEI controller function */
    eqei->CTL &= (~EQEI_CTL_QEIEN_Msk);
}


/*@}*/ /* end of group EQEI_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group EQEI_Driver */

/*@}*/ /* end of group Standard_Driver */

/*** (C) COPYRIGHT 2021 Nuvoton Technology Corp. ***/