49#define _FG_SBI(mem_addr, bit) asm volatile("sbi %0, %1\n" : \
50 : "I" (mem_addr - __SFR_OFFSET), "I" (bit))
51#define _FG_CBI(mem_addr, bit) asm volatile("cbi %0, %1\n" : \
52 : "I" (mem_addr - __SFR_OFFSET), "I" (bit))
53#define _FG_PIN(port, bit) { _SFR_MEM_ADDR(PIN##port), _SFR_MEM_ADDR(PORT##port), \
54 _SFR_MEM_ADDR(DDR##port), bit }
65 typedef struct IOStruct
72 volatile uint8_t * pin()
const
74 return (
volatile uint8_t *)(uint16_t)pinAddr;
77 volatile uint8_t * port()
const
79 return (
volatile uint8_t *)(uint16_t)portAddr;
82 volatile uint8_t * ddr()
const
84 return (
volatile uint8_t *)(uint16_t)ddrAddr;
89#if defined(__AVR_ATmega328PB__) || defined(ARDUINO_AVR_A_STAR_328PB)
91 const IOStruct pinStructs[] = {
147#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
149 const IOStruct pinStructs[] = {
197#elif defined(__AVR_ATmega32U4__)
199 const IOStruct pinStructs[] = {
272#error FastGPIO does not support this board.
275 template<u
int8_t pin>
class Pin
286 _FG_CBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
287 _FG_SBI(pinStructs[pin].ddrAddr, pinStructs[pin].bit);
298 _FG_SBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
299 _FG_SBI(pinStructs[pin].ddrAddr, pinStructs[pin].bit);
307 _FG_SBI(pinStructs[pin].ddrAddr, pinStructs[pin].bit);
318 static inline void setOutput(
bool value) __attribute__((always_inline))
321 _FG_SBI(pinStructs[pin].ddrAddr, pinStructs[pin].bit);
334 _FG_CBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
347 _FG_SBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
362 _FG_SBI(pinStructs[pin].pinAddr, pinStructs[pin].bit);
380 _FG_SBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
384 _FG_CBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
391 static inline void setInput() __attribute__((always_inline))
393 _FG_CBI(pinStructs[pin].ddrAddr, pinStructs[pin].bit);
394 _FG_CBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
402 _FG_CBI(pinStructs[pin].ddrAddr, pinStructs[pin].bit);
403 _FG_SBI(pinStructs[pin].portAddr, pinStructs[pin].bit);
412 return *pinStructs[pin].pin() >> pinStructs[pin].bit & 1;
432 static inline bool isOutput() __attribute__((always_inline))
434 return *pinStructs[pin].ddr() >> pinStructs[pin].bit & 1;
445 return *pinStructs[pin].port() >> pinStructs[pin].bit & 1;
464 :
"I" (pinStructs[pin].bit),
465 "I" (pinStructs[pin].portAddr - __SFR_OFFSET),
466 "I" (pinStructs[pin].ddrAddr - __SFR_OFFSET));
502 "I" (pinStructs[pin].bit),
503 "I" (pinStructs[pin].portAddr - __SFR_OFFSET),
504 "I" (pinStructs[pin].ddrAddr - __SFR_OFFSET));
static bool isOutputValueHigh() __attribute__((always_inline))
Returns the output value of the pin.
static void setOutputValue(bool value) __attribute__((always_inline))
Sets the output value of the pin.
static void setInputPulledUp() __attribute__((always_inline))
Sets a pin to be a digital input with the internal pull-up resistor enabled.
static void setOutputLow() __attribute__((always_inline))
Configures the pin to be an output driving low.
static void setOutputValueToggle() __attribute__((always_inline))
Toggles the output value of the pin.
static void setOutputValueHigh() __attribute__((always_inline))
Sets the output value of the pin to 1.
static void setState(uint8_t state)
Sets the full 2-bit state of the pin.
static uint8_t getState()
Returns the full 2-bit state of the pin.
static void setInput() __attribute__((always_inline))
Sets a pin to be a digital input with the internal pull-up resistor disabled.
static void setOutput(bool value) __attribute__((always_inline))
Sets the pin as an output.
static void setOutputHigh() __attribute__((always_inline))
Configures the pin to be an output driving high.
static void setOutputToggle() __attribute__((always_inline))
Configures the pin to be an output and toggles it.
static bool isInputHigh() __attribute__((always_inline))
Reads the input value of the pin.
static void setOutputValueLow() __attribute__((always_inline))
Sets the output value of the pin to 0.
static bool isOutput() __attribute__((always_inline))
Returns 1 if the pin is configured as an output.
uint8_t state
The state of the pin as returned from FastGPIO::Pin::getState.