mirror of
https://github.com/halleysfifthinc/AVCLAN-Mockingboard.git
synced 2026-08-06 17:03:17 +00:00
Shift avclan_bit_t to avclan.h
This commit is contained in:
+34
-36
@@ -3,54 +3,52 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <cstdint>
|
||||
#define AVCLAN_ENUM_CLASS enum class
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#define AVCLAN_ENUM_CLASS enum
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -8,13 +8,14 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#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);
|
||||
|
||||
+5
-6
@@ -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<avclan_bit_t>(bits & 1U)};
|
||||
template <> inline Bit Bus::Handle::sendbits<1>(uint8_t bits) {
|
||||
const Bit bit{static_cast<Bit>(bits & 1U)};
|
||||
AVCLAN_sendbit(bit);
|
||||
return bit;
|
||||
};
|
||||
template <> inline avclan_bit_t Bus::Handle::readbits<8>(uint8_t *bits) {
|
||||
return static_cast<avclan_bit_t>(AVCLAN_readbyte(bits));
|
||||
template <> inline Bit Bus::Handle::readbits<8>(uint8_t *bits) {
|
||||
return static_cast<Bit>(AVCLAN_readbyte(bits));
|
||||
};
|
||||
|
||||
} // namespace avclan
|
||||
|
||||
+11
-18
@@ -48,10 +48,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
#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<Trailer, with_parity_t>) {
|
||||
uint8_t 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{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 <auto N, class T> avclan_bit_t sendbits(T bits);
|
||||
template <auto N, class T> avclan_bit_t readbits(T *bits);
|
||||
template <auto N, class T> Bit sendbits(T bits);
|
||||
template <auto N, class T> Bit readbits(T *bits);
|
||||
|
||||
// Temporary specializations bridging to legacy C API
|
||||
// Replace with proper (single?) template when phy has been ported
|
||||
template <auto N>
|
||||
requires(N > 1 && N < 8)
|
||||
avclan_bit_t sendbits(uint8_t bits) {
|
||||
Bit sendbits(uint8_t bits) {
|
||||
return AVCLAN_sendbitsi(&bits, N);
|
||||
};
|
||||
template <auto N>
|
||||
requires(N <= 16)
|
||||
avclan_bit_t sendbits(uint16_t bits) {
|
||||
Bit sendbits(uint16_t bits) {
|
||||
return AVCLAN_sendbitsl(&bits, N);
|
||||
};
|
||||
template <auto N>
|
||||
requires(N < 8)
|
||||
avclan_bit_t readbits(uint8_t *bits) {
|
||||
return static_cast<avclan_bit_t>(AVCLAN_readbitsi(bits, N));
|
||||
Bit readbits(uint8_t *bits) {
|
||||
return static_cast<Bit>(AVCLAN_readbitsi(bits, N));
|
||||
};
|
||||
template <auto N>
|
||||
requires(N <= 16)
|
||||
avclan_bit_t readbits(uint16_t *bits) {
|
||||
return static_cast<avclan_bit_t>(AVCLAN_readbitsl(bits, N));
|
||||
Bit readbits(uint16_t *bits) {
|
||||
return static_cast<Bit>(AVCLAN_readbitsl(bits, N));
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user