You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "i2cPort.h"
  2. #define LOG( X ) m_pLog->Log( "I2CP", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "I2CP", this, _LOG_ERROR, X )
  4. i2cPort::i2cPort( CSTR DevicePath, uint8_t DevBusAddr, int Speed, uint8_t RegAddrBytes ){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_nFID = open( DevicePath, O_RDWR );
  7. if( RegAddrBytes > 1 ) m_bRegAddrBytes = 2; else m_bRegAddrBytes = RegAddrBytes;
  8. m_bAddress = DevBusAddr & 0x7F; // 7 bit bus address
  9. if( m_nFID >= 0 ) {
  10. ioctl( m_nFID, I2C_TIMEOUT, 3); // i2c timeout, 1 for 10ms ; if too small, i2c may lose response
  11. ioctl( m_nFID, I2C_RETRIES, 3); // i2c retry limit
  12. };
  13. };
  14. i2cPort::~i2cPort(){
  15. if( m_nFID >= 0 ) {
  16. close( m_nFID );
  17. };
  18. };
  19. int i2cPort::_write( int fd, int device_addr, uint16_t offset, const char* data, int data_len ){
  20. struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
  21. struct i2c_msg i2cmsg;
  22. if( m_nFID < 0 ) return -1;
  23. if( data_len > I2C_PAGE_BYTES ){
  24. char tBuf[256];
  25. sprintf( tBuf, "*** Warning: Page write mode should not exceed %d bytes, otherwise the current page will be rolled over!",I2C_PAGE_BYTES, data_len );
  26. LOG( tBuf );
  27. };
  28. at24c256.nmsgs = 1; // message count in the structure
  29. at24c256.msgs = &i2cmsg;
  30. at24c256.msgs[0].len = m_bRegAddrBytes + data_len; // the offset address word is m_bRegAddrBytes bytes long, plus the buf_len
  31. at24c256.msgs[0].addr = device_addr; // i2c slave device address
  32. at24c256.msgs[0].flags = 0; // 0 for write; 1 for read
  33. #ifdef _LINUX_I2C_DEV_H
  34. at24c256.msgs[0].buf = (char *)malloc( at24c256.msgs[0].len );
  35. #else
  36. at24c256.msgs[0].buf = (uint8_t *)malloc( at24c256.msgs[0].len );
  37. #endif
  38. if(!at24c256.msgs[0].buf){
  39. LOG("_write(): Failed to allocate memory for i2c message buffer!");
  40. return -1;
  41. }
  42. #ifdef _LINUX_I2C_DEV_H
  43. switch( m_bRegAddrBytes ) {
  44. case 1:{
  45. at24c256.msgs[0].buf[0] = (char)(offset & 0xFF);
  46. break;
  47. };
  48. case 2:{
  49. at24c256.msgs[0].buf[0] = (char)(offset >> 8); // MSB first
  50. at24c256.msgs[0].buf[1] = (char)(offset); // LSB
  51. };
  52. };
  53. #else
  54. switch( m_bRegAddrBytes ) {
  55. case 1:{
  56. at24c256.msgs[0].buf[0] = (uint8_t)(offset & 0xFF);
  57. break;
  58. };
  59. case 2:{
  60. at24c256.msgs[0].buf[0] = (uint8_t)(offset >> 8); // MSB first
  61. at24c256.msgs[0].buf[1] = (uint8_t)(offset); // LSB
  62. };
  63. };
  64. #endif
  65. memcpy( at24c256.msgs[0].buf + m_bRegAddrBytes, data, data_len ); // copy user's data to the structure buffer
  66. if(ioctl(fd, I2C_RDWR, (unsigned long)&at24c256 ) < 0 ){ // write ioctl data to the i2c bus
  67. LOG("_write(): Failed to write data to i2c device!");
  68. free( at24c256.msgs[0].buf );
  69. return -1;
  70. };
  71. free(at24c256.msgs[0].buf);
  72. return 0;
  73. };
  74. int i2cPort::_read(int fd, int device_addr, uint16_t offset, char* data, int data_len){
  75. struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
  76. struct i2c_msg i2cmsg[2]; // message count in the structure
  77. #ifdef _LINUX_I2C_DEV_H
  78. char dummy_write_buf[2];
  79. #else
  80. uint8_t dummy_write_buf[2];
  81. #endif
  82. if( m_nFID < 0 ) return -1;
  83. if(data_len>I2C_PAGE_BYTES){
  84. char tBuf[256];
  85. sprintf( tBuf, "*** Warning: Sequential read should not exceed %d bytes, otherwise the read data will be rolled over! [%d]", I2C_PAGE_BYTES, data_len );
  86. LOG(tBuf);
  87. };
  88. if( m_bRegAddrBytes > 0 ) {
  89. // A dummy write operation should be done according to the AT24C256 i2c protocol
  90. at24c256.nmsgs = 2; // message count in the structure
  91. at24c256.msgs = i2cmsg;
  92. at24c256.msgs[0].len = m_bRegAddrBytes; // the offset address word is m_bRegAddrBytes bytes long
  93. at24c256.msgs[0].addr = device_addr;
  94. at24c256.msgs[0].flags = 0; // 0 for write;
  95. at24c256.msgs[0].buf = dummy_write_buf;
  96. switch( m_bRegAddrBytes ){
  97. case 1:{
  98. at24c256.msgs[0].buf[0] = (offset) & 0xFF; // write address
  99. break;
  100. };
  101. case 2:{
  102. at24c256.msgs[0].buf[0] = (offset >> 8) & 0xFF; // write address MSB
  103. at24c256.msgs[0].buf[1] = (offset) & 0xFF; // write address LSB
  104. break;
  105. };
  106. };
  107. // read operation
  108. at24c256.msgs[1].len = data_len;
  109. at24c256.msgs[1].addr = device_addr;
  110. at24c256.msgs[1].flags = 1; // 1 for read; 0 for write
  111. #ifdef _LINUX_I2C_DEV_H
  112. at24c256.msgs[1].buf = data;
  113. #else
  114. at24c256.msgs[1].buf = (uint8_t *)data;
  115. #endif
  116. } else {
  117. at24c256.nmsgs = 1; // message count in the structure
  118. at24c256.msgs = i2cmsg;
  119. at24c256.msgs[0].len = data_len;
  120. at24c256.msgs[0].addr = device_addr;
  121. at24c256.msgs[0].flags = 1; // 1 for read; 0 for write
  122. #ifdef _LINUX_I2C_DEV_H
  123. at24c256.msgs[0].buf = data;
  124. #else
  125. at24c256.msgs[0].buf = (uint8_t *)data;
  126. #endif
  127. };
  128. if (ioctl(fd, I2C_RDWR, (unsigned long)&at24c256) < 0) {
  129. LOG("_read(): Failed to read data from EEPROM via i2c!");
  130. return -1;
  131. };
  132. return 0;
  133. };
  134. bool i2cPort::readMZ( uint16_t offset, uint8_t *buffer, int len ){
  135. if( m_nFID >= 0 ) {
  136. if( _read( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false;
  137. return true;
  138. } else LOG_ERROR("Port is Closed!");
  139. return false;
  140. };
  141. bool i2cPort::writeMZ( uint16_t offset, uint8_t *buffer, int len ){
  142. if( m_nFID >= 0 ) {
  143. if( _write( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false;
  144. return true;
  145. } else LOG_ERROR("Port is Closed!");
  146. return false;
  147. };
  148. uint8_t i2cPort::read8( uint16_t offset ){
  149. uint8_t retVal;
  150. if( m_nFID >= 0 ) {
  151. if( _read( m_nFID, m_bAddress, offset, (char*)&retVal, 1 ) < 0 ) LOG_ERROR("Read8 Error!");
  152. } else LOG_ERROR("Port is Closed!");
  153. return retVal;
  154. };
  155. bool i2cPort::write8( uint16_t offset, uint8_t byte ){
  156. if( m_nFID >= 0 ) {
  157. if( _write( m_nFID, m_bAddress, offset, (char*)&byte, 1 ) < 0 ){
  158. LOG_ERROR("Write8 Error!");
  159. } else return true;
  160. } else LOG_ERROR("Port is Closed!");
  161. return false;
  162. };
  163. /*
  164. int i2cPort::sendBlock(unsigned char *Data, int DataSize){
  165. return 0;
  166. };
  167. int i2cPort::recptBlock(unsigned char *Buffer, int *BufferLength){
  168. return 0;
  169. };
  170. */
  171. #undef LOG
  172. #undef LOG_ERROR