Skip to content

Architecture

How the driver is built: the core, the free conversion functions, the port contract, and your port.

Layer diagram: your application calls the NtcThermistor core, which asks for ADC conversions through the ADC port contract to the port class you write, which reads the divider; the core and the contract are the same on every MCU.Layer diagram: your application calls the NtcThermistor core, which asks for ADC conversions through the ADC port contract to the port class you write, which reads the divider; the core and the contract are the same on every MCU.
The layers, one of them yours
Figure description and sources

Top to bottom, each arrow is a call. Your application calls one object, whose public calls are listed from the umbrella header in inc/. The core turns each call into register reads and writes and hands them to the port contract (the CRTP base in inc/*_interface.hpp), which forwards them, at compile time, to the hooks of the port class you write; your port moves the bytes on the bus. The notes on the right say which layers stay the same on every MCU and which one you write. A library without a port contract has only the first two layers.

Drawn from: inc/ntc_thermistor.hpp public calls; inc/ntc_thermistor_adc_interface.hpp AdcInterface; figure module docs/diagrams/figures/driver-layers.py.

Layer Files Owns
Core inc/ntc_thermistor.hpp, src/ntc_thermistor.ipp Lifecycle, sampling, the bias sequence, the port hooks
Conversion inc/ntc_thermistor_conversion.hpp The pure maths: classification, divider, curve, bound
Maths inc/ntc_thermistor_math.hpp Ln16(), rounded and saturating integer helpers
Types inc/ntc_thermistor_types.hpp Error, Result<T>, Curve, Config, Reading, Validate()
Presets inc/ntc_thermistor_presets.hpp Two example curves, as data; included by nothing in the driver
Port contract inc/ntc_thermistor_adc_interface.hpp The CRTP base and the AdcPort concept
Platform port Your class ADC bring-up, one raw conversion, optional delay and bias

The conversion is outside the class

The conversion does not depend on the port, so it lives in free functions, not in the class template: it is compiled once per configuration, not once per port, can be used (and tested) with no port at all, and is evaluated at compile time when the inputs are constexpr. NtcThermistor<Port>::Read() is sampling around one call to it.

Pipeline of six stages: the mean of the samples, the classification of the code as open circuit, short circuit or out of scale, the divider's node ratio giving the resistance, the curve (table, Steinhart-Hart or beta) giving the temperature, the window and offset, and the uncertainty bound.Pipeline of six stages: the mean of the samples, the classification of the code as open circuit, short circuit or out of scale, the divider's node ratio giving the resistance, the curve (table, Steinhart-Hart or beta) giving the temperature, the window and offset, and the uncertainty bound.
From an ADC code to a temperature
Figure description and sources

Top to bottom, each arrow hands one number to the next stage. Read() takes sample_count codes and their mean (the spread check can stop it as Unstable). The mean code is classified: at or above 2^bits is CodeOutOfScale, a code in the margin at the open rail is OpenCircuit, one in the margin at the short rail is ShortCircuit; which rail is which depends on the divider side. The node ratio gives the thermistor's resistance from the series resistor, in 1/256 ohm. The resistance goes through the curve: a table (a bounded binary search, then linear interpolation in ln R) or Steinhart-Hart (a cubic in ln R) or the beta equation (1/T in Q32), any of them with no floating point and no library. The temperature must lie inside the window; the offset is added last. The uncertainty bound is computed from the same code. The stages are the functions of inc/ntc_thermistor_conversion.hpp.

Drawn from: inc/ntc_thermistor_conversion.hpp ConvertCode(); inc/ntc_thermistor_conversion.hpp FineOfCode(); inc/ntc_thermistor_conversion.hpp CurveTemperature(); figure module docs/diagrams/figures/conversion-pipeline.py.

Rules the code keeps

  • Integer only. Resistance in ohms (1/256 inside), temperature in milli-degrees, voltage in millivolts, tolerance in parts per million. No float, no double, no library math.
  • A part is data. Nothing in inc/ or src/ names a part; the presets are Curve values.
  • Bounded. Every loop has a constant bound: the table search is at most 8 steps, Ln16() a fixed number of steps.
  • Port-independent code stays outside the class template.

Next: Contract.