qei.h 16.3 KB
Newer Older
B
Bluebear233 已提交
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/**************************************************************************//**
 * @file     qei.h
 * @version  V3.00
 * @brief    Quadrature Encoder Interface (QEI) driver header file
 *
 * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#ifndef __QEI_H__
#define __QEI_H__

#ifdef __cplusplus
extern "C"
{
#endif


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

/** @addtogroup QEI_Driver QEI Driver
  @{
*/

/** @addtogroup QEI_EXPORTED_CONSTANTS QEI Exported Constants
  @{
*/

/*---------------------------------------------------------------------------------------------------------*/
/* QEI counting mode selection constants definitions                                                       */
/*---------------------------------------------------------------------------------------------------------*/
#define QEI_CTL_X4_FREE_COUNTING_MODE       (0x0<<QEI_CTL_MODE_Pos) /*!< QEI operate in X4 free-counting mode \hideinitializer */
#define QEI_CTL_X2_FREE_COUNTING_MODE       (0x1<<QEI_CTL_MODE_Pos) /*!< QEI operate in X2 free-counting mode \hideinitializer */
#define QEI_CTL_X4_COMPARE_COUNTING_MODE    (0x2<<QEI_CTL_MODE_Pos) /*!< QEI operate in X4 compare-counting mode \hideinitializer */
#define QEI_CTL_X2_COMPARE_COUNTING_MODE    (0x3<<QEI_CTL_MODE_Pos) /*!< QEI operate in X2 compare-counting mode \hideinitializer */

/*---------------------------------------------------------------------------------------------------------*/
/* QEI noise filter clock pre-divide selection constants definitions                                       */
/*---------------------------------------------------------------------------------------------------------*/
#define QEI_CTL_NFCLKSEL_DIV1   (0x0<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK \hideinitializer */
#define QEI_CTL_NFCLKSEL_DIV2   (0x1<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/2 \hideinitializer */
#define QEI_CTL_NFCLKSEL_DIV4   (0x2<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/4 \hideinitializer */
#define QEI_CTL_NFCLKSEL_DIV16  (0x3<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/16 \hideinitializer */
#define QEI_CTL_NFCLKSEL_DIV32  (0x4<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/32 \hideinitializer */
#define QEI_CTL_NFCLKSEL_DIV64  (0x5<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/64 \hideinitializer */




/*@}*/ /* end of group QEI_EXPORTED_CONSTANTS */


/** @addtogroup QEI_EXPORTED_FUNCTIONS QEI Exported Functions
  @{
*/

/**
  * @brief      Disable QEI compare function
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     None
  * @details    This macro disable QEI counter compare function.
  * \hideinitializer
  */
#define QEI_DISABLE_CNT_CMP(qei)    ((qei)->CTL &= (~QEI_CTL_CMPEN_Msk))

/**
  * @brief      Enable QEI compare function
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     None
  * @details    This macro enable QEI counter compare function.
  * \hideinitializer
  */
#define QEI_ENABLE_CNT_CMP(qei)     ((qei)->CTL |= QEI_CTL_CMPEN_Msk)

/**
  * @brief      Disable QEI index latch function
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     None
  * @details    This macro disable QEI index trigger counter latch function.
  * \hideinitializer
  */
#define QEI_DISABLE_INDEX_LATCH(qei)     ((qei)->CTL &= (~QEI_CTL_IDXLATEN_Msk))

/**
  * @brief      Enable QEI index latch function
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     None
  * @details    This macro enable QEI index trigger counter latch function.
  * \hideinitializer
  */
#define QEI_ENABLE_INDEX_LATCH(qei)     ((qei)->CTL |= QEI_CTL_IDXLATEN_Msk)

/**
  * @brief      Disable QEI index reload function
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     None
  * @details    This macro disable QEI index trigger counter reload function.
  * \hideinitializer
  */
#define QEI_DISABLE_INDEX_RELOAD(qei)    ((qei)->CTL &= (~QEI_CTL_IDXRLDEN_Msk))

/**
  * @brief      Enable QEI index reload function
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     None
  * @details    This macro enable QEI index trigger counter reload function.
  * \hideinitializer
  */
#define QEI_ENABLE_INDEX_RELOAD(qei)    ((qei)->CTL |= QEI_CTL_IDXRLDEN_Msk)

/**
  * @brief      Disable QEI input
  * @param[in]  qei             The pointer of the specified QEI module.
  * @param[in]  u32InputType    Input signal type.
  *                             - \ref QEI_CTL_CHAEN_Msk    : QEA input
  *                             - \ref QEI_CTL_CHAEN_Msk    : QEB input
  *                             - \ref QEI_CTL_IDXEN_Msk    : IDX input
  * @return     None
  * @details    This macro disable specified QEI signal input.
  * \hideinitializer
  */
#define QEI_DISABLE_INPUT(qei, u32InputType)     ((qei)->CTL &= ~(u32InputType))

/**
  * @brief      Enable QEI input
  * @param[in]  qei             The pointer of the specified QEI module.
  * @param[in]  u32InputType    Input signal type .
  *                             - \ref QEI_CTL_CHAEN_Msk    : QEA input
  *                             - \ref QEI_CTL_CHBEN_Msk    : QEB input
  *                             - \ref QEI_CTL_IDXEN_Msk    : IDX input
  * @return     None
  * @details    This macro enable specified QEI signal input.
  * \hideinitializer
  */
#define QEI_ENABLE_INPUT(qei, u32InputType)     ((qei)->CTL |= (u32InputType))

/**
  * @brief      Disable inverted input polarity
  * @param[in]  qei             The pointer of the specified QEI module.
  * @param[in]  u32InputType    Input signal type .
  *                             - \ref QEI_CTL_CHAINV_Msk   : QEA Input
  *                             - \ref QEI_CTL_CHBINV_Msk   : QEB Input
  *                             - \ref QEI_CTL_IDXINV_Msk   : IDX Input
  * @return     None
  * @details    This macro disable specified QEI signal inverted input polarity.
  * \hideinitializer
  */
#define QEI_DISABLE_INPUT_INV(qei, u32InputType)    ((qei)->CTL &= ~(u32InputType))

/**
  * @brief      Enable inverted input polarity
  * @param[in]  qei             The pointer of the specified QEI module.
  * @param[in]  u32InputType    Input signal type.
  *                             - \ref QEI_CTL_CHAINV_Msk   : QEA Input
  *                             - \ref QEI_CTL_CHBINV_Msk   : QEB Input
  *                             - \ref QEI_CTL_IDXINV_Msk   : IDX Input
  * @return     None
  * @details    This macro inverse specified QEI signal input polarity.
  * \hideinitializer
  */
#define QEI_ENABLE_INPUT_INV(qei, u32InputType)     ((qei)->CTL |= (u32InputType))

/**
  * @brief      Disable QEI interrupt
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32IntSel   Interrupt type selection.
  *                         - \ref QEI_CTL_DIRIEN_Msk   : Direction change interrupt
  *                         - \ref QEI_CTL_OVUNIEN_Msk  : Counter overflow or underflow interrupt
  *                         - \ref QEI_CTL_CMPIEN_Msk   : Compare-match interrupt
  *                         - \ref QEI_CTL_IDXIEN_Msk   : Index detected interrupt
  * @return     None
  * @details    This macro disable specified QEI interrupt.
  * \hideinitializer
  */
#define QEI_DISABLE_INT(qei, u32IntSel)     ((qei)->CTL &= ~(u32IntSel))

/**
  * @brief      Enable QEI interrupt
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32IntSel   Interrupt type selection.
  *                         - \ref QEI_CTL_DIRIEN_Msk   : Direction change interrupt
  *                         - \ref QEI_CTL_OVUNIEN_Msk  : Counter overflow or underflow interrupt
  *                         - \ref QEI_CTL_CMPIEN_Msk   : Compare-match interrupt
  *                         - \ref QEI_CTL_IDXIEN_Msk   : Index detected interrupt
  * @return     None
  * @details    This macro enable specified QEI interrupt.
  * \hideinitializer
  */
#define QEI_ENABLE_INT(qei, u32IntSel)      ((qei)->CTL |= (u32IntSel))

/**
  * @brief      Disable QEI noise filter
  * @param[in]  qei         The pointer of the specified QEI module.
  * @return     None
  * @details    This macro disable QEI noise filter function.
  * \hideinitializer
  */
#define QEI_DISABLE_NOISE_FILTER(qei)       ((qei)->CTL |= QEI_CTL_NFDIS_Msk)

/**
  * @brief      Enable QEI noise filter
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32ClkSel   The sampling frequency of the noise filter clock.
  *                         - \ref QEI_CTL_NFCLKSEL_DIV1
  *                         - \ref QEI_CTL_NFCLKSEL_DIV2
  *                         - \ref QEI_CTL_NFCLKSEL_DIV4
  *                         - \ref QEI_CTL_NFCLKSEL_DIV16
  *                         - \ref QEI_CTL_NFCLKSEL_DIV32
  *                         - \ref QEI_CTL_NFCLKSEL_DIV64
  * @return     None
  * @details    This macro enable QEI noise filter function and select noise filter clock.
  * \hideinitializer
  */
#define QEI_ENABLE_NOISE_FILTER(qei, u32ClkSel)     ((qei)->CTL = ((qei)->CTL & (~(QEI_CTL_NFDIS_Msk|QEI_CTL_NFCLKSEL_Msk))) | (u32ClkSel))

/**
  * @brief      Get QEI counter value
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     QEI pulse counter register value.
  * @details    This macro get QEI pulse counter value.
  * \hideinitializer
  */
#define QEI_GET_CNT_VALUE(qei)      ((qei)->CNT)

/**
  * @brief      Get QEI counting direction
  * @param[in]  qei     The pointer of the specified QEI module.
  * @retval     0       QEI counter is in down-counting.
  * @retval     1       QEI counter is in up-counting.
  * @details    This macro get QEI counting direction.
  * \hideinitializer
  */
#define QEI_GET_DIR(qei)    (((qei)->STATUS & (QEI_STATUS_DIRF_Msk))?1:0)

/**
  * @brief      Get QEI counter hold value
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     QEI pulse counter hold register value.
  * @details    This macro get QEI pulse counter hold value, which is updated with counter value in hold counter value control.
  * \hideinitializer
  */
#define QEI_GET_HOLD_VALUE(qei)     ((qei)->CNTHOLD)

/**
  * @brief      Get QEI counter index latch value
  * @param[in]  qei     The pointer of the specified QEI module.
  * @return     QEI pulse counter index latch value
  * @details    This macro get QEI pulse counter index latch value, which is updated with counter value when the index is detected.
  * \hideinitializer
  */
#define QEI_GET_INDEX_LATCH_VALUE(qei)     ((qei)->CNTLATCH)

/**
  * @brief      Set QEI counter index latch value
  * @param[in]  qei     The pointer of the specified QEI module.
  * @param[in]  u32Val  The latch value.
  * @return     QEI pulse counter index latch value
  * @details    This macro set QEI pulse counter index latch value, which is updated with counter value when the index is detected.
  * \hideinitializer
  */
#define QEI_SET_INDEX_LATCH_VALUE(qei,u32Val)     ((qei)->CNTLATCH = (u32Val))

/**
  * @brief      Get QEI interrupt flag status
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32IntSel   Interrupt type selection.
*                           - \ref QEI_STATUS_DIRF_Msk      : Counting direction flag
  *                         - \ref QEI_STATUS_DIRCHGF_Msk   : Direction change flag
  *                         - \ref QEI_STATUS_OVUNF_Msk     : Counter overflow or underflow flag
  *                         - \ref QEI_STATUS_CMPF_Msk      : Compare-match flag
  *                         - \ref QEI_STATUS_IDXF_Msk      : Index detected flag
  * @retval     0           QEI specified interrupt flag is not set.
  * @retval     1           QEI specified interrupt flag is set.
  * @details    This macro get QEI specified interrupt flag status.
  * \hideinitializer
  */
#define QEI_GET_INT_FLAG(qei, u32IntSel)        (((qei)->STATUS & (u32IntSel))?1:0)


/**
  * @brief      Clear QEI interrupt flag
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32IntSel   Interrupt type selection.
  *                         - \ref QEI_STATUS_DIRCHGF_Msk   : Direction change flag
  *                         - \ref QEI_STATUS_OVUNF_Msk     : Counter overflow or underflow flag
  *                         - \ref QEI_STATUS_CMPF_Msk      : Compare-match flag
  *                         - \ref QEI_STATUS_IDXF_Msk      : Index detected flag
  * @return     None
  * @details    This macro clear QEI specified interrupt flag.
  * \hideinitializer
  */
#define QEI_CLR_INT_FLAG(qei, u32IntSel)     ((qei)->STATUS = (u32IntSel))

/**
  * @brief      Set QEI counter compare value
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32Value    The counter compare value.
  * @return     None
  * @details    This macro set QEI pulse counter compare value.
  * \hideinitializer
  */
#define QEI_SET_CNT_CMP(qei, u32Value)      ((qei)->CNTCMP = (u32Value))

/**
  * @brief      Set QEI counter value
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32Value    The counter compare value.
  * @return     None
  * @details    This macro set QEI pulse counter value.
  * \hideinitializer
  */
#define QEI_SET_CNT_VALUE(qei, u32Value)      ((qei)->CNT = (u32Value))

/**
  * @brief      Enable QEI counter hold mode
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32Type     The triggered type.
  *                         - \ref QEI_CTL_HOLDCNT_Msk      : Hold QEI_CNT control
  *                         - \ref QEI_CTL_HOLDTMR0_Msk     : Hold QEI_CNT by Timer0
  *                         - \ref QEI_CTL_HOLDTMR1_Msk     : Hold QEI_CNT by Timer1
  *                         - \ref QEI_CTL_HOLDTMR2_Msk     : Hold QEI_CNT by Timer2
  *                         - \ref QEI_CTL_HOLDTMR3_Msk     : Hold QEI_CNT by Timer3
  * @return     None
  * @details    This macro enable QEI counter hold mode.
  * \hideinitializer
  */
#define QEI_ENABLE_HOLD_TRG_SRC(qei, u32Type)      ((qei)->CTL |= (u32Type))

/**
  * @brief      Disable QEI counter hold mode
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32Type     The triggered type.
  *                         - \ref QEI_CTL_HOLDCNT_Msk      : Hold QEI_CNT control
  *                         - \ref QEI_CTL_HOLDTMR0_Msk     : Hold QEI_CNT by Timer0
  *                         - \ref QEI_CTL_HOLDTMR1_Msk     : Hold QEI_CNT by Timer1
  *                         - \ref QEI_CTL_HOLDTMR2_Msk     : Hold QEI_CNT by Timer2
  *                         - \ref QEI_CTL_HOLDTMR3_Msk     : Hold QEI_CNT by Timer3
  * @return     None
  * @details    This macro disable QEI counter hold mode.
  * \hideinitializer
  */
#define QEI_DISABLE_HOLD_TRG_SRC(qei, u32Type)      ((qei)->CTL &= ~(u32Type))

/**
  * @brief      Set QEI maximum count value
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32Value    The counter maximum value.
  * @return     QEI maximum count value
  * @details    This macro set QEI maximum count value.
  * \hideinitializer
  */
#define QEI_SET_CNT_MAX(qei, u32Value)      ((qei)->CNTMAX = (u32Value))

/**
  * @brief      Set QEI counting mode
  * @param[in]  qei         The pointer of the specified QEI module.
  * @param[in]  u32Mode     QEI counting mode.
  *                         - \ref QEI_CTL_X4_FREE_COUNTING_MODE
  *                         - \ref QEI_CTL_X2_FREE_COUNTING_MODE
  *                         - \ref QEI_CTL_X4_COMPARE_COUNTING_MODE
  *                         - \ref QEI_CTL_X2_COMPARE_COUNTING_MODE
  * @return     None
  * @details    This macro set QEI counting mode.
  * \hideinitializer
  */
#define QEI_SET_CNT_MODE(qei, u32Mode)       ((qei)->CTL = ((qei)->CTL & (~QEI_CTL_MODE_Msk)) | (u32Mode))


void QEI_Close(QEI_T* qei);
void QEI_DisableInt(QEI_T* qei, uint32_t u32IntSel);
void QEI_EnableInt(QEI_T* qei, uint32_t u32IntSel);
void QEI_Open(QEI_T* qei, uint32_t u32Mode, uint32_t u32Value);
void QEI_Start(QEI_T* qei);
void QEI_Stop(QEI_T* qei);


/*@}*/ /* end of group QEI_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group QEI_Driver */

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

#ifdef __cplusplus
}
#endif

#endif /* __QEI_H__ */

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