libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio.c
Go to the documentation of this file.
1/** @addtogroup gpio_defines
2 *
3 * @brief <b>Access functions for the SAM4 I/O Controller</b>
4 * @ingroup SAM4_defines
5 * LGPL License Terms @ref lgpl_license
6 * @author @htmlonly &copy; @endhtmlonly 2016
7 * Maxim Sloyko <maxims@google.com>
8 *
9 */
10
11/*
12 * This file is part of the libopencm3 project.
13 *
14 * Copyright (C) 2012 Gareth McMullin <gareth@blacksphere.co.nz>
15 * Copyright (C) 2014 Felix Held <felix-libopencm3@felixheld.de>
16 *
17 * This library is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Lesser General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with this library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#include <libopencm3/sam/gpio.h>
32
33/** @brief Atomic set output
34 *
35 * @param[in] gpioport uint32_t: GPIO Port base address
36 * @param[in] gpios uint32_t
37 */
38void gpio_set(uint32_t gpioport, uint32_t gpios)
39{
40 GPIO_OVRS(gpioport) = gpios;
41}
42
43/** @brief Atomic clear output
44 *
45 * @param[in] gpioport uint32_t: GPIO Port base address
46 * @param[in] gpios uint32_t
47 */
48void gpio_clear(uint32_t gpioport, uint32_t gpios)
49{
50 GPIO_OVRC(gpioport) = gpios;
51}
52
53/** @brief Atomic toggle output
54 *
55 * @param[in] gpioport uint32_t: GPIO Port base address
56 * @param[in] gpios uint32_t
57 */
58void gpio_toggle(uint32_t gpioport, uint32_t gpios)
59{
60 GPIO_OVRT(gpioport) = gpios;
61}
62
63/** @brief Enable output pins.
64 *
65 * Onlyc the ones where bits are set to "1" are touched, everything else
66 * remains in the old state.
67 *
68 * @param[in] gpioport uint32_t: GPIO Port base address
69 * @param[in] gpios uint32_t
70 * @param[in] mode enum gpio_mode GPIO mode. IN, OUT or peripheral function.
71 */
72void gpio_enable(uint32_t gpioport, uint32_t gpios, enum gpio_mode mode)
73{
74 if (mode < GPIO_MODE_IN) {
75 GPIO_GPERC(gpioport) = gpios;
76 uint8_t i = 0;
77 for (; i < 3; ++i, mode >>= 1) {
78 GPIO_PMR_SETVAL(gpioport, i, mode & 1) = gpios;
79 }
80 } else if (mode == GPIO_MODE_OUT) {
81 GPIO_GPERS(gpioport) = gpios;
82 GPIO_ODERS(gpioport) = gpios;
83 } else if (mode == GPIO_MODE_IN) {
84 GPIO_GPERS(gpioport) = gpios;
85 GPIO_ODERC(gpioport) = gpios;
86 }
87}
88
89/** @brief Disable output pins.
90 *
91 * Onlyc the ones where bits are set to "1" are touched, everything else
92 * remains in the old state.
93 *
94 * @param[in] gpioport uint32_t: GPIO Port base address
95 * @param[in] gpios uint32_t
96 */
97void gpio_disable(uint32_t gpioport, uint32_t gpios)
98{
99 GPIO_GPERC(gpioport) = gpios;
100}
#define GPIO_OVRT(X)
Definition: 4l/gpio.h:94
#define GPIO_GPERC(X)
Definition: 4l/gpio.h:65
#define GPIO_GPERS(X)
Definition: 4l/gpio.h:64
#define GPIO_PMR_SETVAL(P, I, S)
Definition: 4l/gpio.h:69
gpio_mode
Definition: 4l/gpio.h:159
@ GPIO_MODE_OUT
Definition: 4l/gpio.h:170
@ GPIO_MODE_IN
Definition: 4l/gpio.h:169
#define GPIO_ODERS(X)
Definition: 4l/gpio.h:87
#define GPIO_OVRC(X)
Definition: 4l/gpio.h:93
#define GPIO_ODERC(X)
Definition: 4l/gpio.h:88
#define GPIO_OVRS(X)
Definition: 4l/gpio.h:92
void gpio_set(uint32_t gpioport, uint32_t gpios)
Atomic set output.
Definition: gpio.c:38
void gpio_enable(uint32_t gpioport, uint32_t gpios, enum gpio_mode mode)
Enable output pins.
Definition: gpio.c:72
void gpio_disable(uint32_t gpioport, uint32_t gpios)
Disable output pins.
Definition: gpio.c:97
void gpio_clear(uint32_t gpioport, uint32_t gpios)
Atomic clear output.
Definition: gpio.c:48
void gpio_toggle(uint32_t gpioport, uint32_t gpios)
Atomic toggle output.
Definition: gpio.c:58