From 266cc46a09afe2be3fb61d98d3faf12e9a96c0d8 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Sun, 16 Jul 2023 21:17:14 +0200 Subject: [PATCH] Added ADC implementation for STM32 --- include/sta/devices/stm32/adc.hpp | 20 +++++++++++++++++-- src/devices/stm32/adc.cpp | 33 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/devices/stm32/adc.cpp diff --git a/include/sta/devices/stm32/adc.hpp b/include/sta/devices/stm32/adc.hpp index b77555f..b1411be 100644 --- a/include/sta/devices/stm32/adc.hpp +++ b/include/sta/devices/stm32/adc.hpp @@ -11,10 +11,26 @@ namespace sta class STM32ADC { public: - STM32ADC(); + /** + * @param handle A handle to a STM32 ADC. + */ + STM32ADC(ADC_HandleTypeDef * handle); + /** + * @brief Starts conversion of the incoming analog signal. + */ + void start(); + + /** + * @brief + * + * @param timeout + */ + void poll(uint32_t timeout); + + uint32_t getValue(); private: - + ADC_HandleTypeDef * handle_; }; } // namespace sta diff --git a/src/devices/stm32/adc.cpp b/src/devices/stm32/adc.cpp new file mode 100644 index 0000000..01c9eb7 --- /dev/null +++ b/src/devices/stm32/adc.cpp @@ -0,0 +1,33 @@ +#include + +#ifdef STA_PLATFORM_STM32 + +#include + +namespace sta +{ + STM32ADC::STM32ADC(ADC_HandleTypeDef * handle) + : handle_{handle} + { + STA_ASSERT(handle != nullptr); + } + + void STM32ADC::start() + { + HAL_ADC_Start(handle_); + } + + void STM32ADC::poll(uint32_t timeout) + { + HAL_StatusTypeDef res = HAL_ADC_PollForConversion(handle_, timeout); + + STA_ASSERT(res == HAL_OK); + } + + uint32_t STM32ADC::getValue() + { + return HAL_ADC_GetValue(handle_); + } +} // namespace sta + +#endif // STA_PLATFORM_STM32 \ No newline at end of file