#include <cdefBF533.h>
#include <ccblkfn.h>
#include <sys/exception.h>
#include "I2C.h"
//------------------------------------------------------------------------------
// I2C Peripheral Function Prototypes
//------------------------------------------------------------------------------
void Init(void); // Initialize I2C port
void Start(void); // Sends I2C Start Trasfer
void Stop(void); // Sends I2C Stop Trasfer
bool Write(unsigned char data_out); // Writes data over the I2C bus
bool Read(unsigned char *data_in, bool send_ack); // Reads data from the I2C bus
void SetSCLK(bool state); // Set SCLK to <state>
void SetSDATA(bool state); // Set SDATA to <state>
bool GetSDATA(void); // Get SDATA state
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// I2C Peripheral Variables
//------------------------------------------------------------------------------
unsigned char IdentAddr = 0x00;
unsigned int I2C_Delay = 0x00000000;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Procedure: I2C_Init
// Inputs: identaddr
// Outputs: bool
// Description: Initialize I2C and setup Slave Ident Addr. then check the ident
// for response and returns true if ok.
//------------------------------------------------------------------------------
bool I2C_Init(unsigned char identaddr)
{
bool ret;
// Calculate Delay NEED FIX!!!
I2C_Delay = ((*pPLL_CTL & 0x7e00) >> 9); //jiapeng change 9 to 8;
if ((*pPLL_CTL & 0x0001) == 0x0001)
I2C_Delay /= 2;
//jiapeng adds it.
I2C_Delay *= 2;
IdentAddr = identaddr;
Init(); // Initialize I2C port
Start(); // Check Slave Ident Addr
ret = Write(IdentAddr);
Stop();
return ret; // Return true if Ident Addr. Ok
}
//------------------------------------------------------------------------------
// Procedure: I2C_Write
// Inputs: data out, address
// Outputs: bool
// Description: Writes a byte to the given address and return status.
//------------------------------------------------------------------------------
bool I2C_Write(unsigned char data_out, unsigned char address)
{
Start(); // Send start signal
if (!Write(IdentAddr)) // Send identifier I2C address
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Write(address)) // Send address to device
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Write(data_out)) // Send byte to device
{
Stop(); // Send I2C Stop Transfer
return false;
}
Stop(); // Send I2C Stop Transfer
return true;
}
//------------------------------------------------------------------------------
// Procedure: I2C_Read
// Inputs: *data_in, address
// Outputs: bool
// Description: Reads a byte from the given address and return status.
//------------------------------------------------------------------------------
bool I2C_Read(unsigned char *data_in, unsigned char address)
{
Start(); // Send start signal
if (!Write(IdentAddr)) // Send identifer I2C address
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Write(address)) // Send address to device
{
Stop(); // Send I2C Stop Transfer
return false;
}
Start(); // Send I2C Start Transer
if (!Write(IdentAddr+1)) // Send identifer I2C address
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Read(data_in, false)) // Read byte
{
Stop(); // Send I2C Stop Transfer
return false;
}
Stop(); // Send I2C Stop Transfer
return true;
}
//------------------------------------------------------------------------------
// Procedure: I2C_Write_16
// Inputs: data out, address
// Outputs: bool
// Description: Writes a byte to the given address and return status.
//------------------------------------------------------------------------------
bool I2C_Write_16(unsigned char data_out_h,unsigned char data_out_l, unsigned char address)
{
Start(); // Send start signal
Write(IdentAddr); // Send identifier I2C address
Write(address); // Send address to device
Write(data_out_h); // Send byte to device
Write(data_out_l);
Stop(); // Send I2C Stop Transfer
return true;
}
//------------------------------------------------------------------------------
// Procedure: I2C_Read_16
// Inputs: *data_in, address
// Outputs: bool
// Description: Reads a byte from the given address and return status.
//------------------------------------------------------------------------------
bool I2C_Read_16(unsigned char *data_in_h,unsigned char *data_in_l, unsigned char address)
{
Start(); // Send start signal
Write(IdentAddr); // Send identifer I2C address
Write(address); // Send address to device
Stop();
Start(); // Send I2C Start Transer
Write(IdentAddr+1); // Send identifer I2C address
Read(data_in_h, true); // Read byte
Read(data_in_l, false);
Stop(); // Send I2C Stop Transfer
return true;
}
/********************************************************
*
* 函数名 : I2C_Write
* 函数功能 : data out, address
* 函数输入参数 :
* 函数输出 : bool
* 说明 : Writes a byte to the given address
* and return status.
********************************************************/
bool SCCB_Write(unsigned char data_out, unsigned char address)
{
Start(); // Send start signal
if (!Write(IdentAddr)) // Send identifier I2C address
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Write(address)) // Send address to device
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Write(data_out)) // Send byte to device
{
Stop(); // Send I2C Stop Transfer
return false;
}
Stop(); // Send I2C Stop Transfer
return true;
}
/********************************************************
*
* 函数名 : I2C_Read
* 函数功能 : *data_in, address
* 函数输入参数 :
* 函数输出 : bool
* 说明 : Reads a byte from the given address
* and return status.
********************************************************/
bool SCCB_Read(unsigned char *data_in, unsigned char address)
{
Start(); // Send start signal
if (!Write(IdentAddr)) // Send identifer I2C address
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Write(address)) // Send address to device
{
Stop(); // Send I2C Stop Transfer
return false;
}
Stop();
Start(); // Send I2C Start Transer
if (!Write(IdentAddr+1)) // Send identifer I2C address
{
Stop(); // Send I2C Stop Transfer
return false;
}
if (!Read(data_in, false)) // Read byte
{
Stop(); // Send I2C Stop Transfer
return false;
}
Stop(); // Send I2C Stop Transfer
return true;
}
//------------------------------------------------------------------------------
// I2C Functions - Master
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Routine: Init
// Inputs: none
// Outputs: none
// Purpose: Initialize I2C for the ADu812C
//------------------------------------------------------------------------------
inline void Init(void)
{
*pFIO_DIR |= I2C_CLK; // Set SCLK as output and SDATA as input/high
*pFIO_POLAR &= ~I2C_DATA; // Enable Active Hight
*pFIO_EDGE &= ~I2C_DATA; // Enable Level Sensitivity
*pFIO_INEN |= I2C_DATA; // Enable SDATA Input Buffer
SetSDATA(1); // Set SDATA as input/high
SetSCLK(1);