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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:10 am

PhilE wrote:I'm guessing you are using the i2c-rtc overlay? By doing so you are telling the I2C subsystem that the device at 0x68 is owned by the ds1307 driver. If you run "i2cdetect -y 1" you will see that the slot for the 68 address contains "UU" - used.

What you are trying to do is interfere with that device under the feet of the driver, which is not allowed. I think you would get the same result if you took the non-DT route - keep (or add) dtparam=i2c_arm=on, remove dtoverlay=i2c-rtc, reboot then run:

Code: Select all

sudo sh -c "echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"
Wow! Quick reply, thanks.

I have dtparam=i2c_arm=on in /boot/config.txt. Should dtoverlay also be there or is it in another file?

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:10 am

Adco wrote:I can read from the DS1307 using hwclock and i2cdetect but cannot write my own commands to it.

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.
Please don't cross-post viewtopic.php?f=44&t=143410 :(
I've just given the same answer as Phil on that thread.
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: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:12 am

I found dtoverlay in modules. I commented it out and rebooted. Still not communicating with DS1307.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:20 am

6by9 wrote:
Adco wrote:I can read from the DS1307 using hwclock and i2cdetect but cannot write my own commands to it.

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.
Please don't cross-post viewtopic.php?f=44&t=143410 :(
I've just given the same answer as Phil on that thread.
Sorry :oops:
I put that post up last night. I then read through more threads this morning and found this one with Phil giving good advise. I am just a bit frustrated with myself for not getting things to work and became impatient.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:32 am

The bottom line is that you can't get RTC functionality (provided by the ds1307 driver) and user access via i2c-dev at the same time - pick one.

1) run "i2c-detect -y 1" and confirm that 0x68 is in use (UU). If it isn't then something else is going wrong.
2) Find what is loading the driver. It is either an overlay (dtoverlay=i2c-rtc,ds1307 in config.txt) or some kind of init or systemd or /etc/rc.local script.
3) If you want to hack around with the device then disable the loading of the driver.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:46 am

PhilE wrote:The bottom line is that you can't get RTC functionality (provided by the ds1307 driver) and user access via i2c-dev at the same time - pick one.

1) run "i2c-detect -y 1" and confirm that 0x68 is in use (UU). If it isn't then something else is going wrong.
2) Find what is loading the driver. It is either an overlay (dtoverlay=i2c-rtc,ds1307 in config.txt) or some kind of init or systemd or /etc/rc.local script.
3) If you want to hack around with the device then disable the loading of the driver.
I want to control the DS1307 with my own commands by using i2cset and i2cget. Eventually, I want a Python IDLE3 program to do some remote controlling of i2c devices. I think i2c-dev must be the way to go.

When I run i2cdetect, location 68 has UU in it. I will comment out all lines that refer to ds1307 and only leave i2c-dev in /etc/modules. I did have rtc-ds1307 in that file.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:52 am

PhilE wrote:The bottom line is that you can't get RTC functionality (provided by the ds1307 driver) and user access via i2c-dev at the same time - pick one.

1) run "i2c-detect -y 1" and confirm that 0x68 is in use (UU). If it isn't then something else is going wrong.
2) Find what is loading the driver. It is either an overlay (dtoverlay=i2c-rtc,ds1307 in config.txt) or some kind of init or systemd or /etc/rc.local script.
3) If you want to hack around with the device then disable the loading of the driver.
One option would be to mod the kernel driver to set up the DS1307 the way you want it, then userspace doesn't need to touch it.
You do then have a custom kernel module that you'll have to maintain and update on any kernel update (or attempt to get upstreamed).
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: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:57 am

Please look at my string: sudo i2cset -y 1 0x68 0x07 0x10. Is it correct? If I am missing something there, nothing will make it work.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 9:59 am

Does i2cdetect still show UU?

Do you get an error message?

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:01 am

6by9 wrote:
PhilE wrote:The bottom line is that you can't get RTC functionality (provided by the ds1307 driver) and user access via i2c-dev at the same time - pick one.

1) run "i2c-detect -y 1" and confirm that 0x68 is in use (UU). If it isn't then something else is going wrong.
2) Find what is loading the driver. It is either an overlay (dtoverlay=i2c-rtc,ds1307 in config.txt) or some kind of init or systemd or /etc/rc.local script.
3) If you want to hack around with the device then disable the loading of the driver.
One option would be to mod the kernel driver to set up the DS1307 the way you want it, then userspace doesn't need to touch it.
You do then have a custom kernel module that you'll have to maintain and update on any kernel update (or attempt to get upstreamed).
I am not sure how to mod a kernel driver. I am having enough drama getting used to Python and Linux. :shock:

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:03 am

PhilE wrote:Does i2cdetect still show UU?

Do you get an error message?
Still shows UU in location 68.

Only get an error when I try i2cset.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:09 am

Some more info on contents of /etc/modules
snd-bcm2835
i2c-bcm2835
i2c-dev

/boot/config.txt
dtparam=i2c_arm=on (there are lots of other lines but only showing what is relevant)

Are there any other files that could contain stuff to be changed or commented out?

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:14 am

Adco wrote:One option would be to mod the kernel driver to set up the DS1307 the way you want it, then userspace doesn't need to touch it.
You do then have a custom kernel module that you'll have to maintain and update on any kernel update (or attempt to get upstreamed).
I am not sure how to mod a kernel driver. I am having enough drama getting used to Python and Linux. :shock:[/quote]
Admittedly that is only an option for the brave.
Adco wrote:Still shows UU in location 68.

Only get an error when I try i2cset.
Run "lsmod" and there is likely to be a module "rtc-ds1307" or similar.
"sudo rmmod rtc-ds1307" (or amend if I've got that module name wrong). That should unload the module until reboot, and i2cdetect should no longer report UU on 0x68.
Phil listed the most likely sources to trigger loading the driver in posting.php?mode=quote&f=44&p=947597#pr947571 It won't be loaded by default.
Resort to "grep -Ri ds1307 ." if needs be to search for the string.

Or start again from a fresh install of Raspbian.
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.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:16 am

Also, slightly off topic, remove snd-bcm2835 from /etc/modules and add "dtparam=audio=on" to config.txt instead.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:23 am

I recall that there was a stage yesterday when I did i2cdetect and there was 68 instead of UU in the block of info. I don't remember how I got that.

Are you saying that UU means that the system has control and seeing 68 means I have control?

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:27 am

PhilE wrote:Also, slightly off topic, remove snd-bcm2835 from /etc/modules and add "dtparam=audio=on" to config.txt instead.
I took the snd-bcm line out of modules. The second line was already in /boot/config.txt.

No luck. Still UU at 68.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:30 am

Interestingly, I unplugged the DS1307, did a reboot and then i2cdetect and it shows 68 instead of UU and when I do an i2cset, I don't get the usual error. Nothing else happens but at least there is no error.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:34 am

:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D I got it to work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

It must be the snd-bcm thingy. After I removed the DS1307, rebooted and did the write, my LED flashes at 1Hz.

Thanks for the patience and help. Getting this right gives me hope for the future.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 10:36 am

I can now read all the address using i2cget. Told you, easy peasy.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 07, 2016 12:34 pm

I think I must summarise this solution so that some future user can benefit.

This only pertains to someone trying to have control of a DS1307 on a Raspberry Pi 3 running Noobs.

1) In the file /etc/modules have line i2c-bcm2835 and i2c-dev. If snd-bcm2835 is there, comment it out by placing # in front of the line.
2) In the file /boot/config.txt, near the end where it deals with hardware interfaces, have the line dtparam=i2c_arm=on enabled. i.e. no # in front of line.
3) using the terminal, type sudo i2cdetect -y 1. This should bring up a block with location 68 showing a 68. This means that the DS1307 has been detected. If that location shows UU, it means the system has control and you won't be able to do anything with it.
4) then type sudo i2cset -y 1 0x68 0x00 0x00. This enable the oscillator on the DS1307.
5) then type sudo i2cset -y 1 0x68 0x07 0x10. This enable the clock output to give a 1Hz pulse on pin 7 of the DS1307. If you connect an LED with a 470 ohm resistor to 3V3 from pin 7, it will flash.

Thereafter, you can set time registers using i2cget and you can read times using i2cset.

Thanks to PhilE and 6by9 for all the help. They are the ones that sorted this out for me, I just summarised to hopefully help someone.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Fri Apr 08, 2016 8:10 am

What happens exactly when I2CDETECT command is issued?

My guess is that the system, starting at Device Address 0x03 and ending at 0xff, sends the address out and waits each time for an ACK signal. It then remembers each ACK received and shows it on the table afterwards.

In other words, I2CDETECT is only there to identify devices. (ETA: Silly point)

Also, what is the default I2C clock rate? Is it 100kHz and how can it be set to 400kHz?

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Fri Apr 08, 2016 8:33 am

The i2c-detect man page is here, and the source code is here. It seems to use writes on same addresses and reads on others, but that can be controlled by command line options.

Code: Select all

dtparam=i2c_arm_baudrate=400000
is how you change the clock rate (see /boot/overlays/README for details of the other things you can tweak).

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Fri Apr 08, 2016 9:21 am

PhilE wrote:The i2c-detect man page is here, and the source code is here. It seems to use writes on same addresses and reads on others, but that can be controlled by command line options.

Code: Select all

dtparam=i2c_arm_baudrate=400000
is how you change the clock rate (see /boot/overlays/README for details of the other things you can tweak).
Thanks, that's great info.

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 21, 2016 12:53 pm

I'm back! So, I can read and write to a PIC 16F884 set to slave mode! Works like a dream.

I am using I2C #1 on the RPi. That is Pin 3 & 5 on the 40 way GPIO header. I want to use I2C #0 as well. I want to put the RTC on that port and leave the slaves on port #1. There will be 100 slaves in total and I want to keep them separate from the RTC.

When I attach the DS1307 to I2C #0 and type I2Cget -y 0, I get the error message "Could not open file"

Any ideas?

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

Re: Optional interfaces (I2C, I2S, SPI) and Device Tree

Thu Apr 21, 2016 1:14 pm

Please don't use i2c0 - it is there for the use of the VideoCore VPU/GPU, which uses it to drive cameras and control the PMU or the Pi3 LEDs. The ARM-side driver performs no interlocking with the VPU for use of the I2C controller, so accessing it at the wrong time can be dangerous.

If you are still determined to try, use "dtparam=i2c_vc=on", where vc is short for VideoCore.

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

Who is online

Users browsing this forum: No registered users and 11 guests