skullc-peripherals/Peripherals/Inc/peripherals_adc.hpp
Erki 55a8efa579
All checks were successful
continuous-integration/drone/push Build is passing
Clang format pass
2021-04-03 17:49:25 +03:00

64 lines
1.2 KiB
C++

/*
* peripherals_adc.hpp
*
* Created on: Mar 28, 2021
* Author: erki
*/
#ifndef PERIPHERALS_INC_PERIPHERALS_ADC_HPP_
#define PERIPHERALS_INC_PERIPHERALS_ADC_HPP_
#include <array>
#include <cstdint>
#include <adc.h>
namespace Peripherals
{
template<std::size_t N>
struct Adc
{
std::array<std::uint16_t, N> 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<std::uint32_t*>(readings.data()),
readingsCount());
}
void stopDma() { HAL_ADC_Stop_DMA(hadc); }
};
}// namespace Peripherals
#endif /* PERIPHERALS_INC_PERIPHERALS_ADC_HPP_ */