How to use NuttX I2C Scan features

First you need to enable I2C to your MCU, and these additional options:

CONFIG_I2C=y
CONFIG_I2C_DRIVER=y
CONFIG_SYSTEM_I2CTOOL=y
CONFIG_I2CTOOL_MINBUS=0
CONFIG_I2CTOOL_MAXBUS=5
CONFIG_I2CTOOL_MINADDR=0x03
CONFIG_I2CTOOL_MAXADDR=0x77
CONFIG_I2CTOOL_MAXREGADDR=0xff
CONFIG_I2CTOOL_DEFFREQ=400000

Also in your board initialization you need to register the right I2C Bus you want to use. For example, I want to use the I2C Bus 3:

rtcinfo(“Initialize I2C%d\n”, 3);
i2c = sam_i2c_master_initialize(3);
if (!i2c)
{
rtcerr(“ERROR: Failed to initialize I2C%d\n”, 3);
return -ENODEV;
}

/* Register the I2C to get the “nsh> i2c bus” command working */

ret = i2c_register(i2c, 3);
if (ret < 0)
{
rtcerr(“ERROR: Failed to register I2C%d driver: %d\n”, bus, ret);
return -ENODEV;
}

If everything compiled fine you will have:

nsh> i2c bus
BUS EXISTS?
Bus 0: NO
Bus 1: NO
Bus 2: NO
Bus 3: YES
Bus 4: NO
Bus 5: NO
nsh>

nsh> i2c dev 0x00 0x7f
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: 50 — — — — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — 6f
70: — — — — — — — — — — — — — — — —
nsh>

These two addresses are the EEPROM (0x50) and the RTC (0x6f) that I have on my board.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s