summaryrefslogtreecommitdiff
path: root/gpio/gpio_nrf.h
diff options
context:
space:
mode:
Diffstat (limited to 'gpio/gpio_nrf.h')
-rw-r--r--gpio/gpio_nrf.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/gpio/gpio_nrf.h b/gpio/gpio_nrf.h
new file mode 100644
index 0000000..44c85f4
--- /dev/null
+++ b/gpio/gpio_nrf.h
@@ -0,0 +1,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