6 Replies Latest reply on Dec 13, 2015 10:15 AM by hybridnothing

    MCU Programming: Bidirectional GPIO, how to?

    KEMO993

      Hi,

       

      Is there any way to make this code snippet working?

       

      // ...
      gpio_setup(48, 1);
      gpio_write(48, 0);
      // ...
      gpio_setup(48, 0);
      gpio_read(48);
      // ...
      

       

      Or, shortly described, I want to use the same GPIO pin as digital output and digital input. To make Blink example working I need to configure pin as output with sh init_DIG.sh -o 7 -d output command, but I need to change pin direction using MCU API.

       

      According to the MCU API Documentation ("Set up the input or output mode for one GPIO port. It is okay to call this API with different parameters for the same GPIO port. For example, you can set up a GPIO as an input and later as an output.") it can be done, but unfortunately I don't know how.

       

      Thanks in advance!

        • 1. Re: MCU Programming: Bidirectional GPIO, how to?
          CMata_Intel

          Hi KEMO993,

           

          Have you tried with just:

          gpio_setup(48, 0);
          gpio_read(48);
          

           

          How are you testing this? If you are running this inside a loop you need to be careful while testing the code

          What kind of trigger are you using inside the code in order to change the direction of the pin?

          Are you using the mini breakout board or the Arduino Expansion Board?

          Have you measured the signals you are using as inputs?

           

          Regards,

          Charlie

          • 2. Re: MCU Programming: Bidirectional GPIO, how to?
            KEMO993

            Hi CMata,

             

            Thanks for replay. I'm trying to implement part of OneWire protocol using MCU, to communicate with temperature sensor DS18B20. So, written as some kind of pseudocode, it looks as:

            "set pin direction as output"

            "set output value to low"

            "wait xx us"

            "set pin direction as input"

            "read digital value"

             

            I am using Arduino breakout board. Pin 48 in MCU code is equivalent to Arduino breakout board pin 7.

             

            This code works as expected, 0V is 0 and 5V is 1:

            #include "mcu_api.h"
            #include "mcu_errno.h"
            
            void mcu_main()
            {
              while(1)
              {
              mcu_sleep(200);
              gpio_setup(48, 0);
              int value = gpio_read(48);
              char buf[16];
              int len = mcu_snprintf(buf, 16, "t = %d\n", value);
              debug_print(DBG_INFO, (const char *)buf);
              }
            }
            

             

            Inside the code I want to change the direction of the pin using gpio_setup(48, 1); and gpio_setup(48, 0); without calling .sh scripts.

            • 3. Re: MCU Programming: Bidirectional GPIO, how to?
              KEMO993

              This code is not working as expected:

              #include "mcu_api.h"
              #include "mcu_errno.h"
              
              void mcu_main()
              {
                int i = 0;
                debug_print(DBG_INFO, "Testing digital input...\n");
                while(i < 10)
                {
                mcu_sleep(300);
                gpio_setup(48, 0);
                int value = gpio_read(48);
                char buf[16];
                int len = mcu_snprintf(buf, 16, "t = %d\n", value);
                debug_print(DBG_INFO, (const char *)buf);
                i++;
                }
              
                debug_print(DBG_INFO, "Testing digital output...\n");
                gpio_setup(48, 1);
                debug_print(DBG_INFO, "Testing digital output...\n");
                while(1)
                {
                debug_print(DBG_INFO, "while(1) loop...\n");
                gpio_write(48, 0);
                mcu_sleep(100);
                gpio_write(48, 1);
                mcu_sleep(100);
                }
              }
              

               

              For some reason, gpio_setup(48, 1); is not enough to set pin direction as output and generate 'blink example' in while(1) loop. It works after I invoke "sh init_DIG.sh -o 7 -d output" on Intel Edison "Linux side". Is there any way to change direction only using MCU API?

              • 4. Re: MCU Programming: Bidirectional GPIO, how to?
                CMata_Intel

                Hi KEMO993,

                 

                Do you have updates?

                Have you tried to add the mcu_sleep() after the gpio_setup()? Then, you could start changing the interval in mcu_sleep() to see if you  get improvements

                How are you testing this? Are you using a logic analyzer or an oscilloscope?

                 

                Regards,

                Charlie

                • 5. Re: MCU Programming: Bidirectional GPIO, how to?
                  CMata_Intel

                  Hi KEMO993,

                   

                  Did you try with the suggestions above?

                   

                  Regards,

                  Charlie

                  • 6. Re: MCU Programming: Bidirectional GPIO, how to?
                    hybridnothing

                    I have been running into the same issue while trying to read a DHT11/22 sensor from the MCU.

                    Indeed, gpio_setup(48,0) does not switch the pin from output to input.

                    I guess it has to do with the external levelshifter, controlled by gpio255, which would also have to be changed from output to input.

                    Unfortunately, this one cannot be controlled from the MCU afaik.

                    Bummer...