edison-fw / meta-intel-edison

Here is the meta-intel-edison that builds, tries to stay up to date. Master is based on Yocto Poky Gatesgarth LTS 5.10.yy vanilla kernels. It builds a 32bit kernel (Gatesgarth branch 64bit) with ACPI enabled and corresponding rootfs. Telegram group: https://t.me/IntelEdison Web-site:
https://edison-fw.github.io/meta-intel-edison/
MIT License
60 stars 37 forks source link

MRAA usage issues #95

Closed shawnhsoia97 closed 4 years ago

shawnhsoia97 commented 4 years ago

Hi, I try to control gpio and spi now,so I follow examples/c/spi.c and gpio.c. But when i run it shows MRAA: Invalid Handle. dmesg shows: edison kernel: export_store: invalid GPIO 214

Then i found https://github.com/edison-fw/meta-intel-edison/issues/50#issue-369939660 This description:

This indicates MRAA is using the wrong pin numbering - and in fact it should be using the pin names. Doing gpioinfo shows TRI_STATE_ALL is on gpiochip1 line 14. You find gpiochip1 under /sys/class/gpio/gpiochip496/device/, so the base address has become 496 and the gpio is 510.

We need to modify intel_edison_fab_c.c to use either libgpiod or the new gpio chardev interface directly to get mraa working.

But I not use Edison Arduino board,I use to my own circuit board,so I don't have TRI_STATE_ALL on gpiochip1.

root@bovia-deb:/bovia/flashmcu# gpiofind TRI_STATE_ALL
root@bovia-deb:/bovia/flashmcu#
 root@bovia-deb:/# gpioinfo
gpiochip0 - 192 lines:
        line   0:      unnamed       unused   input  active-high
        line   1:      unnamed       unused   input  active-high
        line   2:      unnamed       unused  output  active-high
        line   3:      unnamed       unused   input  active-high
        line   4:      unnamed       unused  output  active-high
        line   5:      unnamed       unused  output  active-high
        line   6:      unnamed       unused  output  active-high
        line   7:      unnamed       unused   input  active-high
        line   8:      unnamed       unused   input  active-high
        line   9:      unnamed       unused  output  active-high
        line  10:      unnamed       unused  output  active-high
        line  11:      unnamed       unused  output  active-high
        line  12:      unnamed       unused  output  active-high
        line  13:      unnamed       unused  output  active-high
        line  14:      unnamed       unused   input  active-high
        line  15:      unnamed       unused   input  active-high
        line  16:      unnamed       unused   input  active-high
        line  17:      unnamed       unused   input  active-high
        line  18:      unnamed       unused   input  active-high
        line  19:      unnamed       unused   input  active-high
        line  20:      unnamed       unused   input  active-high
        line  21:      unnamed       unused   input  active-high
        line  22:      unnamed       unused   input  active-high
        line  23:      unnamed       unused   input  active-high
        line  24:      unnamed       unused   input  active-high
        line  25:      unnamed       unused   input  active-high
        line  26:      unnamed       unused   input  active-high
        line  27:      unnamed       unused   input  active-high
        line  28:      unnamed       unused   input  active-high
        line  29:      unnamed       unused   input  active-high
        line  30:      unnamed       unused   input  active-high
        line  31:      unnamed       unused   input  active-high
        line  32:      unnamed       unused   input  active-high
        line  33:      unnamed       unused   input  active-high
        line  34:      unnamed       unused   input  active-high
        line  35:      unnamed       unused   input  active-high
        line  36:      unnamed       unused   input  active-high
        line  37:      unnamed       unused   input  active-high
        line  38:      unnamed       unused   input  active-high
        line  39:      unnamed       unused   input  active-high
        line  40:      unnamed  "heartbeat"  output  active-high [used]
        line  41:      unnamed       unused   input  active-high
        line  42:      unnamed       unused   input  active-high
        line  43:      unnamed       unused   input  active-high
        line  44:      unnamed       unused   input  active-high
        line  45:      unnamed       unused   input  active-high
        line  46:      unnamed       unused   input  active-high
        line  47:      unnamed       unused   input  active-high
        line  48:      unnamed       unused   input  active-high
        line  49:      unnamed       unused   input  active-high
        line  50:      unnamed       unused  output  active-high
        line  51:      unnamed       unused   input  active-high
        line  52:      unnamed       unused   input  active-high
        line  53:      unnamed       unused   input  active-high
        line  54:      unnamed       unused  output  active-high
        line  55:      unnamed       unused   input  active-high
        line  56:      unnamed       unused   input  active-high
        line  57:      unnamed       unused   input  active-high
        line  58:      unnamed       unused   input  active-high
        line  59:      unnamed       unused   input  active-high
        line  60:      unnamed       unused   input  active-high
        line  61:      unnamed       unused   input  active-high
        line  62:      unnamed       unused   input  active-high
        line  63:      unnamed       unused   input  active-high
        line  64:      unnamed       unused   input  active-high
        line  65:      unnamed       unused   input  active-high
        line  66:      unnamed       unused   input  active-high
        line  67:      unnamed       unused   input  active-high
        line  68:      unnamed       unused   input  active-high
        line  69:      unnamed       unused   input  active-high
        line  70:      unnamed       unused   input  active-high
        line  71:      unnamed   "shutdown"  output  active-high [used]
        line  72:      unnamed       unused   input  active-high
        line  73:      unnamed       unused   input  active-high
        line  74:      unnamed       unused   input  active-high
        line  75:      unnamed       unused   input  active-high
        line  76:      unnamed       unused   input  active-high
        line  77:      unnamed      "sd_cd"   input  active-high [used]
        line  78:      unnamed       unused   input  active-high
        line  79:      unnamed       unused   input  active-high
        line  80:      unnamed       unused   input  active-high
        line  81:      unnamed       unused   input  active-high
        line  82:      unnamed       unused   input  active-high
        line  83:      unnamed       unused   input  active-high
        line  84:      unnamed       unused   input  active-high
        line  85:      unnamed       unused   input  active-high
        line  86:      unnamed       unused   input  active-high
        line  87:      unnamed       unused   input  active-high
        line  88:      unnamed       unused   input  active-high
        line  89:      unnamed       unused   input  active-high
        line  90:      unnamed       unused   input  active-high
        line  91:      unnamed       unused   input  active-high
        line  92:      unnamed       unused   input  active-high
        line  93:      unnamed       unused   input  active-high
        line  94:      unnamed       unused   input  active-high
        line  95:      unnamed       unused   input  active-high
        line  96:      unnamed "ACPI:OpRegion" output active-high [used]
        line  97:      unnamed       unused   input  active-high
        line  98:      unnamed       unused   input  active-high
        line  99:      unnamed       unused   input  active-high
        line 100:      unnamed       unused   input  active-high
        line 101:      unnamed       unused   input  active-high
        line 102:      unnamed       unused   input  active-high
        line 103:      unnamed       unused   input  active-high
        line 104:      unnamed       unused   input  active-high
        line 105:      unnamed       unused   input  active-high
        line 106:      unnamed       unused   input  active-high
        line 107:      unnamed       unused   input  active-high
        line 108:      unnamed       unused   input  active-high
        line 109:      unnamed       unused   input  active-high
        line 110:      unnamed         "cs"  output  active-high [used]
        line 111:      unnamed         "cs"  output  active-high [used]
        line 112:      unnamed         "cs"  output  active-high [used]
        line 113:      unnamed         "cs"  output  active-high [used]
        line 114:      unnamed       unused   input  active-high
        line 115:      unnamed       unused   input  active-high
        line 116:      unnamed       unused   input  active-high
        line 117:      unnamed       unused   input  active-high
        line 118:      unnamed       unused   input  active-high
        line 119:      unnamed       unused   input  active-high
        line 120:      unnamed       unused   input  active-high
        line 121:      unnamed       unused   input  active-high
        line 122:      unnamed       unused   input  active-high
        line 123:      unnamed       unused   input  active-high
        line 124:      unnamed       unused   input  active-high
        line 125:      unnamed       unused   input  active-high
        line 126:      unnamed       unused   input  active-high
        line 127:      unnamed       unused   input  active-high
        line 128:      unnamed       unused   input  active-high
        line 129:      unnamed       unused   input  active-high
        line 130:      unnamed       unused   input  active-high
        line 131:      unnamed       unused   input  active-high
        line 132:      unnamed       unused   input  active-high
        line 133:      unnamed       unused   input  active-high
        line 134:      unnamed       unused   input  active-high
        line 135:      unnamed       unused   input  active-high
        line 136:      unnamed       unused   input  active-high
        line 137:      unnamed       unused   input  active-high
        line 138:      unnamed       unused   input  active-high
        line 139:      unnamed       unused   input  active-high
        line 140:      unnamed       unused   input  active-high
        line 141:      unnamed       unused   input  active-high
        line 142:      unnamed       unused   input  active-high
        line 143:      unnamed       unused   input  active-high
        line 144:      unnamed       unused   input  active-high
        line 145:      unnamed       unused   input  active-high
        line 146:      unnamed       unused   input  active-high
        line 147:      unnamed       unused   input  active-high
        line 148:      unnamed       unused   input  active-high
        line 149:      unnamed       unused   input  active-high
        line 150:      unnamed       unused   input  active-high
        line 151:      unnamed       unused   input  active-high
        line 152:      unnamed       unused   input  active-high
        line 153:      unnamed       unused   input  active-high
        line 154:      unnamed       unused   input  active-high
        line 155:      unnamed       unused   input  active-high
        line 156:      unnamed       unused   input  active-high
        line 157:      unnamed       unused   input  active-high
        line 158:      unnamed       unused   input  active-high
        line 159:      unnamed       unused   input  active-high
        line 160:      unnamed       unused   input  active-high
        line 161:      unnamed       unused   input  active-high
        line 162:      unnamed       unused   input  active-high
        line 163:      unnamed       unused   input  active-high
        line 164:      unnamed       unused   input  active-high
        line 165:      unnamed       unused  output  active-high
        line 166:      unnamed       unused   input  active-high
        line 167:      unnamed       unused   input  active-high
        line 168:      unnamed       unused   input  active-high
        line 169:      unnamed       unused   input  active-high
        line 170:      unnamed       unused   input  active-high
        line 171:      unnamed       unused   input  active-high
        line 172:      unnamed       unused   input  active-high
        line 173:      unnamed       unused  output  active-high
        line 174:      unnamed       unused   input  active-high
        line 175:      unnamed       unused  output  active-high
        line 176:      unnamed       unused  output  active-high
        line 177:      unnamed       unused  output  active-high
        line 178:      unnamed       unused   input  active-high
        line 179:      unnamed       unused  output  active-high
        line 180:      unnamed       unused   input  active-high
        line 181:      unnamed       unused  output  active-high
        line 182:      unnamed       unused  output  active-high
        line 183:      unnamed       unused   input  active-high
        line 184:      unnamed "device-wakeup" output active-high [used]
        line 185:      unnamed "host-wakeup" input active-high [used]
        line 186:      unnamed       unused   input  active-high
        line 187:      unnamed       unused   input  active-high
        line 188:      unnamed       unused   input  active-high
        line 189:      unnamed       unused  output  active-high
        line 190:      unnamed       unused  output  active-high
        line 191:      unnamed       unused   input  active-high
gpiochip1 - 4 lines:
        line   0:      unnamed       unused   input  active-high
        line   1:      unnamed       unused   input  active-high
        line   2:      unnamed       unused   input  active-high
        line   3:      unnamed       unused   input  active-high

Do I have to modify my own circuit board?

Besides,I don't know how to modify intel_edison_fab_c.c,can you help me? Thanks.

Also do you know why insmod i2c-dev.ko on boot?

htot commented 4 years ago

But I not use Edison Arduino board,I use to my own circuit board,so I don't have TRI_STATE_ALL on gpiochip1. Do I have to modify my own circuit board?

No. The description of your board should by in a acpi table. There is one for the Edison-Arduino. For the Edison breakout board none is needed, the tables are in U-Boot already. Maybe you board needs none, maybe it does. Examples here.

Besides,I don't know how to modify intel_edison_fab_c.c,can you help me? Thanks.

Short answer: with an editor :-) From that create a patch, then let the recipe apply that patch. Longer answer: intel_edison_fab_c.c defines pins and their properties. Now we have libgpio, so we need to map pin numbers to pin names and manipulate using libgpio. I think the mraa repo already has examples for other boards doing this. Also I have a patch here that ports the sketch-reset recipe to use libgpiod. This is an example of how to modify a recipe to apply a patch, and also how to port from using sysfs to using libgpiod.

Also do you know why insmod i2c-dev.ko on boot?

I don't know, on my machine that doesn't happen:

root@edison:~# lsmod | grep i2c
root@edison:~# modprobe -i i2c-dev
root@edison:~# lsmod | grep i2c
i2c_dev                20480  0
shawnhsoia97 commented 4 years ago

Sorry,I still don't understand,I just want to control the input and output of gpio,E.g image I want to control GP183 output low,so i use to/usr/share/mraa/examples/c/gpio.c,

/* gpio declaration */
#define GPIO_PIN_1 183 

status = mraa_gpio_write(gpio_1, 0);
        if (status != MRAA_SUCCESS) {
            goto err_exit;
        }

But run gpio.o show Failed to initialize GPIO 183,dmesg show export_store: invalid GPIO 214, GPIO 214 is "TRI_STATE_ALL",but like I said I don't have "TRI_STATE_ALL",because I am not using Edison Arduino board.arduino.asli should be defined on IO0~IO19 of Edison Arduino board? but I don't have this,I just want to control on gpio pin of edison. So do I still need modify intel_edison_fab_c.c?

Also I have a patch here that ports the sketch-reset recipe to use libgpiod. This is an example of how to modify a recipe to apply a patch, and also how to port from using sysfs to using libgpiod.

Do you mean using libgpio not MRAA?then I still need modify intel_edison_fab_c.c? Also can I still use mraa spi without modifying anything?I have /dev/spidev5.1.

Also i try use to branch "zeus",but "make setup" is fail. show :

error: patch failed: meta/recipes-bsp/u-boot/u-boot.inc:85
error: meta/recipes-bsp/u-boot/u-boot.inc: patch does not apply
Makefile:25: recipe for target 'setup' failed
make: *** [setup] Error 1
shawnhsoia97 commented 4 years ago

Also I not found gpiod.h

gpio183.c:1:10: fatal error: gpiod.h: No such file or directory
 #include <gpiod.h>
htot commented 4 years ago

I want to control GP183 output low,so i use to/usr/share/mraa/examples/c/gpio.c, MRAA needs to be fixed, or you need to use libgpiod instead. GPIO 214 is "TRI_STATE_ALL",but like I said I don't have "TRI_STATE_ALL",because I am not using Edison Arduino board.arduino.asli should be defined on IO0~IO19 of Edison Arduino board? but I don't have this,I just want to control on gpio pin of edison.

MRAA is not working and needs to be fixed

So do I still need modify intel_edison_fab_c.c?

If you want to use MRAA, yes.

Also I have a patch here that ports the sketch-reset recipe to use libgpiod. This is an example of how to modify a recipe to apply a patch, and also how to port from using sysfs to using libgpiod.

Do you mean using libgpio not MRAA?then I still need modify intel_edison_fab_c.c?

You can use libgpiod directly, but if you want to use MRAA (to keep existing code working), one way would be to change intel_edison_fab_c.c to translate pin numbers into the pins chip/line, and use libgpiod inside to access that.

The way to access pins (at the low level) has changed completely, that does not depend on the arduino board. See otherwise the changes in the blink-led code.

Also can I still use mraa spi without modifying anything?I have /dev/spidev5.1.

That I haven't tried. MRAA serial works, because I got a patch upstreamed. I2C too, https://htot.github.io/meta-intel-edison/4.5-mraa-upm.html

Also i try use to branch "zeus",but "make setup" is fail.

Yes, sorry about that. I am testing the sketch_reset_v2 branch, it builds without errors.

shawnhsoia97 commented 4 years ago

Thank you.So if i want to modify intel_edison_fab_c.c ,how do i know the right pin now?E.g

static mraa_intel_edison_pinmodes_t pinmodes[MRAA_INTEL_EDISON_PINCOUNT];
static unsigned int outputen[] = { 248, 249, 250, 251, 252, 253, 254, 255, 256, 257,
                                   258, 259, 260, 261, 232, 233, 234, 235, 236, 237 };
static mraa_gpio_context agpioOutputen[sizeof(outputen) / sizeof(outputen[0])];

static unsigned int pullup_map[] = { 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
                                     226, 227, 228, 229, 208, 209, 210, 211, 212, 213 };

Originally should follow image How should I find the right pin now?

root@edison:/home/testgpio# ls /sys/class/gpio/
export       gpiochip0/   gpiochip508/ unexport

I just know GPX number now.follow Edison Pinout(linux GPIO pin),but i don't know current Linux pin of gpio pin mux and output enable liinux pin and pullup enable linux pin. So how do find that?Thank

Yes, sorry about that. I am testing the sketch_reset_v2 branch, it builds without errors.

Many thanks for your hard work,I wait for your good news.^_^

Also I not include ,I install libgpiod,but that still not work.

htot commented 4 years ago

You might use the blink-led python as an example. Here we replaced:

# Init GPIO mux for LED control
subprocess.call("""
echo 40 >/sys/class/gpio/export
echo 214 >/sys/class/gpio/export
echo 243 >/sys/class/gpio/export
echo 261 >/sys/class/gpio/export
echo high >/sys/class/gpio/gpio214/direction
echo mode0 > /sys/kernel/debug/gpio_debug/gpio40/current_pinmux
echo low >/sys/class/gpio/gpio243/direction
echo high >/sys/class/gpio/gpio261/direction
echo low >/sys/class/gpio/gpio214/direction""", shell=True)

by

set_line("TRI_STATE_ALL", 0)
set_line("SPI_CLK_SEL", 0)
set_line("MUX18_DIR", 1)
set_line("TRI_STATE_ALL", 0)
led = gpiod.Chip("gpiochip0").get_line(40)
led.request(consumer=led.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
if led.get_value() == 0:
    initial_led_state = "low"
else:
    initial_led_state = "high"

and

def write_led(value):
    with open("/sys/class/gpio/gpio40/direction","w") as lf:
        lf.write(value)

by

def write_led(value):
    if value == "high":
        level = 1
    else:
        level = 0
    led.set_value(level)

So the problem is not really with gpiochip0, but with the others as the pins received names from the signal names in the schematic.

shawnhsoia97 commented 4 years ago

Hi @htot ,I test blink-led,but not found gpiod.

root@edison:/usr/bin# python blink-led
Traceback (most recent call last):
  File "blink-led", line 35, in <module>
    import gpiod
ImportError: No module named gpiod

I have installed libgpiod,but this not work. How to install gpiod? I try to pip install gpiod and libgpiod ,but failed.

root@edison:/usr/bin# pip3 install gpiod
ERROR: Could not find a version that satisfies the requirement gpiod (from versions: none)
ERROR: No matching distribution found for gpiod
root@edison:/usr/bin# pip3 install libgpiod
ERROR: Could not find a version that satisfies the requirement libgpiod (from versions: none)
ERROR: No matching distribution found for libgpiod
htot commented 4 years ago

It is already installed. Don't use pip.

I don't understand what image you have.

shawnhsoia97 commented 4 years ago

Sorry I used python before,I should use to python3.

but this show the error now.

root@edison:/usr/bin# python3 blink-led
Traceback (most recent call last):
  File "blink-led", line 81, in <module>
    set_line("TRI_STATE_ALL", 0)
  File "blink-led", line 66, in set_line
    line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
AttributeError: 'NoneType' object has no attribute 'request'
htot commented 4 years ago

Strange. I get:

root@edison:~# blink-led 
Traceback (most recent call last):
  File "/usr/bin/blink-led", line 86, in <module>
    led.request(consumer=led.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
OSError: [Errno 16] Device or resource busy

and that is because the led gpio is busy. It's used by the kernel:

root@edison:~# lsmod | grep led
leds_gpio              16384  0
ledtrig_heartbeat      16384  0
led_class              20480  2 leds_gpio,sdhci

I meant to look at the code as an example.

htot commented 4 years ago

Try this:

root@edison:~# ls -l /sys/kernel/config/acpi/table
total 0
drwxr-xr-x 2 root root 0 Jan  1  2000 arduino
drwxr-xr-x 2 root root 0 Jan  1  2000 leds
drwxr-xr-x 2 root root 0 Jan  1  2000 spidev
root@edison:~# rmdir /sys/kernel/config/acpi/table/leds/
# heartbeat stops
root@edison:~# systemctl start blink-led
# blink-led runs
shawnhsoia97 commented 4 years ago

I tried,but not work.

root@edison:/usr/bin# python3 blink-led
Traceback (most recent call last):
  File "blink-led", line 81, in <module>
    set_line("TRI_STATE_ALL", 0)
  File "blink-led", line 66, in set_line
    line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
AttributeError: 'NoneType' object has no attribute 'request'
root@edison:/usr/bin# ls -l /sys/kernel/config/acpi/table
total 0
drwxr-xr-x 2 root root 0 Jan  1  2000 i2c3028
drwxr-xr-x 2 root root 0 Jan  1  2000 spidev
root@edison:/usr/bin#  lsmod | grep led
led_class              20480  1 sdhci
htot commented 4 years ago

I tried,but not work.


root@edison:/usr/bin# python3 blink-led
Traceback (most recent call last):
  File "blink-led", line 81, in <module>
    set_line("TRI_STATE_ALL", 0)
  File "blink-led", line 66, in set_line
    line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
AttributeError: 'NoneType' object has no attribute 'request'
root@edison:/usr/bin# ls -l /sys/kernel/config/acpi/table
total 0
drwxr-xr-x 2 root root 0 Jan  1  2000 i2c3028

Maybe the problem is caused by this table?

Ah, no, you haven't loaded the arduino table, so the TRI_STATE_ALL line is not defined. See:

root@edison:~# gpioinfo | grep -i TRI
        line  14: "TRI_STATE_ALL" unused output active-high 

drwxr-xr-x 2 root root 0 Jan 1 2000 spidev root@edison:/usr/bin# lsmod | grep led led_class 20480 1 sdhci

shawnhsoia97 commented 4 years ago

Because I use other platform not Arduino.so I not found "TRI_STATE_ALL",arduino.asl is not work for me.

root@edison:/home# gpioinfo | grep -i TRI
root@edison:/home#

I think should to create ACPI table for my platform,but I don't know how to do,I not understand arduino.asl,is there any reference?

Do you have reference of gpiod function?Because I not understand how to use line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT).Can I use "TRI_STATE_ALL" directly,not use ACPI table? because that should can use any platform. image

Also I tried blink-led on Arduino,but "TRI_STATE_ALL" is busy.

root@edison:~# gpioinfo | grep -i TRI
        line  14: "TRI_STATE_ALL" unused output active-high
root@edison:~# blink-led
Traceback (most recent call last):
  File "/usr/bin/blink-led", line 82, in <module>
    set_line("SPI_CLK_SEL", 0)
  File "/usr/bin/blink-led", line 66, in set_line
    line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
OSError: [Errno 16] Device or resource busy
root@edison:~#
htot commented 4 years ago

Because I use other platform not Arduino.so I not found "TRI_STATE_ALL",arduino.asl is not work for me.

If you don't have this pin, you can't toggle it.

I think should to create ACPI table for my platform,but I don't know how to do,I not understand arduino.asl,is there any reference?

There exists https://www.acpica.org/documentation

Do you have reference of gpiod function?Because I not understand how to use line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT).Can I use "TRI_STATE_ALL" directly,not use ACPI table? because that should can use any platform. image

It's just a pin. We are getting a handle to the pin, but you don't have the pin.

Do you have a LED on pin 40?

Also I tried blink-led on Arduino,but "TRI_STATE_ALL" is busy.

root@edison: blink-led Traceback (most recent call last): File "/usr/bin/blink-led", line 82, in set_line("SPI_CLK_SEL", 0) File "/usr/bin/blink-led", line 66, in set_line line.request(consumer=line.owner().name(), type=gpiod.LINE_REQ_DIR_OUT) OSError: [Errno 16] Device or resource busy

No, SPI_CLK_SEL is busy. That means it is being used in one of you acpi tables.

shawnhsoia97 commented 4 years ago

I read the arduino.asl

External (_SB_.PCI0.I2C1, DeviceObj)

Scope (\_SB.PCI0.I2C1)
{
    #include "gpioexp.asli"

    // GPIO expander (U17)

I get it,that means it is "TRI_STATE_ALL" not exists edison,it exists in Arduino,Edison use I2C expand it?

So is spidev.asl exclusive to Arduino?

I can control gpio40 output 0/1.

root@edison:~# python3
Python 3.7.6 (default, Mar  4 2020, 03:10:38)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiod
>>> led = gpiod.Chip("gpiochip0").get_line(40)
>>> led.request(consumer=led.owner().name(), type=gpiod.LINE_REQ_DIR_OUT)
>>> led.set_value(1) 
>>> led.set_value(0)
>>> led.set_value(1)

But if I want to control Edison pinout 52. ,change GP40 to I2C_2_CLK or GPIO input,how do I? image

Also will spidev.asl be affected if I don't use Arduino? Because I use to python control spi,but spi_clk is not work.

htot commented 4 years ago

I read the arduino.asl

External (_SB_.PCI0.I2C1, DeviceObj)

Scope (\_SB.PCI0.I2C1)
{
    #include "gpioexp.asli"

    // GPIO expander (U17)

I get it,that means it is "TRI_STATE_ALL" not exists edison,it exists in Arduino,Edison use I2C expand it?

Yes, see the schematics.

So is spidev.asl exclusive to Arduino?

This I don't know.

I can control gpio40 output 0/1.

Because the pin is a GPIO. When you would load leds.aml the pin is taken by the kernel and in use.

But if I want to control Edison pinout 52. ,change GP40 to I2C_2_CLK or GPIO input,how do I? image

Unused (by the kernel) gpios can be controlled from user user space same as gpio40. To change mode gpio->spi must be done from acpi table (or platform driver). To change gpio from input->output use gpio.

Also will spidev.asl be affected if I don't use Arduino? Because I use to python control spi,but spi_clk is not work.

I don't know. You have to compare your schematics with the Edison Arduino?

shawnhsoia97 commented 4 years ago

Because the pin is a GPIO. When you would load leds.aml the pin is taken by the kernel and in use.

No,I not load leds.aml

Unused (by the kernel) gpios can be controlled from user user space same as gpio40. To change mode gpio->spi must be done from acpi table (or platform driver). To change gpio from input->output use gpio.

I have a one question.

DefinitionBlock ("spidev.aml", "SSDT", 5, "", "SPIDEV", 1)
{
    External (_SB_.PCI0.SPI5, DeviceObj)

    Scope (\_SB.PCI0.SPI5)
    {
        Device (TP0)
        {
            Name (_HID, "SPT0001")
            Name (_DDN, "SPI test device")
            Name (_CRS, ResourceTemplate () {
                SpiSerialBus (
                    1,                      // Chip select
                    PolarityLow,            // Chip select is active low
                    FourWireMode,           // Full duplex
                    8,                      // Bits per word is 8 (byte)
                    ControllerInitiated,    // Don't care
                    1000000,                // 1 MHz
                    ClockPolarityLow,       // SPI mode 0
                    ClockPhaseFirst,        // SPI mode 0
                    "\\_SB.PCI0.SPI5",      // SPI host controller
                    0                       // Must be 0
                )
            })
        }
    }
}

Why is it SPI5,not SPI2? Because I read schematics with edison and arduino,but that is both only show SPI2,not SPI5. image image

I don't know. You have to compare your schematics with the Edison Arduino?

Yes,my schematics is not connect MUX ,that connect the device directly . But I don't have /dev/spi5.1 now,after modify edison.conf. Strange,It was something before. I just remove spidev.asl and re-add it.


root@edison:/home#  ls -l /sys/kernel/config/acpi/table
total 0
drwxr-xr-x 2 root root 0 Jan  1  2000 i2c3028
drwxr-xr-x 2 root root 0 Jan  1  2000 spidev
root@edison:/home# ls /dev/s
sequencer   sequencer2  serial/     shm/        snapshot    snd/        stderr      stdin       stdout
htot commented 4 years ago

No,I not load leds.aml

Exactly, therefore you (user space) control gpio40. When you load leds.aml the kernel controls the gpio and user space can not.

shawnhsoia97 commented 4 years ago

Hi @htot ,I cannot use spi with Arduino and Edison,I tried using the following test code.

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>

#include "mraa.h"

#define LED_PIN 21
#define SPI_MISO_PIN 24
#define SPI_MOSI_PIN 11
#define SPI_SCK_PIN 10

/* SPI declaration */
#define SPI_BUS 5

/* SPI frequency in Hz */
#define SPI_FREQ 400000

uint16_t pat[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 };
uint16_t pat_inv[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa };
uint16_t pat_clear[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 };

int running = 0;

void sig_handler(int signo)
{
    if ( signo == SIGINT ) {
        printf("Closing GPIO\n", LED_PIN);
        running = -1;
    }
}

int main()
{
    /* Initialize mraa */
    mraa_result_t status = MRAA_SUCCESS;
    mraa_result_t r = MRAA_SUCCESS;
    mraa_spi_context spi;
    int i, j;
    mraa_init();

        //! [Interesting]
    /* initialize SPI bus */
    spi = mraa_spi_init(SPI_BUS);
    if (spi == NULL) {
        fprintf(stderr, "Failed to initialize SPI\n");
        mraa_deinit();
        return EXIT_FAILURE;
    }

    /* Create access to GPIO pin */
    mraa_gpio_context gpio;
    gpio = mraa_gpio_init(LED_PIN);
    if ( gpio == NULL ) {
        fprintf(stderr, "Error opening GPIO\n");
        exit(1);
    }

    /* set SPI frequency */
    status = mraa_spi_frequency(spi, SPI_FREQ);
    if (status != MRAA_SUCCESS)
        goto err_exit;

    /* set big endian mode */
    status = mraa_spi_lsbmode(spi, 0);
    if (status != MRAA_SUCCESS) {
        goto err_exit;
    }

    /* Set GPIO direction to out */
    r = mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
    if ( r != MRAA_SUCCESS ) {
        mraa_result_print(r);
    }
    /* Create signal handler so we can exit gracefully */
    signal(SIGINT, sig_handler);

    /* Turn LED off and on forever until SIGINT (Ctrl+c) */
    while ( running == 0 ) {

        mraa_spi_write_buf_word(spi, pat, 16);

        r = mraa_gpio_write(gpio, 0);
        if ( r != MRAA_SUCCESS ) {
            mraa_result_print(r);
        }
        sleep(1);

        r = mraa_gpio_write(gpio, 1);
        if ( r != MRAA_SUCCESS ) {
            mraa_result_print(r);
        }
        sleep(1);
    }

    /* Clean up GPIO and exit */
    r = mraa_gpio_close(gpio);
    if ( r != MRAA_SUCCESS ) {
        mraa_result_print(r);
    }

err_exit:
    mraa_result_print(status);

    return r;
}

I can read GP183 output,but my SPI_SCK is NULL. I don't know why. I have do install spidev.asl

root@bovia-deb:/bovia# ls /dev/spidev5.1
/dev/spidev5.1
root@bovia-deb:/bovia# ls -l /sys/kernel/config/acpi/table
total 0
drwxr-xr-x 2 root root 0 Jan  1  2000 arduino
drwxr-xr-x 2 root root 0 Jan  1  2000 i2c3028
drwxr-xr-x 2 root root 0 Jan  1  2000 leds
drwxr-xr-x 2 root root 0 Jan  1  2000 spidev

But my SPI is not work,do you know why? Am i missing something? Thank you.

htot commented 4 years ago

I haven't tried this myself https://edison-fw.github.io/meta-intel-edison/4.4-hsu-i2c-spi.html#spi Maybe you can find who contributed this piece of doc.

But likely it was not tested using mraa.

xlla commented 4 years ago

I cannot use spi with Arduino and Edison,I tried using the following test code.

Yes, Arduino and Mraa can't work right now, until they both migrate to libgpiod.

xlla commented 4 years ago

But my SPI is not work,do you know why?

SPI will work if you use python with libgpiod.

shawnhsoia97 commented 4 years ago

Thank for help me.I give up using SPI directly.I still not work with SPI. So I use Mraa to control GPIO to mimic SPI function.