px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2

Description

Driver to communicate with GPIO slaves.

File(s):

Example:
#include "px_gpio.h"
// LED is on PORT B, pin 0, configured as an output, initally off
#define PX_GPIO_LED PX_GPIO(B, 0, PX_GPIO_DIR_OUT, PX_GPIO_INIT_LO)
// Push Button is on PORT D, pin 7, configured as an input, pull-up enabled
#define PX_GPIO_PB PX_GPIO(D, 7, PX_GPIO_DIR_IN, PX_GPIO_INIT_PULL_UP)
int main(void)
{
// Initialise pins
PX_GPIO_INIT(PX_GPIO_LED);
PX_GPIO_INIT(PX_GPIO_PB);
// Loop forever
while(true)
{
// Is button being pressed?
if(PX_GPIO_IN_IS_LO(PX_GPIO_PB))
{
// Enable LED
PX_GPIO_OUT_SET_HI(PX_GPIO_LED);
}
else
{
// Disable LED
PX_GPIO_OUT_SET_LO(PX_GPIO_LED);
}
}
}
#define PX_GPIO_OUT_SET_HI(gpio)
Set GPIO pin output high.
Definition: px_gpio.h:218
#define PX_GPIO_INIT(gpio)
Initialise a GPIO pin.
Definition: px_gpio.h:175
#define PX_GPIO_IN_IS_LO(gpio)
Test if GPIO pin input is low.
Definition: px_gpio.h:248
#define PX_GPIO_OUT_SET_LO(gpio)
Set GPIO pin output low.
Definition: px_gpio.h:223

Data Structures

struct  px_gpio_handle_t
 GPIO pin handle definition. More...
 
struct  px_gpio_port_init_t
 GPIO register values to intialise a port. More...
 

Macros

#define PX_GPIO(port, pin, mode, output_type, speed, pull, output_init, alt_fn)    GPIO ## port, pin, mode, output_type, speed, pull, output_init, alt_fn
 Helper macro to define a GPIO pin. More...
 
#define PX_GPIO_INIT(gpio)   _GPIO_INIT(gpio)
 Initialise a GPIO pin. More...
 
#define PX_GPIO_OUT_SET_HI(gpio)   _GPIO_OUT_SET_HI(gpio)
 Set GPIO pin output high. More...
 
#define PX_GPIO_OUT_SET_LO(gpio)   _GPIO_PIN_OUT_SET_LO(gpio)
 Set GPIO pin output low. More...
 
#define PX_GPIO_OUT_TOGGLE(gpio)   _GPIO_OUT_TOGGLE(gpio)
 Toggle GPIO pin output. More...
 
#define PX_GPIO_OUT_IS_HI(gpio)   _GPIO_OUT_IS_HI(gpio)
 Test if GPIO pin output is set high. More...
 
#define PX_GPIO_OUT_IS_LO(gpio)   _GPIO_OUT_IS_LO(gpio)
 Test if GPIO pin output is set low. More...
 
#define PX_GPIO_IN_IS_HI(gpio)   _GPIO_IN_IS_HI(gpio)
 Test if GPIO pin input is high. More...
 
#define PX_GPIO_IN_IS_LO(gpio)   _GPIO_IN_IS_LO(gpio)
 Test if GPIO pin input is low. More...
 
#define PX_GPIO_DIR_SET_OUT(gpio)   _GPIO_DIR_SET_OUT(gpio)
 Set GPIO pin direction to output. More...
 
#define PX_GPIO_DIR_SET_IN(gpio)   _GPIO_DIR_SET_IN(gpio)
 Set GPIO pin direction to input. More...
 
#define PX_GPIO_DIR_IS_OUT(gpio)   _GPIO_DIR_IS_OUT(gpio)
 Test if GPIO pin is configured to be an output. More...
 
#define PX_GPIO_DIR_IS_IN(gpio)   _GPIO_DIR_IS_IN(gpio)
 Test if GPIO pin is configured to be an input. More...
 
#define PX_GPIO_PULL_UP_ENABLE(gpio)   _GPIO_PULL_UP_ENABLE(gpio)
 Enable pull-up on GPIO pin. More...
 
#define PX_GPIO_PULL_DN_ENABLE(gpio)   _GPIO_PULL_DN_ENABLE(gpio)
 Enable pull-down on GPIO pin. More...
 
#define PX_GPIO_PULL_DISABLE(gpio)   _GPIO_PULL_DISABLE(gpio)
 Disable pull-up / pull-down on GPIO pin. More...
 
#define PX_GPIO_REG_MODER_INIT(gpio)   _PX_GPIO_REG_MODER_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_MODER register. More...
 
#define PX_GPIO_REG_OTYPER_INIT(gpio)   _PX_GPIO_REG_OTYPER_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_OTYPER register. More...
 
#define PX_GPIO_REG_OSPEEDR_INIT(gpio)   _PX_GPIO_REG_OSPEEDR_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_OSPEEDR register. More...
 
#define PX_GPIO_REG_PUPDR_INIT(gpio)   _PX_GPIO_REG_PUPDR_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_PUPDR register. More...
 
#define PX_GPIO_REG_ODR_INIT(gpio)   _PX_GPIO_REG_ODR_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_OTYPER register. More...
 
#define PX_GPIO_REG_AFRL_INIT(gpio)   _PX_GPIO_REG_AFRL_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_AFRL register (pins 0 to 7) More...
 
#define PX_GPIO_REG_AFRH_INIT(gpio)   _PX_GPIO_REG_AFRH_INIT(gpio)
 Macro to calculate bit mask used to initialise GPIOx_AFRH register (pins 8 to 15) More...
 

Enumerations

enum  px_gpio_mode_t {
  PX_GPIO_MODE_IN = 0 , PX_GPIO_MODE_OUT = 1 , PX_GPIO_MODE_AF = 2 , PX_GPIO_MODE_ANA = 3 ,
  PX_GPIO_MODE_DEF = 3 , PX_GPIO_MODE_BIT_MASK = 0x3
}
 GPIO mode selection; See GPIOx_MODER register. More...
 
enum  px_gpio_otype_t { PX_GPIO_OTYPE_NA = 0 , PX_GPIO_OTYPE_PP = 0 , PX_GPIO_OTYPE_OD = 1 , PX_GPIO_OTYPE_BIT_MASK = 0x1 }
 GPIO output type selection; See GPIOx_OTYPER register. More...
 
enum  px_gpio_ospeed_t {
  PX_GPIO_OSPEED_NA = 0 , PX_GPIO_OSPEED_LO = 0 , PX_GPIO_OSPEED_MD = 1 , PX_GPIO_OSPEED_HI = 2 ,
  PX_GPIO_OSPEED_VH = 3 , PX_GPIO_OSPEED_BIT_MASK = 0x3
}
 GPIO output speed selection; See GPIOx_OSPEEDR register. More...
 
enum  px_gpio_pull_t { PX_GPIO_PULL_NO = 0 , PX_GPIO_PULL_UP = 1 , PX_GPIO_PULL_DN = 2 , PX_GPIO_PULL_BIT_MASK = 0x3 }
 GPIO pull-up / pull-down selection; See GPIOx_PUPDR register. More...
 
enum  px_gpio_out_init_t { PX_GPIO_OUT_INIT_NA = 0 , PX_GPIO_OUT_INIT_LO = 0 , PX_GPIO_OUT_INIT_HI = 1 }
 GPIO initial output selection; See GPIOx_ODR register. More...
 
enum  px_gpio_af_t {
  PX_GPIO_AF_NA = 0 , PX_GPIO_AF_0 = 0 , PX_GPIO_AF_1 = 1 , PX_GPIO_AF_2 = 2 ,
  PX_GPIO_AF_3 = 3 , PX_GPIO_AF_4 = 4 , PX_GPIO_AF_5 = 5 , PX_GPIO_AF_6 = 6 ,
  PX_GPIO_AF_7 = 7 , PX_GPIO_ALT_FN_BIT_MASK = 0xf
}
 GPIO alternate function selection; See GPIOx_AFRL and GPIOx_AFRH register. More...
 

Functions

void px_gpio_port_init (const px_gpio_port_init_t *init)
 Initialise a GPIO port. More...
 
void px_gpio_open2 (px_gpio_handle_t *gpio, GPIO_TypeDef *gpio_base_reg, uint8_t pin)
 Open a GPIO handle and initialize with current pin configuration. More...
 
void px_gpio_init (const px_gpio_handle_t *gpio)
 Initialise a GPIO pin using supplied handle. More...
 

Data Structure Documentation

◆ px_gpio_handle_t

struct px_gpio_handle_t

GPIO pin handle definition.

Definition at line 113 of file px_gpio.h.

Data Fields
GPIO_TypeDef * gpio_base_reg GPIO peripheral base register address.
uint8_t pin Pin: 0, 1, 2, ..., or 15.
px_gpio_mode_t mode Mode: Input, Output, Alternative Function or Analog.
px_gpio_otype_t otype Output type: push-pull or open-drain.
px_gpio_ospeed_t ospeed Output speed: low, medium, high or very high.
px_gpio_pull_t pull None, pull-up or pull-down.
px_gpio_out_init_t out_init Initial output value: Low (0) or high (1)
px_gpio_af_t af Alternative function: AF0, AF1, ... or AF7.
px_gpio_reg_t port PORTx register address.
px_gpio_reg_t ddr DDRx register address.
px_gpio_reg_t pin PINx register address.
uint8_t bit Bit: 0, 1, 2, 3, 4, 5, 6 or 7.
uint8_t dir Direction: PX_GPIO_DIR_OUT or PX_GPIO_DIR_IN.
uint8_t init Initial value: PX_GPIO_INIT_HI, PX_GPIO_INIT_LO, PX_GPIO_INIT_HIZ or PX_GPIO_INIT_PULL_UP.

◆ px_gpio_port_init_t

struct px_gpio_port_init_t

GPIO register values to intialise a port.

Definition at line 126 of file px_gpio.h.

Data Fields
GPIO_TypeDef * gpio_base_reg GPIO peripheral base register address.
uint32_t moder Mode register value.
uint32_t otyper Output type register value.
uint32_t ospeedr Output speed register value.
uint32_t pupdr Pull up / pull-down register value.
uint32_t odr Output data register value.
uint32_t afrl Alternative function low register value.
uint32_t afrh Alternative function high register value.

Macro Definition Documentation

◆ PX_GPIO

#define PX_GPIO (   port,
  pin,
  mode,
  output_type,
  speed,
  pull,
  output_init,
  alt_fn 
)     GPIO ## port, pin, mode, output_type, speed, pull, output_init, alt_fn

Helper macro to define a GPIO pin.

Usage example:

// LED is an output pin on port D, pin 7, initially low
#define PX_GPIO_LED PX_GPIO(D, 7, PX_GPIO_MODE_OUT, \
PX_GPIO_OTYPE_PUSH_PULL, \
PX_GPIO_OSPEED_LOW, \
PX_GPIO_PULL_NONE, \
PX_GPIO_OUT_INIT_LO, \
PX_GPIO_ALT_FN_NA)
// PX_GPIO_LED will be replaced by the C preprocessor with the following
// sequence:
// GPIOD, 7, PX_GPIO_MODE_OUT, PX_GPIO_OTYPE_PUSH_PULL,
// PX_GPIO_OSPEED_LOW, PX_GPIO_PULL_NONE, PX_GPIO_OUT_INIT_LO,
// PX_GPIO_ALT_FN_NA
Parameters
portA, B, C, D, E or H
pin0, 1, 2, ..., or 15
modeInput, Output, Alternative Function or Analog;
See also
px_gpio_mode_t
Parameters
output_typepush-pull or open-drain;
See also
px_gpio_output_type_t
Parameters
speedlow, medium, high or very high;
See also
px_gpio_output_speed_t
Parameters
pullNone, pull-up or pull-down;
See also
px_gpio_pull_t
Parameters
output_initLow (0) or high (1);
See also
px_gpio_output_init_t
Parameters
alt_fnAlternative Function 0 to 7 or Not Applicable

Definition at line 171 of file px_gpio.h.

◆ PX_GPIO_INIT

#define PX_GPIO_INIT (   gpio)    _GPIO_INIT(gpio)

Initialise a GPIO pin.

Definition at line 175 of file px_gpio.h.

◆ PX_GPIO_OUT_SET_HI

#define PX_GPIO_OUT_SET_HI (   gpio)    _GPIO_OUT_SET_HI(gpio)

Set GPIO pin output high.

Definition at line 218 of file px_gpio.h.

◆ PX_GPIO_OUT_SET_LO

#define PX_GPIO_OUT_SET_LO (   gpio)    _GPIO_PIN_OUT_SET_LO(gpio)

Set GPIO pin output low.

Definition at line 223 of file px_gpio.h.

◆ PX_GPIO_OUT_TOGGLE

#define PX_GPIO_OUT_TOGGLE (   gpio)    _GPIO_OUT_TOGGLE(gpio)

Toggle GPIO pin output.

Definition at line 228 of file px_gpio.h.

◆ PX_GPIO_OUT_IS_HI

#define PX_GPIO_OUT_IS_HI (   gpio)    _GPIO_OUT_IS_HI(gpio)

Test if GPIO pin output is set high.

Definition at line 233 of file px_gpio.h.

◆ PX_GPIO_OUT_IS_LO

#define PX_GPIO_OUT_IS_LO (   gpio)    _GPIO_OUT_IS_LO(gpio)

Test if GPIO pin output is set low.

Definition at line 238 of file px_gpio.h.

◆ PX_GPIO_IN_IS_HI

#define PX_GPIO_IN_IS_HI (   gpio)    _GPIO_IN_IS_HI(gpio)

Test if GPIO pin input is high.

Definition at line 243 of file px_gpio.h.

◆ PX_GPIO_IN_IS_LO

#define PX_GPIO_IN_IS_LO (   gpio)    _GPIO_IN_IS_LO(gpio)

Test if GPIO pin input is low.

Definition at line 248 of file px_gpio.h.

◆ PX_GPIO_DIR_SET_OUT

#define PX_GPIO_DIR_SET_OUT (   gpio)    _GPIO_DIR_SET_OUT(gpio)

Set GPIO pin direction to output.

Definition at line 253 of file px_gpio.h.

◆ PX_GPIO_DIR_SET_IN

#define PX_GPIO_DIR_SET_IN (   gpio)    _GPIO_DIR_SET_IN(gpio)

Set GPIO pin direction to input.

Definition at line 258 of file px_gpio.h.

◆ PX_GPIO_DIR_IS_OUT

#define PX_GPIO_DIR_IS_OUT (   gpio)    _GPIO_DIR_IS_OUT(gpio)

Test if GPIO pin is configured to be an output.

Definition at line 263 of file px_gpio.h.

◆ PX_GPIO_DIR_IS_IN

#define PX_GPIO_DIR_IS_IN (   gpio)    _GPIO_DIR_IS_IN(gpio)

Test if GPIO pin is configured to be an input.

Definition at line 268 of file px_gpio.h.

◆ PX_GPIO_PULL_UP_ENABLE

#define PX_GPIO_PULL_UP_ENABLE (   gpio)    _GPIO_PULL_UP_ENABLE(gpio)

Enable pull-up on GPIO pin.

Definition at line 273 of file px_gpio.h.

◆ PX_GPIO_PULL_DN_ENABLE

#define PX_GPIO_PULL_DN_ENABLE (   gpio)    _GPIO_PULL_DN_ENABLE(gpio)

Enable pull-down on GPIO pin.

Definition at line 278 of file px_gpio.h.

◆ PX_GPIO_PULL_DISABLE

#define PX_GPIO_PULL_DISABLE (   gpio)    _GPIO_PULL_DISABLE(gpio)

Disable pull-up / pull-down on GPIO pin.

Definition at line 283 of file px_gpio.h.

◆ PX_GPIO_REG_MODER_INIT

#define PX_GPIO_REG_MODER_INIT (   gpio)    _PX_GPIO_REG_MODER_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_MODER register.

Definition at line 288 of file px_gpio.h.

◆ PX_GPIO_REG_OTYPER_INIT

#define PX_GPIO_REG_OTYPER_INIT (   gpio)    _PX_GPIO_REG_OTYPER_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_OTYPER register.

Definition at line 293 of file px_gpio.h.

◆ PX_GPIO_REG_OSPEEDR_INIT

#define PX_GPIO_REG_OSPEEDR_INIT (   gpio)    _PX_GPIO_REG_OSPEEDR_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_OSPEEDR register.

Definition at line 298 of file px_gpio.h.

◆ PX_GPIO_REG_PUPDR_INIT

#define PX_GPIO_REG_PUPDR_INIT (   gpio)    _PX_GPIO_REG_PUPDR_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_PUPDR register.

Definition at line 303 of file px_gpio.h.

◆ PX_GPIO_REG_ODR_INIT

#define PX_GPIO_REG_ODR_INIT (   gpio)    _PX_GPIO_REG_ODR_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_OTYPER register.

Definition at line 308 of file px_gpio.h.

◆ PX_GPIO_REG_AFRL_INIT

#define PX_GPIO_REG_AFRL_INIT (   gpio)    _PX_GPIO_REG_AFRL_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_AFRL register (pins 0 to 7)

Definition at line 313 of file px_gpio.h.

◆ PX_GPIO_REG_AFRH_INIT

#define PX_GPIO_REG_AFRH_INIT (   gpio)    _PX_GPIO_REG_AFRH_INIT(gpio)

Macro to calculate bit mask used to initialise GPIOx_AFRH register (pins 8 to 15)

Definition at line 318 of file px_gpio.h.

Enumeration Type Documentation

◆ px_gpio_mode_t

GPIO mode selection; See GPIOx_MODER register.

Enumerator
PX_GPIO_MODE_IN 

Set pin mode to input.

PX_GPIO_MODE_OUT 

Set pin mode to output.

PX_GPIO_MODE_AF 

Set pin mode to alternative function.

PX_GPIO_MODE_ANA 

Set pin mode to analog.

PX_GPIO_MODE_DEF 

Set pin mode to default (analog)

PX_GPIO_MODE_BIT_MASK 

Pin mode bit mask.

Definition at line 50 of file px_gpio.h.

◆ px_gpio_otype_t

GPIO output type selection; See GPIOx_OTYPER register.

Enumerator
PX_GPIO_OTYPE_NA 

Output type is not applicable (pin mode is input or analog)

PX_GPIO_OTYPE_PP 

Set output type to push-pull.

PX_GPIO_OTYPE_OD 

Set output type to open-drain.

PX_GPIO_OTYPE_BIT_MASK 

Output type bit mask.

Definition at line 61 of file px_gpio.h.

◆ px_gpio_ospeed_t

GPIO output speed selection; See GPIOx_OSPEEDR register.

Enumerator
PX_GPIO_OSPEED_NA 

Output speed is not applicable (pin mode is input or analog)

PX_GPIO_OSPEED_LO 

Set output speed to low.

PX_GPIO_OSPEED_MD 

Set output speed to medium.

PX_GPIO_OSPEED_HI 

Set output speed to high.

PX_GPIO_OSPEED_VH 

Set output speed to very high.

PX_GPIO_OSPEED_BIT_MASK 

Output speed bit mask.

Definition at line 70 of file px_gpio.h.

◆ px_gpio_pull_t

GPIO pull-up / pull-down selection; See GPIOx_PUPDR register.

Enumerator
PX_GPIO_PULL_NO 

No pull-up / pull-down.

PX_GPIO_PULL_UP 

Enable pull-up.

PX_GPIO_PULL_DN 

Enable pull-down.

PX_GPIO_PULL_BIT_MASK 

Pull-up / Pull-down bit mask.

Definition at line 81 of file px_gpio.h.

◆ px_gpio_out_init_t

GPIO initial output selection; See GPIOx_ODR register.

Enumerator
PX_GPIO_OUT_INIT_NA 

Initialize output is not applicable (pin mode is input or analog)

PX_GPIO_OUT_INIT_LO 

Initialize output pin to low (0)

PX_GPIO_OUT_INIT_HI 

Initialize output pin to high (1)

Definition at line 90 of file px_gpio.h.

◆ px_gpio_af_t

GPIO alternate function selection; See GPIOx_AFRL and GPIOx_AFRH register.

Enumerator
PX_GPIO_AF_NA 

Alternate Function is not applicable (pin mode is not alternate function)

PX_GPIO_AF_0 

Alternate Function 0.

PX_GPIO_AF_1 

Alternate Function 1.

PX_GPIO_AF_2 

Alternate Function 2.

PX_GPIO_AF_3 

Alternate Function 3.

PX_GPIO_AF_4 

Alternate Function 4.

PX_GPIO_AF_5 

Alternate Function 5.

PX_GPIO_AF_6 

Alternate Function 6.

PX_GPIO_AF_7 

Alternate Function 7.

PX_GPIO_ALT_FN_BIT_MASK 

Alternate Function bit mask.

Definition at line 98 of file px_gpio.h.

Function Documentation

◆ px_gpio_port_init()

void px_gpio_port_init ( const px_gpio_port_init_t init)

Initialise a GPIO port.

Parameters
initPointer to structure containing register initialization values

Definition at line 103 of file px_gpio.c.

◆ px_gpio_open2()

void px_gpio_open2 ( px_gpio_handle_t gpio,
GPIO_TypeDef *  gpio_base_reg,
uint8_t  pin 
)

Open a GPIO handle and initialize with current pin configuration.

Parameters
[in,out]gpioPointer to px_gpio_handle_t structure
[in]gpio_base_regGPIO register base address
[in]pinPin: 0, 1, 2, ..., or 15

Definition at line 39 of file px_gpio.c.

◆ px_gpio_init()

void px_gpio_init ( const px_gpio_handle_t gpio)
inline

Initialise a GPIO pin using supplied handle.

Initialise a GPIO pin using supplied handle.

Definition at line 69 of file px_gpio.c.