Pi4J / pi4j-v2

Pi4J Version 2.0
Apache License 2.0
273 stars 57 forks source link

Extend Digital Input and Output implementations in LinuxFS #307

Open FDelporte opened 11 months ago

FDelporte commented 11 months ago

With version 2.4.0 of Pi4J, the LinuxFS plugin got extended to provide Digital Input and Output implementations. This is a first step, but not the "full package".

For instance, handling of PullUpDown Resistors is not implemented yet.

alex9849 commented 10 months ago

Hello Frank, :)

I just tested the LinuxFS implementations. The do work on a raspberry pi 3, but I get the following error on a raspberry pi 5:

java.io.IOException: Invalid argument
        at java.base/sun.nio.ch.FileDispatcherImpl.write0(Native Method) ~[na:na]
        at java.base/sun.nio.ch.FileDispatcherImpl.write(FileDispatcherImpl.java:62) ~[na:na]
        at java.base/sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:132) ~[na:na]
        at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:97) ~[na:na]
        at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:67) ~[na:na]
        at java.base/sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:288) ~[na:na]
        at java.base/java.nio.channels.Channels.writeFullyImpl(Channels.java:74) ~[na:na]
        at java.base/java.nio.channels.Channels.writeFully(Channels.java:96) ~[na:na]
        at java.base/java.nio.channels.Channels$1.write(Channels.java:171) ~[na:na]
        at java.base/java.nio.file.Files.write(Files.java:3517) ~[na:na]
        at java.base/java.nio.file.Files.writeString(Files.java:3734) ~[na:na]
        at java.base/java.nio.file.Files.writeString(Files.java:3674) ~[na:na]
        at com.pi4j.plugin.linuxfs.internal.LinuxGpio.export(LinuxGpio.java:94) ~[pi4j-plugin-linuxfs-2.4.0.jar!/:na]
        at com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutput.initialize(LinuxFsDigitalOutput.java:71) ~[pi4j-plugin-linuxfs-2.4.0.jar!/:na]
        at com.pi4j.plugin.linuxfs.provider.gpio.digital.LinuxFsDigitalOutput.initialize(LinuxFsDigitalOutput.java:46) ~[pi4j-plugin-linuxfs-2.4.0.jar!/:na]
        at com.pi4j.provider.impl.ProviderProxyHandler.invoke(ProviderProxyHandler.java:103) ~[pi4j-core-2.4.0.jar!/:na]
        at jdk.proxy2/jdk.proxy2.$Proxy102.create(Unknown Source) ~[na:na]
        at com.pi4j.context.Context.create(Context.java:325) ~[pi4j-core-2.4.0.jar!/:na]
        at com.pi4j.internal.IOCreator.create(IOCreator.java:58) ~[pi4j-core-2.4.0.jar!/:na]
        at com.pi4j.internal.IOCreator.create(IOCreator.java:96) ~[pi4j-core-2.4.0.jar!/:na]
        at net.alex9849.cocktailpi.utils.PinUtils.getBoardOutputPin(PinUtils.java:41) ~[classes!/:1.0-SNAPSHOT]

The file /sys/class/gpio does exist. I initialized the context using:

                ContextBuilder ctxBuilder = Pi4J.newContextBuilder();
                ctxBuilder.add(LinuxFsI2CProvider.newInstance());
                ctxBuilder.add(LinuxFsDigitalOutputProvider.newInstance());
                ctxBuilder.add(LinuxFsDigitalInputProvider.newInstance());

... and created the DigitalOutput-instances using:

        DigitalOutputConfig config = DigitalOutput
                .newConfigBuilder(pi4J)
                .address(address)
                .shutdown(DigitalState.HIGH)
                .initial(DigitalState.HIGH)
                .build();
        return new Pi4JOutputPin(outputPinMap.get(address));

I would like to get pi4J running on a raspberry pi 5. Sadly pigpio got incompatible.

eitch commented 10 months ago

Hi @alex9849

If you don't have a /sys/class/gpio file, then this means that a kernel module is not loaded. This would be something to do with the Raspberry Pi OS. Further the LinuxFS plugin doesn't use pigpio, just files exported by the kernel.

Sadly i don't yet have a Rasberry Pi 5, so i can't test this out. Maybe you can check the Raspberry Pi forums for the kernel which needs to be loaded? Are you using the latest OS? Maybe you need to enable it in the raspi-config tool?

FDelporte commented 10 months ago

Hi, I have a 5 but only did a very quick test with the minimal example. It looks like PiGpio doesn't work either - as expected based on some messages on tickets in the project, e.g. https://github.com/joan2937/pigpio/issues/589

I will try more later, but agenda is still blocked the next days.

alex9849 commented 10 months ago

Yes. pigpio got incompatible sadly. But I found a few things out.

My kernel is okay. I do have a /sys/class/gpio file. That's not the problem. I just wanted to point out that the error happens even tough that file exists. I tested the DigitalOutput integration with LinuxFS on a raspberry pi 3 and a raspberry pi 5. It works on the Pi3, but not on the Pi5.

The LinuxFS I2C integration works on both platforms. Maybe the format for the gpio files changed? Is the format documented somewhere?

I digged a bit more into it. I couldn't really find anything regarding /sys/class/gpio, but I looks like that interface got deprecated: https://www.kernel.org/doc/Documentation/ABI/obsolete/sysfs-gpio

An alternative seem to be devices at /dev/gpiochip and /dev/gpiomem https://docs.kernel.org/driver-api/gpio/using-gpio.html that should be controlled using https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/

I also found an example: https://blog.lxsang.me/post/id/33