1 Reply Latest reply on Apr 14, 2017 10:46 AM by SergeyK

    Configuring 2 GPIO Pins(one for input and other one for output) of same controller

    Rohit1309

      Hi All,

       

      I need to configure the two GPIO pins of same GPIO controller at same time. But one pin should be configure to inputs and other should be configure to give output.

      I write the following code to do so. But it is not working properly.

      In my code I read the state of pin 3 (PIN_OUT2). On the basis of this state setting and clearing the pin 6 (PIN_OUT). pin 3 and 6 both uses the same GPIO controller i.e. QM_SS_GPIO_0

       

      My Code is:

       

      #include "ss_clk.h"

      #include "qm_ss_gpio.h"

      #include "qm_pinmux.h"

       

      /* The following defines the pin and pin mux details for each SoC. */

      #define PIN_OUT 6

      #define PIN_OUT2 3

      #define LED_PIN_ID (QM_PIN_ID_14)

      #define LED_PIN_ID2 (QM_PIN_ID_11)

      #define PIN_MUX_FN (QM_PMUX_FN_0)

      #define DELAY 250000UL /* 0.25 seconds. */

       

       

      int main(void)

      {

              static qm_ss_gpio_port_config_t cfg;

              static qm_ss_gpio_state_t state_ss;

       

       

              /* Set the GPIO pin muxing. */

              qm_pmux_select(LED_PIN_ID, PIN_MUX_FN);

              qm_pmux_select(LED_PIN_ID2, PIN_MUX_FN);

       

       

              /* Set the GPIO pin direction to out and write the config. */

              cfg.direction = BIT(PIN_OUT);

              qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

       

       

              /* Loop indefinitely while blinking the LED. */

              while (1) {

                      cfg.direction = BIT(0);

                      qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

                      if (qm_ss_gpio_read_pin(QM_SS_GPIO_0, PIN_OUT2, &state_ss)) {

                              QM_PUTS("Error: read pin failed\n");

                              return -EIO;

                      }

                      QM_PRINTF("pinstate is %d",state_ss);

                      cfg.direction = BIT(PIN_OUT);

                      qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

                      if(state_ss == QM_SS_GPIO_HIGH)

                      {

                              printf("\t Motion Detected\n");

                              qm_ss_gpio_set_pin(QM_SS_GPIO_0, PIN_OUT);

                              clk_sys_udelay(DELAY);

                      }

                      else

                      {

                              printf("\t No Motion\n");

                              qm_ss_gpio_clear_pin(QM_SS_GPIO_0, PIN_OUT);

                              clk_sys_udelay(DELAY);

                      }

              }

      }

       

      I always give the output as-

       

      pinstate is 0     No Motion

      pinstate is 0     No Motion

      pinstate is 0     No Motion

      pinstate is 0     No Motion

      ....

      and so on.

       

      Can any one please tell me what i did wrong or missed in this code?

       

      Regards,

      Rohit

        • 1. Re: Configuring 2 GPIO Pins(one for input and other one for output) of same controller
          SergeyK

          Hi Rohit,

           

          Note that the following code lines:

                          cfg.direction = BIT(PIN_OUT);

                          qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

          set the direction for all GPIOs on QM_SS_GPIO_0 controller, not just that of PIN_OUT pin.

          Each bit of the cfg.direction controls the direction of the respective GPIO. For example setting bit '0' of cfg.direction to '1' will configure GPIO0 as an output.

          BIT(n) is just a helper macro that produces an integer with bit 'n' set to '1'.

           

          Generally you'll want to set the GPIO configuration in the beginning of your program, unless you need to switch a GPIO pin direction on the fly.

          In the latter case, you should keep the cfg structure, and only set or reset the relevant bits. Something like this:

          cfg.direction |= BIT(PIN_OUT); /* sets PIN_OUT as output, doesn't change anything else */

          qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

           

          cfg.direction &= ~BIT(PIN_OUT); /* sets PIN_OUT as input, doesn't change anything else */

          qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

           

          And so, the following code doesn't do what you probably expect it to do:

                          cfg.direction = BIT(0);

                          qm_ss_gpio_set_config(QM_SS_GPIO_0, &cfg);

          It sets GPIO_SS[0] direction to output... I assume you wanted to switch all the pins to input mode (I am not sure why), something that cfg.direction = 0 will do.

           

          Thanks,

          Sergey