Shift avclan_bit_t to avclan.h

This commit is contained in:
Allen Hill
2026-07-06 13:55:13 -07:00
parent a0c6601cda
commit 1c0124edcc
6 changed files with 62 additions and 79 deletions
+16 -18
View File
@@ -3,22 +3,19 @@
#pragma once #pragma once
#ifdef __cplusplus
#include <cstdint>
#define AVCLAN_ENUM_CLASS enum class
#else
#include <stdint.h> #include <stdint.h>
#define AVCLAN_ENUM_CLASS enum
#endif
#ifdef __cplusplus #ifdef __cplusplus
#define AVCLAN_ENUM_CLASS class
namespace avclan::detail { namespace avclan::detail {
struct Error { struct Error {
#else
#define AVCLAN_ENUM_CLASS
#endif #endif
// Error enums are ordered such that a lower numeric value corresponds to // Error enums are ordered such that a lower numeric value corresponds to
// more progress/success before an error occured, with 0 being no errors // more progress/success before an error occured, with 0 being no errors
AVCLAN_ENUM_CLASS Read : uint8_t { enum AVCLAN_ENUM_CLASS Read : uint8_t {
BAD_DATA_PARITY = 0x01, BAD_DATA_PARITY = 0x01,
BAD_LENGTH_RANGE, BAD_LENGTH_RANGE,
BAD_LENGTH_PARITY, BAD_LENGTH_PARITY,
@@ -29,13 +26,9 @@ struct Error {
STARTBIT_TOO_SHORT, STARTBIT_TOO_SHORT,
STARTBIT_TOO_LONG, STARTBIT_TOO_LONG,
BAD_STARTBIT, BAD_STARTBIT,
} };
// #ifndef __cplusplus
// Read
// #endif
;
AVCLAN_ENUM_CLASS Send : uint8_t { enum AVCLAN_ENUM_CLASS Send : uint8_t {
NAK_DATA = 0x01, NAK_DATA = 0x01,
NAK_MESSAGE_LENGTH, NAK_MESSAGE_LENGTH,
NAK_CONTROL, NAK_CONTROL,
@@ -43,14 +36,19 @@ struct Error {
NAK, // non-specific NAK NAK, // non-specific NAK
BUSY, BUSY,
MUTED, MUTED,
} };
// #ifndef __cplusplus
// Send
// #endif
;
#ifdef __cplusplus #ifdef __cplusplus
}; };
#endif
enum AVCLAN_ENUM_CLASS Bit : uint8_t {
bit_zero = 0x00,
bit_one = 0x01,
bit_start = 0x10
};
#ifdef __cplusplus
} // namespace avclan::detail } // namespace avclan::detail
#endif #endif
-8
View File
@@ -103,11 +103,3 @@ typedef enum : uint8_t {
Loading_Status_Report = 0xf3, // Typically unprompted, sent to dev_STATUS Loading_Status_Report = 0xf3, // Typically unprompted, sent to dev_STATUS
Report_TOC = 0xf9, Report_TOC = 0xf9,
} actions; } 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;
+6 -5
View File
@@ -8,13 +8,14 @@
#include <stdint.h> #include <stdint.h>
#include "avclan.h" #include "avclan.h"
#include "avclan_defs.h"
#ifdef __cplusplus #ifdef __cplusplus
using Read = avclan::detail::Error::Read; using Read = avclan::detail::Error::Read;
using Bit = avclan::detail::Bit;
extern "C" { extern "C" {
#else #else
typedef enum Read Read; typedef enum Read Read;
typedef enum Bit Bit;
#endif #endif
// One-time bring-up of the bus hardware. Leaves the bus idle and TX unmuted. // 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; // 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. // 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); void AVCLAN_sendbit_ACK(void);
uint8_t AVCLAN_readbit_ACK(void); uint8_t AVCLAN_readbit_ACK(void);
avclan_bit_t AVCLAN_sendbitsi(const uint8_t *bits, int8_t len); Bit AVCLAN_sendbitsi(const uint8_t *bits, int8_t len);
avclan_bit_t AVCLAN_sendbitsl(const uint16_t *bits, int8_t len); Bit AVCLAN_sendbitsl(const uint16_t *bits, int8_t len);
avclan_bit_t AVCLAN_sendbyte(const uint8_t *byte); Bit AVCLAN_sendbyte(const uint8_t *byte);
uint8_t AVCLAN_readbitsi(uint8_t *bits, uint8_t len); uint8_t AVCLAN_readbitsi(uint8_t *bits, uint8_t len);
uint8_t AVCLAN_readbitsl(uint16_t *bits, int8_t len); uint8_t AVCLAN_readbitsl(uint16_t *bits, int8_t len);
+5 -6
View File
@@ -31,7 +31,6 @@
#include "bus.hpp" #include "bus.hpp"
#include "avclan.h" #include "avclan.h"
#include "avclan_defs.h"
#include "avclan_phy.h" // bridge until phy has been ported #include "avclan_phy.h" // bridge until phy has been ported
#include "com232.h" #include "com232.h"
#include "frame.hpp" #include "frame.hpp"
@@ -284,16 +283,16 @@ auto Bus::Handle::readstartbit() -> Read { return AVCLAN_readstartbit(); };
void Bus::Handle::send_ACK() { AVCLAN_sendbit_ACK(); }; void Bus::Handle::send_ACK() { AVCLAN_sendbit_ACK(); };
uint8_t Bus::Handle::read_ACK() { return AVCLAN_readbit_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); return AVCLAN_sendbyte(&bits);
}; };
template <> inline avclan_bit_t Bus::Handle::sendbits<1>(uint8_t bits) { template <> inline Bit Bus::Handle::sendbits<1>(uint8_t bits) {
const avclan_bit_t bit{static_cast<avclan_bit_t>(bits & 1U)}; const Bit bit{static_cast<Bit>(bits & 1U)};
AVCLAN_sendbit(bit); AVCLAN_sendbit(bit);
return bit; return bit;
}; };
template <> inline avclan_bit_t Bus::Handle::readbits<8>(uint8_t *bits) { template <> inline Bit Bus::Handle::readbits<8>(uint8_t *bits) {
return static_cast<avclan_bit_t>(AVCLAN_readbyte(bits)); return static_cast<Bit>(AVCLAN_readbyte(bits));
}; };
} // namespace avclan } // namespace avclan
+11 -18
View File
@@ -48,10 +48,10 @@
#pragma once #pragma once
#include <concepts> #include <concepts>
#include <cstdint>
#include <type_traits> #include <type_traits>
#include "avclan.h" #include "avclan.h"
#include "avclan_defs.h"
#include "avclan_phy.h" // bridge until phy has been ported #include "avclan_phy.h" // bridge until phy has been ported
#include "frame.hpp" #include "frame.hpp"
@@ -125,7 +125,7 @@ public:
if constexpr (std::is_same_v<Trailer, with_parity_t>) { if constexpr (std::is_same_v<Trailer, with_parity_t>) {
uint8_t read_parity; uint8_t read_parity;
readbits<1>(&read_parity); readbits<1>(&read_parity);
if (calc_parity != read_parity) if (static_cast<uint8_t>(calc_parity) != read_parity)
return Read::BAD_PARITY; return Read::BAD_PARITY;
} }
return Read{0}; return Read{0};
@@ -155,42 +155,35 @@ public:
private: private:
using Read = Error::Read; using Read = Error::Read;
using Send = Error::Send; using Send = Error::Send;
using Bit = detail::Bit;
// 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
};
static void send_ACK(); static void send_ACK();
static uint8_t read_ACK(); static uint8_t read_ACK();
template <auto N, class T> avclan_bit_t sendbits(T bits); template <auto N, class T> Bit sendbits(T bits);
template <auto N, class T> avclan_bit_t readbits(T *bits); template <auto N, class T> Bit readbits(T *bits);
// Temporary specializations bridging to legacy C API // Temporary specializations bridging to legacy C API
// Replace with proper (single?) template when phy has been ported // Replace with proper (single?) template when phy has been ported
template <auto N> template <auto N>
requires(N > 1 && N < 8) requires(N > 1 && N < 8)
avclan_bit_t sendbits(uint8_t bits) { Bit sendbits(uint8_t bits) {
return AVCLAN_sendbitsi(&bits, N); return AVCLAN_sendbitsi(&bits, N);
}; };
template <auto N> template <auto N>
requires(N <= 16) requires(N <= 16)
avclan_bit_t sendbits(uint16_t bits) { Bit sendbits(uint16_t bits) {
return AVCLAN_sendbitsl(&bits, N); return AVCLAN_sendbitsl(&bits, N);
}; };
template <auto N> template <auto N>
requires(N < 8) requires(N < 8)
avclan_bit_t readbits(uint8_t *bits) { Bit readbits(uint8_t *bits) {
return static_cast<avclan_bit_t>(AVCLAN_readbitsi(bits, N)); return static_cast<Bit>(AVCLAN_readbitsi(bits, N));
}; };
template <auto N> template <auto N>
requires(N <= 16) requires(N <= 16)
avclan_bit_t readbits(uint16_t *bits) { Bit readbits(uint16_t *bits) {
return static_cast<avclan_bit_t>(AVCLAN_readbitsl(bits, N)); return static_cast<Bit>(AVCLAN_readbitsl(bits, N));
}; };
}; };
+6 -6
View File
@@ -99,7 +99,7 @@ static void set_AVC_logic_for(uint8_t val, uint16_t period) {
return; return;
} }
void AVCLAN_sendbit(avclan_bit_t bit) { void AVCLAN_sendbit(Bit bit) {
uint16_t zero_length, one_length; uint16_t zero_length, one_length;
switch (bit) { switch (bit) {
case bit_zero: case bit_zero:
@@ -160,7 +160,7 @@ uint8_t AVCLAN_readbit_ACK() {
} }
// Send `len` bits on the AVCLAN bus; returns the even parity // 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 b = *bits;
uint8_t parity = 0; uint8_t parity = 0;
int8_t len_mod8 = 8; int8_t len_mod8 = 8;
@@ -173,7 +173,7 @@ avclan_bit_t AVCLAN_sendbitsi(const uint8_t *bits, int8_t len) {
while (len > 0) { while (len > 0) {
len -= len_mod8; len -= len_mod8;
for (; len_mod8 > 0; len_mod8--) { for (; len_mod8 > 0; len_mod8--) {
avclan_bit_t bit = (b & 0x80) != 0; Bit bit = (b & 0x80) != 0;
parity += (uint8_t)bit; parity += (uint8_t)bit;
AVCLAN_sendbit(bit); AVCLAN_sendbit(bit);
b <<= 1; 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 // 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); 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 b = *byte;
uint8_t parity = 0; uint8_t parity = 0;
for (uint8_t nbits = 8; nbits > 0; nbits--) { for (uint8_t nbits = 8; nbits > 0; nbits--) {
avclan_bit_t bit = (b & 0x80) != 0; Bit bit = (b & 0x80) != 0;
parity += (uint8_t)bit; parity += (uint8_t)bit;
AVCLAN_sendbit(bit); AVCLAN_sendbit(bit);
b <<= 1; b <<= 1;