Skip to content

Instantly share code, notes, and snippets.

@jackhumbert
Created September 27, 2018 21:28
Show Gist options
  • Save jackhumbert/a503f73518248ca7672bcfa1d3b88a15 to your computer and use it in GitHub Desktop.
Save jackhumbert/a503f73518248ca7672bcfa1d3b88a15 to your computer and use it in GitHub Desktop.
enum {
PIN_INPUT,
PIN_INPUT_HIGH,
PIN_INPUT_LOW,
PIN_OUTPUT,
PIN_OUTPUT_HIGH,
PIN_OUTPUT_LOW
}
#ifdef __AVR__
#define pin_t uint8_t
#define writePinHigh(pin) _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF)
#define writePinLow(pin) _SFR_IO8((pin >> 4) + 2) &= ~ _BV(pin & 0xF)
#define setPinModeInput(pin) _SFR_IO8((pin >> 4) + 1) &= ~ _BV(pin & 0xF)
#define setPinModeInputHigh(pin) ({\
_SFR_IO8((pin >> 4) + 1) &= ~ _BV(pin & 0xF);\
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);\
})
#define setPinModeInputLow(pin) ({\
_SFR_IO8((pin >> 4) + 1) &= ~ _BV(pin & 0xF);\
_SFR_IO8((pin >> 4) + 2) &= ~ _BV(pin & 0xF);\
})
#define setPinModeOutput(pin) _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF)
#define setPinModeInputHigh(pin) ({\
_SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);\
_SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);\
})
#define setPinModeInputLow(pin) ({\
_SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);\
_SFR_IO8((pin >> 4) + 2) &= ~ _BV(pin & 0xF);\
})
#define readPin(pin) (_SFR_IO8(pin >> 4) & _BV(pin & 0xF))
static inline void writePin(pin_t pin, bool direction) {
if (direction) {
writePinHigh();
} else {
writePinLow();
}
}
static inline void setPinMode(pin_t pin, uint8_t mode) {
switch(mode) {
case PIN_INPUT: setPinModeInput(); break;
case PIN_INPUT_HIGH: setPinModeInputHigh(); break;
case PIN_INPUT_LOW: setPinModeInputLow(); break;
case PIN_OUTPUT: setPinModeOutput(); break;
case PIN_OUTPUT_HIGH: setPinModeOutputHigh(); break;
case PIN_OUTPUT_LOW: setPinModeOutputLow(); break;
}
}
#elif defined(PROTOCOL_CHIBIOS)
#define pin_t ioline_t
#define PIN_INPUT PAL_MODE_INPUT
#define PIN_INPUT_HIGH PAL_MODE_INPUT_PULLUP
#define PIN_INPUT_LOW PAL_MODE_INPUT_PULLDOWN
#define PIN_OUTPUT_HIGH PAL_MODE_OUTPUT_PUSHPULL
#define PIN_OUTPUT_LOW 0b10
#define setPinModeInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
#define setPinModeInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
#define setPinModeInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
#define setPinModeOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
#define setPinModeOutputHigh(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL); palWriteLine(pin, PAL_HIGH)
#define setPinModeOutputLow(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL); palWriteLine(pin, PAL_LOW)
#define readPin(pin) palReadLine(pin)
#define writePin(pin, direction) palWriteLine(pin, direction)
#define writePinHigh(pin) palWriteLine(pin, PAL_HIGH)
#define writePinLow(pin) palWriteLine(pin, PAL_LOW)
#define setPinMode(pin, mode) palSetLineMode(pin, mode)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment