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 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions firmware/mcal/cli/periph/analog_input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// @author Samuel Shi
/// @date 2024-11-17

#pragma once

#include <iostream>
#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_;
};

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


#pragma once

#include <cstdint>

#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
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.h
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions firmware/projects/Demo/AnalogInput/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "bindings.h"


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

while (true) {
bindings::analog_input.Read();
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target_sources(bindings PRIVATE bindings.cc)

target_link_libraries(bindings PUBLIC mcal-cli)
21 changes: 21 additions & 0 deletions firmware/projects/Demo/AnalogInput/platforms/cli/bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

#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
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,3 @@
target_sources(bindings PRIVATE bindings.cc)

target_link_libraries(bindings PUBLIC mcal-stm32f767)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

#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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(MCAL stm32f767)
17 changes: 17 additions & 0 deletions firmware/shared/periph/analog_input.h
Original file line number Diff line number Diff line change
@@ -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
Loading