14 Replies Latest reply on Apr 28, 2016 5:56 PM by PVWB

    Programming GPIO Ports From Linux

    SergeyK

      Hi,

       

      I did some experiments with programming Galileo's GPIO ports directly from Linux (as opposed to using Arduino framework).

      As always my findings are posted in my blog here: http://www.malinov.com/Home/sergey-s-blog/intelgalileo-programminggpiofromlinux

       

      Hope it will be useful for others.

       

      Thanks,

      Sergey

        • 1. Re: Programming GPIO Ports From Linux
          arduino_4_life

          That's awesome Sergey, very useful post

          I'll be pointing others towards this....

          • 2. Re: Programming GPIO Ports From Linux
            mjstanis

            Thanks Sergey for posting, worked like a charm!  Now I can make sure of the two unused GPIOs on the Galileo.

             

            Matt

            • 3. Re: Programming GPIO Ports From Linux
              jwhGaliN00B

              I'm having success with the GPIO port programming except for the setting of the output drive strength.  According to the article by Sergey, the following should work:

               

              root@clanton:~# echo -n "strong" >/sys/class/gpio/gpio3/drive

               

              But I always get:

               

              root@clanton:~# echo -n "strong" >/sys/class/gpio/gpio3/drive

              -sh: echo: write error: Invalid argument

               

              Am I missing something?  It seems to work regardless and I am able to toggle the onboard LED via GPIO3 as expected.  Just wondering about the drive strength option.

               

              Thanks in advance.

              • 4. Re: Programming GPIO Ports From Linux
                SergeyK

                You can't (and don't have to) set "drive" mode for GPIOs that are connected to Quark X1000 SoC directly. That are all GPIOs from gpio0 to gpio15. So just skip that command.

                • 5. Re: Programming GPIO Ports From Linux
                  jwhGaliN00B

                  Thanks so much!  I really appreciate your blog and posts here on this topic.

                  • 6. Re: Programming GPIO Ports From Linux
                    jdschumacher

                    I'm getting an error when running:

                     

                    echo -n "40" > /sys/class/gpio/export

                     

                    -sh: echo: write error: Invalid argument

                     

                    Do you know why what would cause this?

                    • 7. Re: Programming GPIO Ports From Linux
                      AllynH

                      Hi Sergey,

                      I am having some issues getting the PWM working as per your example.

                       

                      Here is my process:

                      echo -n "3" > /sys/class/pwm/pwmchip0/export

                      echo -n "1" > /sys/class/pwm/pwmchip0/pwm3/enable

                      echo -n "1000000" > /sys/class/pwm/pwmchip0/pwm3/period

                      echo -n "500000" > /sys/class/pwm/pwmchip0/pwm3/duty_cycle

                       

                      PWM3 is selected by tying GPIO30 high, which appears correct when I cat /sys/kernel/debug/gpio:

                      gpio-30  (sysfs               ) out hi

                       

                      I've checked the output of the Saleae Logic Analyser and there is no activity on the digital pin 3, which maps to PWM3.

                       

                      Any help is appreciated,

                      • 8. Re: Programming GPIO Ports From Linux
                        PengWan

                        G'day,

                         

                        I've similar problem as well:

                         

                        echo -n "40" > /sys/class/gpio/export

                        /sys/class/gpio/export, I get -sh: echo: write error: Device or resource busy


                        Ignoring this, I continued with what I intended to test:

                        echo "40" > /sys/class/gpio/export                    //write a "40" to the export file, which reserves gpio40 for use

                        echo "out" > /sys/class/gpio/gpio40/direction    //sets gpio40 as an output

                        echo "1" > /sys/class/gpio/gpio40/value            //sets gpio40 high

                        echo "50" > /sys/class/gpio/export                    //write a "50" to the export file, which reserves gpio50 for use

                        echo "out" > /sys/class/gpio/gpio50/direction    //sets gpio50 as an output

                        echo "1" > /sys/class/gpio/gpio50/value            //sets gpio50 high

                         

                        I couldn't get IO0 to drive high in this case. If I use a sketch instead to drive IO0, then it works.

                        Any idea what went wrong?

                         

                        I'm on 1.0.3 now for both firmware and SD Image.

                         

                        Thanks.

                        • 9. Re: Programming GPIO Ports From Linux
                          mhahn

                          one cool feature of the Intel IoT devkit image on https://software.intel.com/en-us/iot/getting-started is that it comes with libmraa / UPM under MIT license (both also contained out of the box in Edison Yocto image). libmraa abstracts away many details of GPIO / PWM / SPI / IPC and even has support for Python, and JavaScript besides C/C++.

                          It contains also examples - below e.g. a Python example for PWM (/usr/share/mraa/examples/python/cycle-pwm3.py)

                           

                          # author [...]

                          # Copyright (c) 2014 Intel Corporation.

                          # [MIT license]

                          import mraa

                          import time

                           

                          x = mraa.Pwm(3)

                          x.period_us(700)

                          x.enable(True)

                          value= 0.0

                           

                           

                          while True:

                              x.write(value)

                              time.sleep(0.05)

                              value = value + 0.01

                              if value >= 1:

                                  value = 0.0

                          • 10. Re: Programming GPIO Ports From Linux
                            PengWan

                            Are you saying that the previous steps in Linux is not working anymore and I should use DevKit to make it works?

                            • 11. Re: Programming GPIO Ports From Linux
                              mhahn

                              PengWan wrote:

                               

                              Are you saying that the previous steps in Linux is not working anymore and I should use DevKit to make it works?

                              no, I am not. I am just saying that using libmraa / UPM (intel-iot-devkit/mraa · GitHub) makes your live much easier:

                              - efficient abstraction layer

                              - support on Intel(R) Galileo, and Intel(R) Edison [Arduino board] - more likely to follow; this allows e.g. to just recompile your Galileo SW and run it on Edison

                              - already included in Edison Yocto* Linux

                              - You might as well recompile libmraa / UPM (link above) for other images and make use of it there. On Intel(R) IoT DevKit it's just already included

                              • 12. Re: Programming GPIO Ports From Linux
                                annu_shah

                                hey is it possible to access pwm and gpio through cpp program on linux

                                • 13. Re: Programming GPIO Ports From Linux
                                  SergeyK

                                  Yes, it is possible, at least in two ways:

                                  1. Use Intel IoT devkit. C++ examples are here: mraa/examples/c++ at master · intel-iot-devkit/mraa · GitHub

                                  2. Using C++ input/output functions open and write SysFS files as described in my blog (link on the top of this thread).

                                  • 14. Re: Programming GPIO Ports From Linux
                                    PVWB

                                    I get the same results also.  The echo "40" > /sys/class/gpio/export

                                    reports an error and the gpio40 file is not created.  This using

                                    SuSE 13.2 Linux on a Haswell design.  We want to use GPIO

                                    pins for JTAG programming of an fpga.  Although /sys/kernel/debug/gpio

                                    exists, nothing ever shows up in there.


                                    I tried modprobe -f gpio

                                    which produced some output.


                                    How do you start debugging this or is there some other way to do

                                    this?  Perhaps by accessing the registers that control the GPIO pins

                                    directly?  root only access is fine for us.


                                    Thanks, Paul