FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef FASTIO_HH
22#define FASTIO_HH
23
24#include "boards/board_traits.h"
25#include "int.h"
26#include "pci.h"
27#include "utilities.h"
28
29namespace board
30{
31 template<DigitalPin PIN> constexpr uint8_t BIT() INLINE;
32 template<DigitalPin PIN> constexpr uint8_t MASK() INLINE;
33
37 template<DigitalPin PIN> constexpr uint8_t BIT()
38 {
39 return board_traits::DigitalPin_trait<PIN>::BIT;
40 }
41
45 template<DigitalPin PIN> constexpr uint8_t MASK()
46 {
47 return bits::BV8(board_traits::DigitalPin_trait<PIN>::BIT);
48 }
49};
50
54namespace gpio
55{
59 enum class PinMode : uint8_t
60 {
62 INPUT,
66 OUTPUT,
67 };
68
89 template<board::Port PORT_, uint8_t BIT_> class FastPin
90 {
91 private:
92 using TRAIT = board_traits::Port_trait<PORT_>;
93
94 public:
96 FastPin(const FastPin&) = default;
97 FastPin& operator=(const FastPin&) = default;
99
101 static constexpr const board::Port PORT = PORT_;
103 static constexpr const uint8_t BIT = BIT_;
105 static constexpr const uint8_t MASK = bits::BV8(BIT);
106
113 {
114 static_assert(TRAIT::DPIN_MASK & bits::BV8(BIT), "BIT must be compatible with PORT available pins");
115 }
116
126 FastPin(PinMode mode, bool value = false) INLINE
127 {
128 static_assert(TRAIT::DPIN_MASK & bits::BV8(BIT), "BIT must be compatible with PORT available pins");
129 set_mode(mode, value);
130 }
131
139 void set_mode(PinMode mode, bool value = false) INLINE
140 {
141 if (mode == PinMode::OUTPUT)
142 TRAIT::DDR |= bits::BV8(BIT);
143 else
144 TRAIT::DDR &= bits::CBV8(BIT);
145 if (value || (mode == PinMode::INPUT_PULLUP))
146 TRAIT::PORT |= bits::BV8(BIT);
147 else
148 TRAIT::PORT &= bits::CBV8(BIT);
149 }
150
155 void set() INLINE
156 {
157 TRAIT::PORT |= bits::BV8(BIT);
158 }
159
165 {
166 TRAIT::PORT &= bits::CBV8(BIT);
167 }
168
175 {
176 TRAIT::PIN |= bits::BV8(BIT);
177 }
178
185 {
186 return TRAIT::PIN & bits::BV8(BIT);
187 }
188 };
189
203 template<board::Port PORT_> class FastPort
204 {
205 private:
206 using TRAIT = board_traits::Port_trait<PORT_>;
207
208 public:
210 FastPort(const FastPort&) = default;
211 FastPort& operator=(const FastPort&) = default;
213
215 static constexpr const board::Port PORT = PORT_;
216
222 FastPort() = default;
223
239 FastPort(uint8_t ddr, uint8_t port = 0) INLINE
240 {
241 set_DDR(ddr);
242 set_PORT(port);
243 }
244
258 template<uint8_t BIT> FastPin<PORT, BIT> get_pin(PinMode mode, bool value = false)
259 {
260 return FastPin<PORT, BIT>{mode, value};
261 }
262
273 template<uint8_t BIT> FastPin<PORT, BIT> get_pin()
274 {
275 return FastPin<PORT, BIT>{};
276 }
277
291 void set_PORT(uint8_t port) INLINE
292 {
293 TRAIT::PORT = port;
294 }
295
305 uint8_t get_PORT() INLINE
306 {
307 return TRAIT::PORT;
308 }
309
319 void set_DDR(uint8_t ddr) INLINE
320 {
321 TRAIT::DDR = ddr;
322 }
323
330 uint8_t get_DDR() INLINE
331 {
332 return TRAIT::DDR;
333 }
334
341 void set_PIN(uint8_t pin) INLINE
342 {
343 TRAIT::PIN = pin;
344 }
345
352 uint8_t get_PIN() INLINE
353 {
354 return TRAIT::PIN;
355 }
356 };
357
379 template<board::Port PORT_, uint8_t MASK_> class FastMaskedPort
380 {
381 private:
382 using TRAIT = board_traits::Port_trait<PORT_>;
383
384 public:
386 FastMaskedPort(const FastMaskedPort&) = default;
387 FastMaskedPort& operator=(const FastMaskedPort&) = default;
389
391 static constexpr const board::Port PORT = PORT_;
392
394 static constexpr const uint8_t MASK = MASK_;
395
401 FastMaskedPort() = default;
402
419 FastMaskedPort(uint8_t ddr, uint8_t port = 0)
420 {
421 set_DDR(ddr);
422 set_PORT(port);
423 }
424
439 void set_PORT(uint8_t port) INLINE
440 {
441 TRAIT::PORT = uint8_t(TRAIT::PORT & bits::COMPL(MASK)) | uint8_t(port & MASK);
442 }
443
455 uint8_t get_PORT() INLINE
456 {
457 return TRAIT::PORT & MASK;
458 }
459
470 void set_DDR(uint8_t ddr) INLINE
471 {
472 TRAIT::DDR = uint8_t(TRAIT::DDR & bits::COMPL(MASK)) | uint8_t(ddr & MASK);
473 }
474
483 uint8_t get_DDR() INLINE
484 {
485 return TRAIT::DDR & MASK;
486 }
487
496 void set_PIN(uint8_t pin) INLINE
497 {
498 TRAIT::PIN = pin & MASK;
499 }
500
507 uint8_t get_PIN() INLINE
508 {
509 return TRAIT::PIN & MASK;
510 }
511 };
512
550 template<board::DigitalPin DPIN_> class FastPinType
551 {
552 private:
553 using TRAIT = board_traits::DigitalPin_trait<DPIN_>;
554 using PTRAIT = board_traits::Port_trait<TRAIT::PORT>;
555
556 public:
557 FastPinType() = delete;
558
560 static constexpr const board::DigitalPin DPIN = DPIN_;
562 static constexpr const board::Port PORT = TRAIT::PORT;
564 static constexpr const uint8_t BIT = TRAIT::BIT;
566 static constexpr const uint8_t MASK = bits::BV8(BIT);
567
572
580 static void set_mode(PinMode mode, bool value = false)
581 {
582 if (mode == PinMode::OUTPUT)
583 PTRAIT::DDR |= bits::BV8(BIT);
584 else
585 PTRAIT::DDR &= bits::CBV8(BIT);
586 if (value || (mode == PinMode::INPUT_PULLUP))
587 PTRAIT::PORT |= bits::BV8(BIT);
588 else
589 PTRAIT::PORT &= bits::CBV8(BIT);
590 }
591
596 static void set()
597 {
598 PTRAIT::PORT |= bits::BV8(BIT);
599 }
600
605 static void clear()
606 {
607 PTRAIT::PORT &= bits::CBV8(BIT);
608 }
609
615 static void toggle()
616 {
617 PTRAIT::PIN |= bits::BV8(BIT);
618 }
619
625 static bool value()
626 {
627 return PTRAIT::PIN & bits::BV8(BIT);
628 }
629 };
630
632 template<> class FastPinType<board::DigitalPin::NONE>
633 {
634 public:
635 FastPinType() = delete;
636
637 static constexpr const board::Port PORT = board::Port::NONE;
638 static constexpr const uint8_t BIT = 0;
639 static constexpr const uint8_t MASK = 0;
640 using TYPE = FastPin<PORT, BIT>;
641 using PORT_TYPE = FastPort<PORT>;
642
643 static void set_mode(UNUSED PinMode mode, UNUSED bool value = false) {}
644 static void set() {}
645 static void clear() {}
646 static void toggle() {}
647 static bool value()
648 {
649 return false;
650 }
651 };
653
655 template<> class FastPin<board::Port::NONE, 0>
656 {
657 public:
658 FastPin(const FastPin&) = default;
659 FastPin& operator=(const FastPin&) = default;
660 FastPin() INLINE = default;
661 FastPin(PinMode mode UNUSED, bool value UNUSED = false) INLINE {}
662 void set() INLINE {}
663 void clear() INLINE {}
664 void toggle() INLINE {}
665 bool value() INLINE
666 {
667 return false;
668 }
669 };
671
693 template<board::DigitalPin DPIN_>
695
716 template<board::InterruptPin IPIN_>
718
739 template<board::ExternalInterruptPin EPIN_>
741}
742
743#endif /* FASTIO_HH */
API that manipulates a part of a digital IO port.
Definition: gpio.h:380
FastMaskedPort()=default
Construct a FastMaskedPort without any physical setup on target MCU.
FastMaskedPort(uint8_t ddr, uint8_t port=0)
Construct a FastMaskedPort for the pins selected by the provide bits mask, with the given direction b...
Definition: gpio.h:419
void set_PIN(uint8_t pin) INLINE
Set the 8-bits value for port PIN register, this value will be masked according to the provided bit m...
Definition: gpio.h:496
uint8_t get_DDR() INLINE
Get the current 8-bit value of port DDR (direction) register, masked according to the bit mask provid...
Definition: gpio.h:483
uint8_t get_PORT() INLINE
Get the current 8-bit value of port PORT register, masked according to the bit mask provided at const...
Definition: gpio.h:455
uint8_t get_PIN() INLINE
Get the current 8-bit value of PIN register for this port, masked according to the bit mask provided ...
Definition: gpio.h:507
static constexpr const board::Port PORT
The actual port in target MCU.
Definition: gpio.h:391
void set_PORT(uint8_t port) INLINE
Set the 8-bits value for port PORT register, this value will be masked according to the provided bit ...
Definition: gpio.h:439
static constexpr const uint8_t MASK
The bit mask used for this FastMaskedPort.
Definition: gpio.h:394
void set_DDR(uint8_t ddr) INLINE
Set the 8-bits value for port DDR (direction) register, this value will be masked according to the pr...
Definition: gpio.h:470
API that manipulates one digital IO pin of a given port.
Definition: gpio.h:90
void set() INLINE
Set pin level to HIGH (i.e.
Definition: gpio.h:155
void set_mode(PinMode mode, bool value=false) INLINE
Set mode (direction) and value (if output) of this pin.
Definition: gpio.h:139
static constexpr const board::Port PORT
The port to which this pin belongs.
Definition: gpio.h:101
static constexpr const uint8_t MASK
The bit-mask to use when accessing DPIN through PORT.
Definition: gpio.h:105
FastPin() INLINE
Construct a FastPin without any physical setup on target MCU.
Definition: gpio.h:112
bool value() INLINE
Return the current level of this pin.
Definition: gpio.h:184
void toggle() INLINE
Toggle pin level, i.e.
Definition: gpio.h:174
void clear() INLINE
Set pin level to LOW (i.e.
Definition: gpio.h:164
static constexpr const uint8_t BIT
The bit position (from 0 to 7), in port, of this pin.
Definition: gpio.h:103
FastPin(PinMode mode, bool value=false) INLINE
Construct a FastPin with the given mode and initial value.
Definition: gpio.h:126
API that manipulates a given digital IO pin of a the target MCU.
Definition: gpio.h:551
FastPin< PORT, BIT > TYPE
The exact FastPin parameterized type for DPIN IO pin.
Definition: gpio.h:569
static bool value()
Return the current level pin DPIN.
Definition: gpio.h:625
static void set()
Set pin level to HIGH (i.e.
Definition: gpio.h:596
static void clear()
Set pin level to LOW (i.e.
Definition: gpio.h:605
static void toggle()
Toggle pin level, i.e.
Definition: gpio.h:615
FastPort< PORT > PORT_TYPE
The exact FastPort parameterized type that DPIN IO pin belongs to.
Definition: gpio.h:571
static constexpr const uint8_t BIT
The bit position of DPIN within its port.
Definition: gpio.h:564
static constexpr const board::Port PORT
The port to which DPIN belongs.
Definition: gpio.h:562
static constexpr const uint8_t MASK
The bit-mask to use when accessing DPIN through PORT.
Definition: gpio.h:566
static constexpr const board::DigitalPin DPIN
The digital pin for this FastPinType.
Definition: gpio.h:560
static void set_mode(PinMode mode, bool value=false)
Set mode (direction) and value (if output) of DPIN.
Definition: gpio.h:580
API that manipulates a whole digital IO port.
Definition: gpio.h:204
FastPort()=default
Construct a FastPort without any physical setup on target MCU.
uint8_t get_PORT() INLINE
Get the current 8-bit value of port PORT register.
Definition: gpio.h:305
FastPin< PORT, BIT > get_pin(PinMode mode, bool value=false)
Create a FastPin instance for a given pin of this port, and sets its direction mode and level value (...
Definition: gpio.h:258
uint8_t get_PIN() INLINE
Get the 8-bits value of PIN register for this port, i.e.
Definition: gpio.h:352
void set_PIN(uint8_t pin) INLINE
Set the 8-bits value for port PIN register.
Definition: gpio.h:341
void set_PORT(uint8_t port) INLINE
Set the 8-bits value for port PORT register.
Definition: gpio.h:291
static constexpr const board::Port PORT
The actual port in target MCU.
Definition: gpio.h:215
void set_DDR(uint8_t ddr) INLINE
Set the 8-bits value for port DDR (direction) register.
Definition: gpio.h:319
uint8_t get_DDR() INLINE
Get the current 8-bit value of port DDR (direction) register.
Definition: gpio.h:330
FastPort(uint8_t ddr, uint8_t port=0) INLINE
Construct a FastPort with the given direction byte and initial values byte.
Definition: gpio.h:239
FastPin< PORT, BIT > get_pin()
Create a FastPin instance for a given pin of this port.
Definition: gpio.h:273
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
Definition: defines.h:45
General API for handling External Interrupt pins.
static constexpr uint8_t COMPL(uint8_t value)
Return the uint8_t 2-complement of a byte.
Definition: bits.h:253
static constexpr uint8_t CBV8(uint8_t bit)
Create a uint8_t inverted bitmask for the given bit number.
Definition: bits.h:137
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
Definition: bits.h:41
Defines all types and constants specific to support a specific MCU target.
Definition: empty.h:38
constexpr uint8_t BIT() INLINE
Determine the bit position, inside its IO port, of the given DigitalPin.
Definition: gpio.h:37
Port
Defines all available ports of the target MCU.
Definition: empty.h:49
DigitalPin
Defines all available digital input/output pins of the target MCU.
Definition: empty.h:56
constexpr uint8_t MASK() INLINE
Determine the bit mask representing the given DigitalPin inside its port.
Definition: gpio.h:45
Defines all API to manipulate general-purpose digital input/output pins.
Definition: gpio.h:55
typename FastPinType< DPIN_ >::TYPE FAST_PIN
Useful alias type to the FastPin type matching a given board::DigitalPin.
Definition: gpio.h:694
typename FastPinType< board::EXT_PIN< EPIN_ >()>::TYPE FAST_EXT_PIN
Useful alias type to the FastPin type matching a given board::ExternalInterruptPin.
Definition: gpio.h:740
typename FastPinType< board::PCI_PIN< IPIN_ >()>::TYPE FAST_INT_PIN
Useful alias type to the FastPin type matching a given board::InterruptPin.
Definition: gpio.h:717
PinMode
Defines the configurable mode of a digital IO pin.
Definition: gpio.h:60
@ OUTPUT
Digital pin is configured as output.
@ INPUT
Digital pin is configured as high-impedance (open drain) input.
@ INPUT_PULLUP
Digital pin is configured as input with an internal pullup resistor.
General API for handling Pin Change Interrupts.
General utilities API that have broad application in programs.