2019-07-26 18:49:53 +02:00
|
|
|
#pragma once
|
2016-02-25 21:48:49 +01:00
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
#include <cstdint>
|
2018-04-24 18:59:12 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
#include <avr/io.h>
|
2019-08-10 13:16:02 +02:00
|
|
|
#include <avr/sfr_defs.h>
|
2016-02-25 21:48:49 +01:00
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Preprocessor defines
|
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#if defined(__AVR_ATmega32__) || defined(__AVR_ATmega32A__) || defined(__AVR_ATmega644P__) || \
|
|
|
|
defined(__AVR_ATmega1284P__)
|
|
|
|
#define GPIO_32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8A__) || defined(__AVR_ATmega168A__) || defined(__AVR_ATmega328P__)
|
|
|
|
#define GPIO_23
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__AVR_ATtiny13A__) || defined(__AVR_ATtiny85__)
|
|
|
|
#define GPIO_6
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega168A__) || \
|
|
|
|
defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny13A__) || defined(__AVR_ATtiny85__)
|
|
|
|
#define HARDWARE_TOGGLE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef GPIO_32
|
|
|
|
#define PORT_A_AVAILABLE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(GPIO_32) || defined(GPIO_23) || defined(GPIO_6)
|
|
|
|
#define PORT_B_AVAILABLE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(GPIO_32) || defined(GPIO_23)
|
|
|
|
#define PORT_C_AVAILABLE
|
|
|
|
#define PORT_D_AVAILABLE
|
|
|
|
#define PIN_B6_AVAILABLE
|
|
|
|
#define PIN_B7_AVAILABLE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(GPIO_32)
|
|
|
|
#define PIN_C7_AVAILABLE
|
|
|
|
#endif
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2018-04-24 18:59:12 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2019-07-26 18:49:53 +02:00
|
|
|
// Library implementation
|
|
|
|
|
|
|
|
namespace io {
|
|
|
|
|
|
|
|
enum class Dir { IN, OUT };
|
|
|
|
|
|
|
|
enum class P {
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_A_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
A0 = 0x00,
|
|
|
|
A1 = 0x01,
|
|
|
|
A2 = 0x02,
|
|
|
|
A3 = 0x03,
|
|
|
|
A4 = 0x04,
|
|
|
|
A5 = 0x05,
|
|
|
|
A6 = 0x06,
|
|
|
|
A7 = 0x07,
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_B_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
B0 = 0x10,
|
|
|
|
B1 = 0x11,
|
|
|
|
B2 = 0x12,
|
|
|
|
B3 = 0x13,
|
|
|
|
B4 = 0x14,
|
|
|
|
B5 = 0x15,
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PIN_B6_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
B6 = 0x16,
|
|
|
|
#endif
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PIN_B7_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
B7 = 0x17,
|
|
|
|
#endif
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_C_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
C0 = 0x20,
|
|
|
|
C1 = 0x21,
|
|
|
|
C2 = 0x22,
|
|
|
|
C3 = 0x23,
|
|
|
|
C4 = 0x24,
|
|
|
|
C5 = 0x25,
|
|
|
|
C6 = 0x26,
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PIN_C7_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
C7 = 0x27,
|
|
|
|
#endif
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_D_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
D0 = 0x30,
|
|
|
|
D1 = 0x31,
|
|
|
|
D2 = 0x32,
|
|
|
|
D3 = 0x33,
|
|
|
|
D4 = 0x34,
|
|
|
|
D5 = 0x35,
|
|
|
|
D6 = 0x36,
|
|
|
|
D7 = 0x37,
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
2022-05-28 15:41:26 +02:00
|
|
|
|
|
|
|
NONE,
|
2019-07-26 18:49:53 +02:00
|
|
|
};
|
2016-02-25 21:48:49 +01:00
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
enum class Bus {
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_A_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
A = 0x00,
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_B_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
B = 0x01,
|
2018-04-24 18:59:12 +02:00
|
|
|
#endif
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_C_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
C = 0x02,
|
2018-04-24 18:59:12 +02:00
|
|
|
#endif
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_D_AVAILABLE
|
2019-07-26 18:49:53 +02:00
|
|
|
D = 0x03,
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
2022-05-28 15:41:26 +02:00
|
|
|
|
|
|
|
NONE,
|
2019-07-26 18:49:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation details
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2019-08-10 13:16:02 +02:00
|
|
|
/*
|
2019-08-15 18:49:09 +02:00
|
|
|
The following works in avr-gcc 5.4.0, but is not legal C++, because ptr's are not legal constexpr's:
|
2019-08-10 13:16:02 +02:00
|
|
|
constexpr auto *foo = ptr;
|
|
|
|
|
2020-02-01 22:28:44 +01:00
|
|
|
Workaround is to store the address of the ptr in a uintptr_t and reinterpret_cast it at call site.
|
2019-08-15 18:49:09 +02:00
|
|
|
The _SFR_ADDR macro in sfr_defs.h would give the address, but it does that by taking the address of the dereferenced
|
|
|
|
pointer and casts it to uint16_t, which is still not a legal constexpr.
|
2020-02-01 22:28:44 +01:00
|
|
|
The workaround therefore is to disable the pointer-cast-and-dereference macro _MMIO_BYTE temporarily.
|
2019-08-10 13:16:02 +02:00
|
|
|
*/
|
|
|
|
|
2019-08-15 18:49:09 +02:00
|
|
|
#pragma push_macro("_MMIO_BYTE")
|
|
|
|
#undef _MMIO_BYTE
|
|
|
|
#define _MMIO_BYTE
|
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_A_AVAILABLE
|
2019-08-15 18:49:09 +02:00
|
|
|
static constexpr uintptr_t PORT_A_DIR_REG_ADDR = DDRA;
|
|
|
|
static constexpr uintptr_t PORT_A_OUTPUT_REG_ADDR = PORTA;
|
|
|
|
static constexpr uintptr_t PORT_A_INPUT_REG_ADDR = PINA;
|
2019-07-26 18:49:53 +02:00
|
|
|
#else
|
2019-08-10 13:58:42 +02:00
|
|
|
static constexpr uintptr_t PORT_A_DIR_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_A_OUTPUT_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_A_INPUT_REG_ADDR = 0;
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
2018-04-24 18:59:12 +02:00
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_B_AVAILABLE
|
2019-08-15 17:37:13 +02:00
|
|
|
|
2019-08-15 18:49:09 +02:00
|
|
|
static constexpr uintptr_t PORT_B_DIR_REG_ADDR = DDRB;
|
|
|
|
static constexpr uintptr_t PORT_B_OUTPUT_REG_ADDR = PORTB;
|
|
|
|
static constexpr uintptr_t PORT_B_INPUT_REG_ADDR = PINB;
|
2019-07-26 18:49:53 +02:00
|
|
|
#else
|
2019-08-10 13:58:42 +02:00
|
|
|
static constexpr uintptr_t PORT_B_DIR_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_B_OUTPUT_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_B_INPUT_REG_ADDR = 0;
|
2018-04-24 18:59:12 +02:00
|
|
|
#endif
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_C_AVAILABLE
|
2019-08-15 18:49:09 +02:00
|
|
|
static constexpr uintptr_t PORT_C_DIR_REG_ADDR = DDRC;
|
|
|
|
static constexpr uintptr_t PORT_C_OUTPUT_REG_ADDR = PORTC;
|
|
|
|
static constexpr uintptr_t PORT_C_INPUT_REG_ADDR = PINC;
|
2019-07-26 18:49:53 +02:00
|
|
|
#else
|
2019-08-10 13:58:42 +02:00
|
|
|
static constexpr uintptr_t PORT_C_DIR_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_C_OUTPUT_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_C_INPUT_REG_ADDR = 0;
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef PORT_D_AVAILABLE
|
2019-08-15 18:49:09 +02:00
|
|
|
static constexpr uintptr_t PORT_D_DIR_REG_ADDR = DDRD;
|
|
|
|
static constexpr uintptr_t PORT_D_OUTPUT_REG_ADDR = PORTD;
|
|
|
|
static constexpr uintptr_t PORT_D_INPUT_REG_ADDR = PIND;
|
2019-07-26 18:49:53 +02:00
|
|
|
#else
|
2019-08-10 13:58:42 +02:00
|
|
|
static constexpr uintptr_t PORT_D_DIR_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_D_OUTPUT_REG_ADDR = 0;
|
|
|
|
static constexpr uintptr_t PORT_D_INPUT_REG_ADDR = 0;
|
2016-02-25 21:48:49 +01:00
|
|
|
#endif
|
|
|
|
|
2019-08-15 18:49:09 +02:00
|
|
|
#pragma pop_macro("_MMIO_BYTE")
|
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
static constexpr auto getBus(const P pin)
|
|
|
|
{
|
|
|
|
// Upper 4 bits of pin encode which port this pin is on
|
2022-05-29 16:14:22 +02:00
|
|
|
const auto port = static_cast<std::uint8_t>(pin) >> 4 & 0x0F;
|
2019-07-26 18:49:53 +02:00
|
|
|
return static_cast<Bus>(port);
|
|
|
|
}
|
|
|
|
|
2019-08-10 12:55:53 +02:00
|
|
|
static constexpr auto getPinBit(const P pin)
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
|
|
|
// Lower 4 bits of pin encode which pin bit it is
|
2022-05-29 16:14:22 +02:00
|
|
|
const auto pinBit = static_cast<std::uint8_t>(pin) & 0x0F;
|
2019-07-26 18:49:53 +02:00
|
|
|
return pinBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto getDDR(const Bus bus)
|
|
|
|
{
|
2022-05-29 16:14:22 +02:00
|
|
|
switch (static_cast<std::uint8_t>(bus)) {
|
2019-07-26 18:49:53 +02:00
|
|
|
case 0: // Bus::A
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_A_DIR_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 1: // Bus::B
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_B_DIR_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 2: // Bus::C
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_C_DIR_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 3: // Bus::D
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_D_DIR_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto getPORT(const Bus bus)
|
|
|
|
{
|
2022-05-29 16:14:22 +02:00
|
|
|
switch (static_cast<std::uint8_t>(bus)) {
|
2019-07-26 18:49:53 +02:00
|
|
|
case 0: // Bus::A
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_A_OUTPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 1: // Bus::B
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_B_OUTPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 2: // Bus::C
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_C_OUTPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 3: // Bus::D
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_D_OUTPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto getPIN(const Bus bus)
|
|
|
|
{
|
2022-05-29 16:14:22 +02:00
|
|
|
switch (static_cast<std::uint8_t>(bus)) {
|
2019-07-26 18:49:53 +02:00
|
|
|
case 0: // Bus::A
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_A_INPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 1: // Bus::B
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_B_INPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 2: // Bus::C
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_C_INPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
case 3: // Bus::D
|
2019-08-10 13:16:02 +02:00
|
|
|
return PORT_D_INPUT_REG_ADDR;
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
using reg_ptr_t = volatile std::uint8_t *;
|
2019-08-10 13:36:50 +02:00
|
|
|
|
2019-08-10 13:16:02 +02:00
|
|
|
template <uintptr_t Address>
|
2019-08-10 13:36:50 +02:00
|
|
|
static inline reg_ptr_t getRegPtr()
|
2019-08-10 13:16:02 +02:00
|
|
|
{
|
2019-08-10 13:36:50 +02:00
|
|
|
return reinterpret_cast<reg_ptr_t>(Address);
|
2019-08-10 13:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
template <template <P, std::uint8_t> typename Func, P pin, P... pins>
|
2020-02-01 22:07:52 +01:00
|
|
|
struct CallHelper {
|
|
|
|
template <typename... Args>
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void call(Args... args)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
Func<pin, sizeof...(pins)>::call(args...);
|
|
|
|
CallHelper<Func, pins...>::call(args...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
template <template <P, std::uint8_t> typename Func, P pin>
|
2020-02-01 22:07:52 +01:00
|
|
|
struct CallHelper<Func, pin> {
|
|
|
|
template <typename... Args>
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void call(Args... args)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
Func<pin, 0>::call(args...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <template <P> typename Func, P pin, P... pins>
|
|
|
|
struct ReadCallHelper {
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static inline std::uint8_t call()
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
return Func<pin>::call() << sizeof...(pins) | ReadCallHelper<Func, pins...>::call();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <template <P> typename Func, P pin>
|
|
|
|
struct ReadCallHelper<Func, pin> {
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static inline std::uint8_t call()
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
return Func<pin>::call();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Zero overhead Pin object for pretty code without losing performance
|
|
|
|
|
2019-07-28 14:00:12 +02:00
|
|
|
template <P pin>
|
2019-07-26 18:49:53 +02:00
|
|
|
class Pin {
|
|
|
|
public:
|
|
|
|
// Pin objects cannot be moved or copied
|
|
|
|
Pin(const Pin &) = delete;
|
|
|
|
Pin(Pin &&) = delete;
|
|
|
|
Pin &operator=(const Pin &) = delete;
|
|
|
|
Pin &operator=(Pin &&) = delete;
|
|
|
|
|
|
|
|
// The only valid way to create a Pin object is with the default constructor
|
|
|
|
Pin() = default;
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void dir(const Dir dir)
|
2016-02-25 21:48:49 +01:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (pin != P::NONE) {
|
|
|
|
constexpr auto bus = detail::getBus(pin);
|
|
|
|
constexpr auto pinBit = detail::getPinBit(pin);
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2022-05-29 14:45:05 +02:00
|
|
|
auto dirRegPtr = detail::getRegPtr<detail::getDDR(bus)>();
|
2019-08-10 13:32:35 +02:00
|
|
|
|
2022-05-28 15:41:26 +02:00
|
|
|
if (dir == Dir::IN)
|
2022-05-29 14:45:05 +02:00
|
|
|
*dirRegPtr = *dirRegPtr & ~(1 << pinBit);
|
2022-05-28 15:41:26 +02:00
|
|
|
else if (dir == Dir::OUT)
|
2022-05-29 14:45:05 +02:00
|
|
|
*dirRegPtr = *dirRegPtr | (1 << pinBit);
|
2022-05-28 15:41:26 +02:00
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2016-02-25 21:48:49 +01:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void pullup(const bool enable)
|
2016-02-25 21:48:49 +01:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (pin != P::NONE) {
|
|
|
|
constexpr auto bus = detail::getBus(pin);
|
|
|
|
constexpr auto pinBit = detail::getPinBit(pin);
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2022-05-29 14:45:05 +02:00
|
|
|
auto portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
2019-08-10 13:32:35 +02:00
|
|
|
|
2022-05-28 15:41:26 +02:00
|
|
|
if (enable)
|
2022-05-29 14:45:05 +02:00
|
|
|
*portRegPtr = *portRegPtr | (1 << pinBit);
|
2022-05-28 15:41:26 +02:00
|
|
|
else
|
2022-05-29 14:45:05 +02:00
|
|
|
*portRegPtr = *portRegPtr & ~(1 << pinBit);
|
2022-05-28 15:41:26 +02:00
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void write(const bool value)
|
2016-02-25 21:48:49 +01:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (pin != P::NONE) {
|
|
|
|
constexpr auto bus = detail::getBus(pin);
|
|
|
|
constexpr auto pinBit = detail::getPinBit(pin);
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2022-05-29 14:45:05 +02:00
|
|
|
auto portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
2019-08-10 13:32:35 +02:00
|
|
|
|
2022-05-28 15:41:26 +02:00
|
|
|
if (value)
|
2022-05-29 14:45:05 +02:00
|
|
|
*portRegPtr = *portRegPtr | (1 << pinBit);
|
2022-05-28 15:41:26 +02:00
|
|
|
else
|
2022-05-29 14:45:05 +02:00
|
|
|
*portRegPtr = *portRegPtr & ~(1 << pinBit);
|
2022-05-28 15:41:26 +02:00
|
|
|
}
|
2016-02-25 21:48:49 +01:00
|
|
|
}
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void toggle()
|
2018-04-26 15:23:42 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (pin != P::NONE) {
|
|
|
|
constexpr auto bus = detail::getBus(pin);
|
|
|
|
constexpr auto pinBit = detail::getPinBit(pin);
|
2019-07-26 19:44:20 +02:00
|
|
|
|
2019-08-10 13:32:35 +02:00
|
|
|
#ifdef HARDWARE_TOGGLE
|
2022-05-29 14:45:05 +02:00
|
|
|
auto pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
|
|
|
|
*pinRegPtr = *pinRegPtr | (1 << pinBit);
|
2019-07-26 19:44:20 +02:00
|
|
|
#else
|
2022-05-29 14:45:05 +02:00
|
|
|
auto portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
|
|
|
*portRegPtr = *portRegPtr ^ (1 << pinBit);
|
2019-07-26 19:44:20 +02:00
|
|
|
#endif
|
2022-05-28 15:41:26 +02:00
|
|
|
}
|
2018-04-26 15:23:42 +02:00
|
|
|
}
|
2018-04-24 18:59:12 +02:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline bool read()
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (pin != P::NONE) {
|
|
|
|
constexpr auto bus = detail::getBus(pin);
|
|
|
|
constexpr auto pinBit = detail::getPinBit(pin);
|
2019-08-10 13:32:35 +02:00
|
|
|
|
2022-05-29 14:45:05 +02:00
|
|
|
auto pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
|
2019-07-26 18:49:53 +02:00
|
|
|
|
2022-05-28 15:41:26 +02:00
|
|
|
if (*pinRegPtr >> pinBit & 1)
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] Pin &operator=(const bool value)
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
|
|
|
write(value);
|
|
|
|
return *this;
|
|
|
|
}
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] operator bool() const
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
|
|
|
return read();
|
|
|
|
}
|
2018-04-24 18:59:12 +02:00
|
|
|
};
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2018-04-24 18:59:12 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2019-07-26 18:49:53 +02:00
|
|
|
// Zero overhead Port object for pretty code without losing performance
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2019-07-28 14:00:12 +02:00
|
|
|
template <Bus port>
|
2019-07-26 18:49:53 +02:00
|
|
|
class Port {
|
|
|
|
public:
|
|
|
|
// Port objects cannot be moved or copied
|
|
|
|
Port(const Port &) = delete;
|
|
|
|
Port(Port &&) = delete;
|
|
|
|
Port &operator=(const Port &) = delete;
|
|
|
|
Port &operator=(Port &&) = delete;
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
// The only valid way to create a Port object is with the default constructor
|
|
|
|
Port() = default;
|
2018-04-24 18:59:12 +02:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void dir(const Dir dir)
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (port != Bus::NONE) {
|
2022-05-29 14:45:05 +02:00
|
|
|
auto dirRegPtr = detail::getRegPtr<detail::getDDR(port)>();
|
2022-05-28 15:41:26 +02:00
|
|
|
|
|
|
|
if (dir == Dir::IN)
|
|
|
|
*dirRegPtr = 0x00;
|
|
|
|
else if (dir == Dir::OUT)
|
|
|
|
*dirRegPtr = 0xFF;
|
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2018-04-26 15:23:42 +02:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void pullup(const bool enable)
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (port != Bus::NONE) {
|
2022-05-29 14:45:05 +02:00
|
|
|
auto portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
2022-05-28 15:41:26 +02:00
|
|
|
|
|
|
|
if (enable)
|
|
|
|
*portRegPtr = 0xFF;
|
|
|
|
else
|
|
|
|
*portRegPtr = 0x00;
|
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2018-04-26 15:23:42 +02:00
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static inline void write([[maybe_unused]] const std::uint8_t value)
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (port != Bus::NONE) {
|
2022-05-29 14:45:05 +02:00
|
|
|
auto portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
2022-05-28 15:41:26 +02:00
|
|
|
*portRegPtr = value;
|
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2019-01-02 20:54:29 +01:00
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void invert()
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (port != Bus::NONE) {
|
2019-08-10 12:54:23 +02:00
|
|
|
#ifdef HARDWARE_TOGGLE
|
2022-05-29 14:45:05 +02:00
|
|
|
auto pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
|
2022-05-28 15:41:26 +02:00
|
|
|
*pinRegPtr = 0xFF;
|
2019-07-26 19:48:43 +02:00
|
|
|
#else
|
2022-05-29 14:45:05 +02:00
|
|
|
auto portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
2022-05-28 15:41:26 +02:00
|
|
|
*portRegPtr = ~(*portRegPtr);
|
2019-07-26 19:48:43 +02:00
|
|
|
#endif
|
2022-05-28 15:41:26 +02:00
|
|
|
}
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2018-04-24 18:59:12 +02:00
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static inline std::uint8_t read()
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
2022-05-28 15:41:26 +02:00
|
|
|
if constexpr (port != Bus::NONE) {
|
2022-05-29 14:45:05 +02:00
|
|
|
auto pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
|
2022-05-28 15:41:26 +02:00
|
|
|
return *pinRegPtr;
|
|
|
|
}
|
|
|
|
return 0x00;
|
2019-07-26 18:49:53 +02:00
|
|
|
}
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] inline Port &operator=(const std::uint8_t value)
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
|
|
|
write(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] inline operator std::uint8_t() const
|
2019-07-26 18:49:53 +02:00
|
|
|
{
|
|
|
|
return read();
|
|
|
|
}
|
2018-04-24 18:59:12 +02:00
|
|
|
};
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2020-02-01 22:07:52 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Zero overhead Virtual Port object for pretty code without losing performance
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
template <P pin, std::uint8_t offset>
|
2020-02-01 22:07:52 +01:00
|
|
|
struct Callers {
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static void call(const Dir dir)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
Pin<pin>::dir(dir);
|
|
|
|
};
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static void call(const bool enable)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
Pin<pin>::pullup(enable);
|
|
|
|
};
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static void call(const std::uint8_t value)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
Pin<pin>::write(value >> offset & 1);
|
|
|
|
};
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static void call()
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
Pin<pin>::toggle();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <P pin>
|
|
|
|
struct readCaller {
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static bool call()
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
return Pin<pin>::read();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <P... pins>
|
|
|
|
class VirtPort {
|
|
|
|
public:
|
2020-02-01 22:13:27 +01:00
|
|
|
static_assert(sizeof...(pins) <= 8, "A virtual port cannot have more than 8 pins");
|
|
|
|
|
2020-02-01 22:07:52 +01:00
|
|
|
// VirtPort objects cannot be moved or copied
|
|
|
|
VirtPort(const VirtPort &) = delete;
|
|
|
|
VirtPort(VirtPort &&) = delete;
|
|
|
|
VirtPort &operator=(const VirtPort &) = delete;
|
|
|
|
VirtPort &operator=(VirtPort &&) = delete;
|
|
|
|
|
|
|
|
// The only valid way to create a VirtPort object is with the default constructor
|
|
|
|
VirtPort() = default;
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void dir(const Dir dir)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
detail::CallHelper<detail::Callers, pins...>::call(dir);
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void pullup(const bool enable)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
detail::CallHelper<detail::Callers, pins...>::call(enable);
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static inline void write(const std::uint8_t value)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
detail::CallHelper<detail::Callers, pins...>::call(value);
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:51:40 +02:00
|
|
|
[[gnu::always_inline]] static inline void invert()
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
detail::CallHelper<detail::Callers, pins...>::call();
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] static inline std::uint8_t read()
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
return detail::ReadCallHelper<detail::readCaller, pins...>::call();
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] inline VirtPort &operator=(const std::uint8_t value)
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
write(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-29 16:14:22 +02:00
|
|
|
[[gnu::always_inline]] inline operator std::uint8_t() const
|
2020-02-01 22:07:52 +01:00
|
|
|
{
|
|
|
|
return read();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
} // namespace io
|
2016-05-24 19:41:59 +02:00
|
|
|
|
2018-04-24 18:59:12 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
#undef GPIO_32
|
|
|
|
#undef GPIO_23
|
|
|
|
#undef GPIO_6
|
2018-04-24 18:59:12 +02:00
|
|
|
|
2019-07-26 18:49:53 +02:00
|
|
|
#undef PORT_A_AVAILABLE
|
|
|
|
#undef PORT_B_AVAILABLE
|
|
|
|
#undef PORT_C_AVAILABLE
|
|
|
|
#undef PORT_D_AVAILABLE
|
|
|
|
|
|
|
|
#undef PIN_B6_AVAILABLE
|
|
|
|
#undef PIN_B7_AVAILABLE
|
|
|
|
#undef PIN_C7_AVAILABLE
|