skullc-peripherals/Peripherals/Inc/peripherals_adc.hpp
Erki a14059f997
All checks were successful
continuous-integration/drone/push Build is passing
Add Periperhals/IrSensors module
2021-04-11 13:16:12 +03:00

63 lines
1.3 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::uint32_t channel, const std::uint32_t sampling_time, const std::uint32_t timeout = 100)
{
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, timeout);
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_ */