Repo restyled using astyle (#683)

The rules are in .tools/astyle/config/style.cfg
and can also be reviewed at
https://www.mysensors.org/view/260#coding-guidelines-core-library
This commit is contained in:
Patrick Fallberg
2016-12-07 23:44:29 +01:00
committed by GitHub
parent d4c4ab82ea
commit 8e1ef13804
167 changed files with 16103 additions and 15168 deletions
+444 -421
View File
@@ -39,10 +39,10 @@
* @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 */
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__)\
@@ -50,102 +50,102 @@ struct pin_map_t {
||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
{&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
{&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__)\
@@ -160,110 +160,110 @@ static const pin_map_t pinMap[] = {
#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
{&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
{&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
{&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
@@ -273,66 +273,66 @@ static const pin_map_t pinMap[] = {
#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
{&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
{&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
//------------------------------------------------------------------------------
@@ -340,52 +340,52 @@ static const pin_map_t pinMap[] = {
|| 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
{&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
@@ -397,15 +397,16 @@ 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")));
__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();
}
static ALWAYS_INLINE void badPinCheck(uint8_t pin)
{
if (!__builtin_constant_p(pin) || pin >= digitalPinCount) {
badPinNumber();
}
}
//------------------------------------------------------------------------------
/** fast write helper
@@ -413,29 +414,31 @@ static ALWAYS_INLINE void badPinCheck(uint8_t pin) {
* @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;
}
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;
static ALWAYS_INLINE bool fastDigitalRead(uint8_t pin)
{
badPinCheck(pin);
return (*pinMap[pin].pin >> pinMap[pin].bit) & 1;
}
//------------------------------------------------------------------------------
/** toggle a pin
@@ -444,24 +447,26 @@ static ALWAYS_INLINE bool fastDigitalRead(uint8_t pin) {
* 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;
}
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);
static ALWAYS_INLINE void fastDigitalWrite(uint8_t pin, bool level)
{
badPinCheck(pin);
fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, level);
}
//------------------------------------------------------------------------------
/** set pin mode
@@ -470,9 +475,10 @@ static ALWAYS_INLINE void fastDigitalWrite(uint8_t pin, bool level) {
*
* 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);
static ALWAYS_INLINE void fastPinMode(uint8_t pin, bool mode)
{
badPinCheck(pin);
fastBitWriteSafe(pinMap[pin].ddr, pinMap[pin].bit, mode);
}
//------------------------------------------------------------------------------
/** set pin configuration
@@ -481,9 +487,10 @@ static ALWAYS_INLINE void fastPinMode(uint8_t pin, bool 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);
static ALWAYS_INLINE void fastPinConfig(uint8_t pin, bool mode, bool level)
{
fastPinMode(pin, mode);
fastDigitalWrite(pin, level);
}
//==============================================================================
/**
@@ -491,96 +498,112 @@ static ALWAYS_INLINE void fastPinConfig(uint8_t pin, bool mode, bool level) {
* @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);
}
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
/** @} */
+20 -15
View File
@@ -32,22 +32,26 @@
/** Constructor
* @param[in] pin Pin assigned to this object.
*/
PinIO::PinIO(uint8_t pin) {
begin(pin);
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;
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
@@ -59,10 +63,11 @@ bool PinIO::begin(uint8_t pin) {
* 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);
}
void PinIO::config(bool mode, bool data)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
modeI(mode);
writeI(data);
}
}
/** @} */
+147 -112
View File
@@ -37,118 +37,153 @@
* @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_;
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
/** @} */
+129 -119
View File
@@ -49,16 +49,17 @@
* @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);
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);
}
//------------------------------------------------------------------------------
/**
@@ -76,30 +77,31 @@ bool I2cMasterBase::transfer(uint8_t addrRW,
* .
* @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;
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
@@ -111,8 +113,9 @@ bool I2cMasterBase::transferContinue(void *buf, size_t nbytes, uint8_t option) {
*
* @param[in] sclPin The software SCL pin number.
*/
SoftI2cMaster::SoftI2cMaster(uint8_t sclPin, uint8_t sdaPin) {
begin(sclPin, sdaPin);
SoftI2cMaster::SoftI2cMaster(uint8_t sclPin, uint8_t sdaPin)
{
begin(sclPin, sdaPin);
}
//------------------------------------------------------------------------------
/**
@@ -122,32 +125,33 @@ SoftI2cMaster::SoftI2cMaster(uint8_t sclPin, uint8_t sdaPin) {
*
* @param[in] sclPin The software SCL pin number.
*/
void SoftI2cMaster::begin(uint8_t sclPin, uint8_t sdaPin) {
uint8_t port;
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 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);
// 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;
// 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);
// Set scl and sda high.
writeScl(HIGH);
writeSda(HIGH);
}
//------------------------------------------------------------------------------
/* Read a byte and send ACK if more reads follow else NACK to terminate read.
@@ -156,51 +160,56 @@ void SoftI2cMaster::begin(uint8_t sclPin, uint8_t sdaPin) {
*
* @return The byte read from the I2C bus.
*/
uint8_t SoftI2cMaster::read(uint8_t last) {
uint8_t b = 0;
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;
// 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);
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);
/* Issue a stop condition. */
void SoftI2cMaster::stop(void)
{
writeSda(LOW);
sclDelay(20);
writeScl(HIGH);
sclDelay(20);
writeSda(HIGH);
sclDelay(20);
}
//------------------------------------------------------------------------------
/*
@@ -210,30 +219,31 @@ void SoftI2cMaster::stop(void) {
*
* @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);
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();
// Get ACK or NACK.
uint8_t rtn = readSda();
// pull scl low.
writeScl(LOW);
// pull scl low.
writeScl(LOW);
// Pull sda low.
writeSda(LOW);
return rtn == 0;
// Pull sda low.
writeSda(LOW);
return rtn == 0;
}
/** @} */
+272 -240
View File
@@ -63,151 +63,161 @@ 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;
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;
/** 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);
/**
* 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;
/**
* 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;
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);
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;
}
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
@@ -217,118 +227,140 @@ class SoftI2cMaster : public I2cMasterBase {
* @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);
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);
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);
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);
// 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);
// get ACK or NACK
sdaWrite(HIGH);
sclWrite(HIGH);
sclDelay(5);
bool rtn = fastDigitalRead(sdaPin);
sclWrite(LOW);
sdaWrite(LOW);
return rtn == 0;
}
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);
}
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
/** @} */
+120 -102
View File
@@ -18,7 +18,7 @@
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @file
* @brief Software SPI.
*
* @defgroup softSPI Software SPI
@@ -48,108 +48,126 @@ const bool SCK_MODE = true;
* @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;
}
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));
}
}
//----------------------------------------------------------------------------
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
/** @} */
+106 -92
View File
@@ -9,118 +9,132 @@
//------------------------------------------------------------------------------
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);
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);
}
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);
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);
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);
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;
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;
// 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
+282 -239
View File
@@ -4,259 +4,302 @@
//==============================================================================
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);
}
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);
}
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);
}
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> {
class MCP3208 : public MCP3204<CsPin, ClkPin, DoutPin, DinPin, UsecDelay>
{
};
#endif // MCP320X_h
+139 -113
View File
@@ -9,119 +9,145 @@ 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);
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();
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;
// 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
@@ -4,9 +4,10 @@ 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);
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);
}
@@ -4,15 +4,17 @@
// Class with compile time pin number.
DigitalPin<13> pin13;
void setup() {
// Set mode to OUTPUT and level LOW.
pin13.config(OUTPUT, LOW);
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);
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);
}
@@ -6,6 +6,7 @@ DigitalPin<13> pin13(OUTPUT);
void setup() {}
void loop() {
pin13 = pin12;
void loop()
{
pin13 = pin12;
}
@@ -10,30 +10,34 @@ 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;
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 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 setup()
{
// Not used.
}
//------------------------------------------------------------------------------
void loop() {
shiftOut(0X55);
delay(2);
void loop()
{
shiftOut(0X55);
delay(2);
}
@@ -6,19 +6,21 @@
PinIO readPin;
PinIO writePin;
//------------------------------------------------------------------------------
void setup() {
// Assign pin 12 to readPin.
readPin.begin(12);
// input mode with pull-ups disabled
readPin.config(INPUT, LOW);
void setup()
{
// Assign pin 12 to readPin.
readPin.begin(12);
// Assign pin 13 to writePin and set mode to output.
writePin.begin(13);
writePin.mode(OUTPUT);
// 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());
void loop()
{
// Copy the value read from readPin to writePin.
writePin.write(readPin.read());
}
@@ -4,13 +4,15 @@
// Set runtime pin number.
PinIO pin13(13);
void setup() {
// set mode to OUTPUT and level LOW
pin13.config(OUTPUT, LOW);
void setup()
{
// set mode to OUTPUT and level LOW
pin13.config(OUTPUT, LOW);
}
void loop() {
pin13.toggle();
delay(100);
pin13.toggle();
delay(400);
void loop()
{
pin13.toggle();
delay(100);
pin13.toggle();
delay(400);
}
@@ -4,13 +4,15 @@
PinIO readPin(12);
PinIO writePin(13);
void setup() {
// Set input mode and disable pull-up.
readPin.config(INPUT, LOW);
void setup()
{
// Set input mode and disable pull-up.
readPin.config(INPUT, LOW);
// set output mode
writePin.mode(OUTPUT);
// set output mode
writePin.mode(OUTPUT);
}
void loop() {
writePin.write(readPin.read());
void loop()
{
writePin.write(readPin.read());
}
@@ -26,52 +26,59 @@ const uint8_t SCL_PIN = A5;
SoftI2cMaster i2c(SCL_PIN, SDA_PIN);
//FastI2cMaster<SCL_PIN, SDA_PIN> i2c;
//------------------------------------------------------------------------------
void setup() {
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.");
Serial.begin(9600);
while (!Serial);
while (!Serial.available());
Serial.println();
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.");
}
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);
while (!Serial.available());
Serial.println();
if (!found) {
Serial.println("No devices found.");
}
Serial.println("Done");
}
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.
void loop()
{
// Not used.
}
@@ -16,30 +16,35 @@ FastI2cMaster<SCL_PIN, SDA_PIN> rtc;
/*
* Read 'count' bytes from the DS1307 starting at 'address'.
*/
uint8_t readDS1307(uint8_t address, uint8_t *buf, uint8_t count) {
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;
// Send address of data.
if (!rtc.transfer(DS1307ADDR | I2C_WRITE, &address, 1)) {
return false;
}
// Read data.
return rtc.transfer(DS1307ADDR | I2C_READ, buf, count);
// 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) {
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);
// 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);
void setup(void)
{
Serial.begin(9600);
}
//------------------------------------------------------------------------------
/** Store and print a string in flash memory.*/
@@ -50,134 +55,142 @@ void setup(void) {
// 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 hexPrint(uint8_t v)
{
Serial.print(v >> 4, HEX);
Serial.print(v & 0XF, HEX);
}
//------------------------------------------------------------------------------
void hexPrintln(uint8_t v) {
hexPrint(v);
Serial.println();
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 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;
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('-');
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('-');
// month
hexPrint(r[5]);
Serial.write('-');
// day
hexPrint(r[4]);
Serial.write(' ');
// day
hexPrint(r[4]);
Serial.write(' ');
Serial.print(Ddd[r[3] < 8 ? r[3] : 0]);
Serial.write(' ');
Serial.print(Ddd[r[3] < 8 ? r[3] : 0]);
Serial.write(' ');
// hour
hexPrint(r[2]);
Serial.write(':');
// hour
hexPrint(r[2]);
Serial.write(':');
// minute
hexPrint(r[1]);
Serial.write(':');
// minute
hexPrint(r[1]);
Serial.write(':');
// second
hexPrintln(r[0]);
// second
hexPrintln(r[0]);
// Control register.
PgmPrint("Control Register: ");
hexPrintln(r[7]);
// 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 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");
}
}
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
@@ -205,120 +218,149 @@ RS1 RS0 FREQUENCY
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 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]);
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");
}
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 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]);
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;
}
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: ");
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");
}
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");
}
}
@@ -1,14 +1,16 @@
// Scope test for write timing with Arduino digitaWrite().
const uint8_t PIN13 = 13;
void setup() {
// Set mode to OUTPUT.
pinMode(PIN13, OUTPUT);
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);
void loop()
{
digitalWrite(PIN13, HIGH);
digitalWrite(PIN13, LOW);
digitalWrite(PIN13, HIGH);
digitalWrite(PIN13, LOW);
delay(1);
}
@@ -4,16 +4,18 @@
// Class with compile time pin number.
DigitalPin<13> pin13;
void setup() {
// set mode to OUTPUT
pin13.mode(OUTPUT);
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);
void loop()
{
pin13.high();
pin13.low();
pin13.write(1);
pin13.write(0);
pin13.toggle();
pin13.toggle();
delay(1);
}
@@ -1,13 +1,15 @@
// Test fastDigital function timing with a scope.
#include <DigitalIO.h>
const uint8_t PIN = 13;
void setup() {
fastPinMode(PIN, OUTPUT);
void setup()
{
fastPinMode(PIN, OUTPUT);
}
void loop() {
fastDigitalWrite(PIN, HIGH);
fastDigitalWrite(PIN, LOW);
fastDigitalToggle(PIN);
fastDigitalToggle(PIN);
delay(1);
void loop()
{
fastDigitalWrite(PIN, HIGH);
fastDigitalWrite(PIN, LOW);
fastDigitalToggle(PIN);
fastDigitalToggle(PIN);
delay(1);
}
@@ -4,16 +4,18 @@
// Class with runtime pin numbers.
PinIO pin(13);
void setup() {
// Set mode to OUTPUT.
pin.mode(OUTPUT);
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);
void loop()
{
pin.high();
pin.low();
pin.write(1);
pin.write(0);
pin.toggle();
pin.toggle();
delay(1);
}
@@ -9,29 +9,35 @@ 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 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);
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);
}
+80 -80
View File
@@ -7,92 +7,92 @@
#define __digitalWriteFast_h_
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA1280) || defined(ARDUINO_AVR_MEGA2560) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
#define __digitalPinToPortReg(__pin) \
(((__pin) >= 22 && (__pin) <= 29) ? &PORTA : \
((((__pin) >= 10 && (__pin) <= 13) || ((__pin) >= 50 && (__pin) <= 53)) ? &PORTB : \
(((__pin) >= 30 && (__pin) <= 37) ? &PORTC : \
((((__pin) >= 18 && (__pin) <= 21) || (__pin) == 38) ? &PORTD : \
((((__pin) <= 3) || (__pin) == 5) ? &PORTE : \
(((__pin) >= 54 && (__pin) <= 61) ? &PORTF : \
((((__pin) >= 39 && (__pin) <= 41) || (__pin) == 4) ? &PORTG : \
((((__pin) >= 6 && (__pin) <= 9) || (__pin) == 16 || (__pin) == 17) ? &PORTH : \
(((__pin) == 14 || (__pin) == 15) ? &PORTJ : \
(((__pin) >= 62 && (__pin) <= 69) ? &PORTK : &PORTL))))))))))
#define __digitalPinToDDRReg(__pin) \
(((__pin) >= 22 && (__pin) <= 29) ? &DDRA : \
((((__pin) >= 10 && (__pin) <= 13) || ((__pin) >= 50 && (__pin) <= 53)) ? &DDRB : \
(((__pin) >= 30 && (__pin) <= 37) ? &DDRC : \
((((__pin) >= 18 && (__pin) <= 21) || (__pin) == 38) ? &DDRD : \
((((__pin) <= 3) || (__pin) == 5) ? &DDRE : \
(((__pin) >= 54 && (__pin) <= 61) ? &DDRF : \
((((__pin) >= 39 && (__pin) <= 41) || (__pin) == 4) ? &DDRG : \
((((__pin) >= 6 && (__pin) <= 9) || (__pin) == 16 || (__pin) == 17) ? &DDRH : \
(((__pin) == 14 || (__pin) == 15) ? &DDRJ : \
(((__pin) >= 62 && (__pin) <= 69) ? &DDRK : &DDRL))))))))))
#define __digitalPinToPINReg(__pin) \
(((__pin) >= 22 && (__pin) <= 29) ? &PINA : \
((((__pin) >= 10 && (__pin) <= 13) || ((__pin) >= 50 && (__pin) <= 53)) ? &PINB : \
(((__pin) >= 30 && (__pin) <= 37) ? &PINC : \
((((__pin) >= 18 && (__pin) <= 21) || (__pin) == 38) ? &PIND : \
((((__pin) <= 3) || (__pin) == 5) ? &PINE : \
(((__pin) >= 54 && (__pin) <= 61) ? &PINF : \
((((__pin) >= 39 && (__pin) <= 41) || (__pin) == 4) ? &PING : \
((((__pin) >= 6 && (__pin) <= 9) || (__pin) == 16 || (__pin) == 17) ? &PINH : \
(((__pin) == 14 || (__pin) == 15) ? &PINJ : \
(((__pin) >= 62 && (__pin) <= 69) ? &PINK : &PINL))))))))))
#define __digitalPinToBit(__pin) \
(((__pin) >= 7 && (__pin) <= 9) ? (__pin) - 3 : \
(((__pin) >= 10 && (__pin) <= 13) ? (__pin) - 6 : \
(((__pin) >= 22 && (__pin) <= 29) ? (__pin) - 22 : \
(((__pin) >= 30 && (__pin) <= 37) ? 37 - (__pin) : \
(((__pin) >= 39 && (__pin) <= 41) ? 41 - (__pin) : \
(((__pin) >= 42 && (__pin) <= 49) ? 49 - (__pin) : \
(((__pin) >= 50 && (__pin) <= 53) ? 53 - (__pin) : \
(((__pin) >= 54 && (__pin) <= 61) ? (__pin) - 54 : \
(((__pin) >= 62 && (__pin) <= 69) ? (__pin) - 62 : \
(((__pin) == 0 || (__pin) == 15 || (__pin) == 17 || (__pin) == 21) ? 0 : \
(((__pin) == 1 || (__pin) == 14 || (__pin) == 16 || (__pin) == 20) ? 1 : \
(((__pin) == 19) ? 2 : \
(((__pin) == 5 || (__pin) == 6 || (__pin) == 18) ? 3 : \
(((__pin) == 2) ? 4 : \
(((__pin) == 3 || (__pin) == 4) ? 5 : 7)))))))))))))))
#define __digitalPinToPortReg(__pin) \
(((__pin) >= 22 && (__pin) <= 29) ? &PORTA : \
((((__pin) >= 10 && (__pin) <= 13) || ((__pin) >= 50 && (__pin) <= 53)) ? &PORTB : \
(((__pin) >= 30 && (__pin) <= 37) ? &PORTC : \
((((__pin) >= 18 && (__pin) <= 21) || (__pin) == 38) ? &PORTD : \
((((__pin) <= 3) || (__pin) == 5) ? &PORTE : \
(((__pin) >= 54 && (__pin) <= 61) ? &PORTF : \
((((__pin) >= 39 && (__pin) <= 41) || (__pin) == 4) ? &PORTG : \
((((__pin) >= 6 && (__pin) <= 9) || (__pin) == 16 || (__pin) == 17) ? &PORTH : \
(((__pin) == 14 || (__pin) == 15) ? &PORTJ : \
(((__pin) >= 62 && (__pin) <= 69) ? &PORTK : &PORTL))))))))))
#define __digitalPinToDDRReg(__pin) \
(((__pin) >= 22 && (__pin) <= 29) ? &DDRA : \
((((__pin) >= 10 && (__pin) <= 13) || ((__pin) >= 50 && (__pin) <= 53)) ? &DDRB : \
(((__pin) >= 30 && (__pin) <= 37) ? &DDRC : \
((((__pin) >= 18 && (__pin) <= 21) || (__pin) == 38) ? &DDRD : \
((((__pin) <= 3) || (__pin) == 5) ? &DDRE : \
(((__pin) >= 54 && (__pin) <= 61) ? &DDRF : \
((((__pin) >= 39 && (__pin) <= 41) || (__pin) == 4) ? &DDRG : \
((((__pin) >= 6 && (__pin) <= 9) || (__pin) == 16 || (__pin) == 17) ? &DDRH : \
(((__pin) == 14 || (__pin) == 15) ? &DDRJ : \
(((__pin) >= 62 && (__pin) <= 69) ? &DDRK : &DDRL))))))))))
#define __digitalPinToPINReg(__pin) \
(((__pin) >= 22 && (__pin) <= 29) ? &PINA : \
((((__pin) >= 10 && (__pin) <= 13) || ((__pin) >= 50 && (__pin) <= 53)) ? &PINB : \
(((__pin) >= 30 && (__pin) <= 37) ? &PINC : \
((((__pin) >= 18 && (__pin) <= 21) || (__pin) == 38) ? &PIND : \
((((__pin) <= 3) || (__pin) == 5) ? &PINE : \
(((__pin) >= 54 && (__pin) <= 61) ? &PINF : \
((((__pin) >= 39 && (__pin) <= 41) || (__pin) == 4) ? &PING : \
((((__pin) >= 6 && (__pin) <= 9) || (__pin) == 16 || (__pin) == 17) ? &PINH : \
(((__pin) == 14 || (__pin) == 15) ? &PINJ : \
(((__pin) >= 62 && (__pin) <= 69) ? &PINK : &PINL))))))))))
#define __digitalPinToBit(__pin) \
(((__pin) >= 7 && (__pin) <= 9) ? (__pin) - 3 : \
(((__pin) >= 10 && (__pin) <= 13) ? (__pin) - 6 : \
(((__pin) >= 22 && (__pin) <= 29) ? (__pin) - 22 : \
(((__pin) >= 30 && (__pin) <= 37) ? 37 - (__pin) : \
(((__pin) >= 39 && (__pin) <= 41) ? 41 - (__pin) : \
(((__pin) >= 42 && (__pin) <= 49) ? 49 - (__pin) : \
(((__pin) >= 50 && (__pin) <= 53) ? 53 - (__pin) : \
(((__pin) >= 54 && (__pin) <= 61) ? (__pin) - 54 : \
(((__pin) >= 62 && (__pin) <= 69) ? (__pin) - 62 : \
(((__pin) == 0 || (__pin) == 15 || (__pin) == 17 || (__pin) == 21) ? 0 : \
(((__pin) == 1 || (__pin) == 14 || (__pin) == 16 || (__pin) == 20) ? 1 : \
(((__pin) == 19) ? 2 : \
(((__pin) == 5 || (__pin) == 6 || (__pin) == 18) ? 3 : \
(((__pin) == 2) ? 4 : \
(((__pin) == 3 || (__pin) == 4) ? 5 : 7)))))))))))))))
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTB : (((__pin) >= 8 && (__pin) <= 15) ? &PORTD : (((__pin) >= 16 && (__pin) <= 23) ? &PORTC : &PORTA)))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRB : (((__pin) >= 8 && (__pin) <= 15) ? &DDRD : (((__pin) >= 8 && (__pin) <= 15) ? &DDRC : &DDRA)))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PINB : (((__pin) >= 8 && (__pin) <= 15) ? &PIND : (((__pin) >= 8 && (__pin) <= 15) ? &PINC : &PINA)))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 15) ? (__pin) - 8 : (((__pin) >= 16 && (__pin) <= 23) ? (__pin) - 16 : (__pin) - 24)))
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTB : (((__pin) >= 8 && (__pin) <= 15) ? &PORTD : (((__pin) >= 16 && (__pin) <= 23) ? &PORTC : &PORTA)))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRB : (((__pin) >= 8 && (__pin) <= 15) ? &DDRD : (((__pin) >= 8 && (__pin) <= 15) ? &DDRC : &DDRA)))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PINB : (((__pin) >= 8 && (__pin) <= 15) ? &PIND : (((__pin) >= 8 && (__pin) <= 15) ? &PINC : &PINA)))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 15) ? (__pin) - 8 : (((__pin) >= 16 && (__pin) <= 23) ? (__pin) - 16 : (__pin) - 24)))
#elif defined(ARDUINO_AVR_LEONARDO) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)
#define __digitalPinToPortReg(__pin) ((((__pin) <= 4) || (__pin) == 6 || (__pin) == 12 || (__pin) == 24 || (__pin) == 25 || (__pin) == 29) ? &PORTD : (((__pin) == 5 || (__pin) == 13) ? &PORTC : (((__pin) >= 18 && (__pin) <= 23)) ? &PORTF : (((__pin) == 7) ? &PORTE : &PORTB)))
#define __digitalPinToDDRReg(__pin) ((((__pin) <= 4) || (__pin) == 6 || (__pin) == 12 || (__pin) == 24 || (__pin) == 25 || (__pin) == 29) ? &DDRD : (((__pin) == 5 || (__pin) == 13) ? &DDRC : (((__pin) >= 18 && (__pin) <= 23)) ? &DDRF : (((__pin) == 7) ? &DDRE : &DDRB)))
#define __digitalPinToPINReg(__pin) ((((__pin) <= 4) || (__pin) == 6 || (__pin) == 12 || (__pin) == 24 || (__pin) == 25 || (__pin) == 29) ? &PIND : (((__pin) == 5 || (__pin) == 13) ? &PINC : (((__pin) >= 18 && (__pin) <= 23)) ? &PINF : (((__pin) == 7) ? &PINE : &PINB)))
#define __digitalPinToBit(__pin) (((__pin) >= 8 && (__pin) <= 11) ? (__pin) - 4 : (((__pin) >= 18 && (__pin) <= 21) ? 25 - (__pin) : (((__pin) == 0) ? 2 : (((__pin) == 1) ? 3 : (((__pin) == 2) ? 1 : (((__pin) == 3) ? 0 : (((__pin) == 4) ? 4 : (((__pin) == 6) ? 7 : (((__pin) == 13) ? 7 : (((__pin) == 14) ? 3 : (((__pin) == 15) ? 1 : (((__pin) == 16) ? 2 : (((__pin) == 17) ? 0 : (((__pin) == 22) ? 1 : (((__pin) == 23) ? 0 : (((__pin) == 24) ? 4 : (((__pin) == 25) ? 7 : (((__pin) == 26) ? 4 : (((__pin) == 27) ? 5 : 6 )))))))))))))))))))
#define __digitalPinToPortReg(__pin) ((((__pin) <= 4) || (__pin) == 6 || (__pin) == 12 || (__pin) == 24 || (__pin) == 25 || (__pin) == 29) ? &PORTD : (((__pin) == 5 || (__pin) == 13) ? &PORTC : (((__pin) >= 18 && (__pin) <= 23)) ? &PORTF : (((__pin) == 7) ? &PORTE : &PORTB)))
#define __digitalPinToDDRReg(__pin) ((((__pin) <= 4) || (__pin) == 6 || (__pin) == 12 || (__pin) == 24 || (__pin) == 25 || (__pin) == 29) ? &DDRD : (((__pin) == 5 || (__pin) == 13) ? &DDRC : (((__pin) >= 18 && (__pin) <= 23)) ? &DDRF : (((__pin) == 7) ? &DDRE : &DDRB)))
#define __digitalPinToPINReg(__pin) ((((__pin) <= 4) || (__pin) == 6 || (__pin) == 12 || (__pin) == 24 || (__pin) == 25 || (__pin) == 29) ? &PIND : (((__pin) == 5 || (__pin) == 13) ? &PINC : (((__pin) >= 18 && (__pin) <= 23)) ? &PINF : (((__pin) == 7) ? &PINE : &PINB)))
#define __digitalPinToBit(__pin) (((__pin) >= 8 && (__pin) <= 11) ? (__pin) - 4 : (((__pin) >= 18 && (__pin) <= 21) ? 25 - (__pin) : (((__pin) == 0) ? 2 : (((__pin) == 1) ? 3 : (((__pin) == 2) ? 1 : (((__pin) == 3) ? 0 : (((__pin) == 4) ? 4 : (((__pin) == 6) ? 7 : (((__pin) == 13) ? 7 : (((__pin) == 14) ? 3 : (((__pin) == 15) ? 1 : (((__pin) == 16) ? 2 : (((__pin) == 17) ? 0 : (((__pin) == 22) ? 1 : (((__pin) == 23) ? 0 : (((__pin) == 24) ? 4 : (((__pin) == 25) ? 7 : (((__pin) == 26) ? 4 : (((__pin) == 27) ? 5 : 6 )))))))))))))))))))
#elif defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_DUEMILANOVE) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__)
#if defined(__AVR_ATmega328PB__)
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTD : (((__pin) >= 8 && (__pin) <= 13) ? &PORTB : (((__pin) >= 14 && (__pin) <= 19) ? &PORTC : &PORTE)))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRD : (((__pin) >= 8 && (__pin) <= 13) ? &DDRB : (((__pin) >= 14 && (__pin) <= 19) ? &DDRC : &DDRE)))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PIND : (((__pin) >= 8 && (__pin) <= 13) ? &PINB : (((__pin) >= 14 && (__pin) <= 19) ? &PINC : &PINE)))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 13) ? (__pin) - 8 : (((__pin) >= 14 && (__pin) <= 19) ? (__pin) - 14 : (((__pin) >= 20 && (__pin) <= 21) ? (__pin) - 18 : (__pin) - 22))))
#else
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTD : (((__pin) >= 8 && (__pin) <= 13) ? &PORTB : &PORTC))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRD : (((__pin) >= 8 && (__pin) <= 13) ? &DDRB : &DDRC))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PIND : (((__pin) >= 8 && (__pin) <= 13) ? &PINB : &PINC))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 13) ? (__pin) - 8 : (__pin) - 14))
#endif
#if defined(__AVR_ATmega328PB__)
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTD : (((__pin) >= 8 && (__pin) <= 13) ? &PORTB : (((__pin) >= 14 && (__pin) <= 19) ? &PORTC : &PORTE)))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRD : (((__pin) >= 8 && (__pin) <= 13) ? &DDRB : (((__pin) >= 14 && (__pin) <= 19) ? &DDRC : &DDRE)))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PIND : (((__pin) >= 8 && (__pin) <= 13) ? &PINB : (((__pin) >= 14 && (__pin) <= 19) ? &PINC : &PINE)))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 13) ? (__pin) - 8 : (((__pin) >= 14 && (__pin) <= 19) ? (__pin) - 14 : (((__pin) >= 20 && (__pin) <= 21) ? (__pin) - 18 : (__pin) - 22))))
#else
#define __digitalPinToPortReg(__pin) (((__pin) <= 7) ? &PORTD : (((__pin) >= 8 && (__pin) <= 13) ? &PORTB : &PORTC))
#define __digitalPinToDDRReg(__pin) (((__pin) <= 7) ? &DDRD : (((__pin) >= 8 && (__pin) <= 13) ? &DDRB : &DDRC))
#define __digitalPinToPINReg(__pin) (((__pin) <= 7) ? &PIND : (((__pin) >= 8 && (__pin) <= 13) ? &PINB : &PINC))
#define __digitalPinToBit(__pin) (((__pin) <= 7) ? (__pin) : (((__pin) >= 8 && (__pin) <= 13) ? (__pin) - 8 : (__pin) - 14))
#endif
#endif
#if defined(ARDUINO_ARCH_AVR)
#if !defined(__atomicWrite)
#define __atomicWrite(A,P,V) do { if ((A) < 0x40) {bitWrite((A), (P), (V) );} else {uint8_t register saveSreg = SREG;cli();bitWrite((A), (P), (V));SREG = saveSreg;}} while(0)
#endif
#if !defined(digitalWriteFast)
#define digitalWriteFast(__pin, __value) do { if (__builtin_constant_p(__pin) && __builtin_constant_p(__value)) { bitWrite(*__digitalPinToPortReg(__pin), __digitalPinToBit(__pin), (__value)); } else { digitalWrite((__pin), (__value)); } } while (0)
#endif
#if !defined(pinModeFast)
#define pinModeFast(__pin, __mode) do { if (__builtin_constant_p(__pin) && __builtin_constant_p(__mode) && (__mode!=INPUT_PULLUP)) { bitWrite(*__digitalPinToDDRReg(__pin), __digitalPinToBit(__pin), (__mode)); } else { pinMode((__pin), (__mode)); } } while (0)
#endif
#if !defined(digitalReadFast)
#define digitalReadFast(__pin) ( (bool) (__builtin_constant_p(__pin) ) ? (( bitRead(*__digitalPinToPINReg(__pin), __digitalPinToBit(__pin))) ) : digitalRead((__pin)) )
#endif
#if !defined(__atomicWrite)
#define __atomicWrite(A,P,V) do { if ((A) < 0x40) {bitWrite((A), (P), (V) );} else {uint8_t register saveSreg = SREG;cli();bitWrite((A), (P), (V));SREG = saveSreg;}} while(0)
#endif
#if !defined(digitalWriteFast)
#define digitalWriteFast(__pin, __value) do { if (__builtin_constant_p(__pin) && __builtin_constant_p(__value)) { bitWrite(*__digitalPinToPortReg(__pin), __digitalPinToBit(__pin), (__value)); } else { digitalWrite((__pin), (__value)); } } while (0)
#endif
#if !defined(pinModeFast)
#define pinModeFast(__pin, __mode) do { if (__builtin_constant_p(__pin) && __builtin_constant_p(__mode) && (__mode!=INPUT_PULLUP)) { bitWrite(*__digitalPinToDDRReg(__pin), __digitalPinToBit(__pin), (__mode)); } else { pinMode((__pin), (__mode)); } } while (0)
#endif
#if !defined(digitalReadFast)
#define digitalReadFast(__pin) ( (bool) (__builtin_constant_p(__pin) ) ? (( bitRead(*__digitalPinToPINReg(__pin), __digitalPinToBit(__pin))) ) : digitalRead((__pin)) )
#endif
#endif
#endif