Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2cdetect like a linux #23

Open
carlospintojunior opened this issue Dec 9, 2024 · 0 comments
Open

i2cdetect like a linux #23

carlospintojunior opened this issue Dec 9, 2024 · 0 comments

Comments

@carlospintojunior
Copy link

carlospintojunior commented Dec 9, 2024

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);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant