добавление mysensors в процессе не рабочая версия

This commit is contained in:
Dmitry Borisenko
2022-11-30 23:31:27 +01:00
parent 3e95049ce9
commit 9436af94df
304 changed files with 67751 additions and 29 deletions

View File

@@ -0,0 +1,36 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Fast Digital I/O functions
*
*/
#ifndef DigitalIO_h
#define DigitalIO_h 1
//------------------------------------------------------------------------------
/** DigitalPin version YYYYMMDD */
#define DIGITAL_IO_VERSION 20151127
//------------------------------------------------------------------------------
#include "DigitalPin.h"
#include "I2cConstants.h"
#include "PinIO.h"
#include "SoftI2cMaster.h"
#include "SoftSPI.h"
#endif // DigitalIO_h

View File

@@ -0,0 +1,416 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Fast Digital Pin functions
*
* @defgroup digitalPin Fast Pin I/O
* @details Fast Digital I/O functions and template class.
* @{
*/
#ifndef DigitalPin_h
#define DigitalPin_h
#if defined(__AVR__) || defined(DOXYGEN)
#include <avr/io.h>
/** GpioPinMap type */
struct GpioPinMap_t {
volatile uint8_t* pin; /**< address of PIN for this pin */
volatile uint8_t* ddr; /**< address of DDR for this pin */
volatile uint8_t* port; /**< address of PORT for this pin */
uint8_t mask; /**< bit mask for this pin */
};
/** Initializer macro. */
#define GPIO_PIN(reg, bit) {&PIN##reg, &DDR##reg, &PORT##reg, 1 << bit}
// Include pin map for current board.
#include "boards/GpioPinMap.h"
//------------------------------------------------------------------------------
/** 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 >= NUM_DIGITAL_PINS) {
badPinNumber();
}
}
//------------------------------------------------------------------------------
/** DDR register address
* @param[in] pin Arduino pin number
* @return register address
*/
static inline __attribute__((always_inline))
volatile uint8_t* ddrReg(uint8_t pin)
{
badPinCheck(pin);
return GpioPinMap[pin].ddr;
}
//------------------------------------------------------------------------------
/** Bit mask for pin
* @param[in] pin Arduino pin number
* @return mask
*/
static inline __attribute__((always_inline))
uint8_t pinMask(uint8_t pin)
{
badPinCheck(pin);
return GpioPinMap[pin].mask;
}
//------------------------------------------------------------------------------
/** PIN register address
* @param[in] pin Arduino pin number
* @return register address
*/
static inline __attribute__((always_inline))
volatile uint8_t* pinReg(uint8_t pin)
{
badPinCheck(pin);
return GpioPinMap[pin].pin;
}
//------------------------------------------------------------------------------
/** PORT register address
* @param[in] pin Arduino pin number
* @return register address
*/
static inline __attribute__((always_inline))
volatile uint8_t* portReg(uint8_t pin)
{
badPinCheck(pin);
return GpioPinMap[pin].port;
}
//------------------------------------------------------------------------------
/** Fast write helper.
* @param[in] address I/O register address
* @param[in] mask bit mask for pin
* @param[in] level value for bit
*/
static inline __attribute__((always_inline))
void fastBitWriteSafe(volatile uint8_t* address, uint8_t mask, bool level)
{
uint8_t s;
if (address > reinterpret_cast<uint8_t*>(0X3F)) {
s = SREG;
cli();
}
if (level) {
*address |= mask;
} else {
*address &= ~mask;
}
if (address > reinterpret_cast<uint8_t*>(0X3F)) {
SREG = s;
}
}
//------------------------------------------------------------------------------
/** Read pin value.
* @param[in] pin Arduino pin number
* @return value read
*/
static inline __attribute__((always_inline))
bool fastDigitalRead(uint8_t pin)
{
return *pinReg(pin) & pinMask(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)
{
if (pinReg(pin) > reinterpret_cast<uint8_t*>(0X3F)) {
// must write bit to high address port
*pinReg(pin) = pinMask(pin);
} else {
// will compile to sbi and PIN register will not be read.
*pinReg(pin) |= pinMask(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)
{
fastBitWriteSafe(portReg(pin), pinMask(pin), level);
}
//------------------------------------------------------------------------------
/** Write the DDR register.
* @param[in] pin Arduino pin number
* @param[in] level value to write
*/
static inline __attribute__((always_inline))
void fastDdrWrite(uint8_t pin, bool level)
{
fastBitWriteSafe(ddrReg(pin), pinMask(pin), level);
}
//------------------------------------------------------------------------------
/** Set pin mode.
* @param[in] pin Arduino pin number
* @param[in] mode INPUT, OUTPUT, or INPUT_PULLUP.
*
* The internal pullup resistors will be enabled if mode is INPUT_PULLUP
* and disabled if the mode is INPUT.
*/
static inline __attribute__((always_inline))
void fastPinMode(uint8_t pin, uint8_t mode)
{
fastDdrWrite(pin, mode == OUTPUT);
if (mode != OUTPUT) {
fastDigitalWrite(pin, mode == INPUT_PULLUP);
}
}
#else // defined(__AVR__)
#if defined(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;
}
}
#elif defined(__SAM3X8E__) || defined(__SAM3X8H__)
//------------------------------------------------------------------------------
/** 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;
}
}
#elif defined(ESP8266)
//------------------------------------------------------------------------------
/** Set pin value
* @param[in] pin Arduino pin number
* @param[in] val value to write
*/
static inline __attribute__((always_inline))
void fastDigitalWrite(uint8_t pin, uint8_t val)
{
if (pin < 16) {
if (val) {
GPOS = (1 << pin);
} else {
GPOC = (1 << pin);
}
} else if (pin == 16) {
if (val) {
GP16O |= 1;
} else {
GP16O &= ~1;
}
}
}
//------------------------------------------------------------------------------
/** Read pin value
* @param[in] pin Arduino pin number
* @return value read
*/
static inline __attribute__((always_inline))
bool fastDigitalRead(uint8_t pin)
{
if (pin < 16) {
return GPIP(pin);
} else if (pin == 16) {
return GP16I & 0x01;
}
return 0;
}
#else // CORE_TEENSY
//------------------------------------------------------------------------------
inline void fastDigitalWrite(uint8_t pin, bool value)
{
digitalWrite(pin, value);
}
//------------------------------------------------------------------------------
inline bool fastDigitalRead(uint8_t pin)
{
return digitalRead(pin);
}
#endif // CORE_TEENSY
//------------------------------------------------------------------------------
inline void fastDigitalToggle(uint8_t pin)
{
fastDigitalWrite(pin, !fastDigitalRead(pin));
}
//------------------------------------------------------------------------------
inline void fastPinMode(uint8_t pin, uint8_t mode)
{
pinMode(pin, mode);
}
#endif // __AVR__
//------------------------------------------------------------------------------
/** set pin configuration
* @param[in] pin Arduino pin number
* @param[in] mode mode INPUT or OUTPUT.
* @param[in] level If mode is output, set level high/low.
* If mode is input, enable or disable the pin's 20K pullup.
*/
#define fastPinConfig(pin, mode, level)\
{fastPinMode(pin, mode); fastDigitalWrite(pin, level);}
//==============================================================================
/**
* @class DigitalPin
* @brief Fast digital port I/O
*/
template<uint8_t PinNumber>
class DigitalPin
{
public:
//----------------------------------------------------------------------------
/** Constructor */
DigitalPin() {}
//----------------------------------------------------------------------------
/** Asignment 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.
* @return Pin's level
*/
inline operator bool () const __attribute__((always_inline))
{
return read();
}
//----------------------------------------------------------------------------
/** Set pin configuration.
* @param[in] mode: INPUT or OUTPUT.
* @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(uint8_t 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);
}
//----------------------------------------------------------------------------
/**
* Set pin level low if output mode or disable 20K pullup if input mode.
*/
inline __attribute__((always_inline))
void low()
{
write(false);
}
//----------------------------------------------------------------------------
/**
* Set pin mode.
* @param[in] mode: INPUT, OUTPUT, or INPUT_PULLUP.
*
* The internal pullup resistors will be enabled if mode is INPUT_PULLUP
* and disabled if the mode is INPUT.
*/
inline __attribute__((always_inline))
void mode(uint8_t mode)
{
fastPinMode(PinNumber, mode);
}
//----------------------------------------------------------------------------
/** @return Pin's level. */
inline __attribute__((always_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.
* @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);
}
};
#endif // DigitalPin_h
/** @} */

View File

@@ -0,0 +1,62 @@
/* Arduino I2C Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino I2C Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino I2C Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file I2cConstants.h
* @brief Two Wire Interface constants.
*
* @defgroup twoWire I2C constants
* @details Two Wire Interface library.
* @{
*/
#ifndef I2cConstants_h
#define I2cConstants_h
#include <inttypes.h>
/** Option argument for transfer() of transferContinue() to continue a
an I2C operation. */
const uint8_t I2C_CONTINUE = 0;
/** Option argument for transfer() of transferContinue() to end a
transfer with a STOP condition */
const uint8_t I2C_STOP = 1;
/** Option argument for transfer() of transferContinue() to end a
transfer with a repeated START condition */
const uint8_t I2C_REP_START = 2;
/** Set I2C bus speed to 100 kHz. Used by TwiMaster class. */
const uint8_t I2C_100KHZ = 0;
/** Set I2C bus speed to 400 kHz. Used by TwiMaster class. */
const uint8_t I2C_400KHZ = 1;
/** Bit to OR with address for a read operation. */
const uint8_t I2C_READ = 1;
/** Bit to OR with address for write operation. */
const uint8_t I2C_WRITE = 0;
/** Disable internal pull-ups on SDA and SCL. Used by TwiMaster class. */
const uint8_t I2C_NO_PULLUPS = 0;
/** Enable internal pull-ups on SDA and SCL. Used by TwiMaster class. */
const uint8_t I2C_INTERNAL_PULLUPS = 1;
#endif // I2cConstants_h
/** @} */

View File

@@ -0,0 +1,75 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Digital AVR port I/O with runtime pin number.
*
* @defgroup runtimeDigital Runtime Pin I/O
* @details Two Wire Interface library.
* @{
*/
#if defined(__AVR__) || defined(DOXYGEN) // AVR only
#include "PinIO.h"
#include <util/atomic.h>
#include <Arduino.h>
//==============================================================================
/** Constructor
* @param[in] pin Pin assigned to this object.
*/
PinIO::PinIO(uint8_t pin)
{
begin(pin);
}
//------------------------------------------------------------------------------
/** Initialize pin bit mask and port address.
* @param[in] pin Arduino board pin number.
* @return true for success or false if invalid pin number.
*/
bool PinIO::begin(uint8_t pin)
{
if (pin >= NUM_DIGITAL_PINS) {
return false;
}
uint8_t port = digitalPinToPort(pin);
pinReg_ = portInputRegister(port);
bit_ = digitalPinToBitMask(pin);
mask_ = ~bit_;
portReg_ = pinReg_ + 2;
return true;
}
//------------------------------------------------------------------------------
/** Configure the pin.
*
* @param[in] mode: INPUT or OUTPUT.
* @param[in] level If mode is OUTPUT, set level high/low.
* If mode is INPUT, enable or disable the pin's 20K pullup.
*
* This function may be used with interrupts enabled or disabled.
* The previous interrupt state will be restored.
*/
void PinIO::config(uint8_t mode, bool level)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
modeI(mode);
writeI(level);
}
}
#endif // __AVR__
/** @} */

View File

@@ -0,0 +1,194 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Digital AVR port I/O with runtime pin number.
*
* @defgroup runtimeDigital Runtime Pin I/O
* @details Two Wire Interface library.
* @{
*/
#ifndef PinIO_h
#define PinIO_h
#if defined(__AVR__) || defined(DOXYGEN) // AVR only
#include <Arduino.h>
#include <util/atomic.h>
#include <avr/io.h>
//------------------------------------------------------------------------------
/**
* @class PinIO
* @brief AVR port I/O with runtime pin numbers.
*/
class PinIO
{
public:
/** Create a PinIO object with no assigned pin. */
// cppcheck-suppress uninitMemberVar
PinIO() : bit_(0), mask_(0XFF) {}
explicit PinIO(uint8_t pin);
bool begin(uint8_t pin);
void config(uint8_t mode, bool data);
//----------------------------------------------------------------------------
/** @return Pin's level */
inline __attribute__((always_inline))
bool read()
{
return *pinReg_ & bit_;
}
//----------------------------------------------------------------------------
/** 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()
{
*pinReg_ = bit_;
}
//============================================================================
/**
* Set pin high if output mode or enable 20K pullup if input mode.
*
* This function must be called with interrupts disabled.
* This function will not change the interrupt state.
*/
inline __attribute__((always_inline))
void highI()
{
writeI(1);
}
/**
* Set pin low if output mode or disable 20K pullup if input mode.
*
* This function must be called with interrupts disabled.
* This function will not change the interrupt state.
*/
inline __attribute__((always_inline))
void lowI()
{
writeI(0);
}
/** Set pin mode.
*
* @param[in] mode: INPUT, OUTPUT, or INPUT_PULLUP.
*
* The internal pullup resistors will be enabled if mode is INPUT_PULLUP
* and disabled if the mode is INPUT.
*
* This function must be called with interrupts disabled.
* This function will not change the interrupt state.
*/
inline __attribute__((always_inline))
void modeI(uint8_t mode)
{
volatile uint8_t* ddrReg = pinReg_ + 1;
*ddrReg = mode == OUTPUT ? *ddrReg | bit_ : *ddrReg & mask_;
if (mode != OUTPUT) {
writeI(mode == INPUT_PULLUP);
}
}
/** Write pin.
*
* @param[in] level If output mode set pin high if true else low.
* If input mode enable 20K pullup if true else disable pullup.
*
* This function must be called with interrupts disabled.
* This function will not change the interrupt state.
*/
inline __attribute__((always_inline))
void writeI(bool level)
{
*portReg_ = level ? *portReg_ | bit_ : *portReg_ & mask_;
}
//============================================================================
/**
* Set pin level high if output mode or enable 20K pullup if input mode.
*
* This function will enable interrupts. This function should not be
* called in an ISR or where interrupts are disabled.
*/
inline __attribute__((always_inline))
void high()
{
ATOMIC_BLOCK(ATOMIC_FORCEON) {
highI();
}
}
/**
* Set pin level low if output mode or disable 20K pullup if input mode.
*
* This function will enable interrupts. This function should not be
* called in an ISR or where interrupts are disabled.
*/
inline __attribute__((always_inline))
void low()
{
ATOMIC_BLOCK(ATOMIC_FORCEON) {
lowI();
}
}
/**
* Set pin mode.
*
* @param[in] mode: INPUT, OUTPUT, or INPUT_PULLUP.
*
* The internal pullup resistors will be enabled if mode is INPUT_PULLUP
* and disabled if the mode is INPUT.
*
* This function will enable interrupts. This function should not be
* called in an ISR or where interrupts are disabled.
*/
inline __attribute__((always_inline))
void mode(uint8_t mode)
{
ATOMIC_BLOCK(ATOMIC_FORCEON) {
modeI(mode);
}
}
/** Write pin.
*
* @param[in] level If output mode set pin high if true else low.
* If input mode enable 20K pullup if true else disable pullup.
*
* This function will enable interrupts. This function should not be
* called in an ISR or where interrupts are disabled.
*/
inline __attribute__((always_inline))
void write(bool level)
{
ATOMIC_BLOCK(ATOMIC_FORCEON) {
writeI(level);
}
}
//----------------------------------------------------------------------------
private:
uint8_t bit_;
uint8_t mask_;
volatile uint8_t* pinReg_;
volatile uint8_t* portReg_;
};
#endif // __AVR__
#endif // PinIO_h
/** @} */

View File

@@ -0,0 +1,252 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#if defined(__AVR__) || defined(DOXYGEN) // AVR only
/**
* @file
* @brief AVR Software I2C library
*
* @defgroup softI2C Software I2C
* @details Software Two Wire Interface library.
* @{
*/
#include "SoftI2cMaster.h"
//------------------------------------------------------------------------------
/**
* Start an I2C transfer with possible continuation.
*
* @param[in] addrRW I2C slave address plus R/W bit.
* The I2C slave address is in the high seven bits
* and is ORed with on of the following:
* - I2C_READ for a read transfer.
* - I2C_WRITE for a write transfer.
* .
* @param[in,out] buf Source or destination for transfer.
* @param[in] nbytes Number of bytes to transfer (may be zero).
* @param[in] option Option for ending the transfer, one of:
* - I2C_STOP end the transfer with an I2C stop
* condition.
* - I2C_REP_START end the transfer with an I2C
* repeated start condition.
* - I2C_CONTINUE allow additional transferContinue()
* calls.
* .
* @return true for success else false.
*/
bool I2cMasterBase::transfer(uint8_t addrRW,
void *buf, size_t nbytes, uint8_t option)
{
if (_state != STATE_REP_START) {
start();
}
if (!write(addrRW)) {
_state = (addrRW & I2C_READ) ? STATE_RX_ADDR_NACK : STATE_TX_ADDR_NACK;
return false;
}
_state = (addrRW & I2C_READ) ? STATE_RX_DATA : STATE_TX_DATA;
return transferContinue(buf, nbytes, option);
}
//------------------------------------------------------------------------------
/**
* Continue an I2C transfer.
*
* @param[in,out] buf Source or destination for transfer.
* @param[in] nbytes Number of bytes to transfer (may be zero).
* @param[in] option Option for ending the transfer, one of:
* - I2C_STOP end the transfer with an I2C stop
* condition.
* - I2C_REP_START end the transfer with an I2C
* repeated start condition.
* - I2C_CONTINUE allow additional transferContinue()
* calls.
* .
* @return true for success else false.
*/
bool I2cMasterBase::transferContinue(void *buf, size_t nbytes, uint8_t option)
{
uint8_t* p = reinterpret_cast<uint8_t*>(buf);
if (_state == STATE_RX_DATA) {
for (size_t i = 0; i < nbytes; i++) {
p[i] = read(i == (nbytes - 1) && option != I2C_CONTINUE);
}
} else if (_state == STATE_TX_DATA) {
for (size_t i = 0; i < nbytes; i++) {
if (!write(p[i])) {
_state = STATE_TX_DATA_NACK;
return false;
}
}
} else {
return false;
}
if (option == I2C_STOP) {
stop();
_state = STATE_STOP;
} else if (option == I2C_REP_START) {
start();
_state = STATE_STOP;
}
return true;
}
//==============================================================================
// WARNING don't change SoftI2cMaster unless you verify the change with a scope
//------------------------------------------------------------------------------
/**
* Constructor, initialize SCL/SDA pins and set the bus high.
*
* @param[in] sdaPin The software SDA pin number.
*
* @param[in] sclPin The software SCL pin number.
*/
SoftI2cMaster::SoftI2cMaster(uint8_t sclPin, uint8_t sdaPin)
{
begin(sclPin, sdaPin);
}
//------------------------------------------------------------------------------
/**
* Initialize SCL/SDA pins and set the bus high.
*
* @param[in] sdaPin The software SDA pin number.
*
* @param[in] sclPin The software SCL pin number.
*/
void SoftI2cMaster::begin(uint8_t sclPin, uint8_t sdaPin)
{
uint8_t port;
// Get bit mask and address of scl registers.
_sclBit = digitalPinToBitMask(sclPin);
port = digitalPinToPort(sclPin);
_sclDDR = portModeRegister(port);
volatile uint8_t* sclOutReg = portOutputRegister(port);
// Get bit mask and address of sda registers.
_sdaBit = digitalPinToBitMask(sdaPin);
port = digitalPinToPort(sdaPin);
_sdaDDR = portModeRegister(port);
_sdaInReg = portInputRegister(port);
volatile uint8_t* sdaOutReg = portOutputRegister(port);
// Clear PORT bit for scl and sda.
uint8_t s = SREG;
noInterrupts();
*sclOutReg &= ~_sclBit;
*sdaOutReg &= ~_sdaBit;
SREG = s;
// Set scl and sda high.
writeScl(HIGH);
writeSda(HIGH);
}
//------------------------------------------------------------------------------
/* Read a byte and send ACK if more reads follow else NACK to terminate read.
*
* @param[in] last Set true to terminate the read else false.
*
* @return The byte read from the I2C bus.
*/
uint8_t SoftI2cMaster::read(uint8_t last)
{
uint8_t b = 0;
// Set sda to high Z mode for read.
writeSda(HIGH);
// Read a byte.
for (uint8_t i = 0; i < 8; i++) {
// Don't change this loop unless you verify the change with a scope.
b <<= 1;
sclDelay(16);
writeScl(HIGH);
sclDelay(12);
if (readSda()) {
b |= 1;
}
writeScl(LOW);
}
// send ACK or NACK
writeSda(last);
sclDelay(12);
writeScl(HIGH);
sclDelay(18);
writeScl(LOW);
writeSda(LOW);
return b;
}
//------------------------------------------------------------------------------
/* Issue a start condition. */
void SoftI2cMaster::start()
{
if (!readSda()) {
writeSda(HIGH);
writeScl(HIGH);
sclDelay(20);
}
writeSda(LOW);
sclDelay(20);
writeScl(LOW);
}
//------------------------------------------------------------------------------
/* Issue a stop condition. */
void SoftI2cMaster::stop(void)
{
writeSda(LOW);
sclDelay(20);
writeScl(HIGH);
sclDelay(20);
writeSda(HIGH);
sclDelay(20);
}
//------------------------------------------------------------------------------
/*
* Write a byte.
*
* @param[in] data The byte to send.
*
* @return The value true, 1, if the slave returned an ACK or false for NACK.
*/
bool SoftI2cMaster::write(uint8_t data)
{
// write byte
for (uint8_t m = 0X80; m != 0; m >>= 1) {
// don't change this loop unless you verify the change with a scope
writeSda(m & data);
sclDelay(8);
writeScl(HIGH);
sclDelay(18);
writeScl(LOW);
}
sclDelay(8);
// Go to sda high Z mode for input.
writeSda(HIGH);
writeScl(HIGH);
sclDelay(16);
// Get ACK or NACK.
uint8_t rtn = readSda();
// pull scl low.
writeScl(LOW);
// Pull sda low.
writeSda(LOW);
return rtn == 0;
}
#endif // __AVR__
/** @} */

View File

@@ -0,0 +1,314 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef SOFT_I2C_MASTER_H
#define SOFT_I2C_MASTER_H
/**
* @file
* @brief AVR Software I2C library
*
* @defgroup softI2C Software I2C
* @details Software Two Wire Interface library.
* @{
*/
#if defined(__AVR__) || defined(DOXYGEN) // AVR only
#include <Arduino.h>
#include <util/delay_basic.h>
#include "DigitalPin.h"
#include "I2cConstants.h"
//------------------------------------------------------------------------------
// State codes.
/** Stop condition transmitted. */
const uint8_t STATE_STOP = 0;
/** Repeated start condition transmitted. */
const uint8_t STATE_REP_START = 1;
/** Read data transfer active. */
const uint8_t STATE_RX_DATA = 2;
/** Write data transfer active. */
const uint8_t STATE_TX_DATA = 3;
/** Slave address plus read bit transmitted, NACK received. */
const uint8_t STATE_RX_ADDR_NACK = 4;
/** Slave address plus write bit transmitted, NACK received. */
const uint8_t STATE_TX_ADDR_NACK = 5;
/** Data byte transmitted, NACK received. */
const uint8_t STATE_TX_DATA_NACK = 6;
//==============================================================================
/**
* @class I2cMasterBase
* @brief Base class for FastI2cMaster, SoftI2cMaster
*/
class I2cMasterBase
{
public:
I2cMasterBase() : _state(STATE_STOP) {}
/** Read a byte
*
* @note This function should only be used by experts. Data should be
* accessed by calling transfer() and transferContinue()
*
* @param[in] last send a NACK to terminate read if last is true else
* send an ACK to continue the read.
*
* @return byte read from I2C bus
*/
virtual uint8_t read(uint8_t last) = 0;
/** Issue a start condition
*
* @note This function should only be used by experts. Data should be
* accessed by calling transfer() and transferContinue()
*/
virtual void start() = 0;
/** Issue a stop condition.
*
* @note This function should only be used by experts. Data should be
* accessed by calling transfer() and transferContinue()
*/
virtual void stop() = 0;
bool transfer(uint8_t addressRW, void *buf,
size_t nbyte, uint8_t option = I2C_STOP);
bool transferContinue(void *buf, size_t nbyte, uint8_t option = I2C_STOP);
/** Write a byte
*
* @note This function should only be used by experts. Data should be
* accessed by calling transfer() and transferContinue()
*
* @param[in] data byte to write
* @return true for ACK or false for NACK */
virtual bool write(uint8_t data) = 0;
private:
uint8_t _state;
};
//==============================================================================
/**
* @class SoftI2cMaster
* @brief AVR Software I2C master class
*/
class SoftI2cMaster : public I2cMasterBase
{
public:
SoftI2cMaster() {}
SoftI2cMaster(uint8_t sclPin, uint8_t sdaPin);
void begin(uint8_t sclPin, uint8_t sdaPin);
uint8_t read(uint8_t last);
void start();
void stop(void);
bool write(uint8_t b);
private:
uint8_t _sclBit;
uint8_t _sdaBit;
volatile uint8_t* _sclDDR;
volatile uint8_t* _sdaDDR;
volatile uint8_t* _sdaInReg;
//----------------------------------------------------------------------------
bool readSda()
{
return *_sdaInReg & _sdaBit;
}
//----------------------------------------------------------------------------
void sclDelay(uint8_t n)
{
_delay_loop_1(n);
}
//----------------------------------------------------------------------------
void writeScl(bool value)
{
uint8_t s = SREG;
noInterrupts();
if (value == LOW) {
// Pull scl low.
*_sclDDR |= _sclBit;
} else {
// Put scl in high Z input mode.
*_sclDDR &= ~_sclBit;
}
SREG = s;
}
//----------------------------------------------------------------------------
void writeSda(bool value)
{
uint8_t s = SREG;
noInterrupts();
if (value == LOW) {
// Pull sda low.
*_sdaDDR |= _sdaBit;
} else {
// Put sda in high Z input mode.
*_sdaDDR &= ~_sdaBit;
}
SREG = s;
}
};
//==============================================================================
// Template based fast software I2C
//------------------------------------------------------------------------------
/**
* @class FastI2cMaster
* @brief AVR Fast software I2C master class.
*/
template<uint8_t sclPin, uint8_t sdaPin>
class FastI2cMaster : public I2cMasterBase
{
public:
//----------------------------------------------------------------------------
FastI2cMaster()
{
begin();
}
//----------------------------------------------------------------------------
/** Initialize I2C bus pins. */
void begin()
{
fastDigitalWrite(sclPin, LOW);
fastDigitalWrite(sdaPin, LOW);
sclWrite(HIGH);
sdaWrite(HIGH);
}
//----------------------------------------------------------------------------
uint8_t read(uint8_t last)
{
uint8_t data = 0;
sdaWrite(HIGH);
readBit(7, &data);
readBit(6, &data);
readBit(5, &data);
readBit(4, &data);
readBit(3, &data);
readBit(2, &data);
readBit(1, &data);
readBit(0, &data);
// send ACK or NACK
sdaWrite(last);
sclDelay(4);
sclWrite(HIGH);
sclDelay(6);
sclWrite(LOW);
sdaWrite(LOW);
return data;
}
//----------------------------------------------------------------------------
void start()
{
if (!fastDigitalRead(sdaPin)) {
// It's a repeat start.
sdaWrite(HIGH);
sclDelay(8);
sclWrite(HIGH);
sclDelay(8);
}
sdaWrite(LOW);
sclDelay(8);
sclWrite(LOW);
sclDelay(8);
}
//----------------------------------------------------------------------------
void stop(void)
{
sdaWrite(LOW);
sclDelay(8);
sclWrite(HIGH);
sclDelay(8);
sdaWrite(HIGH);
sclDelay(8);
}
//----------------------------------------------------------------------------
bool write(uint8_t data)
{
// write byte
writeBit(7, data);
writeBit(6, data);
writeBit(5, data);
writeBit(4, data);
writeBit(3, data);
writeBit(2, data);
writeBit(1, data);
writeBit(0, data);
// get ACK or NACK
sdaWrite(HIGH);
sclWrite(HIGH);
sclDelay(5);
bool rtn = fastDigitalRead(sdaPin);
sclWrite(LOW);
sdaWrite(LOW);
return rtn == 0;
}
private:
//----------------------------------------------------------------------------
inline __attribute__((always_inline))
void sclWrite(bool value)
{
fastDdrWrite(sclPin, !value);
}
//----------------------------------------------------------------------------
inline __attribute__((always_inline))
void sdaWrite(bool value)
{
fastDdrWrite(sdaPin, !value);
}
//----------------------------------------------------------------------------
inline __attribute__((always_inline))
void readBit(uint8_t bit, uint8_t* data)
{
sclWrite(HIGH);
sclDelay(5);
if (fastDigitalRead(sdaPin)) {
*data |= 1 << bit;
}
sclWrite(LOW);
if (bit) {
sclDelay(6);
}
}
//----------------------------------------------------------------------------
void sclDelay(uint8_t n)
{
_delay_loop_1(n);
}
//----------------------------------------------------------------------------
inline __attribute__((always_inline))
void writeBit(uint8_t bit, uint8_t data)
{
uint8_t mask = 1 << bit;
sdaWrite(data & mask);
sclWrite(HIGH);
sclDelay(5);
sclWrite(LOW);
sclDelay(5);
}
};
#endif // __AVR__
#endif // SOFT_I2C_MASTER_H
/** @} */

View File

@@ -0,0 +1,180 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Software SPI.
*
* @defgroup softSPI Software SPI
* @details Software SPI Template Class.
* @{
*/
#ifndef SoftSPI_h
#define SoftSPI_h
#include "DigitalPin.h"
//------------------------------------------------------------------------------
/** Nop for timing. */
#define nop asm volatile ("nop\n\t")
//------------------------------------------------------------------------------
/** Pin Mode for MISO is input.*/
#define MISO_MODE INPUT
/** Pullups disabled for MISO are disabled. */
#define MISO_LEVEL false
/** Pin Mode for MOSI is output.*/
#define MOSI_MODE OUTPUT
/** Pin Mode for SCK is output. */
#define SCK_MODE OUTPUT
//------------------------------------------------------------------------------
/**
* @class SoftSPI
* @brief Fast software SPI.
*/
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.
* @return Data byte received.
*/
inline __attribute__((always_inline))
uint8_t receive()
{
uint8_t data = 0;
receiveBit(7, &data);
receiveBit(6, &data);
receiveBit(5, &data);
receiveBit(4, &data);
receiveBit(3, &data);
receiveBit(2, &data);
receiveBit(1, &data);
receiveBit(0, &data);
return data;
}
//----------------------------------------------------------------------------
/** Soft SPI send byte.
* @param[in] data Data byte to send.
*/
inline __attribute__((always_inline))
void send(uint8_t data)
{
sendBit(7, data);
sendBit(6, data);
sendBit(5, data);
sendBit(4, data);
sendBit(3, data);
sendBit(2, data);
sendBit(1, data);
sendBit(0, data);
}
//----------------------------------------------------------------------------
/** 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)
{
uint8_t rxData = 0;
transferBit(7, &rxData, txData);
transferBit(6, &rxData, txData);
transferBit(5, &rxData, txData);
transferBit(4, &rxData, txData);
transferBit(3, &rxData, txData);
transferBit(2, &rxData, txData);
transferBit(1, &rxData, txData);
transferBit(0, &rxData, txData);
return rxData;
}
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));
}
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));
}
}
//----------------------------------------------------------------------------
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));
nop;
nop;
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));
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));
}
}
//----------------------------------------------------------------------------
};
#endif // SoftSPI_h
/** @} */

View File

@@ -0,0 +1,37 @@
#ifndef AvrDevelopersGpioPinMap_h
#define AvrDevelopersGpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(B, 0), // D0
GPIO_PIN(B, 1), // D1
GPIO_PIN(B, 2), // D2
GPIO_PIN(B, 3), // D3
GPIO_PIN(B, 4), // D4
GPIO_PIN(B, 5), // D5
GPIO_PIN(B, 6), // D6
GPIO_PIN(B, 7), // D7
GPIO_PIN(D, 0), // D8
GPIO_PIN(D, 1), // D9
GPIO_PIN(D, 2), // D10
GPIO_PIN(D, 3), // D11
GPIO_PIN(D, 4), // D12
GPIO_PIN(D, 5), // D13
GPIO_PIN(D, 6), // D14
GPIO_PIN(D, 7), // D15
GPIO_PIN(C, 0), // D16
GPIO_PIN(C, 1), // D17
GPIO_PIN(C, 2), // D18
GPIO_PIN(C, 3), // D19
GPIO_PIN(C, 4), // D20
GPIO_PIN(C, 5), // D21
GPIO_PIN(C, 6), // D22
GPIO_PIN(C, 7), // D23
GPIO_PIN(A, 7), // D24
GPIO_PIN(A, 6), // D25
GPIO_PIN(A, 5), // D26
GPIO_PIN(A, 4), // D27
GPIO_PIN(A, 3), // D28
GPIO_PIN(A, 2), // D29
GPIO_PIN(A, 1), // D30
GPIO_PIN(A, 0) // D31
};
#endif // AvrDevelopersGpioPinMap_h

View File

@@ -0,0 +1,37 @@
#ifndef BobuinoGpioPinMap_h
#define BobuinoGpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(B, 0), // D0
GPIO_PIN(B, 1), // D1
GPIO_PIN(B, 2), // D2
GPIO_PIN(B, 3), // D3
GPIO_PIN(B, 4), // D4
GPIO_PIN(B, 5), // D5
GPIO_PIN(B, 6), // D6
GPIO_PIN(B, 7), // D7
GPIO_PIN(D, 0), // D8
GPIO_PIN(D, 1), // D9
GPIO_PIN(D, 2), // D10
GPIO_PIN(D, 3), // D11
GPIO_PIN(D, 4), // D12
GPIO_PIN(D, 5), // D13
GPIO_PIN(D, 6), // D14
GPIO_PIN(D, 7), // D15
GPIO_PIN(C, 0), // D16
GPIO_PIN(C, 1), // D17
GPIO_PIN(C, 2), // D18
GPIO_PIN(C, 3), // D19
GPIO_PIN(C, 4), // D20
GPIO_PIN(C, 5), // D21
GPIO_PIN(C, 6), // D22
GPIO_PIN(C, 7), // D23
GPIO_PIN(A, 0), // D24
GPIO_PIN(A, 1), // D25
GPIO_PIN(A, 2), // D26
GPIO_PIN(A, 3), // D27
GPIO_PIN(A, 4), // D28
GPIO_PIN(A, 5), // D29
GPIO_PIN(A, 6), // D30
GPIO_PIN(A, 7) // D31
};
#endif // BobuinoGpioPinMap_h

View File

@@ -0,0 +1,64 @@
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef GpioPinMap_h
#define GpioPinMap_h
#if defined(__AVR_ATmega168__)\
||defined(__AVR_ATmega168P__)\
||defined(__AVR_ATmega328P__)
// 168 and 328 Arduinos
#include "UnoGpioPinMap.h"
#elif defined(__AVR_ATmega1280__)\
|| defined(__AVR_ATmega2560__)
// Mega ADK
#include "MegaGpioPinMap.h"
#elif defined(__AVR_ATmega32U4__)
#ifdef CORE_TEENSY
#include "Teensy2GpioPinMap.h"
#else // CORE_TEENSY
// Leonardo or Yun
#include "LeonardoGpioPinMap.h"
#endif // CORE_TEENSY
#elif defined(__AVR_AT90USB646__)\
|| defined(__AVR_AT90USB1286__)
// Teensy++ 1.0 & 2.0
#include "Teensy2ppGpioPinMap.h"
#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 ARDUINO_1284P_AVR_DEVELOPERS
#include "AvrDevelopersGpioPinMap.h"
#elif defined(ARDUINO_1284P_BOBUINO)
#include "BobuinoGpioPinMap.h"
#elif defined(ARDUINO_1284P_SLEEPINGBEAUTY)
#include "SleepingBeautyGpioPinMap.h"
#elif defined(ARDUINO_1284P_STANDARD)
#include "Standard1284GpioPinMap.h"
#else // ARDUINO_1284P_SLEEPINGBEAUTY
#error Undefined variant 1284, 644, 324
#endif // ARDUINO_1284P_SLEEPINGBEAUTY
#else // 1284P, 1284, 644
#error Unknown board type.
#endif // end all boards
#endif // GpioPinMap_h

View File

@@ -0,0 +1,35 @@
#ifndef LeonardoGpioPinMap_h
#define LeonardoGpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(D, 2), // D0
GPIO_PIN(D, 3), // D1
GPIO_PIN(D, 1), // D2
GPIO_PIN(D, 0), // D3
GPIO_PIN(D, 4), // D4
GPIO_PIN(C, 6), // D5
GPIO_PIN(D, 7), // D6
GPIO_PIN(E, 6), // D7
GPIO_PIN(B, 4), // D8
GPIO_PIN(B, 5), // D9
GPIO_PIN(B, 6), // D10
GPIO_PIN(B, 7), // D11
GPIO_PIN(D, 6), // D12
GPIO_PIN(C, 7), // D13
GPIO_PIN(B, 3), // D14
GPIO_PIN(B, 1), // D15
GPIO_PIN(B, 2), // D16
GPIO_PIN(B, 0), // D17
GPIO_PIN(F, 7), // D18
GPIO_PIN(F, 6), // D19
GPIO_PIN(F, 5), // D20
GPIO_PIN(F, 4), // D21
GPIO_PIN(F, 1), // D22
GPIO_PIN(F, 0), // D23
GPIO_PIN(D, 4), // D24
GPIO_PIN(D, 7), // D25
GPIO_PIN(B, 4), // D26
GPIO_PIN(B, 5), // D27
GPIO_PIN(B, 6), // D28
GPIO_PIN(D, 6) // D29
};
#endif // LeonardoGpioPinMap_h

View File

@@ -0,0 +1,75 @@
#ifndef MegaGpioPinMap_h
#define MegaGpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(E, 0), // D0
GPIO_PIN(E, 1), // D1
GPIO_PIN(E, 4), // D2
GPIO_PIN(E, 5), // D3
GPIO_PIN(G, 5), // D4
GPIO_PIN(E, 3), // D5
GPIO_PIN(H, 3), // D6
GPIO_PIN(H, 4), // D7
GPIO_PIN(H, 5), // D8
GPIO_PIN(H, 6), // D9
GPIO_PIN(B, 4), // D10
GPIO_PIN(B, 5), // D11
GPIO_PIN(B, 6), // D12
GPIO_PIN(B, 7), // D13
GPIO_PIN(J, 1), // D14
GPIO_PIN(J, 0), // D15
GPIO_PIN(H, 1), // D16
GPIO_PIN(H, 0), // D17
GPIO_PIN(D, 3), // D18
GPIO_PIN(D, 2), // D19
GPIO_PIN(D, 1), // D20
GPIO_PIN(D, 0), // D21
GPIO_PIN(A, 0), // D22
GPIO_PIN(A, 1), // D23
GPIO_PIN(A, 2), // D24
GPIO_PIN(A, 3), // D25
GPIO_PIN(A, 4), // D26
GPIO_PIN(A, 5), // D27
GPIO_PIN(A, 6), // D28
GPIO_PIN(A, 7), // D29
GPIO_PIN(C, 7), // D30
GPIO_PIN(C, 6), // D31
GPIO_PIN(C, 5), // D32
GPIO_PIN(C, 4), // D33
GPIO_PIN(C, 3), // D34
GPIO_PIN(C, 2), // D35
GPIO_PIN(C, 1), // D36
GPIO_PIN(C, 0), // D37
GPIO_PIN(D, 7), // D38
GPIO_PIN(G, 2), // D39
GPIO_PIN(G, 1), // D40
GPIO_PIN(G, 0), // D41
GPIO_PIN(L, 7), // D42
GPIO_PIN(L, 6), // D43
GPIO_PIN(L, 5), // D44
GPIO_PIN(L, 4), // D45
GPIO_PIN(L, 3), // D46
GPIO_PIN(L, 2), // D47
GPIO_PIN(L, 1), // D48
GPIO_PIN(L, 0), // D49
GPIO_PIN(B, 3), // D50
GPIO_PIN(B, 2), // D51
GPIO_PIN(B, 1), // D52
GPIO_PIN(B, 0), // D53
GPIO_PIN(F, 0), // D54
GPIO_PIN(F, 1), // D55
GPIO_PIN(F, 2), // D56
GPIO_PIN(F, 3), // D57
GPIO_PIN(F, 4), // D58
GPIO_PIN(F, 5), // D59
GPIO_PIN(F, 6), // D60
GPIO_PIN(F, 7), // D61
GPIO_PIN(K, 0), // D62
GPIO_PIN(K, 1), // D63
GPIO_PIN(K, 2), // D64
GPIO_PIN(K, 3), // D65
GPIO_PIN(K, 4), // D66
GPIO_PIN(K, 5), // D67
GPIO_PIN(K, 6), // D68
GPIO_PIN(K, 7) // D69
};
#endif // MegaGpioPinMap_h

View File

@@ -0,0 +1,37 @@
#ifndef SleepingBeautyGpioPinMap_h
#define SleepingBeautyGpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(D, 0), // D0
GPIO_PIN(D, 1), // D1
GPIO_PIN(D, 2), // D2
GPIO_PIN(D, 3), // D3
GPIO_PIN(B, 0), // D4
GPIO_PIN(B, 1), // D5
GPIO_PIN(B, 2), // D6
GPIO_PIN(B, 3), // D7
GPIO_PIN(D, 6), // D8
GPIO_PIN(D, 5), // D9
GPIO_PIN(B, 4), // D10
GPIO_PIN(B, 5), // D11
GPIO_PIN(B, 6), // D12
GPIO_PIN(B, 7), // D13
GPIO_PIN(C, 7), // D14
GPIO_PIN(C, 6), // D15
GPIO_PIN(A, 5), // D16
GPIO_PIN(A, 4), // D17
GPIO_PIN(A, 3), // D18
GPIO_PIN(A, 2), // D19
GPIO_PIN(A, 1), // D20
GPIO_PIN(A, 0), // D21
GPIO_PIN(D, 4), // D22
GPIO_PIN(D, 7), // D23
GPIO_PIN(C, 2), // D24
GPIO_PIN(C, 3), // D25
GPIO_PIN(C, 4), // D26
GPIO_PIN(C, 5), // D27
GPIO_PIN(C, 1), // D28
GPIO_PIN(C, 0), // D29
GPIO_PIN(A, 6), // D30
GPIO_PIN(A, 7) // D31
};
#endif // SleepingBeautyGpioPinMap_h

View File

@@ -0,0 +1,37 @@
#ifndef Standard1284GpioPinMap_h
#define Standard1284GpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(B, 0), // D0
GPIO_PIN(B, 1), // D1
GPIO_PIN(B, 2), // D2
GPIO_PIN(B, 3), // D3
GPIO_PIN(B, 4), // D4
GPIO_PIN(B, 5), // D5
GPIO_PIN(B, 6), // D6
GPIO_PIN(B, 7), // D7
GPIO_PIN(D, 0), // D8
GPIO_PIN(D, 1), // D9
GPIO_PIN(D, 2), // D10
GPIO_PIN(D, 3), // D11
GPIO_PIN(D, 4), // D12
GPIO_PIN(D, 5), // D13
GPIO_PIN(D, 6), // D14
GPIO_PIN(D, 7), // D15
GPIO_PIN(C, 0), // D16
GPIO_PIN(C, 1), // D17
GPIO_PIN(C, 2), // D18
GPIO_PIN(C, 3), // D19
GPIO_PIN(C, 4), // D20
GPIO_PIN(C, 5), // D21
GPIO_PIN(C, 6), // D22
GPIO_PIN(C, 7), // D23
GPIO_PIN(A, 0), // D24
GPIO_PIN(A, 1), // D25
GPIO_PIN(A, 2), // D26
GPIO_PIN(A, 3), // D27
GPIO_PIN(A, 4), // D28
GPIO_PIN(A, 5), // D29
GPIO_PIN(A, 6), // D30
GPIO_PIN(A, 7) // D31
};
#endif // Standard1284GpioPinMap_h

View File

@@ -0,0 +1,30 @@
#ifndef Teensy2GpioPinMap_h
#define Teensy2GpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(B, 0), // D0
GPIO_PIN(B, 1), // D1
GPIO_PIN(B, 2), // D2
GPIO_PIN(B, 3), // D3
GPIO_PIN(B, 7), // D4
GPIO_PIN(D, 0), // D5
GPIO_PIN(D, 1), // D6
GPIO_PIN(D, 2), // D7
GPIO_PIN(D, 3), // D8
GPIO_PIN(C, 6), // D9
GPIO_PIN(C, 7), // D10
GPIO_PIN(D, 6), // D11
GPIO_PIN(D, 7), // D12
GPIO_PIN(B, 4), // D13
GPIO_PIN(B, 5), // D14
GPIO_PIN(B, 6), // D15
GPIO_PIN(F, 7), // D16
GPIO_PIN(F, 6), // D17
GPIO_PIN(F, 5), // D18
GPIO_PIN(F, 4), // D19
GPIO_PIN(F, 1), // D20
GPIO_PIN(F, 0), // D21
GPIO_PIN(D, 4), // D22
GPIO_PIN(D, 5), // D23
GPIO_PIN(E, 6), // D24
};
#endif // Teensy2GpioPinMap_h

View File

@@ -0,0 +1,51 @@
#ifndef Teensypp2GpioPinMap_h
#define Teensypp2GpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(D, 0), // D0
GPIO_PIN(D, 1), // D1
GPIO_PIN(D, 2), // D2
GPIO_PIN(D, 3), // D3
GPIO_PIN(D, 4), // D4
GPIO_PIN(D, 5), // D5
GPIO_PIN(D, 6), // D6
GPIO_PIN(D, 7), // D7
GPIO_PIN(E, 0), // D8
GPIO_PIN(E, 1), // D9
GPIO_PIN(C, 0), // D10
GPIO_PIN(C, 1), // D11
GPIO_PIN(C, 2), // D12
GPIO_PIN(C, 3), // D13
GPIO_PIN(C, 4), // D14
GPIO_PIN(C, 5), // D15
GPIO_PIN(C, 6), // D16
GPIO_PIN(C, 7), // D17
GPIO_PIN(E, 6), // D18
GPIO_PIN(E, 7), // D19
GPIO_PIN(B, 0), // D20
GPIO_PIN(B, 1), // D21
GPIO_PIN(B, 2), // D22
GPIO_PIN(B, 3), // D23
GPIO_PIN(B, 4), // D24
GPIO_PIN(B, 5), // D25
GPIO_PIN(B, 6), // D26
GPIO_PIN(B, 7), // D27
GPIO_PIN(A, 0), // D28
GPIO_PIN(A, 1), // D29
GPIO_PIN(A, 2), // D30
GPIO_PIN(A, 3), // D31
GPIO_PIN(A, 4), // D32
GPIO_PIN(A, 5), // D33
GPIO_PIN(A, 6), // D34
GPIO_PIN(A, 7), // D35
GPIO_PIN(E, 4), // D36
GPIO_PIN(E, 5), // D37
GPIO_PIN(F, 0), // D38
GPIO_PIN(F, 1), // D39
GPIO_PIN(F, 2), // D40
GPIO_PIN(F, 3), // D41
GPIO_PIN(F, 4), // D42
GPIO_PIN(F, 5), // D43
GPIO_PIN(F, 6), // D44
GPIO_PIN(F, 7), // D45
};
#endif // Teensypp2GpioPinMap_h

View File

@@ -0,0 +1,25 @@
#ifndef UnoGpioPinMap_h
#define UnoGpioPinMap_h
static const GpioPinMap_t GpioPinMap[] = {
GPIO_PIN(D, 0), // D0
GPIO_PIN(D, 1), // D1
GPIO_PIN(D, 2), // D2
GPIO_PIN(D, 3), // D3
GPIO_PIN(D, 4), // D4
GPIO_PIN(D, 5), // D5
GPIO_PIN(D, 6), // D6
GPIO_PIN(D, 7), // D7
GPIO_PIN(B, 0), // D8
GPIO_PIN(B, 1), // D9
GPIO_PIN(B, 2), // D10
GPIO_PIN(B, 3), // D11
GPIO_PIN(B, 4), // D12
GPIO_PIN(B, 5), // D13
GPIO_PIN(C, 0), // D14
GPIO_PIN(C, 1), // D15
GPIO_PIN(C, 2), // D16
GPIO_PIN(C, 3), // D17
GPIO_PIN(C, 4), // D18
GPIO_PIN(C, 5) // D19
};
#endif // UnoGpioPinMap_h

View File

@@ -0,0 +1,31 @@
/*
* Optimized digital functions for AVR microcontrollers
* based on http://code.google.com/p/digitalwritefast
*/
#ifndef __digitalWriteFast_h_
#define __digitalWriteFast_h_
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_DUEMILANOVE) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) || defined (__AVR_ATmega168__)
#if defined(__AVR_ATmega328PB__)
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTD : (((__pin) >= 8 && (__pin) <= 13) ? &PORTB : (((__pin) >= 14 && (__pin) <= 19) ? &PORTC : &PORTE)))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRD : (((__pin) >= 8 && (__pin) <= 13) ? &DDRB : (((__pin) >= 14 && (__pin) <= 19) ? &DDRC : &DDRE)))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PIND : (((__pin) >= 8 && (__pin) <= 13) ? &PINB : (((__pin) >= 14 && (__pin) <= 19) ? &PINC : &PINE)))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 13) ? (__pin) - 8 : (((__pin) >= 14 && (__pin) <= 19) ? (__pin) - 14 : (((__pin) >= 20 && (__pin) <= 21) ? (__pin) - 18 : (__pin) - 22))))
#else
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTD : (((__pin) >= 8 && (__pin) <= 13) ? &PORTB : &PORTC))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRD : (((__pin) >= 8 && (__pin) <= 13) ? &DDRB : &DDRC))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PIND : (((__pin) >= 8 && (__pin) <= 13) ? &PINB : &PINC))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 13) ? (__pin) - 8 : (__pin) - 14))
#endif
#define digitalWriteFast(__pin, __value) do { if (__builtin_constant_p(__pin) && __builtin_constant_p(__value)) { bitWrite(*__digitalPinToPortReg(__pin), (uint8_t)__digitalPinToBit(__pin), (__value)); } else { digitalWrite((__pin), (__value)); } } while (0)
#define pinModeFast(__pin, __mode) do { if (__builtin_constant_p(__pin) && __builtin_constant_p(__mode) && (__mode!=INPUT_PULLUP)) { bitWrite(*__digitalPinToDDRReg(__pin), (uint8_t)__digitalPinToBit(__pin), (__mode)); } else { pinMode((__pin), (__mode)); } } while (0)
#define digitalReadFast(__pin) ( (bool) (__builtin_constant_p(__pin) ) ? (( bitRead(*__digitalPinToPINReg(__pin), (uint8_t)__digitalPinToBit(__pin))) ) : digitalRead((__pin)) )
#else
// for all other archs use built-in pin access functions
#define digitalWriteFast(__pin, __value) digitalWrite(__pin, __value)
#define pinModeFast(__pin, __value) pinMode(__pin, __value)
#define digitalReadFast(__pin) digitalRead(__pin)
#endif
#endif