1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/**
* @file gpio.h
* GPIO Abstraction and access
*
* Copyright (C) 2018 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef GPIO_H_
#define GPIO_H_
#include <arch/stm/stm32l476xx.h>
/**
* Helps simplify gpio calls.
* @param p Port, e.g. A
* @param b Pin, e.g. 4
*/
#define GPIO_PORT(p, b) GPIO##p, b
/**
* Defines possible modes for a gpio pin.
*/
enum GPIO_MODE
{
INPUT = 0, /**< digital input */
OUTPUT, /**< digital output */
ALTERNATE, /**< alternate function */
ANALOG /**< analog function */
};
/**
* Defines whether to use push-pull or open drain.
*/
enum GPIO_TYPE
{
PUSHPULL = 0, /**< push-pull */
OPENDRAIN /**< open drain */
};
/**
* Defines the pin's speed
*/
enum GPIO_SPEED
{
LOW = 0, /**< low */
MEDIUM, /**< medium */
HIGH, /**< high */
VERYHIGH /**< very high/maximum */
};
/**
* Defines if a pullup or pulldown should be used.
*/
enum GPIO_PUPD
{
NOPUPD, /**< no pullup/pulldown */
PULLUP, /**< use pullup */
PULLDOWN /**< use pulldown */
};
/**
* Prepares for GPIO usage.
*/
void gpio_init(void);
/**
* Enables or disables pullup/pulldown for the given pin.
* @param port The port, e.g. GPIOA
* @param pin The pin
* @param pupd Pullup/pulldown enable
* @see GPIO_PUPD
*/
void gpio_pupd(GPIO_TypeDef *port, uint32_t pin, uint32_t pupd);
/**
* Sets whether to use push-pull or open drain for the given pin.
* @param port The port
* @param pin The pin
* @param type What type to use
* @see GPIO_TYPE
*/
void gpio_type(GPIO_TypeDef *port, uint32_t pin, uint32_t type);
/**
* Sets the pin's speed.
* @param port The port
* @param pin The pin
* @param speed The speed to use
* @see GPIO_SPEED
*/
void gpio_speed(GPIO_TypeDef *port, uint32_t pin, uint32_t speed);
/**
* Sets the pin's i/o mode.
* @param port The port
* @param pin The pin
* @param mode The mode to use
* @see GPIO_MODE
*/
void gpio_mode(GPIO_TypeDef *port, uint32_t pin, uint32_t mode);
/**
* Sets the state of a digital output pin.
* @param port The port
* @param pin The pin
* @param val Non-zero for high, zero for low
*/
void gpio_dout(GPIO_TypeDef *port, uint32_t pin, uint32_t val);
/**
* Reads a digital input pin.
* @param port The port
* @param pin The pin
* @return Non-zero for high, zero for low
*/
uint32_t gpio_din(GPIO_TypeDef *port, uint32_t pin);
#endif // GPIO_H_
|