Jump to content

Use GPIO on C2 with Mainline Kernel


nik-ii

Recommended Posts

Hi,

 

I´d like to use the GPIO PINs on my Odriod C2 with the mainline kernel 4.18.

 

I tried to install the User Space IO, which seems to be the right tool to help me with the task. Unfortunately I had no luck trying to install it on my own. Hopefully we can install it together and get the GPIOs to do their job.

 

It says "User space devices are exposed through /dev or /sys. Verify the device is showing up prior to trying demo programs." 

 

That is what is shown in this directory's: Picture, are devices installed properly or do I have to configure the C2 to start with User Space IO? 

 

That happens if I try to run the example code to access the GPIO or the SPI interface: Picture2

 

Can someone help me with that?

 

Thank you!

 

Nik

Link to comment
Share on other sites

@nik-ii I actually have a C2 and I think I just loaded mainline on it a few weeks ago.  I'll check tonight, but if your SBC is using mainline then it should work.  With armbian-config you need to enable SPI, I2C, etc. I'll reply back here later with C2 specific info.

 

Note from your pic that GPIO is showing up.

Link to comment
Share on other sites

@nik-ii OK, I had some time to play around with my C2 which already had Armbian Stretch installed:

 

Linux hostname 4.18.8-odroidc2 #264 SMP PREEMPT Wed Sep 19 12:35:18 CEST 2018 aarch64 GNU/Linux

 

armbian-config has no Hardware menu, but it looks like GPIO and I2C are already enabled:

 

ls /dev/gpio* /dev/i2c* /dev/spi*
ls: cannot access '/dev/spi*': No such file or directory
/dev/gpiochip0  /dev/gpiochip1  /dev/i2c-0  /dev/i2c-1

 

If you go to the Odroid C2 wiki you can follow the steps to enable SPI:

 

sudo su -

 

modprobe spidev

modprobe spi_gpio
modprobe: FATAL: Module spi_gpio not found in directory /lib/modules/4.18.8-odroidc2

modprobe spi_bitbang

 

lsmod | grep spi
spi_bitbang            16384  0
spidev                 20480  0

 

ls /dev/spi*
ls: cannot access '/dev/spi*': No such file or directory

 

I'm not sure what's going on here because this works with the HardKernel OS images. I'm actually using my C2 as a Jenkins CI server, but I did play around and made sure gpio, i2c and spi worked. I believe that was a 3.x kernel though :(@Igor may be able to point you in a better direction to get SPI cranking.

 

There are other drivers, but I'm not sure which one to use:

 

ls /lib/modules/4.18.8-odroidc2/kernel/drivers/spi
spi-bitbang.ko    spi-butterfly.ko  spidev.ko  spi-lm70llp.ko  spi-meson-spifc.ko

 

 

Link to comment
Share on other sites

@sgjava the mainline device tree doesn't have the spi nodes the 3.14 had:  https://github.com/hardkernel/linux/blob/odroidc2-3.14.y/arch/arm64/boot/dts/meson64_odroidc2.dts#L802

 

That looks like generic GPIO SPI, and I haven't found, in the last 20 minutes or so, where the spicc and spifc pops out of the processor GPIO wise.  It does not appear to be GPIOX, so I think on the C2 the setup has to be a software SPI.  NanoPi K2 appears to be in the same position, just plain not having SPI.  I will investigate adding it to K2, then someone with a C2 can try it.

Link to comment
Share on other sites

@sgjava Okay, so GPIO and I2C are available. 

Does your User Space IO installation need the SPI device to be enabled? Or should it be possible to just use the GPIO and I2C commands? That would be enough for my use case at the moment.

 

Thanks for your support! 

 

Link to comment
Share on other sites

@TonyMac32 I believe the C2 is software SPI.

 

@nik-ii See Tony's reply above, but it looks like I2C and GPIO are enabled. UserSpace IO components can be used individually. In other words I can use just GPIO if it's enabled. You can try a simple flashing LED example.

Link to comment
Share on other sites

My first crude attempt on the K2 didn't work, but again, different gpio's involved and the bindings have changed somewhat since kernel 3.14.
I can build a C2 kernel and we can see if it works for you. I also see a frequency limit of 500 khz, so keep that in mind. It might not be a "real" limit, some testing would be useful to determine.

Sent from my Pixel using Tapatalk

Link to comment
Share on other sites

@sgjavaKnowing the GPIO should work I tried to get the LED flashing. 

niklas@odroidc2:~/userspaceio/libgpiod/python/src$ python ledtest.py --chip 0 --line 203
Traceback (most recent call last):
  File "ledtest.py", line 17, in <module>
    import sys, time, gpiod
ImportError: No module named 'gpiod'

So I think the User Space IO installation is suspicious. I found some errors in the libgpiod Java Install Logs:

 

Spoiler

10-06-2018 16:08:28 Generate JNA library for libgpiod
10-06-2018 16:08:28 Compiling demo code
src/com/codeferm/demo/ButtonCallback.java:7: error: package com.sun.jna does not exist
import com.sun.jna.NativeLong;
                  ^
src/com/codeferm/demo/ButtonCallback.java:8: error: package com.sun.jna does not exist
import com.sun.jna.Pointer;
                  ^
src/com/codeferm/demo/ButtonThread.java:10: error: package com.sun.jna does not exist
import com.sun.jna.NativeLong;
                  ^
src/com/codeferm/demo/Hcsr501.java:8: error: package com.sun.jna does not exist
import com.sun.jna.Pointer;
                  ^
src/com/codeferm/demo/ButtonCallback.java:43: error: cannot find symbol
                Pointer data) -> {
                ^
  symbol:   class Pointer
  location: class ButtonCallback
src/com/codeferm/demo/ButtonCallback.java:42: error: cannot access Callback
        final GpiodLibrary.gpiod_ctxless_event_handle_cb func = (int evtype, int offset, timespec timeSpec,
                                                                ^
  class file for com.sun.jna.Callback not found
src/com/codeferm/demo/ButtonCallback.java:64: error: cannot find symbol
                new timespec(new NativeLong(10), new NativeLong(0)), null, func, null) != 0) {
                                 ^
  symbol:   class NativeLong
  location: class ButtonCallback
src/com/codeferm/demo/ButtonCallback.java:64: error: cannot find symbol
                new timespec(new NativeLong(10), new NativeLong(0)), null, func, null) != 0) {
                                                     ^
  symbol:   class NativeLong
  location: class ButtonCallback
src/com/codeferm/demo/ButtonCallback.java:63: error: cannot access Library
        if (lib.gpiod_ctxless_event_loop(chipNum, lineNum, (byte) 0, consumer,
               ^
  class file for com.sun.jna.Library not found
src/com/codeferm/demo/ButtonThread.java:62: error: cannot access Structure
                                Instant.ofEpochMilli(event.getTs().tv_sec.longValue() * 1000), ZoneId.systemDefault());
                                                          ^
  class file for com.sun.jna.Structure not found
src/com/codeferm/demo/ButtonThread.java:55: error: cannot find symbol
                rc = lib.gpiod_line_event_wait(line, new timespec(new NativeLong(timeoutSecs), new NativeLong(0)));
                                                                      ^
  symbol:   class NativeLong
  location: class ButtonThread
src/com/codeferm/demo/ButtonThread.java:55: error: cannot find symbol
                rc = lib.gpiod_line_event_wait(line, new timespec(new NativeLong(timeoutSecs), new NativeLong(0)));
                                                                                                   ^
  symbol:   class NativeLong
  location: class ButtonThread
src/com/codeferm/demo/ButtonWait.java:45: error: cannot find symbol
                    final int rc = lib.gpiod_line_event_wait(line, new timespec(new NativeLong(5), new NativeLong(0)));
                                                                                    ^
  symbol:   class NativeLong
  location: class ButtonWait
src/com/codeferm/demo/ButtonWait.java:45: error: cannot find symbol
                    final int rc = lib.gpiod_line_event_wait(line, new timespec(new NativeLong(5), new NativeLong(0)));
                                                                                                       ^
  symbol:   class NativeLong
  location: class ButtonWait
src/com/codeferm/demo/ButtonWait.java:53: error: cannot access Structure$ByValue
                        System.out.println(event.event_type);
                                                ^
  class file for com.sun.jna.Structure$ByValue not found
src/com/codeferm/demo/Hcsr501.java:42: error: cannot find symbol
                Pointer data) -> {
                ^
  symbol:   class Pointer
  location: class Hcsr501


The system is out of resources.
Consult the following stack trace for details.
java.lang.StackOverflowError
    at com.sun.tools.javac.comp.Resolve.isAccessible(Resolve.java:299)

...

 

./java-bindings.sh: 67: ./java-bindings.sh: /usr/lib/jvm/jdk1.8.0/bin/jar: not found
10-06-2018 16:08:32 Done
 

 

Any idea were I made a mistake?

 

 

Link to comment
Share on other sites

@TonyMac32 The following works on the C2:

gpiodetect
gpiochip0 [aobus-banks] (15 lines)
gpiochip1 [periphs-banks] (119 lines)


sudo i2cdetect -l
i2c-1    i2c           DesignWare HDMI                     I2C adapter
i2c-0    i2c           Meson I2C adapter                   I2C adapter


./spidev_test -D /dev/spidev0.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

I need to figure out the MOSI and MISO pins since it's software, but it looks like all the major subsystems are cranking. It doesn't appear the C2 has pins for SPI? https://wiki.odroid.com/odroid-c2/hardware/expansion_connectors

 

I did get SPI working on C1 here https://forum.odroid.com/viewtopic.php?f=116&amp;t=26674

Link to comment
Share on other sites

18 minutes ago, sgjava said:

It doesn't appear the C2 has pins for SPI?

I pulled them out of the source code for the hardkernel 3.14.  they align with the spi pins on RPI afaik.

Link to comment
Share on other sites

@TonyMac32 OK, it uses pins 19 and 20 like Pi and C1. I used a loopback wire and ran a simple Python script:

import spidev

input = [1 ,2 ,3, 4, 5]
print("input: %s" % input)
spi = spidev.SpiDev()
spi.open(0,0)
output = spi.xfer2(input)
print("output: %s" % output)

python3 spitest.py
input: [1, 2, 3, 4, 5]
output: [1, 2, 3, 4, 5]

So I'm confident gpio, i2c and spi are working. If your changes can be committed for the C2 Armbian build that would be most excellent.

 

@nik-ii For UserSpace IO issues please use my github issues page otherwise it looks like everything works. I tested UserSpace IO with C2. Here's the SPI loopback in Python:

 

python3 spiloopback.py --device /dev/spidev0.0 --maxSpeed 500000

255 128

 

and Java:

 

java -Djna.nosys=true -Djava.library.path=/usr/local/lib -cp ../../jnaerator/jna-4.5.2.jar:../../jnaerator/jnaerator-runtime.jar:libperiphery.jar:demo.jar com.codeferm.demo.SpiLoopback /dev/spidev0.0 500000
FF, 80

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines