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.h
1#ifndef __PX_GPIO_H__
2#define __PX_GPIO_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2017 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_gpio.h : ST STM32 GPIO driver
16 Author(s): Pieter Conradie
17 Creation Date: 2017-11-16
18
19============================================================================= */
20
21/**
22 * @ingroup STM32
23 * @defgroup STM32_GPIO px_gpio.h : GPIO peripheral driver
24 *
25 * Driver to communicate with GPIO slaves.
26 *
27 * File(s):
28 * - arch/arm/stm32/inc/px_gpio.h
29 * - arch/arm/stm32/src/px_gpio.c
30 *
31 * @par Example:
32 * @include arch/arm/stm32/test/px_gpio_test.c
33 *
34 * @{
35 */
36
37/* _____STANDARD INCLUDES____________________________________________________ */
38
39/* _____PROJECT INCLUDES_____________________________________________________ */
40#include "px_defs.h"
41#include "px_stm32cube.h"
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46/* _____DEFINITIONS__________________________________________________________ */
47
48/* _____TYPE DEFINITIONS_____________________________________________________ */
49/// GPIO mode selection; See GPIOx_MODER register
50typedef enum
51{
52 PX_GPIO_MODE_IN = 0, ///< Set pin mode to input
53 PX_GPIO_MODE_OUT = 1, ///< Set pin mode to output
54 PX_GPIO_MODE_AF = 2, ///< Set pin mode to alternative function
55 PX_GPIO_MODE_ANA = 3, ///< Set pin mode to analog
56 PX_GPIO_MODE_DEF = 3, ///< Set pin mode to default (analog)
57 PX_GPIO_MODE_BIT_MASK = 0x3, ///< Pin mode bit mask
59
60/// GPIO output type selection; See GPIOx_OTYPER register
61typedef enum
62{
63 PX_GPIO_OTYPE_NA = 0, ///< Output type is not applicable (pin mode is input or analog)
64 PX_GPIO_OTYPE_PP = 0, ///< Set output type to push-pull
65 PX_GPIO_OTYPE_OD = 1, ///< Set output type to open-drain
66 PX_GPIO_OTYPE_BIT_MASK = 0x1, ///< Output type bit mask
68
69/// GPIO output speed selection; See GPIOx_OSPEEDR register
70typedef enum
71{
72 PX_GPIO_OSPEED_NA = 0, ///< Output speed is not applicable (pin mode is input or analog)
73 PX_GPIO_OSPEED_LO = 0, ///< Set output speed to low
74 PX_GPIO_OSPEED_MD = 1, ///< Set output speed to medium
75 PX_GPIO_OSPEED_HI = 2, ///< Set output speed to high
76 PX_GPIO_OSPEED_VH = 3, ///< Set output speed to very high
77 PX_GPIO_OSPEED_BIT_MASK = 0x3, ///< Output speed bit mask
79
80/// GPIO pull-up / pull-down selection; See GPIOx_PUPDR register
81typedef enum
82{
83 PX_GPIO_PULL_NO = 0, ///< No pull-up / pull-down
84 PX_GPIO_PULL_UP = 1, ///< Enable pull-up
85 PX_GPIO_PULL_DN = 2, ///< Enable pull-down
86 PX_GPIO_PULL_BIT_MASK = 0x3, ///< Pull-up / Pull-down bit mask
88
89/// GPIO initial output selection; See GPIOx_ODR register
90typedef enum
91{
92 PX_GPIO_OUT_INIT_NA = 0, ///< Initialize output is not applicable (pin mode is input or analog)
93 PX_GPIO_OUT_INIT_LO = 0, ///< Initialize output pin to low (0)
94 PX_GPIO_OUT_INIT_HI = 1, ///< Initialize output pin to high (1)
96
97/// GPIO alternate function selection; See GPIOx_AFRL and GPIOx_AFRH register
98typedef enum
99{
100 PX_GPIO_AF_NA = 0, ///< Alternate Function is not applicable (pin mode is not alternate function)
101 PX_GPIO_AF_0 = 0, ///< Alternate Function 0
102 PX_GPIO_AF_1 = 1, ///< Alternate Function 1
103 PX_GPIO_AF_2 = 2, ///< Alternate Function 2
104 PX_GPIO_AF_3 = 3, ///< Alternate Function 3
105 PX_GPIO_AF_4 = 4, ///< Alternate Function 4
106 PX_GPIO_AF_5 = 5, ///< Alternate Function 5
107 PX_GPIO_AF_6 = 6, ///< Alternate Function 6
108 PX_GPIO_AF_7 = 7, ///< Alternate Function 7
109 PX_GPIO_ALT_FN_BIT_MASK = 0xf, ///< Alternate Function bit mask
111
112/// GPIO pin handle definition
113typedef struct
114{
115 GPIO_TypeDef * gpio_base_reg; ///< GPIO peripheral base register address
116 uint8_t pin; ///< Pin: 0, 1, 2, ..., or 15
117 px_gpio_mode_t mode; ///< Mode: Input, Output, Alternative Function or Analog
118 px_gpio_otype_t otype; ///< Output type: push-pull or open-drain
119 px_gpio_ospeed_t ospeed; ///< Output speed: low, medium, high or very high
120 px_gpio_pull_t pull; ///< None, pull-up or pull-down
121 px_gpio_out_init_t out_init; ///< Initial output value: Low (0) or high (1)
122 px_gpio_af_t af; ///< Alternative function: AF0, AF1, ... or AF7
124
125/// GPIO register values to intialise a port
126typedef struct
127{
128 GPIO_TypeDef * gpio_base_reg; ///< GPIO peripheral base register address
129 uint32_t moder; ///< Mode register value
130 uint32_t otyper; ///< Output type register value
131 uint32_t ospeedr; ///< Output speed register value
132 uint32_t pupdr; ///< Pull up / pull-down register value
133 uint32_t odr; ///< Output data register value
134 uint32_t afrl; ///< Alternative function low register value
135 uint32_t afrh; ///< Alternative function high register value
137
138/* _____GLOBAL VARIABLES_____________________________________________________ */
139
140/* _____MACROS_______________________________________________________________ */
141/**
142 * Helper macro to define a GPIO pin.
143 *
144 * Usage example:
145 *
146 * @code{.c}
147 * // LED is an output pin on port D, pin 7, initially low
148 * #define PX_GPIO_LED PX_GPIO(D, 7, PX_GPIO_MODE_OUT, \
149 * PX_GPIO_OTYPE_PUSH_PULL, \
150 * PX_GPIO_OSPEED_LOW, \
151 * PX_GPIO_PULL_NONE, \
152 * PX_GPIO_OUT_INIT_LO, \
153 * PX_GPIO_ALT_FN_NA)
154 *
155 * // PX_GPIO_LED will be replaced by the C preprocessor with the following
156 * // sequence:
157 * // GPIOD, 7, PX_GPIO_MODE_OUT, PX_GPIO_OTYPE_PUSH_PULL,
158 * // PX_GPIO_OSPEED_LOW, PX_GPIO_PULL_NONE, PX_GPIO_OUT_INIT_LO,
159 * // PX_GPIO_ALT_FN_NA
160 * @endcode
161 *
162 * @param port A, B, C, D, E or H
163 * @param pin 0, 1, 2, ..., or 15
164 * @param mode Input, Output, Alternative Function or Analog; @see px_gpio_mode_t
165 * @param output_type push-pull or open-drain; @see px_gpio_output_type_t
166 * @param speed low, medium, high or very high; @see px_gpio_output_speed_t
167 * @param pull None, pull-up or pull-down; @see px_gpio_pull_t
168 * @param output_init Low (0) or high (1); @see px_gpio_output_init_t
169 * @param alt_fn Alternative Function 0 to 7 or Not Applicable
170 */
171#define PX_GPIO(port, pin, mode, output_type, speed, pull, output_init, alt_fn) \
172 GPIO ## port, pin, mode, output_type, speed, pull, output_init, alt_fn
173
174/// Initialise a GPIO pin
175#define PX_GPIO_INIT(gpio) _GPIO_INIT(gpio)
176#define _GPIO_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
177 do \
178 { \
179 if(mode == PX_GPIO_MODE_IN) \
180 { \
181 LL_GPIO_SetPinPull(gpio_base_reg, (1 << pin), pull); \
182 } \
183 else if(mode == PX_GPIO_MODE_OUT) \
184 { \
185 if(output_type == PX_GPIO_OTYPE_OD) \
186 { \
187 LL_GPIO_SetPinPull(gpio_base_reg, (1 << pin), pull); \
188 } \
189 if(output_init) \
190 { \
191 LL_GPIO_SetOutputPin(gpio_base_reg, (1 << pin)); \
192 } \
193 else \
194 { \
195 LL_GPIO_ResetOutputPin(gpio_base_reg, (1 << pin)); \
196 } \
197 LL_GPIO_SetPinSpeed(gpio_base_reg, (1 << pin), speed); \
198 LL_GPIO_SetPinOutputType(gpio_base_reg, (1 << pin), output_type); \
199 } \
200 else if(mode == PX_GPIO_MODE_AF) \
201 { \
202 LL_GPIO_SetPinSpeed(gpio_base_reg, (1 << pin), speed); \
203 LL_GPIO_SetPinOutputType(gpio_base_reg, (1 << pin), output_type); \
204 if(pin < 8) \
205 { \
206 LL_GPIO_SetAFPin_0_7(gpio_base_reg, (1 << pin), alt_fn); \
207 } \
208 else \
209 { \
210 LL_GPIO_SetAFPin_8_15(gpio_base_reg, (1 << pin), alt_fn); \
211 } \
212 } \
213 LL_GPIO_SetPinMode(gpio_base_reg, (1 << pin), mode); \
214 } \
215 while(0)
216
217/// Set GPIO pin output high
218#define PX_GPIO_OUT_SET_HI(gpio) _GPIO_OUT_SET_HI(gpio)
219#define _GPIO_OUT_SET_HI(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
220 LL_GPIO_SetOutputPin(gpio_base_reg, ((uint32_t)1 << pin))
221
222/// Set GPIO pin output low
223#define PX_GPIO_OUT_SET_LO(gpio) _GPIO_PIN_OUT_SET_LO(gpio)
224#define _GPIO_PIN_OUT_SET_LO(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
225 LL_GPIO_ResetOutputPin(gpio_base_reg, ((uint32_t)1 << pin))
226
227/// Toggle GPIO pin output
228#define PX_GPIO_OUT_TOGGLE(gpio) _GPIO_OUT_TOGGLE(gpio)
229#define _GPIO_OUT_TOGGLE(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
230 LL_GPIO_TogglePin(gpio_base_reg, ((uint32_t)1 << pin))
231
232/// Test if GPIO pin output is set high
233#define PX_GPIO_OUT_IS_HI(gpio) _GPIO_OUT_IS_HI(gpio)
234#define _GPIO_OUT_IS_HI(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
235 LL_GPIO_IsOutputPinSet(gpio_base_reg, ((uint32_t)1 << pin))
236
237/// Test if GPIO pin output is set low
238#define PX_GPIO_OUT_IS_LO(gpio) _GPIO_OUT_IS_LO(gpio)
239#define _GPIO_OUT_IS_LO(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
240 (!LL_GPIO_IsOutputPinSet(gpio_base_reg, ((uint32_t)1 << pin)))
241
242/// Test if GPIO pin input is high
243#define PX_GPIO_IN_IS_HI(gpio) _GPIO_IN_IS_HI(gpio)
244#define _GPIO_IN_IS_HI(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
245 LL_GPIO_IsInputPinSet(gpio_base_reg, ((uint32_t)1 << pin))
246
247/// Test if GPIO pin input is low
248#define PX_GPIO_IN_IS_LO(gpio) _GPIO_IN_IS_LO(gpio)
249#define _GPIO_IN_IS_LO(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
250 (!LL_GPIO_IsInputPinSet(gpio_base_reg, ((uint32_t)1 << pin)))
251
252/// Set GPIO pin direction to output
253#define PX_GPIO_DIR_SET_OUT(gpio) _GPIO_DIR_SET_OUT(gpio)
254#define _GPIO_DIR_SET_OUT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
255 LL_GPIO_SetPinMode(gpio_base_reg, ((uint32_t)1 << pin), LL_GPIO_MODE_OUTPUT)
256
257/// Set GPIO pin direction to input
258#define PX_GPIO_DIR_SET_IN(gpio) _GPIO_DIR_SET_IN(gpio)
259#define _GPIO_DIR_SET_IN(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
260 LL_GPIO_SetPinMode(gpio_base_reg, ((uint32_t)1 << pin), LL_GPIO_MODE_INPUT)
261
262/// Test if GPIO pin is configured to be an output
263#define PX_GPIO_DIR_IS_OUT(gpio) _GPIO_DIR_IS_OUT(gpio)
264#define _GPIO_DIR_IS_OUT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
265 (LL_GPIO_GetPinMode(gpio_base_reg, ((uint32_t)1 << pin)) == LL_GPIO_MODE_OUTPUT);
266
267/// Test if GPIO pin is configured to be an input
268#define PX_GPIO_DIR_IS_IN(gpio) _GPIO_DIR_IS_IN(gpio)
269#define _GPIO_DIR_IS_IN(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
270 (LL_GPIO_GetPinMode(gpio_base_reg, ((uint32_t)1 << pin)) == LL_GPIO_MODE_OUTPUT);
271
272/// Enable pull-up on GPIO pin
273#define PX_GPIO_PULL_UP_ENABLE(gpio) _GPIO_PULL_UP_ENABLE(gpio)
274#define _GPIO_PULL_UP_ENABLE(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
275 LL_GPIO_SetPinPull(gpio_base_reg, ((uint32_t)1 << pin), LL_GPIO_PULL_UP);
276
277/// Enable pull-down on GPIO pin
278#define PX_GPIO_PULL_DN_ENABLE(gpio) _GPIO_PULL_DN_ENABLE(gpio)
279#define _GPIO_PULL_DN_ENABLE(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
280 LL_GPIO_SetPinPull(gpio_base_reg, ((uint32_t)1 << pin), LL_GPIO_PULL_DN);
281
282/// Disable pull-up / pull-down on GPIO pin
283#define PX_GPIO_PULL_DISABLE(gpio) _GPIO_PULL_DISABLE(gpio)
284#define _GPIO_PULL_DISABLE(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
285 LL_GPIO_SetPinPull(gpio_base_reg, ((uint32_t)1 << pin), LL_GPIO_PULL_NO);
286
287/// Macro to calculate bit mask used to initialise GPIOx_MODER register
288#define PX_GPIO_REG_MODER_INIT(gpio) _PX_GPIO_REG_MODER_INIT(gpio)
289#define _PX_GPIO_REG_MODER_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
290 (((uint32_t)mode) << (pin * 2))
291
292/// Macro to calculate bit mask used to initialise GPIOx_OTYPER register
293#define PX_GPIO_REG_OTYPER_INIT(gpio) _PX_GPIO_REG_OTYPER_INIT(gpio)
294#define _PX_GPIO_REG_OTYPER_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
295 (((uint32_t)output_type) << (pin))
296
297/// Macro to calculate bit mask used to initialise GPIOx_OSPEEDR register
298#define PX_GPIO_REG_OSPEEDR_INIT(gpio) _PX_GPIO_REG_OSPEEDR_INIT(gpio)
299#define _PX_GPIO_REG_OSPEEDR_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
300 (((uint32_t)speed) << (pin * 2))
301
302/// Macro to calculate bit mask used to initialise GPIOx_PUPDR register
303#define PX_GPIO_REG_PUPDR_INIT(gpio) _PX_GPIO_REG_PUPDR_INIT(gpio)
304#define _PX_GPIO_REG_PUPDR_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
305 (((uint32_t)pull) << (pin * 2))
306
307/// Macro to calculate bit mask used to initialise GPIOx_OTYPER register
308#define PX_GPIO_REG_ODR_INIT(gpio) _PX_GPIO_REG_ODR_INIT(gpio)
309#define _PX_GPIO_REG_ODR_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
310 (((uint32_t)output_init) << (pin))
311
312/// Macro to calculate bit mask used to initialise GPIOx_AFRL register (pins 0 to 7)
313#define PX_GPIO_REG_AFRL_INIT(gpio) _PX_GPIO_REG_AFRL_INIT(gpio)
314#define _PX_GPIO_REG_AFRL_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
315 ((pin > 7) ? 0 : (((uint32_t)alt_fn) << (pin * 4)))
316
317/// Macro to calculate bit mask used to initialise GPIOx_AFRH register (pins 8 to 15)
318#define PX_GPIO_REG_AFRH_INIT(gpio) _PX_GPIO_REG_AFRH_INIT(gpio)
319#define _PX_GPIO_REG_AFRH_INIT(gpio_base_reg, pin, mode, output_type, speed, pull, output_init, alt_fn) \
320 ((pin < 8) ? 0 : (((uint32_t)alt_fn) << ((pin - 8) * 4)))
321
322/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
323/**
324 * Initialise a GPIO port
325 *
326 * @param init Pointer to structure containing register initialization values
327 */
328void px_gpio_port_init(const px_gpio_port_init_t * init);
329
330/**
331 * Open a GPIO handle using specified options.
332 *
333 * @param [in, out] gpio_handle Pointer to px_gpio_handle_t structure
334 * @param [in] gpio_base_reg GPIO register base address
335 * @param [in] pin Pin: 0, 1, 2, ..., or 15
336 * @param [in] mode Mode: Input, Output, Alternative Function or Analog
337 * @param [in] output_type Output type: push-pull or open-drain
338 * @param [in] speed Output speed: low, medium, high or very high
339 * @param [in] pull None, pull-up or pull-down
340 * @param [in] output_init Low (0) or high (1)
341 * @param [in] alt_fn Alternative function: AF0, AF1, ... or AF7
342 *
343 * Example:
344 *
345 * @code{.c}
346 * px_gpio_handle_t px_gpio_led;
347 *
348 * px_gpio_init(&px_gpio_led,
349 * GPIOD, 7,
350 * PX_GPIO_MODE_OUT,
351 * PX_GPIO_OTYPE_PUSH_PULL,
352 * PX_GPIO_OSPEED_LOW,
353 * PX_GPIO_PULL_NONE,
354 * PX_GPIO_OUT_INIT_LO,
355 * PX_GPIO_ALT_FN_NA);
356 * @endcode
357 */
358static inline void px_gpio_open(px_gpio_handle_t * gpio,
359 GPIO_TypeDef * gpio_base_reg,
360 uint8_t pin,
361 px_gpio_mode_t mode,
362 px_gpio_otype_t output_type,
363 px_gpio_ospeed_t speed,
364 px_gpio_pull_t pull,
365 px_gpio_out_init_t output_init,
366 px_gpio_af_t alt_fn)
367{
368 gpio->gpio_base_reg = gpio_base_reg;
369 gpio->pin = pin;
370 gpio->mode = mode;
371 gpio->otype = output_type;
372 gpio->ospeed = speed;
373 gpio->pull = pull;
374 gpio->out_init = output_init;
375 gpio->af = alt_fn;
376}
377
378/**
379 * Open a GPIO handle and initialize with current pin configuration.
380 *
381 * @param [in, out] gpio Pointer to px_gpio_handle_t structure
382 * @param [in] gpio_base_reg GPIO register base address
383 * @param [in] pin Pin: 0, 1, 2, ..., or 15
384 *
385 */
387 GPIO_TypeDef * gpio_base_reg,
388 uint8_t pin);
389
390/// Initialise a GPIO pin using supplied handle
391void px_gpio_init(const px_gpio_handle_t * gpio);
392
393/// Set GPIO pin output high
394static inline void px_gpio_out_set_hi(const px_gpio_handle_t * gpio)
395{
396 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
397
398 LL_GPIO_SetOutputPin(gpio->gpio_base_reg, pin_bit_mask);
399}
400
401/// Set GPIO pin output low
402static inline void px_gpio_out_set_lo(const px_gpio_handle_t * gpio)
403{
404 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
405
406 LL_GPIO_ResetOutputPin(gpio->gpio_base_reg, pin_bit_mask);
407}
408
409/// Toggle GPIO pin output
410static inline void px_gpio_out_toggle(const px_gpio_handle_t * gpio)
411{
412 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
413
414 LL_GPIO_TogglePin(gpio->gpio_base_reg, pin_bit_mask);
415}
416
417/// Test if GPIO pin output is set high
418static inline bool px_gpio_out_is_hi(const px_gpio_handle_t * gpio)
419{
420 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
421
422 return LL_GPIO_IsOutputPinSet(gpio->gpio_base_reg, pin_bit_mask);
423}
424
425/// Test if GPIO pin output is set low
426static inline bool px_gpio_out_is_lo(const px_gpio_handle_t * gpio)
427{
428 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
429
430 return !LL_GPIO_IsOutputPinSet(gpio->gpio_base_reg, pin_bit_mask);
431}
432
433/// Test if GPIO pin input is high
434static inline bool px_gpio_in_is_hi(const px_gpio_handle_t * gpio)
435{
436 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
437
438 return LL_GPIO_IsInputPinSet(gpio->gpio_base_reg, pin_bit_mask);
439}
440
441/// Test if GPIO pin input is low
442static inline bool px_gpio_in_is_lo(const px_gpio_handle_t * gpio)
443{
444 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
445
446 return !LL_GPIO_IsInputPinSet(gpio->gpio_base_reg, pin_bit_mask);
447}
448
449/// Set GPIO pin direction to output
450static inline void px_gpio_dir_set_out(const px_gpio_handle_t * gpio)
451{
452 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
453
454 LL_GPIO_SetPinMode(gpio->gpio_base_reg, pin_bit_mask, LL_GPIO_MODE_OUTPUT);
455}
456
457/// Set GPIO pin direction to input
458static inline void px_gpio_dir_set_in(const px_gpio_handle_t * gpio)
459{
460 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
461
462 LL_GPIO_SetPinMode(gpio->gpio_base_reg, pin_bit_mask, PX_GPIO_MODE_IN);
463}
464
465/// Test if GPIO pin is configured to be an output
466static inline bool px_gpio_dir_is_out(const px_gpio_handle_t * gpio)
467{
468 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
469
470 return (LL_GPIO_GetPinMode(gpio->gpio_base_reg, pin_bit_mask) == LL_GPIO_MODE_OUTPUT);
471}
472
473/// Test if GPIO pin is configured to be an input
474static inline bool px_gpio_dir_is_in(const px_gpio_handle_t * gpio)
475{
476 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
477
478 return (LL_GPIO_GetPinMode(gpio->gpio_base_reg, pin_bit_mask) == LL_GPIO_MODE_INPUT);
479}
480
481/// Enable pull-up on GPIO pin
482static inline void px_gpio_pull_up_enable(const px_gpio_handle_t * gpio)
483{
484 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
485
486 LL_GPIO_SetPinPull(gpio->gpio_base_reg, pin_bit_mask, LL_GPIO_PULL_UP);
487}
488
489/// Enable pull-down on GPIO pin
490static inline void px_gpio_pull_dn_enable(const px_gpio_handle_t * gpio)
491{
492 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
493
494 LL_GPIO_SetPinPull(gpio->gpio_base_reg, pin_bit_mask, LL_GPIO_PULL_DOWN);
495}
496
497/// Disable pull-up / pull-down on GPIO pin
498static inline void px_gpio_pull_disable(const px_gpio_handle_t * gpio)
499{
500 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
501
502 LL_GPIO_SetPinPull(gpio->gpio_base_reg, pin_bit_mask, LL_GPIO_PULL_NO);
503}
504
505/// Change GPIO pin mode
506static inline void px_gpio_mode_set(const px_gpio_handle_t * gpio,
507 px_gpio_mode_t mode)
508{
509 uint32_t pin_bit_mask = ((uint32_t)1) << gpio->pin;
510
511 LL_GPIO_SetPinMode(gpio->gpio_base_reg, pin_bit_mask, mode);
512}
513
514/// Calculate bit mask used to initialise GPIOx_MODER register
515static inline uint32_t px_gpio_reg_moder_init(const px_gpio_handle_t * gpio)
516{
517 return (((uint32_t)gpio->mode) << (gpio->pin * 2));
518}
519
520/// Calculate bit mask used to initialise GPIOx_OTYPER register
521static inline uint32_t px_gpio_reg_otyper_init(const px_gpio_handle_t * gpio)
522{
523 return (((uint32_t)gpio->otype) << (gpio->pin));
524}
525
526/// Calculate bit mask used to initialise GPIOx_OSPEEDR register
527static inline uint32_t px_gpio_reg_ospeedr_init(const px_gpio_handle_t * gpio)
528{
529 return (((uint32_t)gpio->ospeed) << (gpio->pin * 2));
530}
531
532/// Calculate bit mask used to initialise GPIOx_PUPDR register
533static inline uint32_t px_gpio_reg_pupdr_init(const px_gpio_handle_t * gpio)
534{
535 return (((uint32_t)gpio->pull) << (gpio->pin * 2));
536}
537
538/// Calculate bit mask used to initialise GPIOx_AFRL register (pins 0 to 7)
539static inline uint32_t px_gpio_reg_afrl_init(const px_gpio_handle_t * gpio)
540{
541 if(gpio->pin < 8)
542 {
543 return (((uint32_t)gpio->pull) << (gpio->pin * 4));
544 }
545 else
546 {
547 return 0;
548 }
549}
550
551/// Calculate bit mask used to initialise GPIOx_AFRL register (pins 8 to 15)
552static inline uint32_t px_gpio_reg_afrh_init(const px_gpio_handle_t * gpio)
553{
554 if(gpio->pin >= 8)
555 {
556 return (((uint32_t)gpio->pull) << ((gpio->pin - 8) * 4));
557 }
558 else
559 {
560 return 0;
561 }
562}
563
564#ifdef __cplusplus
565}
566#endif
567
568/// @}
569#endif
void px_gpio_open(px_gpio_handle_t *gpio, px_gpio_reg_t port, px_gpio_reg_t ddr, px_gpio_reg_t pin, uint8_t bit, uint8_t dir, uint8_t init)
Initialise a GPIO structure.
Definition: px_gpio.h:358
bool px_gpio_out_is_lo(const px_gpio_handle_t *gpio)
Test if GPIO pin output is set low.
Definition: px_gpio.h:432
bool px_gpio_dir_is_in(const px_gpio_handle_t *gpio)
Test if GPIO pin is configured to be an input.
Definition: px_gpio.h:468
void px_gpio_out_set_lo(const px_gpio_handle_t *gpio)
Set GPIO pin output low.
Definition: px_gpio.h:414
bool px_gpio_in_is_lo(const px_gpio_handle_t *gpio)
Test if GPIO pin input is low.
Definition: px_gpio.h:444
bool px_gpio_dir_is_out(const px_gpio_handle_t *gpio)
Test if GPIO pin is configured to be an output.
Definition: px_gpio.h:462
void px_gpio_dir_set_out(const px_gpio_handle_t *gpio)
Set GPIO pin direction to output.
Definition: px_gpio.h:450
void px_gpio_out_set_hi(const px_gpio_handle_t *gpio)
Set GPIO pin output high.
Definition: px_gpio.h:408
void px_gpio_dir_set_in(const px_gpio_handle_t *gpio)
Set GPIO pin direction to input.
Definition: px_gpio.h:456
bool px_gpio_out_is_hi(const px_gpio_handle_t *gpio)
Test if GPIO pin output is set high.
Definition: px_gpio.h:426
void px_gpio_pull_up_enable(const px_gpio_handle_t *gpio)
Enable pull-up on GPIO pin.
Definition: px_gpio.h:474
void px_gpio_out_toggle(const px_gpio_handle_t *gpio)
Toggle GPIO pin output.
Definition: px_gpio.h:420
bool px_gpio_in_is_hi(const px_gpio_handle_t *gpio)
Test if GPIO pin input is high.
Definition: px_gpio.h:438
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_t
GPIO initial output selection; See GPIOx_ODR register.
Definition: px_gpio.h:91
@ PX_GPIO_MODE_AF
Set pin mode to alternative function.
Definition: px_gpio.h:54
@ PX_GPIO_MODE_IN
Set pin mode to input.
Definition: px_gpio.h:52
@ PX_GPIO_MODE_ANA
Set pin mode to analog.
Definition: px_gpio.h:55
@ PX_GPIO_MODE_BIT_MASK
Pin mode bit mask.
Definition: px_gpio.h:57
@ PX_GPIO_MODE_DEF
Set pin mode to default (analog)
Definition: px_gpio.h:56
@ PX_GPIO_MODE_OUT
Set pin mode to output.
Definition: px_gpio.h:53
@ PX_GPIO_OTYPE_PP
Set output type to push-pull.
Definition: px_gpio.h:64
@ PX_GPIO_OTYPE_BIT_MASK
Output type bit mask.
Definition: px_gpio.h:66
@ PX_GPIO_OTYPE_OD
Set output type to open-drain.
Definition: px_gpio.h:65
@ PX_GPIO_OTYPE_NA
Output type is not applicable (pin mode is input or analog)
Definition: px_gpio.h:63
@ PX_GPIO_PULL_BIT_MASK
Pull-up / Pull-down bit mask.
Definition: px_gpio.h:86
@ PX_GPIO_PULL_NO
No pull-up / pull-down.
Definition: px_gpio.h:83
@ PX_GPIO_PULL_UP
Enable pull-up.
Definition: px_gpio.h:84
@ PX_GPIO_PULL_DN
Enable pull-down.
Definition: px_gpio.h:85
@ PX_GPIO_OSPEED_MD
Set output speed to medium.
Definition: px_gpio.h:74
@ PX_GPIO_OSPEED_NA
Output speed is not applicable (pin mode is input or analog)
Definition: px_gpio.h:72
@ PX_GPIO_OSPEED_LO
Set output speed to low.
Definition: px_gpio.h:73
@ PX_GPIO_OSPEED_HI
Set output speed to high.
Definition: px_gpio.h:75
@ PX_GPIO_OSPEED_BIT_MASK
Output speed bit mask.
Definition: px_gpio.h:77
@ PX_GPIO_OSPEED_VH
Set output speed to very high.
Definition: px_gpio.h:76
@ PX_GPIO_AF_6
Alternate Function 6.
Definition: px_gpio.h:107
@ PX_GPIO_AF_2
Alternate Function 2.
Definition: px_gpio.h:103
@ PX_GPIO_AF_1
Alternate Function 1.
Definition: px_gpio.h:102
@ PX_GPIO_AF_7
Alternate Function 7.
Definition: px_gpio.h:108
@ PX_GPIO_AF_5
Alternate Function 5.
Definition: px_gpio.h:106
@ PX_GPIO_ALT_FN_BIT_MASK
Alternate Function bit mask.
Definition: px_gpio.h:109
@ PX_GPIO_AF_3
Alternate Function 3.
Definition: px_gpio.h:104
@ PX_GPIO_AF_4
Alternate Function 4.
Definition: px_gpio.h:105
@ PX_GPIO_AF_0
Alternate Function 0.
Definition: px_gpio.h:101
@ PX_GPIO_AF_NA
Alternate Function is not applicable (pin mode is not alternate function)
Definition: px_gpio.h:100
@ PX_GPIO_OUT_INIT_NA
Initialize output is not applicable (pin mode is input or analog)
Definition: px_gpio.h:92
@ 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