2 Replies Latest reply on Dec 12, 2014 10:18 AM by jwestervelt

    pinMode(n, INPUT_PULLUP) vs. attachInterrup

    jwestervelt

      I am trying to use my Edison with an Arduino sketch that will monitor pin7 and act via ISR, but I am having problems assigning the pullup resistor via pinMode(7, INPUT_PULLUP).  I can verify that the value of /sys/class/gpio/gpio223/direction is "in" meaning that the pullup is not getting enabled.  If I comment out the attachInterrupt() line that triggers on pin7 CHANGE, /sys/class/gpio/gpio223/direction shows "out".   WTH...

       

      The following is my code...

       

        pinMode(7, INPUT_PULLUP);

        attachInterrupt(7, someISR, CHANGE);

       

      Rather hilariously, if I make the small modification to my code to do the following, all works as expected and the internal pullup gets enabled.

       

        attachInterrupt(7, someISR, CHANGE);

        pinMode(7, INPUT_PULLUP);

       

      Did I miss some addendum/errata somewhere?  It appears that the attachInterrupt() function calls pinModeIRQ to assign the pin as HI-Z INPUT:

       

      int pinModeIRQ(uint8_t pin, int8_t mode)

      {

              int ret = 0;

              uint32_t gpio = 0;

              ...

              ret = muxSelect(pin, FN_GPIO_INPUT_HIZ);

              if (ret) {

                      trace_error("%s: can't set mux for pin%d\n", __func__, pin);

                      return ret;

              }

       

      This might easily throw a beginner for a loop, so perhaps it would be best to have attachInterrupt examine the current pinMode and act accordingly.  This is especially true since many Arduino examples exist on the net in which the pinMode is assigned BEFORE attachInterrupt. I haven't verified that the non-Intel Arduino code works regardless of the order within the code.

        • 1. Re: pinMode(n, INPUT_PULLUP) vs. attachInterrup
          Intel_Alvarado

          Hi jwestervelt,

           

          The order of pinMode and attachInterrupt matters. If you use pinMode and then attachInterrupt, attachInterrupt will overwrite the PULLUP condition and put the pin in a HIZ state and the pullup resistor will not be enabled.If you use it the other way around, as you stated, everything should work correctly since the pullup resistor will be enabled.

           

          Regards

          Sergio

          • 2. Re: pinMode(n, INPUT_PULLUP) vs. attachInterrup
            jwestervelt

            Sergio,

             

            Thanks for the input.  I figured out that the order matters, but I didn't see it documented anywhere.  I have verified that the non-Intel Arduino platforms do not have this behavior, and they seem to work fine if the pinMode is specified before attachInterrupt.  I figured that there was likely a document/wiki somewhere that detailed the differences with the Intel Arduino libraries so as to allow folks to port over code easily, but I have not found one.