blob: 44c85f4ac48b2820bd31615ca934631fb657f944 (
plain)
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
|
#ifndef LAKS_GPIO_GPIO_NRF_H
#define LAKS_GPIO_GPIO_NRF_H
#include <stdint.h>
struct GPIO_reg_t {
uint32_t _reserved;
volatile uint32_t OUT;
volatile uint32_t OUTSET;
volatile uint32_t OUTCLR;
volatile uint32_t IN;
volatile uint32_t DIR;
volatile uint32_t DIRSET;
volatile uint32_t DIRCLR;
volatile uint32_t LATCH;
volatile uint32_t DETECTMODE;
uint32_t _reserved_1[118];
volatile uint32_t PIN_CNF[32];
};
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 {
Input,
Output,
};
void set_mode(Mode m) {
switch(m) {
case Input:
g.reg.DIRCLR = 1 << n;
break;
case Output:
g.reg.DIRSET = 1 << n;
break;
}
}
void on() {
g.reg.OUTSET = 1 << n;
}
void off() {
g.reg.OUTCLR = 1 << n;
}
void set(bool value) {
if(value) {
on();
} else {
off();
}
}
bool get() {
return g.reg.IN & (1 << n);
}
void toggle() {
set(!(g.reg.OUT & (1 << n)));
}
};
constexpr GPIO_t(uint32_t reg_addr) : reg(*(GPIO_reg_t*)(reg_addr + 0x500)) {}
constexpr Pin operator[](int pin) {
return Pin(*this, pin);
}
};
typedef GPIO_t::Pin Pin;
#endif
|