From 44b5427813a5fce5fa64d45931ab19685c8c553f Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Thu, 7 Jan 2021 17:37:15 -0800 Subject: [PATCH] better error checking of thermistor config (#2199) * add bounds check on result * validate thermistor config Co-authored-by: Matthew Kennedy --- .../sensors/converters/thermistor_func.cpp | 6 ++++++ firmware/init/sensor/init_thermistors.cpp | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/firmware/controllers/sensors/converters/thermistor_func.cpp b/firmware/controllers/sensors/converters/thermistor_func.cpp index ae77e44ae6..cb540fac8a 100644 --- a/firmware/controllers/sensors/converters/thermistor_func.cpp +++ b/firmware/controllers/sensors/converters/thermistor_func.cpp @@ -25,6 +25,12 @@ SensorResult ThermistorFunc::convert(float ohms) const { float celsius = convertKelvinToCelcius(kelvin); + // bounds check result - please don't try to run rusEfi when colder than -50C + // high end limit is required as this could be an oil temp sensor + if (celsius < -50 || celsius > 200) { + return unexpected; + } + return celsius; } diff --git a/firmware/init/sensor/init_thermistors.cpp b/firmware/init/sensor/init_thermistors.cpp index ded7e006b8..404c0815af 100644 --- a/firmware/init/sensor/init_thermistors.cpp +++ b/firmware/init/sensor/init_thermistors.cpp @@ -27,12 +27,25 @@ static CCM_OPTIONAL FunctionalSensor aux2(SensorType::AuxTemp2, MS2NT(10)); static FuncPair fclt, fiat, faux1, faux2; +void validateThermistorConfig(thermistor_conf_s& cfg) { + if ( + cfg.tempC_1 >= cfg.tempC_2 || + cfg.tempC_2 >= cfg.tempC_3 || + cfg.resistance_1 < cfg.resistance_2 || + cfg.resistance_2 < cfg.resistance_3 + ) { + firmwareError(OBD_Engine_Coolant_Temperature_Circuit_Malfunction, "Invalid thermistor configuration: please check that temperatures & resistances are in the correct order."); + } +} + static SensorConverter& configureTempSensorFunction(thermistor_conf_s& cfg, FuncPair& p, bool isLinear) { if (isLinear) { p.linear.configure(cfg.resistance_1, cfg.tempC_1, cfg.resistance_2, cfg.tempC_2, -50, 250); return p.linear; } else /* sensor is thermistor */ { + validateThermistorConfig(cfg); + p.thermistor.get().configure(5.0f, cfg.bias_resistor); p.thermistor.get().configure(cfg);