uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_qei.c
Go to the documentation of this file.
1 /***********************************************************************/
20 /* Peripheral group ----------------------------------------------------------- */
25 /* Includes ------------------------------------------------------------------- */
26 #include "lpc17xx_qei.h"
27 #include "lpc17xx_clkpwr.h"
28 
29 
30 /* If this source file built with example, the LPC17xx FW library configuration
31  * file in each example directory ("lpc17xx_libcfg.h") must be included,
32  * otherwise the default FW library configuration file must be included instead
33  */
34 #ifdef __BUILD_WITH_EXAMPLE__
35 #include "lpc17xx_libcfg.h"
36 #else
37 #include "lpc17xx_libcfg_default.h"
38 #endif /* __BUILD_WITH_EXAMPLE__ */
39 
40 
41 #ifdef _QEI
42 
43 /* Private Types -------------------------------------------------------------- */
51 typedef union {
52  QEI_CFG_Type bmQEIConfig;
53  uint32_t ulQEIConfig;
54 } QEI_CFGOPT_Type;
55 
61 /* Public Functions ----------------------------------------------------------- */
66 /*********************************************************************/
77 void QEI_Reset(LPC_QEI_TypeDef *QEIx, uint32_t ulResetType)
78 {
79  CHECK_PARAM(PARAM_QEIx(QEIx));
80  CHECK_PARAM(PARAM_QEI_RESET(ulResetType));
81 
82  QEIx->QEICON = ulResetType;
83 }
84 
85 /*********************************************************************/
94 void QEI_Init(LPC_QEI_TypeDef *QEIx, QEI_CFG_Type *QEI_ConfigStruct)
95 {
96 
97  CHECK_PARAM(PARAM_QEIx(QEIx));
98  CHECK_PARAM(PARAM_QEI_DIRINV(QEI_ConfigStruct->DirectionInvert));
99  CHECK_PARAM(PARAM_QEI_SIGNALMODE(QEI_ConfigStruct->SignalMode));
100  CHECK_PARAM(PARAM_QEI_CAPMODE(QEI_ConfigStruct->CaptureMode));
101  CHECK_PARAM(PARAM_QEI_INVINX(QEI_ConfigStruct->InvertIndex));
102 
103  /* Set up clock and power for QEI module */
105 
106  /* As default, peripheral clock for QEI module
107  * is set to FCCLK / 2 */
109 
110  // Reset all remaining value in QEI peripheral
112  QEIx->QEIMAXPOS = 0x00;
113  QEIx->CMPOS0 = 0x00;
114  QEIx->CMPOS1 = 0x00;
115  QEIx->CMPOS2 = 0x00;
116  QEIx->INXCMP = 0x00;
117  QEIx->QEILOAD = 0x00;
118  QEIx->VELCOMP = 0x00;
119  QEIx->FILTER = 0x00;
120  // Disable all Interrupt
121  QEIx->QEIIEC = QEI_IECLR_BITMASK;
122  // Clear all Interrupt pending
123  QEIx->QEICLR = QEI_INTCLR_BITMASK;
124  // Set QEI configuration value corresponding to its setting up value
125  QEIx->QEICONF = ((QEI_CFGOPT_Type *)QEI_ConfigStruct)->ulQEIConfig;
126 }
127 
128 
129 /*********************************************************************/
135 void QEI_DeInit(LPC_QEI_TypeDef *QEIx)
136 {
137  CHECK_PARAM(PARAM_QEIx(QEIx));
138 
139  /* Turn off clock and power for QEI module */
141 }
142 
143 
144 /*****************************************************************************/
154 void QEI_ConfigStructInit(QEI_CFG_Type *QIE_InitStruct)
155 {
156  QIE_InitStruct->CaptureMode = QEI_CAPMODE_4X;
157  QIE_InitStruct->DirectionInvert = QEI_DIRINV_NONE;
158  QIE_InitStruct->InvertIndex = QEI_INVINX_NONE;
159  QIE_InitStruct->SignalMode = QEI_SIGNALMODE_QUAD;
160 }
161 
162 
163 /*********************************************************************/
170 FlagStatus QEI_GetStatus(LPC_QEI_TypeDef *QEIx, uint32_t ulFlagType)
171 {
172  CHECK_PARAM(PARAM_QEIx(QEIx));
173  CHECK_PARAM(PARAM_QEI_STATUS(ulFlagType));
174  return ((QEIx->QEISTAT & ulFlagType) ? SET : RESET);
175 }
176 
177 /*********************************************************************/
182 uint32_t QEI_GetPosition(LPC_QEI_TypeDef *QEIx)
183 {
184  CHECK_PARAM(PARAM_QEIx(QEIx));
185  return (QEIx->QEIPOS);
186 }
187 
188 /*********************************************************************/
194 void QEI_SetMaxPosition(LPC_QEI_TypeDef *QEIx, uint32_t ulMaxPos)
195 {
196  CHECK_PARAM(PARAM_QEIx(QEIx));
197  QEIx->QEIMAXPOS = ulMaxPos;
198 }
199 
200 /*********************************************************************/
210 void QEI_SetPositionComp(LPC_QEI_TypeDef *QEIx, uint8_t bPosCompCh, uint32_t ulPosComp)
211 {
212  uint32_t *tmp;
213 
214  CHECK_PARAM(PARAM_QEIx(QEIx));
215  CHECK_PARAM(PARAM_QEI_COMPPOS_CH(bPosCompCh));
216  tmp = (uint32_t *) (&(QEIx->CMPOS0) + bPosCompCh * 4);
217  *tmp = ulPosComp;
218 
219 }
220 
221 /*********************************************************************/
226 uint32_t QEI_GetIndex(LPC_QEI_TypeDef *QEIx)
227 {
228  CHECK_PARAM(PARAM_QEIx(QEIx));
229  return (QEIx->INXCNT);
230 }
231 
232 /*********************************************************************/
238 void QEI_SetIndexComp(LPC_QEI_TypeDef *QEIx, uint32_t ulIndexComp)
239 {
240  CHECK_PARAM(PARAM_QEIx(QEIx));
241  QEIx->INXCMP = ulIndexComp;
242 }
243 
244 /*********************************************************************/
253 void QEI_SetTimerReload(LPC_QEI_TypeDef *QEIx, QEI_RELOADCFG_Type *QEIReloadStruct)
254 {
255  uint64_t pclk;
256 
257  CHECK_PARAM(PARAM_QEIx(QEIx));
259 
260  if (QEIReloadStruct->ReloadOption == QEI_TIMERRELOAD_TICKVAL) {
261  QEIx->QEILOAD = QEIReloadStruct->ReloadValue - 1;
262  } else {
263  pclk = (uint64_t)CLKPWR_GetPCLK(CLKPWR_PCLKSEL_QEI);
264  pclk = (pclk /(1000000/QEIReloadStruct->ReloadValue)) - 1;
265  QEIx->QEILOAD = (uint32_t)pclk;
266  }
267 }
268 
269 /*********************************************************************/
274 uint32_t QEI_GetTimer(LPC_QEI_TypeDef *QEIx)
275 {
276  CHECK_PARAM(PARAM_QEIx(QEIx));
277  return (QEIx->QEITIME);
278 }
279 
280 /*********************************************************************/
285 uint32_t QEI_GetVelocity(LPC_QEI_TypeDef *QEIx)
286 {
287  CHECK_PARAM(PARAM_QEIx(QEIx));
288  return (QEIx->QEIVEL);
289 }
290 
291 /*********************************************************************/
298 uint32_t QEI_GetVelocityCap(LPC_QEI_TypeDef *QEIx)
299 {
300  CHECK_PARAM(PARAM_QEIx(QEIx));
301  return (QEIx->QEICAP);
302 }
303 
304 /*********************************************************************/
310 void QEI_SetVelocityComp(LPC_QEI_TypeDef *QEIx, uint32_t ulVelComp)
311 {
312  CHECK_PARAM(PARAM_QEIx(QEIx));
313  QEIx->VELCOMP = ulVelComp;
314 }
315 
316 /*********************************************************************/
323 void QEI_SetDigiFilter(LPC_QEI_TypeDef *QEIx, uint32_t ulSamplingPulse)
324 {
325  CHECK_PARAM(PARAM_QEIx(QEIx));
326  QEIx->FILTER = ulSamplingPulse;
327 }
328 
329 /*********************************************************************/
353 FlagStatus QEI_GetIntStatus(LPC_QEI_TypeDef *QEIx, uint32_t ulIntType)
354 {
355  CHECK_PARAM(PARAM_QEIx(QEIx));
356  CHECK_PARAM(PARAM_QEI_INTFLAG(ulIntType));
357 
358  return((QEIx->QEIINTSTAT & ulIntType) ? SET : RESET);
359 }
360 
361 /*********************************************************************/
387 void QEI_IntCmd(LPC_QEI_TypeDef *QEIx, uint32_t ulIntType, FunctionalState NewState)
388 {
389  CHECK_PARAM(PARAM_QEIx(QEIx));
390  CHECK_PARAM(PARAM_QEI_INTFLAG(ulIntType));
392 
393  if (NewState == ENABLE) {
394  QEIx->QEIIES = ulIntType;
395  } else {
396  QEIx->QEIIEC = ulIntType;
397  }
398 }
399 
400 
401 /*********************************************************************/
424 void QEI_IntSet(LPC_QEI_TypeDef *QEIx, uint32_t ulIntType)
425 {
426  CHECK_PARAM(PARAM_QEIx(QEIx));
427  CHECK_PARAM(PARAM_QEI_INTFLAG(ulIntType));
428 
429  QEIx->QEISET = ulIntType;
430 }
431 
432 /*********************************************************************/
455 void QEI_IntClear(LPC_QEI_TypeDef *QEIx, uint32_t ulIntType)
456 {
457  CHECK_PARAM(PARAM_QEIx(QEIx));
458  CHECK_PARAM(PARAM_QEI_INTFLAG(ulIntType));
459 
460  QEIx->QEICLR = ulIntType;
461 }
462 
463 
464 /*********************************************************************/
474 uint32_t QEI_CalculateRPM(LPC_QEI_TypeDef *QEIx, uint32_t ulVelCapValue, uint32_t ulPPR)
475 {
476  uint64_t rpm, clock, Load, edges;
477 
478  // Get current Clock rate for timer input
479  clock = (uint64_t)CLKPWR_GetPCLK(CLKPWR_PCLKSEL_QEI);
480  // Get Timer load value (velocity capture period)
481  Load = (uint64_t)(QEIx->QEILOAD + 1);
482  // Get Edge
483  edges = (uint64_t)((QEIx->QEICONF & QEI_CONF_CAPMODE) ? 4 : 2);
484  // Calculate RPM
485  rpm = ((clock * ulVelCapValue * 60) / (Load * ulPPR * edges));
486 
487  return (uint32_t)(rpm);
488 }
489 
490 
495 #endif /* _QEI */
496 
501 /* --------------------------------- End Of File ------------------------------ */
502