1-wire connection to DS18B20 thermometers via GPIO

Started by ilario, October 06, 2023, 04:47:23 PM

Previous topic - Next topic

ilario

I have a bunch of SNS-TMP-DS18B20 thermometers (bought here) which use a 1-wire interface, and I am going to try to connect them to a GPIO pin of a A64-OLinuXino-2Ge8G-IND.

Did anyone already managed to do that?

On this forum, this question has already been asked many times.

Some people managed to do it, but I didn't manage to reproduce their solutions as they seem obsolete (for old kernel versions), like this, this, this, this, this, this.

Some other people asked but seems they were not successful, like here, here, here, here, here, here, here, here, here, here.

Until now, I managed to adapt the instructions from https://linux-sunxi.org/1-Wire so that they don't give errors, but I didn't manage to have it working.

I created this devicetree overlay:

/dts-v1/;
/plugin/;

/ {
    compatible = "allwinner,sun50i-a64",
                      "olimex,a64-olinuxino";
    description = "Enable 1-Wire port";

    fragment@0 {
        target-path = "/";
        __overlay__ {
            onewire_device: onewire_device {
                compatible = "w1-gpio";
                gpios = <&pio 1 0 0>; /* 1: B of PB0; 0: 0 of PB0; 0: GPIO_ACTIVE_HIGH */
                pinctrl-names = "default";
                pinctrl-0 = <&my_w1_pin>;
            };
        };
    };

    fragment@1 {
        target = <&pio>;
         __overlay__ {
            my_w1_pin: my_w1_pin@0 {
                allwinner,pins = "PB1";
                allwinner,function = "gpio_in";
             /*    allwinner,drive = "0"; <SUN4I_PINCTRL_10_MA> */
            /*    allwinner,pull = "1";  <SUN4I_PINCTRL_PULL_UP> */
            };
        };
    };
};

As you can see, there are a few important differences from the example on the sunxi website:
  • If I indicate the same pin in both sections, as indicated in the sunxi website, I get this error in dmesg:
    sun50i-a64-pinctrl 1c20800.pinctrl: pin PB0 already requested by onewire_device; cannot claim for 1c20800.pinctrl:32
    sun50i-a64-pinctrl 1c20800.pinctrl: pin-32 (1c20800.pinctrl:32) status -22
    w1-gpio onewire_device: gpio_request (pin) failed
    So, without any idea for solving this, I indicated two different pins, PB0 and PB1. This is very likely a bad solution.
  • The pin cannot be PD2 as it is used by something else. So I used PB0 and PB1. These two pins would have been used for uart2 if I activated that overlay.
  • GPIO_ACTIVE_HIGH is undefined, so I found that 0 should do the same.
  • SUN4I_PINCTRL_10_MA and SUN4I_PINCTRL_PULL_UP are undefined, but I found here that the values should be respectively 0 and 1.
  • if I include these two last lines, I get an error in dmesg:
    w1-gpio: probe of onewire_device failed with error -22
  • Also, in the /boot/uEnv.txt file, I did not use the param_w1_pin and param_w1_pin_int_pullup kernel options as they did not seem to have any effect.

And compiled it with:
# dtc -I dts -O dtb sun50i-a64-w1-gpio.dts -o sun50i-a64-w1-gpio.dtbo
sun50i-a64-w1-gpio.dts:24.27-29.6: Warning (unit_address_vs_reg): /fragment@1/__overlay__/my_w1_pin@0: node has a unit name, but no reg property

No idea if the warning is important...

Then I included the path of the dtbo file in the spaces-separated list of fdtoverlays in /boot/uEnv.txt, powered off, connected the DS18B20 on the GPIO1 5V and ground and PB0 (if I connect it to PB1 nothing will appear in /sys/bus/w1/devices, except the w1_bus_master1 directory).


# cat /sys/kernel/debug/gpio
gpiochip1: GPIOs 0-255, parent: platform/1c20800.pinctrl, 1c20800.pinctrl:
  gpio-32  (                    |onewire_device      ) out lo
  gpio-166 (                    |cd                  ) in  lo ACTIVE LOW
  gpio-201 (                    |usb1-vbus           ) out hi
  gpio-230 (                    |usb0_vbus_det       ) in  lo IRQ
  gpio-233 (                    |usb0_id_det         ) in  hi IRQ

gpiochip0: GPIOs 352-383, parent: platform/1f02c00.pinctrl, 1f02c00.pinctrl:

gpiochip2: GPIOs 510-511, parent: platform/axp20x-gpio, axp20x-gpio, can
sleep:

The "out lo" here seems wrong, as, from what I understood, DS18B20 needs a pull up.

# cat /sys/kernel/debug/pinctrl/1c20800.pinctrl/pinmux-pins
Pinmux settings per pin
Format: pin (name): mux_owner|gpio_owner (strict) hog?
pin 32 (PB0): GPIO 1c20800.pinctrl:32
pin 33 (PB1): device onewire_device function gpio_in group PB1
pin 34 (PB2): UNCLAIMED
[...]

# dmesg
Driver for 1-wire Dallas network protocol.
[...]
gpio-32 (onewire_device): enforced open drain please flag it properly in DT/ACPI DSDT/board file
[...]
w1_master_driver w1_bus_master1: w1_search: max_slave_count 64 reached, will continue next search.
w1_master_driver w1_bus_master1: Attaching one wire slave 00.800000000000 crc 8c
w1_master_driver w1_bus_master1: Family 0 for 00.800000000000.8c is not registered.
w1_master_driver w1_bus_master1: Attaching one wire slave 00.400000000000 crc 46
w1_master_driver w1_bus_master1: Family 0 for 00.400000000000.46 is not registered.
w1_master_driver w1_bus_master1: Attaching one wire slave 00.c00000000000 crc ca
w1_master_driver w1_bus_master1: Family 0 for 00.c00000000000.ca is not registered.
w1_master_driver w1_bus_master1: Attaching one wire slave 00.200000000000 crc 23
w1_master_driver w1_bus_master1: Family 0 for 00.200000000000.23 is not registered.


As you can see, many 1-wire devices appear and disappear, even if I have only one DS18B20 directly connected to the GPIO1 connectors on the Olinuxino-A64 and even if I don't plug any thermometer to the PB0 pin.

Inside these folders, there is not the expected w1_slave file:

# ls /sys/bus/w1/devices/*
/sys/bus/w1/devices/00-200000000000:
driver  id  name  power  rw  subsystem  uevent

/sys/bus/w1/devices/00-c00000000000:
driver  id  name  power  rw  subsystem  uevent

/sys/bus/w1/devices/w1_bus_master1:
00-200000000000  subsystem           w1_master_max_slave_count
w1_master_remove       w1_master_timeout
00-c00000000000  uevent              w1_master_name
w1_master_search       w1_master_timeout_us
driver           w1_master_add       w1_master_pointer
w1_master_slave_count
power            w1_master_attempts  w1_master_pullup
w1_master_slaves


Do I need to add a hardware pull up? (a 4.7 kOhm resistance between the 5V and the data wire)

Can you check if my overlay is broken?

EDIT: the spurious attach-detach messages appear even with no thermometer connected

ilario

Dear all,
I posted the same question, with a refined, still not working, DTS file here:
https://groups.google.com/g/linux-sunxi/c/kxhjj3undlk

I will keep this thread updated if there is any news.

ilario

Just reporting another (interesting) failure:
I copied and pasted the sun50i-a64-w1-gpio.dts overlay from Armbian repository here:
https://github.com/armbian/sunxi-DT-overlays/tree/master/sun50i-a64

Just in case it was useful, I added w1_gpio to /etc/modules

And still I get the error message:

sun50i-a64-pinctrl 1c20800.pinctrl: pin PD4 already requested by onewire@0; cannot claim for 1c20800.pinctrl:100
sun50i-a64-pinctrl 1c20800.pinctrl: pin-100 (1c20800.pinctrl:100) status -22 w1-gpio onewire@0: gpio_request (pin) failed
w1-gpio: probe of onewire@0 failed with error -22

So now the question is: would this work if I start using Armbian?
What do they do differently?

ilario

I am trying installing w1-gpio-cl that looks like a promising alternative to w1-gpio with the DTS problems described above.

Has anyone tried to compile it?

I am facing a cascade of troubles, some caused by some missing packages, reported here: https://github.com/OLIMEX/OLINUXINO/issues/99

ilario

I got an answer on the linux-sunxi mailing list and now I have a thermometer working via 1-wire connected on the GPIO1 port of a A64-olinuxino :D

The full thread is here:
https://groups.google.com/g/linux-sunxi/c/kxhjj3undlk

there should be enough information for replicating the result.

If anyone want to do the same, feel free to ask, and I can add more detailed instructions.

LubOlimex

Yohoo, glad you got it going. We are all interested in details man man, if you have the time to describe the steps you took here in the forum too that would be awesome!
Technical support and documentation manager at Olimex

ilario

Before writing here a proper guide, I was waiting to actually try with more than one thermometer connected, that should happen soon :)

In the meanwhile, you can find here the overlay I am using now:
https://github.com/OLIMEX/olinuxino-overlays/pull/3