NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
gpio.c File Reference

HAL GPIO driver implementation for STM32F4 series. More...

Include dependency graph for gpio.c:

Functions

void hal_gpio_setmode (hal_gpio_pin pin, hal_gpio_mode mode, hal_gpio_pullup_pulldown pupd)
 Configure the mode and pull-up/pull-down for a GPIO pin.
hal_gpio_mode hal_gpio_getmode (hal_gpio_pin pin)
 Get the current mode of a GPIO pin.
void hal_gpio_digitalwrite (hal_gpio_pin pin, hal_gpio_state state)
 Write a digital value to a GPIO pin.
hal_gpio_state hal_gpio_digitalread (hal_gpio_pin pin)
 Read the digital value from a GPIO pin.
void hal_gpio_enable_rcc (hal_gpio_pin pin)
 Enable the RCC clock for a GPIO port.
void hal_gpio_set_alternate_function (hal_gpio_pin pin, hal_gpio_alternate_function_t alt_fn)
 Configure alternate function for a GPIO pin.
void hal_gpio_set_output_type (hal_gpio_pin pin, hal_gpio_output_type otyper)
 Configure GPIO output type.
void hal_gpio_set_output_speed (hal_gpio_pin pin, hal_gpio_output_speed speed)
 Configure GPIO output speed.

Detailed Description

HAL GPIO driver implementation for STM32F4 series.

This file contains the implementation of GPIO hardware abstraction layer functions for STM32F4 microcontrollers. It provides complete GPIO pin configuration and control capabilities including:

  • Pin mode configuration (input/output/alternate/analog)
  • Digital input/output operations
  • Alternate function selection
  • Output speed and type configuration
  • Built-in pull-up/pull-down resistor control

All functions handle the required RCC clock enabling automatically.

Function Documentation

◆ hal_gpio_digitalread()

hal_gpio_state hal_gpio_digitalread ( hal_gpio_pin pin)

Read the digital value from a GPIO pin.

Read the digital state from a GPIO pin.

Parameters
pinPin identifier to read
Returns
Current digital state of the pin (HIGH or LOW)

Reads the IDR register for the specified pin and returns the current input state.

< GPIOA base address

◆ hal_gpio_digitalwrite()

void hal_gpio_digitalwrite ( hal_gpio_pin pin,
hal_gpio_state state )

Write a digital value to a GPIO pin.

Write a digital state to a GPIO pin.

Parameters
pinPin identifier to control
stateDesired output state (HIGH or LOW)

Uses the BSRR register for atomic set/reset operations to avoid read-modify-write sequences.

// Example: Set PA5 high
hal_gpio_digitalwrite(PA5, HAL_GPIO_HIGH);
void hal_gpio_digitalwrite(hal_gpio_pin pin, hal_gpio_state state)
Write a digital state to a GPIO pin.
Definition gpio.c:84

< GPIOA base address

< GPIOA base address

◆ hal_gpio_enable_rcc()

void hal_gpio_enable_rcc ( hal_gpio_pin pin)

Enable the RCC clock for a GPIO port.

Enable the RCC clock for a specific GPIO pin's port.

Parameters
pinPin identifier whose port clock should be enabled

Enables the AHB1 peripheral clock for the GPIO port containing the specified pin. This is automatically called by other GPIO functions when needed.

◆ hal_gpio_getmode()

hal_gpio_mode hal_gpio_getmode ( hal_gpio_pin pin)

Get the current mode of a GPIO pin.

Parameters
pinPin identifier to query
Returns
Current GPIO mode of the specified pin

Reads the MODER register for the specified pin and returns the current mode configuration.

< GPIOA base address

◆ hal_gpio_set_alternate_function()

void hal_gpio_set_alternate_function ( hal_gpio_pin pin,
hal_gpio_alternate_function_t alt_fn )

Configure alternate function for a GPIO pin.

Set the alternate function for a GPIO pin.

Parameters
pinPin identifier to configure
alt_fnAlternate function number/type to assign

Configures the specified pin for alternate function mode and sets the appropriate alternate function selection in AFRL/AFRH. Automatically enables the GPIO port clock if needed.

// Example: Set PA2 to USART2_TX alternate function
hal_gpio_set_alternate_function(PA2, HAL_GPIO_AF_USART2);
void hal_gpio_set_alternate_function(hal_gpio_pin pin, hal_gpio_alternate_function_t alt_fn)
Set the alternate function for a GPIO pin.
Definition gpio.c:133

< GPIOA base address

< GPIOA base address

< GPIOA base address

< GPIOA base address

◆ hal_gpio_set_output_speed()

void hal_gpio_set_output_speed ( hal_gpio_pin pin,
hal_gpio_output_speed speed )

Configure GPIO output speed.

Set the output speed for a GPIO pin.

Parameters
pinPin identifier to configure
speedDesired output speed (low/medium/high/very high)

Sets the output speed in the OSPEEDR register for the specified pin. Does not change the pin mode - must be configured separately.

< GPIOA base address

< GPIOA base address

◆ hal_gpio_set_output_type()

void hal_gpio_set_output_type ( hal_gpio_pin pin,
hal_gpio_output_type otyper )

Configure GPIO output type.

Set the output type for a GPIO pin.

Parameters
pinPin identifier to configure
otyperOutput type (push-pull or open-drain)

Sets the output type in the OTYPER register for the specified pin. Does not change the pin mode - must be configured separately.

< GPIOA base address

< GPIOA base address

◆ hal_gpio_setmode()

void hal_gpio_setmode ( hal_gpio_pin pin,
hal_gpio_mode mode,
hal_gpio_pullup_pulldown pupd )

Configure the mode and pull-up/pull-down for a GPIO pin.

Set the mode of a GPIO pin.

Parameters
pinPin identifier (e.g., PA5, PC13)
modeDesired pin mode (input/output/alternate/analog)
pupdPull-up/pull-down configuration

This function:

  1. Enables the GPIO port clock via RCC
  2. Configures the pin mode in MODER register
  3. Sets pull-up/pull-down resistors in PUPDR register
Note
Automatically enables the GPIO port clock if not already enabled
// Example: Configure PA5 as output with no pull-up/pull-down
hal_gpio_setmode(PA5, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_NO_PULL);
void hal_gpio_setmode(hal_gpio_pin pin, hal_gpio_mode mode, hal_gpio_pullup_pulldown pupd)
Set the mode of a GPIO pin.
Definition gpio.c:41

< GPIOA base address

< GPIOA base address

< GPIOA base address

< GPIOA base address