qei.h 16 KB

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