Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

How to Toggle a GPIO

How to Toggle a GPIO

250 replies posted 100 replies posted 50 replies posted

Back to main page: Leveraging TAG4 to better understand the WICED Smart Programming Environment

    To toggle a GPIO, paste the following code into your program:

          #define GPIO_NUM      8

          #define PIN           GPIO_NUM%16

          #define PORT          GPIO_NUM/16


          gpio_setPinOutput(PORT, PIN, GPIO_OUTPUT_HIGH);

    It really is that simple, despite some confusion on the topic. All you have to do it change the pin number, and toggle the parameter high and low. There are, in fact, some special cases to take into account, but for the most part these lines of code are all you need to toggle a GPIO. gpio_configurePin must be called only once, gpio_setPinOutput is the only one that must be called thereafter.

    Description of parameters:

                    gpio_configurePin(PORT, PIN, PARAMETER, HIGH/LOW)

        gpio_setPinOutput(PORT, PIN, HIGH/LOW);

    When referring to a pin, however, we use two separate numbers. First we use the port number, next the pin number. There’s nothing special about these numbers, they just correspond the description below:

                    GPIO 00-15            PORT 0

                    GPIO 16-31            PORT 1

                    GPIO 32-39            PORT 2 

    For the HIGH/LOW parameter, always put a zero, one, or a corresponding reference.

    The PARAMETER is what defines the behavior of the GPIO. Paste the provided code into any app function, and control-click one of the parameters that’s in place. You’ll see a list of every possible parameter to put in here. This is where confusion stems from, because the same API is being used to instantiate inputs, outputs, interrupts, etc. For the purposes of toggling a GPIO as an output, leave the code above as I’ve written it.

    The second API call is only used after initial configuration of an output GPIO. The parameters are easy to work with and identical to parameters 1, 2, and 4 of the configure API.

    There’s one additional thing to take into consideration. There is a GPIO configuration array at the top of every sample app. This is only to be used if you wish to utilize the APIs that they register pins for. For example, in order to blink an LED using the API below, you must configure pin 13 (LED on Tag 4) within that array.

bleprofile_LEDBlink(10, 10, 3);

    But utilizing these APIs can also create problems. By instantiating pin the 13 in the GPIO configuration array you relinquish control of the GPIO to the kernel. Meaning, if you attempt to use both LED APIs and manually toggle the LED GPIO in the same app, you may get unexpected behavior. Choose one or the other to be safe.

Jacob W Torres

Back to main page: Leveraging TAG4 to better understand the WICED Smart Programming Environment