28static constexpr int ADC_RESOLUTION = 4095;
33#if defined(STM32F407xx) || defined(STM32F429xx)
34#define CAL_PT1_VALUE ((uint16_t volatile*)((uint32_t)0x1FFF7A2C))
35#define CAL_PT2_VALUE ((uint16_t volatile*)((uint32_t)0x1FFF7A2E))
36static const float CAL_PT1_TEMP = 30;
37static const float CAL_PT2_TEMP = 110;
38static const float CAL_V_DDA = 3.3f;
39#elif defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F756xx)
40#define CAL_PT1_VALUE ((uint16_t volatile*)((uint32_t)0x1FF0F44C))
41#define CAL_PT2_VALUE ((uint16_t volatile*)((uint32_t)0x1FF0F44E))
42static const float CAL_PT1_TEMP = 30;
43static const float CAL_PT2_TEMP = 110;
44static const float CAL_V_DDA = 3.3f;
46#warning This micro controller does not have a calibrated temperature sensor or is not currently supported by this driver
49#if defined(STM32F407xx) || defined(STM32F205xx)
52static const float VBAT_DIV = 2.0f;
53#elif defined(STM32F429xx) || defined(STM32F767xx) || defined(STM32F769xx) || \
57static const float VBAT_DIV = 4.0f;
64#warning Missing V_DDA_VOLTAGE definition for current target, using default value of 3.0V
65#define V_DDA_VOLTAGE 3.0f
70#ifndef INTERNAL_ADC_WITHOUT_CALIBRATION
71 loadCalibrationValues();
84 ADC->CCR |= ADC_CCR_ADCPRE_1 | ADC_CCR_ADCPRE_0;
86 for (
int i = 0; i <
CH_NUM; i++)
87 channelsEnabled[i] =
false;
99 adc->CR2 |= ADC_CR2_ADON;
111 for (
int i = 0; i <
CH16; i++)
113 if (channelsEnabled[i])
132 ADC->CCR |= ADC_CCR_TSVREFE;
134 auto temperatureRawValue = readChannel(
static_cast<Channel>(TEMP_CH));
135 ADC->CCR &= ~ADC_CCR_TSVREFE;
138 if (temperatureRawValue != 0)
143#ifdef INTERNAL_ADC_WITHOUT_CALIBRATION
161 ADC->CCR |= ADC_CCR_VBATE;
162 auto vbatVoltageRawValue = readChannel(
static_cast<Channel>(VBAT_CH));
163 ADC->CCR &= ~ADC_CCR_VBATE;
166 vbatVoltageRawValue *
V_DDA_VOLTAGE / ADC_RESOLUTION * VBAT_DIV;
174 channelsEnabled[channel] =
true;
176 setChannelSampleTime(channel, sampleTime);
181 channelsEnabled[channel] =
false;
193 if (!vbatEnabled || TEMP_CH != VBAT_CH)
206 if (!tempEnabled || TEMP_CH != VBAT_CH)
236inline void InternalADC::resetRegisters()
255inline void InternalADC::setChannelSampleTime(Channel channel,
256 SampleTime sampleTime)
260 adc->SMPR2 &= ~(0x7 << (channel * 3));
261 adc->SMPR2 |= sampleTime << (channel * 3);
265 adc->SMPR1 &= ~(0x7 << ((channel - 10) * 3));
266 adc->SMPR1 |= sampleTime << ((channel - 10) * 3);
270uint16_t InternalADC::readChannel(Channel channel)
278 adc->CR2 |= ADC_CR2_SWSTART;
280 while (!(adc->SR & ADC_SR_EOC))
283 return static_cast<uint16_t
>(adc->DR);
286#ifndef INTERNAL_ADC_WITHOUT_CALIBRATION
287void InternalADC::loadCalibrationValues()
289 calPt1Voltage =
static_cast<float>(*CAL_PT1_VALUE);
290 calPt1Voltage *= CAL_V_DDA / ADC_RESOLUTION;
292 calPt2Voltage =
static_cast<float>(*CAL_PT2_VALUE);
293 calPt2Voltage *= CAL_V_DDA / ADC_RESOLUTION;
295 calSlope = calPt2Voltage - calPt1Voltage;
296 calSlope /= CAL_PT2_TEMP - CAL_PT1_TEMP;
bool init() override
ADC Initialization.
TemperatureData getTemperature()
void enableChannel(Channel channel, SampleTime sampleTime=CYCLES_480)
ADCData getVoltage(Channel channel)
SampleTime
Conversion sample time. See reference manual.
void enableVbat(SampleTime sampleTime=CYCLES_480)
void disableChannel(Channel channel)
bool selfTest() override
Check if the sensor is working.
InternalADCData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
void enableTemperature(SampleTime sampleTime=CYCLES_480)
InternalADC(ADC_TypeDef *adc)
Resets the ADC configuration and automatically enables the peripheral clock.
Channel
ADC channels enumeration.
void disableTemperature()
virtual InternalADCData getLastSample()
bool disablePeripheralClock(void *peripheral)
Disables a peripheral clock source from the APB1 and APB2 peripheral buses.
bool enablePeripheralClock(void *peripheral)
Enables a peripheral clock source from the APB1 and APB2 peripheral buses.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
Driver for the VN100S IMU.
Structure to handle ADC data.
uint64_t voltageTimestamp
uint64_t temperatureTimestamp