1 /* USRP E310 IO helpers
2 * Copyright (C) 2014 Ettus Research
3 * This file is part of the USRP E310 Firmware
4 * The USRP E310 Firmware is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 * The USRP E310 Firmware is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with the USRP E310 Firmware. If not, see <http://www.gnu.org/licenses/>.
14 */
15 
16 #include <avr/io.h>
17 
18 #include "io.h"
19 #include "utils.h"
20 
21 #define _GET_PIN(pin)           ((pin) & 0xf)
22 #define _GET_MASK(pin)          (BIT(_GET_PIN(pin)))
23 #define _GET_REG(pin, reg_x)    (*reg_x[pin >> 4])
24 
25 static volatile uint8_t *ddr_x[] = {&DDRA, &DDRB, &DDRC, &DDRD};
26 static volatile uint8_t *port_x[] = {&PORTA, &PORTB, &PORTC, &PORTD};
27 static volatile uint8_t *pin_x[] = {&PINA, &PINB, &PINC, &PIND};
28 
io_output_pin(io_pin_t pin)29 void io_output_pin(io_pin_t pin)
30 {
31 	_GET_REG(pin, ddr_x) |= _GET_MASK(pin);
32 }
33 
io_input_pin(io_pin_t pin)34 void io_input_pin(io_pin_t pin)
35 {
36 	_GET_REG(pin, ddr_x) &= ~_GET_MASK(pin);
37 }
38 
io_is_output(io_pin_t pin)39 bool io_is_output(io_pin_t pin)
40 {
41 	return bit_is_set(_GET_REG(pin, ddr_x), _GET_PIN(pin));
42 }
43 
io_is_input(io_pin_t pin)44 bool io_is_input(io_pin_t pin)
45 {
46 	return !io_is_output(pin);
47 }
48 
io_set_pin(io_pin_t pin)49 void io_set_pin(io_pin_t pin)
50 {
51 	_GET_REG(pin, port_x) |= _GET_MASK(pin);
52 }
53 
io_clear_pin(io_pin_t pin)54 void io_clear_pin(io_pin_t pin)
55 {
56 	_GET_REG(pin, port_x) &= ~_GET_MASK(pin);
57 }
58 
io_is_pin_set(io_pin_t pin)59 bool io_is_pin_set(io_pin_t pin)
60 {
61 	return bit_is_set(_GET_REG(pin, port_x), _GET_PIN(pin));
62 }
63 
io_test_pin(io_pin_t pin)64 bool io_test_pin(io_pin_t pin)
65 {
66 	return bit_is_set(_GET_REG(pin, pin_x), _GET_PIN(pin));
67 }
68