summaryrefslogtreecommitdiffstats
path: root/libopencm3/lib/sam/common/gpio.c
blob: 75929f82c3cb7256635383b01a8c167b078f5e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2012 Gareth McMullin <gareth@blacksphere.co.nz>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libopencm3/sam/gpio.h>

void gpio_init(uint32_t port, uint32_t pins, enum gpio_flags flags)
{
	switch (flags & 3) {
	case GPIO_FLAG_GPINPUT:
		/* input mode doesn't really exist, so we make a high
		 * output in open-drain mode
		 */
		PIO_SODR(port) = pins;
		flags |= GPIO_FLAG_OPEN_DRAIN;
		/* fall through */
	case GPIO_FLAG_GPOUTPUT:
		PIO_OER(port) = pins;
		PIO_PER(port) = pins;
		break;
	case GPIO_FLAG_PERIPHA:
		PIO_ABSR(port) &= ~pins;
		PIO_PDR(port) = pins;
		break;
	case GPIO_FLAG_PERIPHB:
		PIO_ABSR(port) |= pins;
		PIO_PDR(port) = pins;
	}

	if (flags & GPIO_FLAG_OPEN_DRAIN) {
		PIO_MDER(port) = pins;
	} else {
		PIO_MDDR(port) = pins;
	}

	if (flags & GPIO_FLAG_PULL_UP) {
		PIO_PUER(port) = pins;
	} else {
		PIO_PUDR(port) = pins;
	}
}

void gpio_toggle(uint32_t gpioport, uint32_t gpios)
{
	uint32_t odsr = PIO_ODSR(gpioport);
	PIO_CODR(gpioport) = odsr & gpios;
	PIO_SODR(gpioport) = ~odsr & gpios;
}