Merge remote-tracking branch 'upstream/master'
[jackhill/qmk/firmware.git] / quantum / pincontrol.h
1 #pragma once
2 // Some helpers for controlling gpio pins
3 #include <avr/io.h>
4
5 enum {
6 PinDirectionInput = 0,
7 PinDirectionOutput = 1,
8 PinLevelHigh = 1,
9 PinLevelLow = 0,
10 };
11
12 // ex: pinMode(B0, PinDirectionOutput);
13 static inline void pinMode(uint8_t pin, int mode) {
14 uint8_t bv = _BV(pin & 0xf);
15 if (mode == PinDirectionOutput) {
16 _SFR_IO8((pin >> 4) + 1) |= bv;
17 } else {
18 _SFR_IO8((pin >> 4) + 1) &= ~bv;
19 _SFR_IO8((pin >> 4) + 2) &= ~bv;
20 }
21 }
22
23 // ex: digitalWrite(B0, PinLevelHigh);
24 static inline void digitalWrite(uint8_t pin, int mode) {
25 uint8_t bv = _BV(pin & 0xf);
26 if (mode == PinLevelHigh) {
27 _SFR_IO8((pin >> 4) + 2) |= bv;
28 } else {
29 _SFR_IO8((pin >> 4) + 2) &= ~bv;
30 }
31 }
32
33 // Return true if the pin is HIGH
34 // digitalRead(B0)
35 static inline bool digitalRead(uint8_t pin) {
36 return _SFR_IO8(pin >> 4) & _BV(pin & 0xf);
37 }