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.

129 lines
4.7 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 DevAddr, int Speed ){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_nFID = open( DevicePath, O_RDWR );
  7. m_bAddress = DevAddr & 0x7F; // 7 bit address
  8. if( m_nFID >= 0 ) {
  9. ioctl( m_nFID, I2C_TIMEOUT, 3); // i2c timeout, 1 for 10ms ; if too small, i2c may lose response
  10. ioctl( m_nFID, I2C_RETRIES, 3); // i2c retry limit
  11. };
  12. };
  13. i2cPort::~i2cPort(){
  14. if( m_nFID >= 0 ) {
  15. close( m_nFID );
  16. };
  17. };
  18. int i2cPort::_write( int fd, int device_addr, uint16_t offset, const char* data, int data_len ){
  19. struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
  20. struct i2c_msg i2cmsg;
  21. if( m_nFID < 0 ) return -1;
  22. if(data_len>I2C_PAGE_BYTES){
  23. char tBuf[256];
  24. sprintf( tBuf, "*** Warning: Page write mode should not exceed %d bytes, otherwise the current page will be rolled over!",I2C_PAGE_BYTES );
  25. LOG( tBuf );
  26. };
  27. at24c256.nmsgs = 1; // message count in the structure
  28. at24c256.msgs = &i2cmsg;
  29. at24c256.msgs[0].len = 2 + data_len; // the offset address word is 2 bytes long, plus the buf_len
  30. at24c256.msgs[0].addr = device_addr; // i2c slave device address
  31. at24c256.msgs[0].flags = 0; // 0 for write; 1 for read
  32. #ifdef _LINUX_I2C_DEV_H
  33. at24c256.msgs[0].buf = (char *)malloc( at24c256.msgs[0].len );
  34. #else
  35. at24c256.msgs[0].buf = (uint8_t *)malloc( at24c256.msgs[0].len );
  36. #endif
  37. if(!at24c256.msgs[0].buf){
  38. LOG("_write(): Failed to allocate memory for i2c message buffer!");
  39. return -1;
  40. }
  41. #ifdef _LINUX_I2C_DEV_H
  42. at24c256.msgs[0].buf[0] = (char)(offset >> 8); // MSB first
  43. at24c256.msgs[0].buf[1] = (char)(offset); // LSB
  44. #else
  45. at24c256.msgs[0].buf[0] = (uint8_t)(offset >> 8); // MSB first
  46. at24c256.msgs[0].buf[1] = (uint8_t)(offset); // LSB
  47. #endif
  48. strncpy( ((char*)at24c256.msgs[0].buf)+2, data, data_len); // copy user's data to the structure buffer
  49. if(ioctl(fd, I2C_RDWR, (unsigned long)&at24c256)<0 ){ // write ioctl data to the i2c bus
  50. LOG("_write(): Failed to write data to i2c device!");
  51. free(at24c256.msgs[0].buf);
  52. return -1;
  53. }
  54. free(at24c256.msgs[0].buf);
  55. return 0;
  56. };
  57. int i2cPort::_read(int fd, int device_addr, uint16_t offset, char* data, int data_len){
  58. struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
  59. struct i2c_msg i2cmsg[2]; // message count in the structure
  60. #ifdef _LINUX_I2C_DEV_H
  61. char dummy_write_buf[2];
  62. #else
  63. uint8_t dummy_write_buf[2];
  64. #endif
  65. if( m_nFID < 0 ) return -1;
  66. if(data_len>I2C_PAGE_BYTES){
  67. char tBuf[256];
  68. sprintf( tBuf, "*** Warning: Sequential read should not exceed %d bytes, otherwise the read data will be rolled over!",I2C_PAGE_BYTES);
  69. LOG(tBuf);
  70. };
  71. // A dummy write operation should be done according to the AT24C256 i2c protocol
  72. at24c256.nmsgs = 2; // message count in the structure
  73. at24c256.msgs = i2cmsg;
  74. //at24c256.msgs[0].len = 2; // the offset address word is 2 bytes long
  75. at24c256.msgs[0].len = 1; // the offset address word is 1 bytes long
  76. at24c256.msgs[0].addr = device_addr;
  77. at24c256.msgs[0].flags = 0; // 0 for write;
  78. at24c256.msgs[0].buf = dummy_write_buf;
  79. //at24c256.msgs[0].buf[0] = (offset >> 8) & 0xFF;// write address
  80. //at24c256.msgs[0].buf[1] = (offset) & 0xFF; // write address
  81. at24c256.msgs[0].buf[0] = (offset) & 0xFF; // write address
  82. // read operation
  83. at24c256.msgs[1].len = data_len;
  84. at24c256.msgs[1].addr = device_addr;
  85. at24c256.msgs[1].flags = 1; // 1 for read; 0 for write
  86. #ifdef _LINUX_I2C_DEV_H
  87. at24c256.msgs[1].buf = data;
  88. #else
  89. at24c256.msgs[1].buf = (uint8_t *)data;
  90. #endif
  91. if (ioctl(fd, I2C_RDWR, (unsigned long)&at24c256) < 0) {
  92. LOG("_read(): Failed to read data from EEPROM via i2c!\n");
  93. return -1;
  94. };
  95. return 0;
  96. };
  97. bool i2cPort::readMZ( uint16_t offset, uint8_t *buffer, int len ){
  98. if( _read( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false;
  99. return true;
  100. };
  101. bool i2cPort::writeMZ( uint16_t offset, uint8_t *buffer, int len ){
  102. if( _write( m_nFID, m_bAddress, offset, (char*)buffer, len ) < 0 ) return false;
  103. return true;
  104. };
  105. /*
  106. int i2cPort::sendBlock(unsigned char *Data, int DataSize){
  107. return 0;
  108. };
  109. int i2cPort::recptBlock(unsigned char *Buffer, int *BufferLength){
  110. return 0;
  111. };
  112. */
  113. #undef LOG
  114. #undef LOG_ERROR