px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_gpio.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2017 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_gpio.h : ST STM32 GPIO driver
14 Author(s): Pieter Conradie
15 Creation Date: 2017-11-16
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20
21/* _____PROJECT INCLUDES_____________________________________________________ */
22#include "px_gpio.h"
23#include "px_log.h"
24
25/* _____LOCAL DEFINITIONS____________________________________________________ */
26PX_LOG_NAME("px_gpio");
27
28/* _____MACROS_______________________________________________________________ */
29
30/* _____GLOBAL VARIABLES_____________________________________________________ */
31
32/* _____LOCAL VARIABLES______________________________________________________ */
33
34/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
35
36/* _____LOCAL FUNCTIONS______________________________________________________ */
37
38/* _____GLOBAL FUNCTIONS_____________________________________________________ */
40 GPIO_TypeDef * gpio_base_reg,
41 uint8_t pin)
42{
43 uint32_t pin_bit_mask = ((uint32_t)1) << pin;
44
45 gpio->gpio_base_reg = gpio_base_reg;
46 gpio->pin = pin;
47 gpio->mode = (px_gpio_mode_t)LL_GPIO_GetPinMode(gpio_base_reg, pin_bit_mask);
48 gpio->otype = (px_gpio_otype_t)LL_GPIO_GetPinOutputType(gpio_base_reg, pin_bit_mask);
49 gpio->ospeed = (px_gpio_ospeed_t)LL_GPIO_GetPinSpeed(gpio_base_reg, pin_bit_mask);
50 gpio->pull = (px_gpio_pull_t)LL_GPIO_GetPinPull(gpio_base_reg, pin_bit_mask);
51 if(LL_GPIO_IsOutputPinSet(gpio_base_reg, pin_bit_mask))
52 {
54 }
55 else
56 {
58 }
59 if(gpio->pin < 8)
60 {
61 gpio->af = (px_gpio_af_t)LL_GPIO_GetAFPin_0_7(gpio_base_reg, pin_bit_mask);
62 }
63 else
64 {
65 gpio->af = (px_gpio_af_t)LL_GPIO_GetAFPin_8_15(gpio_base_reg, pin_bit_mask);
66 }
67}
68
70{
71 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
72
73 // Set pull-up / pull-down (GPIOx_PUPDR: PUPDyy[1:0])
74 LL_GPIO_SetPinPull(gpio->gpio_base_reg, pin_bit_mask, gpio->pull);
75 // Set pin speed
76 LL_GPIO_SetPinSpeed(gpio->gpio_base_reg, pin_bit_mask, gpio->ospeed);
77 // Set pin output type (push-pull or open drain)
78 LL_GPIO_SetPinOutputType(gpio->gpio_base_reg, pin_bit_mask, gpio->otype);
79 // Output?
80 if(gpio->out_init)
81 {
82 // Set output high (1)
83 LL_GPIO_SetOutputPin(gpio->gpio_base_reg, pin_bit_mask);
84 }
85 else
86 {
87 // Set output low (0)
88 LL_GPIO_ResetOutputPin(gpio->gpio_base_reg, pin_bit_mask);
89 }
90 // Set Alternative Function
91 if(gpio->pin < 8)
92 {
93 LL_GPIO_SetAFPin_0_7(gpio->gpio_base_reg, pin_bit_mask, gpio->af);
94 }
95 else
96 {
97 LL_GPIO_SetAFPin_8_15(gpio->gpio_base_reg, pin_bit_mask, gpio->af);
98 }
99 // Set mode: in, out, alternative function or analog
100 LL_GPIO_SetPinMode(gpio->gpio_base_reg, pin_bit_mask, gpio->mode);
101}
102
104{
105 GPIO_TypeDef * gpio_base_reg = init->gpio_base_reg;
106
107 // Select Pull-Up / Pull-Down or None
108 gpio_base_reg->PUPDR = init->pupdr;
109 // Select output type: Push-Pull or Open-Drain
110 gpio_base_reg->OTYPER = init->otyper;
111 // Select output speed: Slow, Medium, Fast or Very Fast
112 gpio_base_reg->OSPEEDR = init->ospeedr;
113 // Set output data: Low or High
114 gpio_base_reg->ODR = init->odr;
115 // Select Alternative Function AF0..AF7 for pins 0..7
116 gpio_base_reg->AFR[0] = init->afrl;
117 // Select Alternative Function AF0..AF7 for pins 8..15
118 gpio_base_reg->AFR[1] = init->afrh;
119 // Select mode: Input, Output, Alternative Function or Analog
120 gpio_base_reg->MODER = init->moder;
121}
#define PX_LOG_NAME(name)
Macro to declare a log name string once for each file to reduce code size.
Definition: px_log.h:349
uint32_t moder
Mode register value.
Definition: px_gpio.h:129
uint32_t otyper
Output type register value.
Definition: px_gpio.h:130
uint32_t afrl
Alternative function low register value.
Definition: px_gpio.h:134
px_gpio_af_t af
Alternative function: AF0, AF1, ... or AF7.
Definition: px_gpio.h:122
uint32_t odr
Output data register value.
Definition: px_gpio.h:133
uint8_t pin
Pin: 0, 1, 2, ..., or 15.
Definition: px_gpio.h:116
uint32_t ospeedr
Output speed register value.
Definition: px_gpio.h:131
GPIO_TypeDef * gpio_base_reg
GPIO peripheral base register address.
Definition: px_gpio.h:128
px_gpio_ospeed_t ospeed
Output speed: low, medium, high or very high.
Definition: px_gpio.h:119
px_gpio_otype_t otype
Output type: push-pull or open-drain.
Definition: px_gpio.h:118
uint32_t afrh
Alternative function high register value.
Definition: px_gpio.h:135
px_gpio_mode_t mode
Mode: Input, Output, Alternative Function or Analog.
Definition: px_gpio.h:117
px_gpio_out_init_t out_init
Initial output value: Low (0) or high (1)
Definition: px_gpio.h:121
GPIO_TypeDef * gpio_base_reg
GPIO peripheral base register address.
Definition: px_gpio.h:115
px_gpio_pull_t pull
None, pull-up or pull-down.
Definition: px_gpio.h:120
uint32_t pupdr
Pull up / pull-down register value.
Definition: px_gpio.h:132
px_gpio_mode_t
GPIO mode selection; See GPIOx_MODER register.
Definition: px_gpio.h:51
void px_gpio_port_init(const px_gpio_port_init_t *init)
Initialise a GPIO port.
Definition: px_gpio.c:103
px_gpio_otype_t
GPIO output type selection; See GPIOx_OTYPER register.
Definition: px_gpio.h:62
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.
Definition: px_gpio.c:39
px_gpio_pull_t
GPIO pull-up / pull-down selection; See GPIOx_PUPDR register.
Definition: px_gpio.h:82
px_gpio_ospeed_t
GPIO output speed selection; See GPIOx_OSPEEDR register.
Definition: px_gpio.h:71
void px_gpio_init(const px_gpio_handle_t *gpio)
Initialise a GPIO pin using supplied handle.
Definition: px_gpio.c:69
px_gpio_af_t
GPIO alternate function selection; See GPIOx_AFRL and GPIOx_AFRH register.
Definition: px_gpio.h:99
@ PX_GPIO_OUT_INIT_HI
Initialize output pin to high (1)
Definition: px_gpio.h:94
@ PX_GPIO_OUT_INIT_LO
Initialize output pin to low (0)
Definition: px_gpio.h:93
GPIO pin handle definition.
Definition: px_gpio.h:114
GPIO register values to intialise a port.
Definition: px_gpio.h:127