libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio_common_3n3s.c
Go to the documentation of this file.
1/** @addtogroup gpio_defines
2 *
3 * @brief <b>Access functions for the SAM3N/S I/O Controller</b>
4 * @ingroup SAM3_defines
5 * LGPL License Terms @ref lgpl_license
6 * @author @htmlonly &copy; @endhtmlonly 2012
7 * Gareth McMullin <gareth@blacksphere.co.nz>
8 * @author @htmlonly &copy; @endhtmlonly 2014
9 * Felix Held <felix-libopencm3@felixheld.de>
10 *
11 */
12
13/*
14 * This file is part of the libopencm3 project.
15 *
16 * Copyright (C) 2012 Gareth McMullin <gareth@blacksphere.co.nz>
17 * Copyright (C) 2014 Felix Held <felix-libopencm3@felixheld.de>
18 *
19 * This library is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU Lesser General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with this library. If not, see <http://www.gnu.org/licenses/>.
31 */
32
33#include <libopencm3/sam/gpio.h>
34
35
36/** @brief Initialize GPIO pins
37 *
38 * @param[in] port uint32_t: GPIO Port base address
39 * @param[in] pins uint32_t bitfield of pins to initialize
40 * @param[in] flags enum gpio_flags
41 */
42void gpio_init(uint32_t port, uint32_t pins, enum gpio_flags flags)
43{
44 switch (flags & 0x7) {
45 case GPIO_FLAG_GPINPUT:
46 PIO_ODR(port) = pins;
47 PIO_PER(port) = pins;
48 break;
49 case GPIO_FLAG_GPOUTPUT:
50 PIO_OER(port) = pins;
51 PIO_PER(port) = pins;
52 break;
53 case GPIO_FLAG_PERIPHA:
54 PIO_ABCDSR1(port) &= ~pins;
55 PIO_ABCDSR2(port) &= ~pins;
56 PIO_PDR(port) = pins;
57 break;
58 case GPIO_FLAG_PERIPHB:
59 PIO_ABCDSR1(port) |= pins;
60 PIO_ABCDSR2(port) &= ~pins;
61 PIO_PDR(port) = pins;
62 break;
63 case GPIO_FLAG_PERIPHC:
64 PIO_ABCDSR1(port) &= ~pins;
65 PIO_ABCDSR2(port) |= pins;
66 PIO_PDR(port) = pins;
67 break;
68 case GPIO_FLAG_PERIPHD:
69 PIO_ABCDSR1(port) |= pins;
70 PIO_ABCDSR2(port) |= pins;
71 PIO_PDR(port) = pins;
72 break;
73 }
74
75 if (flags & GPIO_FLAG_OPEN_DRAIN) {
76 PIO_MDER(port) = pins;
77 } else {
78 PIO_MDDR(port) = pins;
79 }
80
81 if (flags & GPIO_FLAG_PULL_UP) {
82 PIO_PUER(port) = pins;
83 } else {
84 PIO_PUDR(port) = pins;
85 }
86}
void gpio_init(uint32_t port, uint32_t pins, enum gpio_flags flags)
Initialize GPIO pins.