diff --git a/Peripherals/Inc/peripherals_adc.hpp b/Peripherals/Inc/peripherals_adc.hpp new file mode 100644 index 0000000..770c929 --- /dev/null +++ b/Peripherals/Inc/peripherals_adc.hpp @@ -0,0 +1,71 @@ +/* + * peripherals_adc.hpp + * + * Created on: Mar 28, 2021 + * Author: erki + */ + +#ifndef PERIPHERALS_INC_PERIPHERALS_ADC_HPP_ +#define PERIPHERALS_INC_PERIPHERALS_ADC_HPP_ + +#include +#include + +#include + +namespace Peripherals +{ + +template +struct Adc +{ + std::array readings = { 0 }; + ADC_HandleTypeDef* hadc = nullptr; + + static constexpr std::uint32_t readingsCount() + { + return N; + } + + Adc() = delete; + + explicit Adc(ADC_HandleTypeDef* hadc) + : hadc(hadc) + { } + + Adc(const Adc&) = delete; + Adc(Adc&&) = delete; + Adc& operator=(const Adc&) = delete; + Adc& operator=(Adc&&) = delete; + + std::uint16_t read(const std::uint8_t channel, std::uint32_t sampling_time) + { + ADC_ChannelConfTypeDef conf; + + conf.Channel = channel; + conf.Rank = 1; + conf.SamplingTime = sampling_time; + + HAL_ADC_ConfigChannel(hadc, &conf); + + HAL_ADC_Start(hadc); + HAL_ADC_PollForConversion(hadc, 100); + + return HAL_ADC_GetValue(hadc); + } + + void startDma() + { + HAL_ADC_Start_DMA(hadc, reinterpret_cast(readings.data()), readingsCount()); + } + + void stopDma() + { + HAL_ADC_Stop_DMA(hadc); + } +}; + +} + + +#endif /* PERIPHERALS_INC_PERIPHERALS_ADC_HPP_ */