diff --git a/src/avclan/avclan.h b/src/avclan/avclan.h index 572fa3a..da7fc97 100644 --- a/src/avclan/avclan.h +++ b/src/avclan/avclan.h @@ -3,54 +3,52 @@ #pragma once -#ifdef __cplusplus - #include - #define AVCLAN_ENUM_CLASS enum class -#else - #include - #define AVCLAN_ENUM_CLASS enum -#endif +#include #ifdef __cplusplus + #define AVCLAN_ENUM_CLASS class namespace avclan::detail { struct Error { +#else + #define AVCLAN_ENUM_CLASS #endif // Error enums are ordered such that a lower numeric value corresponds to // more progress/success before an error occured, with 0 being no errors - AVCLAN_ENUM_CLASS Read : uint8_t { - BAD_DATA_PARITY = 0x01, - BAD_LENGTH_RANGE, - BAD_LENGTH_PARITY, - BAD_PERIPHERAL_PARITY, - BAD_CONTROLLER_PARITY, - BAD_CONTROL_PARITY, - BAD_PARITY, // non-specific bad parity - STARTBIT_TOO_SHORT, - STARTBIT_TOO_LONG, - BAD_STARTBIT, -} -// #ifndef __cplusplus -// Read -// #endif -; + enum AVCLAN_ENUM_CLASS Read : uint8_t { + BAD_DATA_PARITY = 0x01, + BAD_LENGTH_RANGE, + BAD_LENGTH_PARITY, + BAD_PERIPHERAL_PARITY, + BAD_CONTROLLER_PARITY, + BAD_CONTROL_PARITY, + BAD_PARITY, // non-specific bad parity + STARTBIT_TOO_SHORT, + STARTBIT_TOO_LONG, + BAD_STARTBIT, + }; - AVCLAN_ENUM_CLASS Send : uint8_t { - NAK_DATA = 0x01, - NAK_MESSAGE_LENGTH, - NAK_CONTROL, - NAK_ADDRESS, - NAK, // non-specific NAK - BUSY, - MUTED, -} -// #ifndef __cplusplus -// Send -// #endif -; + enum AVCLAN_ENUM_CLASS Send : uint8_t { + NAK_DATA = 0x01, + NAK_MESSAGE_LENGTH, + NAK_CONTROL, + NAK_ADDRESS, + NAK, // non-specific NAK + BUSY, + MUTED, + }; #ifdef __cplusplus }; +#endif + +enum AVCLAN_ENUM_CLASS Bit : uint8_t { + bit_zero = 0x00, + bit_one = 0x01, + bit_start = 0x10 +}; + +#ifdef __cplusplus } // namespace avclan::detail #endif diff --git a/src/avclan/avclan_defs.h b/src/avclan/avclan_defs.h index 7de56b9..9c59194 100644 --- a/src/avclan/avclan_defs.h +++ b/src/avclan/avclan_defs.h @@ -103,11 +103,3 @@ typedef enum : uint8_t { Loading_Status_Report = 0xf3, // Typically unprompted, sent to dev_STATUS Report_TOC = 0xf9, } actions; - -// A single bus symbol. bit_zero/bit_one carry data (and double as parity -// values); bit_start marks a frame start bit. -typedef enum avclan_bit : uint8_t { - bit_zero = 0x00, - bit_one = 0x01, - bit_start = 0x10 -} avclan_bit_t; diff --git a/src/avclan/avclan_phy.h b/src/avclan/avclan_phy.h index 441f6f9..bf53ce6 100644 --- a/src/avclan/avclan_phy.h +++ b/src/avclan/avclan_phy.h @@ -8,13 +8,14 @@ #include #include "avclan.h" -#include "avclan_defs.h" #ifdef __cplusplus using Read = avclan::detail::Error::Read; +using Bit = avclan::detail::Bit; extern "C" { #else typedef enum Read Read; +typedef enum Bit Bit; #endif // One-time bring-up of the bus hardware. Leaves the bus idle and TX unmuted. @@ -45,13 +46,13 @@ bool AVCLAN_sendstartbit(void); // Per-symbol I/O. The send* helpers return the even parity of the bits sent; // the read* helpers return the even parity of the bits read. -void AVCLAN_sendbit(avclan_bit_t bit); +void AVCLAN_sendbit(Bit bit); void AVCLAN_sendbit_ACK(void); uint8_t AVCLAN_readbit_ACK(void); -avclan_bit_t AVCLAN_sendbitsi(const uint8_t *bits, int8_t len); -avclan_bit_t AVCLAN_sendbitsl(const uint16_t *bits, int8_t len); -avclan_bit_t AVCLAN_sendbyte(const uint8_t *byte); +Bit AVCLAN_sendbitsi(const uint8_t *bits, int8_t len); +Bit AVCLAN_sendbitsl(const uint16_t *bits, int8_t len); +Bit AVCLAN_sendbyte(const uint8_t *byte); uint8_t AVCLAN_readbitsi(uint8_t *bits, uint8_t len); uint8_t AVCLAN_readbitsl(uint16_t *bits, int8_t len); diff --git a/src/avclan/bus.cc b/src/avclan/bus.cc index f6e5ab0..40e12d9 100644 --- a/src/avclan/bus.cc +++ b/src/avclan/bus.cc @@ -31,7 +31,6 @@ #include "bus.hpp" #include "avclan.h" -#include "avclan_defs.h" #include "avclan_phy.h" // bridge until phy has been ported #include "com232.h" #include "frame.hpp" @@ -284,16 +283,16 @@ auto Bus::Handle::readstartbit() -> Read { return AVCLAN_readstartbit(); }; void Bus::Handle::send_ACK() { AVCLAN_sendbit_ACK(); }; uint8_t Bus::Handle::read_ACK() { return AVCLAN_readbit_ACK(); }; -template <> inline avclan_bit_t Bus::Handle::sendbits<8>(uint8_t bits) { +template <> inline Bit Bus::Handle::sendbits<8>(uint8_t bits) { return AVCLAN_sendbyte(&bits); }; -template <> inline avclan_bit_t Bus::Handle::sendbits<1>(uint8_t bits) { - const avclan_bit_t bit{static_cast(bits & 1U)}; +template <> inline Bit Bus::Handle::sendbits<1>(uint8_t bits) { + const Bit bit{static_cast(bits & 1U)}; AVCLAN_sendbit(bit); return bit; }; -template <> inline avclan_bit_t Bus::Handle::readbits<8>(uint8_t *bits) { - return static_cast(AVCLAN_readbyte(bits)); +template <> inline Bit Bus::Handle::readbits<8>(uint8_t *bits) { + return static_cast(AVCLAN_readbyte(bits)); }; } // namespace avclan diff --git a/src/avclan/bus.hpp b/src/avclan/bus.hpp index f7888cf..dc4fc96 100644 --- a/src/avclan/bus.hpp +++ b/src/avclan/bus.hpp @@ -48,10 +48,10 @@ #pragma once #include +#include #include #include "avclan.h" -#include "avclan_defs.h" #include "avclan_phy.h" // bridge until phy has been ported #include "frame.hpp" @@ -125,7 +125,7 @@ public: if constexpr (std::is_same_v) { uint8_t read_parity; readbits<1>(&read_parity); - if (calc_parity != read_parity) + if (static_cast(calc_parity) != read_parity) return Read::BAD_PARITY; } return Read{0}; @@ -155,42 +155,35 @@ public: private: using Read = Error::Read; using Send = Error::Send; - - // A single bus symbol. bit_zero/bit_one carry data (and double as parity - // values); bit_start marks a frame start bit. - enum class avclan_bit : uint8_t { - bit_zero = 0x00, - bit_one = 0x01, - bit_start = 0x10 - }; + using Bit = detail::Bit; static void send_ACK(); static uint8_t read_ACK(); - template avclan_bit_t sendbits(T bits); - template avclan_bit_t readbits(T *bits); + template Bit sendbits(T bits); + template Bit readbits(T *bits); // Temporary specializations bridging to legacy C API // Replace with proper (single?) template when phy has been ported template requires(N > 1 && N < 8) - avclan_bit_t sendbits(uint8_t bits) { + Bit sendbits(uint8_t bits) { return AVCLAN_sendbitsi(&bits, N); }; template requires(N <= 16) - avclan_bit_t sendbits(uint16_t bits) { + Bit sendbits(uint16_t bits) { return AVCLAN_sendbitsl(&bits, N); }; template requires(N < 8) - avclan_bit_t readbits(uint8_t *bits) { - return static_cast(AVCLAN_readbitsi(bits, N)); + Bit readbits(uint8_t *bits) { + return static_cast(AVCLAN_readbitsi(bits, N)); }; template requires(N <= 16) - avclan_bit_t readbits(uint16_t *bits) { - return static_cast(AVCLAN_readbitsl(bits, N)); + Bit readbits(uint16_t *bits) { + return static_cast(AVCLAN_readbitsl(bits, N)); }; }; diff --git a/src/avclan/target/avr-attiny3216/phy_avr.c b/src/avclan/target/avr-attiny3216/phy_avr.c index 6d1523c..0585c4c 100644 --- a/src/avclan/target/avr-attiny3216/phy_avr.c +++ b/src/avclan/target/avr-attiny3216/phy_avr.c @@ -99,7 +99,7 @@ static void set_AVC_logic_for(uint8_t val, uint16_t period) { return; } -void AVCLAN_sendbit(avclan_bit_t bit) { +void AVCLAN_sendbit(Bit bit) { uint16_t zero_length, one_length; switch (bit) { case bit_zero: @@ -160,7 +160,7 @@ uint8_t AVCLAN_readbit_ACK() { } // Send `len` bits on the AVCLAN bus; returns the even parity -avclan_bit_t AVCLAN_sendbitsi(const uint8_t *bits, int8_t len) { +Bit AVCLAN_sendbitsi(const uint8_t *bits, int8_t len) { uint8_t b = *bits; uint8_t parity = 0; int8_t len_mod8 = 8; @@ -173,7 +173,7 @@ avclan_bit_t AVCLAN_sendbitsi(const uint8_t *bits, int8_t len) { while (len > 0) { len -= len_mod8; for (; len_mod8 > 0; len_mod8--) { - avclan_bit_t bit = (b & 0x80) != 0; + Bit bit = (b & 0x80) != 0; parity += (uint8_t)bit; AVCLAN_sendbit(bit); b <<= 1; @@ -185,16 +185,16 @@ avclan_bit_t AVCLAN_sendbitsi(const uint8_t *bits, int8_t len) { } // Send `len` bits on the AVCLAN bus; returns the even parity -avclan_bit_t AVCLAN_sendbitsl(const uint16_t *bits, int8_t len) { +Bit AVCLAN_sendbitsl(const uint16_t *bits, int8_t len) { return AVCLAN_sendbitsi((const uint8_t *)bits + 1, len); } -avclan_bit_t AVCLAN_sendbyte(const uint8_t *byte) { +Bit AVCLAN_sendbyte(const uint8_t *byte) { uint8_t b = *byte; uint8_t parity = 0; for (uint8_t nbits = 8; nbits > 0; nbits--) { - avclan_bit_t bit = (b & 0x80) != 0; + Bit bit = (b & 0x80) != 0; parity += (uint8_t)bit; AVCLAN_sendbit(bit); b <<= 1;