Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 convert adc input to analoginput #348

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions firmware/mcal/cli/periph/adc.hpp

This file was deleted.

31 changes: 31 additions & 0 deletions firmware/mcal/cli/periph/analog_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// @author Samuel Shi
/// @date 2024-11-17

#pragma once

#include <format>
#include <iostream>

#include "shared/periph/analog_input.hpp"

namespace mcal::cli::periph {

class AnalogInput : public shared::periph::AnalogInput {
public:
AnalogInput(std::string name) : name_(name) {}

float ReadVoltage() override {
std::cout << std::format("Reading ADC {} ...", name_) << std::endl;
std::cout << " | Enter a voltage level: ";
float voltage;
std::cin >> voltage;
std::cout << std::format(" | Obtained value {:.3f} V", voltage)
<< std::endl;
return voltage;
}

private:
std::string name_;
};

} // namespace mcal::cli::periph
1 change: 1 addition & 0 deletions firmware/mcal/linux/periph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target_sources(mcal-linux
PRIVATE
adc.cc
analog_input.cc
can.cc
digital_input.cc
digital_output.cc
Expand Down
26 changes: 0 additions & 26 deletions firmware/mcal/linux/periph/adc.cc

This file was deleted.

21 changes: 0 additions & 21 deletions firmware/mcal/linux/periph/adc.hpp

This file was deleted.

24 changes: 24 additions & 0 deletions firmware/mcal/linux/periph/analog_input.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// @author Samuel Shi
/// @date 2025-1-10

#include <format>
#include <iostream>

#include "analog_input.hpp"


namespace mcal::lnx::periph {

AnalogInput::AnalogInput(std::string name) : name_(name) {}

float AnalogInput::ReadVoltage() {
std::cout << std::format("Reading ADC {} ...", name_) << std::endl;
std::cout << " | Enter a voltage level: ";
float voltage;
std::cin >> voltage;
std::cout << std::format(" | Obtained value {:.3f} V", voltage)
<< std::endl;
return voltage;
}

}; // namespace mcal::lnx::periph
22 changes: 22 additions & 0 deletions firmware/mcal/linux/periph/analog_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// @author Samuel Shi
/// @date 2025-1-10

#pragma once

#include <iostream>

#include "shared/periph/analog_input.hpp"

namespace mcal::lnx::periph {

class AnalogInput : public shared::periph::AnalogInput {
public:
AnalogInput(std::string name);

float ReadVoltage() override;

private:
std::string name_;
};

} // namespace mcal::lnx::periph
48 changes: 0 additions & 48 deletions firmware/mcal/stm32f767/periph/adc.hpp

This file was deleted.

62 changes: 62 additions & 0 deletions firmware/mcal/stm32f767/periph/analog_input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@


#pragma once

#include <cstdint>

#include "shared/periph/analog_input.hpp"
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_adc.h"

namespace mcal::stm32f767::periph {

class AnalogInput : public shared::periph::AnalogInput {
public:
AnalogInput(ADC_HandleTypeDef* hadc, uint32_t adc_channel,
float system_volts = 3.3f)
: hadc_(hadc),
adc_channel_(adc_channel),
system_volts_(system_volts) {};

float ReadVoltage() override {
Start();
HAL_ADC_PollForConversion(hadc_, 1000);
uint32_t adc_value = HAL_ADC_GetValue(hadc_);
HAL_ADC_Stop(hadc_);

return adc_value / GetDivisor() * system_volts_;
}

private:
ADC_HandleTypeDef* hadc_;
uint32_t adc_channel_;
const float system_volts_;

void Start() {
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 GetDivisor() {
switch (ADC_GET_RESOLUTION(hadc_)) {
case ADC_RESOLUTION_6B:
return 63.0f;
case ADC_RESOLUTION_8B:
return 255.0f;
case ADC_RESOLUTION_10B:
return 1023.0f;
case ADC_RESOLUTION_12B:
return 4095.0f;
default:
return 4095.0f;
}
};
};

} // namespace mcal::stm32f767::periph
1 change: 1 addition & 0 deletions firmware/projects/Demo/AnalogInput/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources(main PUBLIC main.cc)
11 changes: 11 additions & 0 deletions firmware/projects/Demo/AnalogInput/bindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "shared/periph/analog_input.hpp"

namespace bindings {

extern shared::periph::AnalogInput& analog_input;

extern void Init();

} // namespace bindings
10 changes: 10 additions & 0 deletions firmware/projects/Demo/AnalogInput/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "bindings.hpp"

int main() {
bindings::Init();

BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved
while (true) {
bindings::analog_input.ReadVoltage();
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target_sources(main PRIVATE bindings.cc)
target_link_libraries(main PUBLIC mcal-cli)
21 changes: 21 additions & 0 deletions firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to implement the rest of the bindings for this platform (leds)

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

#include "../../bindings.hpp"
#include "mcal/cli/periph/analog_input.hpp"
#include "shared/periph/analog_input.hpp"

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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(MCAL cli)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target_sources(main PRIVATE bindings.cc)
target_link_libraries(main PRIVATE mcal-linux)
21 changes: 21 additions & 0 deletions firmware/projects/Demo/AnalogInput/platforms/linux/bindings.cc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to implement the rest of the bindings for this platform (leds)

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

#include "../../bindings.hpp"
#include "mcal/linux/periph/analog_input.hpp"
#include "shared/periph/analog_input.hpp"

namespace mcal {

lnx::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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(MCAL linux)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target_sources(main PRIVATE bindings.cc)
target_link_libraries(main PUBLIC mcal-stm32f767)

add_subdirectory(cubemx/cmake/stm32cubemx)
target_link_libraries(main PRIVATE stm32cubemx)
Loading