diff --git a/Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp b/Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp index f12f0df7fb..40407f7f5d 100644 --- a/Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp +++ b/Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp @@ -145,8 +145,8 @@ static const UnwindCallbacks UnwCallbacks = { UnwReadW, UnwReadH, UnwReadB - #if defined(UNW_DEBUG) - ,UnwPrintf + #ifdef UNW_DEBUG + , UnwPrintf #endif }; diff --git a/Marlin/src/HAL/HAL_LPC1768/DebugMonitor_LPC1768.cpp b/Marlin/src/HAL/HAL_LPC1768/DebugMonitor_LPC1768.cpp index 6798d60cc6..c992925bf0 100644 --- a/Marlin/src/HAL/HAL_LPC1768/DebugMonitor_LPC1768.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/DebugMonitor_LPC1768.cpp @@ -118,7 +118,7 @@ static const UnwindCallbacks UnwCallbacks = { UnwReadW, UnwReadH, UnwReadB - #if defined(UNW_DEBUG) + #ifdef UNW_DEBUG ,UnwPrintf #endif }; diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/pinsDebug.h b/Marlin/src/HAL/HAL_TEENSY35_36/pinsDebug.h index f94f55c1d2..2d41821700 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/pinsDebug.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/pinsDebug.h @@ -45,7 +45,7 @@ #define FTM3_CH5_PIN 36 #define FTM3_CH6_PIN 37 #define FTM3_CH7_PIN 38 -#if defined(__MK66FX1M0__) // Teensy3.6 +#ifdef __MK66FX1M0__ // Teensy3.6 #define TPM1_CH0_PIN 16 #define TPM1_CH1_PIN 17 #endif diff --git a/Marlin/src/HAL/shared/backtrace/unwarm.cpp b/Marlin/src/HAL/shared/backtrace/unwarm.cpp index 13079817ba..e1740fc248 100644 --- a/Marlin/src/HAL/shared/backtrace/unwarm.cpp +++ b/Marlin/src/HAL/shared/backtrace/unwarm.cpp @@ -23,7 +23,7 @@ #include "unwarm.h" #include "unwarmmem.h" -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG /** * Printf wrapper. diff --git a/Marlin/src/HAL/shared/backtrace/unwarm.h b/Marlin/src/HAL/shared/backtrace/unwarm.h index 7414ecc06b..9594ed95d7 100644 --- a/Marlin/src/HAL/shared/backtrace/unwarm.h +++ b/Marlin/src/HAL/shared/backtrace/unwarm.h @@ -106,7 +106,7 @@ typedef struct { #define M_IsOriginValid(v) (((v) & 0x7F) ? true : false) #define M_Origin2Str(v) ((v) ? "VALID" : "INVALID") -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG #define UnwPrintd1(a) state->cb->printf(a) #define UnwPrintd2(a,b) state->cb->printf(a,b) #define UnwPrintd3(a,b,c) state->cb->printf(a,b,c) diff --git a/Marlin/src/HAL/shared/backtrace/unwarm_arm.cpp b/Marlin/src/HAL/shared/backtrace/unwarm_arm.cpp index 09528885f7..be43b22392 100644 --- a/Marlin/src/HAL/shared/backtrace/unwarm_arm.cpp +++ b/Marlin/src/HAL/shared/backtrace/unwarm_arm.cpp @@ -142,7 +142,7 @@ UnwResult UnwStartArm(UnwState * const state) { /* MRS */ else if ((instr & 0xFFBF0FFF) == 0xE10F0000) { -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG bool R = (instr & 0x00400000) ? true : false; #endif uint8_t rd = (instr & 0x0000F000) >> 12; @@ -154,7 +154,7 @@ UnwResult UnwStartArm(UnwState * const state) { } /* MSR */ else if ((instr & 0xFFB0F000) == 0xE120F000) { -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG bool R = (instr & 0x00400000) ? true : false; UnwPrintd2("MSR %s_?, ???", R ? "SPSR" : "CPSR"); @@ -172,7 +172,7 @@ UnwResult UnwStartArm(UnwState * const state) { else if (isDataProc(instr)) { bool I = (instr & 0x02000000) ? true : false; uint8_t opcode = (instr & 0x01E00000) >> 21; -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG bool S = (instr & 0x00100000) ? true : false; #endif uint8_t rn = (instr & 0x000F0000) >> 16; @@ -220,7 +220,7 @@ UnwResult UnwStartArm(UnwState * const state) { uint8_t regShift = (operand2 & 0x0010) ? true : false; uint8_t shiftType = (operand2 & 0x0060) >> 5; uint32_t shiftDist; -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG const char * const shiftMnu[4] = { "LSL", "LSR", "ASR", "ROR" }; #endif UnwPrintd2("r%d ", rm); @@ -453,7 +453,7 @@ UnwResult UnwStartArm(UnwState * const state) { bool addrValid = M_IsOriginValid(state->regData[baseReg].o); int8_t r; -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG /* Display the instruction */ if (L) { UnwPrintd6("LDM%c%c r%d%s, {reglist}%s\n", P ? 'E' : 'F', U ? 'D' : 'A', baseReg, W ? "!" : "", S ? "^" : ""); diff --git a/Marlin/src/HAL/shared/backtrace/unwarm_thumb.cpp b/Marlin/src/HAL/shared/backtrace/unwarm_thumb.cpp index a4afbeaf8d..29efb13096 100644 --- a/Marlin/src/HAL/shared/backtrace/unwarm_thumb.cpp +++ b/Marlin/src/HAL/shared/backtrace/unwarm_thumb.cpp @@ -680,7 +680,7 @@ UnwResult UnwStartThumb(UnwState * const state) { uint8_t rs = (instr & 0x0038) >> 3; uint8_t rd = (instr & 0x0007); -#if defined(UNW_DEBUG) +#ifdef UNW_DEBUG static const char * const mnu[16] = { "AND", "EOR", "LSL", "LSR", "ASR", "ADC", "SBC", "ROR", diff --git a/Marlin/src/HAL/shared/backtrace/unwarmmem.cpp b/Marlin/src/HAL/shared/backtrace/unwarmmem.cpp index 7453fdb8db..b836841713 100644 --- a/Marlin/src/HAL/shared/backtrace/unwarmmem.cpp +++ b/Marlin/src/HAL/shared/backtrace/unwarmmem.cpp @@ -90,7 +90,7 @@ bool UnwMemHashWrite(MemData * const memData, uint32_t addr, uint32_t val, bool M_SetIdxUsed(memData->tracked, i); } else { - #if defined(UNW_DEBUG) + #ifdef UNW_DEBUG memData->v[i] = 0xDEADBEEF; #endif M_ClrIdxUsed(memData->tracked, i); diff --git a/Marlin/src/lcd/HD44780/lcdprint_hd44780.cpp b/Marlin/src/lcd/HD44780/lcdprint_hd44780.cpp index 7dc80b0528..3e0bebc782 100644 --- a/Marlin/src/lcd/HD44780/lcdprint_hd44780.cpp +++ b/Marlin/src/lcd/HD44780/lcdprint_hd44780.cpp @@ -1,5 +1,5 @@ /** - * @file lcdprint_hd44780.c + * @file lcdprint_hd44780.cpp * @brief LCD print api for HD44780 * @author Yunhui Fu (yhfudev@gmail.com) * @version 1.0 @@ -39,7 +39,7 @@ typedef struct _hd44780_charmap_t { uint8_t idx2; // the char used to be combined with the idx to simulate a single char } hd44780_charmap_t; -#if defined(__AVR__) +#ifdef __AVR__ #define IV(a) U##a #else #define IV(a) L##a diff --git a/Marlin/src/lcd/dogm/lcdprint_u8g.cpp b/Marlin/src/lcd/dogm/lcdprint_u8g.cpp index 8a09c68084..1a036df7b1 100644 --- a/Marlin/src/lcd/dogm/lcdprint_u8g.cpp +++ b/Marlin/src/lcd/dogm/lcdprint_u8g.cpp @@ -1,5 +1,5 @@ /** - * @file lcdprint_u8g.c + * @file lcdprint_u8g.cpp * @brief LCD print api for u8glib * @author Yunhui Fu (yhfudev@gmail.com) * @version 1.0 diff --git a/Marlin/src/libs/softspi.h b/Marlin/src/libs/softspi.h index ad4e4e7194..819567df81 100644 --- a/Marlin/src/libs/softspi.h +++ b/Marlin/src/libs/softspi.h @@ -1,653 +1,635 @@ -//https://github.com/niteris/ArduinoSoftSpi +// https://github.com/niteris/ArduinoSoftSpi #include + +#ifndef FORCE_INLINE + #define FORCE_INLINE inline __attribute__((always_inline)) +#endif + +#ifndef _BV + #define _BV(B) (1 << (B)) + #define SBI(A,B) (A |= (1 << (B))) + #define CBI(A,B) (A &= ~(1 << (B))) +#endif + +#define nop __asm__ volatile ("nop") // NOP for timing + #ifdef __arm__ -#ifdef CORE_TEENSY -//------------------------------------------------------------------------------ -/** read pin value - * @param[in] pin Arduino pin number - * @return value read - */ -static inline __attribute__((always_inline)) -bool fastDigitalRead(uint8_t pin) { - return *portInputRegister(pin); -} -//------------------------------------------------------------------------------ -/** Set pin value - * @param[in] pin Arduino pin number - * @param[in] level value to write - */ -static inline __attribute__((always_inline)) -void fastDigitalWrite(uint8_t pin, bool value) { - if (value) - *portSetRegister(pin) = 1; - else - *portClearRegister(pin) = 1; -} -#else // CORE_TEENSY -//------------------------------------------------------------------------------ -/** read pin value - * @param[in] pin Arduino pin number - * @return value read - */ -static inline __attribute__((always_inline)) -bool fastDigitalRead(uint8_t pin) { - return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin; -} -//------------------------------------------------------------------------------ -/** Set pin value - * @param[in] pin Arduino pin number - * @param[in] level value to write - */ -static inline __attribute__((always_inline)) -void fastDigitalWrite(uint8_t pin, bool value) { - if (value) - g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin; - else - g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin; -} -#endif // CORE_TEENSY -//------------------------------------------------------------------------------ -inline void fastDigitalToggle(uint8_t pin) { - fastDigitalWrite(pin, !fastDigitalRead(pin)); -} -//------------------------------------------------------------------------------ -inline void fastPinMode(uint8_t pin, bool mode) {pinMode(pin, mode);} -#else // __arm__ -#include -#include -//------------------------------------------------------------------------------ -/** - * @class pin_map_t - * @brief struct for mapping digital pins - */ -struct pin_map_t { - volatile uint8_t* ddr; /**< address of DDR for this pin */ - volatile uint8_t* pin; /**< address of PIN for this pin */ - volatile uint8_t* port; /**< address of PORT for this pin */ - uint8_t bit; /**< bit number for this pin */ -}; -//------------------------------------------------------------------------------ -#if defined(__AVR_ATmega168__)\ -||defined(__AVR_ATmega168P__)\ -||defined(__AVR_ATmega328P__) -// 168 and 328 Arduinos -const static pin_map_t pinMap[] = { - {&DDRD, &PIND, &PORTD, 0}, // D0 0 - {&DDRD, &PIND, &PORTD, 1}, // D1 1 - {&DDRD, &PIND, &PORTD, 2}, // D2 2 - {&DDRD, &PIND, &PORTD, 3}, // D3 3 - {&DDRD, &PIND, &PORTD, 4}, // D4 4 - {&DDRD, &PIND, &PORTD, 5}, // D5 5 - {&DDRD, &PIND, &PORTD, 6}, // D6 6 - {&DDRD, &PIND, &PORTD, 7}, // D7 7 - {&DDRB, &PINB, &PORTB, 0}, // B0 8 - {&DDRB, &PINB, &PORTB, 1}, // B1 9 - {&DDRB, &PINB, &PORTB, 2}, // B2 10 - {&DDRB, &PINB, &PORTB, 3}, // B3 11 - {&DDRB, &PINB, &PORTB, 4}, // B4 12 - {&DDRB, &PINB, &PORTB, 5}, // B5 13 - {&DDRC, &PINC, &PORTC, 0}, // C0 14 - {&DDRC, &PINC, &PORTC, 1}, // C1 15 - {&DDRC, &PINC, &PORTC, 2}, // C2 16 - {&DDRC, &PINC, &PORTC, 3}, // C3 17 - {&DDRC, &PINC, &PORTC, 4}, // C4 18 - {&DDRC, &PINC, &PORTC, 5} // C5 19 -}; -//------------------------------------------------------------------------------ -#elif defined(__AVR_ATmega1280__)\ -|| defined(__AVR_ATmega2560__) -// Mega -static const pin_map_t pinMap[] = { - {&DDRE, &PINE, &PORTE, 0}, // E0 0 - {&DDRE, &PINE, &PORTE, 1}, // E1 1 - {&DDRE, &PINE, &PORTE, 4}, // E4 2 - {&DDRE, &PINE, &PORTE, 5}, // E5 3 - {&DDRG, &PING, &PORTG, 5}, // G5 4 - {&DDRE, &PINE, &PORTE, 3}, // E3 5 - {&DDRH, &PINH, &PORTH, 3}, // H3 6 - {&DDRH, &PINH, &PORTH, 4}, // H4 7 - {&DDRH, &PINH, &PORTH, 5}, // H5 8 - {&DDRH, &PINH, &PORTH, 6}, // H6 9 - {&DDRB, &PINB, &PORTB, 4}, // B4 10 - {&DDRB, &PINB, &PORTB, 5}, // B5 11 - {&DDRB, &PINB, &PORTB, 6}, // B6 12 - {&DDRB, &PINB, &PORTB, 7}, // B7 13 - {&DDRJ, &PINJ, &PORTJ, 1}, // J1 14 - {&DDRJ, &PINJ, &PORTJ, 0}, // J0 15 - {&DDRH, &PINH, &PORTH, 1}, // H1 16 - {&DDRH, &PINH, &PORTH, 0}, // H0 17 - {&DDRD, &PIND, &PORTD, 3}, // D3 18 - {&DDRD, &PIND, &PORTD, 2}, // D2 19 - {&DDRD, &PIND, &PORTD, 1}, // D1 20 - {&DDRD, &PIND, &PORTD, 0}, // D0 21 - {&DDRA, &PINA, &PORTA, 0}, // A0 22 - {&DDRA, &PINA, &PORTA, 1}, // A1 23 - {&DDRA, &PINA, &PORTA, 2}, // A2 24 - {&DDRA, &PINA, &PORTA, 3}, // A3 25 - {&DDRA, &PINA, &PORTA, 4}, // A4 26 - {&DDRA, &PINA, &PORTA, 5}, // A5 27 - {&DDRA, &PINA, &PORTA, 6}, // A6 28 - {&DDRA, &PINA, &PORTA, 7}, // A7 29 - {&DDRC, &PINC, &PORTC, 7}, // C7 30 - {&DDRC, &PINC, &PORTC, 6}, // C6 31 - {&DDRC, &PINC, &PORTC, 5}, // C5 32 - {&DDRC, &PINC, &PORTC, 4}, // C4 33 - {&DDRC, &PINC, &PORTC, 3}, // C3 34 - {&DDRC, &PINC, &PORTC, 2}, // C2 35 - {&DDRC, &PINC, &PORTC, 1}, // C1 36 - {&DDRC, &PINC, &PORTC, 0}, // C0 37 - {&DDRD, &PIND, &PORTD, 7}, // D7 38 - {&DDRG, &PING, &PORTG, 2}, // G2 39 - {&DDRG, &PING, &PORTG, 1}, // G1 40 - {&DDRG, &PING, &PORTG, 0}, // G0 41 - {&DDRL, &PINL, &PORTL, 7}, // L7 42 - {&DDRL, &PINL, &PORTL, 6}, // L6 43 - {&DDRL, &PINL, &PORTL, 5}, // L5 44 - {&DDRL, &PINL, &PORTL, 4}, // L4 45 - {&DDRL, &PINL, &PORTL, 3}, // L3 46 - {&DDRL, &PINL, &PORTL, 2}, // L2 47 - {&DDRL, &PINL, &PORTL, 1}, // L1 48 - {&DDRL, &PINL, &PORTL, 0}, // L0 49 - {&DDRB, &PINB, &PORTB, 3}, // B3 50 - {&DDRB, &PINB, &PORTB, 2}, // B2 51 - {&DDRB, &PINB, &PORTB, 1}, // B1 52 - {&DDRB, &PINB, &PORTB, 0}, // B0 53 - {&DDRF, &PINF, &PORTF, 0}, // F0 54 - {&DDRF, &PINF, &PORTF, 1}, // F1 55 - {&DDRF, &PINF, &PORTF, 2}, // F2 56 - {&DDRF, &PINF, &PORTF, 3}, // F3 57 - {&DDRF, &PINF, &PORTF, 4}, // F4 58 - {&DDRF, &PINF, &PORTF, 5}, // F5 59 - {&DDRF, &PINF, &PORTF, 6}, // F6 60 - {&DDRF, &PINF, &PORTF, 7}, // F7 61 - {&DDRK, &PINK, &PORTK, 0}, // K0 62 - {&DDRK, &PINK, &PORTK, 1}, // K1 63 - {&DDRK, &PINK, &PORTK, 2}, // K2 64 - {&DDRK, &PINK, &PORTK, 3}, // K3 65 - {&DDRK, &PINK, &PORTK, 4}, // K4 66 - {&DDRK, &PINK, &PORTK, 5}, // K5 67 - {&DDRK, &PINK, &PORTK, 6}, // K6 68 - {&DDRK, &PINK, &PORTK, 7}, // K7 69 -//pins_MIGHTYBOARD_REVE.h - {&DDRG, &PING, &PORTG, 4}, // G4 70 - {&DDRG, &PING, &PORTG, 3}, // G3 71 - {&DDRJ, &PINJ, &PORTJ, 2}, // J2 72 - {&DDRJ, &PINJ, &PORTJ, 3}, // J3 73 - {&DDRJ, &PINJ, &PORTJ, 7}, // J7 74 - {&DDRJ, &PINJ, &PORTJ, 4}, // J4 75 - {&DDRJ, &PINJ, &PORTJ, 5}, // J5 76 - {&DDRJ, &PINJ, &PORTJ, 6}, // J6 77 - {&DDRE, &PINE, &PORTE, 2}, // E2 78 - {&DDRE, &PINE, &PORTE, 6} // E6 79 + #ifdef CORE_TEENSY -}; -//------------------------------------------------------------------------------ -#elif defined(__AVR_ATmega1284P__)\ -|| defined(__AVR_ATmega1284__)\ -|| defined(__AVR_ATmega644P__)\ -|| defined(__AVR_ATmega644__)\ -|| defined(__AVR_ATmega64__)\ -|| defined(__AVR_ATmega32__)\ -|| defined(__AVR_ATmega324__)\ -|| defined(__AVR_ATmega16__) - -#ifdef VARIANT_MIGHTY -// Mighty Layout -static const pin_map_t pinMap[] = { - {&DDRB, &PINB, &PORTB, 0}, // B0 0 - {&DDRB, &PINB, &PORTB, 1}, // B1 1 - {&DDRB, &PINB, &PORTB, 2}, // B2 2 - {&DDRB, &PINB, &PORTB, 3}, // B3 3 - {&DDRB, &PINB, &PORTB, 4}, // B4 4 - {&DDRB, &PINB, &PORTB, 5}, // B5 5 - {&DDRB, &PINB, &PORTB, 6}, // B6 6 - {&DDRB, &PINB, &PORTB, 7}, // B7 7 - {&DDRD, &PIND, &PORTD, 0}, // D0 8 - {&DDRD, &PIND, &PORTD, 1}, // D1 9 - {&DDRD, &PIND, &PORTD, 2}, // D2 10 - {&DDRD, &PIND, &PORTD, 3}, // D3 11 - {&DDRD, &PIND, &PORTD, 4}, // D4 12 - {&DDRD, &PIND, &PORTD, 5}, // D5 13 - {&DDRD, &PIND, &PORTD, 6}, // D6 14 - {&DDRD, &PIND, &PORTD, 7}, // D7 15 - {&DDRC, &PINC, &PORTC, 0}, // C0 16 - {&DDRC, &PINC, &PORTC, 1}, // C1 17 - {&DDRC, &PINC, &PORTC, 2}, // C2 18 - {&DDRC, &PINC, &PORTC, 3}, // C3 19 - {&DDRC, &PINC, &PORTC, 4}, // C4 20 - {&DDRC, &PINC, &PORTC, 5}, // C5 21 - {&DDRC, &PINC, &PORTC, 6}, // C6 22 - {&DDRC, &PINC, &PORTC, 7}, // C7 23 - {&DDRA, &PINA, &PORTA, 0}, // A0 24 - {&DDRA, &PINA, &PORTA, 1}, // A1 25 - {&DDRA, &PINA, &PORTA, 2}, // A2 26 - {&DDRA, &PINA, &PORTA, 3}, // A3 27 - {&DDRA, &PINA, &PORTA, 4}, // A4 28 - {&DDRA, &PINA, &PORTA, 5}, // A5 29 - {&DDRA, &PINA, &PORTA, 6}, // A6 30 - {&DDRA, &PINA, &PORTA, 7} // A7 31 -}; -#elif defined(VARIANT_BOBUINO) -// Bobuino Layout -static const pin_map_t pinMap[] = { - {&DDRD, &PIND, &PORTD, 0}, // D0 0 - {&DDRD, &PIND, &PORTD, 1}, // D1 1 - {&DDRD, &PIND, &PORTD, 2}, // D2 2 - {&DDRD, &PIND, &PORTD, 3}, // D3 3 - {&DDRB, &PINB, &PORTB, 0}, // B0 4 - {&DDRB, &PINB, &PORTB, 1}, // B1 5 - {&DDRB, &PINB, &PORTB, 2}, // B2 6 - {&DDRB, &PINB, &PORTB, 3}, // B3 7 - {&DDRD, &PIND, &PORTD, 5}, // D5 8 - {&DDRD, &PIND, &PORTD, 6}, // D6 9 - {&DDRB, &PINB, &PORTB, 4}, // B4 10 - {&DDRB, &PINB, &PORTB, 5}, // B5 11 - {&DDRB, &PINB, &PORTB, 6}, // B6 12 - {&DDRB, &PINB, &PORTB, 7}, // B7 13 - {&DDRA, &PINA, &PORTA, 7}, // A7 14 - {&DDRA, &PINA, &PORTA, 6}, // A6 15 - {&DDRA, &PINA, &PORTA, 5}, // A5 16 - {&DDRA, &PINA, &PORTA, 4}, // A4 17 - {&DDRA, &PINA, &PORTA, 3}, // A3 18 - {&DDRA, &PINA, &PORTA, 2}, // A2 19 - {&DDRA, &PINA, &PORTA, 1}, // A1 20 - {&DDRA, &PINA, &PORTA, 0}, // A0 21 - {&DDRC, &PINC, &PORTC, 0}, // C0 22 - {&DDRC, &PINC, &PORTC, 1}, // C1 23 - {&DDRC, &PINC, &PORTC, 2}, // C2 24 - {&DDRC, &PINC, &PORTC, 3}, // C3 25 - {&DDRC, &PINC, &PORTC, 4}, // C4 26 - {&DDRC, &PINC, &PORTC, 5}, // C5 27 - {&DDRC, &PINC, &PORTC, 6}, // C6 28 - {&DDRC, &PINC, &PORTC, 7}, // C7 29 - {&DDRD, &PIND, &PORTD, 4}, // D4 30 - {&DDRD, &PIND, &PORTD, 7} // D7 31 -}; -#elif defined(VARIANT_STANDARD) -// Standard Layout -static const pin_map_t pinMap[] = { - {&DDRB, &PINB, &PORTB, 0}, // B0 0 - {&DDRB, &PINB, &PORTB, 1}, // B1 1 - {&DDRB, &PINB, &PORTB, 2}, // B2 2 - {&DDRB, &PINB, &PORTB, 3}, // B3 3 - {&DDRB, &PINB, &PORTB, 4}, // B4 4 - {&DDRB, &PINB, &PORTB, 5}, // B5 5 - {&DDRB, &PINB, &PORTB, 6}, // B6 6 - {&DDRB, &PINB, &PORTB, 7}, // B7 7 - {&DDRD, &PIND, &PORTD, 0}, // D0 8 - {&DDRD, &PIND, &PORTD, 1}, // D1 9 - {&DDRD, &PIND, &PORTD, 2}, // D2 10 - {&DDRD, &PIND, &PORTD, 3}, // D3 11 - {&DDRD, &PIND, &PORTD, 4}, // D4 12 - {&DDRD, &PIND, &PORTD, 5}, // D5 13 - {&DDRD, &PIND, &PORTD, 6}, // D6 14 - {&DDRD, &PIND, &PORTD, 7}, // D7 15 - {&DDRC, &PINC, &PORTC, 0}, // C0 16 - {&DDRC, &PINC, &PORTC, 1}, // C1 17 - {&DDRC, &PINC, &PORTC, 2}, // C2 18 - {&DDRC, &PINC, &PORTC, 3}, // C3 19 - {&DDRC, &PINC, &PORTC, 4}, // C4 20 - {&DDRC, &PINC, &PORTC, 5}, // C5 21 - {&DDRC, &PINC, &PORTC, 6}, // C6 22 - {&DDRC, &PINC, &PORTC, 7}, // C7 23 - {&DDRA, &PINA, &PORTA, 7}, // A7 24 - {&DDRA, &PINA, &PORTA, 6}, // A6 25 - {&DDRA, &PINA, &PORTA, 5}, // A5 26 - {&DDRA, &PINA, &PORTA, 4}, // A4 27 - {&DDRA, &PINA, &PORTA, 3}, // A3 28 - {&DDRA, &PINA, &PORTA, 2}, // A2 29 - {&DDRA, &PINA, &PORTA, 1}, // A1 30 - {&DDRA, &PINA, &PORTA, 0} // A0 31 -}; -#else // VARIANT_MIGHTY -#error Undefined variant 1284, 644, 324, 64, 32 -#endif // VARIANT_MIGHTY -//------------------------------------------------------------------------------ -#elif defined(__AVR_ATmega32U4__) -#ifdef CORE_TEENSY -// Teensy 2.0 -static const pin_map_t pinMap[] = { - {&DDRB, &PINB, &PORTB, 0}, // B0 0 - {&DDRB, &PINB, &PORTB, 1}, // B1 1 - {&DDRB, &PINB, &PORTB, 2}, // B2 2 - {&DDRB, &PINB, &PORTB, 3}, // B3 3 - {&DDRB, &PINB, &PORTB, 7}, // B7 4 - {&DDRD, &PIND, &PORTD, 0}, // D0 5 - {&DDRD, &PIND, &PORTD, 1}, // D1 6 - {&DDRD, &PIND, &PORTD, 2}, // D2 7 - {&DDRD, &PIND, &PORTD, 3}, // D3 8 - {&DDRC, &PINC, &PORTC, 6}, // C6 9 - {&DDRC, &PINC, &PORTC, 7}, // C7 10 - {&DDRD, &PIND, &PORTD, 6}, // D6 11 - {&DDRD, &PIND, &PORTD, 7}, // D7 12 - {&DDRB, &PINB, &PORTB, 4}, // B4 13 - {&DDRB, &PINB, &PORTB, 5}, // B5 14 - {&DDRB, &PINB, &PORTB, 6}, // B6 15 - {&DDRF, &PINF, &PORTF, 7}, // F7 16 - {&DDRF, &PINF, &PORTF, 6}, // F6 17 - {&DDRF, &PINF, &PORTF, 5}, // F5 18 - {&DDRF, &PINF, &PORTF, 4}, // F4 19 - {&DDRF, &PINF, &PORTF, 1}, // F1 20 - {&DDRF, &PINF, &PORTF, 0}, // F0 21 - {&DDRD, &PIND, &PORTD, 4}, // D4 22 - {&DDRD, &PIND, &PORTD, 5}, // D5 23 - {&DDRE, &PINE, &PORTE, 6} // E6 24 -}; -//------------------------------------------------------------------------------ -#else // CORE_TEENSY -// Leonardo -static const pin_map_t pinMap[] = { - {&DDRD, &PIND, &PORTD, 2}, // D2 0 - {&DDRD, &PIND, &PORTD, 3}, // D3 1 - {&DDRD, &PIND, &PORTD, 1}, // D1 2 - {&DDRD, &PIND, &PORTD, 0}, // D0 3 - {&DDRD, &PIND, &PORTD, 4}, // D4 4 - {&DDRC, &PINC, &PORTC, 6}, // C6 5 - {&DDRD, &PIND, &PORTD, 7}, // D7 6 - {&DDRE, &PINE, &PORTE, 6}, // E6 7 - {&DDRB, &PINB, &PORTB, 4}, // B4 8 - {&DDRB, &PINB, &PORTB, 5}, // B5 9 - {&DDRB, &PINB, &PORTB, 6}, // B6 10 - {&DDRB, &PINB, &PORTB, 7}, // B7 11 - {&DDRD, &PIND, &PORTD, 6}, // D6 12 - {&DDRC, &PINC, &PORTC, 7}, // C7 13 - {&DDRB, &PINB, &PORTB, 3}, // B3 14 - {&DDRB, &PINB, &PORTB, 1}, // B1 15 - {&DDRB, &PINB, &PORTB, 2}, // B2 16 - {&DDRB, &PINB, &PORTB, 0}, // B0 17 - {&DDRF, &PINF, &PORTF, 7}, // F7 18 - {&DDRF, &PINF, &PORTF, 6}, // F6 19 - {&DDRF, &PINF, &PORTF, 5}, // F5 20 - {&DDRF, &PINF, &PORTF, 4}, // F4 21 - {&DDRF, &PINF, &PORTF, 1}, // F1 22 - {&DDRF, &PINF, &PORTF, 0}, // F0 23 - {&DDRD, &PIND, &PORTD, 4}, // D4 24 - {&DDRD, &PIND, &PORTD, 7}, // D7 25 - {&DDRB, &PINB, &PORTB, 4}, // B4 26 - {&DDRB, &PINB, &PORTB, 5}, // B5 27 - {&DDRB, &PINB, &PORTB, 6}, // B6 28 - {&DDRD, &PIND, &PORTD, 6} // D6 29 -}; -#endif // CORE_TEENSY -//------------------------------------------------------------------------------ -#elif defined(__AVR_AT90USB646__)\ -|| defined(__AVR_AT90USB1286__) -// Teensy++ 1.0 & 2.0 -static const pin_map_t pinMap[] = { - {&DDRD, &PIND, &PORTD, 0}, // D0 0 - {&DDRD, &PIND, &PORTD, 1}, // D1 1 - {&DDRD, &PIND, &PORTD, 2}, // D2 2 - {&DDRD, &PIND, &PORTD, 3}, // D3 3 - {&DDRD, &PIND, &PORTD, 4}, // D4 4 - {&DDRD, &PIND, &PORTD, 5}, // D5 5 - {&DDRD, &PIND, &PORTD, 6}, // D6 6 - {&DDRD, &PIND, &PORTD, 7}, // D7 7 - {&DDRE, &PINE, &PORTE, 0}, // E0 8 - {&DDRE, &PINE, &PORTE, 1}, // E1 9 - {&DDRC, &PINC, &PORTC, 0}, // C0 10 - {&DDRC, &PINC, &PORTC, 1}, // C1 11 - {&DDRC, &PINC, &PORTC, 2}, // C2 12 - {&DDRC, &PINC, &PORTC, 3}, // C3 13 - {&DDRC, &PINC, &PORTC, 4}, // C4 14 - {&DDRC, &PINC, &PORTC, 5}, // C5 15 - {&DDRC, &PINC, &PORTC, 6}, // C6 16 - {&DDRC, &PINC, &PORTC, 7}, // C7 17 - {&DDRE, &PINE, &PORTE, 6}, // E6 18 - {&DDRE, &PINE, &PORTE, 7}, // E7 19 - {&DDRB, &PINB, &PORTB, 0}, // B0 20 - {&DDRB, &PINB, &PORTB, 1}, // B1 21 - {&DDRB, &PINB, &PORTB, 2}, // B2 22 - {&DDRB, &PINB, &PORTB, 3}, // B3 23 - {&DDRB, &PINB, &PORTB, 4}, // B4 24 - {&DDRB, &PINB, &PORTB, 5}, // B5 25 - {&DDRB, &PINB, &PORTB, 6}, // B6 26 - {&DDRB, &PINB, &PORTB, 7}, // B7 27 - {&DDRA, &PINA, &PORTA, 0}, // A0 28 - {&DDRA, &PINA, &PORTA, 1}, // A1 29 - {&DDRA, &PINA, &PORTA, 2}, // A2 30 - {&DDRA, &PINA, &PORTA, 3}, // A3 31 - {&DDRA, &PINA, &PORTA, 4}, // A4 32 - {&DDRA, &PINA, &PORTA, 5}, // A5 33 - {&DDRA, &PINA, &PORTA, 6}, // A6 34 - {&DDRA, &PINA, &PORTA, 7}, // A7 35 - {&DDRE, &PINE, &PORTE, 4}, // E4 36 - {&DDRE, &PINE, &PORTE, 5}, // E5 37 - {&DDRF, &PINF, &PORTF, 0}, // F0 38 - {&DDRF, &PINF, &PORTF, 1}, // F1 39 - {&DDRF, &PINF, &PORTF, 2}, // F2 40 - {&DDRF, &PINF, &PORTF, 3}, // F3 41 - {&DDRF, &PINF, &PORTF, 4}, // F4 42 - {&DDRF, &PINF, &PORTF, 5}, // F5 43 - {&DDRF, &PINF, &PORTF, 6}, // F6 44 - {&DDRF, &PINF, &PORTF, 7} // F7 45 -}; -//------------------------------------------------------------------------------ -#else // CPU type -#error unknown CPU type -#endif // CPU type -//------------------------------------------------------------------------------ -/** count of pins */ -static const uint8_t digitalPinCount = sizeof(pinMap)/sizeof(pin_map_t); -//============================================================================== -/** generate bad pin number error */ -void badPinNumber(void) - __attribute__((error("Pin number is too large or not a constant"))); -//------------------------------------------------------------------------------ -/** Check for valid pin number - * @param[in] pin Number of pin to be checked. - */ -static inline __attribute__((always_inline)) -void badPinCheck(uint8_t pin) { - if (!__builtin_constant_p(pin) || pin >= digitalPinCount) { - badPinNumber(); - } -} -//------------------------------------------------------------------------------ -/** fast write helper - * @param[in] address I/O register address - * @param[in] bit bit number to write - * @param[in] level value for bit - */ -static inline __attribute__((always_inline)) -void fastBitWriteSafe(volatile uint8_t* address, uint8_t bit, bool level) { - uint8_t oldSREG; - if (address > (uint8_t*)0x5F) { - oldSREG = SREG; - cli(); - } - if (level) { - *address |= 1 << bit; - } else { - *address &= ~(1 << bit); - } - if (address > (uint8_t*)0x5F) { - SREG = oldSREG; - } -} -//------------------------------------------------------------------------------ -/** read pin value - * @param[in] pin Arduino pin number - * @return value read - */ -static inline __attribute__((always_inline)) -bool fastDigitalRead(uint8_t pin) { - badPinCheck(pin); - return (*pinMap[pin].pin >> pinMap[pin].bit) & 1; -} -//------------------------------------------------------------------------------ -/** toggle a pin - * @param[in] pin Arduino pin number - * - * If the pin is in output mode toggle the pin level. - * If the pin is in input mode toggle the state of the 20K pullup. - */ -static inline __attribute__((always_inline)) -void fastDigitalToggle(uint8_t pin) { - badPinCheck(pin); - if (pinMap[pin].pin > (uint8_t*)0x5F) { - // must write bit to high address port - *pinMap[pin].pin = 1 << pinMap[pin].bit; - } else { - // will compile to sbi and PIN register will not be read. - *pinMap[pin].pin |= 1 << pinMap[pin].bit; + /** + * Read pin value + * @param[in] pin Arduino pin number + * @return value read + */ + FORCE_INLINE static bool fastDigitalRead(uint8_t pin) { + return *portInputRegister(pin); } -} -//------------------------------------------------------------------------------ -/** Set pin value - * @param[in] pin Arduino pin number - * @param[in] level value to write - */ -static inline __attribute__((always_inline)) -void fastDigitalWrite(uint8_t pin, bool level) { - badPinCheck(pin); - fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, level); -} -//------------------------------------------------------------------------------ -/** set pin mode - * @param[in] pin Arduino pin number - * @param[in] mode if true set output mode else input mode - * - * fastPinMode does not enable or disable the 20K pullup for input mode. - */ -static inline __attribute__((always_inline)) -void fastPinMode(uint8_t pin, bool mode) { - badPinCheck(pin); - fastBitWriteSafe(pinMap[pin].ddr, pinMap[pin].bit, mode); -} -#endif // __arm__ -//------------------------------------------------------------------------------ -/** set pin configuration + /** + * Set pin value + * @param[in] pin Arduino pin number + * @param[in] level value to write + */ + FORCE_INLINE static void fastDigitalWrite(uint8_t pin, bool value) { + if (value) + *portSetRegister(pin) = 1; + else + *portClearRegister(pin) = 1; + } + + #else // !CORE_TEENSY + + /** + * Read pin value + * @param[in] pin Arduino pin number + * @return value read + */ + FORCE_INLINE static bool fastDigitalRead(uint8_t pin) { + return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin; + } + + /** + * Set pin value + * @param[in] pin Arduino pin number + * @param[in] level value to write + */ + FORCE_INLINE static void fastDigitalWrite(uint8_t pin, bool value) { + if (value) + g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin; + else + g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin; + } + + #endif // !CORE_TEENSY + + inline void fastDigitalToggle(uint8_t pin) { fastDigitalWrite(pin, !fastDigitalRead(pin)); } + + inline void fastPinMode(uint8_t pin, bool mode) { pinMode(pin, mode); } + +#else // !__arm__ + + #include + #include + + /** + * @class pin_map_t + * @brief struct for mapping digital pins + */ + struct pin_map_t { + volatile uint8_t* ddr; /**< address of DDR for this pin */ + volatile uint8_t* pin; /**< address of PIN for this pin */ + volatile uint8_t* port; /**< address of PORT for this pin */ + uint8_t bit; /**< bit number for this pin */ + }; + + #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) + + // 168 and 328 Arduinos + const static pin_map_t pinMap[] = { + {&DDRD, &PIND, &PORTD, 0}, // D0 0 + {&DDRD, &PIND, &PORTD, 1}, // D1 1 + {&DDRD, &PIND, &PORTD, 2}, // D2 2 + {&DDRD, &PIND, &PORTD, 3}, // D3 3 + {&DDRD, &PIND, &PORTD, 4}, // D4 4 + {&DDRD, &PIND, &PORTD, 5}, // D5 5 + {&DDRD, &PIND, &PORTD, 6}, // D6 6 + {&DDRD, &PIND, &PORTD, 7}, // D7 7 + {&DDRB, &PINB, &PORTB, 0}, // B0 8 + {&DDRB, &PINB, &PORTB, 1}, // B1 9 + {&DDRB, &PINB, &PORTB, 2}, // B2 10 + {&DDRB, &PINB, &PORTB, 3}, // B3 11 + {&DDRB, &PINB, &PORTB, 4}, // B4 12 + {&DDRB, &PINB, &PORTB, 5}, // B5 13 + {&DDRC, &PINC, &PORTC, 0}, // C0 14 + {&DDRC, &PINC, &PORTC, 1}, // C1 15 + {&DDRC, &PINC, &PORTC, 2}, // C2 16 + {&DDRC, &PINC, &PORTC, 3}, // C3 17 + {&DDRC, &PINC, &PORTC, 4}, // C4 18 + {&DDRC, &PINC, &PORTC, 5} // C5 19 + }; + + #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + + // Mega + static const pin_map_t pinMap[] = { + {&DDRE, &PINE, &PORTE, 0}, // E0 0 + {&DDRE, &PINE, &PORTE, 1}, // E1 1 + {&DDRE, &PINE, &PORTE, 4}, // E4 2 + {&DDRE, &PINE, &PORTE, 5}, // E5 3 + {&DDRG, &PING, &PORTG, 5}, // G5 4 + {&DDRE, &PINE, &PORTE, 3}, // E3 5 + {&DDRH, &PINH, &PORTH, 3}, // H3 6 + {&DDRH, &PINH, &PORTH, 4}, // H4 7 + {&DDRH, &PINH, &PORTH, 5}, // H5 8 + {&DDRH, &PINH, &PORTH, 6}, // H6 9 + {&DDRB, &PINB, &PORTB, 4}, // B4 10 + {&DDRB, &PINB, &PORTB, 5}, // B5 11 + {&DDRB, &PINB, &PORTB, 6}, // B6 12 + {&DDRB, &PINB, &PORTB, 7}, // B7 13 + {&DDRJ, &PINJ, &PORTJ, 1}, // J1 14 + {&DDRJ, &PINJ, &PORTJ, 0}, // J0 15 + {&DDRH, &PINH, &PORTH, 1}, // H1 16 + {&DDRH, &PINH, &PORTH, 0}, // H0 17 + {&DDRD, &PIND, &PORTD, 3}, // D3 18 + {&DDRD, &PIND, &PORTD, 2}, // D2 19 + {&DDRD, &PIND, &PORTD, 1}, // D1 20 + {&DDRD, &PIND, &PORTD, 0}, // D0 21 + {&DDRA, &PINA, &PORTA, 0}, // A0 22 + {&DDRA, &PINA, &PORTA, 1}, // A1 23 + {&DDRA, &PINA, &PORTA, 2}, // A2 24 + {&DDRA, &PINA, &PORTA, 3}, // A3 25 + {&DDRA, &PINA, &PORTA, 4}, // A4 26 + {&DDRA, &PINA, &PORTA, 5}, // A5 27 + {&DDRA, &PINA, &PORTA, 6}, // A6 28 + {&DDRA, &PINA, &PORTA, 7}, // A7 29 + {&DDRC, &PINC, &PORTC, 7}, // C7 30 + {&DDRC, &PINC, &PORTC, 6}, // C6 31 + {&DDRC, &PINC, &PORTC, 5}, // C5 32 + {&DDRC, &PINC, &PORTC, 4}, // C4 33 + {&DDRC, &PINC, &PORTC, 3}, // C3 34 + {&DDRC, &PINC, &PORTC, 2}, // C2 35 + {&DDRC, &PINC, &PORTC, 1}, // C1 36 + {&DDRC, &PINC, &PORTC, 0}, // C0 37 + {&DDRD, &PIND, &PORTD, 7}, // D7 38 + {&DDRG, &PING, &PORTG, 2}, // G2 39 + {&DDRG, &PING, &PORTG, 1}, // G1 40 + {&DDRG, &PING, &PORTG, 0}, // G0 41 + {&DDRL, &PINL, &PORTL, 7}, // L7 42 + {&DDRL, &PINL, &PORTL, 6}, // L6 43 + {&DDRL, &PINL, &PORTL, 5}, // L5 44 + {&DDRL, &PINL, &PORTL, 4}, // L4 45 + {&DDRL, &PINL, &PORTL, 3}, // L3 46 + {&DDRL, &PINL, &PORTL, 2}, // L2 47 + {&DDRL, &PINL, &PORTL, 1}, // L1 48 + {&DDRL, &PINL, &PORTL, 0}, // L0 49 + {&DDRB, &PINB, &PORTB, 3}, // B3 50 + {&DDRB, &PINB, &PORTB, 2}, // B2 51 + {&DDRB, &PINB, &PORTB, 1}, // B1 52 + {&DDRB, &PINB, &PORTB, 0}, // B0 53 + {&DDRF, &PINF, &PORTF, 0}, // F0 54 + {&DDRF, &PINF, &PORTF, 1}, // F1 55 + {&DDRF, &PINF, &PORTF, 2}, // F2 56 + {&DDRF, &PINF, &PORTF, 3}, // F3 57 + {&DDRF, &PINF, &PORTF, 4}, // F4 58 + {&DDRF, &PINF, &PORTF, 5}, // F5 59 + {&DDRF, &PINF, &PORTF, 6}, // F6 60 + {&DDRF, &PINF, &PORTF, 7}, // F7 61 + {&DDRK, &PINK, &PORTK, 0}, // K0 62 + {&DDRK, &PINK, &PORTK, 1}, // K1 63 + {&DDRK, &PINK, &PORTK, 2}, // K2 64 + {&DDRK, &PINK, &PORTK, 3}, // K3 65 + {&DDRK, &PINK, &PORTK, 4}, // K4 66 + {&DDRK, &PINK, &PORTK, 5}, // K5 67 + {&DDRK, &PINK, &PORTK, 6}, // K6 68 + {&DDRK, &PINK, &PORTK, 7}, // K7 69 + + // pins_MIGHTYBOARD_REVE.h + {&DDRG, &PING, &PORTG, 4}, // G4 70 + {&DDRG, &PING, &PORTG, 3}, // G3 71 + {&DDRJ, &PINJ, &PORTJ, 2}, // J2 72 + {&DDRJ, &PINJ, &PORTJ, 3}, // J3 73 + {&DDRJ, &PINJ, &PORTJ, 7}, // J7 74 + {&DDRJ, &PINJ, &PORTJ, 4}, // J4 75 + {&DDRJ, &PINJ, &PORTJ, 5}, // J5 76 + {&DDRJ, &PINJ, &PORTJ, 6}, // J6 77 + {&DDRE, &PINE, &PORTE, 2}, // E2 78 + {&DDRE, &PINE, &PORTE, 6} // E6 79 + }; + + #elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__) \ + || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) \ + || defined(__AVR_ATmega64__) || defined(__AVR_ATmega32__) \ + || defined(__AVR_ATmega324__) || defined(__AVR_ATmega16__) + + #ifdef VARIANT_MIGHTY + + // Mighty Layout + static const pin_map_t pinMap[] = { + {&DDRB, &PINB, &PORTB, 0}, // B0 0 + {&DDRB, &PINB, &PORTB, 1}, // B1 1 + {&DDRB, &PINB, &PORTB, 2}, // B2 2 + {&DDRB, &PINB, &PORTB, 3}, // B3 3 + {&DDRB, &PINB, &PORTB, 4}, // B4 4 + {&DDRB, &PINB, &PORTB, 5}, // B5 5 + {&DDRB, &PINB, &PORTB, 6}, // B6 6 + {&DDRB, &PINB, &PORTB, 7}, // B7 7 + {&DDRD, &PIND, &PORTD, 0}, // D0 8 + {&DDRD, &PIND, &PORTD, 1}, // D1 9 + {&DDRD, &PIND, &PORTD, 2}, // D2 10 + {&DDRD, &PIND, &PORTD, 3}, // D3 11 + {&DDRD, &PIND, &PORTD, 4}, // D4 12 + {&DDRD, &PIND, &PORTD, 5}, // D5 13 + {&DDRD, &PIND, &PORTD, 6}, // D6 14 + {&DDRD, &PIND, &PORTD, 7}, // D7 15 + {&DDRC, &PINC, &PORTC, 0}, // C0 16 + {&DDRC, &PINC, &PORTC, 1}, // C1 17 + {&DDRC, &PINC, &PORTC, 2}, // C2 18 + {&DDRC, &PINC, &PORTC, 3}, // C3 19 + {&DDRC, &PINC, &PORTC, 4}, // C4 20 + {&DDRC, &PINC, &PORTC, 5}, // C5 21 + {&DDRC, &PINC, &PORTC, 6}, // C6 22 + {&DDRC, &PINC, &PORTC, 7}, // C7 23 + {&DDRA, &PINA, &PORTA, 0}, // A0 24 + {&DDRA, &PINA, &PORTA, 1}, // A1 25 + {&DDRA, &PINA, &PORTA, 2}, // A2 26 + {&DDRA, &PINA, &PORTA, 3}, // A3 27 + {&DDRA, &PINA, &PORTA, 4}, // A4 28 + {&DDRA, &PINA, &PORTA, 5}, // A5 29 + {&DDRA, &PINA, &PORTA, 6}, // A6 30 + {&DDRA, &PINA, &PORTA, 7} // A7 31 + }; + + #elif defined(VARIANT_BOBUINO) + + // Bobuino Layout + static const pin_map_t pinMap[] = { + {&DDRD, &PIND, &PORTD, 0}, // D0 0 + {&DDRD, &PIND, &PORTD, 1}, // D1 1 + {&DDRD, &PIND, &PORTD, 2}, // D2 2 + {&DDRD, &PIND, &PORTD, 3}, // D3 3 + {&DDRB, &PINB, &PORTB, 0}, // B0 4 + {&DDRB, &PINB, &PORTB, 1}, // B1 5 + {&DDRB, &PINB, &PORTB, 2}, // B2 6 + {&DDRB, &PINB, &PORTB, 3}, // B3 7 + {&DDRD, &PIND, &PORTD, 5}, // D5 8 + {&DDRD, &PIND, &PORTD, 6}, // D6 9 + {&DDRB, &PINB, &PORTB, 4}, // B4 10 + {&DDRB, &PINB, &PORTB, 5}, // B5 11 + {&DDRB, &PINB, &PORTB, 6}, // B6 12 + {&DDRB, &PINB, &PORTB, 7}, // B7 13 + {&DDRA, &PINA, &PORTA, 7}, // A7 14 + {&DDRA, &PINA, &PORTA, 6}, // A6 15 + {&DDRA, &PINA, &PORTA, 5}, // A5 16 + {&DDRA, &PINA, &PORTA, 4}, // A4 17 + {&DDRA, &PINA, &PORTA, 3}, // A3 18 + {&DDRA, &PINA, &PORTA, 2}, // A2 19 + {&DDRA, &PINA, &PORTA, 1}, // A1 20 + {&DDRA, &PINA, &PORTA, 0}, // A0 21 + {&DDRC, &PINC, &PORTC, 0}, // C0 22 + {&DDRC, &PINC, &PORTC, 1}, // C1 23 + {&DDRC, &PINC, &PORTC, 2}, // C2 24 + {&DDRC, &PINC, &PORTC, 3}, // C3 25 + {&DDRC, &PINC, &PORTC, 4}, // C4 26 + {&DDRC, &PINC, &PORTC, 5}, // C5 27 + {&DDRC, &PINC, &PORTC, 6}, // C6 28 + {&DDRC, &PINC, &PORTC, 7}, // C7 29 + {&DDRD, &PIND, &PORTD, 4}, // D4 30 + {&DDRD, &PIND, &PORTD, 7} // D7 31 + }; + + #elif defined(VARIANT_STANDARD) + + // Standard Layout + static const pin_map_t pinMap[] = { + {&DDRB, &PINB, &PORTB, 0}, // B0 0 + {&DDRB, &PINB, &PORTB, 1}, // B1 1 + {&DDRB, &PINB, &PORTB, 2}, // B2 2 + {&DDRB, &PINB, &PORTB, 3}, // B3 3 + {&DDRB, &PINB, &PORTB, 4}, // B4 4 + {&DDRB, &PINB, &PORTB, 5}, // B5 5 + {&DDRB, &PINB, &PORTB, 6}, // B6 6 + {&DDRB, &PINB, &PORTB, 7}, // B7 7 + {&DDRD, &PIND, &PORTD, 0}, // D0 8 + {&DDRD, &PIND, &PORTD, 1}, // D1 9 + {&DDRD, &PIND, &PORTD, 2}, // D2 10 + {&DDRD, &PIND, &PORTD, 3}, // D3 11 + {&DDRD, &PIND, &PORTD, 4}, // D4 12 + {&DDRD, &PIND, &PORTD, 5}, // D5 13 + {&DDRD, &PIND, &PORTD, 6}, // D6 14 + {&DDRD, &PIND, &PORTD, 7}, // D7 15 + {&DDRC, &PINC, &PORTC, 0}, // C0 16 + {&DDRC, &PINC, &PORTC, 1}, // C1 17 + {&DDRC, &PINC, &PORTC, 2}, // C2 18 + {&DDRC, &PINC, &PORTC, 3}, // C3 19 + {&DDRC, &PINC, &PORTC, 4}, // C4 20 + {&DDRC, &PINC, &PORTC, 5}, // C5 21 + {&DDRC, &PINC, &PORTC, 6}, // C6 22 + {&DDRC, &PINC, &PORTC, 7}, // C7 23 + {&DDRA, &PINA, &PORTA, 7}, // A7 24 + {&DDRA, &PINA, &PORTA, 6}, // A6 25 + {&DDRA, &PINA, &PORTA, 5}, // A5 26 + {&DDRA, &PINA, &PORTA, 4}, // A4 27 + {&DDRA, &PINA, &PORTA, 3}, // A3 28 + {&DDRA, &PINA, &PORTA, 2}, // A2 29 + {&DDRA, &PINA, &PORTA, 1}, // A1 30 + {&DDRA, &PINA, &PORTA, 0} // A0 31 + }; + + #else // !(VARIANT_MIGHTY || VARIANT_BOBUINO || VARIANT_STANDARD) + + #error Undefined variant 1284, 644, 324, 64, 32 + + #endif + + #elif defined(__AVR_ATmega32U4__) + + #ifdef CORE_TEENSY + + // Teensy 2.0 + static const pin_map_t pinMap[] = { + {&DDRB, &PINB, &PORTB, 0}, // B0 0 + {&DDRB, &PINB, &PORTB, 1}, // B1 1 + {&DDRB, &PINB, &PORTB, 2}, // B2 2 + {&DDRB, &PINB, &PORTB, 3}, // B3 3 + {&DDRB, &PINB, &PORTB, 7}, // B7 4 + {&DDRD, &PIND, &PORTD, 0}, // D0 5 + {&DDRD, &PIND, &PORTD, 1}, // D1 6 + {&DDRD, &PIND, &PORTD, 2}, // D2 7 + {&DDRD, &PIND, &PORTD, 3}, // D3 8 + {&DDRC, &PINC, &PORTC, 6}, // C6 9 + {&DDRC, &PINC, &PORTC, 7}, // C7 10 + {&DDRD, &PIND, &PORTD, 6}, // D6 11 + {&DDRD, &PIND, &PORTD, 7}, // D7 12 + {&DDRB, &PINB, &PORTB, 4}, // B4 13 + {&DDRB, &PINB, &PORTB, 5}, // B5 14 + {&DDRB, &PINB, &PORTB, 6}, // B6 15 + {&DDRF, &PINF, &PORTF, 7}, // F7 16 + {&DDRF, &PINF, &PORTF, 6}, // F6 17 + {&DDRF, &PINF, &PORTF, 5}, // F5 18 + {&DDRF, &PINF, &PORTF, 4}, // F4 19 + {&DDRF, &PINF, &PORTF, 1}, // F1 20 + {&DDRF, &PINF, &PORTF, 0}, // F0 21 + {&DDRD, &PIND, &PORTD, 4}, // D4 22 + {&DDRD, &PIND, &PORTD, 5}, // D5 23 + {&DDRE, &PINE, &PORTE, 6} // E6 24 + }; + + #else // !CORE_TEENSY + + // Leonardo + static const pin_map_t pinMap[] = { + {&DDRD, &PIND, &PORTD, 2}, // D2 0 + {&DDRD, &PIND, &PORTD, 3}, // D3 1 + {&DDRD, &PIND, &PORTD, 1}, // D1 2 + {&DDRD, &PIND, &PORTD, 0}, // D0 3 + {&DDRD, &PIND, &PORTD, 4}, // D4 4 + {&DDRC, &PINC, &PORTC, 6}, // C6 5 + {&DDRD, &PIND, &PORTD, 7}, // D7 6 + {&DDRE, &PINE, &PORTE, 6}, // E6 7 + {&DDRB, &PINB, &PORTB, 4}, // B4 8 + {&DDRB, &PINB, &PORTB, 5}, // B5 9 + {&DDRB, &PINB, &PORTB, 6}, // B6 10 + {&DDRB, &PINB, &PORTB, 7}, // B7 11 + {&DDRD, &PIND, &PORTD, 6}, // D6 12 + {&DDRC, &PINC, &PORTC, 7}, // C7 13 + {&DDRB, &PINB, &PORTB, 3}, // B3 14 + {&DDRB, &PINB, &PORTB, 1}, // B1 15 + {&DDRB, &PINB, &PORTB, 2}, // B2 16 + {&DDRB, &PINB, &PORTB, 0}, // B0 17 + {&DDRF, &PINF, &PORTF, 7}, // F7 18 + {&DDRF, &PINF, &PORTF, 6}, // F6 19 + {&DDRF, &PINF, &PORTF, 5}, // F5 20 + {&DDRF, &PINF, &PORTF, 4}, // F4 21 + {&DDRF, &PINF, &PORTF, 1}, // F1 22 + {&DDRF, &PINF, &PORTF, 0}, // F0 23 + {&DDRD, &PIND, &PORTD, 4}, // D4 24 + {&DDRD, &PIND, &PORTD, 7}, // D7 25 + {&DDRB, &PINB, &PORTB, 4}, // B4 26 + {&DDRB, &PINB, &PORTB, 5}, // B5 27 + {&DDRB, &PINB, &PORTB, 6}, // B6 28 + {&DDRD, &PIND, &PORTD, 6} // D6 29 + }; + + #endif // !CORE_TEENSY + + #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) + + // Teensy++ 1.0 & 2.0 + static const pin_map_t pinMap[] = { + {&DDRD, &PIND, &PORTD, 0}, // D0 0 + {&DDRD, &PIND, &PORTD, 1}, // D1 1 + {&DDRD, &PIND, &PORTD, 2}, // D2 2 + {&DDRD, &PIND, &PORTD, 3}, // D3 3 + {&DDRD, &PIND, &PORTD, 4}, // D4 4 + {&DDRD, &PIND, &PORTD, 5}, // D5 5 + {&DDRD, &PIND, &PORTD, 6}, // D6 6 + {&DDRD, &PIND, &PORTD, 7}, // D7 7 + {&DDRE, &PINE, &PORTE, 0}, // E0 8 + {&DDRE, &PINE, &PORTE, 1}, // E1 9 + {&DDRC, &PINC, &PORTC, 0}, // C0 10 + {&DDRC, &PINC, &PORTC, 1}, // C1 11 + {&DDRC, &PINC, &PORTC, 2}, // C2 12 + {&DDRC, &PINC, &PORTC, 3}, // C3 13 + {&DDRC, &PINC, &PORTC, 4}, // C4 14 + {&DDRC, &PINC, &PORTC, 5}, // C5 15 + {&DDRC, &PINC, &PORTC, 6}, // C6 16 + {&DDRC, &PINC, &PORTC, 7}, // C7 17 + {&DDRE, &PINE, &PORTE, 6}, // E6 18 + {&DDRE, &PINE, &PORTE, 7}, // E7 19 + {&DDRB, &PINB, &PORTB, 0}, // B0 20 + {&DDRB, &PINB, &PORTB, 1}, // B1 21 + {&DDRB, &PINB, &PORTB, 2}, // B2 22 + {&DDRB, &PINB, &PORTB, 3}, // B3 23 + {&DDRB, &PINB, &PORTB, 4}, // B4 24 + {&DDRB, &PINB, &PORTB, 5}, // B5 25 + {&DDRB, &PINB, &PORTB, 6}, // B6 26 + {&DDRB, &PINB, &PORTB, 7}, // B7 27 + {&DDRA, &PINA, &PORTA, 0}, // A0 28 + {&DDRA, &PINA, &PORTA, 1}, // A1 29 + {&DDRA, &PINA, &PORTA, 2}, // A2 30 + {&DDRA, &PINA, &PORTA, 3}, // A3 31 + {&DDRA, &PINA, &PORTA, 4}, // A4 32 + {&DDRA, &PINA, &PORTA, 5}, // A5 33 + {&DDRA, &PINA, &PORTA, 6}, // A6 34 + {&DDRA, &PINA, &PORTA, 7}, // A7 35 + {&DDRE, &PINE, &PORTE, 4}, // E4 36 + {&DDRE, &PINE, &PORTE, 5}, // E5 37 + {&DDRF, &PINF, &PORTF, 0}, // F0 38 + {&DDRF, &PINF, &PORTF, 1}, // F1 39 + {&DDRF, &PINF, &PORTF, 2}, // F2 40 + {&DDRF, &PINF, &PORTF, 3}, // F3 41 + {&DDRF, &PINF, &PORTF, 4}, // F4 42 + {&DDRF, &PINF, &PORTF, 5}, // F5 43 + {&DDRF, &PINF, &PORTF, 6}, // F6 44 + {&DDRF, &PINF, &PORTF, 7} // F7 45 + }; + + #else // CPU type + + #error "Unknown CPU type for Software SPI" + + #endif // CPU type + + /** count of pins */ + static constexpr uint8_t digitalPinCount = sizeof(pinMap) / sizeof(pin_map_t); + + /** generate bad pin number error */ + void badPinNumber(void) + __attribute__((error("Pin number is too large or not a constant"))); + + /** + * Check for valid pin number + * @param[in] pin Number of pin to be checked. + */ + FORCE_INLINE static void badPinCheck(const uint8_t pin) { + if (!__builtin_constant_p(pin) || pin >= digitalPinCount) badPinNumber(); + } + + /** + * Fast write helper + * @param[in] address I/O register address + * @param[in] bit bit number to write + * @param[in] level value for bit + */ + FORCE_INLINE static void fastBitWriteSafe(volatile uint8_t* address, uint8_t bit, bool level) { + uint8_t oldSREG; + if (address > (uint8_t*)0x5F) { oldSREG = SREG; cli(); } + if (level) SBI(*address, bit); else CBI(*address, bit); + if (address > (uint8_t*)0x5F) SREG = oldSREG; + } + + /** + * Read pin value + * @param[in] pin Arduino pin number + * @return value read + */ + FORCE_INLINE static bool fastDigitalRead(uint8_t pin) { + badPinCheck(pin); + return (*pinMap[pin].pin >> pinMap[pin].bit) & 1; + } + + /** + * Toggle a pin + * @param[in] pin Arduino pin number + * + * If the pin is in output mode toggle the pin level. + * If the pin is in input mode toggle the state of the 20K pullup. + */ + FORCE_INLINE static void fastDigitalToggle(uint8_t pin) { + badPinCheck(pin); + if (pinMap[pin].pin > (uint8_t*)0x5F) + *pinMap[pin].pin = _BV(pinMap[pin].bit); // Must write bit to high address port + else + SBI(*pinMap[pin].pin, pinMap[pin].bit); // Compiles to sbi and PIN register will not be read + } + + /** + * Set pin value + * @param[in] pin Arduino pin number + * @param[in] level value to write + */ + FORCE_INLINE static void fastDigitalWrite(uint8_t pin, bool level) { + badPinCheck(pin); + fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, level); + } + + /** + * Set pin mode + * @param[in] pin Arduino pin number + * @param[in] mode if true set output mode else input mode + * + * fastPinMode does not enable or disable the 20K pullup for input mode. + */ + FORCE_INLINE static void fastPinMode(uint8_t pin, bool mode) { + badPinCheck(pin); + fastBitWriteSafe(pinMap[pin].ddr, pinMap[pin].bit, mode); + } + +#endif // !__arm__ + +/** + * Set pin configuration * @param[in] pin Arduino pin number * @param[in] mode If true set output mode else input mode * @param[in] level If mode is output, set level high/low. * If mode is input, enable or disable the pin's 20K pullup. */ -static inline __attribute__((always_inline)) -void fastPinConfig(uint8_t pin, bool mode, bool level) { +FORCE_INLINE static void fastPinConfig(uint8_t pin, bool mode, bool level) { fastPinMode(pin, mode); fastDigitalWrite(pin, level); } -//============================================================================== + /** * @class DigitalPin * @brief Fast digital port I/O */ template class DigitalPin { - public: - //---------------------------------------------------------------------------- +public: + /** Constructor */ DigitalPin() {} - //---------------------------------------------------------------------------- - /** Constructor + + /** + * Constructor * @param[in] pinMode if true set output mode else input mode. */ - explicit DigitalPin(bool pinMode) { - mode(pinMode); - } - //---------------------------------------------------------------------------- - /** Constructor + explicit DigitalPin(bool pinMode) { mode(pinMode); } + + /** + * Constructor * @param[in] mode If true set output mode else input mode * @param[in] level If mode is output, set level high/low. * If mode is input, enable or disable the pin's 20K pullup. */ - DigitalPin(bool mode, bool level) { - config(mode, level); - } - //---------------------------------------------------------------------------- - /** Asignment operator + DigitalPin(bool mode, bool level) { config(mode, level); } + + /** + * Assignment operator * @param[in] value If true set the pin's level high else set the * pin's level low. * * @return This DigitalPin instance. */ - inline DigitalPin & operator = (bool value) __attribute__((always_inline)) { - write(value); - return *this; - } - //---------------------------------------------------------------------------- - /** Parenthesis operator + FORCE_INLINE DigitalPin & operator = (bool value) { write(value); return *this; } + + /** + * Parentheses operator * @return Pin's level */ - inline operator bool () const __attribute__((always_inline)) { - return read(); - } - //---------------------------------------------------------------------------- - /** set pin configuration + FORCE_INLINE operator bool () const { return read(); } + + /** + * Set pin configuration * @param[in] mode If true set output mode else input mode * @param[in] level If mode is output, set level high/low. * If mode is input, enable or disable the pin's 20K pullup. */ - inline __attribute__((always_inline)) - void config(bool mode, bool level) { - fastPinConfig(PinNumber, mode, level); - } - //---------------------------------------------------------------------------- + FORCE_INLINE void config(bool mode, bool level) { fastPinConfig(PinNumber, mode, level); } + /** * Set pin level high if output mode or enable 20K pullup if input mode. */ - inline __attribute__((always_inline)) - void high() {write(true);} - //---------------------------------------------------------------------------- + FORCE_INLINE void high() { write(true); } + /** * Set pin level low if output mode or disable 20K pullup if input mode. */ - inline __attribute__((always_inline)) - void low() {write(false);} - //---------------------------------------------------------------------------- + FORCE_INLINE void low() { write(false); } + /** * Set pin mode * @param[in] pinMode if true set output mode else input mode. * * mode() does not enable or disable the 20K pullup for input mode. */ - inline __attribute__((always_inline)) - void mode(bool pinMode) { - fastPinMode(PinNumber, pinMode); - } - //---------------------------------------------------------------------------- + FORCE_INLINE void mode(bool pinMode) { fastPinMode(PinNumber, pinMode); } + /** @return Pin's level */ - inline __attribute__((always_inline)) - bool read() const { - return fastDigitalRead(PinNumber); - } - //---------------------------------------------------------------------------- - /** toggle a pin - * + FORCE_INLINE bool read() const { return fastDigitalRead(PinNumber); } + + /** + * Toggle a pin * If the pin is in output mode toggle the pin's level. * If the pin is in input mode toggle the state of the 20K pullup. */ - inline __attribute__((always_inline)) - void toggle() { - fastDigitalToggle(PinNumber); - } - //---------------------------------------------------------------------------- - /** Write the pin's level. + FORCE_INLINE void toggle() { fastDigitalToggle(PinNumber); } + + /** + * Write the pin's level. * @param[in] value If true set the pin's level high else set the * pin's level low. */ - inline __attribute__((always_inline)) - void write(bool value) { - fastDigitalWrite(PinNumber, value); - } + FORCE_INLINE void write(bool value) { fastDigitalWrite(PinNumber, value); } }; -//------------------------------------------------------------------------------ -/** Nop for timing. */ -#define nop __asm__ volatile ("nop") -//------------------------------------------------------------------------------ -/** Pin Mode for MISO is input.*/ -const bool MISO_MODE = false; -/** Pullups disabled for MISO are disabled. */ -const bool MISO_LEVEL = false; -/** Pin Mode for MOSI is output.*/ -const bool MOSI_MODE = true; -/** Pin Mode for SCK is output. */ -const bool SCK_MODE = true; -//------------------------------------------------------------------------------ +const bool MISO_MODE = false, // Pin Mode for MISO is input. + MISO_LEVEL = false, // Pullups disabled for MISO are disabled. + MOSI_MODE = true, // Pin Mode for MOSI is output. + SCK_MODE = true; // Pin Mode for SCK is output. + /** * @class SoftSPI * @brief Fast software SPI. @@ -655,19 +637,19 @@ const bool SCK_MODE = true; template class SoftSPI { public: - //---------------------------------------------------------------------------- + /** Initialize SoftSPI pins. */ void begin() { fastPinConfig(MisoPin, MISO_MODE, MISO_LEVEL); fastPinConfig(MosiPin, MOSI_MODE, !MODE_CPHA(Mode)); fastPinConfig(SckPin, SCK_MODE, MODE_CPOL(Mode)); } - //---------------------------------------------------------------------------- - /** Soft SPI receive byte. + + /** + * Soft SPI receive byte. * @return Data byte received. */ - inline __attribute__((always_inline)) - uint8_t receive() { + FORCE_INLINE uint8_t receive() { uint8_t data = 0; receiveBit(7, &data); receiveBit(6, &data); @@ -679,12 +661,12 @@ class SoftSPI { receiveBit(0, &data); return data; } - //---------------------------------------------------------------------------- - /** Soft SPI send byte. + + /** + * Soft SPI send byte. * @param[in] data Data byte to send. */ - inline __attribute__((always_inline)) - void send(uint8_t data) { + FORCE_INLINE void send(uint8_t data) { sendBit(7, data); sendBit(6, data); sendBit(5, data); @@ -694,13 +676,13 @@ class SoftSPI { sendBit(1, data); sendBit(0, data); } - //---------------------------------------------------------------------------- - /** Soft SPI transfer byte. + + /** + * Soft SPI transfer byte. * @param[in] txData Data byte to send. * @return Data byte received. */ - inline __attribute__((always_inline)) - uint8_t transfer(uint8_t txData) { + FORCE_INLINE uint8_t transfer(uint8_t txData) { uint8_t rxData = 0; transferBit(7, &rxData, txData); transferBit(6, &rxData, txData); @@ -714,53 +696,35 @@ class SoftSPI { } private: - //---------------------------------------------------------------------------- - inline __attribute__((always_inline)) - bool MODE_CPHA(uint8_t mode) {return (mode & 1) != 0;} - inline __attribute__((always_inline)) - bool MODE_CPOL(uint8_t mode) {return (mode & 2) != 0;} - inline __attribute__((always_inline)) - void receiveBit(uint8_t bit, uint8_t* data) { - if (MODE_CPHA(Mode)) { - fastDigitalWrite(SckPin, !MODE_CPOL(Mode)); - } + + FORCE_INLINE bool MODE_CPHA(uint8_t mode) { return bool(mode & 1); } + FORCE_INLINE bool MODE_CPOL(uint8_t mode) { return bool(mode & 2); } + FORCE_INLINE void receiveBit(uint8_t bit, uint8_t* data) { + if (MODE_CPHA(Mode)) fastDigitalWrite(SckPin, !MODE_CPOL(Mode)); nop; nop; fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode)); - if (fastDigitalRead(MisoPin)) *data |= 1 << bit; - if (!MODE_CPHA(Mode)) { - fastDigitalWrite(SckPin, MODE_CPOL(Mode)); - } + if (fastDigitalRead(MisoPin)) SBI(*data, bit); + if (!MODE_CPHA(Mode)) fastDigitalWrite(SckPin, MODE_CPOL(Mode)); } - //---------------------------------------------------------------------------- - inline __attribute__((always_inline)) - void sendBit(uint8_t bit, uint8_t data) { - if (MODE_CPHA(Mode)) { - fastDigitalWrite(SckPin, !MODE_CPOL(Mode)); - } - fastDigitalWrite(MosiPin, data & (1 << bit)); - fastDigitalWrite(SckPin, - MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode)); + + FORCE_INLINE void sendBit(uint8_t bit, uint8_t data) { + if (MODE_CPHA(Mode)) fastDigitalWrite(SckPin, !MODE_CPOL(Mode)); + fastDigitalWrite(MosiPin, data & _BV(bit)); + fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode)); nop; nop; - if (!MODE_CPHA(Mode)) { - fastDigitalWrite(SckPin, MODE_CPOL(Mode)); - } + if (!MODE_CPHA(Mode)) fastDigitalWrite(SckPin, MODE_CPOL(Mode)); } - //---------------------------------------------------------------------------- - inline __attribute__((always_inline)) - void transferBit(uint8_t bit, uint8_t* rxData, uint8_t txData) { - if (MODE_CPHA(Mode)) { - fastDigitalWrite(SckPin, !MODE_CPOL(Mode)); - } - fastDigitalWrite(MosiPin, txData & (1 << bit)); + + FORCE_INLINE void transferBit(uint8_t bit, uint8_t* rxData, uint8_t txData) { + if (MODE_CPHA(Mode)) fastDigitalWrite(SckPin, !MODE_CPOL(Mode)); + fastDigitalWrite(MosiPin, txData & _BV(bit)); fastDigitalWrite(SckPin, MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode)); - if (fastDigitalRead(MisoPin)) *rxData |= 1 << bit; - if (!MODE_CPHA(Mode)) { - fastDigitalWrite(SckPin, MODE_CPOL(Mode)); - } + if (fastDigitalRead(MisoPin)) SBI(*rxData, bit); + if (!MODE_CPHA(Mode)) fastDigitalWrite(SckPin, MODE_CPOL(Mode)); } - //---------------------------------------------------------------------------- + };