aboutsummaryrefslogtreecommitdiff
path: root/GPIO.h
blob: 0c7ba72663114c027a9f635cc32dc63278e6c191 (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
#include <avr/io.h>
#include <stdint.h>

#ifndef _IOE_GPIO_H_
#define _IOE_GPIO_H_

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

#define gpio_get_mode(SET, PIN)                                                 \
    __GPIO_JOIN2(DDR, SET) & _BV(__GPIO_JOIN3(DD, SET, PIN))

// GPIO output section
#define gpio_set_out(SET, PIN)                                                  \
    __GPIO_JOIN2(DDR, SET) |= _BV(__GPIO_JOIN3(DD, SET, PIN))

#define gpio_set_high(SET, PIN)                                                 \
    __GPIO_JOIN2(PORT, SET) |= _BV(__GPIO_JOIN3(PORT, SET, PIN))

#define gpio_set_low(SET, PIN)                                                  \
    __GPIO_JOIN2(PORT, SET) &= ~_BV(__GPIO_JOIN3(PORT, SET, PIN))

#define gpio_set(SET, PIN, VALUE)                                               \
    if (value)                                                                  \
        gpio_set_high(SET, PIN);                                                \
    else                                                                        \
        gpio_set_low(SET, PIN);

// GPIO input section
#define gpio_set_in(SET, PIN)                                                   \
    __GPIO_JOIN2(DDR, SET) &= ~_BV(__GPIO_JOIN3(DD, SET, PIN))

// TODO whole output and switching betwen input and output

#endif /* _IOE_GPIO_H_ */