Skip to content

Commit

Permalink
max31855/egt: migrate to SensorType framework
Browse files Browse the repository at this point in the history
  • Loading branch information
dron0gus committed Mar 9, 2024
1 parent ab465ac commit 0420809
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
12 changes: 12 additions & 0 deletions firmware/console/status_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ static void updateFuelSensors() {
engine->outputChannels.fuelTankLevel = Sensor::getOrZero(SensorType::FuelLevel);
}

static void updateEgtSensors() {
engine->outputChannels.egt[0] = Sensor::getOrZero(SensorType::EGT1);
engine->outputChannels.egt[1] = Sensor::getOrZero(SensorType::EGT2);
engine->outputChannels.egt[2] = Sensor::getOrZero(SensorType::EGT3);
engine->outputChannels.egt[3] = Sensor::getOrZero(SensorType::EGT4);
engine->outputChannels.egt[4] = Sensor::getOrZero(SensorType::EGT5);
engine->outputChannels.egt[5] = Sensor::getOrZero(SensorType::EGT6);
engine->outputChannels.egt[6] = Sensor::getOrZero(SensorType::EGT7);
engine->outputChannels.egt[7] = Sensor::getOrZero(SensorType::EGT8);
}

static void updateVvtSensors() {
#if EFI_SHAFT_POSITION_INPUT
// 248
Expand Down Expand Up @@ -520,6 +531,7 @@ static void updateSensors() {
updateRawSensors();
updateLambda();
updateFuelSensors();
updateEgtSensors();
updateVvtSensors();
updateVehicleSpeed();
updatePressures();
Expand Down
11 changes: 8 additions & 3 deletions firmware/controllers/sensors/sensor_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ enum class SensorType : unsigned char {

InputShaftSpeed,

// todo: migrate to SensorType framework!
// EGT1,
// EGT2,
EGT1,
EGT2,
EGT3,
EGT4,
EGT5,
EGT6,
EGT7,
EGT8,

Maf2, // Second bank MAF sensor

Expand Down
38 changes: 32 additions & 6 deletions firmware/hw_layer/sensors/max31855.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#if EFI_MAX_31855

#include "thread_controller.h"
#include "stored_value_sensor.h"

#ifndef MAX31855_REFRESH_TIME
#define MAX31855_REFRESH_TIME 500
#endif

/* TODO: move all stuff to Max31855Read class */
class Max31855Read final : public ThreadController<UTILITY_THREAD_STACK_SIZE> {
Expand All @@ -48,12 +53,21 @@ class Max31855Read final : public ThreadController<UTILITY_THREAD_STACK_SIZE> {
/* WARN: this will clear all other bits in cr1 */
spiConfig.cr1 = getSpiPrescaler(_5MHz, device);
for (size_t i = 0; i < EGT_CHANNEL_COUNT; i++) {
/* and mark used! */
auto& sensor = egtSensors[i];

m_cs[i] = Gpio::Invalid;

// If there's already another (CAN?) EGT sensor configured,
// don't configure this one.
if (Sensor::hasSensor(sensor.type()))
continue;

// get CS pin and mark used!
if (isBrainPinValid(cs[i])) {
initSpiCs(&spiConfig, cs[i]);
m_cs[i] = cs[i];
} else {
m_cs[i] = Gpio::Invalid;

sensor.Register();
}
}
ThreadController::start();
Expand All @@ -69,14 +83,15 @@ class Max31855Read final : public ThreadController<UTILITY_THREAD_STACK_SIZE> {

max_31855_code ret = getMax31855EgtValue(i, &value, NULL);
if (ret == MC_OK) {
// todo: migrate to SensorType framework!
engine->currentEgtValue[i] = value;
auto& sensor = egtSensors[i];

sensor.setValidValue(value, getTimeNowNt());
} else {
/* TODO: report error code? */
}
}

chThdSleepMilliseconds(500);
chThdSleepMilliseconds(MAX31855_REFRESH_TIME);
}
}

Expand Down Expand Up @@ -228,6 +243,17 @@ class Max31855Read final : public ThreadController<UTILITY_THREAD_STACK_SIZE> {
}
return code;
}

StoredValueSensor egtSensors[EGT_CHANNEL_COUNT] = {
{ SensorType::EGT1, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT2, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT3, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT4, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT5, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT6, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT7, MS2NT(MAX31855_REFRESH_TIME * 3) },
{ SensorType::EGT8, MS2NT(MAX31855_REFRESH_TIME * 3) }
};
};

static Max31855Read instance;
Expand Down

0 comments on commit 0420809

Please sign in to comment.