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

📄 i2c.c

📁 Z228芯片是ARM926ej-s的内核
💻 C
字号:
/********************************************/
/* I2C.c: implementation of the I2C API.		*/
/* Author   : qzsu													*/
/* Time     : 2006-08-21										*/
/********************************************/

#include <string.h>
#include "I2C.h"

#define PIN_MASKBASE	0x20020000
#if 1

//#define unsigned int u32


// Receive FIFO Completely Full
bool IsRFF(void);

// Receive FIFO Not Empty
bool IsRFNE(void);

// Transmit FIFO Completely Empty
bool IsTFE(void);


/* The following API are used to access the State Register*/

// Receive FIFO Completely Full
bool IsRFF(void)
{
	if(I2C_STATUS & RFF)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// Receive FIFO Not Empty
bool IsRFNE(void)
{
	if(I2C_STATUS & RFNE)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// Transmit FIFO Completely Empty
bool IsTFE(void)
{
	if(I2C_STATUS & TFE)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//end the state Register API


/* brief : Initialize the I2C module */
void InitI2C(void)
{
	// Define the Initialize parameters 

	//Disable the I2C MASTER
	I2C_DISABLE;
	
	//Set the address if targeted as a slave
//	I2C_SAR(0x0045);
	
	//Set the control register for the transfer
	I2C_CON(0x0003);
	
	//-: Set the High period for the SCL 
	//(we have 10 MHz ic_clk) if we want 100kbps , SS_HCNT = 0x0028
  //Set the Low period for the SCL
  //(we have 10 MHz ic_clk) if we want 100kbps , SS_LCNG =0x002f
#if 1
	I2C_SS_HCNT(0x0028);
	I2C_SS_LCNT(0x002f);
#endif

#if 0
//50k
	I2C_SS_HCNT(0x0050);
	I2C_SS_LCNT(0x005E);
#endif

#if 0  
//25K
	I2C_SS_HCNT(0x0140);
	I2C_SS_LCNT(0x0178);
#endif	
  //Set the address for the target register(slave)
//  I2C_TAR(0x0056);

  //Enable interrupts
  I2C_INTR_MASK(0);

  //Set Rx FIFO Threshold 
  I2C_RX_TL(0);

  //Set Tx FIFO Threshold 
  I2C_TX_TL(0);
	
	//Enable the I2C MASTER
	I2C_ENABLE;

}
#endif


/* brief: This API is used to transmit data 
** para : threhold -- the  threhole of the FIFO
** para : buffer -- the pointer of data
** para : target -- the address of the target
** ret  : retvalue is 1 means , threhold value is not illegal;
*/ 
int TransmitData(const int threhold, const unsigned char buffer[], const unsigned short target)
{
	int i;
	
	if(threhold < 0 || threhold >255)
	{
		return 1;
	}
	
	
	I2C_DISABLE;
	
	I2C_TX_TL((threhold-1));

	I2C_TAR(target);			

	I2C_ENABLE;

	while(!IsTFE());	
	
	i=0;
	while(i < threhold)
	{
		I2C_WRITE (buffer[i++]);
		while(!IsTFE());	
	}
			
	
	return 0;
}


int ReadData(const int threhold, const unsigned char buffer[], const unsigned short target)
{
	int retValue = 0 ,i;

	//dw	
	if(threhold < 0 || threhold >255){
		retValue = 1;
		return retValue;
	}
	I2C_DISABLE;
	
	I2C_TX_TL((threhold-1));

	I2C_TAR(target);			

	I2C_ENABLE;

	while(!IsTFE());		// hw_delay(1);	
	//wa
	i=0;
	while(i < threhold)
	{
		I2C_WRITE (buffer[i++]);
		while(!IsTFE());		// hw_delay(1);		
	}		

	//sr
	I2C_CON(I2CR_CON | 0x20);//IC_RESTART_EN
		
	//dr
	while(!IsTFE());		
	I2C_WRITE(0x168);			//read operation
	while(!IsTFE());		
	
	//data
	while(!IsRFNE());		
	while(IsRFF());		
	retValue = I2C_READ;
	
	
	return retValue;
}


unsigned int PinMask( int device , int enable )
{
	unsigned int mask;
	int error = 0, test;
	switch( device )
	{
	case 0:		// disable 656 out ( 68k clock ).
		mask = (1<<14);
		break;
	case 1:		// i2c out
		mask = 0x00000400;
		break;
	case 2:
		mask = (1<<21);	// disable 656 out ( sensor clock enable)
		break;
	case 3:		// 68k enable( sci disable )
		mask = (1<<15);
		
		break;
	default:
		error = 1;
	}
	if( mask )
	{
		volatile int* pinmark = (volatile int*) PIN_MASKBASE;
		test = pinmark[0x10c/4];
//		printf("pin mask 1: 0x%x\n", pinmark[0x10c/4]);
		if( pinmark )
		{
			if( enable )
				pinmark[0x10c/4] |= mask;
			else
				pinmark[0x10c/4] &= ~mask;
		}
		else
		{
			error = 2;
		}
		test = pinmark[0x10c/4];
//		printf("pin mask 2: 0x%x\n", pinmark[0x10c/4]);
		
	}
	return error;
}

⌨️ 快捷键说明

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