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.

GPIO read and write using RTOS

(This is a repost from the RTOS forum, where it was suggested I post here.) I am trying to figure out GPIO reads and writes using RTOS, and the documentation and examples are unclear. Reading a pin is never shown in any of the documentation or examples, for instance. However, here are some specific questions:

The doxygen documentation for the GPIO read is this:

Bits32 GPIO_read ( UInt  index )

Reads the value of a GPIO pin.

The value returned will either be a zero or non-zero bit packed value of the pin(s) read.

Parameters:
index GPIO index
Returns:
Bit packed value of the GPIO pin(s)

My questions are:

1) how is the bit-packed return value organized? I see no further specification. Is the bit location corresponding to the index of the pin that I request? If so, that leads to the second question.

2) How is the index organized? The way I think it is organized makes no sense. The write function example uses an Enum for the index. Looking at this enum, I don't understand how multiple pin reads and writes are handled. The enum is declared as follows:

typedef enum TMDXDOCKH52C1_GPIOName {
    TMDXDOCKH52C1_LD2 = 0,
    TMDXDOCKH52C1_LD3,
    TMDXDOCKH52C1_BUTTON,

    TMDXDOCKH52C1_GPIOCOUNT
} TMDXDOCKH52C1_GPIOName;

This enum declaration will essentially use values 0, 1, 2, 3... I don't get how this even works, especially with multiple read/writes in one function call. First, defining it this way instead of as bit fields (0x1, 0x2, 0x4,..) means that the third option is indistinguishable from (0x1 & 0x2). How am I supposed to read from both the first and second index? Secondly, a zero based index doesn't even make sense. How am I supposed to read LD2 and LD3 in this case? What is the value of (TMDXDOCKH52C1_LD2 & TMDXDOCKH52C1_LD3), which is (0x0 & 0x1)? It doesn't make any sense.

Perhaps you can only read one pin at a time. Then at least the indexes make sense, if only one can be used at a time. However, then the bit-packed field return value makes no sense, and this contradicts the documentation.

I am baffled. Does anyone know what is going on?

  • Hello,

    I know nothing about RTOS, but I guess the way the API written is similar with C2000.

    In C2000, GPIO_read is written like below (the API name is GPIO_getData):

    uint16_t GPIO_getData(GPIO_Handle gpioHandle, const GPIO_Number_e gpioNumber)
    {
        GPIO_Obj *gpio = (GPIO_Obj *)gpioHandle;

        if(gpioNumber < GPIO_Number_32)
        {
            return ((gpio->GPADAT >> gpioNumber) & 0x0001); // You can see here that the GPIO DATA is shifted by the GPIO number, and then filtered
        }
        else
        {
            return ((gpio->GPBDAT >> (gpioNumber - GPIO_Number_32)) & 0x0001);
        }

    } // end of GPIO_getData() function

    And the number of GPIOs is defined like this (from 0 until all number of GPIOs):

    typedef enum
    {
        GPIO_Number_0=0,    //!< Denotes GPIO number 0
        GPIO_Number_1,      //!< Denotes GPIO number 1
        GPIO_Number_2,      //!< Denotes GPIO number 2
        GPIO_Number_3,      //!< Denotes GPIO number 3
        GPIO_Number_4,      //!< Denotes GPIO number 4
        GPIO_Number_5,      //!< Denotes GPIO number 5
        GPIO_Number_6,      //!< Denotes GPIO number 6
        GPIO_Number_7,      //!< Denotes GPIO number 7
    ....

    ....

    ....
        GPIO_Number_32,     //!< Denotes GPIO number 32
        GPIO_Number_33,     //!< Denotes GPIO number 33
        GPIO_Number_34,     //!< Denotes GPIO number 34
        GPIO_Number_35,     //!< Denotes GPIO number 35
        GPIO_Number_36,     //!< Denotes GPIO number 36
        GPIO_Number_37,     //!< Denotes GPIO number 37
        GPIO_Number_38,     //!< Denotes GPIO number 38
        GPIO_numGpios
    } GPIO_Number_e;

    So this number is not bit definition but the name (the number name) of GPIO that will be used to shifted the GPIO DATA register in the API.

    And GPIO DATA in your case is 32 bits (the same in my case).

    Hope this helps. If you can find what inside GPIO_read(), then you will be ensured.

    Best regards,

    Maria

  • Hi Maria,

    Thanks for your help.

    What you wrote makes sense for sequential enums, but doesn't work for retrieving multiple values at once.

    I got the answer on the other thread. It turns out that each enum can also represent multiple pins, not just one pin. They correspond to one or more GPIO pins defined in an array elsewhere. My mistake was thinking that each enum is one pin, since all the examples worked like this.

    Thanks!

    Mike