lpc_qei.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**********************************************************************
  2. * $Id$ lpc_qei.c 2011-06-02
  3. *//**
  4. * @file lpc_qei.c
  5. * @brief Contains all functions support for QEI firmware library
  6. * on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup QEI
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _QEI
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_qei.h"
  44. #include "lpc_clkpwr.h"
  45. /* Private Types -------------------------------------------------------------- */
  46. /** @defgroup QEI_Private_Types QEI Private Types
  47. * @{
  48. */
  49. /**
  50. * @brief QEI configuration union type definition
  51. */
  52. typedef union {
  53. QEI_CFG_Type bmQEIConfig;
  54. uint32_t ulQEIConfig;
  55. } QEI_CFGOPT_Type;
  56. /**
  57. * @}
  58. */
  59. LPC_QEI_TypeDef* QEI_GetPointer(uint8_t qeiId);
  60. /* Public Functions ----------------------------------------------------------- */
  61. /** @addtogroup QEI_Public_Functions
  62. * @{
  63. */
  64. /*********************************************************************//**
  65. * @brief Get the point to typedef of QEI component
  66. * @param[in] qeiId The Id of the expected QEI component
  67. * It should be 0 (zero) always with LPC
  68. * @return None
  69. **********************************************************************/
  70. LPC_QEI_TypeDef* QEI_GetPointer(uint8_t qeiId)
  71. {
  72. LPC_QEI_TypeDef* pQei = NULL;
  73. if(qeiId == 0)
  74. {
  75. pQei = LPC_QEI;
  76. }
  77. return pQei;
  78. }
  79. /*********************************************************************//**
  80. * @brief Resets value for each type of QEI value, such as velocity,
  81. * counter, position, etc..
  82. * @param[in] qeiId The Id of the expected QEI component
  83. * It should be 0 (zero) always with LPC
  84. *
  85. * @param[in] ulResetType QEI Reset Type, should be one of the following:
  86. * - QEI_RESET_POS: Reset Position Counter
  87. * - QEI_RESET_POSOnIDX: Reset Position Counter on Index signal
  88. * - QEI_RESET_VEL: Reset Velocity
  89. * - QEI_RESET_IDX: Reset Index Counter
  90. * @return None
  91. **********************************************************************/
  92. void QEI_Reset(uint8_t qeiId, uint32_t ulResetType)
  93. {
  94. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  95. pQei->CON = ulResetType;
  96. }
  97. /*********************************************************************//**
  98. * @brief Initializes the QEI peripheral according to the specified
  99. * parameters in the QEI_ConfigStruct.
  100. * @param[in] qeiId The Id of the expected QEI component
  101. * It should be 0 (zero) always with LPC
  102. *
  103. * @param[in] QEI_ConfigStruct Pointer to a QEI_CFG_Type structure
  104. * that contains the configuration information for the
  105. * specified QEI peripheral
  106. * @return None
  107. **********************************************************************/
  108. void QEI_Init(uint8_t qeiId, QEI_CFG_Type *QEI_ConfigStruct)
  109. {
  110. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  111. /* Set up clock and power for QEI module */
  112. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCQEI, ENABLE);
  113. // Reset all remaining value in QEI peripheral
  114. pQei->MAXPOS = 0x00;
  115. pQei->CMPOS0 = 0x00;
  116. pQei->CMPOS1 = 0x00;
  117. pQei->CMPOS2 = 0x00;
  118. pQei->INXCMP0 = 0x00;
  119. pQei->VELCOMP = 0x00;
  120. pQei->LOAD = 0x00;
  121. pQei->CON = QEI_CON_RESP | QEI_CON_RESV | QEI_CON_RESI;
  122. pQei->FILTERPHA = 0x00;
  123. pQei->FILTERPHB = 0x00;
  124. pQei->FILTERINX = 0x00;
  125. // Disable all Interrupt
  126. pQei->IEC = QEI_IECLR_BITMASK;
  127. // Clear all Interrupt pending
  128. pQei->CLR = QEI_INTCLR_BITMASK;
  129. // Set QEI configuration value corresponding to its setting up value
  130. pQei->CONF = ((QEI_CFGOPT_Type *)QEI_ConfigStruct)->ulQEIConfig;
  131. }
  132. /*********************************************************************//**
  133. * @brief De-initializes the QEI peripheral registers to their
  134. * default reset values.
  135. * @param[in] qeiId The Id of the expected QEI component
  136. * It should be 0 (zero) always with LPC
  137. *
  138. * @return None
  139. **********************************************************************/
  140. void QEI_DeInit(uint8_t qeiId)
  141. {
  142. /* Turn off clock and power for QEI module */
  143. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCQEI, DISABLE);
  144. }
  145. /*****************************************************************************//**
  146. * @brief Fills each QIE_InitStruct member with its default value:
  147. * - DirectionInvert = QEI_DIRINV_NONE
  148. * - SignalMode = QEI_SIGNALMODE_QUAD
  149. * - CaptureMode = QEI_CAPMODE_4X
  150. * - InvertIndex = QEI_INVINX_NONE
  151. * @param[in] QIE_InitStruct Pointer to a QEI_CFG_Type structure
  152. * which will be initialized.
  153. * @return None
  154. *******************************************************************************/
  155. void QEI_GetCfgDefault(QEI_CFG_Type *QIE_InitStruct)
  156. {
  157. QIE_InitStruct->CaptureMode = QEI_CAPMODE_4X;
  158. QIE_InitStruct->DirectionInvert = QEI_DIRINV_NONE;
  159. QIE_InitStruct->InvertIndex = QEI_INVINX_NONE;
  160. QIE_InitStruct->SignalMode = QEI_SIGNALMODE_QUAD;
  161. }
  162. /*********************************************************************//**
  163. * @brief Check whether if specified flag status is set or not
  164. * @param[in] qeiId The Id of the expected QEI component
  165. * It should be 0 (zero) always with LPC
  166. *
  167. * @param[in] ulFlagType Status Flag Type, should be one of the following:
  168. * - QEI_STATUS_DIR: Direction Status
  169. * @return New Status of this status flag (SET or RESET)
  170. **********************************************************************/
  171. FlagStatus QEI_GetStatus(uint8_t qeiId, uint32_t ulFlagType)
  172. {
  173. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  174. return ((pQei->STAT & ulFlagType) ? SET : RESET);
  175. }
  176. /*********************************************************************//**
  177. * @brief Get current position value in QEI peripheral
  178. * @param[in] qeiId The Id of the expected QEI component
  179. * It should be 0 (zero) always with LPC
  180. *
  181. * @return Current position value of QEI peripheral
  182. **********************************************************************/
  183. uint32_t QEI_GetPosition(uint8_t qeiId)
  184. {
  185. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  186. return (pQei->POS);
  187. }
  188. /*********************************************************************//**
  189. * @brief Set max position value for QEI peripheral
  190. * @param[in] qeiId The Id of the expected QEI component
  191. * It should be 0 (zero) always with LPC
  192. *
  193. * @param[in] ulMaxPos Max position value to set
  194. * @return None
  195. **********************************************************************/
  196. void QEI_SetMaxPosition(uint8_t qeiId, uint32_t ulMaxPos)
  197. {
  198. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  199. pQei->MAXPOS = ulMaxPos;
  200. }
  201. /*********************************************************************//**
  202. * @brief Set position compare value for QEI peripheral
  203. * @param[in] qeiId The Id of the expected QEI component
  204. * It should be 0 (zero) always with LPC
  205. *
  206. * @param[in] bPosCompCh Compare Position channel, should be:
  207. * - QEI_COMPPOS_CH_0: QEI compare position channel 0
  208. * - QEI_COMPPOS_CH_1: QEI compare position channel 1
  209. * - QEI_COMPPOS_CH_2: QEI compare position channel 2
  210. * @param[in] ulPosComp Compare Position value to set
  211. * @return None
  212. **********************************************************************/
  213. void QEI_SetPositionComp(uint8_t qeiId, uint8_t bPosCompCh, uint32_t ulPosComp)
  214. {
  215. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  216. uint32_t *tmp;
  217. tmp = (uint32_t *) (&(pQei->CMPOS0) + bPosCompCh * 4);
  218. *tmp = ulPosComp;
  219. }
  220. /*********************************************************************//**
  221. * @brief Get current index counter of QEI peripheral
  222. * @param[in] qeiId The Id of the expected QEI component
  223. * It should be 0 (zero) always with LPC
  224. *
  225. * @return Current value of QEI index counter
  226. **********************************************************************/
  227. uint32_t QEI_GetIndex(uint8_t qeiId)
  228. {
  229. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  230. return (pQei->INXCNT);
  231. }
  232. /*********************************************************************//**
  233. * @brief Set value for index compare in QEI peripheral
  234. * @param[in] qeiId The Id of the expected QEI component
  235. * It should be 0 (zero) always with LPC
  236. *
  237. * @param[in] ulIndexComp Compare Index Value to set
  238. * @return None
  239. **********************************************************************/
  240. void QEI_SetIndexComp(uint8_t qeiId, uint32_t ulIndexComp)
  241. {
  242. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  243. pQei->INXCMP0 = ulIndexComp;
  244. }
  245. /*********************************************************************//**
  246. * @brief Set timer reload value for QEI peripheral. When the velocity timer is
  247. * over-flow, the value that set for Timer Reload register will be loaded
  248. * into the velocity timer for next period. The calculated velocity in RPM
  249. * therefore will be affect by this value.
  250. * @param[in] qeiId The Id of the expected QEI component
  251. * It should be 0 (zero) always with LPC
  252. *
  253. * @param[in] QEIReloadStruct QEI reload structure
  254. * @return None
  255. **********************************************************************/
  256. void QEI_SetTimerReload(uint8_t qeiId, QEI_RELOADCFG_Type *QEIReloadStruct)
  257. {
  258. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  259. uint64_t pclk;
  260. if (QEIReloadStruct->ReloadOption == QEI_TIMERRELOAD_TICKVAL)
  261. {
  262. pQei->LOAD = QEIReloadStruct->ReloadValue - 1;
  263. }
  264. else
  265. {
  266. #if 1
  267. pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  268. pclk = (pclk /(1000000/QEIReloadStruct->ReloadValue)) - 1;
  269. pQei->LOAD = (uint32_t)pclk;
  270. #else
  271. ld = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  272. if (ld/1000000 > 0)
  273. {
  274. ld /= 1000000;
  275. ld *= QEIReloadStruct->ReloadValue;
  276. ld -= 1;
  277. }
  278. else
  279. {
  280. ld *= QEIReloadStruct->ReloadValue;
  281. ld /= 1000000;
  282. ld -= 1;
  283. }
  284. pQei->LOAD = ld;
  285. #endif
  286. }
  287. }
  288. /*********************************************************************//**
  289. * @brief Get current timer counter in QEI peripheral
  290. * @param[in] qeiId The Id of the expected QEI component
  291. * It should be 0 (zero) always with LPC
  292. *
  293. * @return Current timer counter in QEI peripheral
  294. **********************************************************************/
  295. uint32_t QEI_GetTimer(uint8_t qeiId)
  296. {
  297. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  298. return (pQei->TIME);
  299. }
  300. /*********************************************************************//**
  301. * @brief Get current velocity pulse counter in current time period
  302. * @param[in] qeiId The Id of the expected QEI component
  303. * It should be 0 (zero) always with LPC
  304. *
  305. * @return Current velocity pulse counter value
  306. **********************************************************************/
  307. uint32_t QEI_GetVelocity(uint8_t qeiId)
  308. {
  309. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  310. return (pQei->VEL);
  311. }
  312. /*********************************************************************//**
  313. * @brief Get the most recently measured velocity of the QEI. When
  314. * the Velocity timer in QEI is over-flow, the current velocity
  315. * value will be loaded into Velocity Capture register.
  316. * @param[in] qeiId The Id of the expected QEI component
  317. * It should be 0 (zero) always with LPC
  318. *
  319. * @return The most recently measured velocity value
  320. **********************************************************************/
  321. uint32_t QEI_GetVelocityCap(uint8_t qeiId)
  322. {
  323. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  324. return (pQei->CAP);
  325. }
  326. /*********************************************************************//**
  327. * @brief Set Velocity Compare value for QEI peripheral
  328. * @param[in] qeiId The Id of the expected QEI component
  329. * It should be 0 (zero) always with LPC
  330. *
  331. * @param[in] ulVelComp Compare Velocity value to set
  332. * @return None
  333. **********************************************************************/
  334. void QEI_SetVelocityComp(uint8_t qeiId, uint32_t ulVelComp)
  335. {
  336. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  337. pQei->VELCOMP = ulVelComp;
  338. }
  339. /*********************************************************************//**
  340. * @brief Set value of sampling count for the digital filter in
  341. * QEI peripheral
  342. * @param[in] qeiId The Id of the expected QEI component
  343. * It should be 0 (zero) always with LPC
  344. *
  345. * @param[in] ulSamplingPulse Value of sampling count to set
  346. * @return None
  347. **********************************************************************/
  348. void QEI_SetDigiFilter(uint8_t qeiId, st_Qei_FilterCfg FilterVal)
  349. {
  350. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  351. pQei->FILTERPHA = FilterVal.PHA_FilterVal;
  352. pQei->FILTERPHB = FilterVal.PHB_FilterVal;
  353. pQei->FILTERINX = FilterVal.INX_FilterVal;
  354. }
  355. /*********************************************************************//**
  356. * @brief Check whether if specified interrupt flag status in QEI
  357. * peripheral is set or not
  358. * @param[in] qeiId The Id of the expected QEI component
  359. * It should be 0 (zero) always with LPC
  360. *
  361. * @param[in] ulIntType Interrupt Flag Status type, should be:
  362. - QEI_INTFLAG_INX_Int: index pulse was detected interrupt
  363. - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
  364. - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
  365. - QEI_INTFLAG_DIR_Int: Change of direction interrupt
  366. - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
  367. - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
  368. - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
  369. current position interrupt
  370. - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
  371. current position interrupt
  372. - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
  373. current position interrupt
  374. - QEI_INTFLAG_REV_Int: Index compare value is equal to the current
  375. index count interrupt
  376. - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
  377. - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
  378. - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
  379. * @return New State of specified interrupt flag status (SET or RESET)
  380. **********************************************************************/
  381. FlagStatus QEI_GetIntStatus(uint8_t qeiId, uint32_t ulIntType)
  382. {
  383. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  384. return((pQei->INTSTAT & ulIntType) ? SET : RESET);
  385. }
  386. /*********************************************************************//**
  387. * @brief Enable/Disable specified interrupt in QEI peripheral
  388. * @param[in] qeiId The Id of the expected QEI component
  389. * It should be 0 (zero) always with LPC
  390. *
  391. * @param[in] ulIntType Interrupt Flag Status type, should be:
  392. - QEI_INTFLAG_INX_Int: index pulse was detected interrupt
  393. - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
  394. - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
  395. - QEI_INTFLAG_DIR_Int: Change of direction interrupt
  396. - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
  397. - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
  398. - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
  399. current position interrupt
  400. - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
  401. current position interrupt
  402. - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
  403. current position interrupt
  404. - QEI_INTFLAG_REV_Int: Index compare value is equal to the current
  405. index count interrupt
  406. - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
  407. - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
  408. - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
  409. * @param[in] NewState New function state, should be:
  410. * - DISABLE
  411. * - ENABLE
  412. * @return None
  413. **********************************************************************/
  414. void QEI_IntCmd(uint8_t qeiId, uint32_t ulIntType, FunctionalState NewState)
  415. {
  416. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  417. if (NewState == ENABLE)
  418. {
  419. pQei->IES = ulIntType;
  420. }
  421. else
  422. {
  423. pQei->IEC = ulIntType;
  424. }
  425. }
  426. /*********************************************************************//**
  427. * @brief Sets (forces) specified interrupt in QEI peripheral
  428. * @param[in] qeiId The Id of the expected QEI component
  429. * It should be 0 (zero) always with LPC
  430. *
  431. * @param[in] ulIntType Interrupt Flag Status type, should be:
  432. - QEI_INTFLAG_INX_Int: index pulse was detected interrupt
  433. - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
  434. - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
  435. - QEI_INTFLAG_DIR_Int: Change of direction interrupt
  436. - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
  437. - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
  438. - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
  439. current position interrupt
  440. - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
  441. current position interrupt
  442. - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
  443. current position interrupt
  444. - QEI_INTFLAG_REV_Int: Index compare value is equal to the current
  445. index count interrupt
  446. - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
  447. - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
  448. - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
  449. * @return None
  450. **********************************************************************/
  451. void QEI_IntSet(uint8_t qeiId, uint32_t ulIntType)
  452. {
  453. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  454. pQei->SET = ulIntType;
  455. }
  456. /*********************************************************************//**
  457. * @brief Clear (force) specified interrupt (pending) in QEI peripheral
  458. * @param[in] qeiId The Id of the expected QEI component
  459. * It should be 0 (zero) always with LPC
  460. *
  461. * @param[in] ulIntType Interrupt Flag Status type, should be:
  462. - QEI_INTFLAG_INX_Int: index pulse was detected interrupt
  463. - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt
  464. - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt
  465. - QEI_INTFLAG_DIR_Int: Change of direction interrupt
  466. - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt
  467. - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt
  468. - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the
  469. current position interrupt
  470. - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the
  471. current position interrupt
  472. - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the
  473. current position interrupt
  474. - QEI_INTFLAG_REV_Int: Index compare value is equal to the current
  475. index count interrupt
  476. - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt
  477. - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt
  478. - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt
  479. * @return None
  480. **********************************************************************/
  481. void QEI_IntClear(uint8_t qeiId, uint32_t ulIntType)
  482. {
  483. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  484. pQei->CLR = ulIntType;
  485. }
  486. /*********************************************************************//**
  487. * @brief Calculates the actual velocity in RPM passed via velocity
  488. * capture value and Pulse Per Round (of the encoder) value
  489. * parameter input.
  490. * @param[in] qeiId The Id of the expected QEI component
  491. * It should be 0 (zero) always with LPC
  492. *
  493. * @param[in] ulVelCapValue Velocity capture input value that can
  494. * be got from QEI_GetVelocityCap() function
  495. * @param[in] ulPPR Pulse per round of encoder
  496. * @return The actual value of velocity in RPM (Round per minute)
  497. **********************************************************************/
  498. uint32_t QEI_CalculateRPM(uint8_t qeiId, uint32_t ulVelCapValue, uint32_t ulPPR)
  499. {
  500. LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
  501. uint64_t rpm, clock, Load, edges;
  502. // Get current Clock rate for timer input
  503. clock = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
  504. // Get Timer load value (velocity capture period)
  505. Load = (uint64_t)(pQei->LOAD + 1);
  506. // Get Edge
  507. edges = (uint64_t)((pQei->CONF & QEI_CONF_CAPMODE) ? 4 : 2);
  508. // Calculate RPM
  509. rpm = ((clock * ulVelCapValue * 60) / (Load * ulPPR * edges));
  510. return (uint32_t)(rpm);
  511. }
  512. /**
  513. * @}
  514. */
  515. #endif /*_QEI*/
  516. /**
  517. * @}
  518. */
  519. /* --------------------------------- End Of File ------------------------------ */