1 /**************************************************************************//** 2 * @file qei.h 3 * @version V3.00 4 * @brief Quadrature Encoder Interface (QEI) driver header file 5 * 6 * @copyright SPDX-License-Identifier: Apache-2.0 7 * @copyright Copyright (C) 2017-2020 Nuvoton Technology Corp. All rights reserved. 8 *****************************************************************************/ 9 #ifndef __QEI_H__ 10 #define __QEI_H__ 11 12 #ifdef __cplusplus 13 extern "C" 14 { 15 #endif 16 17 18 /** @addtogroup Standard_Driver Standard Driver 19 @{ 20 */ 21 22 /** @addtogroup QEI_Driver QEI Driver 23 @{ 24 */ 25 26 /** @addtogroup QEI_EXPORTED_CONSTANTS QEI Exported Constants 27 @{ 28 */ 29 30 /*---------------------------------------------------------------------------------------------------------*/ 31 /* QEI counting mode selection constants definitions */ 32 /*---------------------------------------------------------------------------------------------------------*/ 33 #define QEI_CTL_X4_FREE_COUNTING_MODE (0x0<<QEI_CTL_MODE_Pos) /*!< QEI operate in X4 free-counting mode \hideinitializer */ 34 #define QEI_CTL_X2_FREE_COUNTING_MODE (0x1<<QEI_CTL_MODE_Pos) /*!< QEI operate in X2 free-counting mode \hideinitializer */ 35 #define QEI_CTL_X4_COMPARE_COUNTING_MODE (0x2<<QEI_CTL_MODE_Pos) /*!< QEI operate in X4 compare-counting mode \hideinitializer */ 36 #define QEI_CTL_X2_COMPARE_COUNTING_MODE (0x3<<QEI_CTL_MODE_Pos) /*!< QEI operate in X2 compare-counting mode \hideinitializer */ 37 38 /*---------------------------------------------------------------------------------------------------------*/ 39 /* QEI noise filter clock pre-divide selection constants definitions */ 40 /*---------------------------------------------------------------------------------------------------------*/ 41 #define QEI_CTL_NFCLKSEL_DIV1 (0x0<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK \hideinitializer */ 42 #define QEI_CTL_NFCLKSEL_DIV2 (0x1<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/2 \hideinitializer */ 43 #define QEI_CTL_NFCLKSEL_DIV4 (0x2<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/4 \hideinitializer */ 44 #define QEI_CTL_NFCLKSEL_DIV16 (0x3<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/16 \hideinitializer */ 45 #define QEI_CTL_NFCLKSEL_DIV32 (0x4<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/32 \hideinitializer */ 46 #define QEI_CTL_NFCLKSEL_DIV64 (0x5<<QEI_CTL_NFCLKSEL_Pos) /*!< The sampling frequency of the noise filter is QEI_CLK/64 \hideinitializer */ 47 48 49 50 51 /*@}*/ /* end of group QEI_EXPORTED_CONSTANTS */ 52 53 54 /** @addtogroup QEI_EXPORTED_FUNCTIONS QEI Exported Functions 55 @{ 56 */ 57 58 /** 59 * @brief Disable QEI compare function 60 * @param[in] qei The pointer of the specified QEI module. 61 * @return None 62 * @details This macro disable QEI counter compare function. 63 * \hideinitializer 64 */ 65 #define QEI_DISABLE_CNT_CMP(qei) ((qei)->CTL &= (~QEI_CTL_CMPEN_Msk)) 66 67 /** 68 * @brief Enable QEI compare function 69 * @param[in] qei The pointer of the specified QEI module. 70 * @return None 71 * @details This macro enable QEI counter compare function. 72 * \hideinitializer 73 */ 74 #define QEI_ENABLE_CNT_CMP(qei) ((qei)->CTL |= QEI_CTL_CMPEN_Msk) 75 76 /** 77 * @brief Disable QEI index latch function 78 * @param[in] qei The pointer of the specified QEI module. 79 * @return None 80 * @details This macro disable QEI index trigger counter latch function. 81 * \hideinitializer 82 */ 83 #define QEI_DISABLE_INDEX_LATCH(qei) ((qei)->CTL &= (~QEI_CTL_IDXLATEN_Msk)) 84 85 /** 86 * @brief Enable QEI index latch function 87 * @param[in] qei The pointer of the specified QEI module. 88 * @return None 89 * @details This macro enable QEI index trigger counter latch function. 90 * \hideinitializer 91 */ 92 #define QEI_ENABLE_INDEX_LATCH(qei) ((qei)->CTL |= QEI_CTL_IDXLATEN_Msk) 93 94 /** 95 * @brief Disable QEI index reload function 96 * @param[in] qei The pointer of the specified QEI module. 97 * @return None 98 * @details This macro disable QEI index trigger counter reload function. 99 * \hideinitializer 100 */ 101 #define QEI_DISABLE_INDEX_RELOAD(qei) ((qei)->CTL &= (~QEI_CTL_IDXRLDEN_Msk)) 102 103 /** 104 * @brief Enable QEI index reload function 105 * @param[in] qei The pointer of the specified QEI module. 106 * @return None 107 * @details This macro enable QEI index trigger counter reload function. 108 * \hideinitializer 109 */ 110 #define QEI_ENABLE_INDEX_RELOAD(qei) ((qei)->CTL |= QEI_CTL_IDXRLDEN_Msk) 111 112 /** 113 * @brief Disable QEI input 114 * @param[in] qei The pointer of the specified QEI module. 115 * @param[in] u32InputType Input signal type. 116 * - \ref QEI_CTL_CHAEN_Msk : QEA input 117 * - \ref QEI_CTL_CHAEN_Msk : QEB input 118 * - \ref QEI_CTL_IDXEN_Msk : IDX input 119 * @return None 120 * @details This macro disable specified QEI signal input. 121 * \hideinitializer 122 */ 123 #define QEI_DISABLE_INPUT(qei, u32InputType) ((qei)->CTL &= ~(u32InputType)) 124 125 /** 126 * @brief Enable QEI input 127 * @param[in] qei The pointer of the specified QEI module. 128 * @param[in] u32InputType Input signal type . 129 * - \ref QEI_CTL_CHAEN_Msk : QEA input 130 * - \ref QEI_CTL_CHBEN_Msk : QEB input 131 * - \ref QEI_CTL_IDXEN_Msk : IDX input 132 * @return None 133 * @details This macro enable specified QEI signal input. 134 * \hideinitializer 135 */ 136 #define QEI_ENABLE_INPUT(qei, u32InputType) ((qei)->CTL |= (u32InputType)) 137 138 /** 139 * @brief Disable inverted input polarity 140 * @param[in] qei The pointer of the specified QEI module. 141 * @param[in] u32InputType Input signal type . 142 * - \ref QEI_CTL_CHAINV_Msk : QEA Input 143 * - \ref QEI_CTL_CHBINV_Msk : QEB Input 144 * - \ref QEI_CTL_IDXINV_Msk : IDX Input 145 * @return None 146 * @details This macro disable specified QEI signal inverted input polarity. 147 * \hideinitializer 148 */ 149 #define QEI_DISABLE_INPUT_INV(qei, u32InputType) ((qei)->CTL &= ~(u32InputType)) 150 151 /** 152 * @brief Enable inverted input polarity 153 * @param[in] qei The pointer of the specified QEI module. 154 * @param[in] u32InputType Input signal type. 155 * - \ref QEI_CTL_CHAINV_Msk : QEA Input 156 * - \ref QEI_CTL_CHBINV_Msk : QEB Input 157 * - \ref QEI_CTL_IDXINV_Msk : IDX Input 158 * @return None 159 * @details This macro inverse specified QEI signal input polarity. 160 * \hideinitializer 161 */ 162 #define QEI_ENABLE_INPUT_INV(qei, u32InputType) ((qei)->CTL |= (u32InputType)) 163 164 /** 165 * @brief Disable QEI interrupt 166 * @param[in] qei The pointer of the specified QEI module. 167 * @param[in] u32IntSel Interrupt type selection. 168 * - \ref QEI_CTL_DIRIEN_Msk : Direction change interrupt 169 * - \ref QEI_CTL_OVUNIEN_Msk : Counter overflow or underflow interrupt 170 * - \ref QEI_CTL_CMPIEN_Msk : Compare-match interrupt 171 * - \ref QEI_CTL_IDXIEN_Msk : Index detected interrupt 172 * @return None 173 * @details This macro disable specified QEI interrupt. 174 * \hideinitializer 175 */ 176 #define QEI_DISABLE_INT(qei, u32IntSel) ((qei)->CTL &= ~(u32IntSel)) 177 178 /** 179 * @brief Enable QEI interrupt 180 * @param[in] qei The pointer of the specified QEI module. 181 * @param[in] u32IntSel Interrupt type selection. 182 * - \ref QEI_CTL_DIRIEN_Msk : Direction change interrupt 183 * - \ref QEI_CTL_OVUNIEN_Msk : Counter overflow or underflow interrupt 184 * - \ref QEI_CTL_CMPIEN_Msk : Compare-match interrupt 185 * - \ref QEI_CTL_IDXIEN_Msk : Index detected interrupt 186 * @return None 187 * @details This macro enable specified QEI interrupt. 188 * \hideinitializer 189 */ 190 #define QEI_ENABLE_INT(qei, u32IntSel) ((qei)->CTL |= (u32IntSel)) 191 192 /** 193 * @brief Disable QEI noise filter 194 * @param[in] qei The pointer of the specified QEI module. 195 * @return None 196 * @details This macro disable QEI noise filter function. 197 * \hideinitializer 198 */ 199 #define QEI_DISABLE_NOISE_FILTER(qei) ((qei)->CTL |= QEI_CTL_NFDIS_Msk) 200 201 /** 202 * @brief Enable QEI noise filter 203 * @param[in] qei The pointer of the specified QEI module. 204 * @param[in] u32ClkSel The sampling frequency of the noise filter clock. 205 * - \ref QEI_CTL_NFCLKSEL_DIV1 206 * - \ref QEI_CTL_NFCLKSEL_DIV2 207 * - \ref QEI_CTL_NFCLKSEL_DIV4 208 * - \ref QEI_CTL_NFCLKSEL_DIV16 209 * - \ref QEI_CTL_NFCLKSEL_DIV32 210 * - \ref QEI_CTL_NFCLKSEL_DIV64 211 * @return None 212 * @details This macro enable QEI noise filter function and select noise filter clock. 213 * \hideinitializer 214 */ 215 #define QEI_ENABLE_NOISE_FILTER(qei, u32ClkSel) ((qei)->CTL = ((qei)->CTL & (~(QEI_CTL_NFDIS_Msk|QEI_CTL_NFCLKSEL_Msk))) | (u32ClkSel)) 216 217 /** 218 * @brief Get QEI counter value 219 * @param[in] qei The pointer of the specified QEI module. 220 * @return QEI pulse counter register value. 221 * @details This macro get QEI pulse counter value. 222 * \hideinitializer 223 */ 224 #define QEI_GET_CNT_VALUE(qei) ((qei)->CNT) 225 226 /** 227 * @brief Get QEI counting direction 228 * @param[in] qei The pointer of the specified QEI module. 229 * @retval 0 QEI counter is in down-counting. 230 * @retval 1 QEI counter is in up-counting. 231 * @details This macro get QEI counting direction. 232 * \hideinitializer 233 */ 234 #define QEI_GET_DIR(qei) (((qei)->STATUS & (QEI_STATUS_DIRF_Msk))?1:0) 235 236 /** 237 * @brief Get QEI counter hold value 238 * @param[in] qei The pointer of the specified QEI module. 239 * @return QEI pulse counter hold register value. 240 * @details This macro get QEI pulse counter hold value, which is updated with counter value in hold counter value control. 241 * \hideinitializer 242 */ 243 #define QEI_GET_HOLD_VALUE(qei) ((qei)->CNTHOLD) 244 245 /** 246 * @brief Get QEI counter index latch value 247 * @param[in] qei The pointer of the specified QEI module. 248 * @return QEI pulse counter index latch value 249 * @details This macro get QEI pulse counter index latch value, which is updated with counter value when the index is detected. 250 * \hideinitializer 251 */ 252 #define QEI_GET_INDEX_LATCH_VALUE(qei) ((qei)->CNTLATCH) 253 254 /** 255 * @brief Set QEI counter index latch value 256 * @param[in] qei The pointer of the specified QEI module. 257 * @param[in] u32Val The latch value. 258 * @return QEI pulse counter index latch value 259 * @details This macro set QEI pulse counter index latch value, which is updated with counter value when the index is detected. 260 * \hideinitializer 261 */ 262 #define QEI_SET_INDEX_LATCH_VALUE(qei,u32Val) ((qei)->CNTLATCH = (u32Val)) 263 264 /** 265 * @brief Get QEI interrupt flag status 266 * @param[in] qei The pointer of the specified QEI module. 267 * @param[in] u32IntSel Interrupt type selection. 268 * - \ref QEI_STATUS_DIRF_Msk : Counting direction flag 269 * - \ref QEI_STATUS_DIRCHGF_Msk : Direction change flag 270 * - \ref QEI_STATUS_OVUNF_Msk : Counter overflow or underflow flag 271 * - \ref QEI_STATUS_CMPF_Msk : Compare-match flag 272 * - \ref QEI_STATUS_IDXF_Msk : Index detected flag 273 * @retval 0 QEI specified interrupt flag is not set. 274 * @retval 1 QEI specified interrupt flag is set. 275 * @details This macro get QEI specified interrupt flag status. 276 * \hideinitializer 277 */ 278 #define QEI_GET_INT_FLAG(qei, u32IntSel) (((qei)->STATUS & (u32IntSel))?1:0) 279 280 281 /** 282 * @brief Clear QEI interrupt flag 283 * @param[in] qei The pointer of the specified QEI module. 284 * @param[in] u32IntSel Interrupt type selection. 285 * - \ref QEI_STATUS_DIRCHGF_Msk : Direction change flag 286 * - \ref QEI_STATUS_OVUNF_Msk : Counter overflow or underflow flag 287 * - \ref QEI_STATUS_CMPF_Msk : Compare-match flag 288 * - \ref QEI_STATUS_IDXF_Msk : Index detected flag 289 * @return None 290 * @details This macro clear QEI specified interrupt flag. 291 * \hideinitializer 292 */ 293 #define QEI_CLR_INT_FLAG(qei, u32IntSel) ((qei)->STATUS = (u32IntSel)) 294 295 /** 296 * @brief Set QEI counter compare value 297 * @param[in] qei The pointer of the specified QEI module. 298 * @param[in] u32Value The counter compare value. 299 * @return None 300 * @details This macro set QEI pulse counter compare value. 301 * \hideinitializer 302 */ 303 #define QEI_SET_CNT_CMP(qei, u32Value) ((qei)->CNTCMP = (u32Value)) 304 305 /** 306 * @brief Set QEI counter value 307 * @param[in] qei The pointer of the specified QEI module. 308 * @param[in] u32Value The counter compare value. 309 * @return None 310 * @details This macro set QEI pulse counter value. 311 * \hideinitializer 312 */ 313 #define QEI_SET_CNT_VALUE(qei, u32Value) ((qei)->CNT = (u32Value)) 314 315 /** 316 * @brief Enable QEI counter hold mode 317 * @param[in] qei The pointer of the specified QEI module. 318 * @param[in] u32Type The triggered type. 319 * - \ref QEI_CTL_HOLDCNT_Msk : Hold QEI_CNT control 320 * - \ref QEI_CTL_HOLDTMR0_Msk : Hold QEI_CNT by Timer0 321 * - \ref QEI_CTL_HOLDTMR1_Msk : Hold QEI_CNT by Timer1 322 * - \ref QEI_CTL_HOLDTMR2_Msk : Hold QEI_CNT by Timer2 323 * - \ref QEI_CTL_HOLDTMR3_Msk : Hold QEI_CNT by Timer3 324 * @return None 325 * @details This macro enable QEI counter hold mode. 326 * \hideinitializer 327 */ 328 #define QEI_ENABLE_HOLD_TRG_SRC(qei, u32Type) ((qei)->CTL |= (u32Type)) 329 330 /** 331 * @brief Disable QEI counter hold mode 332 * @param[in] qei The pointer of the specified QEI module. 333 * @param[in] u32Type The triggered type. 334 * - \ref QEI_CTL_HOLDCNT_Msk : Hold QEI_CNT control 335 * - \ref QEI_CTL_HOLDTMR0_Msk : Hold QEI_CNT by Timer0 336 * - \ref QEI_CTL_HOLDTMR1_Msk : Hold QEI_CNT by Timer1 337 * - \ref QEI_CTL_HOLDTMR2_Msk : Hold QEI_CNT by Timer2 338 * - \ref QEI_CTL_HOLDTMR3_Msk : Hold QEI_CNT by Timer3 339 * @return None 340 * @details This macro disable QEI counter hold mode. 341 * \hideinitializer 342 */ 343 #define QEI_DISABLE_HOLD_TRG_SRC(qei, u32Type) ((qei)->CTL &= ~(u32Type)) 344 345 /** 346 * @brief Set QEI maximum count value 347 * @param[in] qei The pointer of the specified QEI module. 348 * @param[in] u32Value The counter maximum value. 349 * @return QEI maximum count value 350 * @details This macro set QEI maximum count value. 351 * \hideinitializer 352 */ 353 #define QEI_SET_CNT_MAX(qei, u32Value) ((qei)->CNTMAX = (u32Value)) 354 355 /** 356 * @brief Set QEI counting mode 357 * @param[in] qei The pointer of the specified QEI module. 358 * @param[in] u32Mode QEI counting mode. 359 * - \ref QEI_CTL_X4_FREE_COUNTING_MODE 360 * - \ref QEI_CTL_X2_FREE_COUNTING_MODE 361 * - \ref QEI_CTL_X4_COMPARE_COUNTING_MODE 362 * - \ref QEI_CTL_X2_COMPARE_COUNTING_MODE 363 * @return None 364 * @details This macro set QEI counting mode. 365 * \hideinitializer 366 */ 367 #define QEI_SET_CNT_MODE(qei, u32Mode) ((qei)->CTL = ((qei)->CTL & (~QEI_CTL_MODE_Msk)) | (u32Mode)) 368 369 370 void QEI_Close(QEI_T* qei); 371 void QEI_DisableInt(QEI_T* qei, uint32_t u32IntSel); 372 void QEI_EnableInt(QEI_T* qei, uint32_t u32IntSel); 373 void QEI_Open(QEI_T* qei, uint32_t u32Mode, uint32_t u32Value); 374 void QEI_Start(QEI_T* qei); 375 void QEI_Stop(QEI_T* qei); 376 377 378 /*@}*/ /* end of group QEI_EXPORTED_FUNCTIONS */ 379 380 /*@}*/ /* end of group QEI_Driver */ 381 382 /*@}*/ /* end of group Standard_Driver */ 383 384 #ifdef __cplusplus 385 } 386 #endif 387 388 #endif /*__QEI_H__*/ 389 390