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

📄 lpc177x_8x_i2c.c

📁 优龙LPC1788开发板资料
💻 C
📖 第 1 页 / 共 4 页
字号:
/***********************************************************************//**
 * @file		lpc177x_8x_i2c.c
 * @brief		Contains all functions support for I2C firmware library on LPC178X
 * @version		1.0
 * @date		21. May. 2010
 * @author		NXP MCU SW Application Team
 **************************************************************************
 * 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.
 **********************************************************************/

/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup I2C
 * @{
 */

/* Includes ------------------------------------------------------------------- */
#include "lpc177x_8x_i2c.h"
#include "lpc177x_8x_clkpwr.h"
#include "lpc177x_8x_pinsel.h"
#include "SA56004.h"

#define SA56004_I2C		I2C_0
#define Sa56004_I2CADDR (0x98 >> 1)

/* Private Types -------------------------------------------------------------- */
/** @defgroup I2C_Private_Types I2C Private Types
 * @{
 */

/**
 * @brief I2C device configuration structure type
 */
typedef struct
{
  uint32_t      txrx_setup; 						/* Transmission setup */
  int32_t		dir;								/* Current direction phase, 0 - write, 1 - read */
} I2C_CFG_T;

/**
 * @}
 */

/* Private Variables ---------------------------------------------------------- */
/**
 * @brief II2C driver data for I2C0, I2C1 and I2C2
 */
static I2C_CFG_T i2cdat[3];

static uint32_t I2C_MasterComplete[3];
static uint32_t I2C_SlaveComplete[3];

static uint32_t I2C_MonitorBufferIndex;

/* Private Functions ---------------------------------------------------------- */

/* Get pointer to expected I2C */
static LPC_I2C_TypeDef* I2C_GetPointer(en_I2C_unitId compId);

/* Generate a start condition on I2C bus (in master mode only) */
static uint32_t I2C_Start (LPC_I2C_TypeDef *I2Cx);

/* Generate a stop condition on I2C bus (in master mode only) */
static void I2C_Stop (LPC_I2C_TypeDef *I2Cx);

/* I2C send byte subroutine */
static uint32_t I2C_SendByte (LPC_I2C_TypeDef *I2Cx, uint8_t databyte);

/* I2C get byte subroutine */
static uint32_t I2C_GetByte (LPC_I2C_TypeDef *I2Cx, uint8_t *retdat, Bool ack);

/*--------------------------------------------------------------------------------*/

/********************************************************************//**
 * @brief		Convert from I2C peripheral to number
 * @param[in]	I2Cx: I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @return 		I2C number, could be: 0..2
 *********************************************************************/
static LPC_I2C_TypeDef* I2C_GetPointer(en_I2C_unitId compId)
{
	LPC_I2C_TypeDef* pI2C;

	switch (compId)
	{
		case I2C_0:
			pI2C = LPC_I2C0;
			break;

		case I2C_1:
			pI2C = LPC_I2C1;
			break;

		case I2C_2:
			pI2C = LPC_I2C2;
			break;

		default:
			pI2C = NULL;
			break;
	}

	return pI2C;
}

/********************************************************************//**
 * @brief		Generate a start condition on I2C bus (in master mode only)
 * @param[in]	I2Cx: I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @return 		value of I2C status register after generate a start condition
 *********************************************************************/
static uint32_t I2C_Start (LPC_I2C_TypeDef *I2Cx)
{
	I2Cx->CONCLR = I2C_I2CONCLR_SIC;

	I2Cx->CONSET = I2C_I2CONSET_STA;

	// Wait for complete
	while (!(I2Cx->CONSET & I2C_I2CONSET_SI));

	I2Cx->CONCLR = I2C_I2CONCLR_STAC;

	return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
}

/********************************************************************//**
 * @brief		Generate a stop condition on I2C bus (in master mode only)
 * @param[in]	I2Cx: I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @return 		None
 *********************************************************************/
static void I2C_Stop (LPC_I2C_TypeDef *I2Cx)
{
	/* Make sure start bit is not active */
	if (I2Cx->CONSET & I2C_I2CONSET_STA)
	{
		I2Cx->CONCLR = I2C_I2CONCLR_STAC;
	}

	I2Cx->CONSET = I2C_I2CONSET_STO;

	I2Cx->CONCLR = I2C_I2CONCLR_SIC;
}

/********************************************************************//**
 * @brief		Send a byte
 * @param[in]	I2Cx: I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[in]	databyte: number of byte
 * @return 		value of I2C status register after sending
 *********************************************************************/
static uint32_t I2C_SendByte (LPC_I2C_TypeDef *I2Cx, uint8_t databyte)
{
	/* Make sure start bit is not active */
	if (I2Cx->CONSET & I2C_I2CONSET_STA)
	{
		I2Cx->CONCLR = I2C_I2CONCLR_STAC;
	}

	I2Cx->DAT = databyte & I2C_I2DAT_BITMASK;

	I2Cx->CONCLR = I2C_I2CONCLR_SIC;

	while (!(I2Cx->CONSET & I2C_I2CONSET_SI));

	return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
}

/********************************************************************//**
 * @brief		Get a byte
 * @param[in]	I2Cx: I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[out]	retdat	pointer to return data
 * @param[in]	ack		assert acknowledge or not, should be: TRUE/FALSE
 * @return 		value of I2C status register after sending
 *********************************************************************/
static uint32_t I2C_GetByte (LPC_I2C_TypeDef *I2Cx, uint8_t *retdat, Bool ack)
{
	if (ack == TRUE)
	{
		I2Cx->CONSET = I2C_I2CONSET_AA;
	}
	else
	{
		I2Cx->CONCLR = I2C_I2CONCLR_AAC;
	}

	I2Cx->CONCLR = I2C_I2CONCLR_SIC;

	while (!(I2Cx->CONSET & I2C_I2CONSET_SI));

	*retdat = (uint8_t) (I2Cx->DAT & I2C_I2DAT_BITMASK);

	return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
}


/* End of Private Functions --------------------------------------------------- */


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

/********************************************************************//**
 * @brief		Initializes the I2Cx peripheral with specified parameter.
 * @param[in]	I2Cx	I2C peripheral selected, should be
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[in]	clockrate Target clock rate value to initialized I2C
 * 				peripheral (Hz)
 * @return 		None
 *********************************************************************/
void I2C_Init(uint8_t i2cId, uint32_t clockrate)
{
	uint32_t clkSetting, clkRate;

	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);

	switch (i2cId)
	{
		case I2C_0:
			clkSetting = CLKPWR_PCONP_PCI2C0;
			break;


		case I2C_1:
			clkSetting = CLKPWR_PCONP_PCI2C1;
			break;

		case I2C_2:
			clkSetting = CLKPWR_PCONP_PCI2C2;
			break;

		default:

			return;
	}

	CLKPWR_ConfigPPWR (clkSetting, ENABLE);

	/* Set clock rate */
	clkRate= CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER) / clockrate;

	/* Set the I2C clock value to register */
	I2Cx->SCLH = (uint32_t)(clkRate / 2);

	I2Cx->SCLL = (uint32_t)(clkRate - I2Cx->SCLH);

    /* Set I2C operation to default */
    I2Cx->CONCLR = (I2C_I2CONCLR_AAC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_I2ENC);
}

/*********************************************************************//**
 * @brief		De-initializes the I2C peripheral registers to their
 *                  default reset values.
 * @param[in]	I2Cx	I2C peripheral selected, should be
 *  			- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @return 		None
 **********************************************************************/
void I2C_DeInit(uint8_t i2cId)
{
	uint32_t clkSetting;

	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);

	/* Disable I2C control */
	I2Cx->CONCLR = I2C_I2CONCLR_I2ENC;

	switch (i2cId)
	{
		case I2C_0:
			clkSetting = CLKPWR_PCONP_PCI2C0;
			break;


		case I2C_1:
			clkSetting = CLKPWR_PCONP_PCI2C1;
			break;

		case I2C_2:
			clkSetting = CLKPWR_PCONP_PCI2C2;
			break;

		default:

			return;
	}

	CLKPWR_ConfigPPWR (clkSetting, DISABLE);
}

/*********************************************************************//**
 * @brief		Enable or disable I2C peripheral's operation
 * @param[in]	I2Cx I2C peripheral selected, should be
 *  			- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[in]	NewState New State of I2Cx peripheral's operation
 * @return 		none
 **********************************************************************/
void I2C_Cmd(uint8_t i2cId, FunctionalState NewState)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);

	if (NewState == ENABLE)
	{
		I2Cx->CONSET = I2C_I2CONSET_I2EN;
	}
	else
	{
		I2Cx->CONCLR = I2C_I2CONCLR_I2ENC;
	}
}

/*********************************************************************//**
 * @brief 		Enable/Disable interrupt for I2C peripheral
 * @param[in]	I2Cx	I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[in]	NewState	New State of I2C peripheral interrupt in NVIC core
 * 				should be:
 * 				- ENABLE: enable interrupt for this I2C peripheral
 * 				- DISABLE: disable interrupt for this I2C peripheral
 * @return 		None
 **********************************************************************/
void I2C_IntCmd (uint8_t i2cId, Bool NewState)
{
	IRQn_Type irq;

	switch (i2cId)
	{
		case I2C_0:
			irq = I2C0_IRQn;
			break;

		case I2C_1:
			irq = I2C1_IRQn;
			break;

		case I2C_2:
			irq = I2C2_IRQn;
			break;

		default:

			return;
	}

	if (NewState)
	{
		NVIC_EnableIRQ(irq);
	}
	else
	{
		NVIC_DisableIRQ(irq);
	}

    return;
}


/*********************************************************************//**
 * @brief 		General Master Interrupt handler for I2C peripheral
 * @param[in]	I2Cx	I2C peripheral selected, should be:
 * 				- LPC_I2C
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @return 		None
 **********************************************************************/
void I2C_MasterHandler(uint8_t i2cId)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);

	uint8_t returnCode;
	I2C_M_SETUP_Type *txrx_setup;

	txrx_setup = (I2C_M_SETUP_Type *) i2cdat[i2cId].txrx_setup;

	returnCode = (I2Cx->STAT & I2C_STAT_CODE_BITMASK);

	// Save current status
	txrx_setup->status = returnCode;

	// there's no relevant information
	if (returnCode == I2C_I2STAT_NO_INF)
	{
		I2Cx->CONCLR = I2C_I2CONCLR_SIC;
		return;
	}

	/* ----------------------------- TRANSMIT PHASE --------------------------*/
	if (i2cdat[i2cId].dir == 0)
	{
		switch (returnCode)
		{
			/* A start/repeat start condition has been transmitted -------------------*/
			case I2C_I2STAT_M_TX_START:

			case I2C_I2STAT_M_TX_RESTART:
				I2Cx->CONCLR = I2C_I2CONCLR_STAC;
				/*
				 * If there's any transmit data, then start to
				 * send SLA+W right now, otherwise check whether if there's
				 * any receive data for next state.
				 */
				if ((txrx_setup->tx_data != NULL) && (txrx_setup->tx_length != 0))
				{
					I2Cx->DAT = (txrx_setup->sl_addr7bit << 1);
					I2Cx->CONCLR = I2C_I2CONCLR_SIC;
				}
				else
				{
					goto next_stage;
				}

				break;

			/* SLA+W has been transmitted, ACK has been received ----------------------*/
			case I2C_I2STAT_M_TX_SLAW_ACK:

			/* Data has been transmitted, ACK has been received */

⌨️ 快捷键说明

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