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

📄 lpc177x_8x_i2c.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 3 页
字号:
/**********************************************************************
* $Id$		lpc177x_8x_i2c.c			2011-06-02
*//**
* @file		lpc177x_8x_i2c.c
* @brief	Contains all functions support for I2C 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 I2C
 * @{
 */
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc177x_8x_libcfg.h"
#else
#include "lpc177x_8x_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _I2C

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

/* 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);

/* I2C set clock (hz) */
static void I2C_SetClock (LPC_I2C_TypeDef *I2Cx, uint32_t target_clock);

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

/********************************************************************//**
 * @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)
{
	// Reset STA, STO, SI
	I2Cx->CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;

	// Enter to Master Transmitter mode
	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|I2C_I2CONSET_AA;

	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)
{
	uint32_t CodeStatus = I2Cx->STAT & I2C_STAT_CODE_BITMASK;

	if((CodeStatus != I2C_I2STAT_M_TX_START) &&
		(CodeStatus != I2C_I2STAT_M_TX_RESTART) &&
		(CodeStatus != I2C_I2STAT_M_TX_SLAW_ACK)  &&
		(CodeStatus != I2C_I2STAT_M_TX_DAT_ACK)  )
	{
		return CodeStatus;
	}
	
	/* 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->CONSET = I2C_I2CONSET_AA;

	I2Cx->CONCLR = I2C_I2CONCLR_SIC;

	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)
{
	*retdat = (uint8_t) (I2Cx->DAT & I2C_I2DAT_BITMASK);
	
	if (ack == TRUE)
	{
		I2Cx->CONSET = I2C_I2CONSET_AA;
	}
	else
	{
		I2Cx->CONCLR = I2C_I2CONCLR_AAC;
	}

	I2Cx->CONCLR = I2C_I2CONCLR_SIC;
	
	return (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
}

/*********************************************************************//**
 * @brief 		Setup clock rate for I2C peripheral
 * @param[in] 	I2Cx	I2C peripheral selected, should be:
 * 				- LPC_I2C0
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @param[in]	target_clock : clock of SSP (Hz)
 * @return 		None
 ***********************************************************************/
static void I2C_SetClock (LPC_I2C_TypeDef *I2Cx, uint32_t target_clock)
{
	uint32_t temp;

	temp = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER) / target_clock;

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

	I2Cx->SCLL = (uint32_t)(temp - I2Cx->SCLH);
}


/* 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(en_I2C_unitId i2cId, uint32_t clockrate)
{
	uint32_t clkSetting;

	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 */
	I2C_SetClock(I2Cx,clockrate);

    /* 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(en_I2C_unitId 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(en_I2C_unitId i2cId, en_I2C_Mode Mode, FunctionalState NewState)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);

	if (NewState == ENABLE)
	{
		if(Mode != I2C_SLAVE_MODE)
			I2Cx->CONSET = I2C_I2CONSET_I2EN;
		else
			I2Cx->CONSET = I2C_I2CONSET_I2EN | I2C_I2CONSET_AA;
	}
	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 (en_I2C_unitId 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 		Handle I2C Master states.
 * @param[in]	I2Cx	I2C peripheral selected, should be:
 * 				- I2C_0
 * 				- I2C_1
 * 				- I2C_2
 * @param[in]	CodeStatus	I2C state
 * @param[in]	TransferCfg   Pointer to a I2C_S_SETUP_Type structure that
 * 								contains specified information about the
 * 								configuration for master transfer.
 * @return 		It can be
 *				- I2C_OK
 *				-I2C_BYTE_RECV
 *				-I2C_BYTE_SENT
 *				-I2C_SEND_END
 *				-I2C_RECV_END
 *				- I2C_ERR
 *				- I2C_NAK_RECV
 **********************************************************************/
int32_t I2C_MasterHanleStates(en_I2C_unitId i2cId, uint32_t CodeStatus, I2C_M_SETUP_Type *TransferCfg)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
	uint8_t *txdat;
	uint8_t *rxdat;
	uint8_t tmp;
	int32_t Ret = I2C_OK;
	

⌨️ 快捷键说明

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