I2C Bus Scanner

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

I2C Bus Scanner

Post by Tabulous »

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: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2C Bus Scanner

Post by Giovanni »

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: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: I2C Bus Scanner

Post by steved »

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: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: I2C Bus Scanner

Post by Tabulous »

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: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: I2C Bus Scanner

Post by steved »

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: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2C Bus Scanner

Post by Giovanni »

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
Been thanked: 15 times

Re: I2C Bus Scanner

Post by piers »

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.
Post Reply