nrfcxx
0.1.0
C++-17 Framework for Nordic nRF5 Devices
|
Go to the documentation of this file. 8 #ifndef NRFCXX_SENSOR_ADC_HPP 9 #define NRFCXX_SENSOR_ADC_HPP 13 #include <initializer_list> 54 vdd_callback_{callback},
77 int configure_bi_ ()
override;
78 void complete_bi_ ()
override;
81 volatile uint16_t vdd_adc16_;
100 using packed_type = uint32_t;
101 static constexpr
auto AIN_WIDTH = 4U;
102 static constexpr packed_type AIN_MASK = (1U << AIN_WIDTH) - 1;
105 static constexpr
auto MAX_INTERNAL = 2;
124 template <
typename Iterator>
125 int8_t pack_values (packed_type& dest,
129 static constexpr int8_t max_count = (8 *
sizeof(dest)) / AIN_WIDTH;
131 unsigned int shift = 0;
134 while ((begin != end) && (
count < max_count)) {
135 dest |= (AIN_MASK & *begin) << shift;
146 volatile uint16_t* raw_adc16_ ()
148 if (FL_RAW_EXTERNAL &
flags_) {
154 volatile const uint16_t* raw_adc16_ ()
const 156 if (FL_RAW_EXTERNAL &
flags_) {
162 void configure_instance_ (int8_t channel_count,
163 volatile uint16_t* raw);
196 template <
typename Iterator>
201 volatile uint16_t* raw =
nullptr) :
205 configure_instance_(pack_values(channel_ains_, ains_begin, ains_end), raw);
219 std::initializer_list<uint8_t> ains,
220 volatile uint16_t* raw =
nullptr) :
227 return channel_count_;
239 if (ci < channel_count_) {
240 rv = AIN_MASK & (channel_ains_ >> (ci * AIN_WIDTH));
255 if (ci < channel_count_) {
256 rv = raw_adc16_()[ci];
268 bool truncate =
false)
const;
312 return r2_Ohm * ((uint64_t{1} << 16) - m16) / m16;
314 return r1_Ohm * static_cast<uint64_t>(m16) / ((1U << 16) - m16);
332 bool filter_high_negative =
false)
const;
337 uint32_t channel_ains_ = 0;
340 uint8_t channel_count_ = 0;
344 uint8_t channel_idx_ = 0;
347 uint32_t config_ = 0;
356 volatile uint16_t* ptr;
357 uint16_t
volatile val[MAX_INTERNAL];
360 int configure_bi_ ()
override;
361 int nrf51_next_bi_ (
size_t ci)
override;
405 float b = 2.19852114398042317e-04,
406 float c = 1.72427510143621963e-07,
456 namespace steinhartHart {
555 template <
typename Iterator>
558 volatile uint16_t*raw =
nullptr,
560 unsigned int ref_Ohm = 10000) :
561 super{0, ref_Ohm, ains_begin, ains_end, raw},
567 volatile uint16_t*raw =
nullptr,
569 unsigned int ref_Ohm = 10000) :
596 unsigned int therm_Ohm (
size_t ci = 0)
const;
675 using flags_type = uint8_t;
678 enum flags_enum : flags_type
682 FL_CALIBRATING = 0x01,
694 FL_CALIBRATED = 0x10,
698 FL_THEN_SAMPLE = 0x20,
719 template <
typename Client = periph::ADCClient>
720 Client& client ()
const 722 return reinterpret_cast<Client&>(client_);
741 void lpsm_reset_ ()
override 747 int lpsm_process_ (
int& delay,
748 process_flags_type& lpf)
override;
750 periph::ADCClient& client_;
752 flags_type flags_bi_ = 0;
lpsm_wrapper(notifier_type notify, periph::ADCClient &client)
Construct an instance.
Definition: adc.hpp:713
int convert_adc16_mV(unsigned int vdd_adc16) const
Convert a measured voltage to VDD voltage.
const SteinhartHart adafruit372fullScale
Constants for full-scale measurement with Adafruit thermistor.
constexpr SteinhartHart(float a=1.19438315714902408e-03, float b=2.19852114398042317e-04, float c=1.72427510143621963e-07, uint16_t open_adc16=2281, uint16_t short_adc16=65133)
Store the coefficients and reference values.
Definition: adc.hpp:404
static constexpr int SHORT_cCel
Measurement returned by temperature_cCel() for a shorted thermistor.
Definition: adc.hpp:528
const SteinhartHart *const steinhartHart
The Steinhart-Hart coefficients and extreme values for the thermistor in the circuit.
Definition: adc.hpp:600
static constexpr int INVALID_cCel
An flag value for invalid temperatures.
Definition: adc.hpp:516
int sample_Ohm(size_t ci=0, bool filter_high_negative=false) const
Retrieve the latest estimated resistance for a channel.
unsigned int flags_type
Type for client-specific flags.
Definition: periph.hpp:2127
int temperature_cCel(size_t ci=0)
Convert the stored measured voltage to a thermisor.
int sample_mV(size_t ci=0, bool truncate=false) const
Retrieve the latest output voltage measurement for a channel.
int lpsm_bypass_calibration()
Set the internal flag that indicates calibration has been performed.
flags_type flags_
Flags for use by the core infrastructure and client specifications.
Definition: periph.hpp:2354
unsigned int therm_Ohm(size_t ci=0) const
Return the estimated thermistor resistance, in Ohms.
virtual int measurement_mV(uint16_t m16) const
Convert a raw ADC measurement to mV.
Definition: adc.hpp:290
Type holding Steinhart-Hart coefficients and extreme values.
Definition: adc.hpp:380
Material supporting low-power-mode operations.
int lpsm_calibrate()
Initiate a calibration.
vdd(vdd_callback_bi callback=nullptr)
Construct an instance that measures board Vdd.
Definition: adc.hpp:52
const unsigned int r1_Ohm
The resistance of the upper leg of the voltage divider, connected to the input voltage.
Definition: adc.hpp:172
Class supporting thermistor measurements.
Definition: adc.hpp:496
nvic_BlockIRQ as a template type.
Definition: core.hpp:497
const float a
Coefficient of ln(R)^0.
Definition: adc.hpp:417
int channel_ain(size_t ci) const
Extract the channel associated with the provided index.
Definition: adc.hpp:236
static constexpr state_type MS_SAMPLE
States available to implement a sample state.
Definition: lpm.hpp:192
unsigned int vdd_mV() const
Return the result of the most recent measurement.
Definition: adc.hpp:71
static constexpr int OPEN_cCel
Measurement returned by temperature_cCel() for an open thermistor.
Definition: adc.hpp:540
unsigned int therm_adc16(size_t ci=0) const
Return the last raw 16-bit ADC thermistor reading.
Base for subclass use of flags_.
Definition: periph.hpp:2154
static constexpr int ABSOLUTE_ZERO_cCel
Constant used to convert between Kelvin and Celsius.
Definition: adc.hpp:502
int measurement_Ohm(uint16_t m16) const
Convert a raw ADC measurement to the estimated resistance.
Definition: adc.hpp:306
std::function< void(unsigned int vdd_mV)> vdd_callback_bi
Type for a function invoked from the ADC IRQ to provide the calculated VDD.
Definition: adc.hpp:44
Base class for a client of ADC.
Definition: periph.hpp:2123
voltage_divider(unsigned int r1_Ohm, unsigned int r2_Ohm, Iterator ains_begin, Iterator ains_end, volatile uint16_t *raw=nullptr)
Generic constructor for an iterable set of channels and optional external storage.
Definition: adc.hpp:197
const SteinhartHart adafruit372refrigerator
Constants for full-scale measurement with Adafruit thermistor.
const float c
Coefficient of ln(R)^3.
Definition: adc.hpp:423
const unsigned int r2_Ohm
The resistance of the lower leg of the voltage divider, connected to ground.
Definition: adc.hpp:179
voltage_divider(unsigned int r1_Ohm, unsigned int r2_Ohm, uint8_t ain)
Constructor for common case of a single channel with internal storage.
Definition: adc.hpp:210
int temperature_cK(unsigned int therm_Ohm) const
Calculate the temperature corresponding to a measured thermistor resistance.
int sample_adc16(size_t ci=0) const
Retrieve the latest raw measurement for a channel.
Definition: adc.hpp:252
periph::ADCClient::mutex_type mutex_type
Mutex domain is that of the ADC.
Definition: adc.hpp:703
A class that implements an LPM state machine around a periph::ADCClient.
Definition: adc.hpp:626
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
voltage_divider(unsigned int r1_Ohm, unsigned int r2_Ohm, std::initializer_list< uint8_t > ains, volatile uint16_t *raw=nullptr)
Constructor for an explicit list of channels.
Definition: adc.hpp:217
const uint16_t open_adc16
16-bit ADC upper threshold for open thermistor.
Definition: adc.hpp:432
static constexpr state_type MS_EXIT_SAMPLE
States available to guard exit from a sample state.
Definition: lpm.hpp:201
static constexpr unsigned int VBG_mV
nRF51 reference voltage is 1.2 V.
Definition: impl.hpp:26
Base (or mixin) class for anything that supports a state_machine.
Definition: lpm.hpp:426
static constexpr auto PF_CALIBRATED
Sensor-specific indication from lpm::lpsm_capable::lpsm_process() that the ADC has been calibrated.
Definition: adc.hpp:708
static constexpr state_type MS_ENTRY_SAMPLE
States available to guard entry to a sample state.
Definition: lpm.hpp:183
static constexpr process_flags_type PF_APP_BASE
First lpsm_capable::lpsm_process() flag bit available for application-specific result code bits.
Definition: lpm.hpp:255
int input_mV(size_t ci=0) const
Retrieve the latest input voltage measurement for a channel.
peripheral::mutex_type mutex_type
Mutex required to inhibit ADC interrupts.
Definition: periph.hpp:2178
ADC instance to measure board Vdd.
Definition: adc.hpp:27
ntcThermistor(Iterator ains_begin, Iterator ains_end, volatile uint16_t *raw=nullptr, const SteinhartHart *coeff=&steinhartHart::adafruit372fullScale, unsigned int ref_Ohm=10000)
Generic constructor for an iterable set of thermistor channels and optional external storage.
Definition: adc.hpp:556
const SteinhartHart adafruit372hvac
Constants for HVAC measurement with Adafruit thermistor.
const uint16_t short_adc16
16-bit ADC lower threshold for shorted thermistor.
Definition: adc.hpp:441
const float b
Coefficient of ln(R)^1.
Definition: adc.hpp:420
ADC instance for voltage dividers.
Definition: adc.hpp:96
ntcThermistor(std::initializer_list< uint8_t > ains, volatile uint16_t *raw=nullptr, const SteinhartHart *coeff=&steinhartHart::adafruit372fullScale, unsigned int ref_Ohm=10000)
Constructor for an explicit list of channels.
Definition: adc.hpp:566
Abstraction of Nordic device peripherals.
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
size_t count() const
The number of channels used by this client.
Definition: adc.hpp:225
int convert_adc16_cCel(unsigned int therm_adc16)
Convert a measured voltage to a temperature.