/
avr.h
234 lines (195 loc) · 8.24 KB
/
avr.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
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
/* Efficient and comprehensive MCU peripheral configuration library
* Copyright (c) 2019 Anton Konkevych.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef OPTI_INIT_AVR_H
#define OPTI_INIT_AVR_H
namespace opti_init
{
namespace hardware
{
template<pointer_int_t ptr_direction, pointer_int_t ptr_value, pointer_int_t ptr_read, int bit>
struct gpio {
static constexpr int bit_index = bit;
static constexpr int bit_mask = (1 << bit);
using input_any = modifier< ptr_direction, bit_mask, 0 >;
using output_any = modifier< ptr_direction, bit_mask, bit_mask >;
using low = modifier< ptr_value, bit_mask, 0 >;
using high = modifier< ptr_value, bit_mask, bit_mask >;
using output_low = list< low, output_any >;
using output_high = list< high, output_any >;
using input_floating = list< low, input_any>;
using input_pullup = list< high, input_any>;
using input = input_floating;
using output = output_any;
using ddr_register_bit = peripheral_register_bit<ptr_direction, bit>;
using port_register_bit = peripheral_register_bit<ptr_value, bit>;
using pin_register_bit = peripheral_register_bit<ptr_read, bit>;
template <bool value>
using set = typename port_register_bit::template set<value>;
static bool get_f() { return pin_register_bit::get_f(); }
static void set_f(bool value) { port_register_bit::set_f(value); }
// static const members should be initialized by constexpr, and
// reinterpret_cast is not allowed in constexpr, so we use
// constexpr function instead
static constexpr peripheral_register_t * ddr_reg_ptr() {
return reinterpret_cast<peripheral_register_t *>(ptr_direction);
}
static constexpr peripheral_register_t * port_reg_ptr() {
return reinterpret_cast<peripheral_register_t *>(ptr_value);
}
static constexpr peripheral_register_t * pin_reg_ptr() {
return reinterpret_cast<peripheral_register_t *>(ptr_read);
}
};
#define __AVR_PORT(X) \
template <int index> \
struct port##X : gpio<(pointer_int_t)(&DDR##X), (pointer_int_t)(&PORT##X), (pointer_int_t)(&PIN##X), index> {};
#ifdef PORTA
__AVR_PORT(A);
#endif
#ifdef PORTB
__AVR_PORT(B);
#endif
#ifdef PORTC
__AVR_PORT(C);
#endif
#ifdef PORTD
__AVR_PORT(D);
#endif
#ifdef PORTE
__AVR_PORT(E);
#endif
#ifdef PORTF
__AVR_PORT(F);
#endif
#ifdef PORTG
__AVR_PORT(G);
#endif
#ifdef PORTH
__AVR_PORT(H);
#endif
#ifdef PORTI
__AVR_PORT(I);
#endif
#ifdef PORTJ
__AVR_PORT(J);
#endif
#ifdef PORTK
__AVR_PORT(K);
#endif
#ifdef PORTL
__AVR_PORT(L);
#endif
#undef __AVR_PORT
#ifdef ARDUINO
/* OK OK, it may look stupid to fight for bytes while using Arduino platform with its
* ugly digitalRead/digitalWrite etc, but one may write efficient code even for
* Arduino. There is a pretty good digital[Read|Write]Fast library code which returns
* you to efficiency land, and empty Arduino program is not so far from empty bare
* avr-gcc program. So, if you're picky enough about libraries to use (both standard
* or 3rd party), you may have very effective code in spite of using Arduino codebase.
*/
// These defines are taken from digitalWriteFast.h library - https://codebender.cc/library/digitalWriteFast#bonus%2FdigitalWrite%2Fdigital_write_macros.h
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Arduino Mega Pins
#define digitalPinToPortReg(P) \
(((P) >= 22 && (P) <= 29) ? &PORTA : \
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &PORTB : \
(((P) >= 30 && (P) <= 37) ? &PORTC : \
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &PORTD : \
((((P) >= 0 && (P) <= 3) || (P) == 5) ? &PORTE : \
(((P) >= 54 && (P) <= 61) ? &PORTF : \
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &PORTG : \
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &PORTH : \
(((P) == 14 || (P) == 15) ? &PORTJ : \
(((P) >= 62 && (P) <= 69) ? &PORTK : &PORTL))))))))))
#define digitalPinToDDRReg(P) \
(((P) >= 22 && (P) <= 29) ? &DDRA : \
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &DDRB : \
(((P) >= 30 && (P) <= 37) ? &DDRC : \
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &DDRD : \
((((P) >= 0 && (P) <= 3) || (P) == 5) ? &DDRE : \
(((P) >= 54 && (P) <= 61) ? &DDRF : \
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &DDRG : \
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &DDRH : \
(((P) == 14 || (P) == 15) ? &DDRJ : \
(((P) >= 62 && (P) <= 69) ? &DDRK : &DDRL))))))))))
#define digitalPinToPINReg(P) \
(((P) >= 22 && (P) <= 29) ? &PINA : \
((((P) >= 10 && (P) <= 13) || ((P) >= 50 && (P) <= 53)) ? &PINB : \
(((P) >= 30 && (P) <= 37) ? &PINC : \
((((P) >= 18 && (P) <= 21) || (P) == 38) ? &PIND : \
((((P) >= 0 && (P) <= 3) || (P) == 5) ? &PINE : \
(((P) >= 54 && (P) <= 61) ? &PINF : \
((((P) >= 39 && (P) <= 41) || (P) == 4) ? &PING : \
((((P) >= 6 && (P) <= 9) || (P) == 16 || (P) == 17) ? &PINH : \
(((P) == 14 || (P) == 15) ? &PINJ : \
(((P) >= 62 && (P) <= 69) ? &PINK : &PINL))))))))))
#define __digitalPinToBit(P) \
(((P) >= 7 && (P) <= 9) ? (P) - 3 : \
(((P) >= 10 && (P) <= 13) ? (P) - 6 : \
(((P) >= 22 && (P) <= 29) ? (P) - 22 : \
(((P) >= 30 && (P) <= 37) ? 37 - (P) : \
(((P) >= 39 && (P) <= 41) ? 41 - (P) : \
(((P) >= 42 && (P) <= 49) ? 49 - (P) : \
(((P) >= 50 && (P) <= 53) ? 53 - (P) : \
(((P) >= 54 && (P) <= 61) ? (P) - 54 : \
(((P) >= 62 && (P) <= 69) ? (P) - 62 : \
(((P) == 0 || (P) == 15 || (P) == 17 || (P) == 21) ? 0 : \
(((P) == 1 || (P) == 14 || (P) == 16 || (P) == 20) ? 1 : \
(((P) == 19) ? 2 : \
(((P) == 5 || (P) == 6 || (P) == 18) ? 3 : \
(((P) == 2) ? 4 : \
(((P) == 3 || (P) == 4) ? 5 : 7)))))))))))))))
#else
// Standard Arduino Pins
#define digitalPinToPortReg(P) \
(((P) >= 0 && (P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : &PORTC))
#define digitalPinToDDRReg(P) \
(((P) >= 0 && (P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : &DDRC))
#define digitalPinToPINReg(P) \
(((P) >= 0 && (P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : &PINC))
#define __digitalPinToBit(P) \
(((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (P) - 14))
#endif
template <int number>
using digitalPin = typename gpio<(pointer_int_t)( digitalPinToDDRReg(number)),(pointer_int_t)( digitalPinToPortReg(number)),__digitalPinToBit(number)>;
#endif // ARDUINO
#ifdef OPTI_INIT_TESTS
namespace test
{
using namespace opti_init::test::utils;
// instantiate some classes
using my_gpio = gpio<1, 2, 3, 0>;
static_assert(my_gpio::bit_index == 0, "Bit index invalid");
static_assert(my_gpio::bit_mask == (1 << 0), "Bit mask invalid");
static_assert(my_gpio::ddr_reg_ptr() == (peripheral_register_t *)(1), "DDRx pointer invalid");
static_assert(my_gpio::port_reg_ptr() == (peripheral_register_t *)(2), "PORTx pointer invalid");
static_assert(my_gpio::pin_reg_ptr() == (peripheral_register_t *)(3), "PINx pointer invalid");
using m0 = my_gpio::set<0>;
using m1 = my_gpio::set<1>;
static_assert(is_same<m0, my_gpio::low >::value, "::set<0> isn't equal to ::low");
static_assert(is_same<m1, my_gpio::high >::value, "::set<1> isn't equal to ::high");
}
#endif // OPTI_INIT_TEST
}
}
#endif