Compare commits

...

7 Commits

254
io.hpp
View File

@@ -3,29 +3,47 @@
#include <stdint.h> #include <stdint.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/sfr_defs.h>
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Preprocessor defines // Preprocessor defines
#define GPIO_32 \ #if defined(__AVR_ATmega32__) || defined(__AVR_ATmega32A__) || defined(__AVR_ATmega644P__) || \
defined(__AVR_ATmega32__) || defined(__AVR_ATmega32A__) || defined(__AVR_ATmega644P__) || \
defined(__AVR_ATmega1284P__) defined(__AVR_ATmega1284P__)
#define GPIO_23 \ #define GPIO_32
defined(__AVR_ATmega8__) || defined(__AVR_ATmega8A__) || defined(__AVR_ATmega168A__) || defined(__AVR_ATmega328P__) #endif
#define GPIO_6 defined(__AVR_ATtiny13A__) || defined(__AVR_ATtiny85__)
#define HARDWARE_TOGGLE \ #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8A__) || defined(__AVR_ATmega168A__) || defined(__AVR_ATmega328P__)
defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega168A__) || \ #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__) defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny13A__) || defined(__AVR_ATtiny85__)
#define HARDWARE_TOGGLE
#endif
#define PORT_A_AVAILABLE GPIO_32 #ifdef GPIO_32
#define PORT_B_AVAILABLE GPIO_32 || GPIO_23 || GPIO_6 #define PORT_A_AVAILABLE
#define PORT_C_AVAILABLE GPIO_32 || GPIO_23 #endif
#define PORT_D_AVAILABLE GPIO_32 || GPIO_23
#define PIN_B6_AVAILABLE GPIO_32 || GPIO_23 #if defined(GPIO_32) || defined(GPIO_23) || defined(GPIO_6)
#define PIN_B7_AVAILABLE GPIO_32 || GPIO_23 #define PORT_B_AVAILABLE
#define PIN_C7_AVAILABLE GPIO_32 #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
#define FORCE_INLINE __attribute__((always_inline)) #define FORCE_INLINE __attribute__((always_inline))
@@ -37,7 +55,7 @@ namespace io {
enum class Dir { IN, OUT }; enum class Dir { IN, OUT };
enum class P { enum class P {
#if PORT_A_AVAILABLE #ifdef PORT_A_AVAILABLE
A0 = 0x00, A0 = 0x00,
A1 = 0x01, A1 = 0x01,
A2 = 0x02, A2 = 0x02,
@@ -48,22 +66,22 @@ enum class P {
A7 = 0x07, A7 = 0x07,
#endif #endif
#if PORT_B_AVAILABLE #ifdef PORT_B_AVAILABLE
B0 = 0x10, B0 = 0x10,
B1 = 0x11, B1 = 0x11,
B2 = 0x12, B2 = 0x12,
B3 = 0x13, B3 = 0x13,
B4 = 0x14, B4 = 0x14,
B5 = 0x15, B5 = 0x15,
#if PIN_B6_AVAILABLE #ifdef PIN_B6_AVAILABLE
B6 = 0x16, B6 = 0x16,
#endif #endif
#if PIN_B7_AVAILABLE #ifdef PIN_B7_AVAILABLE
B7 = 0x17, B7 = 0x17,
#endif #endif
#endif #endif
#if PORT_C_AVAILABLE #ifdef PORT_C_AVAILABLE
C0 = 0x20, C0 = 0x20,
C1 = 0x21, C1 = 0x21,
C2 = 0x22, C2 = 0x22,
@@ -71,12 +89,12 @@ enum class P {
C4 = 0x24, C4 = 0x24,
C5 = 0x25, C5 = 0x25,
C6 = 0x26, C6 = 0x26,
#if PIN_C7_AVAILABLE #ifdef PIN_C7_AVAILABLE
C7 = 0x27, C7 = 0x27,
#endif #endif
#endif #endif
#if PORT_D_AVAILABLE #ifdef PORT_D_AVAILABLE
D0 = 0x30, D0 = 0x30,
D1 = 0x31, D1 = 0x31,
D2 = 0x32, D2 = 0x32,
@@ -89,16 +107,16 @@ enum class P {
}; };
enum class Bus { enum class Bus {
#if PORT_A_AVAILABLE #ifdef PORT_A_AVAILABLE
A = 0x00, A = 0x00,
#endif #endif
#if PORT_B_AVAILABLE #ifdef PORT_B_AVAILABLE
B = 0x01, B = 0x01,
#endif #endif
#if PORT_C_AVAILABLE #ifdef PORT_C_AVAILABLE
C = 0x02, C = 0x02,
#endif #endif
#if PORT_D_AVAILABLE #ifdef PORT_D_AVAILABLE
D = 0x03, D = 0x03,
#endif #endif
}; };
@@ -108,46 +126,60 @@ enum class Bus {
namespace detail { namespace detail {
#if PORT_A_AVAILABLE /*
static constexpr volatile uint8_t *PORT_A_DIR_REG = &DDRA; The following works in avr-gcc 5.4.0, but is not legal C++, because ptr's are not legal constexpr's
static constexpr volatile uint8_t *PORT_A_OUTPUT_REG = &PORTA; constexpr auto *foo = ptr;
static constexpr volatile uint8_t *PORT_A_INPUT_REG = &PINA;
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
static constexpr uintptr_t PORT_A_DIR_REG_ADDR = DDRA + __SFR_OFFSET;
static constexpr uintptr_t PORT_A_OUTPUT_REG_ADDR = PORTA + __SFR_OFFSET;
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 = 0;
static constexpr volatile uint8_t *PORT_A_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_A_OUTPUT_REG_ADDR = 0;
static constexpr volatile uint8_t *PORT_A_INPUT_REG = nullptr; static constexpr uintptr_t PORT_A_INPUT_REG_ADDR = 0;
#endif #endif
#if 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 = 0;
static constexpr volatile uint8_t *PORT_B_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_B_OUTPUT_REG_ADDR = 0;
static constexpr volatile uint8_t *PORT_B_INPUT_REG = nullptr; static constexpr uintptr_t PORT_B_INPUT_REG_ADDR = 0;
#endif #endif
#if 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 = 0;
static constexpr volatile uint8_t *PORT_C_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_C_OUTPUT_REG_ADDR = 0;
static constexpr volatile uint8_t *PORT_C_INPUT_REG = nullptr; static constexpr uintptr_t PORT_C_INPUT_REG_ADDR = 0;
#endif #endif
#if 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 = 0;
static constexpr volatile uint8_t *PORT_D_OUTPUT_REG = nullptr; static constexpr uintptr_t PORT_D_OUTPUT_REG_ADDR = 0;
static constexpr volatile uint8_t *PORT_D_INPUT_REG = nullptr; static constexpr uintptr_t PORT_D_INPUT_REG_ADDR = 0;
#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
@@ -155,7 +187,7 @@ static constexpr auto getBus(const P pin)
return static_cast<Bus>(port); return static_cast<Bus>(port);
} }
static constexpr auto getPin(const P pin) static constexpr auto getPinBit(const P pin)
{ {
// Lower 4 bits of pin encode which pin bit it is // Lower 4 bits of pin encode which pin bit it is
uint8_t pinBit = static_cast<uint8_t>(pin) & 0x0F; uint8_t pinBit = static_cast<uint8_t>(pin) & 0x0F;
@@ -166,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;
} }
} }
@@ -180,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;
} }
} }
@@ -194,16 +226,24 @@ 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;
} }
} }
using reg_ptr_t = volatile uint8_t *;
template <uintptr_t Address>
static inline reg_ptr_t getRegPtr()
{
return reinterpret_cast<reg_ptr_t>(Address);
}
} // namespace detail } // namespace detail
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@@ -224,63 +264,64 @@ class Pin {
static inline void dir(const Dir dir) FORCE_INLINE static inline void dir(const Dir dir) FORCE_INLINE
{ {
constexpr auto bus = detail::getBus(pin); constexpr auto bus = detail::getBus(pin);
constexpr auto dirReg = detail::getDDR(bus); constexpr auto pinBit = detail::getPinBit(pin);
constexpr auto pinBit = detail::getPin(pin);
detail::reg_ptr_t dirRegPtr = detail::getRegPtr<detail::getDDR(bus)>();
if (dir == Dir::IN) if (dir == Dir::IN)
*dirReg &= ~(1 << pinBit); *dirRegPtr &= ~(1 << pinBit);
else if (dir == Dir::OUT) else if (dir == Dir::OUT)
*dirReg |= (1 << pinBit); *dirRegPtr |= (1 << pinBit);
} }
static inline void pullup(const bool enable) FORCE_INLINE static inline void pullup(const bool enable) FORCE_INLINE
{ {
constexpr auto bus = detail::getBus(pin); constexpr auto bus = detail::getBus(pin);
constexpr auto portReg = detail::getPORT(bus); constexpr auto pinBit = detail::getPinBit(pin);
constexpr auto pinBit = detail::getPin(pin);
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
if (enable) if (enable)
*portReg |= (1 << pinBit); *portRegPtr |= (1 << pinBit);
else else
*portReg &= ~(1 << pinBit); *portRegPtr &= ~(1 << pinBit);
} }
static inline void write(const bool value) FORCE_INLINE static inline void write(const bool value) FORCE_INLINE
{ {
constexpr auto bus = detail::getBus(pin); constexpr auto bus = detail::getBus(pin);
constexpr auto portReg = detail::getPORT(bus); constexpr auto pinBit = detail::getPinBit(pin);
constexpr auto pinBit = detail::getPin(pin);
detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
if (value) if (value)
*portReg |= (1 << pinBit); *portRegPtr |= (1 << pinBit);
else else
*portReg &= ~(1 << pinBit); *portRegPtr &= ~(1 << pinBit);
} }
static inline void toggle() FORCE_INLINE static inline void toggle() FORCE_INLINE
{ {
#if HARDWARE_TOGGLE
constexpr auto bus = detail::getBus(pin); constexpr auto bus = detail::getBus(pin);
constexpr auto pinReg = detail::getPIN(bus); constexpr auto pinBit = detail::getPinBit(pin);
constexpr auto pinBit = detail::getPin(pin);
*pinReg |= (1 << pinBit); #ifdef HARDWARE_TOGGLE
detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
*pinRegPtr |= (1 << pinBit);
#else #else
constexpr auto bus = detail::getBus(pin); detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(bus)>();
constexpr auto portReg = detail::getPORT(bus); *portRegPtr ^= (1 << pinBit);
constexpr auto pinBit = detail::getPin(pin);
*portReg ^= (1 << pinBit);
#endif #endif
} }
static inline bool read() FORCE_INLINE static inline bool read() FORCE_INLINE
{ {
constexpr auto bus = detail::getBus(pin); constexpr auto bus = detail::getBus(pin);
constexpr auto pinReg = detail::getPIN(bus); constexpr auto pinBit = detail::getPinBit(pin);
constexpr auto pinBit = detail::getPin(pin);
if (*pinReg >> pinBit & 1) detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(bus)>();
if (*pinRegPtr >> pinBit & 1)
return true; return true;
return false; return false;
@@ -315,49 +356,46 @@ class Port {
static inline void dir(const Dir dir) FORCE_INLINE static inline void dir(const Dir dir) FORCE_INLINE
{ {
constexpr auto dirReg = detail::getDDR(port); detail::reg_ptr_t dirRegPtr = detail::getRegPtr<detail::getDDR(port)>();
if (dir == Dir::IN) if (dir == Dir::IN)
*dirReg = 0x00; *dirRegPtr = 0x00;
else if (dir == Dir::OUT) else if (dir == Dir::OUT)
*dirReg = 0xFF; *dirRegPtr = 0xFF;
} }
static inline void pullup(const bool enable) FORCE_INLINE static inline void pullup(const bool enable) FORCE_INLINE
{ {
constexpr auto portReg = detail::getPORT(port); detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
if (enable) if (enable)
*portReg = 0xFF; *portRegPtr = 0xFF;
else else
*portReg = 0x00; *portRegPtr = 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); detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
*portRegPtr = value;
*portReg = value;
} }
static inline void invert() FORCE_INLINE static inline void invert() FORCE_INLINE
{ {
#if HARDWARE_TOGGLE #ifdef HARDWARE_TOGGLE
constexpr auto pinReg = detail::getPIN(port); detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
*pinRegPtr = 0xFF;
*pinReg = 0xFF;
#else #else
constexpr auto portReg = detail::getPORT(port); detail::reg_ptr_t portRegPtr = detail::getRegPtr<detail::getPORT(port)>();
*portRegPtr = ~(*portRegPtr);
*portReg = ~(*portReg);
#endif #endif
} }
static inline uint8_t read() FORCE_INLINE static inline uint8_t read() FORCE_INLINE
{ {
constexpr auto pinReg = detail::getPIN(port); detail::reg_ptr_t pinRegPtr = detail::getRegPtr<detail::getPIN(port)>();
return *pinReg; return *pinRegPtr;
} }
inline Port &operator=(const uint8_t value) FORCE_INLINE inline Port &operator=(const uint8_t value) FORCE_INLINE