NXP's driver library for LPC17xx, ported to mbed's online compiler. Not tested! I had to fix a lot of warings and found a couple of pretty obvious bugs, so the chances are there are more. Original: http://ics.nxp.com/support/documents/microcontrollers/zip/lpc17xx.cmsis.driver.library.zip

Dependencies:   mbed

Committer:
igorsk
Date:
Wed Feb 17 16:22:39 2010 +0000
Revision:
0:1063a091a062

        

Who changed what in which revision?

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