[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
MyMachineryForum.com • View topic - enable i2c bus B+

enable i2c bus B+

All about the Raspberry Pi SBC and Rpi projects,hints, etc.

enable i2c bus B+

Postby Richard-TX » Fri Nov 14, 2014 9:20 pm

User avatar
Richard-TX
Site Admin
 
Posts: 208
Images: 6
Joined: Sat Feb 04, 2012 8:10 pm

Re: enable i2c bus B+

Postby Richard-TX » Fri Nov 14, 2014 11:30 pm

User avatar
Richard-TX
Site Admin
 
Posts: 208
Images: 6
Joined: Sat Feb 04, 2012 8:10 pm

Re: enable i2c bus B+

Postby Richard-TX » Mon Nov 17, 2014 1:56 pm

new enable script

#!/usr/bin/python
#!/usr/bin/env python
#
# W. Greathouse 13-Feb-2013
#
# Enable I2C on P1 and P5 (Rev 2 boards only)
#

# #######
# For I2C configuration test
import os
import mmap

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() <= 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
s2 = 0b00001001000000000000000000000000
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()
User avatar
Richard-TX
Site Admin
 
Posts: 208
Images: 6
Joined: Sat Feb 04, 2012 8:10 pm

Re: enable i2c bus B+

Postby Richard-TX » Mon Nov 17, 2014 1:57 pm

Here is some code I have not tried.

/*
2014-10-30
set_mode.c
Public Domain

gcc -o set_mode set_mode.c
sudo ./set-mode
*/
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>

#define GPIO_BASE 0x20200000
#define GPIO_LEN 0xB4

#define GPLEV0 13
#define GPLEV1 14

/* mode: 0-7 */

#define PI_INPUT 0
#define PI_OUTPUT 1
#define PI_ALT0 4
#define PI_ALT1 5
#define PI_ALT2 6
#define PI_ALT3 7
#define PI_ALT4 3
#define PI_ALT5 2

static volatile uint32_t * gpioReg = MAP_FAILED;

int gpioSetMode(unsigned gpio, unsigned mode)
{
int reg, shift;

reg = gpio/10;
shift = (gpio%10) * 3;

gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);

return 0;
}

int gpioGetMode(unsigned gpio)
{
int reg, shift;

reg = gpio/10;
shift = (gpio%10) * 3;

return (*(gpioReg + reg) >> shift) & 7;
}

main()
{
int fd;

fd = open("/dev/mem", O_RDWR | O_SYNC) ;

gpioReg = mmap(0,
GPIO_LEN,
PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_SHARED|MAP_LOCKED,
fd,
GPIO_BASE);

close(fd);

if (gpioReg != MAP_FAILED)
{
gpioSetMode(17, PI_ALT3);
}
}
User avatar
Richard-TX
Site Admin
 
Posts: 208
Images: 6
Joined: Sat Feb 04, 2012 8:10 pm

Re: enable i2c bus B+

Postby Richard-TX » Mon Nov 17, 2014 2:08 pm

User avatar
Richard-TX
Site Admin
 
Posts: 208
Images: 6
Joined: Sat Feb 04, 2012 8:10 pm

Re: enable i2c bus B+

Postby Richard-TX » Mon Nov 17, 2014 6:37 pm

User avatar
Richard-TX
Site Admin
 
Posts: 208
Images: 6
Joined: Sat Feb 04, 2012 8:10 pm


Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 1 guest

cron