12#ifndef ADS7952_TYPES_HPP
13#define ADS7952_TYPES_HPP
21#include "ads7952_version.h"
38enum class [[nodiscard]]
Error : uint8_t {
92 return "NotInitialized";
94 return "InvalidArgument";
100 return "DeviceIdMismatch";
106 return "DeviceFault";
108 return "Unsupported";
112 return "ChannelMismatch";
114 return "SequenceMismatch";
116 return "WriteMismatch";
118 return "RangeMismatch";
120 return "ScaleMismatch";
122 return "DeviceReset";
142 [[nodiscard]]
constexpr bool ok() const noexcept {
143 return error == Error::Ok;
146 constexpr explicit operator bool() const noexcept {
156 return {v, Error::Ok};
174 [[nodiscard]]
constexpr bool ok() const noexcept {
175 return error == Error::Ok;
178 constexpr explicit operator bool() const noexcept {
245struct EnumLast<
Range> {
341 return static_cast<uint16_t
>((uint32_t{1} <<
ChannelCount(variant)) - 1U);
344static_assert(
kMaxChannels <= 16U,
"channel masks are 16 bits");
355 std::array<uint16_t, kMaxChannels>
code{};
365 [[nodiscard]]
constexpr bool Has(uint8_t channel)
const noexcept {
366 return (channel <
kMaxChannels) && (((
static_cast<uint32_t
>(
mask) >> channel) & 1U) != 0U);
424 std::array<AlarmThreshold, kMaxChannels>
alarms{};
440[[nodiscard]]
constexpr uint32_t FullScaleUv(
Range range, uint16_t vref_mv)
noexcept {
441 return static_cast<uint32_t
>(vref_mv) * ((range ==
Range::TwoVref) ? 2000U : 1000U);
445[[nodiscard]]
constexpr uint32_t CodeOfInput(uint32_t input_uv, uint32_t full_scale_uv)
noexcept {
446 const uint64_t code = (
static_cast<uint64_t
>(input_uv) * 4096U) / full_scale_uv;
447 return (code > 4095U) ? 4095U :
static_cast<uint32_t
>(code);
451struct MonitorWindow {
457[[nodiscard]]
constexpr MonitorWindow WindowOf(
const Config& config)
noexcept {
459 const uint32_t fs = FullScaleUv(config.range, config.vref_mv);
460 const uint64_t margin =
461 ((
static_cast<uint64_t
>(config.monitor.tolerance_uv) * 4096U) + fs - 1U) / fs;
462 return {.expected = CodeOfInput(config.monitor.expected_uv, fs),
463 .other = CodeOfInput(config.monitor.expected_uv, FullScaleUv(other, config.vref_mv)),
464 .margin = (margin > 4096U) ? 4096U : static_cast<uint32_t>(margin)};
467[[nodiscard]]
constexpr bool Within(uint32_t code, uint32_t centre, uint32_t margin)
noexcept {
468 return ((code + margin) >= centre) && (code <= (centre + margin));
488 const detail::MonitorWindow w = detail::WindowOf(config);
489 if ((w.expected <= w.margin) || ((w.expected + w.margin) >=
kMaxCode)) {
492 const uint32_t gap = (w.expected > w.other) ? (w.expected - w.other) : (w.other - w.expected);
508 const detail::MonitorWindow w = detail::WindowOf(config);
509 const uint32_t c =
static_cast<uint32_t
>(code) &
kMaxCode;
510 if (detail::Within(c, w.expected, w.margin)) {
523 const auto alarm =
static_cast<uint32_t
>(detail::ToUnderlying(g.
alarm_output));
526 if ((alarm > 6U) || (alarm == 3U) || (alarm == 5U) || (outputs > 0x0FU) ||
527 ((levels & ~outputs) != 0U)) {
532 uint32_t function_pins = 0U;
533 function_pins |= ((alarm & 0x03U) != 0U) ? 0x01U : 0x00U;
534 function_pins |= ((alarm & 0x04U) != 0U) ? 0x02U : 0x00U;
537 if ((function_pins & outputs) != 0U) {
540 if ((config.package ==
Package::Vqfn) && (((function_pins | outputs) & 0x0EU) != 0U)) {
571 if ((config.vref_mv < 2000U) || (config.vref_mv > 3000U) || (config.va_min_mv < 2700U) ||
572 (config.va_min_mv > 5250U)) {
576 ((2U *
static_cast<uint32_t
>(config.vref_mv)) <=
static_cast<uint32_t
>(config.va_min_mv));
585 return (config.park_channel ==
kNoPark) ||
586 ((config.park_channel != 0U) && (config.park_channel <
ChannelCount(config.variant)));
600 if (!detail::IsEnumerator(config.variant) || !detail::IsEnumerator(config.package) ||
601 !detail::IsEnumerator(config.range)) {
607 const auto all =
static_cast<uint32_t
>(
AllChannels(config.variant));
608 if ((config.auto1_channels == 0U) ||
609 ((
static_cast<uint32_t
>(config.auto1_channels) & ~all) != 0U) ||
610 (config.auto2_last_channel >= channels)) {
632 const auto channels =
static_cast<uint32_t
>(
ChannelCount(variant));
633 c.auto2_last_channel =
static_cast<uint8_t
>((channels > 0U) ? (channels - 1U) : 0U);
654 uint16_t vref_mv)
noexcept {
655 const uint32_t full_scale_uv = detail::FullScaleUv(range, vref_mv);
656 return static_cast<uint32_t
>(
657 (
static_cast<uint64_t
>(
static_cast<uint32_t
>(code) &
kMaxCode) * full_scale_uv) /
662using detail::operator
""_u16;
669 return ADS7952_VERSION_STRING;
673 return ADS7952_VERSION_MAJOR;
677 return ADS7952_VERSION_MINOR;
681 return ADS7952_VERSION_PATCH;
Compile-time helpers of the ADS7952 driver: exact hook matching and enumeration checks.
ADS795x command and reply words: the bit fields of every frame (datasheet SLAS605C,...
constexpr uint32_t kCodesPerFullScale
Codes per full scale: one LSB is full scale / 4096 (Table 8).
Definition ads7952_types.hpp:303
constexpr uint32_t GetDriverVersionMajor() noexcept
Definition ads7952_types.hpp:672
constexpr uint8_t kNoPark
Config::park_channel when the chip stays on channel 0 after a read of it: no parking.
Definition ads7952_types.hpp:374
constexpr std::size_t kFrameBytes
Bytes per transfer: every frame is 16 bits with CS low throughout (section 8.1).
Definition ads7952_types.hpp:310
constexpr uint8_t ChannelCount(Variant variant) noexcept
Number of analog inputs of a variant.
Definition ads7952_types.hpp:317
constexpr uint32_t CodeToMicrovolts(uint16_t code, Range range, uint16_t vref_mv) noexcept
Convert a result code to the input voltage it stands for (Table 8: one LSB is full scale / 4096,...
Definition ads7952_types.hpp:653
constexpr uint8_t kNoMonitor
ScaleMonitor::channel when no channel carries a known voltage: no scale check.
Definition ads7952_types.hpp:371
constexpr bool ValidSupplies(const Config &config) noexcept
Check the reference and the analog supply (section 7.3), and Range 2 against them (2 x VREF <= +VA,...
Definition ads7952_types.hpp:570
constexpr Error Validate(const Config &config) noexcept
Check a configuration without touching the chip.
Definition ads7952_types.hpp:598
Range
Input range, DI06 of the mode control word (Tables 1, 2, 5; Table 8).
Definition ads7952_types.hpp:227
Variant
Which 12-bit member of the family is fitted. The chip cannot tell, so it is configured.
Definition ads7952_types.hpp:212
constexpr std::size_t kMaxChannels
Channels of the largest part (ADS7953); arrays indexed by channel have this size.
Definition ads7952_types.hpp:299
constexpr uint32_t GetDriverVersionPatch() noexcept
Definition ads7952_types.hpp:680
constexpr uint32_t kPowerUpUs
Definition ads7952_types.hpp:306
constexpr bool ValidPark(const Config &config) noexcept
Check Config::park_channel against the variant.
Definition ads7952_types.hpp:584
constexpr const char * GetDriverVersion() noexcept
Definition ads7952_types.hpp:668
constexpr uint32_t GetDriverVersionMinor() noexcept
Definition ads7952_types.hpp:676
constexpr uint16_t AllChannels(Variant variant) noexcept
The mask of every channel of a variant: also the reset value of its Auto-1 program register (section ...
Definition ads7952_types.hpp:339
Package
The package: the VQFN parts bring out GPIO0 only (section 8.5.2).
Definition ads7952_types.hpp:220
constexpr std::size_t kMaxTraceFrames
Longest TraceFrames() request, in frames.
Definition ads7952_types.hpp:308
constexpr Error CheckScaleCode(const Config &config, uint16_t code) noexcept
Judge one code of the scale monitor's channel. No I/O.
Definition ads7952_types.hpp:504
constexpr uint16_t kMaxCode
Largest result code: 12 bits, straight binary (Table 8).
Definition ads7952_types.hpp:301
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition ads7952_types.hpp:38
constexpr const char * ToString(Error error) noexcept
The name of an error, for logs.
Definition ads7952_types.hpp:85
LogLevel
Severity passed to the optional Log() hook.
Definition ads7952_types.hpp:197
constexpr Error ValidateMonitor(const Config &config) noexcept
Check the scale monitor of a configuration.
Definition ads7952_types.hpp:480
constexpr Config ResetConfig(Variant variant) noexcept
The Config of a variant left in its reset state: Config{} with that variant's Auto-1 mask and Auto-2 ...
Definition ads7952_types.hpp:628
constexpr Error ValidateGpio(const Config &config) noexcept
Check the GPIO settings of a configuration against its package.
Definition ads7952_types.hpp:521
constexpr Error ValidateAlarms(const Config &config) noexcept
Check the alarm thresholds of a configuration against its variant.
Definition ads7952_types.hpp:552
Verify
Extra verification for one call, chosen per call (handbook KB-ARCH-041).
Definition ads7952_types.hpp:256
AlarmOutput
What GPIO0 and GPIO1 do besides general-purpose I/O: DI06:04 of the GPIO program register (Table 11)....
Definition ads7952_types.hpp:265
@ Vref
0 to VREF (Range 1). The reset value.
@ TwoVref
0 to 2 x VREF (Range 2); specified only while 2 x VREF <= +VA (section 7.6).
@ Ads7952
12 channels. The default.
@ Tssop
TSSOP (DBT): GPIO0 to GPIO3. The default.
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ Crc
Not returned by this driver (the chip sends no CRC).
@ Bus
The port's Transfer() (or EnsureInitialized()) returned false.
@ DeviceFault
Not returned by this driver (the chip reports no faults).
@ NotInitialized
EnsureInitialized() has not succeeded, or Deinitialize() ran since.
@ Timeout
Not returned by this driver: no call waits for the chip.
@ Busy
Not returned by this driver (the chip is never busy to a frame).
@ Warning
Something unexpected that the driver recovered from.
@ Info
Lifecycle events: initialised.
@ Debug
Detail for bring-up.
@ None
No extra frame. The default.
@ ReadBack
Read back what the chip took (GPIO levels, the scanned sequence).
@ Gpio0HighOrLow
xx1: GPIO0 is the "high or low" alarm output.
@ Gpio0High
010: GPIO0 is the high alarm output.
@ None
000: GPIO0 and GPIO1 are general-purpose I/O. The reset value.
@ Gpio1LowGpio0High
110: GPIO1 the low and GPIO0 the high alarm output (TSSOP only).
@ Gpio1Low
100: GPIO1 is the low alarm output (TSSOP only).
The ADS7952 driver: everything it declares lives here; internals in detail.
Definition ads7952.hpp:30
The two alarm thresholds of one channel, in 12-bit result codes. The chip keeps the upper 10 bits and...
Definition ads7952_types.hpp:293
uint16_t high_code
High alarm when the result is above this; 0 to 4095.
Definition ads7952_types.hpp:295
uint16_t low_code
Low alarm when the result is below this; 0 to 4095.
Definition ads7952_types.hpp:294
The result of one Auto-1 or Auto-2 scan: one code per channel of the sequence.
Definition ads7952_types.hpp:354
constexpr bool Has(uint8_t channel) const noexcept
Definition ads7952_types.hpp:365
std::array< uint16_t, kMaxChannels > code
Result of channel n at index n, 0 to 4095.
Definition ads7952_types.hpp:355
Range range
Definition ads7952_types.hpp:359
uint16_t mask
Definition ads7952_types.hpp:356
What the driver is told about the chip and the board. Config{} is an ADS7952 in TSSOP left in its res...
Definition ads7952_types.hpp:402
uint16_t va_min_mv
Definition ads7952_types.hpp:416
std::array< AlarmThreshold, kMaxChannels > alarms
Definition ads7952_types.hpp:424
ScaleMonitor monitor
Definition ads7952_types.hpp:427
uint16_t vref_mv
Definition ads7952_types.hpp:412
Package package
Definition ads7952_types.hpp:404
Variant variant
The fitted part; every channel is checked against it.
Definition ads7952_types.hpp:403
uint8_t park_channel
Definition ads7952_types.hpp:434
GpioConfig gpio
GPIO program register and output levels.
Definition ads7952_types.hpp:423
uint8_t auto2_last_channel
Definition ads7952_types.hpp:422
uint16_t auto1_channels
Definition ads7952_types.hpp:419
Range range
Definition ads7952_types.hpp:408
The GPIO program register and the GPIO output levels. GpioConfig{} is the reset state: every pin an i...
Definition ads7952_types.hpp:280
uint8_t output_mask
Bit n = 1: GPIOn is a general-purpose output (DI03:00).
Definition ads7952_types.hpp:281
uint8_t output_levels
Levels of the output pins, bit n for GPIOn; only output pins.
Definition ads7952_types.hpp:282
bool gpio2_range_input
GPIO2 selects the range (DI07); DI06 is then ignored.
Definition ads7952_types.hpp:284
bool gpio3_power_down_input
GPIO3 is the active-low power-down input (DI08).
Definition ads7952_types.hpp:285
AlarmOutput alarm_output
GPIO0/GPIO1 alarm functions (DI06:04).
Definition ads7952_types.hpp:283
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition ads7952_types.hpp:191
static constexpr Result Success() noexcept
Definition ads7952_types.hpp:183
constexpr bool ok() const noexcept
Definition ads7952_types.hpp:174
The value of a call, or why there is none.
Definition ads7952_types.hpp:137
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition ads7952_types.hpp:163
static constexpr Result Success(T v) noexcept
A successful result.
Definition ads7952_types.hpp:155
constexpr bool ok() const noexcept
Definition ads7952_types.hpp:142
A channel the board drives with a known voltage, so the driver can prove the scale every code is conv...
Definition ads7952_types.hpp:386
uint32_t expected_uv
Its nominal input, in microvolts; strictly inside the range.
Definition ads7952_types.hpp:388
uint8_t channel
The channel, or kNoMonitor.
Definition ads7952_types.hpp:387
uint32_t tolerance_uv
Definition ads7952_types.hpp:389