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

Cortex-M4 specific GPIO HAL interface. More...

#include "utils/gpio_types.h"
Include dependency graph for gpio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void hal_gpio_setmode (hal_gpio_pin pin, hal_gpio_mode mode, hal_gpio_pullup_pulldown pupd)
 Set the mode of 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 state to a GPIO pin.
hal_gpio_state hal_gpio_digitalread (hal_gpio_pin pin)
 Read the digital state from a GPIO pin.
void hal_gpio_enable_rcc (hal_gpio_pin pin)
 Enable the RCC clock for a specific GPIO pin's port.
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.
void hal_gpio_set_output_type (hal_gpio_pin pin, hal_gpio_output_type otyper)
 Set the output type for a GPIO pin.
void hal_gpio_set_output_speed (hal_gpio_pin pin, hal_gpio_output_speed speed)
 Set the output speed for a GPIO pin.

Detailed Description

Cortex-M4 specific GPIO HAL interface.

This header defines the function prototypes for configuring and controlling GPIO pins on Cortex-M4 microcontrollers. Functions include setting pin modes, reading and writing digital states, enabling clocks, and configuring alternate functions, output type, and speed.

Function Documentation

◆ hal_gpio_digitalread()

hal_gpio_state hal_gpio_digitalread ( hal_gpio_pin pin)

Read the digital state from a GPIO pin.

Parameters
pinThe GPIO pin to read.
Returns
The current digital state of the pin (HIGH/LOW).

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 state to a GPIO pin.

Parameters
pinThe GPIO pin to write.
stateThe digital state to set (HIGH/LOW).

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 specific GPIO pin's port.

Parameters
pinThe GPIO pin whose port clock is to be enabled.

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
pinThe GPIO pin to query.
Returns
The current mode of the 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 )

Set the alternate function for a GPIO pin.

Parameters
pinThe GPIO pin to configure.
alt_fnThe alternate function to assign to the 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 )

Set the output speed for a GPIO pin.

Parameters
pinThe GPIO pin to configure.
speedThe output speed (low, medium, high, very high).

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 )

Set the output type for a GPIO pin.

Parameters
pinThe GPIO pin to configure.
otyperThe output type (push-pull or open-drain).

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 )

Set the mode of a GPIO pin.

Parameters
pinThe GPIO pin to configure.
modeThe desired mode (input, output, alternate function, analog).
pupdPull-up/pull-down configuration for the 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