eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

HELP I2C and DS1307

Thu May 23, 2013 6:47 am

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?

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

Re: HELP I2C and DS1307

Thu May 23, 2013 7:16 am

What about i2cdetect -y 0 ?

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 7:23 am

( i2cdetect -y 0 ) is the same effect.

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

Re: HELP I2C and DS1307

Thu May 23, 2013 7:32 am

How is the I2C device connected to the Pi. Which wires to which pins/gpios?

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 7:38 am

I have rtc ds1307 module. the pins are GPIO 0 (SDA) and GPIO 1 (SCL) on RPi.

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

Re: HELP I2C and DS1307

Thu May 23, 2013 7:49 am

eqooo wrote:I have rtc ds1307 module. the pins are GPIO 0 (SDA) and GPIO 1 (SCL) on RPi.
What about ground?

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 8:02 am

Ground is near the GPIO4 ... is this so important case ?

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

Re: HELP I2C and DS1307

Thu May 23, 2013 8:29 am

You need to connect the grounds of the I2C device and the Pi.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 8:36 am

so I have to do all grounds will be connect i2c devices and Pi?? I understand right?

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

Re: HELP I2C and DS1307

Thu May 23, 2013 8:38 am

All the Pi grounds are connected internally. Just connect one ground on the Pi. e.g. pin 6, to the I2C ground.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 8:44 am

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.

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

Re: HELP I2C and DS1307

Thu May 23, 2013 8:46 am

You should only get a response when the I2C device is connected correctly.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 8:50 am

Thanks a lot joan so now I will check my devices connection and I will come again :)

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 9:18 am

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.

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

Re: HELP I2C and DS1307

Thu May 23, 2013 10:00 am

Good.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 1:07 pm

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...

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

Re: HELP I2C and DS1307

Thu May 23, 2013 1:14 pm

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.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 1:26 pm

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.

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

Re: HELP I2C and DS1307

Thu May 23, 2013 1:45 pm

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.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Thu May 23, 2013 2:02 pm

Thanks for your answer. I will resource and come again :)

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

Re: HELP I2C and DS1307

Thu May 23, 2013 8:12 pm

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 3687 times
i2c-2.png
Readings @ 10 Hz
i2c-2.png (43.19 KiB) Viewed 3687 times
i2c-3.png
Overview
i2c-3.png (42.57 KiB) Viewed 3687 times

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Fri May 24, 2013 6:51 am

Thanks for writing.
So how can I set this pins ? I don't know how to do this?

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

Re: HELP I2C and DS1307

Fri May 24, 2013 7:53 am

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.

eqooo
Posts: 19
Joined: Tue May 21, 2013 6:59 am

Re: HELP I2C and DS1307

Fri May 24, 2013 8:01 am

thanks a lot Joan I'm immediately triying now :) I will back soon :)

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

Re: HELP I2C and DS1307

Fri May 24, 2013 8:26 am

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

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