Page 1 of 2

HELP I2C and DS1307

Posted: Thu May 23, 2013 6:47 am
by eqooo
Hi everyone.
I'm using rev2 for Pi and I have some trouble about I2C ports.I did http://learn.adafruit.com/adding-a-real ... t-rtc-time this documents so my Pi has no effect.

I'm writing i2cdetect -y 1 ,there are no numbers.

Code: Select all

 
 [email protected]:/sys/class/gpio# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
    
I tried lots of time for ds1307 rtc so I didn't see anything ? what can I do to fix it?

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 7:16 am
by joan
What about i2cdetect -y 0 ?

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 7:23 am
by eqooo
( i2cdetect -y 0 ) is the same effect.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 7:32 am
by joan
How is the I2C device connected to the Pi. Which wires to which pins/gpios?

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 7:38 am
by eqooo
I have rtc ds1307 module. the pins are GPIO 0 (SDA) and GPIO 1 (SCL) on RPi.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 7:49 am
by joan
eqooo wrote:I have rtc ds1307 module. the pins are GPIO 0 (SDA) and GPIO 1 (SCL) on RPi.
What about ground?

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:02 am
by eqooo
Ground is near the GPIO4 ... is this so important case ?

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:29 am
by joan
You need to connect the grounds of the I2C device and the Pi.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:36 am
by eqooo
so I have to do all grounds will be connect i2c devices and Pi?? I understand right?

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:38 am
by joan
All the Pi grounds are connected internally. Just connect one ground on the Pi. e.g. pin 6, to the I2C ground.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:44 am
by eqooo
Yes I did but I have no reason. So can I ask something? I want to learn if the rtc is working I will see e.g 0x68 when I use i2cdetect -y 1 or 0 ? or if I don't connect rtc ,can I see some adress when i2cdetect -y 1 or 0 ?

thanks joan.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:46 am
by joan
You should only get a response when the I2C device is connected correctly.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:50 am
by eqooo
Thanks a lot joan so now I will check my devices connection and I will come again :)

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 9:18 am
by eqooo
This problem is solved . thanks a lot for answers. RTC circuit connection was wrong and fix it and see 68 and using sudo hwclock -r and see Sat 01 Jan 2000 00.00.09 like that.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 10:00 am
by joan
Good.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 1:07 pm
by eqooo
Now I have some problem about P5 header pins. so I did P1 headers i2c and used RTCds1307 ,this is alright.But I want to connect P5 header pins so I connected cables P5 pins but the reason is empty.

Code: Select all

sudo i2cdetect -y 1 
sudo i2cdetect -y 0 
I wrote this codes but I couldn't see anything...

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 1:14 pm
by joan
eqooo wrote:Now I have some problem about P5 header pins. so I did P1 headers i2c and used RTCds1307 ,this is alright.But I want to connect P5 header pins so I connected cables P5 pins but the reason is empty.

Code: Select all

sudo i2cdetect -y 1 
sudo i2cdetect -y 0 
I wrote this codes but I couldn't see anything...
Remember the P5 pins are numbered assuming you are looking at the bottom of the board.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 1:26 pm
by eqooo
I checked my connection so all is done. But I can't see any adresses ..

sorry about my terrible questions,but this is all new to me.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 1:45 pm
by joan
I haven't used the P5 pins for I2C.

I just checked gpios 28 and 29 (I2C bus A, SDA/SCL) and they are both set as inputs.

Gpios 0 and 1(I2C bus B, SDA/SCL) are correctly set as ALT0 (SDA/SCL).

I don't know how to enable gpios 28 and 29 as the other I2C bus. It may be as simple as changing the pin modes to ALT0.

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 2:02 pm
by eqooo
Thanks for your answer. I will resource and come again :)

Re: HELP I2C and DS1307

Posted: Thu May 23, 2013 8:12 pm
by joan
I just made a pig's ear of soldering a header on to P5. I reckon the pins weren't tinned.

Anyhow.

I connected up a SRF02 acoustic ranger to ground, 5V, SDA, and SCL on P5 and it works.

You need to set gpios 28 and 29 to mode ALT0 (value 4) and for reasons I don't understand you have to set the old I2C bus gpios (gpios 0 and 1) from mode ALT0 to mode INPUT (value 0).
i2c-1.png
I2C transaction
i2c-1.png (44.25 KiB) Viewed 3954 times
i2c-2.png
Readings @ 10 Hz
i2c-2.png (43.19 KiB) Viewed 3954 times
i2c-3.png
Overview
i2c-3.png (42.57 KiB) Viewed 3954 times

Re: HELP I2C and DS1307

Posted: Fri May 24, 2013 6:51 am
by eqooo
Thanks for writing.
So how can I set this pins ? I don't know how to do this?

Re: HELP I2C and DS1307

Posted: Fri May 24, 2013 7:53 am
by joan
eqooo wrote:Thanks for writing.
So how can I set this pins ? I don't know how to do this?
I'll see if I can put together some C code to set the pin modes.

You can use my pigpio library/daemon to do so.

http://abyz.co.uk/rpi/pigpio/pigpio.tar

Code: Select all

wget abyz.co.uk/rpi/pigpio/pigpio.tar
tar xvf pigpio.tar
cd PIGPIO
cp libpigpio.a-hard libpigpio.a
make
make install
sudo ./pigpiod
pigs m 0 r
pigs m 1 r
pigs m 28 0
pigs m 29 0
The mode set value is strange at first glance. The values are r, w, 0, 1, 2, 3, 4 , 5 which set a mode of input, output, alt0, .., alt5. The value returned by pigs is the actual machine value 0 for input, 1 for output, 4 for alt0.

To capture the SDA/SCL waveform you can use

Code: Select all

pigs no
pig2vcd </dev/pigpio0 >log.vcd &
pigs nb 0 -1
ls -lh log.vcd
ls -lh log.vcd

WHEN THE LOG IS AS BIG AS YOU WANT

pigs nc 0
Use GTKWave to display the waveform.

Use sudo killall pigpiod to remove the daemon when done.

Re: HELP I2C and DS1307

Posted: Fri May 24, 2013 8:01 am
by eqooo
thanks a lot Joan I'm immediately triying now :) I will back soon :)

Re: HELP I2C and DS1307

Posted: Fri May 24, 2013 8:26 am
by joan
I have just checked this C code and it appears to work.

Code: Select all

#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

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;
}

uint32_t gpioRead_Bits_0_31(void) { return (*(gpioReg + GPLEV0)); }
uint32_t gpioRead_Bits_32_53(void) { return (*(gpioReg + GPLEV1)); }

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);

   gpioSetMode(0,  0); /* mode value for input */
   gpioSetMode(1,  0); /* mode value for input */
   gpioSetMode(28, 4); /* mode value for alt0 */
   gpioSetMode(29, 4); /* mode value for alt0 */

}
To compile/link/run

gcc -o modes modes.c
sudo ./modes