Page 1 of 1

I2C Bus Scanner

Posted: Fri Jul 17, 2020 12:29 pm
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 );

            }
        }
       
       

Re: I2C Bus Scanner

Posted: Fri Jul 17, 2020 12:38 pm
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

Re: I2C Bus Scanner

Posted: Fri Jul 17, 2020 1:26 pm
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).

Re: I2C Bus Scanner

Posted: Fri Jul 17, 2020 1:34 pm
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.

Re: I2C Bus Scanner

Posted: Fri Jul 17, 2020 2:03 pm
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).

Re: I2C Bus Scanner

Posted: Fri Jul 17, 2020 3:15 pm
by Giovanni
You are right, it goes in I2C_LOCKED state on timeout, forgot that.

Giovanni

Re: I2C Bus Scanner

Posted: Thu Sep 17, 2020 12:18 pm
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.