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

[FW-94] Changes in ADC + included tests #461

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion Inc/HALALMock/Services/ADC/ADC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class ADC {
static map<Pin, Instance> available_instances;

static unordered_map<uint8_t,Instance> active_instances;
static unordered_map<uint8_t, EmulatedPin> active_emulated_instances;
static unordered_map<uint8_t, EmulatedPin*> active_emulated_instances;

static unordered_map<uint8_t,Pin> id_to_pin;

static uint8_t id_counter;
};
Expand Down
46 changes: 28 additions & 18 deletions Src/HALALMock/Services/ADC/ADC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

uint8_t ADC::id_counter{0};
unordered_map<uint8_t, ADC::Instance> ADC::active_instances{};
unordered_map<uint8_t, EmulatedPin> ADC::active_emulated_instances{};
unordered_map<uint8_t, EmulatedPin*> ADC::active_emulated_instances{};
unordered_map<uint8_t, Pin> ADC::id_to_pin{};

uint8_t ADC::inscribe(Pin pin) {
if (not available_instances.contains(pin)) {
Expand All @@ -28,21 +29,27 @@ uint8_t ADC::inscribe(Pin pin) {
}

Pin::inscribe(pin, OperationMode::ANALOG);
emulated_pin.type = PinType::ADC;
active_instances[id_counter] = available_instances[pin];
id_to_pin[id_counter] = pin;
return id_counter++;
}

void ADC::start() {
// Storing emulated pins with their corresponding instance
uint8_t idx = 0;
for (auto& [pin, instance] : available_instances) {
for(auto& [id,inst] : active_instances){
if(&inst == &instance)
idx = id;
}
EmulatedPin& emulated_pin = SharedMemory::get_pin(pin);
active_emulated_instances[idx] = emulated_pin;
}
// Clear any existing mappings
active_emulated_instances.clear();

// Map each active ADC ID to its corresponding EmulatedPin
for (const auto& [id, instance] : active_instances) {
auto pin_it = id_to_pin.find(id);
if (pin_it == id_to_pin.end()) {
ErrorHandler("No pin mapped for ADC ID %u", id);
continue;
}
Pin pin = pin_it->second;
EmulatedPin& emulated_pin = SharedMemory::get_pin(pin);
active_emulated_instances[id] = &emulated_pin;
}
}

void ADC::turn_on(uint8_t id){
Expand All @@ -56,13 +63,13 @@ void ADC::turn_on(uint8_t id){
float ADC::get_value(uint8_t id) {
Instance& instance = active_instances[id];

EmulatedPin& emulated_pin = active_emulated_instances[id];
if (emulated_pin.type != PinType::ADC) {
EmulatedPin* emulated_pin = active_emulated_instances[id];
if (emulated_pin->type != PinType::ADC) {
ErrorHandler("Pin %s is not configured to be used for ADC usage", emulated_pin);
return 0;
}
ADCResolution resolution = static_cast<ADCResolution>(instance.resolution);
uint16_t raw = emulated_pin.PinData.adc.value;
uint16_t raw = emulated_pin->PinData.adc.value;
switch (resolution) {
case ADCResolution::ADC_RES_16BITS:
return (raw * ADC_MAX_VOLTAGE) / (float)MAX_16BIT;
Expand All @@ -81,14 +88,14 @@ float ADC::get_value(uint8_t id) {
uint16_t ADC::get_int_value(uint8_t id) {
Instance& instance = active_instances[id];

EmulatedPin& emulated_pin = active_emulated_instances[id];
if (emulated_pin.type != PinType::ADC) {
EmulatedPin* emulated_pin = active_emulated_instances[id];
if (emulated_pin->type != PinType::ADC) {
ErrorHandler("Pin %s is not configured to be used for ADC usage", emulated_pin);
return 0;
}

ADCResolution resolution = static_cast<ADCResolution>(instance.resolution);
uint16_t raw = emulated_pin.PinData.adc.value;
uint16_t raw = emulated_pin->PinData.adc.value;
switch (resolution) {
case ADCResolution::ADC_RES_16BITS:
return raw;
Expand All @@ -105,5 +112,8 @@ uint16_t ADC::get_int_value(uint8_t id) {
}

uint16_t* ADC::get_value_pointer(uint8_t id) {
return &active_emulated_instances[id].PinData.adc.value;
if (active_emulated_instances.find(id) != active_emulated_instances.end())
return &active_emulated_instances[id]->PinData.adc.value;
else
return nullptr;
}
27 changes: 15 additions & 12 deletions Src/HALALMock/Services/SharedMemory/SharedMemory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "HALALMock/Services/SharedMemory/SharedMemory.hpp"
//includes to create the shared Memory in Posix
#include <fcntl.h>
Expand Down Expand Up @@ -85,7 +84,7 @@ void SharedMemory::close(){
void SharedMemory::close_gpio_shared_memory(){
if (gpio_memory != nullptr){
//unmap shared memory
if(munmap(gpio_memory,state_machine_memory_size) == -1){
if(munmap(gpio_memory,gpio_memory_size) == -1){
std::cout<<"Error unmapping the gpio shared_memory\n";
std::terminate();
}
Expand Down Expand Up @@ -130,14 +129,18 @@ void SharedMemory::update_current_state(uint8_t index, uint8_t state){
state_machine_memory[index]=state;
}

EmulatedPin &SharedMemory::get_pin(Pin pin){
uint8_t offset;
auto it = SHM::pin_offsets.find(pin);
if(it != SHM::pin_offsets.end()){
offset = it -> second;
}else{
std::cout<<"Pin " <<pin.to_string()<< " doesn't exist\n";
}
EmulatedPin *pin_memory = SharedMemory::gpio_memory + offset;
return *pin_memory;
EmulatedPin& SharedMemory::get_pin(Pin pin){
auto it = SHM::pin_offsets.find(pin);
if(it == SHM::pin_offsets.end()){
std::cout<<"Error: Pin " << pin.to_string() << " doesn't exist.\n";
std::terminate();
}

size_t offset = it->second;
if (offset >= gpio_memory_size){
std::cout<<"Error: Offset " << offset << " is out of scope\n";
std::terminate();
}

return gpio_memory[offset];
}
78 changes: 78 additions & 0 deletions Tests/ADCTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <gtest/gtest.h>
#include "HALALMock/Services/ADC/ADC.hpp"
#include "HALALMock/Services/SharedMemory/SharedMemory.hpp"
#include <iostream>

TEST(ADC,Inscribe){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

//existing pin
uint8_t adc_id_1=ADC::inscribe(PA0);
EXPECT_EQ(adc_id_1, 0);
//not existing pin
uint8_t adc_id_2=ADC::inscribe(PB15);
EXPECT_EQ(adc_id_2, 0);
//existing pin
uint8_t adc_id_3=ADC::inscribe(PB1);
EXPECT_EQ(adc_id_3, 1);

SharedMemory::close();
}

TEST(ADC,Start){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

uint8_t adc_id_1=ADC::inscribe(PA0);
uint8_t adc_id_2=ADC::inscribe(PB15);

ADC::start();

EmulatedPin& my_emulated_pin_1=SharedMemory::get_pin(PA0);
EmulatedPin& my_emulated_pin_2=SharedMemory::get_pin(PB15);

EXPECT_EQ(my_emulated_pin_1.type, PinType::ADC);
EXPECT_EQ(my_emulated_pin_2.type, PinType::NOT_USED);

SharedMemory::close();
}


TEST(ADC,Get_value){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

uint8_t adc_id=ADC::inscribe(PA0);

ADC::start();
ADC::turn_on(adc_id);

uint16_t* my_raw_value=ADC::get_value_pointer(adc_id);
*my_raw_value=32768;

float value=ADC::get_value(adc_id);

EXPECT_NEAR(value, 1.65f, 0.1f);

SharedMemory::close();
}

TEST(ADC,Get_int_value){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

uint8_t adc_id=ADC::inscribe(PA0);

ADC::start();
ADC::turn_on(adc_id);

uint16_t* my_raw_value=ADC::get_value_pointer(adc_id);
*my_raw_value=32768;

uint16_t raw_value=ADC::get_int_value(adc_id);

EXPECT_EQ(raw_value, 32768);

SharedMemory::close();
}
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_executable(${STLIB_TEST_EXECUTABLE}
${CMAKE_CURRENT_LIST_DIR}/Time/test_time.cpp
${CMAKE_CURRENT_LIST_DIR}/EncoderTest.cpp
${CMAKE_CURRENT_LIST_DIR}/InputCaptureTest.cpp
${CMAKE_CURRENT_LIST_DIR}/ADCTest.cpp
)

set_target_properties(${STLIB_TEST_EXECUTABLE} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED C_STANDARD 11 C_STANDARD_REQUIRED)
Expand Down
28 changes: 28 additions & 0 deletions Tests/Runes/SimRunes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,32 @@ std::unordered_map<Pin, size_t> SHM::pin_offsets = {
{PG6, 102}, {PG7, 103}, {PG8, 104}, {PG9, 105},
{PG10, 106}, {PG11, 107}, {PG12, 108}, {PG13, 109},
{PG14, 110}, {PG15, 111}, {PH0, 112}, {PH1, 113}
};

/************************************************
* ADC
***********************************************/

map<Pin, ADC::Instance> ADC::available_instances = {
{PF11, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PF12, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PF13, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PF14, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PF5, Instance(ADC::ADCResolution::ADC_RES_12BITS)},
{PF6, Instance(ADC::ADCResolution::ADC_RES_12BITS)},
{PF7, Instance(ADC::ADCResolution::ADC_RES_12BITS)},
{PF8, Instance(ADC::ADCResolution::ADC_RES_12BITS)},
{PF9, Instance(ADC::ADCResolution::ADC_RES_12BITS)},
{PF10, Instance(ADC::ADCResolution::ADC_RES_12BITS)},
{PC2, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PC3, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PF10, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PC0, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PA0, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PA3, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PA4, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PA5, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PA6, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PB0, Instance(ADC::ADCResolution::ADC_RES_16BITS)},
{PB1, Instance(ADC::ADCResolution::ADC_RES_16BITS)}
};
Loading