📄 i2c.c
字号:
/********************************************/
/* I2C.c: implementation of the I2C API. */
/* Author : qzsu */
/* Time : 2006-08-21 */
/********************************************/
#include <string.h>
#include "I2C.h"
#ifdef I2C_WINCE
#include <windows.h>
#include <ceddk.h>
#include <Pkfuncs.h>
#include "hw_lib.h"
#endif
#define PHY_I2C_BASE 0x20036000
#define PIN_MASKBASE 0x20020000
U32 *g_i2c_base;
// 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
void I2C_PinMask( U32 enable )
{
volatile int* pinmark = NULL;
//RETAILMSG(1,(_T("In I2C_PinMask\r\n")));
#ifdef I2C_WINCE
pinmark = (volatile int*)hw_map_io( PIN_MASKBASE , 0x1000 , 0 );
//RETAILMSG(1,(_T("pinmark:%x\r\n"),pinmark));
hw_delay(1);
#else
pinmark = (volatile int*) PIN_MASKBASE;
#endif
if( pinmark )
{
if( enable )
pinmark[0x10c/4] |= 0x400;
else
pinmark[0x10c/4] &= ~0x400;
#ifndef I2C_WINCE
hw_unmap_io( (void*)pinmark , 0x1000);
#endif
}
}
/* brief : Initialize the I2C module */
void InitI2C(void)
{
///RETAILMSG(1,(_T("In InitI2C\r\n")));
#ifdef I2C_WINCE
g_i2c_base = (U32 *)(hw_map_io(PHY_I2C_BASE, 4*1024, 0));
//RETAILMSG(1,(_T("g_i2c_base:%x\r\n"),g_i2c_base));
hw_delay(1);
#else
g_i2c_base = (U32*)PHY_I2C_BASE;
#endif
// 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
I2C_SS_HCNT(0x0028);
I2C_SS_LCNT(0x002f);
//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;
}
void DeleteI2C(void)
{
#ifdef I2C_WINCE
if(g_i2c_base)
{
hw_unmap_io(g_i2c_base , 4*1024);
}
#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(0x141); //read operation
while(!IsTFE());
//data
while(!IsRFNE());
while(IsRFF());
retValue = I2C_READ;
return retValue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -