Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SAM Port (PORT) Driver

This driver for Atmel® | SMART ARM®-based microcontrollers provides an interface for the configuration and management of the device's General Purpose Input/Output (GPIO) pin functionality, for manual pin state reading and writing.

The following peripheral is used by this module:

The following devices can use this module:

The outline of this documentation is as follows:

Prerequisites

There are no prerequisites for this module.

Module Overview

The device GPIO (PORT) module provides an interface between the user application logic and external hardware peripherals, when general pin state manipulation is required. This driver provides an easy-to-use interface to the physical pin input samplers and output drivers, so that pins can be read from or written to for general purpose external hardware control.

Driver Feature Macro Definition

Driver Feature Macro Supported devices
FEATURE_PORT_INPUT_EVENT SAM L21/L22/C20/C21/R30/R34/R35
Note
The specific features are only available in the driver when the selected device supports those features.

Physical and Logical GPIO Pins

SAM devices use two naming conventions for the I/O pins in the device; one physical and one logical. Each physical pin on a device package is assigned both a physical port and pin identifier (e.g. "PORTA.0") as well as a monotonically incrementing logical GPIO number (e.g. "GPIO0"). While the former is used to map physical pins to their physical internal device module counterparts, for simplicity the design of this driver uses the logical GPIO numbers instead.

Physical Connection

The diagram below shows how this module is interconnected within the device.

Special Considerations

The SAM port pin input sampler can be disabled when the pin is configured in pure output mode to save power; reading the pin state of a pin configured in output-only mode will read the logical output state that was last set.

Extra Information

For extra information, see Extra Information for PORT Driver. This includes:

Examples

For a list of examples related to this driver, see Examples for PORT Driver.

API Overview

Data Structures

struct  port_config
 Port pin configuration structure. More...
 

Enumerations

enum  port_pin_dir {
  PORT_PIN_DIR_INPUT = SYSTEM_PINMUX_PIN_DIR_INPUT,
  PORT_PIN_DIR_OUTPUT = SYSTEM_PINMUX_PIN_DIR_OUTPUT,
  PORT_PIN_DIR_OUTPUT_WTH_READBACK = SYSTEM_PINMUX_PIN_DIR_OUTPUT_WITH_READBACK
}
 Port pin direction configuration enum. More...
 
enum  port_pin_pull {
  PORT_PIN_PULL_NONE = SYSTEM_PINMUX_PIN_PULL_NONE,
  PORT_PIN_PULL_UP = SYSTEM_PINMUX_PIN_PULL_UP,
  PORT_PIN_PULL_DOWN = SYSTEM_PINMUX_PIN_PULL_DOWN
}
 Port pin input pull configuration enum. More...
 

PORT Alias Macros

#define PORTA   PORT->Group[0]
 Convenience definition for GPIO module group A on the device (if available). More...
 
#define PORTB   PORT->Group[1]
 Convenience definition for GPIO module group B on the device (if available). More...
 
#define PORTC   PORT->Group[2]
 Convenience definition for GPIO module group C on the device (if available). More...
 
#define PORTD   PORT->Group[3]
 Convenience definition for GPIO module group D on the device (if available). More...
 

State Reading/Writing (Physical Group Orientated)

static PortGroup * port_get_group_from_gpio_pin (const uint8_t gpio_pin)
 Retrieves the PORT module group instance from a given GPIO pin number. More...
 
static uint32_t port_group_get_input_level (const PortGroup *const port, const uint32_t mask)
 Retrieves the state of a group of port pins that are configured as inputs. More...
 
static uint32_t port_group_get_output_level (const PortGroup *const port, const uint32_t mask)
 Retrieves the state of a group of port pins that are configured as outputs. More...
 
static void port_group_set_output_level (PortGroup *const port, const uint32_t mask, const uint32_t level_mask)
 Sets the state of a group of port pins that are configured as outputs. More...
 
static void port_group_toggle_output_level (PortGroup *const port, const uint32_t mask)
 Toggles the state of a group of port pins that are configured as an outputs. More...
 

Configuration and Initialization

static void port_get_config_defaults (struct port_config *const config)
 Initializes a Port pin/group configuration structure to defaults. More...
 
void port_pin_set_config (const uint8_t gpio_pin, const struct port_config *const config)
 Writes a Port pin configuration to the hardware module. More...
 
void port_group_set_config (PortGroup *const port, const uint32_t mask, const struct port_config *const config)
 Writes a Port group configuration group to the hardware module. More...
 

State Reading/Writing (Logical Pin Orientated)

static bool port_pin_get_input_level (const uint8_t gpio_pin)
 Retrieves the state of a port pin that is configured as an input. More...
 
static bool port_pin_get_output_level (const uint8_t gpio_pin)
 Retrieves the state of a port pin that is configured as an output. More...
 
static void port_pin_set_output_level (const uint8_t gpio_pin, const bool level)
 Sets the state of a port pin that is configured as an output. More...
 
static void port_pin_toggle_output_level (const uint8_t gpio_pin)
 Toggles the state of a port pin that is configured as an output. More...
 

#define PORTA   PORT->Group[0]

Convenience definition for GPIO module group A on the device (if available).

#define PORTB   PORT->Group[1]

Convenience definition for GPIO module group B on the device (if available).

#define PORTC   PORT->Group[2]

Convenience definition for GPIO module group C on the device (if available).

#define PORTD   PORT->Group[3]

Convenience definition for GPIO module group D on the device (if available).

Port pin direction configuration enum.

Enum for the possible pin direction settings of the port pin configuration structure, to indicate the direction the pin should use.

Enumerator
PORT_PIN_DIR_INPUT 

The pin's input buffer should be enabled, so that the pin state can be read.

PORT_PIN_DIR_OUTPUT 

The pin's output buffer should be enabled, so that the pin state can be set.

PORT_PIN_DIR_OUTPUT_WTH_READBACK 

The pin's output and input buffers should be enabled, so that the pin state can be set and read back.

Port pin input pull configuration enum.

Enum for the possible pin pull settings of the port pin configuration structure, to indicate the type of logic level pull the pin should use.

Enumerator
PORT_PIN_PULL_NONE 

No logical pull should be applied to the pin.

PORT_PIN_PULL_UP 

Pin should be pulled up when idle.

PORT_PIN_PULL_DOWN 

Pin should be pulled down when idle.

static void port_get_config_defaults ( struct port_config *const  config)
inlinestatic

Initializes a Port pin/group configuration structure to defaults.

Initializes a given Port pin/group configuration structure to a set of known default values. This function should be called on all new instances of these configuration structures before being modified by the user application.

The default configuration is as follows:

  • Input mode with internal pull-up enabled
Parameters
[out]configConfiguration structure to initialize to default values

References Assert, port_config::direction, port_config::input_pull, PORT_PIN_DIR_INPUT, PORT_PIN_PULL_UP, and port_config::powersave.

Referenced by usb_dual_enable().

static PortGroup* port_get_group_from_gpio_pin ( const uint8_t  gpio_pin)
inlinestatic

Retrieves the PORT module group instance from a given GPIO pin number.

Retrieves the PORT module group instance associated with a given logical GPIO pin number.

Parameters
[in]gpio_pinIndex of the GPIO pin to convert
Returns
Base address of the associated PORT module.

References system_pinmux_get_group_from_gpio_pin().

Referenced by port_pin_get_input_level(), port_pin_get_output_level(), port_pin_set_output_level(), and port_pin_toggle_output_level().

static uint32_t port_group_get_input_level ( const PortGroup *const  port,
const uint32_t  mask 
)
inlinestatic

Retrieves the state of a group of port pins that are configured as inputs.

Reads the current logic level of a port module's pins and returns the current levels as a bitmask.

Parameters
[in]portBase of the PORT module to read from
[in]maskMask of the port pin(s) to read
Returns
Status of the port pin(s) input buffers.

References Assert.

static uint32_t port_group_get_output_level ( const PortGroup *const  port,
const uint32_t  mask 
)
inlinestatic

Retrieves the state of a group of port pins that are configured as outputs.

Reads the current logical output level of a port module's pins and returns the current levels as a bitmask.

Parameters
[in]portBase of the PORT module to read from
[in]maskMask of the port pin(s) to read
Returns
Status of the port pin(s) output buffers.

References Assert.

void port_group_set_config ( PortGroup *const  port,
const uint32_t  mask,
const struct port_config *const  config 
)

Writes a Port group configuration group to the hardware module.

Writes out a given configuration of a Port group configuration to the hardware module.

Note
If the pin direction is set as an output, the pull-up/pull-down input configuration setting is ignored.
Parameters
[out]portBase of the PORT module to write to
[in]maskMask of the port pin(s) to configure
[in]configConfiguration settings for the pin group

References Assert, system_pinmux_config::direction, port_config::direction, system_pinmux_config::input_pull, port_config::input_pull, system_pinmux_config::mux_position, system_pinmux_config::powersave, port_config::powersave, system_pinmux_get_config_defaults(), SYSTEM_PINMUX_GPIO, and system_pinmux_group_set_config().

static void port_group_set_output_level ( PortGroup *const  port,
const uint32_t  mask,
const uint32_t  level_mask 
)
inlinestatic

Sets the state of a group of port pins that are configured as outputs.

Sets the current output level of a port module's pins to a given logic level.

Parameters
[out]portBase of the PORT module to write to
[in]maskMask of the port pin(s) to change
[in]level_maskMask of the port level(s) to set

References Assert.

static void port_group_toggle_output_level ( PortGroup *const  port,
const uint32_t  mask 
)
inlinestatic

Toggles the state of a group of port pins that are configured as an outputs.

Toggles the current output levels of a port module's pins.

Parameters
[out]portBase of the PORT module to write to
[in]maskMask of the port pin(s) to toggle

References Assert.

static bool port_pin_get_input_level ( const uint8_t  gpio_pin)
inlinestatic

Retrieves the state of a port pin that is configured as an input.

Reads the current logic level of a port pin and returns the current level as a Boolean value.

Parameters
[in]gpio_pinIndex of the GPIO pin to read
Returns
Status of the port pin's input buffer.

References port_get_group_from_gpio_pin().

static bool port_pin_get_output_level ( const uint8_t  gpio_pin)
inlinestatic

Retrieves the state of a port pin that is configured as an output.

Reads the current logical output level of a port pin and returns the current level as a Boolean value.

Parameters
[in]gpio_pinIndex of the GPIO pin to read
Returns
Status of the port pin's output buffer.

References port_get_group_from_gpio_pin().

void port_pin_set_config ( const uint8_t  gpio_pin,
const struct port_config *const  config 
)

Writes a Port pin configuration to the hardware module.

Writes out a given configuration of a Port pin configuration to the hardware module.

Note
If the pin direction is set as an output, the pull-up/pull-down input configuration setting is ignored.
Parameters
[in]gpio_pinIndex of the GPIO pin to configure
[in]configConfiguration settings for the pin

References Assert, system_pinmux_config::direction, port_config::direction, system_pinmux_config::input_pull, port_config::input_pull, system_pinmux_config::mux_position, system_pinmux_config::powersave, port_config::powersave, system_pinmux_get_config_defaults(), SYSTEM_PINMUX_GPIO, and system_pinmux_pin_set_config().

Referenced by usb_dual_enable().

static void port_pin_set_output_level ( const uint8_t  gpio_pin,
const bool  level 
)
inlinestatic

Sets the state of a port pin that is configured as an output.

Sets the current output level of a port pin to a given logic level.

Parameters
[in]gpio_pinIndex of the GPIO pin to write to
[in]levelLogical level to set the given pin to

References port_get_group_from_gpio_pin().

static void port_pin_toggle_output_level ( const uint8_t  gpio_pin)
inlinestatic

Toggles the state of a port pin that is configured as an output.

Toggles the current output level of a port pin.

Parameters
[in]gpio_pinIndex of the GPIO pin to toggle

References port_get_group_from_gpio_pin().