Skip to content

Commit c0402ac

Browse files
committed
Refactoring and bug fix
1 parent dd6a34a commit c0402ac

File tree

8 files changed

+59
-26
lines changed

8 files changed

+59
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ template class may be used to construct additional libraries.
1212
This library supports boards based on SAM3X8E, ATmega168, ATmega328P,
1313
ATmega32U4, ATmega1280, ATmega2560, ATtinyX4 and ATtinyX5.
1414

15-
Version: 1.7
15+
Version: 1.8
1616

1717
## Classes
1818

examples/Benchmark/Benchmark.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ void setup()
2020
{
2121
pinMode(LED_PIN, OUTPUT);
2222
pinMode(BUTTON_PIN, INPUT_PULLUP);
23+
2324
led.output();
24-
button.input_pullup();
25+
button.input().pullup();
2526
ss.output();
2627
}
2728

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Arduino-GPIO
2-
version=1.7
2+
version=1.8
33
author=Mikael Patel
44
maintainer=Mikael Patel <mikael.patel@gmail.com>
55
sentence=General Purpose Input/Output (GPIO) library for Arduino.

mainpage.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ additional classes and libraries.
1414
This library supports boards based on SAM3X8E, ATmega168, ATmega328P,
1515
ATmega32U4, ATmega1280, ATmega2560, ATtinyX4 and ATtinyX5.
1616

17-
Version: 1.7
17+
Version: 1.8
1818
*/
1919

2020
/** @page License

src/Hardware/AVR/Board.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Board.h
3-
* @version 1.4
3+
* @version 1.5
44
*
55
* @section License
66
* Copyright (C) 2017, Mikael Patel
@@ -31,16 +31,16 @@
3131
* Return port control register address from board pin value.
3232
* @return io port address
3333
*/
34-
#define GPIO_REG(pin) (pin >> 4)
34+
#define GPIO_REG(pin) ((pin) >> 4)
3535

36-
/** Maximum port control register address for atomic instructions. */
37-
#define GPIO_ATOMIC_MAX GPIO_PIN(0x60,0)
36+
/** Maximum port control register address for atomic bit instructions. */
37+
#define GPIO_ATOMIC_MAX GPIO_PIN(0x40,0)
3838

3939
/**
4040
* Return pin mask from board pin value.
4141
* @return pin mask
4242
*/
43-
#define GPIO_MASK(pin) _BV(pin & 0xf)
43+
#define GPIO_MASK(pin) _BV((pin) & 0xf)
4444

4545
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
4646
/**

src/Hardware/AVR/GPIO.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Hardware/AVR/GPIO.h
3-
* @version 1.4
3+
* @version 1.5
44
*
55
* @section License
66
* Copyright (C) 2017, Mikael Patel
@@ -44,7 +44,7 @@
4444
/**
4545
* General Purpose Digital I/O pin template class. Highly optimized
4646
* pin access. The PIN address is bit pointer to the port control
47-
* register and pin bit position. See Board.h for details.
47+
* register and pin bit position. See Hardware/AVR/Board.h for details.
4848
* @param[in] PIN board pin definition.
4949
*/
5050
template<BOARD::pin_t PIN>
@@ -53,19 +53,20 @@ class GPIO {
5353
/**
5454
* Set pin to input mode.
5555
*/
56-
void input()
56+
GPIO<PIN>& input()
5757
__attribute__((always_inline))
5858
{
5959
GPIO_ATOMIC(SFR()->ddr &= ~MASK);
60+
return (*this);
6061
}
6162

6263
/**
63-
* Set pin to input mode and activate internal pullup resistor.
64+
* Used with input() to activate internal pullup resistor on
65+
* input pin.
6466
*/
65-
void input_pullup()
67+
void pullup()
6668
__attribute__((always_inline))
6769
{
68-
input();
6970
high();
7071
}
7172

@@ -78,6 +79,17 @@ class GPIO {
7879
GPIO_ATOMIC(SFR()->ddr |= MASK);
7980
}
8081

82+
/**
83+
* Open-collector pin. Use input() and output() to source and sink
84+
* current.
85+
*/
86+
void open_collector()
87+
__attribute__((always_inline))
88+
{
89+
input();
90+
low();
91+
}
92+
8193
/**
8294
* Return current pin state.
8395
* @return state.

src/Hardware/SAM/Board.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Hardware/SAM/Board.h
3-
* @version 1.0
3+
* @version 1.1
44
*
55
* @section License
66
* Copyright (C) 2017, Mikael Patel
@@ -31,7 +31,7 @@
3131
* Return port control register index from board pin value.
3232
* @return io port address
3333
*/
34-
#define GPIO_REG(pin) (pin >> 8)
34+
#define GPIO_REG(pin) ((pin) >> 8)
3535

3636
/**
3737
* Return pin mask from board pin value.

src/Hardware/SAM/GPIO.h

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file Hardware/SAM/GPIO.h
3-
* @version 1.0
3+
* @version 1.1
44
*
55
* @section License
66
* Copyright (C) 2017, Mikael Patel
@@ -32,26 +32,27 @@ class GPIO {
3232
public:
3333
GPIO()
3434
{
35+
pmc_enable_periph_clk(ID_PIO());
3536
SFR()->PIO_PER = MASK;
3637
}
3738

3839
/**
3940
* Set pin to input mode.
4041
*/
41-
void input()
42+
GPIO<PIN>& input()
4243
__attribute__((always_inline))
4344
{
4445
SFR()->PIO_ODR = MASK;
45-
SFR()->PIO_PUDR = MASK;
46+
return (*this);
4647
}
4748

4849
/**
49-
* Set pin to input mode and activate internal pullup resistor.
50+
* Used with input() to activate internal pullup resistor on
51+
* input pin.
5052
*/
51-
void input_pullup()
53+
void pullup()
5254
__attribute__((always_inline))
5355
{
54-
SFR()->PIO_ODR = MASK;
5556
SFR()->PIO_PUER = MASK;
5657
}
5758

@@ -62,7 +63,16 @@ class GPIO {
6263
__attribute__((always_inline))
6364
{
6465
SFR()->PIO_OER = MASK;
65-
SFR()->PIO_PUER = MASK;
66+
}
67+
68+
/**
69+
* Open-collector pin.
70+
*/
71+
void open_collector()
72+
__attribute__((always_inline))
73+
{
74+
SFR()->PIO_CODR = MASK;
75+
SFR()->PIO_MDER = MASK;
6676
}
6777

6878
/**
@@ -72,8 +82,6 @@ class GPIO {
7282
bool read()
7383
__attribute__((always_inline))
7484
{
75-
if (SFR()->PIO_OSR & MASK)
76-
return ((SFR()->PIO_ODSR & MASK) != 0);
7785
return ((SFR()->PIO_PDSR & MASK) != 0);
7886
}
7987

@@ -184,5 +192,17 @@ class GPIO {
184192
return (PIOD);
185193
}
186194
}
195+
196+
uint32_t ID_PIO()
197+
__attribute__((always_inline))
198+
{
199+
switch (GPIO_REG(PIN)) {
200+
case 0: return (ID_PIOA);
201+
case 1: return (ID_PIOB);
202+
case 2: return (ID_PIOC);
203+
default:
204+
return (ID_PIOD);
205+
}
206+
}
187207
};
188208
#endif

0 commit comments

Comments
 (0)