Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

This section describes the programming interface of the GPIO Peripheral driver.The GPIO Peripheral driver configures pins to digital input/output and provides API functions for input/output operations.

GPIO Pin Definitions

Define GPIO pins according to the target board configuration and ensure that the definitions are correct. Define a header file to store the GPIO pin names and the input/output configuration arrays defined in source files.
Include this header file in source files where you want to use GPIO driver to operate GPIO pins.

GPIO pin header file example:

// Feel free to change the pin names as what you want
enum _gpio_pins
{
kGpioLED1 = GPIO_MAKE_PIN(HW_PORTC, 0x0),
kGpioLED2 = GPIO_MAKE_PIN(HW_PORTC, 0x1),
kGpioLED3 = GPIO_MAKE_PIN(HW_PORTC, 0x2),
kGpioLED4 = GPIO_MAKE_PIN(HW_PORTC, 0x3),
};
// Extern input/output arrays defined in source files.
extern gpio_input_pin_t inputPin[];
extern gpio_output_pin_t outputPin[];

GPIO Initialization

  1. To initialize the GPIO driver, define two arrays, the gpio_input_pin_t inputPin[], and the gpio_output_pin_t outputPin[].
  2. Then, call the GPIO_DRV_Init() function and pass these two arrays.

This is an example of the inputPin and outputPin array definition:

#include "gpio/fsl_gpio_driver.h"
#include "gpio_pin_header_file.h"
// Configure kGpioPTA2 as a digital input and enable the interrupt on the rising edge.
gpio_input_pin_t inputPin[] = {
{
.pinName = kGpioPTA2,
.config.isPullEnable = false,
.config.pullSelect = kPortPullDown,
.config.isPassiveFilterEnabled = false,
.config.interrupt = kPortIntRisingEdge,
},
{
//Note: This pinName must be defined here to indicate end of array.
}
};
// Configure the kGpioLED4 and kGpioPTB9 as a digital output and enable the high drive strength.
gpio_output_pin_t outputPin[] = {
{
.pinName = kGpioLED4,
.config.outputLogic = 0,
.config.slewRate = kPortFastSlewRate,
.config.driveStrength = kPortHighDriveStrength,
.config.interrupt = kPortIntDisabled,
},
{
.pinName = kGpioPTB9,
.config.outputLogic = 0,
.config.slewRate = kPortFastSlewRate,
.config.driveStrength = kPortHighDriveStrength,
.config.interrupt = kPortIntDisabled,
},
{
//Note: This pinName must be defined here to indicate the end of array.
}
};
// Initializes GPIO pins.
GPIO_DRV_Init(inputPin, outputPin);

<note> If the digital filter is enabled, the digital filter clock source and width must be configured before calling the GPIO_DRV_Init function. To configure the digital filter, use this API function from porting the HAL driver. Each pin in the same port shares the same digital filter settings.</note>

void PORT_HAL_SetDigitalFilterClock(uint32_t baseAddr, port_digital_filter_clock_source_t clockSource);
void PORT_HAL_SetDigitalFilterWidth(uint32_t baseAddr, uint8_t width);

Output Operations

To use the output operation, configure the target GPIO pin as a digital output in the GPIO_DRV_Init function. The output operations include set, clear, and toggle of the output logic level. Three API functions are provided for these operations:

void GPIO_DRV_SetPinOutput(uint32_t pinName);
void GPIO_DRV_ClearPinOutput(uint32_t pinName);
void GPIO_DRV_TogglePinOutput(uint32_t pinName);

These functions are used when the logic level of the GPIO output is known.
Otherwise, a different function is provided to configure the output logic level according to a passed parameter:

void GPIO_DRV_WritePinOutput(uint32_t pinName, uint32_t output);

Use this function to change the GPIO output according to the results of other code. Pass an integer as the "uint32_t output" parameter. If that integer is not 0, it generates the high logic. If the integer is 0, it generates the low logic.

Input Operations

To use the input operation, configure the target GPIO pin as a digital input in the GPIO_DRV_Init function. For the input operation, this is the most commonly used API function:

uint32_t GPIO_DRV_ReadPinInput(uint32_t pinName);

This function returns the logic level read from a specific GPIO pin.
If the digital filter is enabled, use this function to disable it:

void GPIO_DRV_SetDigitalFilterCmd(uint32_t pinName, bool isDigitalFilterEnabled);

GPIO Interrupt

Enable a specific pin interrupt in GPIO initialization structures. Both output and input can trigger an interrupt. This API function clears the interrupt flag inside the interrupt handler:

void GPIO_DRV_ClearPinIntFlag(uint32_t pinName);

Files

file  fsl_gpio_driver.h
 The GPIO driver uses the virtual GPIO name rather than an actual port and a pin number.
 

Data Structures

struct  gpio_input_pin_t
 The GPIO input pin configuration structure. More...
 
struct  gpio_output_pin_t
 The GPIO output pin configuration structure. More...
 
struct  gpio_input_pin_user_config_t
 The GPIO input pin structure. More...
 
struct  gpio_output_pin_user_config_t
 The GPIO output pin structure. More...
 

Variables

GPIO_Type *const g_gpioBase [GPIO_INSTANCE_COUNT]
 Table of base addresses for GPIO instances. More...
 
PORT_Type *const g_portBase [PORT_INSTANCE_COUNT]
 Table of base addresses for PORT instances. More...
 

GPIO Pin Macros

#define GPIO_PINS_OUT_OF_RANGE   (0xFFFFFFFFU)
 Indicates the end of a pin configuration structure. More...
 
#define GPIO_PORT_SHIFT   (0x8U)
 Bits shifted for the GPIO port number. More...
 
#define GPIO_MAKE_PIN(r, p)   (((r)<< GPIO_PORT_SHIFT) | (p))
 Combines the port number and the pin number into a single scalar value. More...
 
#define GPIO_EXTRACT_PORT(v)   (((v) >> GPIO_PORT_SHIFT) & 0xFFU)
 Extracts the port number from a combined port and pin value. More...
 
#define GPIO_EXTRACT_PIN(v)   ((v) & 0xFFU)
 Extracts the pin number from a combined port and pin value. More...
 

Initialization

void GPIO_DRV_Init (const gpio_input_pin_user_config_t *inputPins, const gpio_output_pin_user_config_t *outputPins)
 Initializes all GPIO pins used by the board. More...
 
void GPIO_DRV_InputPinInit (const gpio_input_pin_user_config_t *inputPin)
 Initializes one GPIO input pin used by the board. More...
 
void GPIO_DRV_OutputPinInit (const gpio_output_pin_user_config_t *outputPin)
 Initializes one GPIO output pin used by the board. More...
 

Pin Direction

gpio_pin_direction_t GPIO_DRV_GetPinDir (uint32_t pinName)
 Gets the current direction of the individual GPIO pin. More...
 
void GPIO_DRV_SetPinDir (uint32_t pinName, gpio_pin_direction_t direction)
 Sets the current direction of the individual GPIO pin. More...
 

Output Operations

void GPIO_DRV_WritePinOutput (uint32_t pinName, uint32_t output)
 Sets the output level of the individual GPIO pin to the logic 1 or 0. More...
 
void GPIO_DRV_SetPinOutput (uint32_t pinName)
 Sets the output level of the individual GPIO pin to the logic 1. More...
 
void GPIO_DRV_ClearPinOutput (uint32_t pinName)
 Sets the output level of the individual GPIO pin to the logic 0. More...
 
void GPIO_DRV_TogglePinOutput (uint32_t pinName)
 Reverses current output logic of the individual GPIO pin. More...
 

Input Operations

uint32_t GPIO_DRV_ReadPinInput (uint32_t pinName)
 Reads the current input value of the individual GPIO pin. More...
 

Interrupt

bool GPIO_DRV_IsPinIntPending (uint32_t pinName)
 Reads the individual pin-interrupt status flag. More...
 
void GPIO_DRV_ClearPinIntFlag (uint32_t pinName)
 Clears the individual GPIO pin interrupt status flag. More...
 

Data Structure Documentation

struct gpio_input_pin_t

Although every pin is configurable, valid configurations depend on a specific device. Users should check the related reference manual to ensure that the specific feature is valid in an individual pin. A configuration of unavailable features is harmless, but takes no effect.

struct gpio_output_pin_t

Although every pin is configurable, valid configurations depend on a specific device. Users should check the related reference manual to ensure that the specific feature is valid in an individual pin. The configuration of unavailable features is harmless, but takes no effect.

Data Fields

uint32_t outputLogic
 Set default output logic. More...
 

Field Documentation

uint32_t gpio_output_pin_t::outputLogic
struct gpio_input_pin_user_config_t

Although the pinName is defined as a uint32_t type, values assigned to the pinName should be the enumeration names defined in the enum _gpio_pins.

Data Fields

uint32_t pinName
 Virtual pin name from enumeration defined by the user. More...
 
gpio_input_pin_t config
 Input pin configuration structure. More...
 

Field Documentation

uint32_t gpio_input_pin_user_config_t::pinName
gpio_input_pin_t gpio_input_pin_user_config_t::config
struct gpio_output_pin_user_config_t

Although the pinName is defined as a uint32_t type, values assigned to the pinName should be the enumeration names defined in the enum _gpio_pins.

Data Fields

uint32_t pinName
 Virtual pin name from enumeration defined by the user. More...
 
gpio_output_pin_t config
 Input pin configuration structure. More...
 

Field Documentation

uint32_t gpio_output_pin_user_config_t::pinName
gpio_output_pin_t gpio_output_pin_user_config_t::config

Macro Definition Documentation

#define GPIO_PINS_OUT_OF_RANGE   (0xFFFFFFFFU)
#define GPIO_PORT_SHIFT   (0x8U)
#define GPIO_MAKE_PIN (   r,
 
)    (((r)<< GPIO_PORT_SHIFT) | (p))
#define GPIO_EXTRACT_PORT (   v)    (((v) >> GPIO_PORT_SHIFT) & 0xFFU)
#define GPIO_EXTRACT_PIN (   v)    ((v) & 0xFFU)

Function Documentation

void GPIO_DRV_Init ( const gpio_input_pin_user_config_t inputPins,
const gpio_output_pin_user_config_t outputPins 
)

To initialize the GPIO driver, define two arrays similar to the gpio_input_pin_user_config_t inputPin[] array and the gpio_output_pin_user_config_t outputPin[] array in the user file. Then, call the GPIO_DRV_Init() function and pass in the two arrays. If the input or output pins are not needed, pass in a NULL.

This is an example to define an input pin array:

// Configure the kGpioPTA2 as digital input.
{
.pinName = kGpioPTA2,
.config.isPullEnable = false,
.config.pullSelect = kPortPullDown,
.config.isPassiveFilterEnabled = false,
.config.interrupt = kPortIntDisabled,
},
{
// Note: This pinName must be defined here to indicate the end of the array.
}
};
Parameters
inputPinsinput GPIO pins pointer.
outputPinsoutput GPIO pins pointer.
void GPIO_DRV_InputPinInit ( const gpio_input_pin_user_config_t inputPin)
Parameters
inputPininput GPIO pins pointer.
void GPIO_DRV_OutputPinInit ( const gpio_output_pin_user_config_t outputPin)
Parameters
outputPinoutput GPIO pins pointer.
gpio_pin_direction_t GPIO_DRV_GetPinDir ( uint32_t  pinName)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
Returns
GPIO directions.
  • kGpioDigitalInput: corresponding pin is set as digital input.
  • kGpioDigitalOutput: corresponding pin is set as digital output.
void GPIO_DRV_SetPinDir ( uint32_t  pinName,
gpio_pin_direction_t  direction 
)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
directionGPIO directions.
  • kGpioDigitalInput: corresponding pin is set as digital input.
  • kGpioDigitalOutput: corresponding pin is set as digital output.
void GPIO_DRV_WritePinOutput ( uint32_t  pinName,
uint32_t  output 
)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
outputpin output logic level.
  • 0: corresponding pin output low logic level.
  • Non-0: corresponding pin output high logic level.
void GPIO_DRV_SetPinOutput ( uint32_t  pinName)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
void GPIO_DRV_ClearPinOutput ( uint32_t  pinName)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
void GPIO_DRV_TogglePinOutput ( uint32_t  pinName)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
uint32_t GPIO_DRV_ReadPinInput ( uint32_t  pinName)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
Returns
GPIO port input value.
  • 0: Pin logic level is 0, or is not configured for use by digital function.
  • 1: Pin logic level is 1.
bool GPIO_DRV_IsPinIntPending ( uint32_t  pinName)

If a pin is configured to generate the DMA request, the corresponding flag is cleared automatically at the completion of the requested DMA transfer. Otherwise, the flag remains set until a logic one is written to that flag. If configured for a level sensitive interrupt that remains asserted, the flag is set again immediately.

Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.
Returns
current pin interrupt status flag
  • 0: interrupt is not detected.
  • 1: interrupt is detected.
void GPIO_DRV_ClearPinIntFlag ( uint32_t  pinName)
Parameters
pinNameGPIO pin name defined by the user in the GPIO pin enumeration list.

Variable Documentation

GPIO_Type* const g_gpioBase[GPIO_INSTANCE_COUNT]
PORT_Type* const g_portBase[PORT_INSTANCE_COUNT]