|
|
#include "i2cPort.h"
#define LOG( X ) m_pLog->Log( "I2CP", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "I2CP", this, _LOG_ERROR, X )
i2cPort::i2cPort( CSTR DevicePath, uint8_t DevAddr, int Speed ){ m_pLog = basicLogCollection::getInstance(); m_nFID = open( DevicePath, O_RDWR ); m_bAddress = DevAddr & 0x7F; // 7 bit address
if( m_nFID >= 0 ) { ioctl( m_nFID, I2C_TIMEOUT, 3); // i2c timeout, 1 for 10ms ; if too small, i2c may lose response
ioctl( m_nFID, I2C_RETRIES, 3); // i2c retry limit
};};
i2cPort::~i2cPort(){ if( m_nFID >= 0 ) { close( m_nFID ); };};
int i2cPort::_write( int fd, int device_addr, uint16_t offset, const char* data, int data_len ){ struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
struct i2c_msg i2cmsg; if( m_nFID < 0 ) return -1; if(data_len>I2C_PAGE_BYTES){ char tBuf[256]; sprintf( tBuf, "*** Warning: Page write mode should not exceed %d bytes, otherwise the current page will be rolled over!",I2C_PAGE_BYTES ); LOG( tBuf ); }; at24c256.nmsgs = 1; // message count in the structure
at24c256.msgs = &i2cmsg; at24c256.msgs[0].len = 2 + data_len; // the offset address word is 2 bytes long, plus the buf_len
at24c256.msgs[0].addr = device_addr; // i2c slave device address
at24c256.msgs[0].flags = 0; // 0 for write; 1 for read
#ifdef _LINUX_I2C_DEV_H
at24c256.msgs[0].buf = (char *)malloc( at24c256.msgs[0].len );#else
at24c256.msgs[0].buf = (uint8_t *)malloc( at24c256.msgs[0].len );#endif
if(!at24c256.msgs[0].buf){ LOG("_write(): Failed to allocate memory for i2c message buffer!"); return -1; }#ifdef _LINUX_I2C_DEV_H
at24c256.msgs[0].buf[0] = (char)(offset >> 8); // MSB first
at24c256.msgs[0].buf[1] = (char)(offset); // LSB
#else
at24c256.msgs[0].buf[0] = (uint8_t)(offset >> 8); // MSB first
at24c256.msgs[0].buf[1] = (uint8_t)(offset); // LSB
#endif
strncpy( ((char*)at24c256.msgs[0].buf)+2, data, data_len); // copy user's data to the structure buffer
if(ioctl(fd, I2C_RDWR, (unsigned long)&at24c256)<0 ){ // write ioctl data to the i2c bus
LOG("_write(): Failed to write data to i2c device!"); free(at24c256.msgs[0].buf); return -1; } free(at24c256.msgs[0].buf); return 0;};
int i2cPort::_read(int fd, int device_addr, uint16_t offset, char* data, int data_len){ struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
struct i2c_msg i2cmsg[2]; // message count in the structure
#ifdef _LINUX_I2C_DEV_H
char dummy_write_buf[2];#else
uint8_t dummy_write_buf[2];#endif
if( m_nFID < 0 ) return -1; if(data_len>I2C_PAGE_BYTES){ char tBuf[256]; sprintf( tBuf, "*** Warning: Sequential read should not exceed %d bytes, otherwise the read data will be rolled over!",I2C_PAGE_BYTES); LOG(tBuf); }; // A dummy write operation should be done according to the AT24C256 i2c protocol
at24c256.nmsgs = 2; // message count in the structure
at24c256.msgs = i2cmsg; //at24c256.msgs[0].len = 2; // the offset address word is 2 bytes long
at24c256.msgs[0].len = 1; // the offset address word is 1 bytes long
at24c256.msgs[0].addr = device_addr; at24c256.msgs[0].flags = 0; // 0 for write;
at24c256.msgs[0].buf = dummy_write_buf; //at24c256.msgs[0].buf[0] = (offset >> 8) & 0xFF;// write address
//at24c256.msgs[0].buf[1] = (offset) & 0xFF; // write address
at24c256.msgs[0].buf[0] = (offset) & 0xFF; // write address
// read operation
at24c256.msgs[1].len = data_len; at24c256.msgs[1].addr = device_addr; at24c256.msgs[1].flags = 1; // 1 for read; 0 for write
#ifdef _LINUX_I2C_DEV_H
at24c256.msgs[1].buf = data;#else
at24c256.msgs[1].buf = (uint8_t *)data;#endif
if (ioctl(fd, I2C_RDWR, (unsigned long)&at24c256) < 0) { LOG("_read(): Failed to read data from EEPROM via i2c!\n"); return -1; }; return 0;};
bool i2cPort::readMZ( uint16_t offset, uint8_t *buffer, int len ){ if( _read( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false; return true;};bool i2cPort::writeMZ( uint16_t offset, uint8_t *buffer, int len ){ if( _write( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false; return true;};
/*
int i2cPort::sendBlock(unsigned char *Data, int DataSize){ return 0;};int i2cPort::recptBlock(unsigned char *Buffer, int *BufferLength){ return 0;};*/
#undef LOG
#undef LOG_ERROR
|