aboutsummaryrefslogtreecommitdiffstats
path: root/include/gpio.h
blob: 46a8e15428bcf501023919082316aac491116e69 (plain)
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
/**
 * @file gpio.h
 * Abstracts gpio access, makes things easier
 */

#ifndef GPIO_H_
#define GPIO_H_

#include <stm32l476xx.h>

/**
 * Helps simplify gpio calls.
 * @param p port, e.g. GPIOA
 * @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 */
};

/**
 * Initializes the gpio.
 */
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, uint8_t pin, uint8_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 to use
 * @see GPIO_TYPE
 */
void gpio_type(GPIO_TypeDef *port, uint8_t pin, uint8_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, uint8_t pin, uint8_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, uint8_t pin, uint8_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, uint8_t pin, uint8_t val);

/**
 * Reads a digital input pin.
 * @param port the port
 * @param pin the pin
 * @return non-zero for high, zero for low
 */
uint8_t gpio_din(GPIO_TypeDef *port, uint8_t pin);

#endif // GPIO_H_