forked from RebelTechnology/OpenWare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pin.h
258 lines (249 loc) · 6.8 KB
/
Pin.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#ifndef __PIN_H
#define __PIN_H
#include <stdint.h>
#include <stdbool.h>
#include "device.h"
#define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))
enum PinPull {
PIN_PULL_NONE = GPIO_NOPULL,
PIN_PULL_UP = GPIO_PULLUP,
PIN_PULL_DOWN = GPIO_PULLDOWN
};
enum PinSpeed {
PIN_SPEED_LOW = GPIO_SPEED_FREQ_LOW,
PIN_SPEED_MEDIUM = GPIO_SPEED_FREQ_MEDIUM,
PIN_SPEED_HIGH = GPIO_SPEED_FREQ_HIGH,
PIN_SPEED_VERY_HIGH = GPIO_SPEED_FREQ_VERY_HIGH
};
typedef uint16_t pin_t;
#define GPIO_OUTPUT_TYPE (0x00000010U)
class Pin {
private:
GPIO_TypeDef* port;
pin_t pin;
public:
Pin(GPIO_TypeDef* port, uint16_t pin) : port(port), pin(POSITION_VAL(pin)) {}
bool get(){
return port->IDR & (1<<pin);
}
void high(){
port->BSRR = (1<<pin);
}
void low(){
port->BSRR = (1<<(pin+16U));
}
void set(bool on){
port->BSRR = (1<<(pin+(16U*!on)));
}
void toggle(){
port->ODR ^= 1<<pin;
}
#ifdef STM32H7xx
void analogMode(){
// Analog mode is used by ADC and DAC
uint32_t tmp = port->MODER;
tmp |= GPIO_MODER_MODE0 << (pin*2U);
port->MODER = tmp;
}
void inputMode(){
uint32_t tmp = port->MODER;
tmp &= ~(GPIO_MODER_MODE0 << (pin*2U));
port->MODER = tmp;
}
void outputMode(){
uint32_t tmp = port->MODER;
tmp |= GPIO_MODER_MODE0_0 << (pin*2U);
tmp &= ~(GPIO_MODER_MODE0_1 << (pin*2U));
port->MODER = tmp;
}
void afMode(){
uint32_t tmp = port->MODER;
tmp &= ~(GPIO_MODER_MODE0_0 << (pin*2U));
tmp |= GPIO_MODER_MODE0_1 << (pin*2U);
port->MODER = tmp;
}
void setAlternateFunction(uint32_t af){
/* Configure Alternate function mapped with the current IO */
uint32_t tmp = port->AFR[pin >> 3U];
tmp &= ~(0xFU << ((pin & 0x07U) * 4U));
tmp |= (af << ((pin & 0x07U) * 4U));
port->AFR[pin >> 3U] = tmp;
}
void pushPullMode(){
uint32_t tmp = port->OTYPER;
tmp &= ~(GPIO_OTYPER_OT0 << pin) ;
tmp |= (((GPIO_MODE_OUTPUT_PP & GPIO_OUTPUT_TYPE) >> 4U) << pin);
port->OTYPER = tmp;
}
void openDrainMode(){
uint32_t tmp = port->OTYPER;
tmp &= ~(GPIO_OTYPER_OT0 << pin) ;
tmp |= (((GPIO_MODE_OUTPUT_OD & GPIO_OUTPUT_TYPE) >> 4U) << pin);
port->OTYPER = tmp;
}
void setPull(uint32_t pull){
uint32_t tmp = port->PUPDR;
tmp &= ~(GPIO_PUPDR_PUPD0 << (pin*2U));
tmp |= (pull << (pin*2U));
port->PUPDR = tmp;
}
uint32_t getPull(){
return (port->PUPDR & (GPIO_PUPDR_PUPD0 << (pin*2U))) >> (pin*2U);
}
void setSpeed(PinSpeed speed){
uint32_t tmp = port->OSPEEDR;
tmp &= ~(GPIO_OSPEEDR_OSPEED0 << (pin * 2U));
tmp |= (speed << (pin * 2U));
port->OSPEEDR = tmp;
}
#else
void analogMode(){
// Analog mode is used by ADC and DAC
uint32_t tmp = port->MODER;
tmp |= GPIO_MODER_MODER0 << (pin*2U);
port->MODER = tmp;
}
void inputMode(){
uint32_t tmp = port->MODER;
tmp &= ~(GPIO_MODER_MODER0 << (pin*2U));
port->MODER = tmp;
}
void outputMode(){
uint32_t tmp = port->MODER;
tmp |= GPIO_MODER_MODER0_0 << (pin*2U);
tmp &= ~(GPIO_MODER_MODER0_1 << (pin*2U));
port->MODER = tmp;
}
void afMode(){
uint32_t tmp = port->MODER;
tmp &= ~(GPIO_MODER_MODER0_0 << (pin*2U));
tmp |= GPIO_MODER_MODER0_1 << (pin*2U);
port->MODER = tmp;
}
void setAlternateFunction(uint32_t af){
/* Configure Alternate function mapped with the current IO */
uint32_t tmp = port->AFR[pin >> 3U];
tmp &= ~(0xFU << ((pin & 0x07U) * 4U));
tmp |= (af << ((pin & 0x07U) * 4U));
port->AFR[pin >> 3U] = tmp;
}
void pushPullMode(){
port->OTYPER |= 1<<pin;
} // todo: these two can't be the same
void openDrainMode(){
port->OTYPER |= 1<<pin;
}
void setPull(uint32_t pull){
uint32_t tmp = port->PUPDR;
tmp &= ~(GPIO_PUPDR_PUPDR0 << (pin*2U));
tmp |= (pull << (pin*2U));
port->PUPDR = tmp;
}
uint32_t getPull(){
return (port->PUPDR & (GPIO_PUPDR_PUPDR0 << (pin*2U))) >> (pin*2U);
}
void setSpeed(PinSpeed speed){
uint32_t tmp = port->OSPEEDR;
tmp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pin * 2U));
tmp |= (speed << (pin * 2U));
port->OSPEEDR = tmp;
}
#endif
};
// typedef uint8_t pin_t;
// template<GPIO_TypeDef* port, unsigned int pin>
// class Pin {
// static bool get(pin_t pin){
// return port->IDR & (1<<pin);
// }
// static void high(pin_t pin){
// port->BSRR = (1<<pin);
// }
// static void low(pin_t pin){
// port->BSRR = (1<<(pin+16U));
// }
// static void set(pin_t pin, bool on){
// port->BSRR = (1<<(pin+16U*!on));
// // if(on)
// // high(pin);
// // else
// // low(pin);
// }
// static void toggle(pin_t pin){
// port->ODR ^= 1<<pin;
// }
// };
// template<GPIO_TypeDef* port, unsigned int pin>
// class Pin {
// static bool get(){
// return port->IDR & (1<<pin);
// }
// static void high(){
// port->BSRR = (1<<pin);
// }
// static void low(){
// port->BSRR = (1<<(pin+16U));
// }
// static void set(bool on){
// port->BSRR = (1<<(pin+16U*!on));
// // if(on)
// // high(pin);
// // else
// // low(pin);
// }
// static void toggle(){
// port->ODR ^= 1<<pin;
// }
// static void analogMode(){
// // Analog mode is used by ADC and DAC
// uint32_t tmp = port->MODER;
// tmp |= GPIO_MODER_MODER0 << (pin*2U);
// port->MODER = tmp;
// }
// static void inputMode(){
// uint32_t tmp = port->MODER;
// tmp &= ~(GPIO_MODER_MODER0 << (pin*2U));
// port->MODER = tmp;
// }
// static void outputMode(){
// uint32_t tmp = port->MODER;
// tmp |= GPIO_MODER_MODER0_0 << (pin*2U);
// tmp &= ~(GPIO_MODER_MODER0_1 << (pin*2U));
// port->MODER = tmp;
// }
// static void afMode(){
// uint32_t tmp = port->MODER;
// tmp &= ~(GPIO_MODER_MODER0_0 << (pin*2U));
// tmp |= GPIO_MODER_MODER0_1 << (pin*2U);
// port->MODER = tmp;
// }
// static void setAlternateFunction(uint32_t af){
// /* Configure Alternate function mapped with the current IO */
// uint32_t tmp = port->AFR[pin >> 3U];
// tmp &= ~(0xFU << ((pin & 0x07U) * 4U));
// tmp |= (af << ((pin & 0x07U) * 4U));
// port->AFR[pin >> 3U] = tmp;
// }
// static void pushPullMode(){
// port->OTYPER |= 1<<pin;
// }
// static void openDrainMode(){
// port->OTYPER |= 1<<pin;
// }
// static void setPull(uint32_t pull){
// uint32_t tmp = port->PUPDR;
// tmp &= ~(GPIO_PUPDR_PUPDR0 << (pin*2U));
// tmp |= (pull << (pin*2U));
// port->PUPDR = tmp;
// }
// static uint32_t getPull(){
// return (port->PUPDR & (GPIO_PUPDR_PUPDR0 << (pin*2U))) >> (pin*2U);
// }
// static void setSpeed(PinSpeed speed){
// uint32_t tmp = port->OSPEEDR;
// tmp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pin * 2U));
// tmp |= (speed << (pin * 2U));
// port->OSPEEDR = tmp;
// }
// };
#endif /* __PIN_H */