24#include "boards/board_traits.h"
74 using TRAIT = board_traits::AnalogPin_trait<APIN>;
75 using GLOBAL_TRAIT = board_traits::GlobalAnalogPin_trait;
76 using AREF_TRAIT = board_traits::AnalogReference_trait<AREF>;
77 using TYPE_TRAIT = board_traits::AnalogSampleType_trait<SAMPLE_TYPE>;
78 using FREQ_TRAIT = board_traits::AnalogClock_trait<MAXFREQ>;
80 static constexpr const uint16_t BG_STABILIZATION_DELAY_US = 400;
96 static constexpr const uint8_t
PRESCALER = FREQ_TRAIT::PRESCALER;
107 GLOBAL_TRAIT::ADCSRA_.loop_until_bit_clear(ADSC);
109 GLOBAL_TRAIT::ADMUX_ = AREF_TRAIT::MASK | TYPE_TRAIT::ADLAR1 | TRAIT::MUX_MASK1;
110 GLOBAL_TRAIT::ADCSRB_ = TRAIT::MUX_MASK2 | TYPE_TRAIT::ADLAR2;
117 GLOBAL_TRAIT::ADCSRA_ =
bits::BV8(ADEN, ADSC) | TRAIT::MUX_MASK2 | FREQ_TRAIT::PRESCALER_MASK;
119 GLOBAL_TRAIT::ADCSRA_.loop_until_bit_clear(ADSC);
121 return TYPE_TRAIT::ADC_;
133 template<board::AnalogPin BANDGAP_ = board::AnalogPin::BANDGAP>
135 :
public AnalogInput<BANDGAP_, uint16_t, board::AnalogReference::AVCC, board::AnalogClock::MAX_FREQ_50KHz>
144 using TRAIT = board_traits::AnalogPin_trait<BANDGAP>;
145 static_assert(TRAIT::IS_BANDGAP,
"BANDGAP_ parameter must be a bandgap ADC input");
146 static constexpr const uint16_t REFERENCE_MV = TRAIT::BANDGAP_VOLTAGE_MV;
161 uint16_t rate = this->
template sample();
163 return REFERENCE_MV * 1024L / rate;
API that uses bandgap feature to calculate current voltage fed to the MCU.
uint16_t voltage_mV()
Get the voltage, in mV, feeding the MCU.
static constexpr const board::AnalogPin BANDGAP
The bandgap analog pin of the MCU target, used for this PowerVoltage.
Defines all API to manipulate analog input/output.
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
AnalogClock
Defines available clocks of the target MCU, used for analog input.
AnalogPin
Defines all available analog input pins of the target MCU.
AnalogReference
Defines available voltage references of the target MCU, used for analog input.
void delay_us(uint16_t us) INLINE
Delay program execution for the given amount of microseconds.