akira215 / RaspberryPi-w1_module

Works kernel module w1_therm
2 stars 2 forks source link

Enhanced w1_therm Linux Kernel Module

Source files to build enhanced module for One-wire Dallas thermometer devices. It is an enhacement from the existing module from Evgeniy Polyakov zbr@ioremap.net, adding some features:

Building module

It should be build within the kernel tree :

drivers/w1/

and overwrite existing w1_therm.c file in slave/ subdir. The only parameter is the strong pullup which can take the following values:

On automatic detection, strong pullup will be applied on the bus line only if the device required it. If you have 2 devices on the line, one parasite powered and the second externally powered, strong pullup will be applied only when required (conversion operation and eeprom write) during communication with the parasite powered device. It will not be applied during communication with the externally powered device. If you trigger a bulk read on the line, strong pullup will be applied as soon as there is one parasite powered device on the line.

If you want to build only the w1_therm module, you first have to put the kernel source files in a directory and build it. Then you can put a Makefile in the slave/ subdir like this one

#Compiler path
PREFIX := /path/to/the/arm/compiler/arm-bcm2708-linux-gnueabi-
#Kernel Source Path
KERNEL_SRC := /path/to/the/kernel/files/linux-X.XX.XX-vXX/

# some warnings about bad code
ccflags-y := -Wall

# Only build the w1_therm.ko module
obj-m += w1_therm.o

all:
    make ARCH=arm CROSS_COMPILE=$(PREFIX) -C $(KERNEL_SRC) M=$(shell pwd) modules

clean:
    make ARCH=arm CROSS_COMPILE=$(PREFIX) -C $(KERNEL_SRC) M=$(shell pwd) clean

And then run make in the subdir. It will build a w1_therm.ko file. You can add a symlink on the module directory to this file to run it. For example you can do :

$ cd /lib/modules/X.XX.XX-vXX/kernel/drivers/w1/slaves
$ sudo mv w1_therm.ko w1_therm.ko.official
$ sudo ln -s /path/to/your/file/w1_therm.ko w1_therm.ko

This way you the module will run on the w1_therm module you have build. Be aware that you need to build the module on the same kernel that the one you will run on. You also need to rebuild the module each time you will change your kernel.

Sysfs entry

The sysfs interface of the module is :

Example of use

You don't need to directly the module. The wire module will load it directly as soon as it will detect compliant devices on the bus. To load the wire module, ou may need an overlay:

$ sudo dtoverlay w1-gpio

and to detect that the module is correctly loaded

$ lsmod
w1_therm               24576  0
w1_gpio                16384  0
wire                   40960  2 w1_gpio,w1_therm
.....

Herebelow some CLI usage example of the w1_therm module. You should either be logged as root either have added a udev rules in /etc/udev/rules.d directory to run write commands.

Checking online devices

$ cd /sys/bus/w1/devices
/sys/bus/w1/devices $ ls 
28-000002453b80  28-000002453d4f  28-03119779df08  w1_bus_master1

We can see here 3 devices (28 header indicate DS18B20) and the bus master

Reading informations on a device

/sys/bus/w1/devices $ cd 28-03119779df08
/sys/bus/w1/devices/28-03119779df08 $ cat ext_power 
1
/sys/bus/w1/devices/28-03119779df08 $ cat resolution 
10
/sys/bus/w1/devices/28-03119779df08 $ cat alarms 
5 20
/sys/bus/w1/devices/28-03119779df08 $ cat temperature 
21500
/sys/bus/w1/devices/28-03119779df08 $ cat w1_slave 
58 01 14 05 3f a5 a5 66 e7 : crc=e7 YES
58 01 14 05 3f a5 a5 66 e7 t=21500

Settings a device

/sys/bus/w1/devices/28-03119779df08 $ sudo su
[sudo] Password : 
/sys/bus/w1/devices/28-03119779df08# echo 12  > resolution 
/sys/bus/w1/devices/28-03119779df08# cat resolution 
12
/sys/bus/w1/devices/28-03119779df08# echo -5 55 > alarms
/sys/bus/w1/devices/28-03119779df08# cat alarms
-5 55
/sys/bus/w1/devices/28-03119779df08# echo save > eeprom

Bulk read example

/sys/bus/w1/devices/28-03119779df08# cd ../w1_bus_master1
/sys/bus/w1/devices/w1_bus_master1# echo trigger > therm_bulk_read 
/sys/bus/w1/devices/w1_bus_master1# cd ../28-03119779df08
/sys/bus/w1/devices/28-03119779df08# cat temperature
21625

Contributing

Please read post issues on this repository and/or submit pull requests.

Authors

License

Copyright (c) 2020 Akira Shimahara akira215corp@gmail.com

This program is free software; you can redistribute it and/or modify it under the therms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.