This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/AM5728: Toggling GPIO other than on-board LED

Part Number: AM5728

Tool/software: TI-RTOS

I'm using Blink LED GPIO example from ti-processor-sdk-rtos-am57xx-evm-04.00.00.04. It blinks USER_LED0 which is GPIO7_8 (F16). What changes do I need to make to toggle GPIO4_10, pin 7 of header P18. 

I've tried changing GPIO_USER0_LED_PIN_NUM and PORT_NUM in 'GPIO_evmAM572x_board.c' to desired pin and port and kept everything same, but it doesn't work. I've also used pinmux tool and added all the files generated by this tool but then I didn't understand how to toggle desired pin.

GPIO_write(index, value) requires index of that pin, I don't know what is index of the desired pin. Basically in my main, I'm doing following. 

Board_initCfg boardCfg;
boardCfg = BOARD_INIT_PINMUX_CONFIG |
BOARD_INIT_MODULE_CLOCK |
BOARD_INIT_UART_STDIO;
Board_init(boardCfg);

GPIO_init();
while(1){
GPIO_write(USER_LED0, GPIO_PIN_VAL_HIGH);
AppDelay(DELAY_VALUE);
GPIO_write(USER_LED0, GPIO_PIN_VAL_LOW);
AppDelay(DELAY_VALUE);
}

Please help.

Thanks.

 

  • Ahmed,

    You have stumbled into a big, obfuscated can of worms that TI created in this example.  I was doing the same thing in a BBB Am335x device.

    Start with the structure "GPIO_v1_config"  which contains substructures of "GPIO_PinConfig".  That structure has a list of GPIOs entries created with a MACRO called GPIO_DEVICE_CONFIG.  The Macro takes the Port address and bit offset and creates a value from it...

    GPIO_PinConfig   gpioPinConfigs   [] = { /* Output pins...   */ 
    
           GPIO_DEVICE_CONFIG((GPIO_USER0_LED_PORT_NUM + 1), GPIO_USER0_LED_PIN_NUM) | GPIO_CFG_OUTPUT, 
           GPIO_DEVICE_CONFIG((GPIO_USER1_LED_PORT_NUM + 1), GPIO_USER1_LED_PIN_NUM) | GPIO_CFG_OUTPUT,
           // ... more entries etc...
    }

    So, what you have is this  (see my comments in the snippet) :

    /* GPIO Driver configuration structure */
    GPIO_v1_Config GPIO_v1_config = {
        gpioPinConfigs,                    //   A list of PIN GPIO configurations from above 
        gpioCallbackFunctions,        //    A LIST OF CALL BACKS  (All NULLed out in the sample I have) 
        sizeof(gpioPinConfigs) / sizeof(GPIO_PinConfig),   //   How many are in the list  
        sizeof(gpioCallbackFunctions) / sizeof(GPIO_CallbackFxn),     //  How many are in the list
        0x1U,
    };
    
    

    Next, when you want to light up a GPIO, don't think you are simply going to assert a true or false to that pin.  Actually, when you do this:

    GPIO_write((LED2), GPIO_PIN_VAL_HIGH);   //  where LED2 = 2 (offset from zero)

    What you are REALLY telling it, is that you want to set high the THIRD ENTRY OF THE LIST of gpioPinConfigs.  

    So if you want to toggle a different GPIO, than you need to modify the gpioPinConfigs  entry so the index of that list is the address/bit you want to modify, or add a new entry to the end of the list.

    I am suggesting this as an answer...  Hope this explains it.  And helps. 

    -Scott

  • Hi Scott.

    In my case when I use following line, no gpio toggles

    GPIO_DEVICE_CONFIG((GPIO_USER0_LED_PORT_NUM + 1), GPIO_USER0_LED_PIN_NUM) | GPIO_CFG_OUTPUT,

    but when I use it without '+1', I'm able to toggle GPIO7_8 and GPIO7_9(LED USER0 and USER1). But if I try to toggle any other GPIO pins other than above two, it doesn't work. Which makes me believe that there is some other place where this example is configuring these two pins.

    Please help. Attached are 'GPIO_evmAM572x_board.c' and 'main_led_blink.c'.

    Thanks.

    -Ahmed.  

    /**
     *  \file   GPIO_evmAM572x_board.c
     *
     *  \brief  AM572x GP EVM board specific GPIO parameters.
     *
     */
    
    /*
     * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * Redistributions of source code must retain the above copyright
     * notice, this list of conditions and the following disclaimer.
     *
     * Redistributions in binary form must reproduce the above copyright
     * notice, this list of conditions and the following disclaimer in the
     * documentation and/or other materials provided with the
     * distribution.
     *
     * Neither the name of Texas Instruments Incorporated nor the names of
     * its contributors may be used to endorse or promote products derived
     * from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    
    #include <stdio.h>
    #include <ti/drv/gpio/GPIO.h>
    #include <ti/csl/soc.h>
    #include <ti/drv/gpio/soc/GPIO_soc.h>
    
    //#define GPIO_USER0_LED_PIN_NUM    (0x10)
    //#define GPIO_USER0_LED_PORT_NUM   (0x04)
    
    //#define GPIO_USER0_LED_PIN_NUM    (0x14)
    //#define GPIO_USER0_LED_PORT_NUM   (0x07)
    
    #define GPIO_USER0_LED_PIN_NUM    (0x014)
    #define GPIO_USER0_LED_PORT_NUM   (0x07)
    //#if defined (__TI_ARM_V7M4__)
    //#define GPIO_USER1_LED_PIN_NUM    (0x10)       /* IRQ mapped GPIO 1&2 banks for M4*/
    //#define GPIO_USER1_LED_PORT_NUM   (0x01)       /* IRQ mapped GPIO 1&2 banks for M4*/
    //#else
    #define GPIO_USER1_LED_PIN_NUM    (0x15)
    #define GPIO_USER1_LED_PORT_NUM   (0x07)
    //#endif
    
    /* GPIO Driver board specific pin configuration structure */
    GPIO_PinConfig gpioPinConfigs[] = {
        /* Input pin with interrupt enabled : AM57X_IDK_GRN_LED */
    //#if defined (__TI_ARM_V7M4__)
    //    GPIO_DEVICE_CONFIG(GPIO_USER1_LED_PORT_NUM, GPIO_USER1_LED_PIN_NUM) |
    //    GPIO_CFG_IN_INT_RISING | GPIO_CFG_INPUT,
    //#else
    //    GPIO_DEVICE_CONFIG(GPIO_USER0_LED_PORT_NUM, GPIO_USER0_LED_PIN_NUM) |
    //    GPIO_CFG_IN_INT_RISING | GPIO_CFG_INPUT,
    //#endif
    
        /* Output pin : AM57X_IDK_YEL_LED */
    
    //    GPIO_DEVICE_CONFIG(GPIO_USER0_LED_PORT_NUM, GPIO_USER0_LED_PIN_NUM) |
    //    GPIO_CFG_IN_INT_RISING | GPIO_CFG_INPUT,
    //    GPIO_DEVICE_CONFIG(GPIO_USER1_LED_PORT_NUM, GPIO_USER1_LED_PIN_NUM) |
    //    GPIO_CFG_OUTPUT
    
        GPIO_DEVICE_CONFIG((GPIO_USER0_LED_PORT_NUM), GPIO_USER0_LED_PIN_NUM) | GPIO_CFG_OUTPUT,
        GPIO_DEVICE_CONFIG((GPIO_USER1_LED_PORT_NUM), GPIO_USER1_LED_PIN_NUM) | GPIO_CFG_OUTPUT
    };
    
    /* GPIO Driver call back functions */
    GPIO_CallbackFxn gpioCallbackFunctions[] = {
        NULL,
        NULL
    };
    
    /* GPIO Driver configuration structure */
    GPIO_v1_Config GPIO_v1_config = {
        gpioPinConfigs,
        gpioCallbackFunctions,
        sizeof(gpioPinConfigs) / sizeof(GPIO_PinConfig),
        sizeof(gpioCallbackFunctions) / sizeof(GPIO_CallbackFxn),
        0x20,
        };
    
    /**
     *  \file   main.c
     *
     *  \brief  Example application main file. This application will toggle the led.
     *          The led toggling will be done inside an callback function, which
     *          will be called by Interrupt Service Routine. Interrupts are
     *          triggered manually and no external source is used to trigger
     *          interrupts.
     *
     */
    
    /*
     * Copyright (C) 2014 - 2016 Texas Instruments Incorporated - http://www.ti.com/
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * Redistributions of source code must retain the above copyright
     * notice, this list of conditions and the following disclaimer.
     *
     * Redistributions in binary form must reproduce the above copyright
     * notice, this list of conditions and the following disclaimer in the
     * documentation and/or other materials provided with the
     * distribution.
     *
     * Neither the name of Texas Instruments Incorporated nor the names of
     * its contributors may be used to endorse or promote products derived
     * from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    
    
    #ifndef BARE_METAL
    /* XDCtools Header files */
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    #include <xdc/runtime/Error.h>
    
    
    /* BIOS Header files */
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #endif
    
    #include <stdio.h>
    
    /* TI-RTOS Header files */
    #include <ti/drv/gpio/GPIO.h>
    #include <ti/drv/gpio/soc/GPIO_soc.h>
    
    //#include "GPIO_log.h"
    #include <ti/drv/gpio/test/led_blink/src/GPIO_log.h>
    //#include "GPIO_board.h"
    #include <ti/drv/gpio/test/led_blink/src/GPIO_board.h>
    
    #include <ti/board/board.h>
    
    /**********************************************************************
     ************************** Macros ************************************
     **********************************************************************/
    //#if defined(SOC_AM572x) || defined (SOC_AM571x)
    //#if defined (__TI_ARM_V7M4__)
    //#define DELAY_VALUE       (0x6FFFFFU) /* Update Delay as it is not sufficent for M4 core */
    //#else
    //#define DELAY_VALUE       (0x6FFFFFU)
    //#endif
    //#else
    #define DELAY_VALUE       (0x6FFFFFU)
    //#endif
    
    /**********************************************************************
     ************************** Internal functions ************************
     **********************************************************************/
    
    /* Delay function */
    void AppDelay(unsigned int delayVal);
    
    /* Callback function */
    void AppGpioCallbackFxn(void);
    
    //#if defined(idkAM572x) || defined(idkAM571x)
    ///* GPIO clock and pinmux configurations */
    //extern void AppGPIOInit(void);
    //#endif
    
    //#if defined(idkAM572x)
    //extern void GPIOApp_UpdateBoardInfo(void);
    //extern void GPIOAppUpdateConfig(uint32_t *gpioBaseAddr, uint32_t *gpioPin);
    //#endif
    
    /*
     *  ======== Board_initI2C ========
     */
    static void Board_initGPIO(void)
    {
        Board_initCfg boardCfg;
    
    //#if defined(SOC_K2H) || defined(SOC_K2K) || defined(SOC_K2E) || defined(SOC_K2L) || defined(SOC_K2G) || defined(SOC_C6678) || defined(SOC_C6657) || defined(SOC_OMAPL137) || defined(SOC_OMAPL138)
    //    GPIO_v0_HwAttrs gpio_cfg;
    //
    //    /* Get the default SPI init configurations */
    //    GPIO_socGetInitCfg(GPIO_LED0_PORT_NUM, &gpio_cfg);
    //
    //    /* Modify the default GPIO configurations if necessary */
    //
    //    /* Set the default GPIO init configurations */
    //    GPIO_socSetInitCfg(GPIO_LED0_PORT_NUM, &gpio_cfg);
    //
    //#if defined(SOC_K2G)
    //    /* Setup GPIO interrupt configurations */
    //    GPIO_socSetIntMux(GPIO_LED0_PORT_NUM, GPIO_LED0_PIN_NUM, NULL, GPIO_MUX_SEL);
    //#endif
    //#if defined(SOC_OMAPL137) || defined(SOC_OMAPL138)
    //    /* Setup GPIO interrupt configurations */
    //    GPIO_socSetBankInt(GPIO_LED0_PORT_NUM, GPIO_LED0_PIN_NUM, NULL);
    //#endif
    //#endif
    
    //#if defined(evmK2E) || defined(evmC6678)
    //    boardCfg = BOARD_INIT_MODULE_CLOCK |
    //        BOARD_INIT_UART_STDIO;
    //#else
        boardCfg = BOARD_INIT_PINMUX_CONFIG |
            BOARD_INIT_MODULE_CLOCK |
            BOARD_INIT_UART_STDIO;
    //#endif
        Board_init(boardCfg);
    
    //#if defined(idkAM572x)
    //    GPIOApp_UpdateBoardInfo();
    //#endif
    }
    
    /**********************************************************************
     ************************** Global Variables **************************
     **********************************************************************/
    volatile uint32_t gpio_intr_triggered = 0;
    uint32_t gpioBaseAddr;
    uint32_t gpioPin;
    
    /*
     *  ======== test function ========
     */
    //#ifndef BARE_METAL
    void gpio_test(UArg arg0, UArg arg1)
    {
    //#else
    //void main()
    //{
    //    Board_initGPIO();
    //#endif
        uint32_t testOutput = 1;
    
        /* GPIO initialization */
        GPIO_init();
    
        /* Set the callback function */
        GPIO_setCallback(USER_LED0, AppGpioCallbackFxn);
    
        /* Enable GPIO interrupt on the specific gpio pin */
        GPIO_enableInt(USER_LED0);
    
        /* Write high to gpio pin to control LED1 */
        GPIO_write((USER_LED1), GPIO_PIN_VAL_HIGH);
        AppDelay(DELAY_VALUE);
    
        GPIO_log("\n GPIO Led Blink Application \n");
    
    //#if defined(SOC_K2L) || defined(SOC_C6678) || defined(SOC_C6657)
    //    /* No GPIO pin directly connected to user LED's on K2L/K2G/C6678/C6657 EVM, just trigger interrupt once */
    //    GPIO_toggle(USER_LED0);
    //    while (!gpio_intr_triggered);
    //
    //    GPIO_log("\n All tests have passed \n");
    //#else
    
        while(1)
        {
    //#if defined(SOC_AM572x) || defined(SOC_AM571x)|| defined(SOC_AM335x) || defined(SOC_AM437x)
    
    //#if defined (idkAM572x)
    //        /* Update GPIO info based on the board */
    //        GPIOAppUpdateConfig(&gpioBaseAddr, &gpioPin);
    //#else
            gpioBaseAddr = GPIO_BASE_ADDR;
            gpioPin      = GPIO_LED_PIN;
    //#endif
            /* Trigger interrupt */
            GPIOTriggerPinInt(gpioBaseAddr, 0, gpioPin);
    //#endif
    //#if defined(SOC_K2H) || defined(SOC_K2K) || defined(SOC_K2E) || defined(SOC_K2G) || defined(SOC_OMAPL137) || defined(SOC_OMAPL138)
    //        GPIO_toggle(USER_LED0);
    //#endif
            AppDelay(DELAY_VALUE);
            if (testOutput)
            {
                GPIO_log("\n All tests have passed \n");
                testOutput = 0;
            }
        }
    //#endif
        Task_exit();
    }
    
    //#ifndef BARE_METAL
    /*
     *  ======== main ========
     */
    int main(void)
    {
        /* Call board init functions */
        //Board_initGPIO();
        Board_initCfg boardCfg;
        boardCfg = BOARD_INIT_PINMUX_CONFIG |
            BOARD_INIT_MODULE_CLOCK |
            BOARD_INIT_UART_STDIO;
        Board_init(boardCfg);
    
        GPIO_init();
        while(1){
                GPIO_write(0, GPIO_PIN_VAL_HIGH);
                AppDelay(DELAY_VALUE);
                GPIO_write(0, GPIO_PIN_VAL_LOW);
                AppDelay(DELAY_VALUE);
        }
    
    //#if defined(idkAM572x) || defined(idkAM571x)
    //    AppGPIOInit();
    //#endif
    
        /* Start BIOS */
        BIOS_start();
        return (0);
    }
    //#endif
    
    /*
     *  ======== AppDelay ========
     */
    void AppDelay(unsigned int delayVal)
    {
        while(delayVal)
        {
            delayVal--;
        }
    }
    
    /*
     *  ======== Callback function ========
     */
    void AppGpioCallbackFxn(void)
    {
        /* Toggle LED1 */
        GPIO_toggle(USER_LED1);
        AppDelay(DELAY_VALUE);
        gpio_intr_triggered = 1;
    }
    
    
    

  • Thank you Scott. 

    I figured out that pin and port needs to be in hexadecimal, so if pin is 15 it should be 0x0E and if 16 it should be 0x0F. 

    Thanks a lot for your help. 

  • Okay, that's good to know as well.

    Sorry I couldn't help more, I'm not familiar with that board or device. I can barely make the BBB work in this nightmare environment.

    Interesting that the C compiler has difficulty adding a decimal and hex number together.
    More likely I would suspect just some flaky phantom problem with the compiler/linker/loader/OS... I have those all the time. A project doesn't work, I come back the next day after changing nothing, and suddenly it works. It doesn't give me a lot of faith in these tool chains or the environment.
  • Ahmed Ghumman said:

    Thank you Scott. 

    I figured out that pin and port needs to be in hexadecimal, so if pin is 15 it should be 0x0E and if 16 it should be 0x0F. 

    Thanks a lot for your help. 

    Also, if you are satisfied that solved it, you should mark your followup as an answer as well.

    -Scott