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

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?

  • Michael,

    You may have better luck posting this question in a device forum, as it doesn't seem to be related to TI-RTOS or SYS/BIOS.  What device are you using?  What TI libraries are you using?

  • The API is part of TI-RTOS. This is the GPIO driver portion of TI-RTOS. The documentation is found in the RTOS User Guide. That's why I posted it here. I will try the device forum too. The enums are in the board-specific file, so that may be the issue.

    I am using the F28M35x. The library is just part of the TI-RTOS package.

  • Hi Michael,

    Sorry about bouncing you around. First a little background...with other TI-RTOS drivers, you call <Driver>_open which returns a handle (pointer to an object). When you want to act on that instance, you use the handle (e.g. UART_write(handle, ...)). Note you supply an array of objects in the <Driver>_config structure.

    This seemed like over-kill for GPIO. We don't have objects for GPIO instances. Instead we use an index. The index is a virtual index that starts at zero and increases.

    You can have multiple pins from the same port assigned to an index. For example, on the Tiva launchpad, you could tie both buttons (SW1 and SW2) to one index:

    const GPIO_HWAttrs gpioHWAttrs[EK_TM4C123GXL_GPIOCOUNT] = {
        {GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_OUTPUT}, /* EK_TM4C123GXL_LED_RED */
        {GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_OUTPUT}, /* EK_TM4C123GXL_LED_BLUE */
        {GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_OUTPUT}, /* EK_TM4C123GXL_LED_GREEN */
        {GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_INPUT},  /* EK_TM4C123GXL_GPIO_SW1 & SW2 */
    };

    Now if you do a GPIO_read(Board_BUTTON0), (which on Tiva LP is 3), you'll read both pins (0 and 4) on port F. The GPIO_read returns a mask value. So for Tiva LP case, you'd get a return of 17 (16 | 1) if both buttons are not depressed. If you depress both, the return would be 0.

    So in summary, think of index as the index into the gpioHWAttrs structure.

    Note: we have both board specific enums in the board specific header file (e.g. EK_TM4C123GXL_SW1 in EK_TM4C123GXL.h) and generic ones in the Board.h file (Board_BUTTON0) . This allows us to make more generic examples that run on multiple boards.

    Hope that helps. Let me know if I've just confused things more!

    Todd

  • Thank you, that makes perfect sense. I had thought that multiple indices would correspond to multiple enum values, since all the example enums are set up as single pins. But this makes sense. Multiple pins can be specified at the array gpioHWAttrs, and the enum just corresponds to that array.