kernel patch for Dallas 1-wire interface


223 posts   Page 1 of 9   1, 2, 3, 4, 5 ... 9
by Frank Buss » Sat May 26, 2012 8:11 pm
I've tested the GPIO one-wire implementation in the Linux kernel. This is the Linux kernel patch:

https://github.com/FrankBuss/linux-1/tree/rpi-w1

At the moment it is fixed at GPIO 4, maybe the new Linux pinctrl concept will change this and then this patch is not needed anymore, but if you want to access some one-wire chips, you can use this patch until then.

You need to set the following Linux .config variables: W1_MASTER_GPIO W1 W1_SLAVE_THERM (just tested with "y", but "m" should work, too). See http://elinux.org/RPi_Kernel_Compilation for Linux kernel compile instruction. If you don't want to compile your own kernel, you can install my pre-compiled kernel and modules like this on the Debian image (login as root, or do a "sudo bash" if logged in as "pi") :

cd /boot
wget http://www.frank-buss.de/raspberrypi/kernel-rpi-w1.tgz
tar -xzf kernel-rpi-w1.tgz
rm -f kernel-rpi-w1.tgz
cd /lib/modules
wget http://www.frank-buss.de/raspberrypi/modules-rpi-w1.tgz
tar -xzf modules-rpi-w1.tgz
rm -f modules-rpi-w1.tgz
sync
reboot


This is based on bootc's kernel version and contains also the SPI and I2C driver.

You can use it like this:

root@raspberrypi:~# modprobe w1-gpio
root@raspberrypi:~# ls /sys/bus/w1/devices/w1_bus_master1/
22-0000001d84f2 w1_master_attempts w1_master_search
driver w1_master_max_slave_count w1_master_slave_count
power w1_master_name w1_master_slaves
subsystem w1_master_pointer w1_master_timeout
uevent w1_master_pullup
w1_master_add w1_master_remove
root@raspberrypi:~# cat /sys/bus/w1/devices/w1_bus_master1/22-0000001d84f2/w1_slave
9d 01 4b 46 7f ff 03 10 57 : crc=57 YES
9d 01 4b 46 7f ff 03 10 57 t=25812


With the "ls" command you can see the detected devices, "22-0000001d84f2" in this case (I've tested it with a DS1822 temperature sensor). The output "t=25812" means that the measured temperature is 25.812 °C.

There is a tutorial at this page with some Python example code: http://www.acmesystems.it/1wire
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by domesday » Sat May 26, 2012 8:23 pm
Great stuff Frank, quite a few 1-wire devices that could potentially be used.

Thanks for all your hard work.
Posts: 257
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by arm2 » Mon May 28, 2012 8:18 pm
Being a Linux newbie I tried the precompiled version and it failed to boot:-(
I rewrote the SD card with the latest official Debian download and retried having set up a root password first and rebooted. My collegue checked I was following the instructions and no errors were reported but it also failed to reboot!
Any ideas?
Frank when you next do a kernel compile could you include the DS1307(Which is DS1338 compatible) Real Time Clock driver please?
I'd really appreciate it. Chris.
Posts: 217
Joined: Thu Dec 15, 2011 3:46 pm
by Frank Buss » Mon May 28, 2012 10:30 pm
arm2 wrote:Being a Linux newbie I tried the precompiled version and it failed to boot:-(
I rewrote the SD card with the latest official Debian download and retried having set up a root password first and rebooted. My collegue checked I was following the instructions and no errors were reported but it also failed to reboot!
Any ideas?
Frank when you next do a kernel compile could you include the DS1307(Which is DS1338 compatible) Real Time Clock driver please?
I'd really appreciate it. Chris.


Sorry, I've just tested it on my development SD card. You'll need the latest firmware, too, which can be downloaded at https://github.com/raspberrypi/firmware ... aster/boot
And for me there was a problem with using the 224 MB memory split for the GPU (the *start.elf files), maybe because of my high resolution TFT, but copying arm192_start.elf to start.elf worked.

I've created an update script, which copies the boot files which worked on my platform, which I've tested with a clean debian6-19-04-2012.img installation. Login as "pi", password "raspberry", then you can load and execute it like this:
Code: Select all
sudo bash
wget http://www.frank-buss.de/raspberrypi/w1-test
bash w1-test

It updates the /boot directory with the firmware files and the linux kernel, and installs all modules in the /lib/modules directory for the kernel.

The DS1307 driver is now integrated as a module, too.

BTW: for easier copying command lines from a PC to the terminal, I setup remote access by SSH on the Raspberry Pi by renaming a file in boot:
Code: Select all
cd /boot
sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc

Then you can use your favorite SSH client, like "putty" from Windows, or just "ssh" from Linux, to remote login to your device.
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by Frank Buss » Mon May 28, 2012 10:33 pm
For testing the DS1307 driver module: "modprobe rtc-ds1307"
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by fulburncams » Tue May 29, 2012 9:12 am
Frank

I tried your script and it seems to have stopped my RASPI. I had a clean build, and was running from serial port using sudo. After the last line the RASPI rebooted and never came back, just the single red led came on. Tried powering the RASPI off but no luck. I had not changed the elf file from the standard, is this significant ? I will reformat with the Deb 6 19-04 image and start again. Copy of console output below.
Cheers
Steve

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
pi@raspberrypi:~$ sudo bash
wget http://www.frank-buss.de/raspberrypi/w1-test
bash w1-testroot@raspberrypi:/home/pi# wget http://www.frank-buss.de/raspberrypi/w1-test
--2012-05-29 10:01:59-- http://www.frank-buss.de/raspberrypi/w1-test
Resolving www.frank-buss.de... 87.106.15.184
Connecting to www.frank-buss.de|87.106.15.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 559 [application/octet-stream]
Saving to: `w1-test'

100%[======================================>] 559 --.-K/s in 0s

2012-05-29 10:01:59 (6.42 MB/s) - `w1-test' saved [559/559]

root@raspberrypi:/home/pi# bash w1-test
--2012-05-29 10:02:24-- http://www.frank-buss.de/raspberrypi/kernel-rpi-w1.tgz
Resolving www.frank-buss.de... 87.106.15.184
Connecting to www.frank-buss.de|87.106.15.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1976762 (1.9M) [application/x-gtar]
Saving to: `kernel-rpi-w1.tgz'

100%[======================================>] 1,976,762 255K/s in 6.6s

2012-05-29 10:02:31 (291 KB/s) - `kernel-rpi-w1.tgz' saved [1976762/1976762]

--2012-05-29 10:02:31-- http://www.frank-buss.de/raspberrypi/bo ... _start.elf
Resolving www.frank-buss.de... 87.106.15.184
Connecting to www.frank-buss.de|87.106.15.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2040392 (1.9M) [application/octet-stream]
Saving to: `arm192_start.elf.1'

100%[======================================>] 2,040,392 274K/s in 7.1s

2012-05-29 10:02:39 (282 KB/s) - `arm192_start.elf.1' saved [2040392/2040392]

--2012-05-29 10:02:39-- http://www.frank-buss.de/raspberrypi/bo ... otcode.bin
Resolving www.frank-buss.de... 87.106.15.184
Connecting to www.frank-buss.de|87.106.15.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16528 (16K) [application/octet-stream]
Saving to: `bootcode.bin'

100%[======================================>] 16,528 --.-K/s in 0.07s

2012-05-29 10:02:39 (248 KB/s) - `bootcode.bin' saved [16528/16528]

--2012-05-29 10:02:39-- http://www.frank-buss.de/raspberrypi/bo ... loader.bin
Resolving www.frank-buss.de... 87.106.15.184
Connecting to www.frank-buss.de|87.106.15.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 314691 (307K) [application/octet-stream]
Saving to: `loader.bin'

100%[======================================>] 314,691 268K/s in 1.1s

2012-05-29 10:02:40 (268 KB/s) - `loader.bin' saved [314691/314691]

--2012-05-29 10:02:40-- http://www.frank-buss.de/raspberrypi/modules-rpi-w1.tgz
Resolving www.frank-buss.de... 87.106.15.184
Connecting to www.frank-buss.de|87.106.15.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5284104 (5.0M) [application/x-gtar]
Saving to: `modules-rpi-w1.tgz'

100%[======================================>] 5,284,104 299K/s in 18s

2012-05-29 10:02:58 (294 KB/s) - `modules-rpi-w1.tgz' saved [5284104/5284104]


The system is going down for reboot NOW!(ttyAMA0) (Tue May 29 10:03:18 2012):
root@raspberrypi:/home/pi# Restarting system.


User avatar
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm
by arm2 » Tue May 29, 2012 11:17 am
Thanks Frank.
fulburncams: I got the same result until I updated the SD card using my PC with the files found at Franks https://github.com/raspberrypi/firmware ... aster/boot link. I update all nine to be sure.
RTC:
I unplugged the network cable and plugged in my battery backed DS1338 RTC module which has had its date set correctly on another ARM system but on rebooting 'date' is reporting the time I last turned off. Reading the RTC on power up is that yet to be implemented?

modprobe rtc-ds1307
gives
FATAL: module rtc-ds1307 not found
I've tried that as both root & pi user
Posts: 217
Joined: Thu Dec 15, 2011 3:46 pm
by Frank Buss » Tue May 29, 2012 4:39 pm
Looks like there were still some bugs in the update script. The problem from fulburncams was, that there was alread a file names arm192_start.elf, so wget saved it as arm192_start.elf.1 and then the script failed and leaved the /boot directory without any start.elf file. I guess I've tested the start.elf file first, then tested the update script, so was not a clean Debian image installation. You could fix it by saving the file http://www.frank-buss.de/raspberrypi/bo ... _start.elf as start.elf on your PC (the /boot directory (first partition) is displayed as default in Windows).

The problem from arm2 could be the same: there was already a modules file, so the new modules file was not extracted, because wget saved it to another filename.

I've fixed the script, now it should really work on the Debian image, when started as root, and even when started multiple times for later updates. If the arm192_start.elf version doesn't work because of display resolution, you could try to save http://www.frank-buss.de/raspberrypi/bo ... _start.elf as start.elf. I'll treat you to a beer if it still doesn't work :-)
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by eikcam » Wed May 30, 2012 10:44 am
Frank,

Great work getting the DS18B20 working. I am currently using an Arduino to collect and log all of my temp data but I was hoping to shift all the work over the my Raspberry Pi once it arrived.

Well, guess what? Today it arrived. The first thing I did was run your script and hook up my breadboard. Apart from initially putting the DS18B20 in backwards, and burning my fingers due to it being REALLY hot when I realised 3 minutes late, all is working fine.

Output is as follows;
Code: Select all
pi@raspberrypi:~$ modprobe w1-gpio
pi@raspberrypi:~$ ls /sys/bus/w1/devices/w1_bus_master1/
28-000002f83611  uevent                     w1_master_name     w1_master_search
driver           w1_master_add              w1_master_pointer  w1_master_slave_count
power            w1_master_attempts         w1_master_pullup   w1_master_slaves
subsystem        w1_master_max_slave_count  w1_master_remove   w1_master_timeout
pi@raspberrypi:~$ cat /sys/bus/w1/devices/w1_bus_master1/28-000002f83611/w1_slave
74 01 4b 46 7f ff 0c 10 55 : crc=55 YES
74 01 4b 46 7f ff 0c 10 55 t=23250


As my current Arduino project has two sensors, I will endevour to hook another one up shortly. This post was mainly to show that others are using your works even though we don't all post.

Can't wait to see how people start using this.

Daniel.
Posts: 6
Joined: Wed May 30, 2012 10:39 am
by eikcam » Wed May 30, 2012 10:52 am
Frank,

Further to my last, the following output is with two DS18B20's;

Code: Select all
pi@raspberrypi:/sys/bus/w1/devices$ ls
28-000002f81b52  28-000002f83611  w1_bus_master1
pi@raspberrypi:/sys/bus/w1/devices$ cat 28-000002f81b52/w1_slave 28-000002f83611/w1_slave
70 01 4b 46 7f ff 10 10 e1 : crc=e1 YES
70 01 4b 46 7f ff 10 10 e1 t=23000
6e 01 4b 46 7f ff 02 10 71 : crc=71 YES
6e 01 4b 46 7f ff 02 10 71 t=22875
pi@raspberrypi:/sys/bus/w1/devices$ cat 28-000002f81b52/w1_slave 28-000002f83611/w1_slave
7c 01 4b 46 7f ff 04 10 09 : crc=09 YES
7c 01 4b 46 7f ff 04 10 09 t=23750
78 01 4b 46 7f ff 08 10 51 : crc=51 YES
78 01 4b 46 7f ff 08 10 51 t=23500
pi@raspberrypi:/sys/bus/w1/devices$

For the second call I put my finger on the sensors to get a rise. I'm amazed that the initial sensor still works after I cooked it for a bit. Quality sensors :)

Daniel.
Posts: 6
Joined: Wed May 30, 2012 10:39 am
by eikcam » Wed May 30, 2012 11:39 am
With some basic perl scripting, I have come up with the following;

Code: Select all
@sensors = `cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves`;
chomp(@sensors);

foreach $line(@sensors) {
  $output = `cat /sys/bus/w1/devices/$line/w1_slave`;
  $output =~ /t=(?<temp>\d+)/;
  $calc = $+{temp} / 1000;
  print "Sensor ID: $line, Temp: $calc\n";
}

It outputs this:
Code: Select all
Sensor ID: 28-000002f83611, Temp: 22.062
Sensor ID: 28-000002f81b52, Temp: 22.375


Just thought it worth posting for info.

Daniel.
Posts: 6
Joined: Wed May 30, 2012 10:39 am
by Frank Buss » Wed May 30, 2012 5:25 pm
Great Daniel, glad to see that it finally works. The kernel is still in much development, but mabe it would be a good idea to compile all possible drivers as modules (for devices which are attachable to the Raspberry Pi)? They are just some kB each. Then they could be loaded like in other big Linux distributions, where the hardware is detected at boot time and the drivers are automaticly loaded.
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by davidmam » Wed May 30, 2012 5:34 pm
This looks great - can't wait to try this out. Just updated the image on the card and when I get home I'll be getting out the breadboard and the temperature sensors..
Posts: 98
Joined: Tue Dec 06, 2011 4:13 pm
by Bass » Thu May 31, 2012 6:53 am
I also have a couple of DS18B20, but a bit unsure about how to hook them up?
http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf
Leg 1 -> gnd
Leg 2 -> gpio 4
Leg 3 -> 3v3 or 5v0?

Thanks!
Posts: 12
Joined: Tue Feb 21, 2012 7:21 am
by davidmam » Thu May 31, 2012 8:44 am
I'm sure someone else will correct me but I would expect it to be 3.3V and a bridging resistor between the data and +ve lines as per the figure at http://www.acmesystems.it/1wire

Yet to try this, waiting on a breadboard to prototype with but this would make web based home automation/building monitoring a doddle.
Posts: 98
Joined: Tue Dec 06, 2011 4:13 pm
by GadgetUK » Thu May 31, 2012 8:58 am
Not sure how far you're intending on pushing this in terms of number of sensors / length of cable. But I'd recommend the use of a 'proper' 1-wire driver chip.
What software are you using ?

I've been successful in using OWFS, with an i2c 1-wire bus master see my blog post http://raspberrypi.homelabs.org.uk/i2c- ... e-masters/

I'm also developing a 1-wire breakout board, so if you've got any suggestions / features you'd like to see comments on the following please.

http://raspberrypi.homelabs.org.uk/rasp ... expansion/
Posts: 29
Joined: Thu Jan 19, 2012 6:02 pm
by eikcam » Thu May 31, 2012 12:01 pm
Bass,

The post from davidmam is the easiest to follow. That is how my one-wire systems are wired up. Both with Pi and Arduino.

GadgetUK,

If one is only intending to use a few 1-wire devices then I see no need to go for anything more than what has been described. I am no expert though as I just use these sensors to monitor my delicious homebrew fridge to ensure the perfect fermentation.

Currently I am running the sensors over about 2-metres of cable and they are working fine. Stable for all the results I have logged so far anyways.

But like I said, im no expert. Hell, I put the sensor in backwards and burnt my finger :D

As for software, whats the point in having a Pi if you arent planning on writing your own. If my current perl script can extract the required data then I'm extremely positive that my highly advanced, almost godlike, coding skills will result in a flawless application coming to fruition.

Cheers,
Dan.
Posts: 6
Joined: Wed May 30, 2012 10:39 am
by GadgetUK » Thu May 31, 2012 8:41 pm
eikcam wrote:Currently I am running the sensors over about 2-metres of cable and they are working fine. Stable for all the results I have logged so far anyways.


Short lengths are probably fine, the other thing to consider is with 3.3v you are close to hitting the minimum voltage for the DS18B20's due to voltage drop in the cable.

The other thing to consider is that the amount of available current on the 3v3 is 50mA, and the i/o pins themselves are limited to 2-16mA

eikcam wrote:As for software, whats the point in having a Pi if you arent planning on writing your own. If my current perl script can extract the required data then I'm extremely positive that my highly advanced, almost godlike, coding skills will result in a flawless application coming to fruition.


Pi is low power, small, and cheap providing a nice bridge between 1-wire and ethernet. Using something like OWFS , I can combine multiple Pi's into a singe 1-wire 'tree' with no effort.
:D
Posts: 29
Joined: Thu Jan 19, 2012 6:02 pm
by JonSenior » Fri Jun 01, 2012 9:07 am
@GadgetUK:

This is exactly what I've been looking for, thanks for your hard work. I'd definitely be interested in purchasing a populated breakout board in the not-too-distant future. I'm currently buried by the construction of my house, but once built, the sensors will be installed in every conceivable location and my (soon to arrive) Pi will be charged with the job of monitoring the house and (in the future) controlling it to some extent. I was stalling over the choice of a USB to 1-wire interface and would have preferred something more tightly integrated. Since the cabling has to cover the whole of a house, bit-banged GPIO seems to be not-quite solid enough.

In any case, thanks again. This thread has been bookmarked and I'll be watching your development with interest. If you don't get enough interest for a populated board, but could provide etched ones, I guess I could try my hand at SMD soldering. The chips are cheap enough that I could afford to fry one or two while learning! :-)
Posts: 7
Joined: Fri Jun 01, 2012 8:53 am
by netomx » Fri Jun 01, 2012 4:56 pm
Thank you!! Thats what I needed!!
Posts: 78
Joined: Tue Oct 11, 2011 4:06 am
by Frank Buss » Fri Jun 01, 2012 8:18 pm
GadgetUK wrote:Short lengths are probably fine, the other thing to consider is with 3.3v you are close to hitting the minimum voltage for the DS18B20's due to voltage drop in the cable.


The active current of the DS18B20 is 1.5 mA and minimum allowed voltage is 3 V. To be below a 0.3 V voltage drop at 1.5 mA, the cable can be up to 200 ohm, so should be not a problem.

GadgetUK wrote:The other thing to consider is that the amount of available current on the 3v3 is 50mA, and the i/o pins themselves are limited to 2-16mA


I think the default setting is 8 mA: http://www.scribd.com/doc/91353537/GPIO-Pads-Control and there is the warning about simultaneous switching outputs limitations. But the GPIOs are not protected, so short to GND or even worse, accidently to higher voltages, might damage the processor. And if you use cables longer than maybe 1 or 2 m, this could be a high driving load (capacitance and inductance), or near motors could induce high voltage spikes, which could damage the chip, too. Good idea to use driver chips for cables longer than 1 m.

A better solution to wire the whole house would be RS485, which is more fault tolerant and allows much longer cables, but 1-wire adds at least a CRC check byte, which allows to detect transfer errors, so it might work with a driver chip.
User avatar
Posts: 92
Joined: Fri Jan 06, 2012 4:39 pm
by davidmam » Fri Jun 01, 2012 8:58 pm
Thank you guys so much for the code and examples - it worked pretty much straight out of the box (once I had put the DS1820 in the right way round and let the polyfuse reset..)

Now to work out how to drive relays etc..
Posts: 98
Joined: Tue Dec 06, 2011 4:13 pm
by netomx » Sat Jun 02, 2012 10:47 pm
If mine says:

Code: Select all
root@raspberrypi:/sys/bus/w1/devices/w1_bus_master1/28-0000030a0170# cat w1_slave
50 05 4b 46 7f ff 0c 10 1c : crc=1c YES
50 05 4b 46 7f ff 0c 10 1c t=85000


I'm using the wrong resistor?
Posts: 78
Joined: Tue Oct 11, 2011 4:06 am
by eikcam » Sun Jun 03, 2012 5:22 am
davidmam wrote:Now to work out how to drive relays etc..


Thats my next mission as well.
Posts: 6
Joined: Wed May 30, 2012 10:39 am
by eikcam » Sun Jun 03, 2012 5:26 am
netomx wrote:If mine says:

Code: Select all
root@raspberrypi:/sys/bus/w1/devices/w1_bus_master1/28-0000030a0170# cat w1_slave
50 05 4b 46 7f ff 0c 10 1c : crc=1c YES
50 05 4b 46 7f ff 0c 10 1c t=85000


I'm using the wrong resistor?

85c is the initial value of the sensor on power up. It seems that your sensor isnt doing any calculations. What size resistor are you using? 4.7k?
Posts: 6
Joined: Wed May 30, 2012 10:39 am