#include <stdio.h>
#include "interface.h"
#define DEBUGER_MODE 0
#define PRINTFMODE 0
void _i2c_start(void)
{
#if PRINTFMODE
printf("\r\n _i2c_start");
#endif
SDA_HIGH;
I2C_DELAY;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
I2C_DELAY;
SDA_LOW;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
void _i2c_stop(void)
{
#if PRINTFMODE
printf("\r\n _i2c_stop");
#endif
SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SDA_HIGH;
I2C_DELAY_LONG;
}
unsigned char _i2c_write_byte(unsigned char data)
{
unsigned char i;
#if PRINTFMODE
printf("\r\n _i2c_write_byte");
#endif
for(i = 0; i< 8; i++)
{
if( (data << i) & 0x80) SDA_HIGH;
else SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
if(_i2c_ack_detect()) {
#if PRINTFMODE
printf("\n\r Acknoledge Error");
#endif
return ERROR_CODE_FALSE;
}
return ERROR_CODE_TRUE;
}
unsigned char _i2c_read_byte(void)
{
unsigned char i, data;
#if PRINTFMODE
printf("\r\n _i2c_read_byte");
#endif
data = 0;
//20081016
//SDA_HIGH;
//end
SDA_IN;
for(i = 0; i< 8; i++){
data <<= 1;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
if (SDA_DETECT) data |= 0x01;
SCL_LOW;
I2C_DELAY;
}
_i2c_ack_send();
return data;
}
unsigned char _i2c_ack_detect(void)
{
#if PRINTFMODE
printf("\r\n _i2c_ack_detect");
#endif
//SDA_HIGH;
SDA_IN; // SDA Input Mode
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
if (SDA_DETECT)
{
SDA_OUT;
#if PRINTFMODE
printf("\r\n Ack Error!");
#endif
return ERROR_CODE_FALSE; // false
}
I2C_DELAY;
SCL_LOW;
SDA_OUT;
return ERROR_CODE_TRUE; // true
}
void _i2c_ack_send(void)
{
#if PRINTFMODE
printf("\r\n _i2c_ack_send");
#endif
SDA_OUT;
SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
unsigned char _i2c_write(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo)
{
unsigned char i;
#if PRINTFMODE
printf("\r\n_i2c_write");
#endif
_i2c_start();
I2C_DELAY;
if(_i2c_write_byte(device_addr)) {
_i2c_stop();
#if DEBUGER_MODE
printf("\n\r Tx device_addr Error - 0x%2x ",device_addr);
#endif
return ERROR_CODE_WRITE_ADDR;
}
if(_i2c_write_byte(sub_addr)) {
_i2c_stop();
#if DEBUGER_MODE
printf("\n\r Tx sub_addr Error - 0x%2x 0x%2x ",device_addr,sub_addr);
#endif
return ERROR_CODE_WRITE_ADDR;
}
for(i = 0; i<ByteNo; i++) {
if(_i2c_write_byte(buff[i])) {
_i2c_stop();
#if DEBUGER_MODE
printf("\n\r Tx Data Err : ");
printf("0x%2x 0x%2x ",device_addr,sub_addr);
for (i=0; i<ByteNo; i++) printf("0x%2x ", buff[i]);
#endif
return ERROR_CODE_WRITE_DATA;
}
}
I2C_DELAY;
_i2c_stop();
I2C_DELAY_LONG;
#if DEBUGER_MODE
printf("\n\r Tx Data : ");
printf("0x%2x 0x%2x ",device_addr,sub_addr);
for (i=0; i<ByteNo; i++) printf("0x%2x ", buff[i]);
#endif
return ERROR_CODE_TRUE;
}
unsigned char _i2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo)
{
unsigned char i;
#if PRINTFMODE
printf("\r\n_i2c_read");
#endif
_i2c_start();
I2C_DELAY;
if(_i2c_write_byte(device_addr)) {
_i2c_stop();
#if DEBUGER_MODE
printf("\n\r Rx device_addr Error - 0x%2x ",device_addr);
#endif
return ERROR_CODE_READ_ADDR;
}
if(_i2c_write_byte(sub_addr)) {
_i2c_stop();
#if DEBUGER_MODE
printf("\n\r Rx sub_addr Error - 0x%2x 0x%2x ",device_addr,sub_addr);
#endif
return ERROR_CODE_READ_ADDR;
}
_i2c_start();
I2C_DELAY;
if(_i2c_write_byte(device_addr+1)) {
_i2c_stop();
#if DEBUGER_MODE
printf("\n\r Rx Data Err : ");
printf("0x%2x 0x%2x ",device_addr,sub_addr);
for (i=0; i<ByteNo; i++) printf("0x%2x ", buff[i]);
#endif
return ERROR_CODE_READ_ADDR;
}
for(i = 0; i<ByteNo; i++) buff[i] = _i2c_read_byte();
I2C_DELAY;
I2C_DELAY_LONG;
_i2c_stop();
I2C_DELAY_LONG;
#if DEBUGER_MODE
printf("\n\r Rx Data : ");
printf("0x%2x 0x%2x ",device_addr,sub_addr);
for (i=0; i<ByteNo; i++) printf("0x%2x ", buff[i]);
#endif
return ERROR_CODE_TRUE;
}
/* EOF */