Fixed non-compliant code storing a ptr as a constexpr

This commit is contained in:
BlackMark 2019-08-10 13:16:02 +02:00
parent 4f40aa70dc
commit 1bc8a38988

127
io.hpp
View File

@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/sfr_defs.h>
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Preprocessor defines // Preprocessor defines
@ -125,46 +126,60 @@ enum class Bus {
namespace detail { namespace detail {
/*
The following works in avr-gcc 5.4.0, but is not legal C++, because ptr's are not legal constexpr's
constexpr auto *foo = ptr;
Workaround is to store the the address of the ptr in a uintptr_t and reinterpret_cast it at call site
For this to work we need to temporarily disable the _SFR_IO8 macro so that the register macro just gives the address
*/
#undef _SFR_IO8
#define _SFR_IO8
#ifdef PORT_A_AVAILABLE #ifdef PORT_A_AVAILABLE
static constexpr volatile uint8_t *PORT_A_DIR_REG = &DDRA; static constexpr uintptr_t PORT_A_DIR_REG_ADDR = DDRA + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_A_OUTPUT_REG = &PORTA; static constexpr uintptr_t PORT_A_OUTPUT_REG_ADDR = PORTA + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_A_INPUT_REG = &PINA; static constexpr uintptr_t PORT_A_INPUT_REG_ADDR = PINA + __SFR_OFFSET;
#else #else
static constexpr volatile uint8_t *PORT_A_DIR_REG = nullptr; static constexpr uintptr_t PORT_A_DIR_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_A_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_A_OUTPUT_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_A_INPUT_REG = nullptr; static constexpr uintptr_t PORT_A_INPUT_REG_ADDR = nullptr;
#endif #endif
#ifdef PORT_B_AVAILABLE #ifdef PORT_B_AVAILABLE
static constexpr volatile uint8_t *PORT_B_DIR_REG = &DDRB; static constexpr uintptr_t PORT_B_DIR_REG_ADDR = DDRB + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_B_OUTPUT_REG = &PORTB; static constexpr uintptr_t PORT_B_OUTPUT_REG_ADDR = PORTB + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_B_INPUT_REG = &PINB; static constexpr uintptr_t PORT_B_INPUT_REG_ADDR = PINB + __SFR_OFFSET;
#else #else
static constexpr volatile uint8_t *PORT_B_DIR_REG = nullptr; static constexpr uintptr_t PORT_B_DIR_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_B_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_B_OUTPUT_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_B_INPUT_REG = nullptr; static constexpr uintptr_t PORT_B_INPUT_REG_ADDR = nullptr;
#endif #endif
#ifdef PORT_C_AVAILABLE #ifdef PORT_C_AVAILABLE
static constexpr volatile uint8_t *PORT_C_DIR_REG = &DDRC; static constexpr uintptr_t PORT_C_DIR_REG_ADDR = DDRC + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_C_OUTPUT_REG = &PORTC; static constexpr uintptr_t PORT_C_OUTPUT_REG_ADDR = PORTC + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_C_INPUT_REG = &PINC; static constexpr uintptr_t PORT_C_INPUT_REG_ADDR = PINC + __SFR_OFFSET;
#else #else
static constexpr volatile uint8_t *PORT_C_DIR_REG = nullptr; static constexpr uintptr_t PORT_C_DIR_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_C_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_C_OUTPUT_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_C_INPUT_REG = nullptr; static constexpr uintptr_t PORT_C_INPUT_REG_ADDR = nullptr;
#endif #endif
#ifdef PORT_D_AVAILABLE #ifdef PORT_D_AVAILABLE
static constexpr volatile uint8_t *PORT_D_DIR_REG = &DDRD; static constexpr uintptr_t PORT_D_DIR_REG_ADDR = DDRD + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_D_OUTPUT_REG = &PORTD; static constexpr uintptr_t PORT_D_OUTPUT_REG_ADDR = PORTD + __SFR_OFFSET;
static constexpr volatile uint8_t *PORT_D_INPUT_REG = &PIND; static constexpr uintptr_t PORT_D_INPUT_REG_ADDR = PIND + __SFR_OFFSET;
#else #else
static constexpr volatile uint8_t *PORT_D_DIR_REG = nullptr; static constexpr uintptr_t PORT_D_DIR_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_D_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_D_OUTPUT_REG_ADDR = nullptr;
static constexpr volatile uint8_t *PORT_D_INPUT_REG = nullptr; static constexpr uintptr_t PORT_D_INPUT_REG_ADDR = nullptr;
#endif #endif
#undef _SFR_IO8
#define _SFR_IO8(io_addr) _MMIO_BYTE((io_addr) + __SFR_OFFSET)
static constexpr auto getBus(const P pin) static constexpr auto getBus(const P pin)
{ {
// Upper 4 bits of pin encode which port this pin is on // Upper 4 bits of pin encode which port this pin is on
@ -183,13 +198,13 @@ static constexpr auto getDDR(const Bus bus)
{ {
switch (static_cast<uint8_t>(bus)) { switch (static_cast<uint8_t>(bus)) {
case 0: // Bus::A case 0: // Bus::A
return PORT_A_DIR_REG; return PORT_A_DIR_REG_ADDR;
case 1: // Bus::B case 1: // Bus::B
return PORT_B_DIR_REG; return PORT_B_DIR_REG_ADDR;
case 2: // Bus::C case 2: // Bus::C
return PORT_C_DIR_REG; return PORT_C_DIR_REG_ADDR;
case 3: // Bus::D case 3: // Bus::D
return PORT_D_DIR_REG; return PORT_D_DIR_REG_ADDR;
} }
} }
@ -197,13 +212,13 @@ static constexpr auto getPORT(const Bus bus)
{ {
switch (static_cast<uint8_t>(bus)) { switch (static_cast<uint8_t>(bus)) {
case 0: // Bus::A case 0: // Bus::A
return PORT_A_OUTPUT_REG; return PORT_A_OUTPUT_REG_ADDR;
case 1: // Bus::B case 1: // Bus::B
return PORT_B_OUTPUT_REG; return PORT_B_OUTPUT_REG_ADDR;
case 2: // Bus::C case 2: // Bus::C
return PORT_C_OUTPUT_REG; return PORT_C_OUTPUT_REG_ADDR;
case 3: // Bus::D case 3: // Bus::D
return PORT_D_OUTPUT_REG; return PORT_D_OUTPUT_REG_ADDR;
} }
} }
@ -211,16 +226,22 @@ static constexpr auto getPIN(const Bus bus)
{ {
switch (static_cast<uint8_t>(bus)) { switch (static_cast<uint8_t>(bus)) {
case 0: // Bus::A case 0: // Bus::A
return PORT_A_INPUT_REG; return PORT_A_INPUT_REG_ADDR;
case 1: // Bus::B case 1: // Bus::B
return PORT_B_INPUT_REG; return PORT_B_INPUT_REG_ADDR;
case 2: // Bus::C case 2: // Bus::C
return PORT_C_INPUT_REG; return PORT_C_INPUT_REG_ADDR;
case 3: // Bus::D case 3: // Bus::D
return PORT_D_INPUT_REG; return PORT_D_INPUT_REG_ADDR;
} }
} }
template <uintptr_t Address>
static inline volatile uint8_t *getRegPtr()
{
return reinterpret_cast<volatile uint8_t *>(Address);
}
} // namespace detail } // namespace detail
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -245,9 +266,9 @@ class Pin {
constexpr auto pinBit = detail::getPinBit(pin); constexpr auto pinBit = detail::getPinBit(pin);
if (dir == Dir::IN) if (dir == Dir::IN)
*dirReg &= ~(1 << pinBit); *detail::getRegPtr<dirReg>() &= ~(1 << pinBit);
else if (dir == Dir::OUT) else if (dir == Dir::OUT)
*dirReg |= (1 << pinBit); *detail::getRegPtr<dirReg>() |= (1 << pinBit);
} }
static inline void pullup(const bool enable) FORCE_INLINE static inline void pullup(const bool enable) FORCE_INLINE
@ -257,9 +278,9 @@ class Pin {
constexpr auto pinBit = detail::getPinBit(pin); constexpr auto pinBit = detail::getPinBit(pin);
if (enable) if (enable)
*portReg |= (1 << pinBit); *detail::getRegPtr<portReg>() |= (1 << pinBit);
else else
*portReg &= ~(1 << pinBit); *detail::getRegPtr<portReg>() &= ~(1 << pinBit);
} }
static inline void write(const bool value) FORCE_INLINE static inline void write(const bool value) FORCE_INLINE
@ -269,9 +290,9 @@ class Pin {
constexpr auto pinBit = detail::getPinBit(pin); constexpr auto pinBit = detail::getPinBit(pin);
if (value) if (value)
*portReg |= (1 << pinBit); *detail::getRegPtr<portReg>() |= (1 << pinBit);
else else
*portReg &= ~(1 << pinBit); *detail::getRegPtr<portReg>() &= ~(1 << pinBit);
} }
static inline void toggle() FORCE_INLINE static inline void toggle() FORCE_INLINE
@ -281,13 +302,13 @@ class Pin {
constexpr auto pinReg = detail::getPIN(bus); constexpr auto pinReg = detail::getPIN(bus);
constexpr auto pinBit = detail::getPinBit(pin); constexpr auto pinBit = detail::getPinBit(pin);
*pinReg |= (1 << pinBit); *detail::getRegPtr<pinReg>() |= (1 << pinBit);
#else #else
constexpr auto bus = detail::getBus(pin); constexpr auto bus = detail::getBus(pin);
constexpr auto portReg = detail::getPORT(bus); constexpr auto portReg = detail::getPORT(bus);
constexpr auto pinBit = detail::getPinBit(pin); constexpr auto pinBit = detail::getPinBit(pin);
*portReg ^= (1 << pinBit); *detail::getRegPtr<pinReg>() ^= (1 << pinBit);
#endif #endif
} }
@ -297,7 +318,7 @@ class Pin {
constexpr auto pinReg = detail::getPIN(bus); constexpr auto pinReg = detail::getPIN(bus);
constexpr auto pinBit = detail::getPinBit(pin); constexpr auto pinBit = detail::getPinBit(pin);
if (*pinReg >> pinBit & 1) if (*detail::getRegPtr<pinReg>() >> pinBit & 1)
return true; return true;
return false; return false;
@ -335,9 +356,9 @@ class Port {
constexpr auto dirReg = detail::getDDR(port); constexpr auto dirReg = detail::getDDR(port);
if (dir == Dir::IN) if (dir == Dir::IN)
*dirReg = 0x00; *detail::getRegPtr<dirReg>() = 0x00;
else if (dir == Dir::OUT) else if (dir == Dir::OUT)
*dirReg = 0xFF; *detail::getRegPtr<dirReg>() = 0xFF;
} }
static inline void pullup(const bool enable) FORCE_INLINE static inline void pullup(const bool enable) FORCE_INLINE
@ -345,16 +366,16 @@ class Port {
constexpr auto portReg = detail::getPORT(port); constexpr auto portReg = detail::getPORT(port);
if (enable) if (enable)
*portReg = 0xFF; *detail::getRegPtr<portReg>() = 0xFF;
else else
*portReg = 0x00; *detail::getRegPtr<portReg>() = 0x00;
} }
static inline void write(const uint8_t value) FORCE_INLINE static inline void write(const uint8_t value) FORCE_INLINE
{ {
constexpr auto portReg = detail::getPORT(port); constexpr auto portReg = detail::getPORT(port);
*portReg = value; *detail::getRegPtr<portReg>() = value;
} }
static inline void invert() FORCE_INLINE static inline void invert() FORCE_INLINE
@ -362,11 +383,11 @@ class Port {
#ifdef HARDWARE_TOGGLE #ifdef HARDWARE_TOGGLE
constexpr auto pinReg = detail::getPIN(port); constexpr auto pinReg = detail::getPIN(port);
*pinReg = 0xFF; *detail::getRegPtr<pinReg>() = 0xFF;
#else #else
constexpr auto portReg = detail::getPORT(port); constexpr auto portReg = detail::getPORT(port);
*portReg = ~(*portReg); *detail::getRegPtr<portReg>() = ~(*portReg);
#endif #endif
} }
@ -374,7 +395,7 @@ class Port {
{ {
constexpr auto pinReg = detail::getPIN(port); constexpr auto pinReg = detail::getPIN(port);
return *pinReg; return *detail::getRegPtr<pinReg>();
} }
inline Port &operator=(const uint8_t value) FORCE_INLINE inline Port &operator=(const uint8_t value) FORCE_INLINE