|
|
|
@ -1,62 +1,78 @@
|
|
|
|
|
// https://github.com/niteris/ArduinoSoftSpi
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read pin value
|
|
|
|
|
* @param[in] pin Arduino pin number
|
|
|
|
|
* @return value read
|
|
|
|
|
*/
|
|
|
|
|
static inline __attribute__((always_inline))
|
|
|
|
|
bool fastDigitalRead(uint8_t pin) {
|
|
|
|
|
FORCE_INLINE static bool fastDigitalRead(uint8_t pin) {
|
|
|
|
|
return *portInputRegister(pin);
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/** Set pin value
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
#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) {
|
|
|
|
|
FORCE_INLINE static bool fastDigitalRead(uint8_t pin) {
|
|
|
|
|
return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin;
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/** Set pin value
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#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__
|
|
|
|
|
|
|
|
|
|
#else // !__arm__
|
|
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <util/atomic.h>
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @class pin_map_t
|
|
|
|
|
* @brief struct for mapping digital pins
|
|
|
|
@ -67,10 +83,9 @@ struct pin_map_t {
|
|
|
|
|
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__)
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
@ -94,9 +109,9 @@ const static pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRC, &PINC, &PORTC, 4}, // C4 18
|
|
|
|
|
{&DDRC, &PINC, &PORTC, 5} // C5 19
|
|
|
|
|
};
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
#elif defined(__AVR_ATmega1280__)\
|
|
|
|
|
|| defined(__AVR_ATmega2560__)
|
|
|
|
|
|
|
|
|
|
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
|
|
|
|
|
|
|
|
// Mega
|
|
|
|
|
static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRE, &PINE, &PORTE, 0}, // E0 0
|
|
|
|
@ -181,19 +196,15 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&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__)
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
@ -229,7 +240,9 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&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
|
|
|
|
@ -265,7 +278,9 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&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
|
|
|
|
@ -301,12 +316,17 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRA, &PINA, &PORTA, 1}, // A1 30
|
|
|
|
|
{&DDRA, &PINA, &PORTA, 0} // A0 31
|
|
|
|
|
};
|
|
|
|
|
#else // VARIANT_MIGHTY
|
|
|
|
|
|
|
|
|
|
#else // !(VARIANT_MIGHTY || VARIANT_BOBUINO || VARIANT_STANDARD)
|
|
|
|
|
|
|
|
|
|
#error Undefined variant 1284, 644, 324, 64, 32
|
|
|
|
|
#endif // VARIANT_MIGHTY
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#elif defined(__AVR_ATmega32U4__)
|
|
|
|
|
|
|
|
|
|
#ifdef CORE_TEENSY
|
|
|
|
|
|
|
|
|
|
// Teensy 2.0
|
|
|
|
|
static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRB, &PINB, &PORTB, 0}, // B0 0
|
|
|
|
@ -335,8 +355,9 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRD, &PIND, &PORTD, 5}, // D5 23
|
|
|
|
|
{&DDRE, &PINE, &PORTE, 6} // E6 24
|
|
|
|
|
};
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
#else // CORE_TEENSY
|
|
|
|
|
|
|
|
|
|
#else // !CORE_TEENSY
|
|
|
|
|
|
|
|
|
|
// Leonardo
|
|
|
|
|
static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRD, &PIND, &PORTD, 2}, // D2 0
|
|
|
|
@ -370,10 +391,11 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRB, &PINB, &PORTB, 6}, // B6 28
|
|
|
|
|
{&DDRD, &PIND, &PORTD, 6} // D6 29
|
|
|
|
|
};
|
|
|
|
|
#endif // CORE_TEENSY
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
#elif defined(__AVR_AT90USB646__)\
|
|
|
|
|
|| defined(__AVR_AT90USB1286__)
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
@ -423,114 +445,102 @@ static const pin_map_t pinMap[] = {
|
|
|
|
|
{&DDRF, &PINF, &PORTF, 6}, // F6 44
|
|
|
|
|
{&DDRF, &PINF, &PORTF, 7} // F7 45
|
|
|
|
|
};
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#else // CPU type
|
|
|
|
|
#error unknown CPU type
|
|
|
|
|
|
|
|
|
|
#error "Unknown CPU type for Software SPI"
|
|
|
|
|
|
|
|
|
|
#endif // CPU type
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** count of pins */
|
|
|
|
|
static const uint8_t digitalPinCount = sizeof(pinMap)/sizeof(pin_map_t);
|
|
|
|
|
//==============================================================================
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
FORCE_INLINE static void badPinCheck(const uint8_t pin) {
|
|
|
|
|
if (!__builtin_constant_p(pin) || pin >= digitalPinCount) badPinNumber();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/** fast write helper
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
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 (address > (uint8_t*)0x5F) { oldSREG = SREG; cli(); }
|
|
|
|
|
if (level) SBI(*address, bit); else CBI(*address, bit);
|
|
|
|
|
if (address > (uint8_t*)0x5F) SREG = oldSREG;
|
|
|
|
|
}
|
|
|
|
|
if (level) {
|
|
|
|
|
*address |= 1 << bit;
|
|
|
|
|
} else {
|
|
|
|
|
*address &= ~(1 << bit);
|
|
|
|
|
}
|
|
|
|
|
if (address > (uint8_t*)0x5F) {
|
|
|
|
|
SREG = oldSREG;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/** read pin value
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read pin value
|
|
|
|
|
* @param[in] pin Arduino pin number
|
|
|
|
|
* @return value read
|
|
|
|
|
*/
|
|
|
|
|
static inline __attribute__((always_inline))
|
|
|
|
|
bool fastDigitalRead(uint8_t pin) {
|
|
|
|
|
FORCE_INLINE static bool fastDigitalRead(uint8_t pin) {
|
|
|
|
|
badPinCheck(pin);
|
|
|
|
|
return (*pinMap[pin].pin >> pinMap[pin].bit) & 1;
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/** toggle a pin
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
FORCE_INLINE static 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;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
FORCE_INLINE static void fastDigitalWrite(uint8_t pin, bool level) {
|
|
|
|
|
badPinCheck(pin);
|
|
|
|
|
fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, level);
|
|
|
|
|
}
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
/** set pin mode
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
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
|
|
|
|
|
#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
|
|
|
|
@ -538,116 +548,88 @@ void fastPinConfig(uint8_t pin, bool mode, bool level) {
|
|
|
|
|
template<uint8_t PinNumber>
|
|
|
|
|
class DigitalPin {
|
|
|
|
|
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<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin, uint8_t Mode = 0>
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|