Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

DS1307 address query

Wed Apr 06, 2016 1:36 pm

Morning

Using Raspberry Pi 3 with Python IDLE 3.

I have hooked up a DS1307 to the 40 way connector and connected the 2 pull-ups to 3V3. When I run i2cdetect, I can see it at address 68. I can write to the device with hwclock -w and I read the time back using hwclock -r. It seems to work. If I remove the DS1307 from the board, nothing works. That is my test that all is ok.

1) The data sheet shows the DS1307 to have 0xd0 as it's i2c address. The RPi shows it to be 0x68. How does that work? That is confusing me.

2) I want to enable the DS1307 so that pin7 flashes an LED. I know I have to write to register 7 and send 0x40. This enable the SW output for 1Hz. I know this because I have done it on PIC devices many time.

I have tried i2cset -y 1 0x68 0x07 0x40 and a few other variations but it doesn't work. I want to flash the led so that I know for sure the i2c system is working properly. I am going small steps at a time and ultimately want to use many remote stations that I can address on the i2c bus.

Any advise. Thanks.

Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

Re: DS1307 address query

Wed Apr 06, 2016 2:08 pm

Found one problem!

In my PIC code, I send a 0xD0 because I am adding the read bit at the end. 7 bits for the address code and 1 bit for R/w.

7 bit address code gives 68 which is correct!

Wow! That was crazy. Took a while to see that!

Now to work out why I can't turn the LED on. Bet it is easy.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5798
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: DS1307 address query

Wed Apr 06, 2016 2:16 pm

Adco wrote:I have hooked up a DS1307 to the 40 way connector and connected the 2 pull-ups to 3V3.
I2C-1 on header pins 3&5 already has pull ups installed.
Adco wrote:When I run i2cdetect, I can see it at address 68. I can write to the device with hwclock -w and I read the time back using hwclock -r. It seems to work. If I remove the DS1307 from the board, nothing works. That is my test that all is ok.
hwclock working implies to me that you've added a dtoverlay line to config.txt. You haven't mentioned that if you have.
Define "nothing". The Pi doesn't boot?
Adco wrote:1) The data sheet shows the DS1307 to have 0xd0 as it's i2c address. The RPi shows it to be 0x68. How does that work? That is confusing me.
I2C uses 7 bits of address and 1 bit of direction. Some device spec sheets quote both together, so technically DS1307 it should be addressed as 0xD0 and 0xD1. Linux (and others) specify just the 7bits of address, which is 0x68.
Adco wrote:2) I want to enable the DS1307 so that pin7 flashes an LED. I know I have to write to register 7 and send 0x40. This enable the SW output for 1Hz. I know this because I have done it on PIC devices many time.

I have tried i2cset -y 1 0x68 0x07 0x40 and a few other variations but it doesn't work. I want to flash the led so that I know for sure the i2c system is working properly. I am going small steps at a time and ultimately want to use many remote stations that I can address on the i2c bus.
Why 0x40? The datasheet reads

Code: Select all

BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
OUT   0     0     SQWE  0     0     RS1   RS0   
So BIT 4 needs to be set, and that would be 0x10.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

Re: DS1307 address query

Wed Apr 06, 2016 4:46 pm

6by9 wrote:
Adco wrote:
I have hooked up a DS1307 to the 40 way connector and connected the 2 pull-ups to 3V3.
I2C-1 on header pins 3&5 already has pull ups installed.
Yup, resistors removed.
Adco wrote:
When I run i2cdetect, I can see it at address 68. I can write to the device with hwclock -w and I read the time back using hwclock -r. It seems to work. If I remove the DS1307 from the board, nothing works. That is my test that all is ok.
hwclock working implies to me that you've added a dtoverlay line to config.txt. You haven't mentioned that if you have.
Define "nothing". The Pi doesn't boot?
"Nothing" means I can't access the RTC when it is removed (obviously) so it tells me that I was addressing it and now that's it's out, I'm not. A test to say I was seeing it.
Adco wrote:
1) The data sheet shows the DS1307 to have 0xd0 as it's i2c address. The RPi shows it to be 0x68. How does that work? That is confusing me.
I2C uses 7 bits of address and 1 bit of direction. Some device spec sheets quote both together, so technically DS1307 it should be addressed as 0xD0 and 0xD1. Linux (and others) specify just the 7bits of address, which is 0x68.
Yeh, I worked that out. See my next post.
Adco wrote:
2) I want to enable the DS1307 so that pin7 flashes an LED. I know I have to write to register 7 and send 0x40. This enable the SW output for 1Hz. I know this because I have done it on PIC devices many time.

I have tried i2cset -y 1 0x68 0x07 0x40 and a few other variations but it doesn't work. I want to flash the led so that I know for sure the i2c system is working properly. I am going small steps at a time and ultimately want to use many remote stations that I can address on the i2c bus.
Why 0x40? The datasheet reads

Code: Select all

BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
OUT   0     0     SQWE  0     0     RS1   RS0   
So BIT 4 needs to be set, and that would be 0x10.
A typo. I did mean 0x10. I have that in my code. Still no response.

Tomorrow is another day. Thanks for the reply.

Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

Re: DS1307 address query

Thu Apr 07, 2016 7:58 am

I placed 2 LEDs, in series with the pull-ups, on the SDA and SCL lines on the I2C bus. When I do I2Cdetect, the leds flash briefly as they should. That is when the DS1307 gets read and identified. All good.

When I try and read or write to the DS1307, no flashing. Tells me the I2Cset command is incorrect.

I am entering the following: sudo i2cset -y 1 0x68 0x07 0x10 (I am trying to get the SW output to toggle at 1Hz)
I have tried b and w at the end just in case. No use. The b mode is the default for byte mode so I have left it off. There must be something missing from my above string. Just not sure what.

The error I get back is "Could not set address to 0x68: Device or resource busy". The device can't be busy because both SDA and SCL is high. The LEDs are off. The resource must be busy because my command is incomplete.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5798
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: DS1307 address query

Thu Apr 07, 2016 9:07 am

Adco wrote:I placed 2 LEDs, in series with the pull-ups, on the SDA and SCL lines on the I2C bus. When I do I2Cdetect, the leds flash briefly as they should. That is when the DS1307 gets read and identified. All good.

When I try and read or write to the DS1307, no flashing. Tells me the I2Cset command is incorrect.

I am entering the following: sudo i2cset -y 1 0x68 0x07 0x10 (I am trying to get the SW output to toggle at 1Hz)
I have tried b and w at the end just in case. No use. The b mode is the default for byte mode so I have left it off. There must be something missing from my above string. Just not sure what.

The error I get back is "Could not set address to 0x68: Device or resource busy". The device can't be busy because both SDA and SCL is high. The LEDs are off. The resource must be busy because my command is incomplete.
Post the results of i2cdetect.
As I commented earlier, it sounds like you have loaded the kernel driver for ds1307, which will then show up as UU, not 0x68. If you have a kernel driver bound to an address, then I believe all other accesses to it will be blocked, hence it is busy.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5798
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: DS1307 address query

Thu Apr 07, 2016 9:09 am

Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

Re: DS1307 address query

Thu Apr 07, 2016 9:23 am

6by9 wrote:And please don't cross-post - viewtopic.php?f=44&t=98318&sid=74a5149a ... 50#p947543 :(
Again, sorry. :oops:

Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

Re: DS1307 address query

Thu Apr 07, 2016 9:27 am

6by9 wrote:
Adco wrote:
I placed 2 LEDs, in series with the pull-ups, on the SDA and SCL lines on the I2C bus. When I do I2Cdetect, the leds flash briefly as they should. That is when the DS1307 gets read and identified. All good.

When I try and read or write to the DS1307, no flashing. Tells me the I2Cset command is incorrect.

I am entering the following: sudo i2cset -y 1 0x68 0x07 0x10 (I am trying to get the SW output to toggle at 1Hz)
I have tried b and w at the end just in case. No use. The b mode is the default for byte mode so I have left it off. There must be something missing from my above string. Just not sure what.

The error I get back is "Could not set address to 0x68: Device or resource busy". The device can't be busy because both SDA and SCL is high. The LEDs are off. The resource must be busy because my command is incomplete.
Post the results of i2cdetect.
As I commented earlier, it sounds like you have loaded the kernel driver for ds1307, which will then show up as UU, not 0x68. If you have a kernel driver bound to an address, then I believe all other accesses to it will be blocked, hence it is busy.
I can't post pic of i2cdetect result. It is on another machine. I can tell you that address 68 shows UU. Must I remove the DS1307 driver? Where is it?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5798
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: DS1307 address query

Thu Apr 07, 2016 9:51 am

See Phil's response on the other thread
(This is why we get annoyed at cross-posting - the multiple threads end up covering the same ground).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Adco
Posts: 42
Joined: Fri Mar 11, 2016 3:01 pm

Re: DS1307 address query

Thu Apr 07, 2016 9:55 am

6by9 wrote:See Phil's response on the other thread
(This is why we get annoyed at cross-posting - the multiple threads end up covering the same ground).
I do understand. I am only going to talk to you guys via the other thread.

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