#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 DevBusAddr, int Speed, uint8_t RegAddrBytes ){ m_pLog = basicLogCollection::getInstance(); m_nFID = open( DevicePath, O_RDWR ); if( RegAddrBytes > 1 ) m_bRegAddrBytes = 2; else m_bRegAddrBytes = RegAddrBytes; m_bAddress = DevBusAddr & 0x7F; // 7 bit bus 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, data_len ); LOG( tBuf ); }; at24c256.nmsgs = 1; // message count in the structure at24c256.msgs = &i2cmsg; at24c256.msgs[0].len = m_bRegAddrBytes + data_len; // the offset address word is m_bRegAddrBytes 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 switch( m_bRegAddrBytes ) { case 1:{ at24c256.msgs[0].buf[0] = (char)(offset & 0xFF); break; }; case 2:{ at24c256.msgs[0].buf[0] = (char)(offset >> 8); // MSB first at24c256.msgs[0].buf[1] = (char)(offset); // LSB }; }; #else switch( m_bRegAddrBytes ) { case 1:{ at24c256.msgs[0].buf[0] = (uint8_t)(offset & 0xFF); break; }; case 2:{ at24c256.msgs[0].buf[0] = (uint8_t)(offset >> 8); // MSB first at24c256.msgs[0].buf[1] = (uint8_t)(offset); // LSB }; }; #endif memcpy( at24c256.msgs[0].buf + m_bRegAddrBytes, 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! [%d]", I2C_PAGE_BYTES, data_len ); LOG(tBuf); }; if( m_bRegAddrBytes > 0 ) { // 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 = m_bRegAddrBytes; // the offset address word is m_bRegAddrBytes bytes long at24c256.msgs[0].addr = device_addr; at24c256.msgs[0].flags = 0; // 0 for write; at24c256.msgs[0].buf = dummy_write_buf; switch( m_bRegAddrBytes ){ case 1:{ at24c256.msgs[0].buf[0] = (offset) & 0xFF; // write address break; }; case 2:{ at24c256.msgs[0].buf[0] = (offset >> 8) & 0xFF; // write address MSB at24c256.msgs[0].buf[1] = (offset) & 0xFF; // write address LSB break; }; }; // 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 } else { at24c256.nmsgs = 1; // message count in the structure at24c256.msgs = i2cmsg; at24c256.msgs[0].len = data_len; at24c256.msgs[0].addr = device_addr; at24c256.msgs[0].flags = 1; // 1 for read; 0 for write #ifdef _LINUX_I2C_DEV_H at24c256.msgs[0].buf = data; #else at24c256.msgs[0].buf = (uint8_t *)data; #endif }; if (ioctl(fd, I2C_RDWR, (unsigned long)&at24c256) < 0) { LOG("_read(): Failed to read data from EEPROM via i2c!"); return -1; }; return 0; }; bool i2cPort::readMZ( uint16_t offset, uint8_t *buffer, int len ){ if( m_nFID >= 0 ) { if( _read( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false; return true; } else LOG_ERROR("Port is Closed!"); return false; }; bool i2cPort::writeMZ( uint16_t offset, uint8_t *buffer, int len ){ if( m_nFID >= 0 ) { if( _write( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false; return true; } else LOG_ERROR("Port is Closed!"); return false; }; uint8_t i2cPort::read8( uint16_t offset ){ uint8_t retVal; if( m_nFID >= 0 ) { if( _read( m_nFID, m_bAddress, offset, (char*)&retVal, 1 ) < 0 ) LOG_ERROR("Read8 Error!"); } else LOG_ERROR("Port is Closed!"); return retVal; }; bool i2cPort::write8( uint16_t offset, uint8_t byte ){ if( m_nFID >= 0 ) { if( _write( m_nFID, m_bAddress, offset, (char*)&byte, 1 ) < 0 ){ LOG_ERROR("Write8 Error!"); } else return true; } else LOG_ERROR("Port is Closed!"); return false; }; /* 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