14#ifndef ADS9324_ENCODE_HPP
15#define ADS9324_ENCODE_HPP
27namespace ads9324::detail {
32 std::array<std::array<uint16_t, reg::kAddressSpace>,
reg::kBankCount> value{};
33 std::array<uint64_t, reg::kBankCount> touched{};
37constexpr std::size_t BankIndex(
reg::Bank bank)
noexcept {
38 return static_cast<std::size_t
>(ToUnderlying(bank));
42constexpr uint64_t AddressBit(uint8_t address)
noexcept {
43 return uint64_t{1} << (
static_cast<uint32_t
>(address) & 63U);
47constexpr uint16_t Or(uint16_t a, uint16_t b)
noexcept {
48 return static_cast<uint16_t
>(
static_cast<uint32_t
>(a) |
static_cast<uint32_t
>(b));
52constexpr uint16_t Bit(
bool condition, uint16_t mask)
noexcept {
53 return condition ? mask :
static_cast<uint16_t
>(0U);
57constexpr void Put(RegisterImage& img,
reg::Bank bank, uint8_t address, uint16_t value)
noexcept {
61 img.value[BankIndex(bank)][address] = value;
62 img.touched[BankIndex(bank)] |= AddressBit(address);
66constexpr uint16_t Get(
const RegisterImage& img,
reg::Bank bank, uint8_t address)
noexcept {
68 :
static_cast<uint16_t
>(0U);
72constexpr bool IsTouched(
const RegisterImage& img,
reg::Bank bank, uint8_t address)
noexcept {
73 return (img.touched[BankIndex(bank)] & AddressBit(address)) != 0U;
78constexpr void Merge(RegisterImage& img,
reg::Bank bank, uint8_t address, uint16_t mask,
79 uint16_t bits)
noexcept {
81 static_cast<uint32_t
>(Get(img, bank, address)) & ~
static_cast<uint32_t
>(mask);
82 Put(img, bank, address,
static_cast<uint16_t
>(kept | (
static_cast<uint32_t
>(bits) & mask)));
86template <Enumeration E>
87constexpr uint32_t Code(E e)
noexcept {
88 return static_cast<uint32_t
>(ToUnderlying(e));
91constexpr void EncodeInterface(RegisterImage& img,
const Interface& i)
noexcept {
93 uint16_t gc3 = reg::DoutLaneSel::Insert(0_u16, Code(i.lanes));
94 gc3 = reg::DoutLength::Insert(gc3, Code(i.length));
101 const auto& d = i.delay_ns;
102 uint16_t d1 = reg::DelaySdout::Insert(0_u16, d[0]);
103 d1 = reg::DelayD3::Insert(d1, d[4]);
104 d1 = reg::DelayD2::Insert(d1, d[3]);
105 d1 = reg::DelayD1::Insert(d1, d[2]);
106 d1 = reg::DelayD0::Insert(d1, d[1]);
107 uint16_t d2 = reg::DelayD7::Insert(0_u16, d[8]);
108 d2 = reg::DelayD6::Insert(d2, d[7]);
109 d2 = reg::DelayD5::Insert(d2, d[6]);
110 d2 = reg::DelayD4::Insert(d2, d[5]);
113 bool any_delay =
false;
114 for (
const uint8_t v : d) {
115 any_delay = any_delay || (v != 0U);
117 uint16_t gc4 = reg::DriveStrengthField::Insert(0_u16, Code(i.drive));
122constexpr void EncodeAlarm(RegisterImage& img,
const Alarm& a)
noexcept {
126 const uint32_t mask = or_masked ? (~static_cast<uint32_t>(a.sources) & 0xFFU) : 0U;
127 uint16_t v = reg::DrdyAlrmSel::Insert(0_u16, Code(a.function));
128 v = reg::AlarmMask::Insert(v, mask);
151constexpr uint32_t ChannelSelCode(
const ChannelSelect& select)
noexcept {
152 const auto group =
static_cast<uint32_t
>(select.group);
153 switch (select.count) {
166constexpr void EncodeAnalog(RegisterImage& img,
const Config& c, uint16_t cal_mode)
noexcept {
168 Put(img, Bank::Common,
reg::kAnaCfg1, ReferenceBits(c.reference));
169 uint16_t a2 = reg::AdcNumSel::Insert(0_u16, Code(c.channels.count));
170 a2 = reg::AdcChSel::Insert(a2, ChannelSelCode(c.channels));
173 uint16_t cal = reg::AdcCalMode::Insert(0_u16, cal_mode);
179constexpr bool AnyPhaseDelay(
const Config& c)
noexcept {
180 return std::ranges::any_of(c.inputs, [](
const Input& in) { return in.phase_delay != 0U; });
183constexpr void EncodeFilter(RegisterImage& img,
const Config& c)
noexcept {
185 const Filter& f = c.filter;
188 v = reg::FirFiltSel::Insert(v, Code(f.fir));
190 v = reg::MvgAvgLen::Insert(v, Code(f.length));
192 v = reg::BlkAvgOsr::Insert(v, Code(f.length));
204constexpr uint8_t PgaByte(
const Input& in)
noexcept {
205 uint8_t b = reg::InputRangeField::Insert(0_u8, Code(in.range));
206 b = reg::CmRange::Insert(b, Code(in.type));
207 return reg::CmeCorrEn::Insert(b, in.cm_correction ? 1U : 0U);
210constexpr uint16_t HalfMask(uint8_t local, uint16_t mask)
noexcept {
211 return static_cast<uint16_t
>(
static_cast<uint32_t
>(mask) <<
reg::HalfShift(local));
214constexpr uint16_t Twos(int32_t value, uint32_t bits)
noexcept {
215 return static_cast<uint16_t
>(
static_cast<uint32_t
>(value) & ((uint32_t{1} << bits) - 1U));
218constexpr void EncodeChannel(RegisterImage& img, uint8_t channel,
const Input& in)
noexcept {
219 const reg::ChannelLocation at =
reg::Locate(channel);
220 const uint8_t l = at.local;
222 const auto pga =
static_cast<uint16_t
>(
static_cast<uint32_t
>(PgaByte(in)) << half);
224 const uint32_t bw_shift = 2U *
static_cast<uint32_t
>(l);
225 Merge(img, at.bank,
reg::kPgaBwSel,
static_cast<uint16_t
>(3U << bw_shift),
226 static_cast<uint16_t
>(Code(in.bandwidth) << bw_shift));
227 const auto delay =
static_cast<uint16_t
>(
static_cast<uint32_t
>(in.phase_delay) << half);
231 const auto th =
static_cast<uint16_t
>(
232 (
static_cast<uint32_t
>(
static_cast<uint8_t
>(in.window.high_code)) << 8U) |
233 static_cast<uint32_t
>(
static_cast<uint8_t
>(in.window.low_code)));
235 const auto hys =
static_cast<uint16_t
>(
static_cast<uint32_t
>(in.window.hysteresis) << half);
237 const auto en =
static_cast<uint16_t
>(in.window.enabled ? (1U << l) : 0U);
238 Merge(img, at.bank,
reg::kDwcCfg,
static_cast<uint16_t
>(1U << l), en);
241constexpr void EncodeBankSettings(RegisterImage& img,
const Config& c)
noexcept {
242 for (uint32_t g = 0U; g < 2U; ++g) {
245 reg::DwcGlitchFilt::Insert(0_u16, c.window_glitch[g]));
249 const TestPattern& t = c.test_pattern[g];
250 uint16_t cfg = reg::TpMode::Insert(0_u16, Code(t.kind));
255 for (uint32_t i = 0U; i < 8U; ++i) {
256 const reg::ChannelLocation at =
reg::Locate(
static_cast<uint8_t
>((8U * g) + i));
269constexpr RegisterImage Encode(
const Config& c, uint16_t cal_mode)
noexcept {
271 EncodeInterface(img, c.interface);
272 EncodeAlarm(img, c.alarm);
273 EncodeAnalog(img, c, cal_mode);
274 EncodeFilter(img, c);
276 EncodeChannel(img, ch, c.inputs[ch]);
278 EncodeBankSettings(img, c);
290static_assert(Get(Encode(
293 c.interface.randomize =
true;
300static_assert(Get(Encode(
310constexpr bool IsClipped(int16_t code)
noexcept {
311 return (code == 32767) || (code == -32768);
316constexpr void DecodeWords(
const Config& c, std::span<const uint8_t> bytes, uint16_t mask,
317 Frame& frame)
noexcept {
320 for (uint32_t slot = 0U; slot < n; ++slot) {
321 const uint32_t at = slot * per;
323 const uint32_t word =
324 (
static_cast<uint32_t
>(bytes[at]) << 8U) |
static_cast<uint32_t
>(bytes[at + 1U]);
325 const auto raw =
static_cast<uint16_t
>(word ^
static_cast<uint32_t
>(mask));
326 const int16_t code =
WireToCode(raw, c.interface.format);
328 frame.codes[ch] = code;
329 frame.low_byte[ch] = (per == 3U) ? bytes[at + 2U] : static_cast<uint8_t>(0U);
332 static_cast<uint16_t
>(
static_cast<uint32_t
>(frame.present_mask) | (1U << ch));
333 if (IsClipped(code)) {
335 static_cast<uint16_t
>(
static_cast<uint32_t
>(frame.clipped_mask) | (1U << ch));
347constexpr Result<Frame> DecodeFrame(
const Config& c, std::span<const uint8_t> bytes)
noexcept {
348 if (bytes.size() !=
FrameBytes(c.interface, c.channels)) {
355 frame.diag = diag ? bytes[trailer] :
static_cast<uint8_t
>(0U);
356 frame.prbs = c.interface.randomize ? bytes[trailer + 1U] :
static_cast<uint8_t
>(0U);
357 const uint16_t mask =
358 c.interface.randomize ?
DerandomizeMask(frame.prbs) : static_cast<uint16_t>(0U);
359 DecodeWords(c, bytes, mask, frame);
360 frame.window_alert = (
static_cast<uint32_t
>(frame.diag) &
kDiagWindowAlert) != 0U;
361 frame.reset_detected = (
static_cast<uint32_t
>(frame.diag) &
kDiagResetDetect) != 0U;
375constexpr uint32_t RangeCode(
InputRange r)
noexcept {
Compile-time helpers of the ADS9324 driver: typed integer literals, exact hook matching,...
ADS9324 register map: banks, addresses, bit fields, reset values and the channel layout (datasheet se...
Public types of the ADS9324 driver: errors, Result<T>, enumerations, configuration,...
constexpr ChannelLocation Locate(uint8_t channel) noexcept
The bank and local index of a channel (the AIN9 to AIN16 bank is reversed).
Definition ads9324_registers.hpp:247
constexpr uint8_t kXorBitsCtl
XOR_BITS_CTL: output randomizer.
Definition ads9324_registers.hpp:90
constexpr uint8_t GainAddress(uint8_t local) noexcept
The address of the GAN register of a local index.
Definition ads9324_registers.hpp:297
constexpr uint16_t kXorBitsRandomizer
Definition ads9324_registers.hpp:150
constexpr uint16_t kRefselCtrlDis
REFSEL_CTRL_DIS: ignore the REFSEL pin.
Definition ads9324_registers.hpp:183
constexpr uint16_t kSeDiffAin1To8
SE_DIFF_MODE_AIN1_8: 1 = differential.
Definition ads9324_registers.hpp:192
constexpr uint16_t kExtRefEn
EXT_REF_EN: external reference.
Definition ads9324_registers.hpp:184
constexpr uint16_t kAlrmPol
ALRM_POL: 1 = active low.
Definition ads9324_registers.hpp:156
constexpr uint8_t kDigDelayCfg2
DIG_DELAY_CFG2: D7 to D4 delay.
Definition ads9324_registers.hpp:94
constexpr uint8_t kTpCfg
TP_CFG: test pattern control.
Definition ads9324_registers.hpp:114
constexpr uint16_t kTpUpdMode
TP_UPD_MODE: 1 = ramp steps at every CONVST.
Definition ads9324_registers.hpp:227
constexpr uint8_t kPgaConfig
PGA_CONFIG, 4 registers, two channels each.
Definition ads9324_registers.hpp:106
constexpr uint16_t kEnDiagFlag
EN_DIAG_FLAG: DIAG byte after the data.
Definition ads9324_registers.hpp:142
constexpr uint8_t kDrdyAlarmSel
DRDY_ALARM_SEL: pin function.
Definition ads9324_registers.hpp:91
constexpr uint16_t kAvgModeOvrEn
AVG_MODE_OVR_EN.
Definition ads9324_registers.hpp:206
constexpr uint8_t kGenCfg5
GEN_CFG5: AVG_MODE gate.
Definition ads9324_registers.hpp:99
constexpr uint8_t kAnaCfg1
ANA_CFG1: reference source.
Definition ads9324_registers.hpp:95
constexpr uint16_t kAlrmDis
ALRM_DIS: 1 = alarm function disabled.
Definition ads9324_registers.hpp:162
constexpr uint16_t kGanCorrDis
GAN_CORR_DIS.
Definition ads9324_registers.hpp:230
constexpr uint16_t kDigDelayEn
DIG_DELAY_EN.
Definition ads9324_registers.hpp:163
constexpr uint16_t kIntTrigMode
INT_TRIG_MODE: internal oversampling clock.
Definition ads9324_registers.hpp:204
constexpr uint8_t kCorrection
GEN_CFG5 of a channel bank: correction bypass.
Definition ads9324_registers.hpp:116
constexpr uint16_t kEnXorPatt
EN_XOR_PATT: randomizer.
Definition ads9324_registers.hpp:141
constexpr uint16_t kAdcDataSdoutEn
ADC_DATA_SDOUT_EN.
Definition ads9324_registers.hpp:145
constexpr uint32_t HalfShift(uint8_t local) noexcept
The bit position of the odd (high) or even (low) half of a two-channel register.
Definition ads9324_registers.hpp:330
constexpr uint16_t kTpEn
TP_EN.
Definition ads9324_registers.hpp:228
constexpr uint8_t kBankCount
Number of register banks.
Definition ads9324_registers.hpp:55
constexpr uint8_t kGenCfg4
GEN_CFG4: drive strength, delay enable.
Definition ads9324_registers.hpp:92
constexpr uint16_t kAvgMode
AVG_MODE.
Definition ads9324_registers.hpp:207
Bank
A register bank. The value is the index of the bank in the driver's tables.
Definition ads9324_registers.hpp:47
constexpr uint8_t DwcThAddress(uint8_t local) noexcept
The address of the DWC_TH register of a local index.
Definition ads9324_registers.hpp:305
constexpr uint8_t kDigFilter
DIG_FILTER: digital filters and SYSREF.
Definition ads9324_registers.hpp:98
constexpr uint16_t kEnOfsBinary
EN_OFS_BINARY: 1 = offset binary.
Definition ads9324_registers.hpp:140
constexpr uint16_t kAlrmType
ALRM_TYPE: 1 = pulse (window alert only).
Definition ads9324_registers.hpp:155
constexpr uint16_t kTpDisIdx
TP_DIS_IDX: 1 = no channel index insertion.
Definition ads9324_registers.hpp:226
constexpr uint8_t PhaseDelayAddress(uint8_t local) noexcept
The address of the PHASE_DELAY register of a local index.
Definition ads9324_registers.hpp:281
constexpr uint8_t PgaConfigAddress(uint8_t local) noexcept
The address of the register that holds the PGA_CONFIG of a local index.
Definition ads9324_registers.hpp:273
constexpr uint8_t DwcHysAddress(uint8_t local) noexcept
The address of the DWC_HYS register of a local index.
Definition ads9324_registers.hpp:313
constexpr uint8_t OffsetAddress(uint8_t local) noexcept
The address of the OFS register of a local index.
Definition ads9324_registers.hpp:289
constexpr uint16_t kXtalkLowSpeed
CH_XTALK_LOW_SPEED.
Definition ads9324_registers.hpp:185
constexpr uint8_t kAnaCfg2
ANA_CFG2: channel mode, low-speed crosstalk.
Definition ads9324_registers.hpp:96
constexpr uint8_t kChannelCount
Number of channels.
Definition ads9324_registers.hpp:57
constexpr uint8_t kAdcCal
ADC_CAL: ADC calibration module.
Definition ads9324_registers.hpp:97
constexpr uint8_t TpAddress(uint8_t local) noexcept
The address of the TP_AIN register of a local index.
Definition ads9324_registers.hpp:321
constexpr uint8_t kDigDelayCfg1
DIG_DELAY_CFG1: SDOUT and D3 to D0 delay.
Definition ads9324_registers.hpp:93
constexpr uint16_t kSeDiffAin9To16
SE_DIFF_MODE_AIN9_16: 1 = differential.
Definition ads9324_registers.hpp:193
constexpr uint8_t kDwcCfg
DWC_CFG: enables, glitch filter, flag reset.
Definition ads9324_registers.hpp:111
constexpr uint8_t kGenCfg3
GEN_CFG3: data format and lanes.
Definition ads9324_registers.hpp:89
constexpr uint8_t kPgaBwSel
PGA_BW_SEL, 2 bits per channel.
Definition ads9324_registers.hpp:107
constexpr uint16_t kOfsCorrDis
OFS_CORR_DIS.
Definition ads9324_registers.hpp:229
constexpr uint8_t kAddressSpace
Addresses per bank table: the highest documented address is 0x3E.
Definition ads9324_registers.hpp:61
constexpr uint16_t kPhaseDelayEn
PHASE_DELAY_EN.
Definition ads9324_registers.hpp:200
@ Ain1To8
Channel registers AIN1 to AIN8, BANK_SEL code 010b.
@ Common
The common registers, BANK_SEL code 001b.
constexpr std::size_t FrameBytes(const Interface &interface, const ChannelSelect &select) noexcept
Bytes of one data frame (section 7.3.14).
Definition ads9324_types.hpp:1223
constexpr bool HasDiagByte(const Interface &interface) noexcept
Whether the frame ends with a DIAG byte: asked for, or implied by the randomizer (the datasheet shows...
Definition ads9324_types.hpp:1212
InputRange
Input range of a channel (INPUT_RANGE_AINn, Table 7-1).
Definition ads9324_types.hpp:200
constexpr uint8_t FrameSlotChannel(const ChannelSelect &select, uint32_t slot) noexcept
Which channel a slot of the frame carries (Table 7-12).
Definition ads9324_types.hpp:1179
constexpr int16_t WireToCode(uint16_t raw, DataFormat format) noexcept
The signed code of a wire value.
Definition ads9324_types.hpp:1134
constexpr uint8_t kDiagResetDetect
DIAG bit 0, RESET_DETECT_FLAG.
Definition ads9324_types.hpp:1248
static constexpr Result Success(T v) noexcept
A successful result.
Definition ads9324_types.hpp:131
constexpr int32_t CodeToMicrovolts(int32_t code, InputRange range) noexcept
Microvolts of a signed code: code x full scale / 32768, rounded half away from zero.
Definition ads9324_types.hpp:1111
constexpr Config ChipResetConfig() noexcept
The chip's reset state as a Config: eight lanes on D7 to D0, everything else as Config{}.
Definition ads9324_types.hpp:691
constexpr uint16_t DerandomizeMask(uint8_t prbs) noexcept
The XOR mask of the randomizer (equations 7 and 8, section 7.3.14.4).
Definition ads9324_types.hpp:1146
ReferenceSource
Where the ADC reference comes from (Table 7-3).
Definition ads9324_types.hpp:244
constexpr uint8_t kDiagWindowAlert
DIAG bit 6, DWC_ALERT_FLAG (Table 7-13).
Definition ads9324_types.hpp:1246
constexpr uint32_t FrameChannels(const ChannelSelect &select) noexcept
Number of channels in a frame.
Definition ads9324_types.hpp:1157
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition ads9324_types.hpp:139
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ All16
16 channels. Reset value.
@ Four
4 channels; group 0 to 3.
@ Two
2 channels; group 0 to 7.
@ Eight
8 channels; group 0 or 1.
@ OffsetBinary
Straight offset binary.
@ Bits24
24 bits: the result and an extra byte.
@ Fir
A FIR filter, Filter::fir.
@ MovingAverage
Moving average over Filter::length conversions.
@ BlockAverage
Block average over Filter::length conversions.
@ Internal
Internal 4.096 V reference, REFSEL ignored.
@ Pin
The REFSEL pin chooses. Reset value.