Reorganizing library structure, prep for 2.0.0
This commit is contained in:
@@ -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 20140217
|
||||
//------------------------------------------------------------------------------
|
||||
#include "DigitalPin.h"
|
||||
#include "I2cConstants.h"
|
||||
#include "PinIO.h"
|
||||
#include "SoftI2cMaster.h"
|
||||
#include "SoftSPI.h"
|
||||
#endif // DigitalIO_h
|
||||
@@ -0,0 +1,586 @@
|
||||
/* 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
|
||||
* @ingroup internals
|
||||
* @details Fast Digital I/O functions and template class.
|
||||
* @{
|
||||
*/
|
||||
#ifndef DigitalPin_h
|
||||
#define DigitalPin_h
|
||||
#include <avr/io.h>
|
||||
#include <util/atomic.h>
|
||||
#include <Arduino.h>
|
||||
/** @brief Helper macro for complex inline attributes */
|
||||
#define ALWAYS_INLINE inline __attribute__((always_inline))
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* @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
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
#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 defined(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 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 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 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 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;
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Set pin value
|
||||
* @param[in] pin Arduino pin number
|
||||
* @param[in] level value to write
|
||||
*/
|
||||
static 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 ALWAYS_INLINE void fastPinMode(uint8_t pin, bool mode) {
|
||||
badPinCheck(pin);
|
||||
fastBitWriteSafe(pinMap[pin].ddr, pinMap[pin].bit, mode);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** 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 ALWAYS_INLINE void fastPinConfig(uint8_t pin, bool mode, bool level) {
|
||||
fastPinMode(pin, mode);
|
||||
fastDigitalWrite(pin, level);
|
||||
}
|
||||
//==============================================================================
|
||||
/**
|
||||
* @class DigitalPin
|
||||
* @brief Fast AVR digital port I/O
|
||||
*/
|
||||
template<uint8_t PinNumber>
|
||||
class DigitalPin {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/** Constructor */
|
||||
DigitalPin() {}
|
||||
//----------------------------------------------------------------------------
|
||||
/** Constructor
|
||||
* @param[in] pinMode if true set output mode else input mode.
|
||||
*/
|
||||
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
|
||||
* @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 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.
|
||||
*/
|
||||
ALWAYS_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.
|
||||
*/
|
||||
ALWAYS_INLINE void high() {write(true);}
|
||||
//----------------------------------------------------------------------------
|
||||
/**
|
||||
* Set pin level low if output mode or disable 20K pullup if input mode.
|
||||
*/
|
||||
ALWAYS_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.
|
||||
*/
|
||||
ALWAYS_INLINE void mode(bool pinMode) {
|
||||
fastPinMode(PinNumber, pinMode);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** @return Pin's level */
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
ALWAYS_INLINE void write(bool value) {
|
||||
fastDigitalWrite(PinNumber, value);
|
||||
}
|
||||
};
|
||||
#endif // DigitalPin_h
|
||||
/** @} */
|
||||
@@ -0,0 +1,63 @@
|
||||
/* 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
|
||||
* @ingroup internals
|
||||
* @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
|
||||
/** @} */
|
||||
@@ -0,0 +1,68 @@
|
||||
/* 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.
|
||||
* @{
|
||||
*/
|
||||
#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 Configure as output mode if true else input mode.
|
||||
* @param[in] data For output mode set pin high if true else low.
|
||||
* For input mode enable 20K pullup if true else Hi-Z.
|
||||
*
|
||||
* This function may be used with interrupts enabled or disabled.
|
||||
* The previous interrupt state will be restored.
|
||||
*/
|
||||
void PinIO::config(bool mode, bool data) {
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
modeI(mode);
|
||||
writeI(data);
|
||||
}
|
||||
}
|
||||
/** @} */
|
||||
@@ -0,0 +1,154 @@
|
||||
/* 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
|
||||
* @ingroup internals
|
||||
* @details Two Wire Interface library.
|
||||
* @{
|
||||
*/
|
||||
#ifndef PinIO_h
|
||||
#define PinIO_h
|
||||
#include <util/atomic.h>
|
||||
#include <avr/io.h>
|
||||
/** @brief Helper macro for complex inline attributes */
|
||||
#define ALWAYS_INLINE inline __attribute__((always_inline))
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* @class PinIO
|
||||
* @brief AVR port I/O with runtime pin numbers.
|
||||
*/
|
||||
class PinIO {
|
||||
public:
|
||||
/** Create a PinIO object with no assigned pin. */
|
||||
PinIO() : bit_(0), mask_(0XFF) {}
|
||||
/** Constructor
|
||||
* @param[in] pin Pin assigned to this object.
|
||||
*/
|
||||
explicit PinIO(uint8_t 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 begin(uint8_t pin);
|
||||
/** Configure the pin
|
||||
*
|
||||
* @param[in] mode Configure as output mode if true else input mode.
|
||||
* @param[in] data For output mode set pin high if true else low.
|
||||
* For input mode enable 20K pullup if true else Hi-Z.
|
||||
*
|
||||
* This function may be used with interrupts enabled or disabled.
|
||||
* The previous interrupt state will be restored.
|
||||
*/
|
||||
void config(bool mode, bool data);
|
||||
//----------------------------------------------------------------------------
|
||||
/** @return Pin's level */
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
ALWAYS_INLINE void lowI() {writeI(0);}
|
||||
/**
|
||||
* Set pin mode
|
||||
* @param[in] mode if true set output mode else input mode.
|
||||
*
|
||||
* mode() does not enable or disable the 20K pullup for input mode.
|
||||
*
|
||||
* This function must be called with interrupts disabled.
|
||||
* This function will not change the interrupt state.
|
||||
*/
|
||||
ALWAYS_INLINE void modeI(bool mode) {
|
||||
volatile uint8_t* ddrReg = pinReg_ + 1;
|
||||
*ddrReg = mode ? *ddrReg | bit_ : *ddrReg & mask_;
|
||||
}
|
||||
/** 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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
ALWAYS_INLINE void low() {ATOMIC_BLOCK(ATOMIC_FORCEON) {lowI();}}
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* This function will enable interrupts. This function should not be
|
||||
* called in an ISR or where interrupts are disabled.
|
||||
*/
|
||||
ALWAYS_INLINE void mode(bool pinMode) {ATOMIC_BLOCK(ATOMIC_FORCEON) {modeI(pinMode);}}
|
||||
/** 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.
|
||||
*/
|
||||
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 // PinIO_h
|
||||
/** @} */
|
||||
@@ -0,0 +1,239 @@
|
||||
/* 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 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;
|
||||
}
|
||||
/** @} */
|
||||
@@ -0,0 +1,335 @@
|
||||
/* 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 Software I2C library
|
||||
*
|
||||
* @defgroup softI2C Software I2C
|
||||
* @ingroup internals
|
||||
* @details Software Two Wire Interface library.
|
||||
* @{
|
||||
*/
|
||||
#if ARDUINO < 100
|
||||
#error Requires Arduino 1.0 or greater.
|
||||
#else // ARDUINO
|
||||
#include <Arduino.h>
|
||||
#endif // ARDUINO
|
||||
#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;
|
||||
|
||||
/**
|
||||
* Start an I2C transfer with possible continuation.
|
||||
*
|
||||
* @param[in] addressRW 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] nbyte 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 transfer(uint8_t addressRW, void *buf,
|
||||
size_t nbyte, uint8_t option = I2C_STOP);
|
||||
|
||||
/**
|
||||
* Continue an I2C transfer.
|
||||
*
|
||||
* @param[in,out] buf Source or destination for transfer.
|
||||
* @param[in] nbyte 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 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 Software I2C master class
|
||||
*/
|
||||
class SoftI2cMaster : public I2cMasterBase {
|
||||
public:
|
||||
SoftI2cMaster() {}
|
||||
/**
|
||||
* 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(uint8_t sclPin, uint8_t 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 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 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) {fastPinMode(sclPin, !value);}
|
||||
//----------------------------------------------------------------------------
|
||||
inline __attribute__((always_inline))
|
||||
void sdaWrite(bool value) {fastPinMode(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 // SOFT_I2C_MASTER_H
|
||||
/** @} */
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
/* 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
|
||||
* @ingroup internals
|
||||
* @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.*/
|
||||
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;
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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:
|
||||
//----------------------------------------------------------------------------
|
||||
ALWAYS_INLINE bool MODE_CPHA(uint8_t mode) {return (mode & 1) != 0;}
|
||||
ALWAYS_INLINE bool MODE_CPOL(uint8_t mode) {return (mode & 2) != 0;}
|
||||
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));
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
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));
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
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
|
||||
/** @} */
|
||||
@@ -0,0 +1,126 @@
|
||||
#ifndef ADS7818_h
|
||||
#define ADS7871_h
|
||||
//------------------------------------------------------------------------------
|
||||
/** nop to tune soft SPI timing */
|
||||
#define nop asm volatile ("nop\n\t")
|
||||
//------------------------------------------------------------------------------
|
||||
/** doxygen can't handle __attribute__ very well */
|
||||
#define STATIC_ALWAYS_INLINE static inline __attribute__((always_inline))
|
||||
//------------------------------------------------------------------------------
|
||||
template<uint8_t ClkPin, uint8_t ConvPin, uint8_t DataPin>
|
||||
/** ADS7818 class */
|
||||
class ADS7818 {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/** Set pin modes and initial levels. */
|
||||
void begin() {
|
||||
fastPinMode(ClkPin, 1);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
fastPinMode(ConvPin, 1);
|
||||
fastDigitalWrite(ConvPin, 1);
|
||||
fastPinMode(DataPin, 0);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** Read ADS7818 12-bit ADC
|
||||
* \return 16-bit value read.
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
uint16_t read() {
|
||||
uint16_t v = 0;
|
||||
fastDigitalWrite(ConvPin, 0);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
fastDigitalWrite(ConvPin, 1);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
|
||||
readBitFast16(v, 11);
|
||||
readBitFast16(v, 10);
|
||||
readBitFast16(v, 9);
|
||||
readBitFast16(v, 8);
|
||||
readBitFast16(v, 7);
|
||||
readBitFast16(v, 6);
|
||||
readBitFast16(v, 5);
|
||||
readBitFast16(v, 4);
|
||||
readBitFast16(v, 3);
|
||||
readBitFast16(v, 2);
|
||||
readBitFast16(v, 1);
|
||||
readBitFast16(v, 0);
|
||||
return v;
|
||||
}
|
||||
private:
|
||||
//----------------------------------------------------------------------------
|
||||
inline __attribute__((always_inline))
|
||||
void readBitFast16(uint16_t &v, uint8_t b) {
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
if (fastDigitalRead(DataPin)) v |= (1 << b);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
};
|
||||
#if 0
|
||||
/** initialize I/O ports */
|
||||
STATIC_ALWAYS_INLINE void adcBegin() {
|
||||
fastPinMode(ADC_CLK_PIN, 1);
|
||||
fastDigitalWrite(ADC_CLK_PIN, 0);
|
||||
fastPinMode(ADC_CONV_PIN, 1);
|
||||
fastDigitalWrite(ADC_CONV_PIN, 1);
|
||||
fastPinMode(ADC_DATA_PIN, 0);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** clock for don't care bits
|
||||
*\param[in] delayBefore if true, delay before raising clock*/
|
||||
STATIC_ALWAYS_INLINE void fastDummy(bool first) {
|
||||
if (!first) nop;
|
||||
fastDigitalWrite(ADC_CLK_PIN, 1);
|
||||
nop;
|
||||
fastDigitalWrite(ADC_CLK_PIN, 0);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** read next bit fast as possible
|
||||
* \param[in] v word to receive bit
|
||||
* \param[in] b bit number to be set. v |= (1 << b) if next bit is high.
|
||||
*/
|
||||
STATIC_ALWAYS_INLINE void readBitFast16(uint16_t &v, uint8_t b) {
|
||||
fastDigitalWrite(ADC_CLK_PIN, 1);
|
||||
if (fastDigitalRead(ADC_DATA_PIN)) v |= (1 << b);
|
||||
fastDigitalWrite(ADC_CLK_PIN, 0);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Read AD7680 16-bit ADC in less than 8 microseconds
|
||||
* cs is chip select pin
|
||||
*/
|
||||
STATIC_ALWAYS_INLINE uint16_t adcRead(bool centered = false) {
|
||||
uint16_t v = 0;
|
||||
fastDigitalWrite(ADC_CONV_PIN, 0);
|
||||
fastDigitalWrite(ADC_CLK_PIN, 1);
|
||||
fastDigitalWrite(ADC_CLK_PIN, 0);
|
||||
fastDigitalWrite(ADC_CONV_PIN, 1);
|
||||
fastDigitalWrite(ADC_CLK_PIN, 1);
|
||||
fastDigitalWrite(ADC_CLK_PIN, 0);
|
||||
// uint16_t v = 0;
|
||||
|
||||
// fastDummy(1);
|
||||
// fastDummy(0);
|
||||
// fastDummy(0);
|
||||
// fastDummy(0);
|
||||
|
||||
// readBitFast16(v, 15);
|
||||
// readBitFast16(v, 14);
|
||||
// readBitFast16(v, 13);
|
||||
// readBitFast16(v, 12);
|
||||
readBitFast16(v, 11);
|
||||
readBitFast16(v, 10);
|
||||
readBitFast16(v, 9);
|
||||
readBitFast16(v, 8);
|
||||
readBitFast16(v, 7);
|
||||
readBitFast16(v, 6);
|
||||
readBitFast16(v, 5);
|
||||
readBitFast16(v, 4);
|
||||
readBitFast16(v, 3);
|
||||
readBitFast16(v, 2);
|
||||
readBitFast16(v, 1);
|
||||
readBitFast16(v, 0);
|
||||
return centered ? v ^ 0X8000 : v;
|
||||
}
|
||||
#endif // 0
|
||||
#endif // ADS7818_h
|
||||
@@ -0,0 +1,262 @@
|
||||
#ifndef MCP320X_h
|
||||
#define MCP320X_h
|
||||
#include <DigitalIO.h>
|
||||
//==============================================================================
|
||||
template<uint8_t CsPin, uint8_t ClkPin, uint8_t DoutPin, uint8_t UsecDelay = 0>
|
||||
/** MCP3201 class */
|
||||
class MCP3201 {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/** Set pin modes and initial levels. */
|
||||
void begin() {
|
||||
fastPinMode(ClkPin, 1);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
fastPinMode(CsPin, 1);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
fastPinMode(DoutPin, 0);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** Read MCP3201 12-bit ADC
|
||||
* \return 16-bit value read.
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
uint16_t read() {
|
||||
fastDigitalWrite(CsPin, 0);
|
||||
// Start sample.
|
||||
mcpAdcDummy(3);
|
||||
// Extra sample time.
|
||||
if (UsecDelay){
|
||||
delayMicroseconds(UsecDelay);
|
||||
}
|
||||
// End sample.
|
||||
mcpAdcDummy(3);
|
||||
// Null bit.
|
||||
mcpAdcDummy(3);
|
||||
uint16_t v = 0;
|
||||
readBit(v, 11);
|
||||
readBit(v, 10);
|
||||
readBit(v, 9);
|
||||
readBit(v, 8);
|
||||
readBit(v, 7);
|
||||
readBit(v, 6);
|
||||
readBit(v, 5);
|
||||
readBit(v, 4);
|
||||
readBit(v, 3);
|
||||
readBit(v, 2);
|
||||
readBit(v, 1);
|
||||
readBit(v, 0);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
return v;
|
||||
}
|
||||
private:
|
||||
//----------------------------------------------------------------------------
|
||||
/** delay n nops
|
||||
* \param[in] n nops to delay, must be a constant so compiler optimizes if().
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
void delayCycles(uint8_t n) {
|
||||
if (n & 1) asm volatile("nop\n\t");
|
||||
if (n & 2) asm volatile("nop\n\t" "nop\n\t");
|
||||
if (n & 4) asm volatile("nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t");
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** clock for don't care bits
|
||||
*\param[in] delayBefore if true, delay before raising clock*/
|
||||
inline __attribute__((always_inline))
|
||||
void mcpAdcDummy(uint8_t delayBefore) {
|
||||
delayCycles(delayBefore);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
delayCycles(3);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
inline __attribute__((always_inline))
|
||||
void readBit(uint16_t &v, uint8_t b, uint8_t delayRead = 2) {
|
||||
delayCycles(3);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
delayCycles(delayRead);
|
||||
if (fastDigitalRead(DoutPin)) v |= (1 << b);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
};
|
||||
//==============================================================================
|
||||
template<uint8_t CsPin, uint8_t ClkPin,
|
||||
uint8_t DoutPin, uint8_t DinPin, uint8_t UsecDelay = 0>
|
||||
/** MCP3202 class */
|
||||
class MCP3202 {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/** Set pin modes and initial levels. */
|
||||
void begin() {
|
||||
fastPinMode(ClkPin, 1);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
fastPinMode(CsPin, 1);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
fastPinMode(DoutPin, 0);
|
||||
fastPinMode(DinPin, 1);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** Read MCP3202 12-bit ADC
|
||||
* \return 16-bit value read.
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
uint16_t read(uint8_t config) {
|
||||
uint16_t v = 0;
|
||||
fastDigitalWrite(CsPin, 0);
|
||||
// Start bit.
|
||||
writeBit(true);
|
||||
// Mode bit.
|
||||
writeBit(config & 2);
|
||||
// Channel Selection and start sample.
|
||||
writeBit(config & 1);
|
||||
// Extra sample time.
|
||||
if (UsecDelay){
|
||||
delayMicroseconds(UsecDelay);
|
||||
}
|
||||
// MSB first format and end sample.
|
||||
writeBit(true);
|
||||
// Null bit.
|
||||
writeBit(true);
|
||||
// Data bits.
|
||||
readBit(v, 11);
|
||||
readBit(v, 10);
|
||||
readBit(v, 9);
|
||||
readBit(v, 8);
|
||||
readBit(v, 7);
|
||||
readBit(v, 6);
|
||||
readBit(v, 5);
|
||||
readBit(v, 4);
|
||||
readBit(v, 3);
|
||||
readBit(v, 2);
|
||||
readBit(v, 1);
|
||||
readBit(v, 0);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
return v;
|
||||
}
|
||||
private:
|
||||
//----------------------------------------------------------------------------
|
||||
/** delay n nops
|
||||
* \param[in] n nops to delay, must be a constant so compiler optimizes if().
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
void delayCycles(uint8_t n) {
|
||||
if (n & 1) asm volatile("nop\n\t");
|
||||
if (n & 2) asm volatile("nop\n\t" "nop\n\t");
|
||||
if (n & 4) asm volatile("nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t");
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** clock for don't care bits
|
||||
*\param[in] delayBefore if true, delay before raising clock*/
|
||||
inline __attribute__((always_inline))
|
||||
void writeBit(bool value) {
|
||||
fastDigitalWrite(DinPin, value);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
delayCycles(2);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
inline __attribute__((always_inline))
|
||||
void readBit(uint16_t &v, uint8_t b, uint8_t delayRead = 1) {
|
||||
delayCycles(2);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
delayCycles(delayRead);
|
||||
if (fastDigitalRead(DoutPin)) v |= (1 << b);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
};
|
||||
//==============================================================================
|
||||
template<uint8_t CsPin, uint8_t ClkPin,
|
||||
uint8_t DoutPin, uint8_t DinPin, uint8_t UsecDelay = 0>
|
||||
/** MCP3204 class */
|
||||
class MCP3204 {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/** Set pin modes and initial levels. */
|
||||
void begin() {
|
||||
fastPinMode(ClkPin, 1);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
fastPinMode(CsPin, 1);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
fastPinMode(DoutPin, 0);
|
||||
fastPinMode(DinPin, 1);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** Read MCP3204 12-bit ADC
|
||||
* \return 16-bit value read.
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
uint16_t read(uint8_t config) {
|
||||
uint16_t v = 0;
|
||||
fastDigitalWrite(CsPin, 0);
|
||||
// Start bit.
|
||||
writeBit(true);
|
||||
// Mode bit.
|
||||
writeBit(config & 8);
|
||||
// D2 channel bit.
|
||||
writeBit(config & 4);
|
||||
// D1 channel bit.
|
||||
writeBit(config & 2);
|
||||
// D0 channel bit and start sample.
|
||||
writeBit(config & 1);
|
||||
// Extra sample time.
|
||||
if (UsecDelay){
|
||||
delayMicroseconds(UsecDelay);
|
||||
}
|
||||
// End sample cycle.
|
||||
writeBit(true);
|
||||
// Null bit.
|
||||
writeBit(true);
|
||||
// Data bits.
|
||||
readBit(v, 11);
|
||||
readBit(v, 10);
|
||||
readBit(v, 9);
|
||||
readBit(v, 8);
|
||||
readBit(v, 7);
|
||||
readBit(v, 6);
|
||||
readBit(v, 5);
|
||||
readBit(v, 4);
|
||||
readBit(v, 3);
|
||||
readBit(v, 2);
|
||||
readBit(v, 1);
|
||||
readBit(v, 0);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
return v;
|
||||
}
|
||||
private:
|
||||
//----------------------------------------------------------------------------
|
||||
/** delay n nops
|
||||
* \param[in] n nops to delay, must be a constant so compiler optimizes if().
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
void delayCycles(uint8_t n) {
|
||||
if (n & 1) asm volatile("nop\n\t");
|
||||
if (n & 2) asm volatile("nop\n\t" "nop\n\t");
|
||||
if (n & 4) asm volatile("nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t");
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** clock for don't care bits
|
||||
*\param[in] delayBefore if true, delay before raising clock*/
|
||||
inline __attribute__((always_inline))
|
||||
void writeBit(bool value) {
|
||||
fastDigitalWrite(DinPin, value);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
delayCycles(2);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
inline __attribute__((always_inline))
|
||||
void readBit(uint16_t &v, uint8_t b, uint8_t delayRead = 1) {
|
||||
delayCycles(2);
|
||||
fastDigitalWrite(ClkPin, 1);
|
||||
delayCycles(delayRead);
|
||||
if (fastDigitalRead(DoutPin)) v |= (1 << b);
|
||||
fastDigitalWrite(ClkPin, 0);
|
||||
}
|
||||
};
|
||||
//==============================================================================
|
||||
// MCP3204 and MCP3208 use the same code.
|
||||
template<uint8_t CsPin, uint8_t ClkPin,
|
||||
uint8_t DoutPin, uint8_t DinPin, uint8_t UsecDelay = 0>
|
||||
class MCP3208 : public MCP3204<CsPin, ClkPin, DoutPin, DinPin, UsecDelay> {
|
||||
};
|
||||
#endif // MCP320X_h
|
||||
@@ -0,0 +1,127 @@
|
||||
#ifndef MCP355X_h
|
||||
#define MCP355X_h
|
||||
#include <DigitalIO.h>
|
||||
// Overflow values.
|
||||
const int32_t MCP355X_OVH = 2097152L;
|
||||
const int32_t MCP355X_OVL = -2097153L;
|
||||
const int32_t MCP355X_ERR = 0X80000000;
|
||||
const uint8_t MCP355X_TO_MS = 100;
|
||||
//==============================================================================
|
||||
template<uint8_t CsPin, uint8_t SckPin, uint8_t SdoPin>
|
||||
/** MCP355X class */
|
||||
class MCP355X {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/** Set pin modes and initial levels. */
|
||||
void begin(bool singleMode = true) {
|
||||
m_singleMode = singleMode;
|
||||
fastPinMode(SdoPin, 0);
|
||||
fastPinMode(SckPin, 1);
|
||||
fastDigitalWrite(SckPin, 1);
|
||||
fastPinMode(CsPin, 1);
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
if (!m_singleMode) {
|
||||
// Wait for conversion to complete.
|
||||
delay(100);
|
||||
// Start continuous conversion mode.
|
||||
fastDigitalWrite(CsPin, 0);
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/** Read MCP355X 22-bit ADC
|
||||
* \return 22-bit value.
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
int32_t read() {
|
||||
uint8_t t = 0;
|
||||
uint8_t v3 = 0;
|
||||
if (m_singleMode) {
|
||||
// Start conversion.
|
||||
fastDigitalWrite(CsPin, 0);
|
||||
// Delay at least 10 usec to avoid RDY glitch on exit from Shutdown.
|
||||
delay(1);
|
||||
// Toggle CsPin to indicate single conversion mode.
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
// Wait for conversion to complete.
|
||||
while (1) {
|
||||
delay(1);
|
||||
fastDigitalWrite(CsPin, 0);
|
||||
// Delay while RDY settles.
|
||||
delayCycles(4);
|
||||
if (!fastDigitalRead(SdoPin)) break;
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
if (t++ > MCP355X_TO_MS) return MCP355X_ERR;
|
||||
}
|
||||
} else {
|
||||
while (1) {
|
||||
if (!fastDigitalRead(SdoPin)) break;
|
||||
delay(1);
|
||||
if (t++ > MCP355X_TO_MS) return MCP355X_ERR;
|
||||
}
|
||||
}
|
||||
uint8_t v2 = readByte();
|
||||
uint8_t v1 = readByte();
|
||||
uint8_t v0 = readByte();
|
||||
|
||||
// The 25th falling edge of SCK changes SDO/RDY from Data mode to RDY mode.
|
||||
uint8_t dummy = 0;
|
||||
readBit(dummy, 0);
|
||||
|
||||
if (m_singleMode) {
|
||||
fastDigitalWrite(CsPin, 1);
|
||||
}
|
||||
if ((v2 & 0XE0) == 0X20) {
|
||||
// Negative in range so extend sign bit.
|
||||
v2 |= 0XC0;
|
||||
v3 = 0XFF;
|
||||
} else if (v2 & 0X40) {
|
||||
// Overflow high. Cause value to be >= MCP355X_OVH.
|
||||
if (v2 & 0X20) v2 &= 0X3F;
|
||||
} else if (v2 & 0X80) {
|
||||
// Overflow low. Cause value to be <= MCP355X_OVH.
|
||||
if ((v2 & 0X20) == 0) v2 |= 0X40;
|
||||
v3 = 0XFF;
|
||||
}
|
||||
uint16_t v_high = (v3 << 8) | v2;
|
||||
uint16_t v_low = (v1 << 8) | v0;
|
||||
return ((uint32_t)v_high << 16) | v_low;
|
||||
}
|
||||
private:
|
||||
//----------------------------------------------------------------------------
|
||||
/** delay n nops
|
||||
* \param[in] n nops to delay, must be a constant so compiler optimizes if().
|
||||
*/
|
||||
inline __attribute__((always_inline))
|
||||
void delayCycles(uint8_t n) {
|
||||
if (n & 1) asm volatile("nop\n\t");
|
||||
if (n & 2) asm volatile("nop\n\t" "nop\n\t");
|
||||
if (n & 4) asm volatile("nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t");
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
// Default delay yields about 2 MHz clock.
|
||||
inline __attribute__((always_inline))
|
||||
void readBit(uint8_t &v, uint8_t b, uint8_t delayRead = 0) {
|
||||
fastDigitalWrite(SckPin, 0);
|
||||
delayCycles(2 + delayRead);
|
||||
fastDigitalWrite(SckPin, 1);
|
||||
if (fastDigitalRead(SdoPin)) v |= (1 << b);
|
||||
delayCycles(delayRead);
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
inline __attribute__((always_inline))
|
||||
uint8_t readByte() {
|
||||
uint8_t v = 0;
|
||||
readBit(v, 7);
|
||||
readBit(v, 6);
|
||||
readBit(v, 5);
|
||||
readBit(v, 4);
|
||||
readBit(v, 3);
|
||||
readBit(v, 2);
|
||||
readBit(v, 1);
|
||||
readBit(v, 0);
|
||||
return v;
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
bool m_singleMode;
|
||||
};
|
||||
#endif // MCP355X_h
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <DigitalIO.h>
|
||||
// Create object for pin 13 in output mode and demo toggle().
|
||||
DigitalPin<13> pin13(OUTPUT);
|
||||
|
||||
void setup() {}
|
||||
|
||||
void loop() {
|
||||
// toggle is a two byte instruction that executes
|
||||
// in two cycles or 125 ns on a 16 MHz CPU
|
||||
pin13.toggle();
|
||||
delay(250);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Demo of config() and fast toggle() function.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Class with compile time pin number.
|
||||
DigitalPin<13> pin13;
|
||||
|
||||
void setup() {
|
||||
// Set mode to OUTPUT and level LOW.
|
||||
pin13.config(OUTPUT, LOW);
|
||||
}
|
||||
void loop() {
|
||||
// toggle is a two byte instruction that executes
|
||||
// in two cycles or 125 ns on a 16 MHz CPU
|
||||
pin13.toggle();
|
||||
delay(100);
|
||||
pin13.toggle();
|
||||
delay(400);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Read pin 12 and write value to pin 13.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
DigitalPin<12> pin12(INPUT);
|
||||
DigitalPin<13> pin13(OUTPUT);
|
||||
|
||||
void setup() {}
|
||||
|
||||
void loop() {
|
||||
pin13 = pin12;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Scope test for fast shiftOut function.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Create clockPin in output mode with inital level LOW.
|
||||
DigitalPin<12> clockPin(OUTPUT, LOW);
|
||||
|
||||
// Create dataPin in output mode with inital level HIGH.
|
||||
DigitalPin<13> dataPin(OUTPUT, HIGH);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Time to send one bit is ten cycles or 625 ns for 16 MHz CPU.
|
||||
inline __attribute__((always_inline))
|
||||
void sendBit(uint8_t bit, uint8_t data) {
|
||||
dataPin = data & (1 << bit);
|
||||
clockPin = 1;
|
||||
// may want a nop here - clock pulse is 125 ns wide
|
||||
clockPin = 0;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// Time to send one byte is 5 usec.
|
||||
void shiftOut(uint8_t bits) {
|
||||
sendBit(7, bits);
|
||||
sendBit(6, bits);
|
||||
sendBit(5, bits);
|
||||
sendBit(4, bits);
|
||||
sendBit(3, bits);
|
||||
sendBit(2, bits);
|
||||
sendBit(1, bits);
|
||||
sendBit(0, bits);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void setup() {
|
||||
// Not used.
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void loop() {
|
||||
shiftOut(0X55);
|
||||
delay(2);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Use begin to assign pin numbers.
|
||||
// Read pin 12 and write the value to pin 13.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Declare the PinIO instances.
|
||||
PinIO readPin;
|
||||
PinIO writePin;
|
||||
//------------------------------------------------------------------------------
|
||||
void setup() {
|
||||
// Assign pin 12 to readPin.
|
||||
readPin.begin(12);
|
||||
|
||||
// input mode with pull-ups disabled
|
||||
readPin.config(INPUT, LOW);
|
||||
|
||||
// Assign pin 13 to writePin and set mode to output.
|
||||
writePin.begin(13);
|
||||
writePin.mode(OUTPUT);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void loop() {
|
||||
// Copy the value read from readPin to writePin.
|
||||
writePin.write(readPin.read());
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Test the config() and toggle() functions.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Set runtime pin number.
|
||||
PinIO pin13(13);
|
||||
|
||||
void setup() {
|
||||
// set mode to OUTPUT and level LOW
|
||||
pin13.config(OUTPUT, LOW);
|
||||
}
|
||||
void loop() {
|
||||
pin13.toggle();
|
||||
delay(100);
|
||||
pin13.toggle();
|
||||
delay(400);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Read pin 12 and write the value to pin 13.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
PinIO readPin(12);
|
||||
PinIO writePin(13);
|
||||
|
||||
void setup() {
|
||||
// Set input mode and disable pull-up.
|
||||
readPin.config(INPUT, LOW);
|
||||
|
||||
// set output mode
|
||||
writePin.mode(OUTPUT);
|
||||
}
|
||||
void loop() {
|
||||
writePin.write(readPin.read());
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/* Scan I2C bus for devices.
|
||||
*
|
||||
* For I2C write addresses: the scanner sends a start,
|
||||
* the write address, looks for an ACK, then sends a stop.
|
||||
*
|
||||
* For I2C read addresses: the scanner sends a start, the
|
||||
* read address, looks for an ACK. If there is an ACK, it
|
||||
* reads a byte and NACKs it. Finally it sends a stop.
|
||||
*
|
||||
* When the I2C chip responds to the read address, it
|
||||
* outputs data and will miss a stop condition sent
|
||||
* immediately after the read address (bus contention).
|
||||
*
|
||||
* If the I2C chip misses the stop condition, the
|
||||
* address scanner will see ghost addresses until the
|
||||
* read ends randomly. By reading a byte after any read
|
||||
* address that ACKs, there is a chance to NACK the read
|
||||
* and properly end the I2C transaction.
|
||||
*/
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Set pin numbers for your configuration.
|
||||
const uint8_t SDA_PIN = A4;
|
||||
const uint8_t SCL_PIN = A5;
|
||||
|
||||
SoftI2cMaster i2c(SCL_PIN, SDA_PIN);
|
||||
//FastI2cMaster<SCL_PIN, SDA_PIN> i2c;
|
||||
//------------------------------------------------------------------------------
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
if (!digitalRead(SDA_PIN) && !digitalRead(SCL_PIN)) {
|
||||
Serial.println("External pull-up resistors appear to be missing.");
|
||||
Serial.println("Many false responses may be detected.");
|
||||
Serial.println("Type any character to continue.");
|
||||
|
||||
while (!Serial.available());
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
uint8_t add = 0;
|
||||
bool found = false;
|
||||
do {
|
||||
bool wr = i2c.transfer(add | I2C_WRITE, 0, 0);
|
||||
bool rd = i2c.transfer(add | I2C_READ, 0, 0, I2C_CONTINUE);
|
||||
if (rd) {
|
||||
uint8_t dummy;
|
||||
// Must read byte, send NACK, and issue STOP.
|
||||
i2c.transferContinue(&dummy, 1);
|
||||
}
|
||||
if (rd || wr) {
|
||||
found = true;
|
||||
Serial.print("Device at address: 0X");
|
||||
Serial.print(add, HEX);
|
||||
Serial.print(" responds to ");
|
||||
if (rd) Serial.print("Read");
|
||||
if (rd && wr) Serial.print(" and ");
|
||||
if (wr) Serial.print("Write");
|
||||
Serial.println('.');
|
||||
}
|
||||
add += 2;
|
||||
}
|
||||
while (add);
|
||||
|
||||
if (!found) {
|
||||
Serial.println("No devices found.");
|
||||
}
|
||||
Serial.println("Done");
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void loop() {
|
||||
// Not used.
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
// Utility sketch to explore DS1307 and demonstrate software I2C Master.
|
||||
//
|
||||
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Set pin numbers for your configuration.
|
||||
const uint8_t SDA_PIN = A4;
|
||||
const uint8_t SCL_PIN = A5;
|
||||
|
||||
//SoftI2cMaster rtc(SCL_PIN, SDA_PIN);
|
||||
FastI2cMaster<SCL_PIN, SDA_PIN> rtc;
|
||||
|
||||
// i2c 8-bit address for DS1307. low bit is read/write
|
||||
#define DS1307ADDR 0XD0
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
* Read 'count' bytes from the DS1307 starting at 'address'.
|
||||
*/
|
||||
uint8_t readDS1307(uint8_t address, uint8_t *buf, uint8_t count) {
|
||||
|
||||
// Send address of data.
|
||||
if (!rtc.transfer(DS1307ADDR | I2C_WRITE, &address, 1)) return false;
|
||||
|
||||
// Read data.
|
||||
return rtc.transfer(DS1307ADDR | I2C_READ, buf, count);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
* write 'count' bytes to DS1307 starting at 'address'.
|
||||
*/
|
||||
uint8_t writeDS1307(uint8_t address, uint8_t *buf, uint8_t count) {
|
||||
|
||||
// Write address and continue transfer for write of data.
|
||||
if (!rtc.transfer(DS1307ADDR | I2C_WRITE, &address, 1, I2C_CONTINUE)) {
|
||||
return false;
|
||||
}
|
||||
// Write data.
|
||||
return rtc.transferContinue(buf, count);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Store and print a string in flash memory.*/
|
||||
#define PgmPrint(x) Serial.print(F(x))
|
||||
/** Store and print a string in flash memory followed by a CR/LF.*/
|
||||
#define PgmPrintln(x) Serial.println(F(x))
|
||||
//-------------------------------------------------------------------------------
|
||||
// Day of week U.S. convention.
|
||||
char *Ddd[] = {"Bad", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
||||
//------------------------------------------------------------------------------
|
||||
void hexPrint(uint8_t v) {
|
||||
Serial.print(v >> 4, HEX);
|
||||
Serial.print(v & 0XF, HEX);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void hexPrintln(uint8_t v) {
|
||||
hexPrint(v);
|
||||
Serial.println();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// read hex input
|
||||
uint8_t hexRead(uint16_t* v) {
|
||||
bool valid = false;
|
||||
uint16_t n = 0;
|
||||
while (!Serial.available());
|
||||
while (Serial.available()) {
|
||||
uint8_t c = Serial.read();
|
||||
// Exit if end-of-line.
|
||||
if (c == '\n' || c == '\r') {
|
||||
delay(10);
|
||||
// If CR/LF.
|
||||
Serial.read();
|
||||
break;
|
||||
}
|
||||
n <<= 4;
|
||||
if ('a' <= c && c <= 'f') {
|
||||
n += c - ('a' - 10);
|
||||
}
|
||||
else if ('A' <= c && c <= 'F') {
|
||||
n += c - ('A' - 10);
|
||||
}
|
||||
else if ('0' <= c && c <= '9') {
|
||||
n += c - '0';
|
||||
}
|
||||
else {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
delay(10);
|
||||
valid = true;
|
||||
}
|
||||
if (!valid) {
|
||||
PgmPrintln("Invalid entry");
|
||||
return false;
|
||||
}
|
||||
*v = n;
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t bcdRead(uint8_t min, uint8_t max, uint8_t* n) {
|
||||
uint16_t v;
|
||||
if (!hexRead(&v)) return false;
|
||||
uint8_t d = 10 * (v >> 4) + (v & 0XF);
|
||||
if ((v >> 4) > 9 || (v & 0XF) > 9 || d < min || d > max) {
|
||||
PgmPrintln("Invalid");
|
||||
return false;
|
||||
}
|
||||
*n = v;
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void displayTime(void) {
|
||||
uint8_t r[8];
|
||||
if (!readDS1307(0, r, 8)) {
|
||||
PgmPrintln("Read Failed for display time");
|
||||
return;
|
||||
}
|
||||
PgmPrint("The current time is 20");
|
||||
// year
|
||||
hexPrint(r[6]);
|
||||
Serial.write('-');
|
||||
|
||||
// month
|
||||
hexPrint(r[5]);
|
||||
Serial.write('-');
|
||||
|
||||
// day
|
||||
hexPrint(r[4]);
|
||||
Serial.write(' ');
|
||||
|
||||
Serial.print(Ddd[r[3] < 8 ? r[3] : 0]);
|
||||
Serial.write(' ');
|
||||
|
||||
// hour
|
||||
hexPrint(r[2]);
|
||||
Serial.write(':');
|
||||
|
||||
// minute
|
||||
hexPrint(r[1]);
|
||||
Serial.write(':');
|
||||
|
||||
// second
|
||||
hexPrintln(r[0]);
|
||||
|
||||
// Control register.
|
||||
PgmPrint("Control Register: ");
|
||||
hexPrintln(r[7]);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// dump registers and 56 bytes of RAM
|
||||
void dumpAll(void) {
|
||||
uint8_t buf[8];
|
||||
for (uint8_t a = 0; a < 64; a += 8) {
|
||||
hexPrint(a);
|
||||
Serial.write(' ');
|
||||
if (!readDS1307(a, buf, 8)) {
|
||||
PgmPrint("read failed for dumpAll");
|
||||
return;
|
||||
}
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
Serial.write(' ');
|
||||
hexPrint(buf[i]);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void fillNvRam(void) {
|
||||
uint8_t buf[8];
|
||||
PgmPrint("Enter HEX value for all NV RAM locations (00-FF): ");
|
||||
uint16_t v;
|
||||
if (!hexRead(&v)) return;
|
||||
hexPrint(v);
|
||||
for (uint8_t a = 8; a < 64; a ++) {
|
||||
if (!writeDS1307(a, (uint8_t *)&v, 1)) {
|
||||
PgmPrintln("write failed for fillNvRam");
|
||||
}
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// set control register
|
||||
/*
|
||||
The DS1307 control register is used to control the operation of the SQW/OUT pin.
|
||||
+-----------------------------------------------+
|
||||
|BIT 7|BIT 6|BIT 5|BIT 4|BIT 3|BIT 2|BIT 1|BIT 0|
|
||||
+-----------------------------------------------+
|
||||
|OUT | 0 | 0 |SQWE | 0 | 0 | RS1 | RS0 |
|
||||
+-----------------------------------------------+
|
||||
|
||||
OUT (Output control): This bit controls the output level of the SQW/OUT pin
|
||||
when the square wave output is disabled. If SQWE = 0, the logic level on the
|
||||
SQW/OUT pin is 1 if OUT = 1 and is 0 if OUT = 0.
|
||||
|
||||
SQWE (Square Wave Enable): This bit, when set to a logic 1, will enable the
|
||||
oscillator output. The frequency of the square wave output depends upon the
|
||||
value of the RS0 and RS1 bits. With the square wave output set to 1Hz, the
|
||||
clock registers update on the falling edge of the square wave.
|
||||
|
||||
Square wave Output Frequency for SQWE = 1.
|
||||
RS1 RS0 FREQUENCY
|
||||
0 0 1Hz
|
||||
0 1 4.096kHz
|
||||
1 0 8.192kHz
|
||||
1 1 32.768kHz
|
||||
*/
|
||||
void setControl(void) {
|
||||
PgmPrintln("SQW/OUT pin: ");
|
||||
PgmPrintln("(00) Low");
|
||||
PgmPrintln("(10) 1Hz");
|
||||
PgmPrintln("(11) 4.096kHz");
|
||||
PgmPrintln("(12) 8.192kHz");
|
||||
PgmPrintln("(13) 32.768kHz");
|
||||
PgmPrintln("(80) High");
|
||||
PgmPrint("Enter control: ");
|
||||
uint16_t r;
|
||||
if (!hexRead(&r)) return;
|
||||
hexPrintln(r);
|
||||
if (!writeDS1307(7, (uint8_t *)&r, 1)) {
|
||||
PgmPrint("Write Failed for setControl");
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void setDate(void) {
|
||||
uint8_t r[4];
|
||||
PgmPrint("Enter year (00-99): ");
|
||||
if (!bcdRead(0, 99, &r[3])) return;
|
||||
hexPrintln(r[3]);
|
||||
PgmPrint("Enter month (01-12): ");
|
||||
if (!bcdRead(1, 12, &r[2])) return;
|
||||
hexPrintln(r[2]);
|
||||
PgmPrint("Enter day (01-31): ");
|
||||
if (!bcdRead(1, 31, &r[1])) return;
|
||||
hexPrintln(r[1]);
|
||||
PgmPrint("Enter day of week (01-07, Sun-Sat): ");
|
||||
if (!bcdRead(1, 7, &r[0])) return;
|
||||
hexPrintln(r[0]);
|
||||
|
||||
if (!writeDS1307(3, r, 4)) {
|
||||
PgmPrintln("Write failed for setDate");
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void setNvRam() {
|
||||
uint8_t u;
|
||||
uint16_t a, v;
|
||||
while (true) {
|
||||
PgmPrint("Enter zero to quit or Hex address (8-3F):");
|
||||
if (!hexRead(&a) || a == 0) return;
|
||||
if (a < 8 || a > 0X3F) {
|
||||
PgmPrintln("Invalid address");
|
||||
return;
|
||||
}
|
||||
hexPrintln(a);
|
||||
if (!readDS1307(a, &u, 1)) {
|
||||
PgmPrintln("Read failed");
|
||||
return;
|
||||
}
|
||||
PgmPrint("Current value: ");
|
||||
hexPrintln(u);
|
||||
PgmPrint("Enter new HEX value (00-FF): ");
|
||||
if (!hexRead(&v)) return;
|
||||
hexPrintln(v);
|
||||
if (!writeDS1307(a, (uint8_t *)&v, 1)) {
|
||||
PgmPrint("Write Failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void setTime(void) {
|
||||
uint8_t r[3];
|
||||
PgmPrint("Enter hours (00-23): ");
|
||||
if (!bcdRead(0, 23, &r[2])) return;
|
||||
hexPrintln(r[2]);
|
||||
PgmPrint("Enter minutes (00-59): ");
|
||||
if (!bcdRead(0, 59, &r[1])) return;
|
||||
hexPrintln(r[1]);
|
||||
PgmPrint("Enter seconds (00-59): ");
|
||||
if (!bcdRead(0, 59, &r[0])) return;
|
||||
hexPrintln(r[0]);
|
||||
|
||||
if (!writeDS1307(0, r, 3)) {
|
||||
PgmPrintln("write failed in setTime");
|
||||
return;
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void loop(void) {
|
||||
Serial.println();
|
||||
displayTime();
|
||||
while (Serial.read() >= 0) {}
|
||||
PgmPrintln("\nOptions are:");
|
||||
PgmPrintln("(0) Display date and time");
|
||||
PgmPrintln("(1) Set time");
|
||||
PgmPrintln("(2) Set date");
|
||||
PgmPrintln("(3) Set Control");
|
||||
PgmPrintln("(4) Dump all");
|
||||
PgmPrintln("(5) Fill NV RAM");
|
||||
PgmPrintln("(6) Set NV RAM value");
|
||||
PgmPrint("Enter option: ");
|
||||
|
||||
uint16_t n;
|
||||
if (!hexRead(&n)) return;
|
||||
Serial.println(n, DEC);
|
||||
if (n == 0) return;
|
||||
Serial.println();
|
||||
if (n == 1) {
|
||||
setTime();
|
||||
} else if (n == 2) {
|
||||
setDate();
|
||||
} else if (n == 3) {
|
||||
setControl();
|
||||
} else if (n == 4) {
|
||||
dumpAll();
|
||||
} else if (n == 5) {
|
||||
fillNvRam();
|
||||
} else if (n == 6) {
|
||||
setNvRam();
|
||||
} else {
|
||||
PgmPrintln("Invalid option");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Scope test for write timing with Arduino digitaWrite().
|
||||
const uint8_t PIN13 = 13;
|
||||
|
||||
void setup() {
|
||||
// Set mode to OUTPUT.
|
||||
pinMode(PIN13, OUTPUT);
|
||||
}
|
||||
void loop() {
|
||||
digitalWrite(PIN13, HIGH);
|
||||
digitalWrite(PIN13, LOW);
|
||||
digitalWrite(PIN13, HIGH);
|
||||
digitalWrite(PIN13, LOW);
|
||||
delay(1);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Scope test for write timing of DigitalPin functions.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Class with compile time pin number.
|
||||
DigitalPin<13> pin13;
|
||||
|
||||
void setup() {
|
||||
// set mode to OUTPUT
|
||||
pin13.mode(OUTPUT);
|
||||
}
|
||||
void loop() {
|
||||
pin13.high();
|
||||
pin13.low();
|
||||
pin13.write(1);
|
||||
pin13.write(0);
|
||||
pin13.toggle();
|
||||
pin13.toggle();
|
||||
delay(1);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Test fastDigital function timing with a scope.
|
||||
#include <DigitalIO.h>
|
||||
const uint8_t PIN = 13;
|
||||
void setup() {
|
||||
fastPinMode(PIN, OUTPUT);
|
||||
}
|
||||
void loop() {
|
||||
fastDigitalWrite(PIN, HIGH);
|
||||
fastDigitalWrite(PIN, LOW);
|
||||
fastDigitalToggle(PIN);
|
||||
fastDigitalToggle(PIN);
|
||||
delay(1);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Scope test for PinIO class write timing.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
// Class with runtime pin numbers.
|
||||
PinIO pin(13);
|
||||
|
||||
void setup() {
|
||||
// Set mode to OUTPUT.
|
||||
pin.mode(OUTPUT);
|
||||
}
|
||||
void loop() {
|
||||
pin.high();
|
||||
pin.low();
|
||||
pin.write(1);
|
||||
pin.write(0);
|
||||
pin.toggle();
|
||||
pin.toggle();
|
||||
delay(1);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Scope test for development - assumes 328 processor.
|
||||
#include <DigitalIO.h>
|
||||
|
||||
const uint8_t SOFT_SPI_MISO_PIN = 7;
|
||||
const uint8_t SOFT_SPI_MOSI_PIN = 8;
|
||||
const uint8_t SOFT_SPI_SCK_PIN = 9;
|
||||
const uint8_t SPI_MODE = 0;
|
||||
|
||||
SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi;
|
||||
|
||||
int test;
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
spi.begin();
|
||||
while(1) {
|
||||
Serial.println("Enter:");
|
||||
Serial.println("R - Receive");
|
||||
Serial.println("S - Send");
|
||||
Serial.println("T - Transfer");
|
||||
while ((test = Serial.read()) <= 0) {}
|
||||
test = toupper(test);
|
||||
if (strchr("RST", test)) break;
|
||||
Serial.println("Invalid entry");
|
||||
};
|
||||
Serial.print("Starting test ");
|
||||
Serial.println((char)test);
|
||||
}
|
||||
void loop() {
|
||||
if (test == 'S') spi.send(0X55);
|
||||
if (test == 'R') {
|
||||
Serial.println(spi.receive(), HEX);
|
||||
}
|
||||
if (test == 'T') {
|
||||
Serial.println(spi.transfer(0XAA), HEX);
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
Reference in New Issue
Block a user