Tm4clib GPIO pin configuration

From EdWiki

Revision as of 05:58, 22 December 2018 by Jshankar (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


#define PCTL_AF(pin, af) ((af) << ((pin) << 2))
#define PCTL_MASK(pin) PCTL_AF((pin), 0xf)


Enabling GPIO ports

Before accessing GPIO functionality through this API, the AHB aperture for GPIO ports must be enabled via a call to gpio_enable_ahb_aperture(). Failing to do so will cause a hard fault.

Once the AHB aperture is enabled, GPIO registers can no longer be accessed via the APB aperture. The two apertures are mutually exclusive.

Enabling the AHB aperture only needs to be done once. However, in order to access a certain GPIO port, its clock must also be enabled. Enabling the GPIO clock needs to be done for every port that will be used.

For example, to enable GPIOA and GPIOD:

/* Make sure we can access the GPIO via the AHB aperture */
/* Enable GPIO ports A and D */

On reset all ports are configured as digital floating inputs (no pull-up or pull-down), except for special function pins.

Configuring pins as inputs

Configuring GPIO pins as inputs is done with gpio_mode_setup(), with GPIO_MODE_INPUT for the mode parameter. The direction of the pull-up must be specified with the same call

For example, PA2, PA3, and PA4 as inputs, with pull-up on PA4:


Configuring pins as outputs

Output pins have more configuration options than input pins. LM4F pins can be configured as either push-pull, or open drain. The drive strength of each pin can be adjusted between 2mA, 4mA, or 8mA. Slew-rate control is available when the pins are configured to drive 8mA. These extra options can be specified with gpio_set_output_config(). The default is push-pull configuration with 2mA drive capability.

gpio_set_output_config() controls different capabilities than the similar sounding gpio_set_output_options() from the STM GPIO API. They are intentionally named differently to prevent confusion between the two. They are API incompatible.

For example, to set PA2 to output push-pull with a drive strength of 8mA:

gpio_set_output_config(GPIOA, GPIO_OTYPE_PP, GPIO_DRIVE_8MA, GPIO2);

Configuring pins as analog function

Configuring GPIO pins to their analog function is done with gpio_mode_setup(), with GPIO_MODE_ANALOG for the mode parameter.

Suppose PD4 and PD5 are the USB pins. To enable their analog functionality (USB D+ and D- in this case), use:

/* Mux USB pins to their analog function */

Configuring pins as alternate functions

Most pins have alternate functions associated with them. When a pin is set to an alternate function, it is multiplexed to one of the dedicated hardware peripheral in the chip. The alternate function mapping can be found in the part's datasheet, and usually varies between arts of the same family.

Multiplexing a pin, or group of pins to an alternate function is done with gpio_set_af(). Because AF0 is not used on the LM4F, passing 0 as the alt_func_num parameter will disable the alternate function of the given pins.

/* Mux PB0 and PB1 to AF1 (UART1 TX/RX in this case) */
gpio_set_af(GPIOB, 1, GPIO0 | GPIO1);

Changing configuration of special function pins

On the LM4F, the NMI and JTAG/SWD default to their alternate function. These pins cannot normally be committed to GPIO usage. To enable these special function pins to be used as GPIO, they must be unlocked. This may be achieved via gpio_unlock_commit. Once a special function pin is unlocked, its settings may be altered in the usual way.

For example, to unlock the PF0 pin (NMI on the LM4F120):

/* PF0 is an NMI pin, and needs to be unlocked */
gpio_unlock_commit(GPIOF, GPIO0);
/* Now the pin can be configured */
gpio_mode_setup(RGB_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, btnpins);