libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
|
Enabling and configuring GPIO pins More...
Macros | |
#define | PCTL_AF(pin, af) ((af) << ((pin) << 2)) |
#define | PCTL_MASK(pin) PCTL_AF((pin), 0xf) |
Functions | |
void | gpio_enable_ahb_aperture (void) |
Enable access to GPIO registers via the AHB aperture. More... | |
void | gpio_mode_setup (uint32_t gpioport, enum gpio_mode mode, enum gpio_pullup pullup, uint8_t gpios) |
Configure a group of pins. More... | |
void | gpio_set_output_config (uint32_t gpioport, enum gpio_output_type otype, enum gpio_drive_strength drive, uint8_t gpios) |
Configure output parameters of a group of pins. More... | |
void | gpio_set_af (uint32_t gpioport, uint8_t alt_func_num, uint8_t gpios) |
Multiplex group of pins to the given alternate function. More... | |
void | gpio_unlock_commit (uint32_t gpioport, uint8_t gpios) |
Unlock the commit control of a special function pin. More... | |
Enabling and configuring GPIO pins
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:
On reset all ports are configured as digital floating inputs (no pull-up or pull-down), except for special function pins.
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:
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.
For example, to set PA2 to output push-pull with a drive strength of 8mA:
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:
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.
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):
void gpio_enable_ahb_aperture | ( | void | ) |
Enable access to GPIO registers via the AHB aperture.
All GPIO registers are accessed in libopencm3 via the AHB aperture. It provides faster control over the older APB aperture. This aperture must be enabled before calling any other gpio_*() function.
Definition at line 186 of file gpio.c.
References SYSCTL_GPIOHBCTL.
void gpio_mode_setup | ( | uint32_t | gpioport, |
enum gpio_mode | mode, | ||
enum gpio_pullup | pullup, | ||
uint8_t | gpios | ||
) |
Configure a group of pins.
Sets the Pin direction, analog/digital mode, and pull-up configuration of or a set of GPIO pins on a given GPIO port.
[in] | gpioport | GPIO block register address base GPIO register base addresses |
[in] | mode | Pin mode (gpio_mode)
|
[in] | pullup | Pin pullup/pulldown configuration (gpio_pullup)
|
[in] | gpios | GPIO pin identifiers. Any combination of pins may be specified by OR'ing then together |
Definition at line 209 of file gpio.c.
References GPIO_AMSEL, GPIO_DEN, GPIO_DIR, GPIO_MODE_ANALOG, GPIO_MODE_INPUT, GPIO_MODE_OUTPUT, GPIO_PDR, GPIO_PUPD_NONE, GPIO_PUPD_PULLDOWN, GPIO_PUPD_PULLUP, and GPIO_PUR.
void gpio_set_af | ( | uint32_t | gpioport, |
uint8_t | alt_func_num, | ||
uint8_t | gpios | ||
) |
Multiplex group of pins to the given alternate function.
Mux the pin or group of pins to the given alternate function. Note that a number of pins may be set but only with a single AF number. This is useful when one or more of a peripheral's pins are assigned to the same alternate function.
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.
[in] | gpioport | GPIO block register address base GPIO register base addresses |
[in] | alt_func_num | Pin alternate function number or 0 to disable the alternate function multiplexing. |
[in] | gpios | GPIO pin identifiers. Any combination of pins may be specified by OR'ing then together |
Definition at line 321 of file gpio.c.
References GPIO_AFSEL, GPIO_DEN, GPIO_PCTL, and PCTL_AF.
void gpio_set_output_config | ( | uint32_t | gpioport, |
enum gpio_output_type | otype, | ||
enum gpio_drive_strength | drive, | ||
uint8_t | gpios | ||
) |
Configure output parameters of a group of pins.
Sets the output configuration and drive strength, of or a set of GPIO pins for a set of GPIO pins in output mode.
[in] | gpioport | GPIO block register address base GPIO register base addresses |
[in] | otype | Output driver configuration (gpio_output_type)
|
[in] | drive | Pin drive strength (gpio_drive_strength)
|
[in] | gpios | GPIO pin identifiers. Any combination of pins may be specified by OR'ing then together |
Definition at line 270 of file gpio.c.
References GPIO_DR2R, GPIO_DR4R, GPIO_DR8R, GPIO_DRIVE_2MA, GPIO_DRIVE_4MA, GPIO_DRIVE_8MA, GPIO_DRIVE_8MA_SLEW_CTL, GPIO_ODR, GPIO_OTYPE_OD, and GPIO_SLR.
void gpio_unlock_commit | ( | uint32_t | gpioport, |
uint8_t | gpios | ||
) |
Unlock the commit control of a special function pin.
Unlocks the commit control of the given pin or group of pins. If a pin is a JTAG/SWD or NMI, the pin may then be reconfigured as a GPIO pin. If the pin is not locked by default, this has no effect.
[in] | gpioport | GPIO block register address base GPIO register base addresses |
[in] | gpios | GPIO pin identifiers. Any combination of pins may be specified by OR'ing then together. |
Definition at line 365 of file gpio.c.
References GPIO_CR, GPIO_LOCK, and GPIO_LOCK_UNLOCK_CODE.