microHAL
An abstraction layer for your future F4xx projects
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1
12#ifndef GPIO_H
13#define GPIO_H
14
15/* -- Includes -- */
16#include <stdint.h>
17#include "stm32f4xx.h"
18#include "defines.h"
19
20/* -- Structs -- */
24struct __attribute__((packed)) GPIORegs {
25 REG32 MODER;
26 REG32 OTYPER;
27 REG32 OSPEEDR;
28 REG32 PUPDR;
29 REG32 IDR;
30 REG32 ODR;
31 REG32 BSSR;
32 REG32 LCKR;
33 REG32 AFR[2];
34};
35
36_Static_assert((sizeof(struct GPIORegs)) == (sizeof(uint32_t) * 10U),
37 "GPIO register struct size mismatch. Is it aligned?");
38
39#ifndef UTEST
40#define GPIO(bank) \
41 (struct GPIORegs *)(GPIOA_BASE + (0x400U * ((uint8_t)bank - (uint8_t)'A')))
42#else
43extern struct GPIORegs *GPIO(const uint8_t bank);
44#endif
45
46/* -- Enums -- */
50typedef enum gp_bank {
51#ifdef GPIOA_BASE
52 GP_BANK_A = (uint8_t)'A',
53#endif
54#ifdef GPIOB_BASE
55 GP_BANK_B = (uint8_t)'B',
56#endif
57#ifdef GPIOC_BASE
58 GP_BANK_C = (uint8_t)'C',
59#endif
60#ifdef GPIOD_BASE
61 GP_BANK_D = (uint8_t)'D',
62#endif
63#ifdef GPIOE_BASE
64 GP_BANK_E = (uint8_t)'E',
65#endif
66#ifdef GPIOF_BASE
67 GP_BANK_F = (uint8_t)'F',
68#endif
69#ifdef GPIOG_BASE
70 GP_BANK_G = (uint8_t)'G',
71#endif
72#ifdef GPIOH_BASE
73 GP_BANK_H = (uint8_t)'H',
74#endif
75#ifdef GPIOI_BASE
76 GP_BANK_I = (uint8_t)'I',
77#endif
78#ifdef GPIOJ_BASE
79 GP_BANK_J = (uint8_t)'J',
80#endif
81#ifdef GPIOK_BASE
82 GP_BANK_K = (uint8_t)'K',
83#endif
84 GP_BANK_LEN // This should be substracted with 'A'
86
90typedef enum gp_dir {
91 GP_DIR_IN = 0x00,
92 GP_DIR_OU = 0x01,
93 GP_DIR_AL = 0x02,
94 GP_DIR_AN = 0x03
96
100typedef enum gp_otype {
101 GP_OTYPE_PP = 0x00,
102 GP_OTYPE_OD = 0x01
104
108typedef enum gp_speed {
109 GP_SPEED_LOW = 0x00,
110 GP_SPEED_MED = 0x01,
111 GP_SPEED_FAS = 0x02,
112 GP_SPEED_HIG = 0x03
114
118typedef enum gp_pupd {
119 GP_PUPD_NONE = 0x00,
120 GP_PUPD_PLUP = 0x01,
121 GP_PUPD_PLDO = 0x02,
123
135void gp_set_direction(const gp_bank_t bank, const uint8_t pin,
136 const gp_dir_t dir);
137
149void gp_set_output_type(const gp_bank_t bank, const uint8_t pin,
150 const gp_otype_t type);
151
163void gp_set_speed(const gp_bank_t bank, const uint8_t pin,
164 const gp_speed_t speed);
165
178void gp_set_pupd(const gp_bank_t bank, const uint8_t pin,
179 const gp_pupd_t poopdr);
180
192void gp_set_val(const gp_bank_t bank, const uint8_t pin, const _Bool value);
193
201uint8_t gp_read_val(const gp_bank_t bank, const uint8_t pin);
202
211void gp_set_af(const gp_bank_t bank, const uint8_t pin, const uint8_t af);
212
213#endif /* GPIO_H */
Defines used commonly in most files.
gp_pupd
Available GPIO pull states.
Definition gpio.h:118
void gp_set_output_type(const gp_bank_t bank, const uint8_t pin, const gp_otype_t type)
Sets the GPIO output pin to the desired type.
Definition gpio.c:87
enum gp_dir gp_dir_t
Available GPIO modes.
enum gp_speed gp_speed_t
Available GPIO output speeds.
void gp_set_val(const gp_bank_t bank, const uint8_t pin, const _Bool value)
Sets the GPIO output pin to the desired value.
Definition gpio.c:157
uint8_t gp_read_val(const gp_bank_t bank, const uint8_t pin)
Reads the GPIO input pin value.
Definition gpio.c:169
enum gp_pupd gp_pupd_t
Available GPIO pull states.
gp_bank
Available GPIO peripherals.
Definition gpio.h:50
void gp_set_direction(const gp_bank_t bank, const uint8_t pin, const gp_dir_t dir)
Sets the GPIO pin to the desired mode.
Definition gpio.c:61
void gp_set_pupd(const gp_bank_t bank, const uint8_t pin, const gp_pupd_t poopdr)
Sets the GPIO input pin to the desired state.
Definition gpio.c:132
gp_speed
Available GPIO output speeds.
Definition gpio.h:108
enum gp_otype gp_otype_t
Available GPIO output types.
gp_otype
Available GPIO output types.
Definition gpio.h:100
gp_dir
Available GPIO modes.
Definition gpio.h:90
enum gp_bank gp_bank_t
Available GPIO peripherals.
void gp_set_speed(const gp_bank_t bank, const uint8_t pin, const gp_speed_t speed)
Sets the GPIO output pin to the desired speed.
Definition gpio.c:106
void gp_set_af(const gp_bank_t bank, const uint8_t pin, const uint8_t af)
Sets the alternate function of the GPIO pin.
Definition gpio.c:178
CMSIS device header stubs.
Contains GPIO registers.
Definition gpio.h:24