From d1db8a9bf95ffdd3e15df682f6805a7456f9bd97 Mon Sep 17 00:00:00 2001 From: Samuel Shi Date: Sun, 17 Nov 2024 00:21:40 -0500 Subject: [PATCH 1/2] primitive analog_input.h --- firmware/mcal/cli/periph/analog_input.h | 28 +++++++++++++++++++++++++ firmware/shared/periph/analog_input.h | 17 +++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 firmware/mcal/cli/periph/analog_input.h create mode 100644 firmware/shared/periph/analog_input.h diff --git a/firmware/mcal/cli/periph/analog_input.h b/firmware/mcal/cli/periph/analog_input.h new file mode 100644 index 000000000..ae71da89b --- /dev/null +++ b/firmware/mcal/cli/periph/analog_input.h @@ -0,0 +1,28 @@ +/// @author Samuel Shi +/// @date 2024-11-17 + +#pragma once + +#include +#include "shared/periph/analog_input.h" + +namespace mcal::cli::periph{ + +class AnalogInput : public shared::periph::AnalogInput { + void Start() override { + std::cout << "Reading ADC " << name_ << "..." << std::endl; + } + + float Read() override { + Start(); + float adc_val; + std::cout << " | Enter an unsigned 32-bit value: "; + std::cin >> adc_val; + std::cout << " | Obtained value " << adc_val << std::endl; + return adc_val; + } + private: + std::string name_; +}; + +} diff --git a/firmware/shared/periph/analog_input.h b/firmware/shared/periph/analog_input.h new file mode 100644 index 000000000..0d9a8e6d8 --- /dev/null +++ b/firmware/shared/periph/analog_input.h @@ -0,0 +1,17 @@ +/// @author Samuel Shi +/// @date 2024-11-17 +/// Modified from adc.h + +#pragma once + +#include "shared/util/peripheral.h" + +namespace shared::periph { + +class AnalogInput : public util::Peripheral { +public: + virtual void Start() = 0; + virtual float Read() = 0; +}; + +} // namespace shared::periph \ No newline at end of file From 2bbb9590f24df06c79765d1b1f6bf2cc99a68174 Mon Sep 17 00:00:00 2001 From: Samuel Shi Date: Sun, 24 Nov 2024 18:42:12 -0500 Subject: [PATCH 2/2] demo for cli --- firmware/mcal/cli/periph/analog_input.h | 7 ++- firmware/mcal/stm32f767/periph/analog_input.h | 47 +++++++++++++++++++ .../projects/Demo/AnalogInput/CMakeLists.txt | 1 + firmware/projects/Demo/AnalogInput/bindings.h | 11 +++++ firmware/projects/Demo/AnalogInput/main.cc | 11 +++++ .../AnalogInput/platforms/cli/CMakeLists.txt | 3 ++ .../AnalogInput/platforms/cli/bindings.cc | 21 +++++++++ .../AnalogInput/platforms/cli/mcal_conf.cmake | 1 + .../platforms/stm32f767/CMakeLists.txt | 3 ++ .../platforms/stm32f767/bindings.cc | 21 +++++++++ .../platforms/stm32f767/mcal_conf.cmake | 1 + 11 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 firmware/mcal/stm32f767/periph/analog_input.h create mode 100644 firmware/projects/Demo/AnalogInput/CMakeLists.txt create mode 100644 firmware/projects/Demo/AnalogInput/bindings.h create mode 100644 firmware/projects/Demo/AnalogInput/main.cc create mode 100644 firmware/projects/Demo/AnalogInput/platforms/cli/CMakeLists.txt create mode 100644 firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc create mode 100644 firmware/projects/Demo/AnalogInput/platforms/cli/mcal_conf.cmake create mode 100644 firmware/projects/Demo/AnalogInput/platforms/stm32f767/CMakeLists.txt create mode 100644 firmware/projects/Demo/AnalogInput/platforms/stm32f767/bindings.cc create mode 100644 firmware/projects/Demo/AnalogInput/platforms/stm32f767/mcal_conf.cmake diff --git a/firmware/mcal/cli/periph/analog_input.h b/firmware/mcal/cli/periph/analog_input.h index ae71da89b..30f6a8469 100644 --- a/firmware/mcal/cli/periph/analog_input.h +++ b/firmware/mcal/cli/periph/analog_input.h @@ -9,6 +9,9 @@ namespace mcal::cli::periph{ class AnalogInput : public shared::periph::AnalogInput { +public: + AnalogInput(std::string name) : name_(name) {} + void Start() override { std::cout << "Reading ADC " << name_ << "..." << std::endl; } @@ -18,8 +21,8 @@ class AnalogInput : public shared::periph::AnalogInput { float adc_val; std::cout << " | Enter an unsigned 32-bit value: "; std::cin >> adc_val; - std::cout << " | Obtained value " << adc_val << std::endl; - return adc_val; + std::cout << " | Obtained value " << (adc_val/4095.0f * 3.3f) << std::endl; + return adc_val/4095.0f * 3.3f; } private: std::string name_; diff --git a/firmware/mcal/stm32f767/periph/analog_input.h b/firmware/mcal/stm32f767/periph/analog_input.h new file mode 100644 index 000000000..530dda714 --- /dev/null +++ b/firmware/mcal/stm32f767/periph/analog_input.h @@ -0,0 +1,47 @@ + + +#pragma once + +#include + +#include "shared/periph/analog_input.h" +#include "stm32f7xx_hal.h" + +namespace mcal::stm32f767::periph { + +class AnalogInput : public shared::periph::AnalogInput { +private: + ADC_HandleTypeDef* hadc_; + uint32_t adc_channel_; + +public: + AnalogInput(ADC_HandleTypeDef* hadc, uint32_t adc_channel) + : hadc_(hadc), adc_channel_(adc_channel){}; + + void Start() override { + ADC_ChannelConfTypeDef adc_config = { + .Channel = adc_channel_, + .Rank = ADC_REGULAR_RANK_1, + .SamplingTime = ADC_SAMPLETIME_28CYCLES, + .Offset = 0}; + + HAL_ADC_ConfigChannel(hadc_, &adc_config); + HAL_ADC_Start(hadc_); + } + + float Read() override { + /// @todo should there be a standard output range? + /// if so, the conversion would require knowing the resolution and + /// alignment settings + + /// @todo change this to be non-blocking, currently just for demo + Start(); + HAL_ADC_PollForConversion(hadc_, 1000); + uint32_t adc_value = HAL_ADC_GetValue(hadc_); + HAL_ADC_Stop(hadc_); + + return adc_value/4095.0f * 3.3f; + } +}; + +} // namespace mcal::stm32f767::periph diff --git a/firmware/projects/Demo/AnalogInput/CMakeLists.txt b/firmware/projects/Demo/AnalogInput/CMakeLists.txt new file mode 100644 index 000000000..d528587c7 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(main PUBLIC main.cc) \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/bindings.h b/firmware/projects/Demo/AnalogInput/bindings.h new file mode 100644 index 000000000..b0c2ddd44 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/bindings.h @@ -0,0 +1,11 @@ +#pragma once + +#include "shared/periph/analog_input.h" + +namespace bindings { + +extern shared::periph::AnalogInput& analog_input; + +extern void Init(); + +} // namespace bindings \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/main.cc b/firmware/projects/Demo/AnalogInput/main.cc new file mode 100644 index 000000000..f3528e69a --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/main.cc @@ -0,0 +1,11 @@ +#include "bindings.h" + + +int main(){ + bindings::Init(); + + while (true) { + bindings::analog_input.Read(); + } + return 0; +} \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/platforms/cli/CMakeLists.txt b/firmware/projects/Demo/AnalogInput/platforms/cli/CMakeLists.txt new file mode 100644 index 000000000..6748669b7 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/platforms/cli/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(bindings PRIVATE bindings.cc) + +target_link_libraries(bindings PUBLIC mcal-cli) \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc b/firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc new file mode 100644 index 000000000..a66c52765 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc @@ -0,0 +1,21 @@ +#include + +#include "../../bindings.h" +#include "mcal/cli/periph/analog_input.h" +#include "shared/periph/analog_input.h" + +namespace mcal { + +cli::periph::AnalogInput analog_input{"analog input"}; + +} // namespace mcal + +namespace bindings { + +shared::periph::AnalogInput& analog_input = mcal::analog_input; + +void Init() { + std::cout << "Initializing the CLI..." << std::endl; +} + +} // namespace bindings \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/platforms/cli/mcal_conf.cmake b/firmware/projects/Demo/AnalogInput/platforms/cli/mcal_conf.cmake new file mode 100644 index 000000000..a30e90e12 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/platforms/cli/mcal_conf.cmake @@ -0,0 +1 @@ +set(MCAL cli) \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/platforms/stm32f767/CMakeLists.txt b/firmware/projects/Demo/AnalogInput/platforms/stm32f767/CMakeLists.txt new file mode 100644 index 000000000..f1d9691a2 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/platforms/stm32f767/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(bindings PRIVATE bindings.cc) + +target_link_libraries(bindings PUBLIC mcal-stm32f767) \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/platforms/stm32f767/bindings.cc b/firmware/projects/Demo/AnalogInput/platforms/stm32f767/bindings.cc new file mode 100644 index 000000000..a66c52765 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/platforms/stm32f767/bindings.cc @@ -0,0 +1,21 @@ +#include + +#include "../../bindings.h" +#include "mcal/cli/periph/analog_input.h" +#include "shared/periph/analog_input.h" + +namespace mcal { + +cli::periph::AnalogInput analog_input{"analog input"}; + +} // namespace mcal + +namespace bindings { + +shared::periph::AnalogInput& analog_input = mcal::analog_input; + +void Init() { + std::cout << "Initializing the CLI..." << std::endl; +} + +} // namespace bindings \ No newline at end of file diff --git a/firmware/projects/Demo/AnalogInput/platforms/stm32f767/mcal_conf.cmake b/firmware/projects/Demo/AnalogInput/platforms/stm32f767/mcal_conf.cmake new file mode 100644 index 000000000..7c5404f10 --- /dev/null +++ b/firmware/projects/Demo/AnalogInput/platforms/stm32f767/mcal_conf.cmake @@ -0,0 +1 @@ +set(MCAL stm32f767) \ No newline at end of file