I2C Bus Scanner

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

I2C Bus Scanner

Postby Tabulous » Fri Jul 17, 2020 12:29 pm

Trying to scan I2C buss to find a device, there is only this device on the bus. am i doing something wrong here ?

Code: Select all


        TGL_DEBUG( DBG_MASK_ALS,
                   "$ ALS - SCANNING....\r\n" );

        for( address=1; address<127; address++ )
        {
            i2cStop( &I2CD1 );
            i2cStart( &I2CD1, &alsconfig );

            chThdSleepMilliseconds( 100 );

            als.address = address;
            als.txbuf[0] = 0x00;
            als.txbuf[1] = 0x00;   
                                           
            i2cAcquireBus( &I2CD1 );
            status = i2cMasterTransmitTimeout( &I2CD1,
                                               als.address,
                                               als.txbuf, 2,
                                               als.rxbuf, 0,
                                               tmo );
            i2cReleaseBus( &I2CD1 );

            if( status == MSG_OK )
            {
                TGL_DEBUG( DBG_MASK_ALS,
                           "$ ALS - FOUND DEVICE @ (0x%02X)\r\n",
                           address );

            }
        }
       
       

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: I2C Bus Scanner

Postby Giovanni » Fri Jul 17, 2020 12:38 pm

I think there is no need to stop/start I2C each time, nor to acquire/release unless you have multiple threads accessing it.

Giovanni

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: I2C Bus Scanner

Postby steved » Fri Jul 17, 2020 1:26 pm

I'm sure I did find a need to stop/start I2C, since its in some of my test code which checks for recovery from an access to an address where nothing responded; maybe it was only a problem with the I2Cv1 driver, which I was certainly using on the F4 until the F7 came out.

I think also you need to send something which the addressed device is happy with, otherwise it may not respond. (Part of the problem is that the I2C drivers were never intended to work like this; if things don't go completely right they return an error. What's needed here is to know whether the address was acknowledged properly, even if subsequent data is rejected).

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Re: I2C Bus Scanner

Postby Tabulous » Fri Jul 17, 2020 1:34 pm

Kind of defeats the object having to send something that the device will accept.

The whole point of this is to determine, which type of light ALS sensor is fitted.

Maybe i'm trying to be too cute with this.

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: I2C Bus Scanner

Postby steved » Fri Jul 17, 2020 2:03 pm

Tabulous wrote:Kind of defeats the object having to send something that the device will accept.

For a totally generic solution, I agree - you'd need to use a different I2C driver which reports whether the address was acknowledged.

For your requirement, an application-specific solution should work quite well. A lot of I2C peripherals can only be assigned an address within a small range, so have a list of addresses and test strings (or a list in the driver for each supported device).

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: I2C Bus Scanner

Postby Giovanni » Fri Jul 17, 2020 3:15 pm

You are right, it goes in I2C_LOCKED state on timeout, forgot that.

Giovanni

User avatar
piers
Posts: 42
Joined: Fri Apr 06, 2012 1:02 am
Location: Netherlands
Been thanked: 15 times
Contact:

Re: I2C Bus Scanner

Postby piers » Thu Sep 17, 2020 12:18 pm

Tabulous, did you find a solution for scanning the I2C bus? I'm trying something similar, and I also think an I2C scanner would be a nice ChibiOS I2C example project.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 20 guests