nu_qei.h 16 KB

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