| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | #include "pch.h" | |||
| 2 | ||||
| 3 | #include "adc_subscription.h" | |||
| 4 | #include "functional_sensor.h" | |||
| 5 | #include "linear_func.h" | |||
| 6 | #include "thermistor_func.h" | |||
| 7 | ||||
| 8 | // Each one could be either linear or thermistor | |||
| 9 | struct FuncPair { | |||
| 10 | LinearFunc linear; | |||
| 11 | thermistor_t thermistor; | |||
| 12 | }; | |||
| 13 | ||||
| 14 | static CCM_OPTIONAL FunctionalSensor clt(SensorType::Clt, MS2NT(10)); | |||
| 15 | static CCM_OPTIONAL FunctionalSensor iat(SensorType::Iat, MS2NT(10)); | |||
| 16 | static CCM_OPTIONAL FunctionalSensor aux1(SensorType::AuxTemp1, MS2NT(10)); | |||
| 17 | static CCM_OPTIONAL FunctionalSensor aux2(SensorType::AuxTemp2, MS2NT(10)); | |||
| 18 | ||||
| 19 | static CCM_OPTIONAL FunctionalSensor oilTempSensor(SensorType::OilTemperature, MS2NT(10)); | |||
| 20 | static CCM_OPTIONAL FunctionalSensor fuelTempSensor(SensorType::FuelTemperature, MS2NT(10)); | |||
| 21 | static CCM_OPTIONAL FunctionalSensor ambientTempSensor(SensorType::AmbientTemperature, MS2NT(10)); | |||
| 22 | static CCM_OPTIONAL FunctionalSensor compressorDischargeTemp(SensorType::CompressorDischargeTemperature, MS2NT(10)); | |||
| 23 | ||||
| 24 | static FuncPair fclt, fiat, faux1, faux2, foil, ffuel, fambient, fcdt; | |||
| 25 | ||||
| 26 | 4 | static void validateThermistorConfig(const char *msg, thermistor_conf_s& cfg) { | ||
| 27 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 4 times.
|
4 | if (cfg.tempC_1 >= cfg.tempC_2 || |
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | cfg.tempC_2 >= cfg.tempC_3) { | |
| 29 | ✗ | firmwareError(ObdCode::OBD_ThermistorConfig, "Invalid thermistor %s configuration: please check that temperatures are in the ascending order %f %f %f", | ||
| 30 | msg, | |||
| 31 | ✗ | (float)cfg.tempC_1, | ||
| 32 | ✗ | (float)cfg.tempC_2, | ||
| 33 | ✗ | (float)cfg.tempC_3); | ||
| 34 | } | |||
| 35 | 4 | } | ||
| 36 | ||||
| 37 | 4 | static SensorConverter& configureTempSensorFunction(const char *msg, | ||
| 38 | thermistor_conf_s& cfg, FuncPair& p, bool isLinear, bool isPulldown) { | |||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 4 times.
|
4 | if (isLinear) { |
| 40 | ✗ | p.linear.configure(cfg.resistance_1, cfg.tempC_1, cfg.resistance_2, cfg.tempC_2, -50, 250); | ||
| 41 | ||||
| 42 | ✗ | return p.linear; | ||
| 43 | } else /* sensor is thermistor */ { | |||
| 44 | 4 | validateThermistorConfig(msg, cfg); | ||
| 45 | ||||
| 46 | 4 | p.thermistor.get<resist>().configure(5.0f, cfg.bias_resistor, isPulldown); | ||
| 47 | 4 | p.thermistor.get<therm>().configure(cfg); | ||
| 48 | ||||
| 49 | 3 | return p.thermistor; | ||
| 50 | } | |||
| 51 | } | |||
| 52 | ||||
| 53 | 4 | static void configTherm(const char *msg, | ||
| 54 | FunctionalSensor &sensor, | |||
| 55 | FuncPair &p, | |||
| 56 | ThermistorConf &p_config, | |||
| 57 | bool isLinear, | |||
| 58 | bool isPulldown) { | |||
| 59 | // nothing to do if no channel | |||
| 60 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 4 times.
|
4 | if (!isAdcChannelValid(p_config.adcChannel)) { |
| 61 | ✗ | return; | ||
| 62 | } | |||
| 63 | ||||
| 64 | // Configure the conversion function for this sensor | |||
| 65 | 4 | sensor.setFunction(configureTempSensorFunction(msg, p_config.config, p, isLinear, isPulldown)); | ||
| 66 | } | |||
| 67 | ||||
| 68 | 31 | static void configureTempSensor(const char *msg, | ||
| 69 | FunctionalSensor &sensor, | |||
| 70 | FuncPair &p, | |||
| 71 | ThermistorConf &p_config, | |||
| 72 | bool isLinear, | |||
| 73 | bool isPulldown = false) { | |||
| 74 | 31 | auto channel = p_config.adcChannel; | ||
| 75 | ||||
| 76 | // Only register if we have a sensor | |||
| 77 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 2 taken 4 times.
|
2/2✓ Decision 'true' taken 27 times.
✓ Decision 'false' taken 4 times.
|
31 | if (!isAdcChannelValid(channel)) { |
| 78 | 27 | return; | ||
| 79 | } | |||
| 80 | ||||
| 81 | 4 | configTherm(msg, sensor, p, p_config, isLinear, isPulldown); | ||
| 82 | ||||
| 83 | // Register & subscribe | |||
| 84 | 3 | AdcSubscription::SubscribeSensor(sensor, channel, 2); | ||
| 85 | 3 | sensor.Register(); | ||
| 86 | } | |||
| 87 | ||||
| 88 | 4 | void initThermistors() { | ||
| 89 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 4 times.
✗ Decision 'false' not taken.
|
4 | if (!engineConfiguration->consumeObdSensors) { |
| 90 | 4 | configureTempSensor("clt", | ||
| 91 | clt, | |||
| 92 | fclt, | |||
| 93 | 4 | engineConfiguration->clt, | ||
| 94 | 4 | engineConfiguration->useLinearCltSensor, | ||
| 95 | 4 | engineConfiguration->cltSensorPulldown); | ||
| 96 | ||||
| 97 | 4 | configureTempSensor("iat", | ||
| 98 | iat, | |||
| 99 | fiat, | |||
| 100 | 4 | engineConfiguration->iat, | ||
| 101 | 4 | engineConfiguration->useLinearIatSensor, | ||
| 102 | 4 | engineConfiguration->iatSensorPulldown); | ||
| 103 | } | |||
| 104 | ||||
| 105 | 4 | configureTempSensor("oil temp", | ||
| 106 | oilTempSensor, | |||
| 107 | faux2, | |||
| 108 | 4 | engineConfiguration->oilTempSensor, | ||
| 109 | false); | |||
| 110 | ||||
| 111 | 4 | configureTempSensor("fuel temp", | ||
| 112 | fuelTempSensor, | |||
| 113 | ffuel, | |||
| 114 | 4 | engineConfiguration->fuelTempSensor, | ||
| 115 | false); | |||
| 116 | ||||
| 117 | 4 | configureTempSensor("ambient temp", | ||
| 118 | ambientTempSensor, | |||
| 119 | fambient, | |||
| 120 | 4 | engineConfiguration->ambientTempSensor, | ||
| 121 | false); | |||
| 122 | ||||
| 123 | 4 | configureTempSensor("compressor discharge temp", | ||
| 124 | compressorDischargeTemp, | |||
| 125 | fcdt, | |||
| 126 | 4 | engineConfiguration->compressorDischargeTemperature, | ||
| 127 | false); | |||
| 128 | ||||
| 129 | 4 | configureTempSensor("aux1", | ||
| 130 | aux1, | |||
| 131 | faux1, | |||
| 132 | 4 | engineConfiguration->auxTempSensor1, | ||
| 133 | false); | |||
| 134 | ||||
| 135 | 3 | configureTempSensor("aux2", | ||
| 136 | aux2, | |||
| 137 | faux2, | |||
| 138 | 3 | engineConfiguration->auxTempSensor2, | ||
| 139 | false); | |||
| 140 | 3 | } | ||
| 141 | ||||
| 142 | ✗ | void deinitThermistors() { | ||
| 143 | ✗ | AdcSubscription::UnsubscribeSensor(clt, engineConfiguration->clt.adcChannel); | ||
| 144 | ✗ | AdcSubscription::UnsubscribeSensor(iat, engineConfiguration->iat.adcChannel); | ||
| 145 | ✗ | AdcSubscription::UnsubscribeSensor(oilTempSensor, engineConfiguration->oilTempSensor.adcChannel); | ||
| 146 | ✗ | AdcSubscription::UnsubscribeSensor(fuelTempSensor, engineConfiguration->fuelTempSensor.adcChannel); | ||
| 147 | ✗ | AdcSubscription::UnsubscribeSensor(ambientTempSensor, engineConfiguration->ambientTempSensor.adcChannel); | ||
| 148 | ✗ | AdcSubscription::UnsubscribeSensor(compressorDischargeTemp, engineConfiguration->compressorDischargeTemperature.adcChannel); | ||
| 149 | ✗ | AdcSubscription::UnsubscribeSensor(aux1, engineConfiguration->auxTempSensor1.adcChannel); | ||
| 150 | ✗ | AdcSubscription::UnsubscribeSensor(aux2, engineConfiguration->auxTempSensor2.adcChannel); | ||
| 151 | ✗ | } | ||
| 152 |