GPIO
The General-Purpose Input/Output (GPIO) driver provides API to configure general-purpose pins as either inputs or outputs. It also provided API to configure GPIO to produce host CPU interrupts and DMA synchronization events in different interrupt/event generation modes.
Features Supported
Supports up to 16 GPIO signals per bank
Supports up to 9 banks of interrupt and DMA trigger capable GPIOs
Set/clear functionality
SysConfig Features
Note
It is strongly recommend to use SysConfig where it is available instead of using direct SW API calls. This will help simplify the SW application and also catch common mistakes early in the development cycle.
Set pin direction: input or output
Set interrupt trigger type
Configuring pinmux based on selected pin
Features NOT Supported
NA
Important Usage Guidelines
Note: Not all GPIO pins, banks are present in a particular device. Refer device TRM for actual GPIO instances and pins supported
Example Usage
Include the below file to access the APIs
#include <drivers/gpio.h>
GPIO configuration as output
uint32_t pinNum = gGpioPinNum, pinValue;
GPIO_setDirMode(gGpioBaseAddr, pinNum, GPIO_DIRECTION_OUTPUT);
GPIO_pinWriteHigh(gGpioBaseAddr, pinNum);
ClockP_sleep(1);
GPIO_pinWriteLow(gGpioBaseAddr, pinNum);
/* Check output value */
pinValue = GPIO_pinOutValueRead(gGpioBaseAddr, pinNum);
if(pinValue != GPIO_PIN_LOW)
{
DebugP_assert(FALSE);
}
GPIO configuration as input
uint32_t pinNum = gGpioPinNum, pinValue;
GPIO_setDirMode(gGpioBaseAddr, pinNum, GPIO_DIRECTION_INPUT);
pinValue = GPIO_pinRead(gGpioBaseAddr, pinNum);
if(pinValue == GPIO_PIN_HIGH)
{
DebugP_log("Pin value is HIGH\r\n");
}
else
{
DebugP_log("Pin value is LOW\r\n");
}
GPIO configuration for bank interrupt
static void GPIO_bankIsrFxn(void *args)
{
uint32_t pinNum = (uint32_t) args, bankNum;
uint32_t intrStatus, pinMask = GPIO_GET_BANK_BIT_MASK(pinNum);
bankNum = GPIO_GET_BANK_INDEX(pinNum);
intrStatus = GPIO_getBankIntrStatus(gGpioBaseAddr, bankNum);
GPIO_clearBankIntrStatus(gGpioBaseAddr, bankNum, intrStatus);
if(intrStatus & pinMask)
{
/*
* Handle all the expected pin interrupts within a bank using intrStatus flag
*/
}
}
void gpio_bank_interrupt_init(void)
{
int32_t retVal;
uint32_t pinNum = gGpioPinNum, bankNum;
HwiP_Params hwiPrms;
bankNum = GPIO_GET_BANK_INDEX(pinNum);
/* Interrupt setup */
GPIO_setDirMode(gGpioBaseAddr, pinNum, GPIO_DIRECTION_INPUT);
GPIO_setTrigType(gGpioBaseAddr, pinNum, GPIO_TRIG_TYPE_RISE_EDGE);
GPIO_bankIntrEnable(gGpioBaseAddr, bankNum);
/* Register bank interrupt */
HwiP_Params_init(&hwiPrms);
hwiPrms.intNum = gGpioBankIntrNum;
hwiPrms.callback = &GPIO_bankIsrFxn;
hwiPrms.args = (void *) pinNum;
retVal = HwiP_construct(&gGpioHwiObject, &hwiPrms);
if(SystemP_SUCCESS != retVal)
{
DebugP_assert(FALSE);
}
}
void gpio_bank_interrupt_deinit(void)
{
uint32_t pinNum = gGpioPinNum, bankNum, intrStatus;
bankNum = GPIO_GET_BANK_INDEX(pinNum);
/* Interrupt disable and clear any pending interrupts */
GPIO_bankIntrDisable(gGpioBaseAddr, bankNum);
GPIO_setTrigType(gGpioBaseAddr, pinNum, GPIO_TRIG_TYPE_NONE);
intrStatus = GPIO_getBankIntrStatus(gGpioBaseAddr, bankNum);
GPIO_clearBankIntrStatus(gGpioBaseAddr, bankNum, intrStatus);
/* Unregister interrupt */
HwiP_destruct(&gGpioHwiObject);
}
GPIO configuration for per pin interrupt
static void GPIO_pinIsrFxn(void *args)
{
/*
* Handle pin interrupt - This is pulse interrupt. No need to clear status
*/
}
void gpio_pin_interrupt_init(void)
{
int32_t retVal;
uint32_t pinNum = gGpioPinNum, bankNum;
HwiP_Params hwiPrms;
bankNum = GPIO_GET_BANK_INDEX(pinNum);
/* Interrupt setup */
GPIO_setDirMode(gGpioBaseAddr, pinNum, GPIO_DIRECTION_INPUT);
GPIO_setTrigType(gGpioBaseAddr, pinNum, GPIO_TRIG_TYPE_RISE_EDGE);
GPIO_bankIntrEnable(gGpioBaseAddr, bankNum);
/* Register pin interrupt */
HwiP_Params_init(&hwiPrms);
hwiPrms.intNum = gGpioPinIntrNum;
hwiPrms.callback = &GPIO_pinIsrFxn;
hwiPrms.args = (void *) pinNum;
retVal = HwiP_construct(&gGpioHwiObject, &hwiPrms);
if(SystemP_SUCCESS != retVal)
{
DebugP_assert(FALSE);
}
}
void gpio_pin_interrupt_deinit(void)
{
uint32_t pinNum = gGpioPinNum, bankNum;
bankNum = GPIO_GET_BANK_INDEX(pinNum);
/* Interrupt disable and clear any pending interrupts */
GPIO_bankIntrDisable(gGpioBaseAddr, bankNum);
GPIO_setTrigType(gGpioBaseAddr, pinNum, GPIO_TRIG_TYPE_NONE);
GPIO_clearIntrStatus(gGpioBaseAddr, pinNum);
/* Unregister interrupt */
HwiP_destruct(&gGpioHwiObject);
}
API Reference
GPIO Driver API/interface file.
The GPIO pins are grouped into banks (16 pins per bank and 9 banks per GPIO instance). This means means that each GPIO module provides up to 144 dedicated general-purpose pins with input and output capabilities
Note: Not all GPIO pins and banks are present in a particular device. Refer device TRM for actual GPIO instances and pins supported
GPIO Trigger Type
-
GPIO_TRIG_TYPE_NONE
No interrupt request on either rising or falling edges on the pin.
-
GPIO_TRIG_TYPE_RISE_EDGE
Interrupt request on occurrence of a rising edge on the input pin.
-
GPIO_TRIG_TYPE_FALL_EDGE
Interrupt request on occurrence of a falling edge on the input pin.
-
GPIO_TRIG_TYPE_BOTH_EDGE
Interrupt request on occurrence of a rising/falling edge on the input pin.
Defines
-
GPIO_MAX_BANKS
Maximum number of banks per instance/module.
-
GPIO_MAX_PIN_PER_BANK
Maximum number of pins per bank.
-
GPIO_MAX_PIN_PER_INSTANCE
Maximum number of pins per instance/module.
-
GPIO_BANKS_PER_REG
Number of banks per register.
-
GPIO_PINS_PER_REG
Number of pins per register - 32 pins.
-
GPIO_PINS_PER_REG_SHIFT
Number of pins per register - shift value - used instead of divide operator.
-
GPIO_PINS_PER_BANK_SHIFT
Number of pins per bank - shift value - used instead of divide operator.
-
GPIO_GET_BANK_INDEX(pinNum)
Returns the bank index based on pin number.
-
GPIO_GET_REG_INDEX(pinNum)
Returns the register index based on pin number.
-
GPIO_GET_BIT_POS(pinNum)
Returns the bit position within a register based on pin number.
-
GPIO_GET_BIT_MASK(pinNum)
Returns the bit mask within a register based on pin number.
-
GPIO_GET_BANK_BIT_POS(pinNum)
Returns the bit position within a bank based on pin number.
-
GPIO_GET_BANK_BIT_MASK(pinNum)
Returns the bit mask within a bank based on pin number.
Functions
-
void GPIO_setDirMode(uint32_t baseAddr, uint32_t pinNum, uint32_t pinDir)
This API configures the direction of a specified GPIO pin as being either input or output.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
pinDir – The required direction for the GPIO pin. This can take one of the value from the following enum:
-
static inline void GPIO_pinWriteHigh(uint32_t baseAddr, uint32_t pinNum)
This API drives an output GPIO pin to a logic HIGH state.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
-
static inline void GPIO_pinWriteLow(uint32_t baseAddr, uint32_t pinNum)
This API drives an output GPIO pin to a logic LOW state.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
-
uint32_t GPIO_pinRead(uint32_t baseAddr, uint32_t pinNum)
This API reads the logic level(value) on a specified GPIO pin.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
- Returns:
-
uint32_t GPIO_pinOutValueRead(uint32_t baseAddr, uint32_t pinNum)
This API determines the output logic level(value) on a specified GPIO pin.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
- Returns:
-
void GPIO_setTrigType(uint32_t baseAddr, uint32_t pinNum, uint32_t trigType)
This API configures the trigger type for a specified input GPIO pin.
Whenever the selected trigger occurs on that GPIO pin and if interrupt generation is enabled for that pin, the GPIO module will send an interrupt to CPU. To disable the per pin event/interrupt generation, set the trigger type to #GPIO_TRIG_TYPE_NONE
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= 63).
trigType – This specifies the trigger type on whose detection, the GPIO module will send an interrupt to CPU, provided interrupt generation for that pin is enabled. ‘trigType’ can take one of the following values following enum:
-
void GPIO_bankIntrEnable(uint32_t baseAddr, uint32_t bankNum)
This API enables the bank interrupt. This has to be called after setting all the GPIO pin triggers of a bank to get interrupt.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
bankNum – The bank index ‘bankNum’ can take one of the following values: (0 <= bankNum <= GPIO_MAX_BANKS - 1)
-
void GPIO_bankIntrDisable(uint32_t baseAddr, uint32_t bankNum)
This API disables the bank interrupt.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
bankNum – The bank index ‘bankNum’ can take one of the following values: (0 <= bankNum <= GPIO_MAX_BANKS - 1)
-
static inline uint32_t GPIO_getIntrStatus(uint32_t baseAddr, uint32_t pinNum)
This API determines the enabled interrupt status of a specified pin.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
- Returns:
intrStatus The enabled interrupt status of the pin on the specified interrupt line. 1 - interrupt occurred 0 - interrupt hasnt occurred since last cleared writing 1 clears the corresponding interrupt status
-
static inline void GPIO_clearIntrStatus(uint32_t baseAddr, uint32_t pinNum)
This API clears the enabled interrupt status of a specified GPIO pin.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
pinNum – The number of the pin in the GPIO instance ‘pinNum’ can take one of the following values: (0 <= pinNum <= GPIO_MAX_PIN_PER_INSTANCE - 1)
-
static inline uint32_t GPIO_getBankIntrStatus(uint32_t baseAddr, uint32_t bankNum)
This API returns the interrupt status of the specified bank.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
bankNum – Bank index of the GPIO instance.
- Returns:
Raw interrupt status for the provided register index
-
static inline void GPIO_clearBankIntrStatus(uint32_t baseAddr, uint32_t bankNum, uint32_t intrStatus)
This API clears the interrupt status of the specified bank.
- Parameters:
baseAddr – The memory address of the GPIO instance being used
bankNum – Bank index of the GPIO instance.
intrStatus – Interrupt mask to clear the status