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
|
#ifndef GPIO_H
#define GPIO_H
#include <stdint.h>
struct GPIO_reg_t {
#if defined(STM32F1)
volatile uint32_t CRL;
volatile uint32_t CRH;
volatile uint32_t IDR;
volatile uint32_t ODR;
volatile uint32_t BSRR;
volatile uint32_t BRR;
volatile uint32_t LCKR;
#else
volatile uint32_t MODER;
volatile uint32_t OTYPER;
volatile uint32_t OSPEEDR;
volatile uint32_t PUPDR;
volatile uint32_t IDR;
volatile uint32_t ODR;
volatile uint32_t BSRR;
volatile uint32_t LCKR;
volatile uint32_t AFRL;
volatile uint32_t AFRH;
#endif
};
class GPIO_t {
public:
GPIO_reg_t& reg;
class Pin {
private:
const GPIO_t& g;
int n;
public:
constexpr Pin(const GPIO_t& gpio, int pin) : g(gpio), n(pin) {}
enum Mode {
#if defined(STM32F1)
Input = 0x4,
InputPull = 0x8,
Output = 0x3,
AF = 0xb,
Analog = 0x0,
#else
Input,
Output,
AF,
Analog,
#endif
};
enum Type {
PushPull,
OpenDrain,
};
enum Pull {
PullNone,
PullUp,
PullDown,
};
enum Speed {
Low,
Medium,
Fast,
High,
};
void set_mode(Mode m) {
#if defined(STM32F1)
if(n < 8) {
g.reg.CRL = (g.reg.CRL & ~(0xf << (n * 4))) | m << (n * 4);
} else {
g.reg.CRH = (g.reg.CRH & ~(0xf << (n * 4 - 32))) | m << (n * 4 - 32);
}
#else
g.reg.MODER = (g.reg.MODER & ~(3 << (n * 2))) | m << (n * 2);
#endif
}
void set_type(Type t) {
#if defined(STM32F1)
// TODO: Unified configure() method?
#else
if(t) {
g.reg.OTYPER |= 1 << n;
} else {
g.reg.OTYPER &= ~(1 << n);
}
#endif
}
void set_pull(Pull p) {
#if defined(STM32F1)
// TODO: Unified configure() method?
#else
g.reg.PUPDR = (g.reg.PUPDR & ~(3 << (n * 2))) | p << (n * 2);
#endif
}
void set_af(int af) {
#if defined(STM32F1)
// TODO: Unified configure() method?
#else
if(n < 8) {
g.reg.AFRL = (g.reg.AFRL & ~(0xf << (n * 4))) | af << (n * 4);
} else {
g.reg.AFRH = (g.reg.AFRH & ~(0xf << (n * 4 - 32))) | af << (n * 4 - 32);
}
#endif
}
void set_speed(Speed s) {
#if defined(STM32F1)
// TODO: Unified configure() method?
#else
g.reg.OSPEEDR = (g.reg.OSPEEDR & ~(3 << (n * 2))) | s << (n * 2);
#endif
}
void on() {
g.reg.BSRR = 1 << n;
}
void off() {
g.reg.BSRR = 1 << 16 << n;
}
void set(bool value) {
if(value) {
on();
} else {
off();
}
}
bool get() {
return g.reg.IDR & (1 << n);
}
void toggle() {
set(!(g.reg.ODR & (1 << n)));
}
};
class PinArray {
private:
const GPIO_t& g;
int f;
int l;
constexpr uint32_t mask1() {
return ((2 << l) - 1) ^ ((1 << f) - 1);
}
constexpr uint32_t mask2() {
return ((4 << (l * 2)) - 1) ^ ((1 << (f * 2)) - 1);
}
public:
constexpr PinArray(const GPIO_t& gpio, int first, int last) : g(gpio), f(first), l(last) {}
#if ! defined(STM32F1)
void set_mode(Pin::Mode m) {
g.reg.MODER = (g.reg.MODER & ~mask2()) | ((0x55555555 * m) & mask2());
}
void set_type(Pin::Type t) {
g.reg.OTYPER = (g.reg.OTYPER & ~mask2()) | ((0x55555555 * t) & mask2());
}
void set_pull(Pin::Pull p) {
g.reg.PUPDR = (g.reg.PUPDR & ~mask2()) | ((0x55555555 * p) & mask2());
}
#endif
void set(uint16_t value) {
value <<= f;
g.reg.BSRR = ((~value & mask1()) << 16) | (value & mask1());
}
uint16_t get() {
return (g.reg.IDR & mask1()) >> f;
}
};
constexpr GPIO_t(uint32_t reg_addr) : reg(*(GPIO_reg_t*)reg_addr) {}
constexpr Pin operator[](int pin) {
return Pin(*this, pin);
}
constexpr PinArray array(int first, int last) {
return PinArray(*this, first, last);
}
};
typedef GPIO_t::Pin Pin;
typedef GPIO_t::PinArray PinArray;
#if defined(STM32F0)
static GPIO_t GPIOA(0x48000000);
static GPIO_t GPIOB(0x48000400);
static GPIO_t GPIOC(0x48000800);
static GPIO_t GPIOD(0x48000c00);
static GPIO_t GPIOE(0x48001000);
static GPIO_t GPIOF(0x48001400);
#elif defined(STM32F1)
static GPIO_t GPIOA(0x40010800);
static GPIO_t GPIOB(0x40010c00);
static GPIO_t GPIOC(0x40011000);
static GPIO_t GPIOD(0x40011400);
static GPIO_t GPIOE(0x40011800);
#elif defined(STM32F3)
static GPIO_t GPIOA(0x48000000);
static GPIO_t GPIOB(0x48000400);
static GPIO_t GPIOC(0x48000800);
static GPIO_t GPIOD(0x48000c00);
static GPIO_t GPIOE(0x48001000);
static GPIO_t GPIOF(0x48001400);
#elif defined(STM32F4)
static GPIO_t GPIOA(0x40020000);
static GPIO_t GPIOB(0x40020400);
static GPIO_t GPIOC(0x40020800);
static GPIO_t GPIOD(0x40020c00);
static GPIO_t GPIOE(0x40021000);
static GPIO_t GPIOF(0x40021400);
static GPIO_t GPIOG(0x40021800);
static GPIO_t GPIOH(0x40021c00);
static GPIO_t GPIOI(0x40022000);
#elif defined(STM32L0)
static GPIO_t GPIOA(0x50000000);
static GPIO_t GPIOB(0x50000400);
static GPIO_t GPIOC(0x50000800);
static GPIO_t GPIOD(0x50000c00);
static GPIO_t GPIOH(0x50001c00);
#endif
#endif
|