| 14 | \******************************************************************************************/ |
| 15 | |
| 16 | std::string i2c_detect(i2c_smbus_interface * bus, int mode) |
| 17 | { |
| 18 | int i, j; |
| 19 | int first = 0x03, last = 0x77; |
| 20 | int res; |
| 21 | int slave_addr; |
| 22 | char line[128]; |
| 23 | std::string text; |
| 24 | |
| 25 | snprintf(line, 128, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); |
| 26 | text.append(line); |
| 27 | |
| 28 | for (i = 0; i < 128; i += 16) |
| 29 | { |
| 30 | snprintf(line, 128, "%02x: ", i); |
| 31 | text.append(line); |
| 32 | |
| 33 | for (j = 0; j < 16; j++) |
| 34 | { |
| 35 | /* Skip unwanted addresses */ |
| 36 | if (i+j < first || i+j > last) |
| 37 | { |
| 38 | snprintf(line, 128, " "); |
| 39 | text.append(line); |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | /* Set slave address */ |
| 44 | slave_addr = i + j; |
| 45 | |
| 46 | /* Probe this address */ |
| 47 | switch (mode) |
| 48 | { |
| 49 | case MODE_QUICK: |
| 50 | res = bus->i2c_smbus_write_quick(slave_addr, I2C_SMBUS_WRITE); |
| 51 | break; |
| 52 | case MODE_READ: |
| 53 | res = bus->i2c_smbus_read_byte(slave_addr); |
| 54 | break; |
| 55 | default: |
| 56 | if ((i + j >= 0x30 && i + j <= 0x37) |
| 57 | || (i + j >= 0x50 && i + j <= 0x5F)) |
| 58 | res = bus->i2c_smbus_read_byte(slave_addr); |
| 59 | else |
| 60 | res = bus->i2c_smbus_write_quick(slave_addr, I2C_SMBUS_WRITE); |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | if (res < 0) |
| 65 | { |
| 66 | snprintf(line, 128, "-- "); |
| 67 | text.append(line); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | snprintf(line, 128, "%02x ", i + j); |
| 72 | text.append(line); |
| 73 | } |
no test coverage detected