Gadjetnut
Posts: 18
Joined: Fri Apr 05, 2013 9:13 pm

Source code to w1-therm?

Wed Apr 10, 2013 2:03 am

Does anyone know where I can get the source code for the w1-therm program that polls GPIO 4 that is interface to a 1 wire Dallas DS18B20 temperature sensor?

I want to be able to change the GPIO port it uses.

Alternatively if anyone know how to change the GPIO port, that will also do the trick.

Thanks

User avatar
silverfox0786
Posts: 215
Joined: Mon Feb 04, 2013 10:32 pm
Location: Slough, UK
Contact: Website

Re: Source code to w1-therm?

Thu Apr 11, 2013 10:37 pm

you cant change the GPIO it hard wired to GPIO 4
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)

jjackowski
Posts: 44
Joined: Thu Jan 10, 2013 5:25 am

Re: Source code to w1-therm?

Fri Apr 12, 2013 10:26 pm

The use of GPIO 4 comes from some BCM2835 platform data that is referenced by the w1-gpio kernel module. It looks like changing the w1-gpio module, or better yet, make another module, should be relatively simple. At least after you get the kernel building and if you're familiar with C . The code in w1-gpio isn't complex; all the one-wire control logic is elsewhere.

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: Source code to w1-therm?

Fri Apr 12, 2013 10:39 pm

Having been along a similar path, you may find the tutorial at:-
http://www.cl.cam.ac.uk/freshers/raspbe ... ndix-three
useful.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

Gadjetnut
Posts: 18
Joined: Fri Apr 05, 2013 9:13 pm

Re: Source code to w1-therm?

Fri Apr 12, 2013 11:14 pm

Hi feverish,

I've seen that tutorial before, and find it very good, but it doesn't answer the question. Having followed the link from your reply I noticed an email address at the top and have emailed the author. I'll let you know if I get a reply.

Jonathan

Gadjetnut
Posts: 18
Joined: Fri Apr 05, 2013 9:13 pm

Re: Source code to w1-therm?

Sat Apr 13, 2013 2:58 am

Thanks jjackowski, you solved the problem. :D

I found the offending hard coded pin for one-wire in arch\arm\mach-bcm2708\bcm2708.c

// use GPIO 4 for the one-wire GPIO pin, if enabled
#define W1_GPIO 4

Why do people hard code things - really!!??

jjackowski
Posts: 44
Joined: Thu Jan 10, 2013 5:25 am

Re: Source code to w1-therm?

Sat Apr 13, 2013 11:36 pm

Glad I could help!

I'm not sure why it is hard coded like that. The module even has a parameter; it tells the module what pin to use for a strong pull-up. Seems like a fine example for making it take an optional parameter for the one-wire pin, and falling back on the hard coded one as a default. Certainly the default has to be platform specific, but why not have an optional parameter, too?

Oh well. At least w1-gpio isn't a complex piece of code.

ant_thomas
Posts: 115
Joined: Fri May 04, 2012 4:33 pm

Re: Source code to w1-therm?

Tue Apr 30, 2013 4:56 pm

Is it possible to make this GPIO pin change and rebuild the required modules without recompiling the whole kernel?

jjackowski
Posts: 44
Joined: Thu Jan 10, 2013 5:25 am

Re: Source code to w1-therm?

Tue Apr 30, 2013 5:31 pm

ant_thomas wrote:Is it possible to make this GPIO pin change and rebuild the required modules without recompiling the whole kernel?
Yes, as long as the w1 stuff is in modules as apposed to built-into the kernel. In that case, you can even unload the old modules and then load the new ones without rebooting. Make the pin a module parameter and you could change it without rebuilding the module. I haven't tried that, yet, but the w1-gpio module does take a parameter already.

ant_thomas
Posts: 115
Joined: Fri May 04, 2012 4:33 pm

Re: Source code to w1-therm?

Tue Apr 30, 2013 7:39 pm

jjackowski wrote:Yes, as long as the w1 stuff is in modules as apposed to built-into the kernel. In that case, you can even unload the old modules and then load the new ones without rebooting. Make the pin a module parameter and you could change it without rebuilding the module. I haven't tried that, yet, but the w1-gpio module does take a parameter already.
Thanks.

I better have a look how to compile the modules with the relevant changes!

Damgot
Posts: 22
Joined: Thu Feb 28, 2013 12:51 pm

Re: Source code to w1-therm?

Thu Jun 20, 2013 7:39 am

Hello,

I have the same problem with this GPIO 4 for the One-Wire and would like to put GPIO 17.

The solution seems to be the good one for me but I'm a noob in linux, I started to learn linux one month ago.

So, could you please tell me :
- How can I acces to arch\arm\mach-bcm2708\bcm2708.c (because there is no folder "arch" in root)
- How can I recompile the module (and which module ?)

Many thanks for your help

marknorgren
Posts: 1
Joined: Thu Jul 25, 2013 2:08 pm

Re: Source code to w1-therm?

Thu Jul 25, 2013 3:04 pm

@ant_thomas

Were you able to change the PIN and get it working? Do you know if there are any plans to make this configurable in the future?

I would love to be able to change the GPIO pin my 1 wire sensor is running on.

I have a openSprinkler setup that is using GPIO 4, so it would be very helpful to move the 1-wire to another pin.

TNe-TWo
Posts: 2
Joined: Tue Aug 27, 2013 6:04 pm

Re: Source code to w1-therm?

Tue Aug 27, 2013 6:13 pm

Hello,

>I found the offending hard coded pin for one-wire in arch\arm\mach-bcm2708\bcm2708.c
yes it's work
i have change the gpio from 4 to 29 (on P5 (new B 512MB)) while i use Raspi-LCD (http://www.emsystech.de/raspi-lcd/)
and this use also GPIO 4


i compile kernel under ubuntu with this http://elinux.org/RPi_Kernel_Compilation
install new image and modules

mathewjenkinson
Posts: 60
Joined: Mon Mar 26, 2012 8:13 am

Re: Source code to w1-therm?

Wed Oct 02, 2013 9:26 pm

Hi all,

Has the GPIO hardwire'ed issue been recompiled?
Im stuck in the same position and would like to use w1-therm on a different pin.

many thanks!
Mathew

TNe-TWo
Posts: 2
Joined: Tue Aug 27, 2013 6:04 pm

Re: Source code to w1-therm?

Thu Oct 03, 2013 4:20 pm

Hello

i found a better way to change it without rebuild kernel image

only patch w1-gpio.c

--- w1-gpio.c Fr 30. Aug 20:01:56 2013
+++ w1-gpio-patch.c Do 3. Okt 18:17:08 2013
@@ -18,6 +18,10 @@
#include "../w1.h"
#include "../w1_int.h"

+// use GPIO 31 for the one-wire GPIO pin, if enabled
+#define W1_GPIO 31
+static int w1_gpio_org;
+
static int w1_gpio_pullup = 0;
module_param_named(pullup, w1_gpio_pullup, int, 0);

@@ -68,6 +72,10 @@
if (!master)
return -ENOMEM;

+ printk(KERN_INFO "ORG. GPIO pin is %d change to %d\n", pdata->pin,W1_GPIO);
+ w1_gpio_org = pdata->pin;
+ pdata->pin = W1_GPIO;
+
err = gpio_request(pdata->pin, "w1");
if (err)
goto free_master;
@@ -118,6 +126,8 @@
pdata->enable_external_pullup(0);

w1_remove_master_device(master);
+ printk(KERN_INFO "Restore ORG. GPIO pin %d\n", w1_gpio_org);
+ pdata->pin = w1_gpio_org;
gpio_free(pdata->pin);
kfree(master);


TNe

when is need i can send a bin file with GPIO you want...

dpenezic
Posts: 99
Joined: Sat Dec 22, 2012 2:36 pm

Re: Source code to w1-therm?

Wed Oct 16, 2013 9:36 am

Hi all,
I dont like idea that something not necessary is only hard coded in file without possibility to be change on execution time, so i change w1-gpio.c file and now w1-gpio module is configurable on execution time.

Follow is diff :

Code: Select all

diff -Naur w1-gpio.c.orig w1-gpio.c
--- w1-gpio.c.orig      2013-10-16 08:04:25.006736402 +0200
+++ w1-gpio.c   2013-10-16 10:23:33.816724916 +0200        
@@ -21,6 +21,12 @@                                         
 static int w1_gpio_pullup = 0;                            
 module_param_named(pullup, w1_gpio_pullup, int, 0);       
                                                           
+static int gpiopin = 0;                                   
+module_param(gpiopin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+MODULE_PARM_DESC(gpiopin, "GPIO pin number");
+
+static int w1_gpio_orig;
+
 static void w1_gpio_write_bit_dir(void *data, u8 bit)
 {
        struct w1_gpio_platform_data *pdata = data;
@@ -68,6 +74,12 @@
        if (!master)
                return -ENOMEM;

+        if (gpiopin > 0) {
+                       printk(KERN_INFO "1-Wire GPIO pin is %d change to %d\n", pdata->pin,gpiopin);
+               w1_gpio_orig = pdata->pin;
+               pdata->pin = gpiopin;
+        }
+
        err = gpio_request(pdata->pin, "w1");
        if (err)
                goto free_master;
@@ -83,12 +95,14 @@
                master->write_bit = w1_gpio_write_bit_dir;
        }

-       if (w1_gpio_pullup)
-               if (pdata->is_open_drain)
+       if (w1_gpio_pullup) {
+               if (pdata->is_open_drain) {
                        printk(KERN_ERR "w1-gpio 'pullup' option "
                               "doesn't work with open drain GPIO\n");
-               else
+               } else {
                        master->bitbang_pullup = w1_gpio_bitbang_pullup;
+               }
+        }

        err = w1_add_master_device(master);
        if (err)
@@ -118,6 +132,12 @@
                pdata->enable_external_pullup(0);

        w1_remove_master_device(master);
+
+        if (gpiopin > 0) {
+                       printk(KERN_INFO "1-Wire GPIO pin is restored to %d\n", w1_gpio_orig);
+               pdata->pin = w1_gpio_orig;
+        }
+
        gpio_free(pdata->pin);
        kfree(master);
When use module you may use it on follow way :
modprobe w1-gpio (standard hard coded pin 4 will be used)
modprobe w1-gpio gpiopin=18 (pin 18 will be used for 1-Wire devices. Pin number need to be real pin number not pin position on GPIO interface)

For your convenience I attach binary executable module file to this post.
You need to make backup copy of w1-gpio.ko file in /lib/modules/3.6.11+/kernel/drivers/w1/masters and gunzip and copy binary file to same place. Module is compiled against 2013-09-25-wheezy-raspbian and will not work to other distribution.

Regards,
Dubravko Penezic
Attachments
w1-gpio.ko.gz
(2.63 KiB) Downloaded 534 times

User avatar
theoriginalxbrav
Posts: 41
Joined: Sun May 12, 2013 7:16 pm

Re: Source code to w1-therm?

Sun Dec 01, 2013 10:47 pm

Is there any chance we could have it recompiled for 3.10.19? I have no idea how to patch my module myself.
Projects:
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf

dpenezic
Posts: 99
Joined: Sat Dec 22, 2012 2:36 pm

Re: Source code to w1-therm?

Mon Dec 02, 2013 10:27 am

theoriginalxbrav wrote:Is there any chance we could have it recompiled for 3.10.19? I have no idea how to patch my module myself.
I will try to do it :) ... unfortunately I dont have a lot time today, but tomorrow i will investigate what need to be done ...

Regards,
Dubravko Penezic

User avatar
theoriginalxbrav
Posts: 41
Joined: Sun May 12, 2013 7:16 pm

Re: Source code to w1-therm?

Tue Dec 03, 2013 12:21 am

dpenezic wrote:
theoriginalxbrav wrote:Is there any chance we could have it recompiled for 3.10.19? I have no idea how to patch my module myself.
I will try to do it :) ... unfortunately I dont have a lot time today, but tomorrow i will investigate what need to be done ...

Regards,
Dubravko Penezic
Thanks! I tried compiling your previous additions into the code, but it seemed to cause a crazy kernel panic (probably my fault). Second attempt increased boot time to 5+ minutes and USB never initialized.

I'd keep messing around with it, but I only have remote access currently. My Pi camera rig is surrounded by 2 feet of snow currently!

Image
Projects:
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf

dpenezic
Posts: 99
Joined: Sat Dec 22, 2012 2:36 pm

Re: Source code to w1-therm?

Tue Dec 03, 2013 8:01 am

Hi,
i will need follow from your RPi .config file:

Code: Select all

zcat /proc/config.gz > .config
My WLAN probe use only standard distribution with few Kernel patches :).

Regards,
DUbravko Penezic

User avatar
theoriginalxbrav
Posts: 41
Joined: Sun May 12, 2013 7:16 pm

Re: Source code to w1-therm?

Tue Dec 03, 2013 1:45 pm

Ironically, as soon as I exported my .config, I realized I had inadvertently downgraded my kernel after screwing up one of my builds. rpi-update is currently delivering 3.10.19+, but the raspberrypi-bootloader package in aptitude is still delivering 3.6.11+. I tried the old kernel module and it worked.

I'll see if I can patch up another Pi to 3.10.19+ to get you the proper .config file.
Projects:
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf

YoungJules
Posts: 14
Joined: Thu Jan 26, 2012 12:13 pm

Re: Source code to w1-therm?

Wed Dec 04, 2013 8:58 pm

Rather than run my thermometer and piface digital through the pirack, I've connected the thermometer to the auxiliary GPIO connector, using pin 2 (3.3v power), pin 6 (GPIO11 aka BCM GPIO 31), and pin 8 (ground).

Using modprobe, just add the option gpiopin=31 (it's 31 in my case, as I used pin 6 of the auxiliary GPIO)

I couldn't get the diff file to work, so have made the changes manually. In the tar file is the fully patched w1-gpio.c and the w1-gpio.ko file compiled for 3.10.19+

Hope this helps someone, thanks very much Dubravko for doing the most difficult part! :D

Regards,
YoungJules

Reminders for myself... add option for the gpio pin in /etc/modules

Code: Select all

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

snd-bcm2835
wire
w1-gpio gpiopin=31
w1-therm
w1-gpio.ko is in
/lib/modules/3.10.19+/kernel/drivers/w1/masters/w1-gpio.ko
or
/lib/modules/3.6.11+/kernel/drivers/w1/masters/w1-gpio.ko
Attachments
w1-gpio.tar
The full source of the patched w1-gpio.c and w1-gpio.ko compiled for 3.10.19+
(20 KiB) Downloaded 411 times
Last edited by YoungJules on Tue Jan 21, 2014 11:26 am, edited 2 times in total.

User avatar
theoriginalxbrav
Posts: 41
Joined: Sun May 12, 2013 7:16 pm

Re: Source code to w1-therm?

Wed Dec 04, 2013 9:04 pm

Honestly, does anyone have a reason why we cannot offer this patch up to the official git? It seems like a no-brainer. I'd offer it up, but I only used GIT once to host some Python code. Any takers? This should be standardized.
Projects:
Raspberry Pi Weather Camera and weeWX: http://reboot.us.com/local-weather/
Daily Timelapses: https://www.youtube.com/playlist?list=PLi1er5S-VzV8d2t21zT0U_aBInl1_Ojkf

dpenezic
Posts: 99
Joined: Sat Dec 22, 2012 2:36 pm

Re: Source code to w1-therm?

Wed Dec 04, 2013 9:55 pm

theoriginalxbrav wrote:Honestly, does anyone have a reason why we cannot offer this patch up to the official git? It seems like a no-brainer. I'd offer it up, but I only used GIT once to host some Python code. Any takers? This should be standardized.
Hi,
I will try to do that tomorrow morning ... it spouse to be that i know how to work with Git :) ...
Regards,
Dubravko Penezic

dpenezic
Posts: 99
Joined: Sat Dec 22, 2012 2:36 pm

Re: Source code to w1-therm?

Thu Dec 05, 2013 8:05 am

Hi all,
after some struggling with github.com, Raspberry Pi documentation and some other things one simple patch is submitted to github.com repo :) (hopefully on right branch, I didnt found any info how Raspbian community organise repos).

Regards,
Dubravko Penezic

Return to “Automation, sensing and robotics”