System information page with i2c detect
This commit is contained in:
parent
7dddb9d111
commit
b06f384350
8 changed files with 245 additions and 87 deletions
72
i2c_tools/i2c_tools.cpp
Normal file
72
i2c_tools/i2c_tools.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include "i2c_tools.h"
|
||||
|
||||
/******************************************************************************************\
|
||||
* *
|
||||
* i2c_detect *
|
||||
* *
|
||||
* Prints a list of all detected I2C addresses on the given bus *
|
||||
* *
|
||||
* bus - pointer to i2c_smbus_interface to scan *
|
||||
* mode - one of AUTO, QUICK, READ, FUNC - method of access *
|
||||
* *
|
||||
* Code adapted from i2cdetect.c from i2c-tools Linux package *
|
||||
* *
|
||||
\******************************************************************************************/
|
||||
|
||||
std::string i2c_detect(i2c_smbus_interface * bus, int mode)
|
||||
{
|
||||
int i, j;
|
||||
int res;
|
||||
int slave_addr;
|
||||
char line[128];
|
||||
std::string text;
|
||||
|
||||
sprintf(line, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
|
||||
text.append(line);
|
||||
|
||||
for (i = 0; i < 128; i += 16)
|
||||
{
|
||||
sprintf(line, "%02x: ", i);
|
||||
text.append(line);
|
||||
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
/* Set slave address */
|
||||
slave_addr = i + j;
|
||||
|
||||
/* Probe this address */
|
||||
switch (mode)
|
||||
{
|
||||
case MODE_QUICK:
|
||||
res = bus->i2c_smbus_write_quick(slave_addr, I2C_SMBUS_WRITE);
|
||||
break;
|
||||
case MODE_READ:
|
||||
res = bus->i2c_smbus_read_byte(slave_addr);
|
||||
break;
|
||||
default:
|
||||
if ((i + j >= 0x30 && i + j <= 0x37)
|
||||
|| (i + j >= 0x50 && i + j <= 0x5F))
|
||||
res = bus->i2c_smbus_read_byte(slave_addr);
|
||||
else
|
||||
res = bus->i2c_smbus_write_quick(slave_addr, I2C_SMBUS_WRITE);
|
||||
break;
|
||||
}
|
||||
|
||||
if (res < 0)
|
||||
{
|
||||
sprintf(line, "-- ");
|
||||
text.append(line);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(line, "%02x ", i + j);
|
||||
text.append(line);
|
||||
}
|
||||
}
|
||||
sprintf(line, "\r\n");
|
||||
text.append(line);
|
||||
}
|
||||
|
||||
return text;
|
||||
|
||||
} /* i2c_detect() */
|
||||
Loading…
Add table
Add a link
Reference in a new issue