Tm4clib GPIO pin control

From EdWiki

GPIO pin Control


  • void gpio_toggle(uint32_t gpioport, uint8_t gpios)
    Toggle a Group of Pins. More...
  • static uint8_t gpio_read(uint32_t gpioport, uint8_t gpios)
    Get status of a Group of Pins (atomic) More...
  • static void gpio_write(uint32_t gpioport, uint8_t gpios, uint8_t data)
    Set level of a Group of Pins (atomic) More...
  • static void gpio_set(uint32_t gpioport, uint8_t gpios)
    Set a Group of Pins (atomic) More...
  • static void gpio_clear(uint32_t gpioport, uint8_t gpios)
    Clear a Group of Pins (atomic) More...
  • static uint8_t gpio_port_read(uint32_t gpioport)
    Read level of all pins from a port (atomic) More...
  • static void gpio_port_write(uint32_t gpioport, uint8_t data)
    Set level of of all pins from a port (atomic) More...

Controlling GPIO pins

Each I/O port has 8 individually configurable bits. When reading and writing data to the GPIO ports, address bits [9:2] mask the pins to be read or written. This mechanism makes all GPIO port reads and writes on the TM4C atomic operations. The GPIO API takes full advantage of this fact to preserve the atomicity of these operations.

Setting or clearing a group of bits can be accomplished with gpio_set() and gpio_clear() respectively. These operation use the masking mechanism described above to only affect the specified pins.

Sometimes it is more appropriate to read or set the level of a group of pins on a port, in one atomic operation. Reading the status can be accomplished with gpio_read(). The result is equivalent to reading all the pins, then masking only the desired pins; however, the masking is done in hardware, and does not require an extra hardware operation.

Writing a group of pins can be accomplished with gpio_write(). The mask ('gpios' parameter) is applied in hardware, and the masked pins are not affected, regardless of the value of the respective bits written to the GPIO port.

Two extra functions are provided, gpio_port_read() and gpio_port_write(). They are functionally identical to gpio_read(port, GPIO_ALL) and gpio_write(port, GPIO_ALL, val) respectively. Hence, they are also atomic.


The gpio_toggle() operation is the only GPIO port operation which is not atomic. It involves a read-modify-write cycle.

Suppose PA0, PA1, PA2, and PA3 are to be modified without affecting the other pins on port A. This is common when controlling, for example, a 4-bit bus:

/* Pins 4,5,6, and 7 are unaffected, regardless of the bits in val */
gpio_write(GPIOA, GPIO0 | GPIO1 | GPIO2 | GPIO3, val);
/* Wait a bit then send the other 4 bits */
gpio_write(GPIOA, GPIO0 | GPIO1 | GPIO2 | GPIO3, val >> 4);

GPIO pins may be toggled with gpio_toggle(). This function does not translate to an atomic operation.

gpio_set(GPIOD, GPIO4);
gpio_set(GPIOD, GPIO4);