STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Loading...
Searching...
No Matches
GPIO

Pin configuration and digital I/O. More...

Collaboration diagram for GPIO:

Data Structures

struct  GpioResetValues_t
 Power-on values of the GPIO registers that are not zero on every port. More...

Macros

#define GPIO_AF_MAX   (15U)
 Highest alternate-function number (AF0-AF15).

Typedefs

typedef enum GpioPin_e GpioPin_e
 Pin number within a port.
typedef enum GpioPinState_e GpioPinState_e
 Logic level of a pin.
typedef struct GpioResetValues_t GpioResetValues_t
 Power-on values of the GPIO registers that are not zero on every port.

Enumerations

enum  GpioPin_e {
  GPIO_PIN_0 = 0 , GPIO_PIN_1 = 1 , GPIO_PIN_2 = 2 , GPIO_PIN_3 = 3 ,
  GPIO_PIN_4 = 4 , GPIO_PIN_5 = 5 , GPIO_PIN_6 = 6 , GPIO_PIN_7 = 7 ,
  GPIO_PIN_8 = 8 , GPIO_PIN_9 = 9 , GPIO_PIN_10 = 10 , GPIO_PIN_11 = 11 ,
  GPIO_PIN_12 = 12 , GPIO_PIN_13 = 13 , GPIO_PIN_14 = 14 , GPIO_PIN_15 = 15
}
 Pin number within a port. More...
enum  GpioPinState_e { GPIO_PIN_RESET = 0 , GPIO_PIN_SET = 1 }
 Logic level of a pin. More...

Functions

GpioResetValues_t gpioPortResetValues (const GpioRegisters_t *port)
 Look up a port's power-on MODER, OSPEEDR and PUPDR values.
DRIVER_MUST_CHECK DriverStatus_e gpioInit (GpioRegisters_t *port, GpioPin_e pin, uint32_t mode, uint32_t otype, uint32_t ospeed, uint32_t pupd)
 Configure a pin's mode, output type, speed and pull.
void gpioDeinit (GpioRegisters_t *port, GpioPin_e pin)
 Return a pin to its power-on configuration.
void gpioDeinitToReset (GpioRegisters_t *port, GpioPin_e pin, GpioResetValues_t reset)
 gpioDeinit() with the reset values supplied by the caller.
DRIVER_MUST_CHECK DriverStatus_e gpioSetAlternateFunction (GpioRegisters_t *port, GpioPin_e pin, uint32_t af)
 Select a pin's alternate function in AFRL/AFRH.
void gpioWritePin (GpioRegisters_t *port, GpioPin_e pin, GpioPinState_e level)
 Drive a pin high or low with one atomic BSRR write.
GpioPinState_e gpioReadPin (const GpioRegisters_t *port, GpioPin_e pin)
 Read a pin's input level from IDR.
void gpioTogglePin (GpioRegisters_t *port, GpioPin_e pin)
 Invert a pin's output level.
static DriverStatus_e gpioValidatePin (GpioPin_e pin)
 Reject a pin outside 0-15, which would shift by 32 or more.

Detailed Description

Pin configuration and digital I/O.

Macro Definition Documentation

◆ GPIO_AF_MAX

#define GPIO_AF_MAX   (15U)

Highest alternate-function number (AF0-AF15).

Definition at line 29 of file gpio.h.

Referenced by gpioSetAlternateFunction().

Typedef Documentation

◆ GpioPin_e

typedef enum GpioPin_e GpioPin_e

Pin number within a port.

C does not stop a caller casting any integer in. Functions that return a DriverStatus_e reject a pin above 15; the others state it as an @pre, because a larger value shifts by 32 or more, which is undefined behaviour.

Enumeration Type Documentation

◆ GpioPin_e

enum GpioPin_e

Pin number within a port.

C does not stop a caller casting any integer in. Functions that return a DriverStatus_e reject a pin above 15; the others state it as an @pre, because a larger value shifts by 32 or more, which is undefined behaviour.

Enumerator
GPIO_PIN_0 0 

Pin 0.

GPIO_PIN_1 1 

Pin 1.

GPIO_PIN_2 2 

Pin 2.

GPIO_PIN_3 3 

Pin 3.

GPIO_PIN_4 4 

Pin 4.

GPIO_PIN_5 5 

Pin 5.

GPIO_PIN_6 6 

Pin 6.

GPIO_PIN_7 7 

Pin 7.

GPIO_PIN_8 8 

Pin 8.

GPIO_PIN_9 9 

Pin 9.

GPIO_PIN_10 10 

Pin 10.

GPIO_PIN_11 11 

Pin 11.

GPIO_PIN_12 12 

Pin 12.

GPIO_PIN_13 13 

Pin 13.

GPIO_PIN_14 14 

Pin 14.

GPIO_PIN_15 15 

Pin 15.

Definition at line 38 of file gpio.h.

◆ GpioPinState_e

Logic level of a pin.

Enumerator
GPIO_PIN_RESET 0 

Low.

GPIO_PIN_SET 1 

High.

Definition at line 59 of file gpio.h.

Function Documentation

◆ gpioPortResetValues()

GpioResetValues_t gpioPortResetValues ( const GpioRegisters_t * port)

Look up a port's power-on MODER, OSPEEDR and PUPDR values.

Only GPIOA and GPIOB are non-zero: they hold the SWD/JTAG pins. Compares port by address and never dereferences it. Public so the host tests can check the lookup.

Parameters
portGPIO port (e.g. GPIOA); any other address gives all zeros.
Returns
The port's reset values.

Definition at line 60 of file gpio.c.

References GPIOA, GPIOA_MODER_RESET, GPIOA_PUPDR_RESET, GPIOB, GPIOB_MODER_RESET, GPIOB_OSPEEDR_RESET, GPIOB_PUPDR_RESET, gpioPortResetValues(), GpioResetValues_t::MODER, GpioResetValues_t::OSPEEDR, and GpioResetValues_t::PUPDR.

Referenced by gpioDeinit(), and gpioPortResetValues().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioInit()

DriverStatus_e gpioInit ( GpioRegisters_t * port,
GpioPin_e pin,
uint32_t mode,
uint32_t otype,
uint32_t ospeed,
uint32_t pupd )

Configure a pin's mode, output type, speed and pull.

Parameters
portGPIO port (e.g. GPIOA).
pinPin within the port.
modeA GPIO_MODE_* value.
otypeA GPIO_OTYPE_* value.
ospeedA GPIO_OSPEED_* value.
pupdA GPIO_PUPD_* value.
Precondition
The port's clock is enabled (rccGpioClockEnable()).
For GPIO_MODE_AF, gpioSetAlternateFunction() has already run for this pin.
Return values
DRIVER_STATUS_OKConfigured; MODER is written last.
DRIVER_STATUS_ERR_INVALID_PARAMpin is above 15, or a value is not one of its macros (for example the reserved PUPDR 0b11).

Definition at line 30 of file gpio.c.

References DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, GPIO_MODE_ANALOG, GPIO_OSPEED_HIGH, GPIO_OTYPE_OD, GPIO_PUPD_DOWN, gpioInit(), gpioValidatePin(), GpioRegisters_t::MODER, GpioRegisters_t::OSPEEDR, GpioRegisters_t::OTYPER, and GpioRegisters_t::PUPDR.

Referenced by gpioInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioDeinit()

void gpioDeinit ( GpioRegisters_t * port,
GpioPin_e pin )

Return a pin to its power-on configuration.

Restores MODER, OTYPER, OSPEEDR, PUPDR and the AFR field. The SWD/JTAG pins return to their debug configuration, so deinitialising one does not disconnect the debugger.

Parameters
portGPIO port (e.g. GPIOA).
pinPin within the port.
Precondition
pin is 0-15; there is no status to report a bad one.

Definition at line 83 of file gpio.c.

References gpioDeinit(), gpioDeinitToReset(), and gpioPortResetValues().

Referenced by gpioDeinit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioDeinitToReset()

void gpioDeinitToReset ( GpioRegisters_t * port,
GpioPin_e pin,
GpioResetValues_t reset )

gpioDeinit() with the reset values supplied by the caller.

Exists for the host tests: gpioDeinit() looks the values up by comparing port with GPIOA and GPIOB, which a test's in-memory struct never matches. Firmware calls gpioDeinit().

Parameters
portGPIO port.
pinPin within the port.
resetThe port's reset values, as from gpioPortResetValues().
Precondition
pin is 0-15.

Definition at line 88 of file gpio.c.

References GpioRegisters_t::AFRH, GpioRegisters_t::AFRL, gpioDeinitToReset(), GpioRegisters_t::MODER, GpioResetValues_t::MODER, GpioRegisters_t::OSPEEDR, GpioResetValues_t::OSPEEDR, GpioRegisters_t::OTYPER, GpioRegisters_t::PUPDR, and GpioResetValues_t::PUPDR.

Referenced by gpioDeinit(), and gpioDeinitToReset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioSetAlternateFunction()

DriverStatus_e gpioSetAlternateFunction ( GpioRegisters_t * port,
GpioPin_e pin,
uint32_t af )

Select a pin's alternate function in AFRL/AFRH.

Has no effect on the pin until its mode is GPIO_MODE_AF.

Parameters
portGPIO port (e.g. GPIOA).
pinPin within the port.
afAlternate function, 0 to GPIO_AF_MAX.
Return values
DRIVER_STATUS_OKSelected.
DRIVER_STATUS_ERR_INVALID_PARAMpin is above 15 or af above GPIO_AF_MAX.

Definition at line 110 of file gpio.c.

References GpioRegisters_t::AFRH, GpioRegisters_t::AFRL, DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, GPIO_AF_MAX, gpioSetAlternateFunction(), and gpioValidatePin().

Referenced by gpioSetAlternateFunction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioWritePin()

void gpioWritePin ( GpioRegisters_t * port,
GpioPin_e pin,
GpioPinState_e level )

Drive a pin high or low with one atomic BSRR write.

Parameters
portGPIO port (e.g. GPIOA).
pinPin within the port.
levelGPIO_PIN_SET or GPIO_PIN_RESET.
Precondition
pin is 0-15.

Definition at line 140 of file gpio.c.

References GpioRegisters_t::BSRR, GPIO_PIN_SET, and gpioWritePin().

Referenced by gpioWritePin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioReadPin()

GpioPinState_e gpioReadPin ( const GpioRegisters_t * port,
GpioPin_e pin )

Read a pin's input level from IDR.

Parameters
portGPIO port (e.g. GPIOA).
pinPin within the port.
Returns
GPIO_PIN_SET if the pin is high, GPIO_PIN_RESET otherwise.
Precondition
pin is 0-15.

Definition at line 154 of file gpio.c.

References GPIO_PIN_RESET, GPIO_PIN_SET, gpioReadPin(), and GpioRegisters_t::IDR.

Referenced by gpioReadPin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioTogglePin()

void gpioTogglePin ( GpioRegisters_t * port,
GpioPin_e pin )

Invert a pin's output level.

Reads ODR, then writes BSRR.

Parameters
portGPIO port (e.g. GPIOA).
pinPin within the port.
Precondition
pin is 0-15.
Warning
Not atomic: drive each pin from only one context. See Concurrency.

Definition at line 167 of file gpio.c.

References GpioRegisters_t::BSRR, gpioTogglePin(), and GpioRegisters_t::ODR.

Referenced by gpioTogglePin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gpioValidatePin()

DriverStatus_e gpioValidatePin ( GpioPin_e pin)
static

Reject a pin outside 0-15, which would shift by 32 or more.

Parameters
pinCandidate pin.
Return values
DRIVER_STATUS_OKpin is 0-15.
DRIVER_STATUS_ERR_INVALID_PARAMOtherwise.

Definition at line 18 of file gpio.c.

References DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, GPIO_PIN_15, and gpioValidatePin().

Referenced by gpioInit(), gpioSetAlternateFunction(), and gpioValidatePin().

Here is the call graph for this function:
Here is the caller graph for this function: