MCP9700 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
Public Member Functions | List of all members
mcp9700::MCP9700< Port > Class Template Reference

Driver for one MCP970x analog temperature sensor (or a declared Custom part) on an ADC port. More...

#include <mcp9700.hpp>

Public Member Functions

 MCP9700 (Port &port, const Config &config={}) noexcept
 Bind the driver to a port and store the configuration. No I/O, cannot fail.
 
 MCP9700 (const MCP9700 &)=delete
 One driver object per sensor.
 
MCP9700 & operator= (const MCP9700 &)=delete
 One driver object per sensor.
 
Result< void > EnsureInitialized () noexcept
 Start the sensor, once: the only way to initialise the driver.
 
bool IsInitialized () const noexcept
 
Result< void > Deinitialize () noexcept
 Mark the driver uninitialised and switch the sensor off when the port has GpioSet(); the next EnsureInitialized() starts it again (the recovery after a fault). Ok with no I/O when it was not initialised.
 
Result< void > Sleep () noexcept
 Switch the sensor off; EnsureInitialized() powers it again.
 
Result< Measurement > ReadMeasurement () noexcept
 The validated reading: discard_conversions dummy conversions, then samples averaged.
 
Result< int32_t > ReadTemperatureMilliC () noexcept
 The temperature of ReadMeasurement(), milli-degrees Celsius, after offset_cal_mc.
 
Result< uint32_t > ReadOutputMv () noexcept
 The mean output voltage at VOUT (scaled by the divider), validated like a reading.
 
Result< uint32_t > ReadRawPortMv () noexcept
 One conversion, once: no scaling, no check, no retry (handbook KB-CPP-129). For bring-up.
 
uint32_t GetLastRawPortMv () const noexcept
 
uint32_t ResolutionMilliC () const noexcept
 One ADC step expressed in milli-degrees at the current slope, rounded up. No I/O.
 
const Config & GetConfig () const &noexcept
 
void GetConfig () const &&=delete
 Not on a temporary driver: the reference would dangle (handbook KB-CPP20-044).
 
Result< VariantSpec > GetSpec () const noexcept
 The effective variant row (a table row, or the one built from the Custom declaration). No I/O.
 

Detailed Description

template<AdcPort Port>
class mcp9700::MCP9700< Port >

Driver for one MCP970x analog temperature sensor (or a declared Custom part) on an ADC port.

Lifecycle: construct with the configuration (no I/O), then EnsureInitialized(), the one way to start, use, optionally Deinitialize() or Sleep(). Every call that touches the sensor returns Error::NotInitialized until EnsureInitialized() has succeeded; no call starts it as a side effect.

Hooks beyond the two required ADC ones: void DelayUs(uint32_t) (required by every wait: a missing one is a compile error), bool GpioSet(CtrlPin, GpioSignal) (the sensor's supply from an MCU pin; without it Sleep() returns Unsupported), void Log(LogLevel, const char*).

A reading never retries and never clamps: a failed read is reported once, and a failed read does not uninitialise the driver.

Template Parameters
PortYour class derived from AdcInterface<Port>.

Constructor & Destructor Documentation

◆ MCP9700()

template<AdcPort Port>
mcp9700::MCP9700< Port >::MCP9700 ( Port &  port,
const Config &  config = {} 
)
inlineexplicitnoexcept

Bind the driver to a port and store the configuration. No I/O, cannot fail.

Parameters
portThe port; it must outlive the driver.
configThe sensor and the ADC in front of it, checked by EnsureInitialized() before any I/O.

Member Function Documentation

◆ Deinitialize()

template<AdcPort Port>
Result< void > mcp9700::MCP9700< Port >::Deinitialize ( )
noexcept

Mark the driver uninitialised and switch the sensor off when the port has GpioSet(); the next EnsureInitialized() starts it again (the recovery after a fault). Ok with no I/O when it was not initialised.

Returns
Ok or Bus (the pin write failed; the driver is uninitialised either way and the sensor may still be powered). A second Deinitialize() does nothing; to retry the switch-off call EnsureInitialized() and then Sleep() or Deinitialize() again.

◆ EnsureInitialized()

template<AdcPort Port>
Result< void > mcp9700::MCP9700< Port >::EnsureInitialized ( )
noexcept

Start the sensor, once: the only way to initialise the driver.

Checks the configuration, brings the ADC up, powers the sensor (GpioSet, when the port has it), waits power_on_settle_us (the datasheet gives t_ON typical only, 800 us), then takes one validated reading: the analog equivalent of a chip-ID check. After it succeeds, later calls return Ok at once with no I/O. After it fails the sensor is switched off again (best effort), the driver stays uninitialised and the next call starts again.

Returns
Ok, InvalidArgument (the configuration fails Validate(); no I/O), Bus, or DeviceIdMismatch (no plausible, stable output on the channel).

◆ GetConfig()

template<AdcPort Port>
const Config & mcp9700::MCP9700< Port >::GetConfig ( ) const &
inlinenoexcept
Returns
The configuration stored by the constructor. No I/O.

◆ GetLastRawPortMv()

template<AdcPort Port>
uint32_t mcp9700::MCP9700< Port >::GetLastRawPortMv ( ) const
inlinenoexcept
Returns
The last single conversion any read made, millivolts at the pin; 0 before the first, after a failed conversion and after Deinitialize()/Sleep(). No I/O.

◆ GetSpec()

template<AdcPort Port>
Result< VariantSpec > mcp9700::MCP9700< Port >::GetSpec ( ) const
inlinenoexcept

The effective variant row (a table row, or the one built from the Custom declaration). No I/O.

Returns
The row, or InvalidArgument when the configuration is invalid.

◆ IsInitialized()

template<AdcPort Port>
bool mcp9700::MCP9700< Port >::IsInitialized ( ) const
inlinenoexcept
Returns
true after a successful EnsureInitialized(). No I/O.

◆ ReadMeasurement()

template<AdcPort Port>
Result< Measurement > mcp9700::MCP9700< Port >::ReadMeasurement ( )
noexcept

The validated reading: discard_conversions dummy conversions, then samples averaged.

Sends exactly discard_conversions + samples conversions, never retries. Checks, in this order of precedence: Bus (stops at the first failed conversion), Saturated, Unstable, then OutputLow / OutputHigh against the specified range plus range_margin_mc.

Returns
The measurement, or NotInitialized, Bus, Saturated, Unstable, OutputLow or OutputHigh.

◆ ReadOutputMv()

template<AdcPort Port>
Result< uint32_t > mcp9700::MCP9700< Port >::ReadOutputMv ( )
noexcept

The mean output voltage at VOUT (scaled by the divider), validated like a reading.

Returns
Millivolts, or the errors of ReadMeasurement().

◆ ReadRawPortMv()

template<AdcPort Port>
Result< uint32_t > mcp9700::MCP9700< Port >::ReadRawPortMv ( )
noexcept

One conversion, once: no scaling, no check, no retry (handbook KB-CPP-129). For bring-up.

Returns
Millivolts at the ADC pin as the port reported them, or NotInitialized or Bus.

◆ ReadTemperatureMilliC()

template<AdcPort Port>
Result< int32_t > mcp9700::MCP9700< Port >::ReadTemperatureMilliC ( )
noexcept

The temperature of ReadMeasurement(), milli-degrees Celsius, after offset_cal_mc.

Returns
The temperature, or the errors of ReadMeasurement().

◆ ResolutionMilliC()

template<AdcPort Port>
uint32_t mcp9700::MCP9700< Port >::ResolutionMilliC ( ) const
noexcept

One ADC step expressed in milli-degrees at the current slope, rounded up. No I/O.

Returns
The step, or 0 when the configuration is invalid.

◆ Sleep()

template<AdcPort Port>
Result< void > mcp9700::MCP9700< Port >::Sleep ( )
noexcept

Switch the sensor off; EnsureInitialized() powers it again.

Returns
Ok, NotInitialized, Unsupported (no GpioSet()) or Bus (the pin write failed: see Deinitialize() for the retry).

The documentation for this class was generated from the following file: