aboutsummaryrefslogtreecommitdiff
path: root/GPIO.h
blob: 974a77801737176494a079a6ebc39cd5c4f784c4 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*! \defgroup gpio GPIO
 * \brief General-purpose input/output
 *
 * This module defines macros for input/output pins of microcontroller.
 * Pin is specified with SET and PIN. SET is letter and PIN is number. Combination
 * of SET and PIN specifies hardware pin. You can find it in documentation.
 *
 * To use this module:
 *
 *     #include "avr-ioe/GPIO.h"
 */
#include <avr/io.h>
#include <stdint.h>

#ifdef IOE_CONFIG
#include <AVR-IOE-CONFIG.h>
#endif /* IOE_CONFIG */

#ifndef _IOE_GPIO_H_
#define _IOE_GPIO_H_

#define __GPIO_JOIN2(TXT1, TXT2) TXT1##TXT2
#define __GPIO_JOIN3(TXT1, TXT2, TXT3) TXT1##TXT2##TXT3

/*! \ingroup gpio
 * Return direction of specified pin. Pin is set as output if value is nonzero
 * and as input if returned value is zero.
 */
#define gpio_get_mode(SET, PIN)                                                 \
    __GPIO_JOIN2(DDR, SET) & _BV(__GPIO_JOIN3(DD, SET, PIN))

/*! \ingroup gpio
 * Set specified pin as output.
 * \param SET -- pin set (letter)
 * \param PIN -- pin number (number)
 */
#define gpio_set_out(SET, PIN)                                                  \
    __GPIO_JOIN2(DDR, SET) |= _BV(__GPIO_JOIN3(DD, SET, PIN))

/*! \ingroup gpio
 * Set specific pin as input.
 * \param SET -- pin set (letter)
 * \param PIN -- pin number (number)
 */
#define gpio_set_in(SET, PIN)                                                   \
    __GPIO_JOIN2(DDR, SET) &= ~_BV(__GPIO_JOIN3(DD, SET, PIN))

// -- GPIO output section --
/*! \defgroup gpio_out GPIO output
 * \ingroup gpio
 */
/*! \ingroup gpio_out
 * Set pin high. Pin must be set as output. Undefined behavior for non
 * output pins.
 * \param SET -- pin set (letter)
 * \param PIN -- pin number (number)
 */
#define gpio_set_high(SET, PIN)                                                 \
    __GPIO_JOIN2(PORT, SET) |= _BV(__GPIO_JOIN3(PORT, SET, PIN))

/*! \ingroup gpio_out
 * Set pin low. Pin must be set as output. Undefined behavior for non output pins.
 * \param SET -- pin set (letter)
 * \param PIN -- pin number (number)
 */
#define gpio_set_low(SET, PIN)                                                  \
    __GPIO_JOIN2(PORT, SET) &= ~_BV(__GPIO_JOIN3(PORT, SET, PIN))

/*! \ingroup gpio_out
 * Set pin according to \a VALUE. Pin must be set as output. Undefined behavior
 * for non output pins.
 * \warning Use this only if VALUE is runtime variable. For constants are prefered
 * gpio_set_high() and gpio_set_low().
 * \param SET -- pin set (letter)
 * \param PIN -- pin number (number)
 * \param VALUE -- runtime variable or boolean expression used in boolean context.
 */
#define gpio_set(SET, PIN, VALUE)                                               \
    if (VALUE)                                                                  \
        gpio_set_high(SET, PIN);                                                \
    else                                                                        \
        gpio_set_low(SET, PIN);

/*! \ingroup gpio_out
 * Set pin opposite state. If it is set as high it will be low and if it is set
 * as low it will be high. Pin must be set as output. Undefined behavior for non
 * output pins.
 * \param SET -- pin set (letter)
 * \param PIN -- pin number (number)
 */
#define gpio_set_not(SET, PIN)                                                  \
    __GPIO_JOIN2(PORT, SET) ^= _BV(__GPIO_JOIN3(PORT, SET, PIN))

// -- GPIO input section --
/*! \defgroup gpio_in GPIO input
 * \ingroup gpio
 */
// TODO whole output and switching betwen input and output

#endif /* _IOE_GPIO_H_ */