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

📄 lpc_i2c.c

📁 给大家提供一个在inram/exram中调试的示例,在周公的lpc2200上调试过.
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
 *         BU MMS China, Philips Semiconductor Software Support
 *         Embest info&Tech Co. Software Support
 *---------------------------------------------------------------------------
 * The software is delivered "AS IS" without warranty or condition of any
 * kind, either express, implied or statutory.  Everybody can use it as 
 * it is opened and without copyright. We will not take any law responsibility
 * for any problem produced by using this software.
 *---------------------------------------------------------------------------                                                        
 *    File name: 	LPC_I2C.c
 *    Description: 	Define API for I2C module
 *                                                                                      
 *    History:                                                                     
 *    1. Date: 		Nov 22, 2004                                              
 *       Author: 	Shawn Zhang                                                    
 *       Description: Create
 *     
 *	2. Date: 		Dec 07, 2004                                              
 *       Author: 	Shawn Zhang                                                    
 *       Description: Modify the slave address description
 *
 *	$Revision: 1.0 $
 **********************************************************************/

#include "LPC_I2C.h"

LPC_I2C_Mode_t	I2CMode;
volatile int		I2CState;
LPC_I2C_Msg_t	I2CMsg;

/*************************************************************************
 * Function Name: I2C_EnableI2C
 * Parameters: void
 * Return: void
 * Description: Enable I2C device.
 *************************************************************************/
__inline void I2C_EnableI2C(void)
{
	I2C_I2CONSET |= (1<<6);				
}

/*************************************************************************
 * Function Name: I2C_DisableI2C
 * Parameters: void
 * Return: void
 * Description: Disable I2C device.
 *************************************************************************/
__inline void I2C_DisableI2C(void)
{
	I2C_I2CONCLR |= (1<<6);				
}

/*************************************************************************
 * Function Name: __I2C_SetFlag
 * Parameters: int FlagType -- AA, INT and START and STOP
 *
 * Return: void
 * Description: Set the flag.
 *
 *************************************************************************/
static __inline void __I2C_SetFlag (int FlagType)
{
	I2C_I2CONSET = FlagType; 
}

/*************************************************************************
 * Function Name: __I2C_ClearFlag
 * Parameters: int FlagType -- AA, INT and START (Excl. STOP)
 *
 * Return: void
 * Description: Clear the flag.
 *
 *************************************************************************/
static __inline void __I2C_ClearFlag (int FlagType)
{
	I2C_I2CONCLR = FlagType; 
}

/*************************************************************************
 * Function Name: __I2C_SendData
 * Parameters: void
 * Return: void
 * Description: Load data to I2CDAT. Just used in I2C module.
 *************************************************************************/
static __inline void __I2C_SendData(LPC_BYTE data)
{
	I2C_I2DAT = data;
}

/*************************************************************************
 * Function Name: __I2C_ReceiveData
 * Parameters: void
 * Return: void
 * Description: Load data from I2CDAT. Just used in I2C module.
 *************************************************************************/
static __inline LPC_BYTE __I2C_ReceiveData(void)
{
	return I2C_I2DAT;
}

/*************************************************************************
 * Function Name: I2C_InitMaster
 * Parameters: lpc_uint16 BusSpeed
 *
 * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: Initialize the current device as I2C bus master.
 *
 *************************************************************************/
int I2C_InitMaster (lpc_uint32 BusSpeed)
{

	if (BusSpeed > I2C_MAXSPEED)
		return 1;

	// Bit Frequency = Fplk / (I2C_I2SCLH + I2C_I2SCLL)
	I2C_I2SCLH = ((SYS_GetFpclk() / BusSpeed) + 1) / 2 ;
	I2C_I2SCLL = (SYS_GetFpclk() / BusSpeed ) / 2 ;

	if (I2C_I2SCLH < 4 || I2C_I2SCLL < 4)
	{
		return 1;
	}

	I2CState = I2C_IDLE;
	I2CMode =   I2C_MASTER;

	/* Enable SCL and SDA, bit 4~7=0101 */
	PINSEL0 &= ~0xF0;
	PINSEL0 |= 0x50;
	
	return 0;
}

/*************************************************************************
 * Function Name: I2C_InitSlave
 * Parameters: lpc_uint16 BusSpeed
 *			  lpc_uint32 SlaveAddr
 *
 * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: Initialize the current device as I2C bus slave.
 *
 *************************************************************************/
int I2C_InitSlave (lpc_uint32 BusSpeed, lpc_uint32 SlaveAddr)
{

	if (BusSpeed > I2C_MAXSPEED)
		return 1;

	// Bit Frequency = Fplk / (I2C_I2SCLH + I2C_I2SCLL)
	I2C_I2SCLH = ((SYS_GetFpclk() / BusSpeed) + 1) / 2 ;
	I2C_I2SCLL = (SYS_GetFpclk() / BusSpeed ) / 2 ;

	if (I2C_I2SCLH < 4 || I2C_I2SCLL < 4)
	{
		return 1;
	}
	I2C_I2ADR = SlaveAddr;
	
	I2CState = I2C_IDLE;
	I2CMode =   I2C_SLAVE;

	/* Enable SCL and SDA, bit 4~7=0101 */
	PINSEL0 &= ~0xF0;
	PINSEL0 |= 0x50;
	
	return 0;
}

/*************************************************************************
 * Function Name: I2C_MasterWrite
 * Parameters: lpc_uint8 addr -- the slave address which you send message to
 *			  lpc_uint8 *pMsg -- the point to the message
 *			  lpc_uint32 numMsg -- the byte number of the message
 * Return: int 
 *             	0: success
 *			non-zero: error number
 *
 * Description: Transmit messages
 *
 *************************************************************************/
int I2C_MasterWrite (lpc_uint8 addr, lpc_uint8 *pMsg , lpc_uint32 numMsg)
{
	return I2C_Transfer (addr, pMsg , numMsg, WRITE, 0);
}

/*************************************************************************
 * Function Name: I2C_SlaveWrite
 * Parameters: 
 *			  lpc_uint8 *pMsg -- the point to the message
 *			  lpc_uint32 numMsg -- the byte number of the message
 * Return: int 
 *             	0: success
 *			non-zero: error number
 *
 * Description: Transmit messages
 *
 *************************************************************************/
int I2C_SlaveWrite (lpc_uint8 *pMsg , lpc_uint32 numMsg)
{
	return I2C_Transfer (NULL, pMsg , numMsg, WRITE, 0);
}

/*************************************************************************
 * Function Name: I2C_MasterRead
 * Parameters: lpc_uint8 addr -- the slave address which you send message to
 *			  lpc_uint8 *pMsg -- the point to the message
 *			  lpc_uint32 numMsg -- the byte number of the message
 * Return: int 
 *             	0: success
 *			non-zero: error number
 *
 * Description: Receive messages
 *
 *************************************************************************/
int I2C_MasterRead (lpc_uint8 addr, lpc_uint8 *pMsg , lpc_uint32 numMsg)
{
	return I2C_Transfer (addr, pMsg , numMsg, READ, 0);
}

/*************************************************************************
 * Function Name: I2C_SlaveRead
 * Parameters: 
 *			  lpc_uint8 *pMsg -- the point to the message
 *			  lpc_uint32 numMsg -- the byte number of the message
 * Return: int 
 *             	0: success
 *			non-zero: error number
 *
 * Description: Receive messages
 *
 *************************************************************************/
int I2C_SlaveRead (lpc_uint8 *pMsg , lpc_uint32 numMsg)
{
	return I2C_Transfer (NULL, pMsg , numMsg, READ, 0);
}

/*************************************************************************
 * Function Name: I2C_Transfer
 * Parameters: lpc_uint8 addr -- the slave address which you send message to
 *			  lpc_uint8 *pMsg -- the point to the message
 *			  lpc_uint32 numMsg -- the byte number of the message
 *			  LPC_I2C_TransMode_t transMode -- Read, Write, Write then read
 *			  lpc_uint32 numWrite -- this is only for "Write then read" mode
 *			  
 * Return: int 
 *             	0: success
 *			non-zero: error number
 *
 * Description: Transfer messages
 *
 *************************************************************************/
int I2C_Transfer (lpc_uint8 addr, lpc_uint8 *pMsg , lpc_uint32 numMsg, 
	LPC_I2C_TransMode_t transMode, lpc_uint32 numWrite)
{
	
	if ( I2CMode == I2C_MASTER)
	{
		if (transMode == WRITETHENREAD)
		{
			if (numWrite >= numMsg)
				return 1;
			else
				I2CMsg.nrWriteBytes = numWrite;
		}
		else
			I2CMsg.nrWriteBytes= 0;
	
		I2CMsg.buf = pMsg;
		I2CMsg.nrBytes= numMsg;
		I2CMsg.address = addr;
		I2CMsg.transMode= transMode;
		I2CMsg.dataCount = 0;

		VIC_EnableInt(INT_I2C);		// Open interrupt
		
		I2C_EnableI2C();
		__I2C_ClearFlag(I2C_MSG_START | I2C_MSG_SI | I2C_MSG_AA);
		__I2C_SetFlag(I2C_MSG_START);
		
		I2CState = I2C_BUSY;	

		while(I2CState != I2C_OK);
		if (I2CState == I2C_OK)
			return 0;
		else
			return 1;
		
	}
	else if ( I2CMode == I2C_SLAVE )

⌨️ 快捷键说明

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