summaryrefslogtreecommitdiffstats
path: root/quantum/pincontrol.h
blob: 36ce29ef2203db380702d8643918df980e7768b8 (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
#pragma once
// Some helpers for controlling gpio pins
#include <avr/io.h>

enum {
  PinDirectionInput = 0,
  PinDirectionOutput = 1,
  PinLevelHigh = 1,
  PinLevelLow = 0,
};

// ex: pinMode(B0, PinDirectionOutput);
static inline void pinMode(uint8_t pin, int mode) {
  uint8_t bv = _BV(pin & 0xf);
  if (mode == PinDirectionOutput) {
    _SFR_IO8((pin >> 4) + 1) |= bv;
  } else {
    _SFR_IO8((pin >> 4) + 1) &= ~bv;
    _SFR_IO8((pin >> 4) + 2) &= ~bv;
  }
}

// ex: digitalWrite(B0, PinLevelHigh);
static inline void digitalWrite(uint8_t pin, int mode) {
  uint8_t bv = _BV(pin & 0xf);
  if (mode == PinLevelHigh) {
    _SFR_IO8((pin >> 4) + 2) |= bv;
  } else {
    _SFR_IO8((pin >> 4) + 2) &= ~bv;
  }
}

// Return true if the pin is HIGH
// digitalRead(B0)
static inline bool digitalRead(uint8_t pin) {
  return _SFR_IO8(pin >> 4) & _BV(pin & 0xf);
}