Skyward boardcore
Loading...
Searching...
No Matches
InternalADC.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2020 Skyward Experimental Rocketry
2 * Authors: Matteo Piazzolla, Luca Erbetta, Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <miosix.h>
26#include <sensors/Sensor.h>
27
28#include "InternalADCData.h"
29
30#if not defined(STM32F407xx) && not defined(STM32F429xx) && \
31 not defined(STM32F767xx) && not defined(STM32F769xx)
32#define INTERNAL_ADC_WITHOUT_CALIBRATION
33#endif
34
35namespace Boardcore
36{
37
50class InternalADC : public Sensor<InternalADCData>
51{
52public:
79
85 enum SampleTime : uint8_t
86 {
87 // CYCLES_3 = 0x0,
88 CYCLES_15 = 0x1,
89 CYCLES_28 = 0x2,
90 CYCLES_56 = 0x3,
91 CYCLES_84 = 0x4,
94 CYCLES_480 = 0x7
95 };
96
101 explicit InternalADC(ADC_TypeDef* adc);
102
103 ~InternalADC();
104
112 bool init() override;
113
114 bool selfTest() override;
115
116 InternalADCData sampleImpl() override;
117
118 void enableChannel(Channel channel, SampleTime sampleTime = CYCLES_480);
119
120 void disableChannel(Channel channel);
121
122 void enableTemperature(SampleTime sampleTime = CYCLES_480);
123
124 void disableTemperature();
125
126 void enableVbat(SampleTime sampleTime = CYCLES_480);
127
128 void disableVbat();
129
130 ADCData getVoltage(Channel channel);
131
133
135
136private:
137 void resetRegisters();
138
139 void setChannelSampleTime(Channel channel, SampleTime sampleTime);
140
141 uint16_t readChannel(Channel channel);
142
143 ADC_TypeDef* adc;
144
145 bool channelsEnabled[CH_NUM];
146 bool tempEnabled = false;
147 bool vbatEnabled = false;
148
149#ifndef INTERNAL_ADC_WITHOUT_CALIBRATION
150 void loadCalibrationValues();
151
152 // Factory calibration values
153 // Read "Temperature sensor characteristics" chapter in the datasheet
154 float calPt1Voltage;
155 float calPt2Voltage;
156 float calSlope;
157#endif
158};
159
160} // namespace Boardcore
Driver for stm32 internal ADC.
Definition InternalADC.h:51
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.
Definition InternalADC.h:86
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.
Definition InternalADC.h:57
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
This file includes all the types the logdecoder script will decode.
Structure to handle ADC data.
Definition SensorData.h:356