libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
port.c
Go to the documentation of this file.
1/** @addtogroup port_file IO Port API
2 * @ingroup peripheral_apis
3 * @brief <b>Access functions for the SAMD I/O Controller</b>
4 * @date 10 April 2020
5 * @copyright SPDX: LGPL-3.0-or-later
6 * @author 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
7 */
8
9/**@{*/
10
12
13/** @brief Initialize GPIO pins
14 *
15 * Configure a group of Pins for the given port.
16 *
17 * @param[in] gpioport port register address base @ref port_reg_base
18 * @param[in] mode direction @ref gpio_direction
19 * @param[in] cnf configuration mode @ref gpio_cnf
20 * @param[in] gpios @ref gpio_pin_id. Any combination of pins
21 * may be specified by OR'ing then together.
22 */
23void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t cnf, uint32_t gpios)
24{
25 uint32_t reg = PORT_WRCONFIG_WRPINCFG;
26 /* enable pull */
27 if (cnf == GPIO_CNF_PULLDOWN || cnf == GPIO_CNF_PULLUP) {
29 }
30 /* enable input buffer */
31 if (mode != GPIO_MODE_OUTPUT) {
32 reg |= PORT_WRCONFIG_INEN;
33 }
34 /* set pmuxen */
35 if (cnf == GPIO_CNF_AF) {
37 }
38
39 /* PORTx_WRCONFIG allows to configure pins [31:16] or [15:0] */
40 /* write low pins */
41 PORT_WRCONFIG(gpioport) = reg | PORT_WRCONFIG_PINMASK(gpios);
42 /* write high pins */
43 PORT_WRCONFIG(gpioport) = reg | PORT_WRCONFIG_HWSEL |
44 PORT_WRCONFIG_PINMASK(gpios >> 16);
45
46 /* configure port direction for selected gpios */
47 /* DIR is always 0 when PULL */
48 if (cnf == GPIO_CNF_PULLDOWN || cnf == GPIO_CNF_PULLUP) {
49 PORT_DIRCLR(gpioport) = gpios;
50 } else if (mode == GPIO_MODE_INPUT) {
51 PORT_DIRCLR(gpioport) = gpios;
52 } else {
53 PORT_DIRSET(gpioport) = gpios;
54 }
55
56 /* PULL UP/DOWN is configured through OUT */
57 if (cnf == GPIO_CNF_PULLDOWN) {
58 PORT_OUTCLR(gpioport) = gpios;
59 } else if (cnf == GPIO_CNF_PULLUP) {
60 PORT_OUTSET(gpioport) = gpios;
61 }
62}
63
64/** @brief Alternate function GPIO pins
65 *
66 * Configure a group of Pins in alternate function.
67 *
68 * @param[in] gpioport port register address base @ref port_reg_base
69 * @param[in] af pmux configuration @ref gpio_mux
70 * @param[in] gpios @ref gpio_pin_id. Any combination of pins
71 * may be specified by OR'ing then together.
72 */
73void gpio_set_af(uint32_t gpioport, uint8_t af, uint32_t gpios)
74{
75 uint32_t reg = PORT_WRCONFIG_WRPINCFG |
78
79 /* write gpios[15:0] */
80 PORT_WRCONFIG(gpioport) = reg | PORT_WRCONFIG_PINMASK(gpios);
81
82 /* write gpios[31:16] */
83 PORT_WRCONFIG(gpioport) = reg | PORT_WRCONFIG_HWSEL |
84 PORT_WRCONFIG_PINMASK(gpios >> 16);
85}
86
87/** @brief Set a group of Pins
88 *
89 * Set a group of Pins for the given port.
90 *
91 * @param[in] gpioport port register address base @ref port_reg_base
92 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be
93 * specified by OR'ing then together.
94 */
95void gpio_set(uint32_t gpioport, uint32_t gpios)
96{
97 PORT_OUTSET(gpioport) = gpios;
98}
99
100/** @brief Clear a group of Pins
101 *
102 * Clear a group of Pins for the given port.
103 *
104 * @param[in] gpioport port register address base @ref port_reg_base
105 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be
106 * specified by OR'ing then together.
107 */
108void gpio_clear(uint32_t gpioport, uint32_t gpios)
109{
110 PORT_OUTCLR(gpioport) = gpios;
111}
112
113/** @brief Read level of a group of Pins
114 *
115 * Read the level of a group of Pins for the given port.
116 *
117 * @param[in] gpioport port register address base @ref port_reg_base
118 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be
119 * specified by OR'ing then together.
120 */
121uint32_t gpio_get(uint32_t gpioport, uint32_t gpios)
122{
123 return PORT_IN(gpioport) & gpios;
124}
125
126/** @brief Toggle level of a group of Pins
127 *
128 * Toggle one or more pins of the givent port.
129 *
130 * @param[in] gpioport port register address base @ref port_reg_base
131 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be
132 * specified by OR'ing then together.
133 */
134void gpio_toggle(uint32_t gpioport, uint32_t gpios)
135{
136 PORT_OUTTGL(gpioport) = gpios;
137}
138
139/** @brief Read level for all pins from a port
140 *
141 * Read the level of all pins of the given port.
142 *
143 * @param[in] port register address base @ref port_reg_base
144 *
145 * @return The level of all pins on the port.
146 */
147uint32_t port_read(uint32_t port)
148{
149 return PORT_IN(port);
150}
151
152/** @brief Set level for all pins from a port
153 *
154 * Set the level of all pins of the given port.
155 *
156 * @param[in] port register address base @ref port_reg_base
157 * @param[in] data @ref gpio_pin_id. Any combination of pins
158 * may be specified by OR'ing then together.
159 */
160void port_write(uint32_t port, uint32_t data)
161{
162 PORT_OUT(port) = data;
163}
164
165/**@}*/
#define GPIO_CNF_PULLDOWN
Definition: port.h:238
#define GPIO_CNF_PULLUP
Definition: port.h:239
#define GPIO_CNF_AF
Definition: port.h:240
#define GPIO_MODE_OUTPUT
Definition: port.h:227
#define GPIO_MODE_INPUT
Definition: port.h:226
uint32_t gpio_get(uint32_t gpioport, uint32_t gpios)
Read level of a group of Pins.
Definition: port.c:121
void gpio_set(uint32_t gpioport, uint32_t gpios)
Set a group of Pins.
Definition: port.c:95
void port_write(uint32_t port, uint32_t data)
Set level for all pins from a port.
Definition: port.c:160
void gpio_clear(uint32_t gpioport, uint32_t gpios)
Clear a group of Pins.
Definition: port.c:108
void gpio_toggle(uint32_t gpioport, uint32_t gpios)
Toggle level of a group of Pins.
Definition: port.c:134
void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t cnf, uint32_t gpios)
Initialize GPIO pins.
Definition: port.c:23
uint32_t port_read(uint32_t port)
Read level for all pins from a port.
Definition: port.c:147
void gpio_set_af(uint32_t gpioport, uint8_t af, uint32_t gpios)
Alternate function GPIO pins.
Definition: port.c:73
#define PORT_WRCONFIG(port)
Write configuration register.
Definition: port.h:116
#define PORT_OUTSET(port)
output set register
Definition: port.h:104
#define PORT_DIRCLR(port)
Direction clear register.
Definition: port.h:89
#define PORT_IN(port)
input register
Definition: port.h:110
#define PORT_OUTTGL(port)
output toggle register
Definition: port.h:107
#define PORT_DIRSET(port)
Direction set register.
Definition: port.h:92
#define PORT_OUT(port)
output register
Definition: port.h:98
#define PORT_OUTCLR(port)
output clear register
Definition: port.h:101
#define PORT_WRCONFIG_PULLEN
PULLEN: Pull Enable: enable PINCFGy.PULLEN for pins in PINMASK.
Definition: port.h:184
#define PORT_WRCONFIG_WRPMUX
WRPMUX: Write PMUX: 1 to update pins pmux for selected by PINMASK.
Definition: port.h:175
#define PORT_WRCONFIG_HWSEL
HWSEL: Half word select: 0 [15:0], 1 [31:16].
Definition: port.h:169
#define PORT_WRCONFIG_PMUXEN
PMUXEN: Peripheral Multiplexer Enable: enable PINCFGy.PMUXEN for pins in PINMASK.
Definition: port.h:190
#define PORT_WRCONFIG_PINMASK(pins)
PINMASK: Pin Mask for Multiple Pin Configuration: select pins to be configured [31:16] if HWSET=1,...
Definition: port.h:195
#define PORT_WRCONFIG_INEN
INEN: Input Enable: enable PINCFGy.INEN for pins in PINMASK.
Definition: port.h:187
#define PORT_WRCONFIG_PMUX(mux)
PMUX: Peripheral Multiplexing: determine pmux for pins selected by PINMASK.
Definition: port.h:178
#define PORT_WRCONFIG_WRPINCFG
WRPINCFG: Write PINCFG: 1 to update pins for selected by PINMASK.
Definition: port.h:172