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

SOLVED: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 11:13 am

About 2 months ago I was evaluating Jessie and wIth Joan's help was able to get i2c-0 working. Since the Pi2 has come out and device tree has been added, things have changed again. The little enable script I have doesn't work so I have to change it to cover the pi2. I have tried a few things and I just can't seem to get devices to appear on i2c-0

I2c-0 and i2c-1 are there. It is just i2c-0 that doesn't communicate.
I am using Raspbian - release date 2015-02-16

cat /proc/cpuinfo | grep Revision
a21041

Here is my code that enables the 2nd i2c bus.

Code: Select all

     #!/usr/bin/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 )
    #

    # #######
    # 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()



Last edited by Richard-TX on Thu Mar 05, 2015 12:09 am, edited 2 times in total.
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
DougieLawson
Posts: 35823
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 11:16 am

Have you added the kernel parm bcm2708.vc_i2c_override=1 to /boot/cmdline.txt?
And dtparam=i2c_vc=on to /boot/config.txt?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 11:27 am

Here is ther current state. i2c devices are connected to both buses

Code: Select all

# i2cdetect -l
i2c-0   i2c             3f205000.i2c                            I2C adapter
i2c-1   i2c             3f804000.i2c                            I2C adapter
# i2cdetect -y 0                      
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- 4c -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
# 
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: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 11:32 am

DougieLawson wrote:Have you added the kernel parm bcm2708.vc_i2c_override=1 to /boot/cmdline.txt?
And dtparam=i2c_vc=on to /boot/config.txt?
yes and yes

here are the pertinent lines in config.txt

Code: Select all

dtparam=i2c_arm=on
dtparam=i2c_vc=on
device_tree_param=i2c0=on
device_tree_param=i2c=on
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
DougieLawson
Posts: 35823
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 11:48 am

What's your kernel version?
It may be time to run rpi-update and go forwards to 3.18.8.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 12:08 pm

#uname -a
Linux raspberrypi 3.18.7-v7+ #755 SMP PREEMPT Thu Feb 12 17:20:48 GMT 2015 armv7l GNU/Linux

rpi-update and apt-get upgrade have been broken for quite some time. I will not waste me time with that. The last time I did a upgrade, I wound up with an unusable system. I would rather just download a new image and run with that. It is quicker to reimage the Rpi than to do any of the other upgrade options.

Got a link to a later image?
Last edited by Richard-TX on Sat Feb 28, 2015 12:18 pm, edited 1 time in total.
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
DougieLawson
Posts: 35823
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 12:18 pm

OK, so you want to run the device tree in an odd way but you're no prepared to update to the latest kernel/firmware to support it.

Your choice. I'm outta here.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 12:20 pm

I didn't realize it was mandatory.
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: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 12:22 pm

I did the rpi-update/ It seemed to go well. I will have to revise my assessment of rpi-update.

Still no joy on the i2c-0
# uname -a
Linux raspberrypi 3.18.8-v7+ #761 SMP PREEMPT Fri Feb 27 15:48:09 GMT 2015 armv7l GNU/Linux
Last edited by Richard-TX on Sat Feb 28, 2015 12:32 pm, edited 1 time in total.
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: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 12:23 pm

# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
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
DougieLawson
Posts: 35823
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 1:28 pm

Richard-TX wrote:I did the rpi-update/ It seemed to go well. I will have to revise my assessment of rpi-update.

Still no joy on the i2c-0
# uname -a
Linux raspberrypi 3.18.8-v7+ #761 SMP PREEMPT Fri Feb 27 15:48:09 GMT 2015 armv7l GNU/Linux
Now you can open an issue at http://github.com/raspberrypi/firmware and get the RPF folks to take a look for you.

rpi-update isn't mandatory, in fact it's a very bad idea (for all values of bad), but when you're running outside the normal mode of operation it makes sense to try it.

The RPF did really well this week, they fixed a firmware bug and added a hardware support enhancement for me.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 3:30 pm

I've just tried an RTC on bus 0 of the Pi 2. i2cdetect (you have to use sudo now) does not detect the device. However piscope suggests the 9th bit (gold line) is being held low by the device (compare the blue line of the next device address).
pi2-i2c-0.gif
pi2-i2c-0.gif (39.83 KiB) Viewed 10411 times

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

Re: pi2, i2c0 and Wheezy

Sat Feb 28, 2015 7:24 pm

Root only permissions are a non-issue for me.

Thanks for looking at this Joan.

I have opened an issue. It is #382
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: pi2, i2c0 and Wheezy

Sun Mar 01, 2015 3:44 pm

Here is the output from a gpio readall. Both i2c0 and i2c1 look the same.

BTW my script does nothing as it detects that no changes need to be made.

Both i2c1 and i2c0 are set to ALT0

Code: Select all

# gpio readall
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |  OUT | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 | ALT0 | 1 | 27 || 28 | 1 | ALT0 | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
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

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2318
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: pi2, i2c0 and Wheezy

Tue Mar 03, 2015 10:40 am

Rev 1.1 Pi2's use I2C slightly differently than the earlier 1.0 variants. This resulted in the BSC0 (I2C0) function being mapped onto two sets of pins simultaneously, which stops it working, but it wasn't obvious because HAT EEPROM is read successfully.

I have a fix for the next firmware release that removes the bogus mapping, but in the meantime there is a workaround:

Code: Select all

echo 28 | sudo tee /sys/class/gpio/export
echo 29 | sudo tee /sys/class/gpio/export
echo in | sudo tee /sys/class/gpio/28/direction
echo in | sudo tee /sys/class/gpio/29/direction
This removes the ALT0 (BSC0) function on GPIOs 28 and 29, allowing it to work on GPIOs 0 and 1.

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

Re: pi2, i2c0 and Wheezy

Tue Mar 03, 2015 2:43 pm

Thanks very much Phil.

I did notice the semi-mirroring of i2c0. I say semi because there is a difference in the two. I am not sure why. Maybe because I didn't have any devices connected to 27/28.
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: pi2, i2c0 and Wheezy

Wed Mar 04, 2015 1:46 am

Here is the corrected code.

Code: Select all

echo 28 | sudo tee /sys/class/gpio/export
echo 29 | sudo tee /sys/class/gpio/export
echo in | sudo tee /sys/class/gpio/gpio28/direction
echo in | sudo tee /sys/class/gpio/gpio29/direction
if running from a root shell or rc.local

Code: Select all

echo 28 > /sys/class/gpio/export
echo 29 > /sys/class/gpio/export
echo in >  /sys/class/gpio/gpio28/direction
echo in > /sys/class/gpio/gpio29/direction
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: pi2, i2c0 and Wheezy

Thu Mar 05, 2015 12:09 am

Updating the firmware fixes the issue. Now I2C-0 is alive.

I am marking this issue solved.
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

ronaldteune
Posts: 8
Joined: Wed Oct 16, 2013 5:39 am

Re: SOLVED: pi2, i2c0 and Wheezy

Wed Apr 15, 2015 6:43 am

Looking for a way to use i2c0 on my P5 header, I found this thread. Now it seems that the thread author wants to use i2c0 on other pins and that works. Is there a similar way to use i2c0 on the P5 pins (GPIO28 & GPIO29)? I have a pi B rev 2. After reading this and some more, and some experimenting, I got where I am no. This is the current readout:

Code: Select all

 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 0 |  7 || 8  | 1 | OUT  | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | OUT  | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 1 | 11 || 12 | 1 | OUT  | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 1 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 1 | 15 || 16 | 1 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 1 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 1 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 |   IN | 0 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
Kernel 3.18.11+ #777 PREEMPT Sat Apr 11 17:24:23 BST 2015 armv6l GNU/Linux

i2c-0 i2c 20205000.i2c I2C adapter
i2c-1 i2c 20804000.i2c I2C adapter

$ i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

After reading this, it seems that I need to set the GPIO's to ALT0. But I cannot find how I can do that. Can someone help me with this last bit?

edit: doing rpi-update didn't help btw.

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

Re: SOLVED: pi2, i2c0 and Wheezy

Wed Apr 15, 2015 7:36 am

If I remember correctly you have to switch gpios 28/29 to ALT0 so they become the I2C bus 0 and gpios 0/1 to inputs from ALT0 so they are no longer I2C bus 0.

I did write a few lines of C code to do this. It'll be on the forum somewhere, probably several years ago.

Perhaps easier to use my pigpio library and the pigs utility.

Code: Select all

sudo pigpiod
pigs m 0 r m 1 r m 28 0 m 29 0 # gpios 0/1 mode read, gpios 28/29 mode ALT0

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2318
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SOLVED: pi2, i2c0 and Wheezy

Wed Apr 15, 2015 8:25 am

Since @ronaldteune already has wiringPi installed, surely it would be simpler to use it?
Try this:

Code: Select all

gpio -g mode 0 in
gpio -g mode 1 in
gpio -g mode 28 alt0
gpio -g mode 29 alt0
But there is another way, and that is to create a Device Tree overlay to do the same job. There are advantages to doing it this way - the interface will come up configured that way from the start, and the system will correctly track that those pins are being used and object if you try to reuse them for something else.

This (untested) overlay should do the job:

Code: Select all

// Change the pins used by i2c0
/dts-v1/;
/plugin/;

/ {
	compatible = "brcm,bcm2708";

	__overrides__ {
		sda0 = <&i2c0_pins>,"brcm,pins:0";
		scl0 = <&i2c0_pins>,"brcm,pins:4";
	};
};
I've left new pin values as parameters. Any unspecified parameter will leave the default pin unchanged.

You can download the compiled overlay (remap-i2c0-overlay.dtb) from here.

In your case, after copying remap-i2c-overlay.dtb to /boot/overlays you would add this to your config.txt:

Code: Select all

dtparam=i2c0=on
dtoverlay=remap-i2c0,sda0=28,scl0=29
Note that this overlay doesn't change the alt function, which will remain at alt0. This would stop you using alt1 on GPIO44 and GPIO45, but I think those pins are unconnected anyway so it is no loss.

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

Re: SOLVED: pi2, i2c0 and Wheezy

Wed Apr 15, 2015 9:08 am

PhilE wrote:Since @ronaldteune already has wiringPi installed, surely it would be simpler to use it?
Try this:

Code: Select all

gpio -g mode 0 in
gpio -g mode 1 in
gpio -g mode 28 alt0
gpio -g mode 29 alt0
...
Yes it would. However the last time I went through this loop I'm fairly sure wiringPi had no provision for setting alternate modes other than INPUT/OUTPUT and possibly PWM.

edited to add:

According to man gpio it is still not implemented. Since it works I'll put that down as a documentation bug.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2318
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SOLVED: pi2, i2c0 and Wheezy

Wed Apr 15, 2015 9:33 am

Code: Select all

man gpio
...
       mode <pin> <mode>
              Set  a pin into input, output or pwm mode. Can also use the lit‐
              erals up, down or tri to set the internal pull-up, pull-down  or
              tristate (off) controls.

              The ALT modes can also be set using alt0, alt1,  ... alt5.

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

Re: SOLVED: pi2, i2c0 and Wheezy

Wed Apr 15, 2015 9:38 am

Yes, I didn't scroll down far enough. But I also looked at http://wiringpi.com/the-gpio-utility/

ronaldteune
Posts: 8
Joined: Wed Oct 16, 2013 5:39 am

Re: SOLVED: pi2, i2c0 and Wheezy

Thu Apr 23, 2015 2:59 pm

Aha, I indeed didn't find it in the wiringpi docs but good to know it's possible. :)
The device tree overlay didn't seem to help enough, I also needed to set the gpio mode.
After that, i2cdetect returned something else than just '--'.
However, it seems to return crap now, on 30kHz as well as 3kHz speeds.
So my guess is that my i2C-device is broken. But I have only very hard ways to test it. Ah well, just wait for courage to start over and open that whole thing up again...

Thanks a lot for all the help though @joan and @PhilE!

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