This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
parallel_io.h
153 lines (131 loc) · 3.21 KB
/
parallel_io.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2009 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Templates for using full ports or half-ports for parallel output
#ifndef AVRLIB_PARALLEL_H_
#define AVRLIB_PARALLEL_H_
#include <avr/io.h>
#include "avrlib/gpio.h"
namespace avrlib {
enum ParallelPortMode {
PARALLEL_BYTE,
PARALLEL_NIBBLE_HIGH,
PARALLEL_NIBBLE_LOW,
PARALLEL_TRIPLE_HIGHEST,
PARALLEL_TRIPLE_HIGH,
PARALLEL_TRIPLE_LOW,
PARALLEL_DOUBLE_HIGH,
PARALLEL_DOUBLE_MIDHIGH,
PARALLEL_DOUBLE_MIDLOW,
PARALLEL_DOUBLE_LOW
};
template<ParallelPortMode mode>
struct ShiftMasks {
enum Masks {
mask = 0xff,
shift = 0
};
};
template<>
struct ShiftMasks<PARALLEL_NIBBLE_HIGH> {
enum Masks {
mask = 0xf0,
shift = 4,
};
};
template<>
struct ShiftMasks<PARALLEL_NIBBLE_LOW> {
enum Masks {
mask = 0x0f,
shift = 0,
};
};
template<>
struct ShiftMasks<PARALLEL_TRIPLE_HIGHEST> {
enum Masks {
mask = 0xe0,
shift = 5,
};
};
template<>
struct ShiftMasks<PARALLEL_TRIPLE_HIGH> {
enum Masks {
mask = 0x38,
shift = 3,
};
};
template<>
struct ShiftMasks<PARALLEL_TRIPLE_LOW> {
enum Masks {
mask = 0x07,
shift = 0,
};
};
template<>
struct ShiftMasks<PARALLEL_DOUBLE_HIGH> {
enum Masks {
mask = 0xc0,
shift = 6,
};
};
template<>
struct ShiftMasks<PARALLEL_DOUBLE_MIDHIGH> {
enum Masks {
mask = 0x30,
shift = 4,
};
};
template<>
struct ShiftMasks<PARALLEL_DOUBLE_MIDLOW> {
enum Masks {
mask = 0x0c,
shift = 2,
};
};
template<>
struct ShiftMasks<PARALLEL_DOUBLE_LOW> {
enum Masks {
mask = 0x03,
shift = 0,
};
};
template<typename Port, ParallelPortMode parallel_mode = PARALLEL_BYTE>
struct ParallelPort {
typedef ShiftMasks<parallel_mode> Masks;
// Mode change.
static inline void set_mode(uint8_t mode) {
uint8_t preserve = (*Port::Mode::ptr() & ~Masks::mask);
if (mode == DIGITAL_INPUT) {
*Port::Mode::ptr() = preserve;
} else if (mode == DIGITAL_OUTPUT) {
*Port::Mode::ptr() = preserve | Masks::mask;
}
}
static inline void Write(uint8_t value) {
uint8_t preserve = *Port::Output::ptr() & ~Masks::mask;
*Port::Output::ptr() = preserve | (value << Masks::shift);
}
static inline void EnablePullUpResistors() {
uint8_t preserve = *Port::Output::ptr();
*Port::Output::ptr() = preserve | Masks::mask;
}
static inline uint8_t Read() {
return (*Port::Input::ptr() & Masks::mask) >> Masks::shift;
}
};
} // namespace avrlib
#endif // AVRLIB_PARALLEL_H_