diff --git a/firmware/mcal/cli/periph/analog_input.h b/firmware/mcal/cli/periph/analog_input.h new file mode 100644 index 000000000..30f6a8469 --- /dev/null +++ b/firmware/mcal/cli/periph/analog_input.h @@ -0,0 +1,31 @@ +/// @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 { +public: + AnalogInput(std::string name) : name_(name) {} + + 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/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 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