⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpc177x_8x_qei.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************************
* $Id$		lpc177x_8x_qei.c			2011-06-02
*//**
* @file		lpc177x_8x_qei.c
* @brief	Contains all functions support for QEI firmware library
*			on LPC177x_8x
* @version	1.0
* @date		02. June. 2011
* @author	NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup QEI
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc177x_8x_libcfg.h"
#else
#include "lpc177x_8x_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _QEI

/* Includes ------------------------------------------------------------------- */
#include "lpc177x_8x_qei.h"
#include "lpc177x_8x_clkpwr.h"

/* Private Types -------------------------------------------------------------- */
/** @defgroup QEI_Private_Types QEI Private Types
 * @{
 */

/**
 * @brief QEI configuration union type definition
 */
typedef union {
	QEI_CFG_Type bmQEIConfig;
	uint32_t ulQEIConfig;
} QEI_CFGOPT_Type;

/**
 * @}
 */

LPC_QEI_TypeDef* QEI_GetPointer(uint8_t qeiId);


/* Public Functions ----------------------------------------------------------- */
/** @addtogroup QEI_Public_Functions
 * @{
 */

/*********************************************************************//**
 * @brief		Get the point to typedef of QEI component
 * @param[in]	qeiId			The Id of the expected QEI component
 * 								It should be 0 (zero) always with LPC177x_8x
 * @return		None
 **********************************************************************/
LPC_QEI_TypeDef* QEI_GetPointer(uint8_t qeiId)
{
	LPC_QEI_TypeDef* pQei = NULL;

	if(qeiId == 0)
	{
		pQei = LPC_QEI;
	}

	return pQei;
}


/*********************************************************************//**
 * @brief		Resets value for each type of QEI value, such as velocity,
 * 				counter, position, etc..
 * @param[in]	qeiId			The Id of the expected QEI component
 * 								It should be 0 (zero) always with LPC177x_8x
 *
 * @param[in]	ulResetType		QEI Reset Type, should be one of the following:
 * 								- QEI_RESET_POS: Reset Position Counter
 * 								- QEI_RESET_POSOnIDX: Reset Position Counter on Index signal
 * 								- QEI_RESET_VEL: Reset Velocity
 * 								- QEI_RESET_IDX: Reset Index Counter
 * @return		None
 **********************************************************************/
void QEI_Reset(uint8_t qeiId, uint32_t ulResetType)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	pQei->CON = ulResetType;
}

/*********************************************************************//**
 * @brief		Initializes the QEI peripheral according to the specified
*               parameters in the QEI_ConfigStruct.
* @param[in]	qeiId			The Id of the expected QEI component
* 								It should be 0 (zero) always with LPC177x_8x
*
* @param[in]	QEI_ConfigStruct	Pointer to a QEI_CFG_Type structure
*                    that contains the configuration information for the
*                    specified QEI peripheral
 * @return		None
 **********************************************************************/
void QEI_Init(uint8_t qeiId, QEI_CFG_Type *QEI_ConfigStruct)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	/* Set up clock and power for QEI module */
	CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCQEI, ENABLE);

	// Reset all remaining value in QEI peripheral

	pQei->MAXPOS = 0x00;
	pQei->CMPOS0 = 0x00;
	pQei->CMPOS1 = 0x00;
	pQei->CMPOS2 = 0x00;
	pQei->INXCMP0 = 0x00;
	pQei->VELCOMP = 0x00;

	pQei->LOAD = 0x00;
	pQei->CON = QEI_CON_RESP | QEI_CON_RESV | QEI_CON_RESI;

	pQei->FILTERPHA = 0x00;
	pQei->FILTERPHB = 0x00;
	pQei->FILTERINX = 0x00;

	// Disable all Interrupt
	pQei->IEC = QEI_IECLR_BITMASK;

	// Clear all Interrupt pending
	pQei->CLR = QEI_INTCLR_BITMASK;

	// Set QEI configuration value corresponding to its setting up value
	pQei->CONF = ((QEI_CFGOPT_Type *)QEI_ConfigStruct)->ulQEIConfig;
}


/*********************************************************************//**
 * @brief		De-initializes the QEI peripheral registers to their
 *                  default reset values.
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @return 		None
 **********************************************************************/
void QEI_DeInit(uint8_t qeiId)
{
	/* Turn off clock and power for QEI module */
	CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCQEI, DISABLE);
}


/*****************************************************************************//**
* @brief		Fills each QIE_InitStruct member with its default value:
* 				- DirectionInvert = QEI_DIRINV_NONE
* 				- SignalMode = QEI_SIGNALMODE_QUAD
* 				- CaptureMode = QEI_CAPMODE_4X
* 				- InvertIndex = QEI_INVINX_NONE
* @param[in]	QIE_InitStruct Pointer to a QEI_CFG_Type structure
*                    which will be initialized.
* @return		None
*******************************************************************************/
void QEI_GetCfgDefault(QEI_CFG_Type *QIE_InitStruct)
{
	QIE_InitStruct->CaptureMode = QEI_CAPMODE_4X;
	QIE_InitStruct->DirectionInvert = QEI_DIRINV_NONE;
	QIE_InitStruct->InvertIndex = QEI_INVINX_NONE;
	QIE_InitStruct->SignalMode = QEI_SIGNALMODE_QUAD;
}


/*********************************************************************//**
 * @brief		Check whether if specified flag status is set or not
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @param[in]	ulFlagType	Status Flag Type, should be one of the following:
 * 							- QEI_STATUS_DIR: Direction Status
 * @return		New Status of this status flag (SET or RESET)
 **********************************************************************/
FlagStatus QEI_GetStatus(uint8_t qeiId, uint32_t ulFlagType)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	return ((pQei->STAT & ulFlagType) ? SET : RESET);
}

/*********************************************************************//**
 * @brief		Get current position value in QEI peripheral
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @return		Current position value of QEI peripheral
 **********************************************************************/
uint32_t QEI_GetPosition(uint8_t qeiId)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	return (pQei->POS);
}

/*********************************************************************//**
 * @brief		Set max position value for QEI peripheral
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @param[in]	ulMaxPos	Max position value to set
 * @return		None
 **********************************************************************/
void QEI_SetMaxPosition(uint8_t qeiId, uint32_t ulMaxPos)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	pQei->MAXPOS = ulMaxPos;
}

/*********************************************************************//**
 * @brief		Set position compare value for QEI peripheral
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @param[in]	bPosCompCh	Compare Position channel, should be:
 * 							- QEI_COMPPOS_CH_0: QEI compare position channel 0
 * 							- QEI_COMPPOS_CH_1: QEI compare position channel 1
 * 							- QEI_COMPPOS_CH_2: QEI compare position channel 2
 * @param[in]	ulPosComp	Compare Position value to set
 * @return		None
 **********************************************************************/
void QEI_SetPositionComp(uint8_t qeiId, uint8_t bPosCompCh, uint32_t ulPosComp)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
	uint32_t *tmp;

	tmp = (uint32_t *) (&(pQei->CMPOS0) + bPosCompCh * 4);
	*tmp = ulPosComp;
}

/*********************************************************************//**
 * @brief		Get current index counter of QEI peripheral
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @return		Current value of QEI index counter
 **********************************************************************/
uint32_t QEI_GetIndex(uint8_t qeiId)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	return (pQei->INXCNT);
}

/*********************************************************************//**
 * @brief		Set value for index compare in QEI peripheral
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @param[in]	ulIndexComp		Compare Index Value to set
 * @return		None
 **********************************************************************/
void QEI_SetIndexComp(uint8_t qeiId, uint32_t ulIndexComp)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);

	pQei->INXCMP0 = ulIndexComp;
}

/*********************************************************************//**
 * @brief		Set timer reload value for QEI peripheral. When the velocity timer is
 * 				over-flow, the value that set for Timer Reload register will be loaded
 * 				into the velocity timer for next period. The calculated velocity in RPM
 * 				therefore will be affect by this value.
 * @param[in]	 qeiId			 The Id of the expected QEI component
 *								 It should be 0 (zero) always with LPC177x_8x
 *
 * @param[in]	QEIReloadStruct	QEI reload structure
 * @return		None
 **********************************************************************/
void QEI_SetTimerReload(uint8_t qeiId, QEI_RELOADCFG_Type *QEIReloadStruct)
{
	LPC_QEI_TypeDef* pQei = QEI_GetPointer(qeiId);
	uint64_t pclk;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -