17#ifndef MCP9700_TYPES_HPP
18#define MCP9700_TYPES_HPP
25#include "mcp9700_version.h"
46enum class [[nodiscard]]
Error : uint8_t {
77 return "NotInitialized";
79 return "InvalidArgument";
85 return "DeviceIdMismatch";
117 [[nodiscard]]
constexpr bool ok() const noexcept {
118 return error == Error::Ok;
121 constexpr explicit operator bool() const noexcept {
130 return {v, Error::Ok};
148 [[nodiscard]]
constexpr bool ok() const noexcept {
149 return error == Error::Ok;
152 constexpr explicit operator bool() const noexcept {
339 std::array<AccuracyBand, kMaxBands>
bands{};
350constexpr AccuracyBand Band(int32_t from_mc, int32_t to_mc, int32_t min_mc, int32_t max_mc,
351 bool high_only)
noexcept {
356 .high_only = high_only};
368 .tc_uv_per_c = 10'000U,
370 .max_ext_mc = 125'000,
371 .max_high_mc = 150'000,
375 .tested_vdd_mv = 3300U,
379 .bands = {{detail::Band(0, 70'000, -4000, 4000,
false),
380 detail::Band(-40'000, 125'000, -4000, 6000,
false),
381 detail::Band(-40'000, 150'000, -4000, 6000,
true)}}},
384 .tc_uv_per_c = 10'000U,
386 .max_ext_mc = 125'000,
387 .max_high_mc = 125'000,
391 .tested_vdd_mv = 3300U,
395 .bands = {{detail::Band(0, 70'000, -2000, 2000,
false),
396 detail::Band(-40'000, 125'000, -2000, 4000,
false)}}},
399 .tc_uv_per_c = 10'000U,
401 .max_ext_mc = 125'000,
402 .max_high_mc = 150'000,
406 .tested_vdd_mv = 3300U,
410 .bands = {{detail::Band(20'000, 70'000, -1000, 1000,
false),
411 detail::Band(0, 125'000, -2000, 3000,
false),
412 detail::Band(-40'000, 125'000, -2000, 4000,
false),
413 detail::Band(-40'000, 150'000, -4000, 4000,
true)}}},
416 .tc_uv_per_c = 19'500U,
418 .max_ext_mc = 125'000,
419 .max_high_mc = 125'000,
423 .tested_vdd_mv = 5000U,
427 .bands = {{detail::Band(0, 70'000, -4000, 4000,
false),
428 detail::Band(-10'000, 125'000, -4000, 6000,
false)}}},
431 .tc_uv_per_c = 19'500U,
433 .max_ext_mc = 125'000,
434 .max_high_mc = 125'000,
438 .tested_vdd_mv = 5000U,
442 .bands = {{detail::Band(0, 70'000, -2000, 2000,
false),
443 detail::Band(-10'000, 125'000, -2000, 4000,
false)}}}}};
465inline constexpr int64_t kPerMega = 1'000'000;
468constexpr int64_t RoundDivAway(int64_t num, int64_t den)
noexcept {
469 const int64_t half = den / 2;
470 return (num >= 0) ? ((num + half) / den) : (-((-num + half) / den));
474constexpr int64_t OutputNanoVolts(uint32_t v0_mv, uint32_t tc_uv_per_c, int64_t t_mc)
noexcept {
475 return (
static_cast<int64_t
>(v0_mv) * kPerMega) + (
static_cast<int64_t
>(tc_uv_per_c) * t_mc);
479constexpr bool WithinSymmetric(int32_t x, int32_t limit)
noexcept {
480 return (x >= -limit) && (x <= limit);
484constexpr bool CustomIsZero(
const CustomSpec& c)
noexcept {
485 return (c.v0_mv == 0U) && (c.tc_uv_per_c == 0U) && (c.min_mc == 0) && (c.max_mc == 0) &&
486 (c.accuracy_min_mc == 0) && (c.accuracy_max_mc == 0);
491constexpr bool CustomOutputInWindow(
const CustomSpec& c, int32_t t_mc)
noexcept {
492 const int64_t nv = OutputNanoVolts(c.v0_mv, c.tc_uv_per_c, t_mc);
497constexpr bool CustomTransferOk(
const CustomSpec& c)
noexcept {
502 CustomOutputInWindow(c, c.max_mc);
507constexpr bool CustomAccuracyOk(
const CustomSpec& c)
noexcept {
508 return (c.accuracy_min_mc <= 0) && (c.accuracy_max_mc >= 0) &&
509 ((c.accuracy_min_mc != 0) || (c.accuracy_max_mc != 0)) &&
516constexpr bool VariantDeclarationOk(
const Config& config)
noexcept {
517 if (!IsEnumerator(config.variant) || !IsEnumerator(config.range)) {
522 CustomAccuracyOk(config.custom);
524 if (!CustomIsZero(config.custom)) {
528 kVariantTable[
static_cast<std::size_t
>(ToUnderlying(config.variant))].has_high;
532constexpr bool AdcDeclarationOk(
const Config& config)
noexcept {
537 (config.divider_den != 0U) && WithinSymmetric(config.offset_cal_mc,
kMaxOffsetCalMc) &&
542constexpr bool SamplingOk(
const Config& config)
noexcept {
543 if ((config.samples == 0U) || (config.samples >
kMaxSamples) ||
548 const uint64_t conversions =
static_cast<uint64_t
>(config.discard_conversions) +
549 static_cast<uint64_t
>(config.samples) - 1U;
550 return ((conversions *
static_cast<uint64_t
>(config.sample_interval_us)) <=
567 if (!detail::VariantDeclarationOk(config) || !detail::AdcDeclarationOk(config) ||
568 !detail::SamplingOk(config)) {
586 kVariantTable[
static_cast<std::size_t
>(detail::ToUnderlying(config.variant))]);
589 spec.
v0_mv = config.custom.v0_mv;
590 spec.tc_uv_per_c = config.custom.tc_uv_per_c;
591 spec.min_mc = config.custom.min_mc;
592 spec.max_ext_mc = config.custom.max_mc;
593 spec.max_high_mc = config.custom.max_mc;
594 spec.band_count = 1U;
595 spec.bands[0] = detail::Band(config.custom.min_mc, config.custom.max_mc,
596 config.custom.accuracy_min_mc, config.custom.accuracy_max_mc,
false);
609 constexpr uint32_t kLargest = 65'535U;
610 const uint32_t mv = (output_mv < kLargest) ? output_mv : kLargest;
612 (
static_cast<int64_t
>(mv) -
static_cast<int64_t
>(spec.v0_mv)) * detail::kPerMega;
613 return static_cast<int32_t
>(detail::RoundDivAway(num,
static_cast<int64_t
>(spec.tc_uv_per_c)));
624 const int64_t nv = detail::OutputNanoVolts(spec.v0_mv, spec.tc_uv_per_c, temperature_mc);
628 const int64_t mv = detail::RoundDivAway(nv, detail::kPerMega);
629 constexpr int64_t kLargest = 0xFFFF'FFFFLL;
630 return static_cast<uint32_t
>((mv < kLargest) ? mv : kLargest);
646 int32_t temperature_mc)
noexcept {
648 const int32_t top = high ? spec.max_high_mc : spec.max_ext_mc;
649 if ((high && !spec.has_high) || (temperature_mc < spec.min_mc) || (temperature_mc > top)) {
652 for (std::size_t i = 0; i <
kMaxBands; ++i) {
654 if ((i <
static_cast<std::size_t
>(spec.band_count)) && (band.
high_only == high) &&
655 (temperature_mc >= band.
from_mc) && (temperature_mc <= band.
to_mc)) {
669 return static_cast<float>(milli_c) / 1000.0F;
691 c.sample_interval_us = 0U;
702 c.sample_interval_us = 200U;
724static_assert(
Validate(Config{}) ==
Error::Ok,
"the default configuration is valid");
732 return MCP9700_VERSION_STRING;
736 return MCP9700_VERSION_MAJOR;
740 return MCP9700_VERSION_MINOR;
744 return MCP9700_VERSION_PATCH;
constexpr uint8_t kMaxAdcResolutionBits
Largest declarable ADC resolution, bits.
Definition mcp9700_types.hpp:237
constexpr Config HighTemperaturePreset() noexcept
An MCP9700 with the High temperature option (to +150 C).
Definition mcp9700_types.hpp:718
LogLevel
Severity passed to the optional Log() hook.
Definition mcp9700_types.hpp:454
constexpr float ToCelsius(int32_t milli_c) noexcept
A temperature as float degrees Celsius, for printing and for callers that want it....
Definition mcp9700_types.hpp:668
constexpr uint8_t kMaxDiscard
Largest number of dummy conversions before a reading.
Definition mcp9700_types.hpp:215
constexpr Config FastPreset() noexcept
One conversion per reading: lowest latency, no noise rejection.
Definition mcp9700_types.hpp:688
constexpr uint32_t kCustomMinTcUvPerC
Custom: smallest slope, microvolts per degree.
Definition mcp9700_types.hpp:239
constexpr uint32_t GetDriverVersionPatch() noexcept
Definition mcp9700_types.hpp:743
constexpr int32_t OutputMvToMilliC(const VariantSpec &spec, uint32_t output_mv) noexcept
The temperature of an output voltage, from the row's V0 and TC; rounded half away from zero....
Definition mcp9700_types.hpp:608
constexpr int32_t kMaxRangeMarginMc
Largest range_margin_mc, milli-degrees.
Definition mcp9700_types.hpp:229
constexpr std::array< VariantSpec, 5 > kVariantTable
The five datasheet devices (DS20001942L), indexed by Variant. Every value is cited in docs/hardware....
Definition mcp9700_types.hpp:365
constexpr uint16_t kMaxSamples
Largest number of conversions one reading averages.
Definition mcp9700_types.hpp:211
constexpr uint8_t kPackagesAll
Every package: SOT-23-3, SC70-5 and TO-92-3.
Definition mcp9700_types.hpp:312
constexpr uint32_t kMaxPowerOnSettleUs
Largest power-on wait, microseconds.
Definition mcp9700_types.hpp:217
constexpr uint32_t MilliCToOutputMv(const VariantSpec &spec, int32_t temperature_mc) noexcept
The output voltage of a temperature: the inverse of OutputMvToMilliC(), rounded to the nearest milliv...
Definition mcp9700_types.hpp:623
constexpr int32_t kCustomMaxAccuracyMc
Custom: largest absolute accuracy limit, milli-degrees.
Definition mcp9700_types.hpp:250
constexpr uint8_t kMinAdcResolutionBits
Smallest declarable ADC resolution, bits.
Definition mcp9700_types.hpp:235
constexpr const char * GetDriverVersion() noexcept
Definition mcp9700_types.hpp:731
constexpr uint64_t kMaxAveragingWindowUs
Longest span of one reading, (discard + samples - 1) x interval, microseconds: below the 1....
Definition mcp9700_types.hpp:223
constexpr uint8_t kPackagesNoTo92
The packages without TO-92-3 (the MCP9700B, and the High option of every part).
Definition mcp9700_types.hpp:316
constexpr int32_t kMaxOffsetCalMc
Largest absolute offset_cal_mc, milli-degrees.
Definition mcp9700_types.hpp:227
constexpr uint32_t GetDriverVersionMinor() noexcept
Definition mcp9700_types.hpp:739
constexpr uint32_t kCustomMaxV0Mv
Custom: largest output at 0 C, millivolts.
Definition mcp9700_types.hpp:243
constexpr uint32_t kCustomMaxOutputMv
Custom: largest output voltage anywhere in the declared range, millivolts.
Definition mcp9700_types.hpp:248
constexpr uint8_t kPackageSc70
Package bit: SC70-5 (/LT).
Definition mcp9700_types.hpp:307
constexpr int32_t kCustomMaxTemperatureMc
Custom: largest absolute temperature limit, milli-degrees.
Definition mcp9700_types.hpp:245
constexpr uint32_t kMaxSpreadLimitMv
Largest max_spread_mv.
Definition mcp9700_types.hpp:225
constexpr uint32_t GetDriverVersionMajor() noexcept
Definition mcp9700_types.hpp:735
constexpr const char * ToString(Error error) noexcept
The name of an error, for logs.
Definition mcp9700_types.hpp:72
constexpr uint32_t kMaxSampleIntervalUs
Largest wait between two conversions, microseconds.
Definition mcp9700_types.hpp:213
Variant
Which part of the family is wired up: one of the five datasheet devices, or Custom.
Definition mcp9700_types.hpp:177
TemperatureClass
The temperature class of the part: the upper end of its specified range.
Definition mcp9700_types.hpp:191
constexpr uint32_t kMinAdcFullScaleMv
Smallest declarable ADC full scale, millivolts.
Definition mcp9700_types.hpp:231
constexpr Result< VariantSpec > ResolveSpec(const Config &config) noexcept
The effective variant row of a configuration: a table row, or the row built from the Custom declarati...
Definition mcp9700_types.hpp:580
constexpr std::size_t kMaxBands
Most accuracy bands one row holds.
Definition mcp9700_types.hpp:319
constexpr uint8_t kPackageTo92
Package bit: TO-92-3 (/TO).
Definition mcp9700_types.hpp:309
Error
Why a call failed. Values 0 to 9 are the shared base set; chip-specific values start at 16.
Definition mcp9700_types.hpp:46
constexpr uint32_t kCustomMaxTcUvPerC
Custom: largest slope, microvolts per degree.
Definition mcp9700_types.hpp:241
constexpr Error Validate(const Config &config) noexcept
Check a configuration without touching the hardware.
Definition mcp9700_types.hpp:566
constexpr uint8_t kPackageSot23
Package bit: SOT-23-3 (/TT).
Definition mcp9700_types.hpp:305
constexpr Result< AccuracyLimits > AccuracyLimitsFor(const VariantSpec &spec, TemperatureClass range, int32_t temperature_mc) noexcept
The error limits the datasheet guarantees at a temperature (first band that contains it,...
Definition mcp9700_types.hpp:645
constexpr Config LowNoisePreset() noexcept
32 conversions 200 us apart: averages ADC noise and mains pickup.
Definition mcp9700_types.hpp:699
constexpr Config Mcp9701Preset() noexcept
The MCP9701 transfer function and range.
Definition mcp9700_types.hpp:710
constexpr Config ConfigFor(Variant variant) noexcept
The configuration of a named part: Config{} with that variant.
Definition mcp9700_types.hpp:678
constexpr uint32_t kMaxAdcFullScaleMv
Largest declarable ADC full scale, millivolts.
Definition mcp9700_types.hpp:233
@ Warning
Something unexpected the driver continued from.
@ Debug
Detail for bring-up.
@ Mcp9701
400 mV at 0 C, 19.5 mV/C, -10 to +125 C, accuracy +-4 C.
@ Mcp9701A
As the MCP9701 with the +-2 C grade (0 to +70 C).
@ Mcp9700A
As the MCP9700 with the +-2 C grade (0 to +70 C).
@ Extended
-40 (MCP9701: -10) to +125 C, every part.
@ InvalidArgument
Validate() failed (at EnsureInitialized()); nothing was sent.
@ DeviceIdMismatch
Start-up found no plausible, stable output on the channel.
@ Crc
Not returned by this driver.
@ Bus
The port's ADC bring-up, a conversion or a pin write failed.
@ DeviceFault
Not returned by this driver.
@ OutputLow
The output is below the specified range minus the margin.
@ OutputHigh
The output is above the specified range plus the margin.
@ Timeout
Not returned by this driver.
@ Busy
Not returned by this driver.
Compile-time helpers of the MCP9700 driver: typed integer literals, exact hook matching,...
The MCP970x driver: everything it declares lives here; internals in detail.
Definition mcp9700.hpp:24
One accuracy limit of the datasheet: the error band guaranteed over a temperature span.
Definition mcp9700_types.hpp:296
bool high_only
The row exists for the High temperature class only.
Definition mcp9700_types.hpp:301
int32_t max_mc
Highest error, milli-C.
Definition mcp9700_types.hpp:300
int32_t to_mc
Upper end of the span, milli-C.
Definition mcp9700_types.hpp:298
int32_t min_mc
Lowest error, milli-C.
Definition mcp9700_types.hpp:299
int32_t from_mc
Lower end of the span, milli-C.
Definition mcp9700_types.hpp:297
The guaranteed error limits at one temperature, milli-C.
Definition mcp9700_types.hpp:343
int32_t min_mc
Lowest error.
Definition mcp9700_types.hpp:344
int32_t max_mc
Highest error.
Definition mcp9700_types.hpp:345
Everything EnsureInitialized() uses. Config{} is an MCP9700 on a 12 bit, 3.3 V ADC.
Definition mcp9700_types.hpp:274
uint8_t discard_conversions
Dummy conversions before a reading, up to kMaxDiscard.
Definition mcp9700_types.hpp:287
uint16_t divider_num
VOUT = pin x divider_num / divider_den.
Definition mcp9700_types.hpp:281
uint16_t divider_den
See divider_num.
Definition mcp9700_types.hpp:282
CustomSpec custom
Only with Variant::Custom.
Definition mcp9700_types.hpp:278
uint16_t samples
Conversions averaged per reading, 1 to kMaxSamples.
Definition mcp9700_types.hpp:285
uint32_t sample_interval_us
Wait between conversions, up to kMaxSampleIntervalUs.
Definition mcp9700_types.hpp:286
uint32_t max_spread_mv
Largest max-min of one reading at VOUT, mV.
Definition mcp9700_types.hpp:288
uint32_t power_on_settle_us
Wait after enabling the sensor, up to kMaxPowerOnSettleUs.
Definition mcp9700_types.hpp:289
uint32_t adc_full_scale_mv
Largest voltage the port can report, mV.
Definition mcp9700_types.hpp:279
int32_t range_margin_mc
Plausibility margin each side of the range, milli-C.
Definition mcp9700_types.hpp:284
Variant variant
A named part, or Custom.
Definition mcp9700_types.hpp:276
uint8_t adc_resolution_bits
ADC resolution, 8 to 16 bits.
Definition mcp9700_types.hpp:280
TemperatureClass range
High only for a part with that option.
Definition mcp9700_types.hpp:277
uint8_t channel
Passed to ReadVoltageMv() unchanged.
Definition mcp9700_types.hpp:275
int32_t offset_cal_mc
Added to the result: one-point calibration.
Definition mcp9700_types.hpp:283
What the user declares for Variant::Custom. Every field must be set (an all-zero declaration fails Va...
Definition mcp9700_types.hpp:260
int32_t accuracy_min_mc
Definition mcp9700_types.hpp:265
uint32_t v0_mv
Output at 0 C, millivolts (0 is valid).
Definition mcp9700_types.hpp:261
int32_t max_mc
Highest specified temperature, milli-C.
Definition mcp9700_types.hpp:264
uint32_t tc_uv_per_c
Slope, microvolts per C, 1000 to 100000 (positive only).
Definition mcp9700_types.hpp:262
int32_t accuracy_max_mc
Definition mcp9700_types.hpp:267
int32_t min_mc
Lowest specified temperature, milli-C.
Definition mcp9700_types.hpp:263
The temperature and the voltage behind it, from one validated reading.
Definition mcp9700_types.hpp:447
int32_t temperature_mc
Temperature after offset_cal_mc, milli-C.
Definition mcp9700_types.hpp:448
uint32_t spread_mv
Max minus min of the conversions, scaled, millivolts.
Definition mcp9700_types.hpp:450
uint32_t output_mv
Mean VOUT, scaled by the divider, millivolts.
Definition mcp9700_types.hpp:449
constexpr bool ok() const noexcept
Definition mcp9700_types.hpp:148
static constexpr Result Success() noexcept
Definition mcp9700_types.hpp:156
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition mcp9700_types.hpp:164
The value of a call, or why there is none.
Definition mcp9700_types.hpp:112
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition mcp9700_types.hpp:137
static constexpr Result Success(T v) noexcept
A successful result.
Definition mcp9700_types.hpp:129
constexpr bool ok() const noexcept
Definition mcp9700_types.hpp:117
One row of the variant table: the only place a coefficient, range, supply, accuracy band or package l...
Definition mcp9700_types.hpp:326
bool has_high
The row has a High class.
Definition mcp9700_types.hpp:332
uint8_t packages
kPackage* mask of the packages sold; 0 for Custom.
Definition mcp9700_types.hpp:336
uint32_t tested_vdd_mv
Supply at which the accuracy is tested, mV (0 unknown).
Definition mcp9700_types.hpp:335
int32_t max_ext_mc
Highest, Extended class, milli-C.
Definition mcp9700_types.hpp:330
uint8_t high_packages
kPackage* mask of the High option.
Definition mcp9700_types.hpp:337
std::array< AccuracyBand, kMaxBands > bands
Accuracy bands, narrowest first.
Definition mcp9700_types.hpp:339
int32_t min_mc
Lowest specified temperature, milli-C.
Definition mcp9700_types.hpp:329
uint32_t vdd_min_mv
Lowest supply, mV (0 when unknown).
Definition mcp9700_types.hpp:333
int32_t max_high_mc
Highest, High class, milli-C (= Extended when none).
Definition mcp9700_types.hpp:331
uint32_t vdd_max_mv
Highest supply, mV (0 when unknown).
Definition mcp9700_types.hpp:334
uint32_t tc_uv_per_c
Slope, microvolts per C (typical).
Definition mcp9700_types.hpp:328
uint8_t band_count
Used entries of bands.
Definition mcp9700_types.hpp:338
uint32_t v0_mv
Output at 0 C, millivolts (typical).
Definition mcp9700_types.hpp:327