Skip to content

i2cdetect like a linux #23

Description

@carlospintojunior

Perhaps this routine will be interesting and could be included in a future version.
The expected response is as shown below.
Note that device 0x53 is an accelerometer connected to pins I2C_SCL (B11) and I2C_SDA (B10) - LE910C1*.

Scanning I2C bus on /dev/i2c-4...

   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00:          -- 04 05 06 07 -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- XX -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

Code:

#include <fcntl.h>			// ioctl
#include <sys/ioctl.h>		// open(), O_RDONLY, etc.
#include <unistd.h>       	// close(), read(), etc.
#include <linux/i2c-dev.h>

#include "m2mb_types.h"

#include "azx_log.h"
#include "azx_utils.h"

#include "app_cfg.h"

#define I2C_4_DEV_NAME "/dev/i2c-4"

void i2cdetect(const char *i2c_dev_name) {
    int i2c_fd;
    int addr;
    char buf[1];

    if ((i2c_fd = open(i2c_dev_name, O_RDWR)) < 0) {
        AZX_LOG_INFO("[I2C] Unable to open I2C device\n");
        return;
    }

    AZX_LOG_INFO("\nScanning I2C bus on %s...\r\n\n", i2c_dev_name);
    AZX_LOG_INFO("   ");
    for (int i = 0; i < 16; i++) {
        AZX_LOG_INFO(" %02X", i);
    }
    AZX_LOG_INFO("\n");

    for (int row = 0; row < 8; row++) {
        AZX_LOG_INFO("%02X: ", row << 4);
        for (int col = 0; col < 16; col++) {
            addr = (row << 4) | col;
            if (addr < 0x03 || addr > 0x77) {
                // reserved address
                AZX_LOG_INFO("   ");
                continue;
            }

            // define address
            if (ioctl(i2c_fd, I2C_SLAVE, addr) < 0) {
                AZX_LOG_INFO("XX ");
                continue; // Next
            }

            // is present ?
            if (read(i2c_fd, buf, 1) == 1) {
                AZX_LOG_INFO("%02X ", addr);
            } else {
                AZX_LOG_INFO("-- ");
            }
        }
        AZX_LOG_INFO("\n");
    }

    close(i2c_fd);
}

void M2MB_main( int argc, char **argv ){
	
	(void)argc;
	(void)argv;
	
	AZX_LOG_INIT();
	AZX_LOG_INFO("Starting. This is v%s built on %s %s. LEVEL: %d\r\n",
			VERSION, __DATE__, __TIME__, azx_log_getLevel());

	azx_sleep_ms(5000);

	AZX_LOG_INFO( "\r\n Start i2cdetect Application  [ version: %f ] \r\n", 1.0 );

	while (1) {
		// Detecting i2c devices
	    i2cdetect(I2C_4_DEV_NAME);
	    azx_sleep_ms(1000);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions