sssemil
Posts: 10
Joined: Wed Feb 19, 2014 10:44 am

i2c to other i2c's xda xcl

Sat Jan 10, 2015 2:14 pm

Hi. I'm trying to connect bmp180 and mpu6050 to rpi. Both of them work separately. First I tried to connect one of them to 2nd i2c bus on P5, but failed. Now i`m trying to connect it through xda and xcl pins on mpu6050(read somewhere that these pins are for sub-i2c device), how can I do it(read bmp180 bytes through mpu6050)? Thanks.

User avatar
joan
Posts: 14995
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 2:24 pm

I2C is a bus. You can connect both to the same bus as long as they have different I2C addresses.

Do they have the same address? If not just connect both to SDA and SCL on the Pi.

sssemil
Posts: 10
Joined: Wed Feb 19, 2014 10:44 am

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 2:29 pm

Thanks, it worked :D

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 3:12 pm

I am concerned that you never got the 2nd bus operational.

there are a few things to remember.

1 - Despite the fact that the P5 header is very close to the existing header, DuPont connectors fit just fine.
2 - You have to enable the 2nd i2c bus by running a script at boot.
3 - The P5 pinout pictures on the wiki are confusing.
4 - You will likely need some sort of pullups for the 2nd bus. 10k ohm is minimalist. The lower the value, the higher the speed attainable.

The following script should work for both the B and B+

Code: Select all

        #!/usr/bin/python
        #!/usr/bin/env python
        #
        # W. Greathouse 13-Feb-2013
        # Richard A. - Nov, 17 2014 - Now works for B, B+ and CM
        #   Enable I2C on P1 and P5 (Rev 2+ boards only)
        #

        # #######
        # For I2C configuration test
        import os
        import mmap
        bplus=0
        BCM2708_PERI_BASE=0x20000000
        GPIO_BASE=(BCM2708_PERI_BASE + 0x00200000)
        BLOCK_SIZE=4096

        def _strto32bit_(str):
            return ((ord(str[3])<<24) + (ord(str[2])<<16) + (ord(str[1])<<8) + ord(str[0]))

        def _32bittostr_(val):
            return chr(val&0xff) + chr((val>>8)&0xff) + chr((val>>16)&0xff) + chr((val>>24)&0xff)

        def get_revision():
            with open('/proc/cpuinfo') as lines:
                for line in lines:
                    if line.startswith('Revision'):
                        return int(line.strip()[-4:],16)
            raise RuntimeError('No revision found.')

        def i2cConfig():
            if get_revision() >= 10:
               print "B+ or CM detected."
               s0 = 0b00000000000000000000100100100100
               s2 = 0b00000000000000000000000000000000
            if get_revision() <=9:
               s0 = 0b00000000000000000000100100000000
               s2 = 0b00100100000000000000000000000000
            if get_revision() <= 3:
                print "Rev 2 or greater Raspberry Pi required."
                return
            # Use /dev/mem to gain access to peripheral registers
            mf=os.open("/dev/mem", os.O_RDWR|os.O_SYNC)
            m = mmap.mmap(mf,BLOCK_SIZE, mmap.MAP_SHARED,
                    mmap.PROT_READ|mmap.PROT_WRITE,offset=GPIO_BASE)
            # can close the file after we have mmap
            os.close(mf)
            # Read function select registers
            # GPFSEL0 -- GPIO 0,1 I2C0   GPIO 2,3 I2C1
            m.seek(0)
            reg0=_strto32bit_(m.read(4))
            # GPFSEL2 -- GPIO 28,29 I2C0
            m.seek(8)
            reg2=_strto32bit_(m.read(4))
            # print bin(reg0)[2:].zfill(32)[2:]
            # print bin(reg2)[2:].zfill(32)[2:]

            # GPFSEL0 bits --> x[26] SCL0[3] SDA0[3]
            #                        GPIO    GPIO
            m0 = 0b00000000000000000000111111111111
            #s0 = 0b00000000000000000000100100100100
            b0 = reg0 & m0
            if b0 <> s0:
                #print "reg0 I2C configuration not correct. Updating."
                reg0 = (reg0 & ~m0) | s0
                m.seek(0)
                m.write(_32bittostr_(reg0))

            # GPFSEL2 bits --> x[2] SCL0[3] SDA0[3] x[24]
            m2 = 0b00111111000000000000000000000000
            b2 = reg2 & m2
            if b2 <> s2:
                #print "reg2 I2C configuration not correct. Updating."
                reg2 = (reg2 & ~m2) | s2
                m.seek(8)
                m.write(_32bittostr_(reg2))

            # No longer need the mmap
            m.close()


        if __name__ == '__main__':
            i2cConfig()
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

sssemil
Posts: 10
Joined: Wed Feb 19, 2014 10:44 am

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 3:20 pm

After I run this script will /dev/i2c-0 appear or what?

UPD: Nothing changed

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 3:56 pm

If you do not have /dev/i2c-0 appear then you are missing some more fundamental things

If you have a B+ then you need the following.

your /boot/cmdline.txt must look like this:

Code: Select all

dwc_otg.lpm_enable=0 bcm2708.vc_i2c_override=1 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
In addition you need to execute this

Code: Select all

echo "blacklist snd_soc_tas5713" >> //etc/modprobe.d/raspi-blacklist.conf
adding the following may be needed as well to get rid of the ridiculous reserved devices.

Code: Select all

echo "blacklist snd_soc_wm8804" >> //etc/modprobe.d/raspi-blacklist.conf
then reboot

assuming you added the script to rc.local, you should be good to go.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 4:06 pm

If you are using a B then I am not sure what to tell you.

/dev/i2c-0 should be there assuming you took the entries out of the blacklist and added the correct lines to the modules file.

adding the packages i2c-tools and python-smbus helps.

What is the OS image name that you downloaded?
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

sssemil
Posts: 10
Joined: Wed Feb 19, 2014 10:44 am

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 4:46 pm

I`m using B and my OS is minimal raspbian made with debootstrap(how I did it - http://raspberry.tips/raspberrypi-tutor ... erstellen/)

My /boot/cmdline.txt:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

sssemil
Posts: 10
Joined: Wed Feb 19, 2014 10:44 am

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 5:03 pm

It helped! Now I have both /dev/i2c-0 and /dev/i2c-1.
My cmdline.txt:
dwc_otg.lpm_enable=0 bcm2708.vc_i2c_override=1 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
My /etc/modprobe.d/raspi-blacklist.conf:
blacklist snd_soc_tas5713
blacklist snd_soc_wm8804

Thanks

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: i2c to other i2c's xda xcl

Sat Jan 10, 2015 5:10 pm

Now if you do a i2cdetect, the reserved devices (UU) should be gone. They never should have been there in the first place but that is another topic.

I really should try the latest raspbian on a model B. I am having such good reliability with the year+ old image that I hate to change.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Return to “Interfacing (DSI, CSI, I2C, etc.)”