-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGpio.hpp
286 lines (238 loc) · 8.36 KB
/
Gpio.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
//Gpio.hpp
#pragma once
#include "MyStm32.hpp"
/*--------------------------------------------------------------
enum additions to the PINS namespace for GpioPin use
--------------------------------------------------------------*/
namespace PINS {
enum
MODE { INPUT, OUTPUT, ALTERNATE, ANALOG };
enum
OTYPE { PUSHPULL, ODRAIN };
enum
PULL { NOPULL, PULLUP, PULLDOWN };
enum
SPEED { SPEED0, SPEED1, SPEED2, SPEED3 }; //VLOW to VHIGH
enum
ALTFUNC { AF0, AF1, AF2, AF3, AF4, AF5, AF6, AF7 };
enum
INVERT { HIGHISON, LOWISON };
};
/*--------------------------------------------------------------
GpioPort class
--------------------------------------------------------------*/
struct GpioPort {
//-------------|
protected:
//-------------|
u8 port_; //port number(letter)
//-------------|
public:
//-------------|
GPIO_TypeDef& reg_;
II
GpioPort (PINS::PIN pin)
: port_( pin/16 ),
reg_( *(GPIO_TypeDef*)(GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE)*(pin/16)) )
{
}
II auto
enable () { RCC->IOPENR or_eq (1<<port_); }
//lock pin(s) on this port (bitmask)
II auto
lock (u16 bm)
{
u32 vL = (1<<16) bitor bm;
reg_.LCKR = vL;
reg_.LCKR = bm;
reg_.LCKR = vL;
return (reg_.LCKR bitand vL) == vL;
}
};
/*--------------------------------------------------------------
GpioPin class
--------------------------------------------------------------*/
struct GpioPin : GpioPort {
//-------------|
private:
//-------------|
u8 pin_; //0-15
u16 pinmask_; //for bsr/bsrr
bool invert_; //so can do on/off
//-------------|
public:
//-------------|
//no init, rcc clock enabled by default unless not wanted
II
GpioPin (PINS::PIN pin, PINS::INVERT inv = PINS::HIGHISON, bool clken = true)
: GpioPort(pin), pin_(pin%16), pinmask_(1<<(pin%16)), invert_(inv)
{
if( clken ) enable();
}
// properties
II auto
lock () { GpioPort::lock(pinmask_); return *this; } //lock return value ignored
II auto
mode (PINS::MODE e)
{
auto bp = 2*pin_, bmclr = compl (3<<bp), bmset = e<<bp;
reg_.MODER = (reg_.MODER bitand bmclr) bitor bmset;
return *this;
}
II auto
outType (PINS::OTYPE e)
{
if( e == PINS::ODRAIN ) reg_.OTYPER or_eq pinmask_;
else reg_.OTYPER and_eq compl pinmask_;
return *this;
}
II auto
pull (PINS::PULL e)
{
auto bp = 2*pin_, bmclr = compl (3<<bp), bmset = e<<bp;
reg_.PUPDR = (reg_.PUPDR bitand bmclr) bitor bmset;
return *this;
}
II auto
speed (PINS::SPEED e)
{
auto bp = 2*pin_, bmclr = compl (3<<bp), bmset = e<<bp;
reg_.OSPEEDR = (reg_.OSPEEDR bitand bmclr) bitor bmset;
return *this;
}
II auto
altFunc (PINS::ALTFUNC e)
{
auto& r = reg_.AFR[pin_>7 ? 1 : 0];
auto bp = 4*(pin_ bitand 7), bmclr = compl (15<<bp), bmset = e<<bp;
r = (r bitand bmclr) bitor bmset;
mode( PINS::ALTERNATE );
return *this;
}
// irq
//get rising flag, clear if set
II bool
isFlagRise ()
{
auto bm = EXTI->RPR1 bitand pinmask_;
EXTI->RPR1 = bm;
return bm;
}
//get falling flag, clear if set
II bool
isFlagFall ()
{
auto bm = EXTI->FPR1 bitand pinmask_;
EXTI->FPR1 = bm;
return bm;
}
//get any flag, clear if set
II bool
isFlag ()
{ //get both so the 'or' does not leave the second untouched if set
bool r = isFlagRise(), f = isFlagFall();
return r or f;
}
II auto
irqOff ()
{
EXTI->IMR1 and_eq compl pinmask_;
return *this;
}
II auto
irqOn ()
{
RCC->APBENR2 or_eq RCC_APBENR2_SYSCFGEN; //so can read EXTI_LINEx
//set our port to use this pin irq
auto& r = EXTI->EXTICR[pin_/4];
auto bp = (pin_ bitand 3)*8 /*0,8,16,24*/, bmset = port_<<bp, bmclr = compl (0xFF<<bp);
r = (r bitand bmclr) bitor bmset;
isFlag(); //clear flags
EXTI->IMR1 or_eq pinmask_;
return *this;
}
II auto
irqNoEdges ()
{
EXTI->FTSR1 and_eq compl pinmask_;
EXTI->RTSR1 and_eq compl pinmask_;
return *this;
}
II auto
irqRising ()
{
EXTI->FTSR1 and_eq compl pinmask_;
EXTI->RTSR1 or_eq pinmask_;
return *this;
}
II auto
irqFalling ()
{
EXTI->RTSR1 and_eq compl pinmask_;
EXTI->FTSR1 or_eq pinmask_;
return *this;
}
II auto
irqBothEdges ()
{
EXTI->FTSR1 or_eq pinmask_;
EXTI->RTSR1 or_eq pinmask_;
return *this;
}
//get which IRQn_Type we belong to
II IRQn_Type
irqN ()
{
return pin_ <= 1 ? EXTI0_1_IRQn :
pin_ <= 3 ? EXTI2_3_IRQn :
EXTI4_15_IRQn;
}
// read
II auto
pinVal () { return reg_.IDR bitand pinmask_; }
II auto
latVal () { return reg_.ODR bitand pinmask_; }
II auto
isHigh () { return pinVal(); }
II auto
isLow () { return not isHigh(); }
II auto
isOn () { return ( invert_ == PINS::LOWISON ) ? isLow() : isHigh(); }
II auto
isOff () { return not isOn(); }
// write
II auto
high () { reg_.BSRR = pinmask_; return *this; }
II auto
low () { reg_.BRR = pinmask_; return *this; }
II auto
on () { return invert_ == PINS::LOWISON ? low() : high(); }
II auto
off () { return invert_ == PINS::LOWISON ? high() : low(); }
II auto
on (bool tf) { return tf ? on() : off(); }
II auto
toggle () { return latVal() ? low() : high(); }
II auto
pulseHL () { high(); return low(); }
II auto
pulseLH () { low(); return high(); }
II auto
pulse () { return latVal() ? pulseLH() : pulseHL(); }
//back to reset state- if reconfiguring pin from an unknown state
II auto
deinit ()
{
mode(PINS::ANALOG).outType(PINS::PUSHPULL).altFunc(PINS::AF0)
.speed(PINS::SPEED0).pull(PINS::NOPULL).low();
//if we are on the sw port, and is a sw pin
//then we have a different reset state
if ( port_ == (PINS::SWCLK/16) and (pin_ == (PINS::SWCLK bitand 15)) ) {
pull( PINS::PULLDOWN ).mode( PINS::ALTERNATE );
}
if ( port_ == (PINS::SWCLK/16) and (pin_ == (PINS::SWDIO bitand 15)) ) {
pull( PINS::PULLUP ).speed( PINS::SPEED3 ).mode( PINS::ALTERNATE );
}
return *this;
}
};