Accessing GPIO from linux

Discussion in 'UDOO NEO' started by necromancer, Nov 10, 2015.

  1. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8
    Hello,
    I am having a hard time figuring out how to access GPIO pins from linux. According to the documentation, you need to first export pin using;
    \ = ((\ - 1) * 32 ) + \
    In the documentation, as an example, it uses GPIO1_IO_25; \ = ((1 - 1) * 32) + 25 = 25;

    Now I am trying to export pin 13. I believe this is the pin connected to the orange led.
    Pin 13 according to the document is GPIO_102. using the formula;
    (0-1) * 32 + 102 = 70
    After this, I can do,
    echo 70 > /sys/class/gpio/export
    which will create a new folder callged gpio70.

    trying to print the "value" of this pin returns either 0 or 1. it is not consistent and it doesn't relate to the actual value of the pin.
    I have the arduino send LOW to this pin, and reading this value gives 1 at times and 0 the other.

    Any idea what I am doing wrong here?
     
  2. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    You can't export pin 13 because is used by M4.
    Internal pins are for Arduino, external ones for Linux.
    So you can't export it.
    Take another look at the guide, everything's explained better there.
    We plan to make the pinmuxing easier by the way, if you are looking for a way to "mess it up" a bit.
     
  3. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8
    If that is the case,
    If i want to export A3 (GPIO_IO_143) do i need to export it as
    ((0 - 1) * 32) + 143 = 111?

    I just want to make sure that I am getting 0 for (0 - 1) correct.
     
  4. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Every peripheral, to be accessible from a core, should be assigned to it. It's all decided at boot in the kernel.
    So at runtime you just can't now. You should recompile the kernel.
    Obviously our plan is to make it easy, we don't want you to have to recompile the kernel for this, but since the software is still a beta this is what you can do right now.
     
  5. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8
    I am confused then. The document you linked here says that i can export pins.
    so this is not ready yet?
     
  6. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    I think the documentation is really poor in this area. The formula doesn't seem to make any sense.

    Here is some help, maybe:
    [​IMG]
    You can use this diagram to see the position of A3 in the box that the Neo was sent in (sorry for the quality): 36
    [​IMG]

    The corresponding GPIO to 36 would be GPIO_177

    So I guess 177 is the answer? I don't have any breadboard or anything to confirm this unfortunately. If you can try it and come back to me, that would be neat.
     
  7. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Yes, that's the correspondent pin.
    A3 located in the internal row is assigned to Cortex-M4 (Arduino), so by default you can't export it under Linux.
    36 located in the external row is assigned to Cortex-A9, so you can export it as GPIO 177.
     
    Julien likes this.
  8. Vinz87

    Vinz87 UDOOer

    Joined:
    Nov 11, 2015
    Messages:
    105
    Likes Received:
    19
    Then I understand you simply have to read the figure for the corresponding GPIO number. So what's the formula for?
     
  9. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    Here's something that may come in handy (part of a node library I'm working on)

    Code:
    function pinToGpio(pinNumber) {
        if (16 > pinNumber || pinNumber > 47) {
            throw new Error("Invalid pin number.");
        }
        var gpios = [106, 107, 180, 181, 172, 173, 182, 124, 25, 22, 14, 15, 16, 17, 18, 19, 20, 21, 203, 202, 177, 176, 175, 174, 119, 124, 127, 116, 7, 6, 5, 4];
        return gpios[pinNumber - 16];
    }
     
    Andrea Rovai likes this.
  10. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Julien likes this.
  11. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
    Thanks! I'm honoured :)
     
    Andrea Rovai likes this.
  12. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8
    The function seems to be working properly. It looks like you export whatever GPIO_xxxx you see in the diagram. So what is the point of the formula?
     
  13. Julien

    Julien New Member

    Joined:
    Nov 8, 2015
    Messages:
    21
    Likes Received:
    9
  14. Chris Dickerson

    Chris Dickerson New Member

    Joined:
    Nov 20, 2015
    Messages:
    6
    Likes Received:
    2
    As root:

    echo 177 > /sys/class/gpio/export
    echo out > /sys/class/gpio/gpio177/direction
    echo 1 > /sys/class/gpio/gpio177/value
    cat /sys/class/gpio/gpio177/value​

    returns : 0

    What am I doing wrong?
     
  15. necromancer

    necromancer New Member

    Joined:
    Nov 10, 2015
    Messages:
    26
    Likes Received:
    8
    If you read the document it says;

    If you put an LED on that pin out, you can actually see that the led will turn on.
     
    Chris Dickerson likes this.
  16. Chris Dickerson

    Chris Dickerson New Member

    Joined:
    Nov 20, 2015
    Messages:
    6
    Likes Received:
    2
    Derp, Thanks!
     

Share This Page