libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio_common.c
Go to the documentation of this file.
1/** @addtogroup gpio_file GPIO peripheral API
2 * @ingroup peripheral_apis
3 */
4/*
5 * This file is part of the libopencm3 project.
6 *
7 * Copyright (C) 2015 Kuldeep Singh Dhaka <kuldeepdhaka9@gmail.com>
8 *
9 * This library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22
24
25/**@{*/
26
27/**
28 * Enable GPIO registers lock.
29 * @see gpio_disable_lock()
30 * @see gpio_get_lock_flag()
31 */
33{
35}
36
37/**
38 * Disable GPIO registers lock.
39 * @see gpio_enable_lock()
40 * @see gpio_get_lock_flag()
41 */
43{
45}
46
47/**
48 * Get GPIO register lock flag
49 * @retval true if flag is set
50 * @retval false if flag is not set
51 * @see gpio_enable_lock()
52 * @see gpio_disable_lock()
53 */
55{
58}
59
60/**
61 * Set port pins drive strength
62 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
63 * @param[in] drive_stength Driver Stength (use GPIO_STENGTH_*)
64 */
65void gpio_set_drive_strength(uint32_t gpio_port,
66 enum gpio_drive_strength drive_stength)
67{
68 GPIO_P_CTRL(gpio_port) = GPIO_P_CTRL_DRIVEMODE(drive_stength);
69}
70
71/**
72 * Set port pins mode
73 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
74 * @param[in] mode Mode (use GPIO_MODE_*)
75 * @param[in] gpios (pins mask (use GPIO* ex . GPIO0, GPIO1 .... GPIO_ALL,
76 * use bitwise OR '|' to separate)
77 */
78void gpio_mode_setup(uint32_t gpio_port, enum gpio_mode mode, uint16_t gpios)
79{
80 unsigned i;
81
82 uint32_t high = GPIO_P_MODEH(gpio_port);
83 uint32_t low = GPIO_P_MODEL(gpio_port);
84
85 for (i = 0; i < 8; i++) {
86 if (gpios & (1 << i)) {
87 low &= ~GPIO_P_MODE_MODEx_MASK(i);
88 low |= GPIO_P_MODE_MODEx(i, mode);
89 }
90
91 if (gpios & (1 << (i + 8))) {
92 high &= ~GPIO_P_MODE_MODEx_MASK(i);
93 high |= GPIO_P_MODE_MODEx(i, mode);
94 }
95 }
96
97 GPIO_P_MODEL(gpio_port) = low;
98 GPIO_P_MODEH(gpio_port) = high;
99}
100
101/**
102 * Set port pins output value (Atomic)
103 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
104 * @param[in] gpios (pins mask (use GPIO* ex . GPIO0, GPIO1 .... GPIO_ALL,
105 * use bitwise OR '|' to separate)
106 */
107void gpio_set(uint32_t gpio_port, uint16_t gpios)
108{
109 GPIO_P_DOUTSET(gpio_port) = gpios;
110}
111
112/**
113 * Set port pins output value (Atomic)
114 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
115 * @param[in] gpios (pins mask (use GPIO* ex . GPIO0, GPIO1 .... GPIO_ALL,
116 * use bitwise OR '|' to separate)
117 */
118void gpio_clear(uint32_t gpio_port, uint16_t gpios)
119{
120 GPIO_P_DOUTCLR(gpio_port) = gpios;
121}
122
123/**
124 * Get port pins input value
125 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
126 * @param[in] gpios (pins mask (use GPIO* ex . GPIO0, GPIO1 .... GPIO_ALL,
127 * use bitwise OR '|' to separate)
128 * @return masked pins value (separated by bitwise OR '|')
129 */
130uint16_t gpio_get(uint32_t gpio_port, uint16_t gpios)
131{
132 return GPIO_P_DIN(gpio_port) & gpios;
133}
134
135/**
136 * Toggle port pins output value (Atomic)
137 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
138 * @param[in] gpios (pins mask (use GPIO* ex . GPIO0, GPIO1 .... GPIO_ALL,
139 * use bitwise OR '|' to separate)
140 */
141void gpio_toggle(uint32_t gpio_port, uint16_t gpios)
142{
143 GPIO_P_DOUTTGL(gpio_port) = gpios;
144}
145
146/**
147 * Get port (all) input value's
148 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
149 * @return all pins input value
150 */
151uint16_t gpio_port_read(uint32_t gpio_port)
152{
153 return GPIO_P_DIN(gpio_port);
154}
155
156/**
157 * Set port (all) output value's
158 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
159 * @param[in] data Data (all pins output value)
160 */
161void gpio_port_write(uint32_t gpio_port, uint16_t data)
162{
163 GPIO_P_DOUT(gpio_port) = data;
164}
165
166/**
167 * @brief Lock the Configuration of a Group of Pins
168 *
169 * The configuration of one or more pins of the given GPIO port is locked.
170 * There is no mechanism to unlock these via software. Unlocking occurs at the
171 * next reset.
172 *
173 * @param[in] gpio_port GPIO Port (use GPIO* ex. GPIOA, GPIOB, ....)
174 * @param[in] gpios (pins mask (use GPIO* ex . GPIO0, GPIO1 .... GPIO_ALL,
175 * use bitwise OR '|' to separate)
176*/
177void gpio_port_config_lock(uint32_t gpio_port, uint16_t gpios)
178{
179 GPIO_P_PINLOCKN(gpio_port) = ~gpios;
180}
181
182/**@}*/
#define GPIO_LOCK_LOCKKEY_LOCK
Definition: gpio_common.h:231
gpio_drive_strength
Definition: gpio_common.h:294
#define GPIO_P_PINLOCKN(port)
Definition: gpio_common.h:165
#define GPIO_P_MODEL(port)
Definition: gpio_common.h:61
#define GPIO_LOCK
Definition: gpio_common.h:182
#define GPIO_LOCK_LOCKKEY_MASK
Definition: gpio_common.h:228
gpio_mode
Definition: gpio_common.h:274
#define GPIO_P_DOUT(port)
Definition: gpio_common.h:130
#define GPIO_P_CTRL_DRIVEMODE(v)
Definition: gpio_common.h:47
#define GPIO_P_MODE_MODEx(x, mode)
Definition: gpio_common.h:58
#define GPIO_P_DIN(port)
Definition: gpio_common.h:158
#define GPIO_P_DOUTCLR(port)
Definition: gpio_common.h:144
#define GPIO_P_MODEH(port)
Definition: gpio_common.h:95
#define GPIO_P_DOUTTGL(port)
Definition: gpio_common.h:151
#define GPIO_P_CTRL(port)
Definition: gpio_common.h:37
#define GPIO_LOCK_LOCKKEY_UNLOCK
Definition: gpio_common.h:232
#define GPIO_P_DOUTSET(port)
Definition: gpio_common.h:137
#define GPIO_LOCK_LOCKKEY_LOCKED
Definition: gpio_common.h:230
void gpio_set_drive_strength(uint32_t gpio_port, enum gpio_drive_strength drive_stength)
Set port pins drive strength.
Definition: gpio_common.c:65
void gpio_port_write(uint32_t gpio_port, uint16_t data)
Set port (all) output value's.
Definition: gpio_common.c:161
void gpio_enable_lock(void)
Enable GPIO registers lock.
Definition: gpio_common.c:32
bool gpio_get_lock_flag(void)
Get GPIO register lock flag.
Definition: gpio_common.c:54
void gpio_clear(uint32_t gpio_port, uint16_t gpios)
Set port pins output value (Atomic)
Definition: gpio_common.c:118
void gpio_disable_lock(void)
Disable GPIO registers lock.
Definition: gpio_common.c:42
void gpio_mode_setup(uint32_t gpio_port, enum gpio_mode mode, uint16_t gpios)
Set port pins mode.
Definition: gpio_common.c:78
void gpio_toggle(uint32_t gpio_port, uint16_t gpios)
Toggle port pins output value (Atomic)
Definition: gpio_common.c:141
uint16_t gpio_port_read(uint32_t gpio_port)
Get port (all) input value's.
Definition: gpio_common.c:151
void gpio_set(uint32_t gpio_port, uint16_t gpios)
Set port pins output value (Atomic)
Definition: gpio_common.c:107
void gpio_port_config_lock(uint32_t gpio_port, uint16_t gpios)
Lock the Configuration of a Group of Pins.
Definition: gpio_common.c:177
uint16_t gpio_get(uint32_t gpio_port, uint16_t gpios)
Get port pins input value.
Definition: gpio_common.c:130