libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio_common_all.c
Go to the documentation of this file.
1/** @addtogroup gpio_file GPIO peripheral API
2 * @ingroup peripheral_apis
3
4@author @htmlonly &copy; @endhtmlonly 2009 Uwe Hermann <uwe@hermann-uwe.de>
5
6*/
7
8/*
9 * This file is part of the libopencm3 project.
10 *
11 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
12 *
13 * This library is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this library. If not, see <http://www.gnu.org/licenses/>.
25 */
26
28
29/**@{*/
30
31/*---------------------------------------------------------------------------*/
32/** @brief Set a Group of Pins Atomic
33
34Set one or more pins of the given GPIO port to 1 in an atomic operation.
35
36@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
37@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id
38 If multiple pins are to be changed, use bitwise OR '|' to separate
39 them.
40*/
41void gpio_set(uint32_t gpioport, uint16_t gpios)
42{
43 GPIO_BSRR(gpioport) = gpios;
44}
45
46/*---------------------------------------------------------------------------*/
47/** @brief Clear a Group of Pins Atomic
48
49Clear one or more pins of the given GPIO port to 0 in an atomic operation.
50
51@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
52@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id
53 If multiple pins are to be changed, use bitwise OR '|' to separate
54 them.
55*/
56void gpio_clear(uint32_t gpioport, uint16_t gpios)
57{
58 GPIO_BSRR(gpioport) = (gpios << 16);
59}
60
61/*---------------------------------------------------------------------------*/
62/** @brief Read a Group of Pins.
63
64@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
65@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id
66 If multiple pins are to be read, use bitwise OR '|' to separate
67 them.
68@return Unsigned int16 value of the pin values. The bit position of the pin
69 value returned corresponds to the pin number.
70*/
71uint16_t gpio_get(uint32_t gpioport, uint16_t gpios)
72{
73 return gpio_port_read(gpioport) & gpios;
74}
75
76/*---------------------------------------------------------------------------*/
77/** @brief Toggle a Group of Pins
78
79Toggle one or more pins of the given GPIO port. The toggling is not atomic, but
80the non-toggled pins are not affected.
81
82@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
83@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id
84 If multiple pins are to be changed, use bitwise OR '|' to separate
85 them.
86*/
87void gpio_toggle(uint32_t gpioport, uint16_t gpios)
88{
89 uint32_t port = GPIO_ODR(gpioport);
90 GPIO_BSRR(gpioport) = ((port & gpios) << 16) | (~port & gpios);
91}
92
93/*---------------------------------------------------------------------------*/
94/** @brief Read from a Port
95
96Read the current value of the given GPIO port. Only the lower 16 bits contain
97valid pin data.
98
99@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
100@return Unsigned int16. The value held in the specified GPIO port.
101*/
102uint16_t gpio_port_read(uint32_t gpioport)
103{
104 return (uint16_t)GPIO_IDR(gpioport);
105}
106
107/*---------------------------------------------------------------------------*/
108/** @brief Write to a Port
109
110Write a value to the given GPIO port.
111
112@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
113@param[in] data Unsigned int16. The value to be written to the GPIO port.
114*/
115void gpio_port_write(uint32_t gpioport, uint16_t data)
116{
117 GPIO_ODR(gpioport) = data;
118}
119
120/*---------------------------------------------------------------------------*/
121/** @brief Lock the Configuration of a Group of Pins
122
123The configuration of one or more pins of the given GPIO port is locked. There
124is no mechanism to unlock these via software. Unlocking occurs at the next
125reset.
126
127@param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id
128@param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id
129 If multiple pins are to be locked, use bitwise OR '|' to separate
130 them.
131*/
132void gpio_port_config_lock(uint32_t gpioport, uint16_t gpios)
133{
134 uint32_t reg32;
135
136 /* Special "Lock Key Writing Sequence", see datasheet. */
137 GPIO_LCKR(gpioport) = GPIO_LCKK | gpios; /* Set LCKK. */
138 GPIO_LCKR(gpioport) = ~GPIO_LCKK & gpios; /* Clear LCKK. */
139 GPIO_LCKR(gpioport) = GPIO_LCKK | gpios; /* Set LCKK. */
140 reg32 = GPIO_LCKR(gpioport); /* Read LCKK. */
141 reg32 = GPIO_LCKR(gpioport); /* Read LCKK again. */
142
143 /* Tell the compiler the variable is actually used. It will get
144 * optimized out anyways.
145 */
146 reg32 = reg32;
147
148 /* If (reg32 & GPIO_LCKK) is true, the lock is now active. */
149}
150
151/**@}*/
152
#define GPIO_ODR(port)
#define GPIO_LCKR(port)
#define GPIO_IDR(port)
#define GPIO_BSRR(port)
#define GPIO_LCKK
void gpio_toggle(uint32_t gpioport, uint16_t gpios)
Toggle a Group of Pins.
uint16_t gpio_get(uint32_t gpioport, uint16_t gpios)
Read a Group of Pins.
void gpio_port_config_lock(uint32_t gpioport, uint16_t gpios)
Lock the Configuration of a Group of Pins.
void gpio_set(uint32_t gpioport, uint16_t gpios)
Set a Group of Pins Atomic.
void gpio_clear(uint32_t gpioport, uint16_t gpios)
Clear a Group of Pins Atomic.
uint16_t gpio_port_read(uint32_t gpioport)
Read from a Port.
void gpio_port_write(uint32_t gpioport, uint16_t data)
Write to a Port.