4 Replies Latest reply on Jun 4, 2014 2:08 AM by gustafn

    Disambiguating digital writes to pin 13?

    rwaldron

      In How can I control the onboard LED through sysfs? user arduino_4_life says:

       

      the onboard LED is not actually connected to pin 13. The board is set up so that when you call to pin 13 in an Arduino sketch, then this controls the onboard LED too (this was done so that the Blink sketch would work out of the box.)

      If you want to control this LED through sysfs, then you need to write to gpio3. no muxes need to be set for this.

       

      echo 3 > /sys/class/gpio/export

      echo out > /sys/class/gpio/gpio3/direction

      echo 1 > /sys/class/gpio/gpio3/value



      In a follow up question, I asked:

       

      do you know where in the source code for the Arduino IDE (the version for Galileo) this alternate path is taken? I've been through all of the x86/cores/arduino code as I'm trying to figure out how a call to digitalWrite(13, HIGH|LOW) would be disambiguated.



      Hopefully asking this broader audience will help find the answer


      Thanks!

        • 1. Re: Disambiguating digital writes to pin 13?
          JPMontero_Intel

          Hi rwaldron

          I’ll look for that in the libraries, if I’m able to find it I’ll let you know. In the meantime remember you can use system(“command”),  to program the gpio3 in order to control the GP LED for example:

          system(“echo 1 > /sys/class/gpio/gpio3/value”);     //to turn the LED on
          system(“echo 0 > /sys/class/gpio/gpio3/value”);     //to turn the LED off

          Also you can use this function to control pin 13, for this you would have to program gpio55 to mux the gpio39, if this is not clear take a look at Sergey’s blog http://www.malinov.com/Home/sergey-s-blog/intelgalileo-programminggpiofromlinux , right where it says GPIO Mapping in Galileo, take a look at the picture and look for the pin 13, you'll see that the gpio55 control the gpio39 or the SPI1_SCK. If you program this three GPIOs 3, 55 and 39 you would be able to use the LED and pin 13 one independent from the other.

           

          Regards
          JPMontero_Intel

          • 2. Re: Re: Disambiguating digital writes to pin 13?
            rwaldron

            @JPMontero_Intel

             

            Thanks for all of the details!

             

            Presumably, `system` is being used in a sketch? This is out of scope for the project I'm working on, which is a node.js module for controlling the Galileo's GPIO (rwaldron/galileo-io · GitHub)—that is to say that there is no sketch involved.

             

            Also you can use this function to control pin 13, for this you would have to program gpio55 to mux the gpio39, if this is not clear take a look at Sergey’s bloghttp://www.malinov.com/Home/sergey-s-blog/intelgalileo-programminggpiofromlinux , right where it says GPIO Mapping in Galileo, take a look at the picture and look for the pin 13, you'll see that the gpio55 control the gpio39 or the SPI1_SCK. If you program this three GPIOs 3, 55 and 39 you would be able to use the LED and pin 13 one independent from the other.

             

            Yes, thank you—this is what I followed to design the GPIO mapping and muxing system. I think what I actually want to do is control them as one, because user code will expect writes to pin 13 to behave the same as they do in sketches. When the Galileo is running the Blink.ino sketch, how does it know that user code:

             

            int led = 13;

            void setup() {               

              pinMode(led, OUTPUT);    

            }

            void loop() {

              digitalWrite(led, HIGH);

              delay(1000);           

              digitalWrite(led, LOW);

              delay(1000);           

            }

             

            ...referring to pin 13 actually means "write to the onboard led*, not the actual pin 13**"

             

             

             

             

            * gpio3

            ** gpio55 mux to gpio39

            • 3. Re: Disambiguating digital writes to pin 13?
              JPMontero_Intel

              Greetings rwaldron

              We have found the library, its located in this path C:\arduino-1.5.3\hardware\arduino\x86\variants\galileo_fab_d . Open the file variant.cpp and take a look at the table right where it says "PinDescription g_APinDescription[]=" that's the one that describes the behavior of the gpios. On the fourth row is the gpiolib 3 that corresponds to the onboard LED, and if you go to the gpiolib 39 right next to it there is a 20 that is the one that associate the pin 13 to the LED:

               

              { 39,       20,          NONE,  13,          0,            LOW,     (mux_sel_t*)&MuxDesc39,        MUX_SIZE(MuxDesc39),                FN_GPIO_OPENDRAIN,                -1,           0,            NULL  },

               

              Then if you change the value to NONE the LED won’t turn on

               

              { 39,       NONE,  NONE,  13,          0,            LOW,     (mux_sel_t*)&MuxDesc39,        MUX_SIZE(MuxDesc39),                FN_GPIO_OPENDRAIN,                -1,           0,            NULL     },

               

              But if you move that 20 to the gpiolib 38 the LED will turn on every time you put HIGH on the pin 12, you can test it by running the Blink sketch and change the pin to 12

               

              { 38,       20,          NONE,  12,          0,            LOW,     (mux_sel_t*)&MuxDesc38,        MUX_SIZE(MuxDesc38),                FN_GPIO_OPENDRAIN,                -1,           0,            NULL  },

              { 39,       NONE,  NONE,  13,          0,            LOW,     (mux_sel_t*)&MuxDesc39,        MUX_SIZE(MuxDesc39),                FN_GPIO_OPENDRAIN,                -1,           0,            NULL     },

              Is that what you are looking for?

               

              Regards
              JPMontero_Intel

              • 4. Re: Disambiguating digital writes to pin 13?
                gustafn

                You find the implementation of digitalWrite() of the Arduino API ..../hardware/arduino/x86/cores/arduino/wiring_digital.c. The digitalWrite() operation performs both, the "wiring" (i.e. the multiplexer setup according to the Galileo I/O Function Multiplexing Mapping (https://communities.intel.com/servlet/JiveServlet/previewBody/21920-102-2-25976/GalileoIOMappingRev2.pdf) and the actual I/O operation. If one programs directly from Linux, the two steps can be separated (e.g. wiring at startup, I/O at runtime) since not every read/write operation requires a new wiring, as long no other program changes the setup under your feet.

                 

                i did as well some integration of webserver + sensors on the galileo (using naviserver instead of node.js) but ended up with two separate communicating processes (sketch + webserver) instead of integrating everything in the webserver. While the blink example is easy to achieve with a few lines in the webserver, it is much more convenient to use sketches with their pre-existing libraries for more complex sensors interfaces. Furthermore, messing around low-level I/O in the  Linux kernel requires root permissions, and requiring the webserver to run as root is on the longer range not desirable.