-
Notifications
You must be signed in to change notification settings - Fork 1
/
IOpin.hpp
342 lines (282 loc) · 7.08 KB
/
IOpin.hpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#pragma once
/*
* File: IOpin.h
* Author: Cameron Tacklind
*
* Created on May 5, 2010, 3:23 PM
*/
#include "Ports.hpp"
#include <avr/io.h>
namespace AVR {
using namespace Basic;
template <Ports Port, unsigned Pin>
class IOpin {
constexpr static auto PUE = u1(Port) - 0;
constexpr static auto PORT = u1(Port);
constexpr static auto DDR = u1(Port) - 1;
constexpr static auto PIN = u1(Port) - 2;
constexpr static u1 mask = u1(1 << Pin);
static_assert(Pin <= 8, "AVR has 8-bit IO Ports. '8' is allowed to disable a Pin");
public:
constexpr static bool isDummy = Pin == 8;
constexpr inline IOpin() {}
/**
* Sets bit in DDRx
*/
inline static void output() {
if (!mask) {
asm("; IOpin::output dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)DDR |= mask;
}
/**
* Clears bit in DDRx
*/
inline static void input() {
if (!mask) {
asm("; IOpin::input dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)DDR &= ~mask;
}
/**
* Sets bit in PUEx (or PORT)
*/
inline static void enablePullUp() {
if (!mask) {
asm("; IOpin::enablePullUp dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)PUE |= mask;
}
/**
* Clears bit in PUEx (or PORT)
*/
inline static void disablePullUp() {
if (!mask) {
asm("; IOpin::disablePullUp dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)PUE &= ~mask;
}
/**
* Clears bit in PUEx (or PORT)
*/
inline static void setPullUp(bool v) {
if (!mask) {
asm("; IOpin::setPullUp dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
v ? enablePullUp() : disablePullUp();
}
/**
* Sets bit in PORTx
*/
inline static void set() {
if (!mask) {
asm("; IOpin::set dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)PORT |= mask;
}
/**
* Clears bit in PORTx
*/
inline static void clr() {
if (!mask) {
asm("; IOpin::clr dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)PORT &= ~mask;
}
/**
* Sets bit in PINx
*/
inline static void tgl() {
if (!mask) {
asm("; IOpin::tgl dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
*(volatile uint8_t *)PIN = mask;
}
/**
* Returns value of bit in PINx
*/
inline static bool isHigh() {
if (!mask) {
asm("; IOpin::isHigh dummy. %[PORT]" ::[PORT] "I"(Port));
return false;
}
return *(volatile uint8_t *)PIN & mask;
}
/**
* Returns value of bit in PORTx
*/
inline static bool isDriveHigh() {
if (!mask) {
asm("; IOpin::isDriveHigh dummy. %[PORT]" ::[PORT] "I"(Port));
return false;
}
return *(volatile uint8_t *)PORT & mask;
}
/**
* Returns value of bit in DDRx
*/
inline static bool isOutputEnabled() {
if (!mask) {
asm("; IOpin::isOutputEnabled dummy. %[PORT]" ::[PORT] "I"(Port));
return false;
}
return *(volatile uint8_t *)DDR & mask;
}
/**
* set() or clr() based on v
*/
inline static void set(bool v) {
if (!mask) {
asm("; IOpin::set dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
v ? set() : clr();
}
/**
* set() or clr() based on v
*/
inline bool operator=(bool v) {
if (!mask) {
asm("; IOpin::operator=(bool v) dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
set(v);
return v;
}
/**
* Toggles the output
*/
inline bool operator++(int) {
if (!mask) {
asm("; IOpin::operator++(int) dummy. %[PORT]" ::[PORT] "I"(Port));
return;
}
tgl();
return isDriveHigh();
}
};
template <Ports Port, unsigned Pin, bool inverted = false, bool startOn = false>
class WeakOutput : public IOpin<Port, Pin> {
protected:
using IOpin<Port, Pin>::input;
using IOpin<Port, Pin>::isDriveHigh;
public:
inline WeakOutput() {}
/**
* Turns on output, whatever logic level that is
*/
inline static void on() { IOpin<Port, Pin>::set(!inverted); }
/**
* Turns off output, whatever logic level that is
*/
inline static void off() { IOpin<Port, Pin>::set(inverted); }
/**
* Checks output state, based on if this Output is inverted or not
*/
inline static bool isOn() { return isDriveHigh() != inverted; }
/**
* Turns on() or off() based on v
*/
inline static void set(bool v) { v ? on() : off(); }
/**
* Turns on() or off() based on v
*/
inline bool operator=(bool v) {
set(v);
return v;
}
inline operator bool() const { return isOn(); }
inline static void init() {
input();
set(startOn);
}
};
template <Ports Port, unsigned Pin, bool inverted = false, bool startOn = false>
class Output : public WeakOutput<Port, Pin, inverted, startOn> {
protected:
using IOpin<Port, Pin>::output;
using WeakOutput<Port, Pin, inverted, startOn>::input;
using WeakOutput<Port, Pin, inverted, startOn>::output;
using WeakOutput<Port, Pin, inverted, startOn>::isDriveHigh;
public:
inline Output() {}
using WeakOutput<Port, Pin, inverted, startOn>::on;
using WeakOutput<Port, Pin, inverted, startOn>::off;
using WeakOutput<Port, Pin, inverted, startOn>::isOn;
using WeakOutput<Port, Pin, inverted, startOn>::set;
using WeakOutput<Port, Pin, inverted, startOn>::operator=;
using WeakOutput<Port, Pin, inverted, startOn>::operator bool;
inline static void init() {
set(startOn);
output();
}
};
template <Ports Port, unsigned Pin, bool activeLow = true, bool pullUp = activeLow>
class Input : public IOpin<Port, Pin> {
using IOpin<Port, Pin>::setPullUp;
using IOpin<Port, Pin>::isHigh;
using IOpin<Port, Pin>::input;
public:
inline Input() {}
inline static bool isActive() { return isHigh() != activeLow; }
inline operator bool() const { return isActive(); }
inline static void init() {
input();
setPullUp(pullUp);
}
};
template <Ports Port, unsigned Pin, bool activeLow = true, bool pullUp = false>
class OpenDrain : public IOpin<Port, Pin> {
using IOpin<Port, Pin>::setPullUp;
using IOpin<Port, Pin>::isHigh;
using IOpin<Port, Pin>::output;
using IOpin<Port, Pin>::input;
using IOpin<Port, Pin>::clr;
using IOpin<Port, Pin>::set;
using IOpin<Port, Pin>::isOutputEnabled;
public:
inline OpenDrain() {}
inline static bool isSink() { return isOutputEnabled(); }
inline static bool isOpen() { return !isOutputEnabled(); }
inline static bool isActive() { return isHigh() != activeLow; }
inline operator bool() const { return isActive(); }
inline static void sink() {
if (pullUp) { clr(); }
output();
}
inline static void open() {
input();
if (pullUp) { set(); }
}
inline static void tgl() {
if (isSink()) {
open();
} else {
sink();
}
}
/**
* sink() or open() based on v
*/
inline static void set(bool v) { (v == activeLow) ? sink() : open(); }
/**
* sink() or open() based on v
*/
inline bool operator=(bool v) {
set(v);
return v;
}
inline static void init() {
input();
if (pullUp) { setPullUp(pullUp); }
}
};
}; // namespace AVR