Compare commits
2 Commits
d89322bdaa
...
5407e94337
| Author | SHA1 | Date | |
|---|---|---|---|
| 5407e94337 | |||
| bf94ebaac1 |
83
io.hpp
83
io.hpp
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <cstdint>
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/sfr_defs.h>
|
||||
@@ -188,20 +188,20 @@ static constexpr uintptr_t PORT_D_INPUT_REG_ADDR = 0;
|
||||
static constexpr auto getBus(const P pin)
|
||||
{
|
||||
// Upper 4 bits of pin encode which port this pin is on
|
||||
uint8_t port = static_cast<uint8_t>(pin) >> 4 & 0x0F;
|
||||
const auto port = static_cast<std::uint8_t>(pin) >> 4 & 0x0F;
|
||||
return static_cast<Bus>(port);
|
||||
}
|
||||
|
||||
static constexpr auto getPinBit(const P pin)
|
||||
{
|
||||
// Lower 4 bits of pin encode which pin bit it is
|
||||
uint8_t pinBit = static_cast<uint8_t>(pin) & 0x0F;
|
||||
const auto pinBit = static_cast<std::uint8_t>(pin) & 0x0F;
|
||||
return pinBit;
|
||||
}
|
||||
|
||||
static constexpr auto getDDR(const Bus bus)
|
||||
{
|
||||
switch (static_cast<uint8_t>(bus)) {
|
||||
switch (static_cast<std::uint8_t>(bus)) {
|
||||
case 0: // Bus::A
|
||||
return PORT_A_DIR_REG_ADDR;
|
||||
case 1: // Bus::B
|
||||
@@ -215,7 +215,7 @@ static constexpr auto getDDR(const Bus bus)
|
||||
|
||||
static constexpr auto getPORT(const Bus bus)
|
||||
{
|
||||
switch (static_cast<uint8_t>(bus)) {
|
||||
switch (static_cast<std::uint8_t>(bus)) {
|
||||
case 0: // Bus::A
|
||||
return PORT_A_OUTPUT_REG_ADDR;
|
||||
case 1: // Bus::B
|
||||
@@ -229,7 +229,7 @@ static constexpr auto getPORT(const Bus bus)
|
||||
|
||||
static constexpr auto getPIN(const Bus bus)
|
||||
{
|
||||
switch (static_cast<uint8_t>(bus)) {
|
||||
switch (static_cast<std::uint8_t>(bus)) {
|
||||
case 0: // Bus::A
|
||||
return PORT_A_INPUT_REG_ADDR;
|
||||
case 1: // Bus::B
|
||||
@@ -241,7 +241,7 @@ static constexpr auto getPIN(const Bus bus)
|
||||
}
|
||||
}
|
||||
|
||||
using reg_ptr_t = volatile uint8_t *;
|
||||
using reg_ptr_t = volatile std::uint8_t *;
|
||||
|
||||
template <uintptr_t Address>
|
||||
static inline reg_ptr_t getRegPtr()
|
||||
@@ -249,7 +249,7 @@ static inline reg_ptr_t getRegPtr()
|
||||
return reinterpret_cast<reg_ptr_t>(Address);
|
||||
}
|
||||
|
||||
template <template <P, uint8_t> typename Func, P pin, P... pins>
|
||||
template <template <P, std::uint8_t> typename Func, P pin, P... pins>
|
||||
struct CallHelper {
|
||||
template <typename... Args>
|
||||
[[gnu::always_inline]] static inline void call(Args... args)
|
||||
@@ -259,7 +259,7 @@ struct CallHelper {
|
||||
}
|
||||
};
|
||||
|
||||
template <template <P, uint8_t> typename Func, P pin>
|
||||
template <template <P, std::uint8_t> typename Func, P pin>
|
||||
struct CallHelper<Func, pin> {
|
||||
template <typename... Args>
|
||||
[[gnu::always_inline]] static inline void call(Args... args)
|
||||
@@ -270,7 +270,7 @@ struct CallHelper<Func, pin> {
|
||||
|
||||
template <template <P> typename Func, P pin, P... pins>
|
||||
struct ReadCallHelper {
|
||||
[[gnu::always_inline]] static inline uint8_t call()
|
||||
[[gnu::always_inline]] static inline std::uint8_t call()
|
||||
{
|
||||
return Func<pin>::call() << sizeof...(pins) | ReadCallHelper<Func, pins...>::call();
|
||||
}
|
||||
@@ -278,7 +278,7 @@ struct ReadCallHelper {
|
||||
|
||||
template <template <P> typename Func, P pin>
|
||||
struct ReadCallHelper<Func, pin> {
|
||||
[[gnu::always_inline]] static inline uint8_t call()
|
||||
[[gnu::always_inline]] static inline std::uint8_t call()
|
||||
{
|
||||
return Func<pin>::call();
|
||||
}
|
||||
@@ -307,12 +307,12 @@ class Pin {
|
||||
constexpr auto bus = detail::getBus(pin);
|
||||
constexpr auto pinBit = detail::getPinBit(pin);
|
||||
|
||||
detail::reg_ptr_t dirRegPtr = detail::getRegPtr<detail::getDDR(bus)>();
|
||||
auto dirRegPtr = detail::getRegPtr<detail::getDDR(bus)>();
|
||||
|
||||
if (dir == Dir::IN)
|
||||
*dirRegPtr &= ~(1 << pinBit);
|
||||
*dirRegPtr = *dirRegPtr & ~(1 << pinBit);
|
||||
else if (dir == Dir::OUT)
|
||||
*dirRegPtr |= (1 << pinBit);
|
||||
*dirRegPtr = *dirRegPtr | (1 << pinBit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,12 +322,12 @@ class Pin {
|
||||
constexpr auto bus = detail::getBus(pin);
|
||||
constexpr auto pinBit = detail::getPinBit(pin);
|
||||
|
||||
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
||||
auto portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
||||
|
||||
if (enable)
|
||||
*portRegPtr |= (1 << pinBit);
|
||||
*portRegPtr = *portRegPtr | (1 << pinBit);
|
||||
else
|
||||
*portRegPtr &= ~(1 << pinBit);
|
||||
*portRegPtr = *portRegPtr & ~(1 << pinBit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,12 +337,12 @@ class Pin {
|
||||
constexpr auto bus = detail::getBus(pin);
|
||||
constexpr auto pinBit = detail::getPinBit(pin);
|
||||
|
||||
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
||||
auto portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
||||
|
||||
if (value)
|
||||
*portRegPtr |= (1 << pinBit);
|
||||
*portRegPtr = *portRegPtr | (1 << pinBit);
|
||||
else
|
||||
*portRegPtr &= ~(1 << pinBit);
|
||||
*portRegPtr = *portRegPtr & ~(1 << pinBit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,11 +353,11 @@ class Pin {
|
||||
constexpr auto pinBit = detail::getPinBit(pin);
|
||||
|
||||
#ifdef HARDWARE_TOGGLE
|
||||
detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
|
||||
*pinRegPtr |= (1 << pinBit);
|
||||
auto pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
|
||||
*pinRegPtr = *pinRegPtr | (1 << pinBit);
|
||||
#else
|
||||
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
||||
*portRegPtr ^= (1 << pinBit);
|
||||
auto portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
|
||||
*portRegPtr = *portRegPtr ^ (1 << pinBit);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -368,7 +368,7 @@ class Pin {
|
||||
constexpr auto bus = detail::getBus(pin);
|
||||
constexpr auto pinBit = detail::getPinBit(pin);
|
||||
|
||||
detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
|
||||
auto pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
|
||||
|
||||
if (*pinRegPtr >> pinBit & 1)
|
||||
return true;
|
||||
@@ -406,7 +406,7 @@ class Port {
|
||||
[[gnu::always_inline]] static inline void dir(const Dir dir)
|
||||
{
|
||||
if constexpr (port != Bus::NONE) {
|
||||
detail::reg_ptr_t dirRegPtr = detail::getRegPtr<detail::getDDR(port)>();
|
||||
auto dirRegPtr = detail::getRegPtr<detail::getDDR(port)>();
|
||||
|
||||
if (dir == Dir::IN)
|
||||
*dirRegPtr = 0x00;
|
||||
@@ -418,7 +418,7 @@ class Port {
|
||||
[[gnu::always_inline]] static inline void pullup(const bool enable)
|
||||
{
|
||||
if constexpr (port != Bus::NONE) {
|
||||
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
||||
auto portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
||||
|
||||
if (enable)
|
||||
*portRegPtr = 0xFF;
|
||||
@@ -427,10 +427,10 @@ class Port {
|
||||
}
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static inline void write([[maybe_unused]] const uint8_t value)
|
||||
[[gnu::always_inline]] static inline void write([[maybe_unused]] const std::uint8_t value)
|
||||
{
|
||||
if constexpr (port != Bus::NONE) {
|
||||
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
||||
auto portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
||||
*portRegPtr = value;
|
||||
}
|
||||
}
|
||||
@@ -439,32 +439,31 @@ class Port {
|
||||
{
|
||||
if constexpr (port != Bus::NONE) {
|
||||
#ifdef HARDWARE_TOGGLE
|
||||
detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
|
||||
auto pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
|
||||
*pinRegPtr = 0xFF;
|
||||
#else
|
||||
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
||||
auto portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
|
||||
*portRegPtr = ~(*portRegPtr);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static inline uint8_t read()
|
||||
[[gnu::always_inline]] static inline std::uint8_t read()
|
||||
{
|
||||
if constexpr (port != Bus::NONE) {
|
||||
detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
|
||||
|
||||
auto pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
|
||||
return *pinRegPtr;
|
||||
}
|
||||
return 0x00;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] inline Port &operator=(const uint8_t value)
|
||||
[[gnu::always_inline]] inline Port &operator=(const std::uint8_t value)
|
||||
{
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] inline operator uint8_t() const
|
||||
[[gnu::always_inline]] inline operator std::uint8_t() const
|
||||
{
|
||||
return read();
|
||||
}
|
||||
@@ -475,7 +474,7 @@ class Port {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <P pin, uint8_t offset>
|
||||
template <P pin, std::uint8_t offset>
|
||||
struct Callers {
|
||||
[[gnu::always_inline]] static void call(const Dir dir)
|
||||
{
|
||||
@@ -487,7 +486,7 @@ struct Callers {
|
||||
Pin<pin>::pullup(enable);
|
||||
};
|
||||
|
||||
[[gnu::always_inline]] static void call(const uint8_t value)
|
||||
[[gnu::always_inline]] static void call(const std::uint8_t value)
|
||||
{
|
||||
Pin<pin>::write(value >> offset & 1);
|
||||
};
|
||||
@@ -532,7 +531,7 @@ class VirtPort {
|
||||
detail::CallHelper<detail::Callers, pins...>::call(enable);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static inline void write(const uint8_t value)
|
||||
[[gnu::always_inline]] static inline void write(const std::uint8_t value)
|
||||
{
|
||||
detail::CallHelper<detail::Callers, pins...>::call(value);
|
||||
}
|
||||
@@ -542,18 +541,18 @@ class VirtPort {
|
||||
detail::CallHelper<detail::Callers, pins...>::call();
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static inline uint8_t read()
|
||||
[[gnu::always_inline]] static inline std::uint8_t read()
|
||||
{
|
||||
return detail::ReadCallHelper<detail::readCaller, pins...>::call();
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] inline VirtPort &operator=(const uint8_t value)
|
||||
[[gnu::always_inline]] inline VirtPort &operator=(const std::uint8_t value)
|
||||
{
|
||||
write(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] inline operator uint8_t() const
|
||||
[[gnu::always_inline]] inline operator std::uint8_t() const
|
||||
{
|
||||
return read();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user