2021-07-25 22:05:17 -07:00
# include "pch.h"
2020-04-07 13:07:09 -07:00
# include "adc_subscription.h"
# include "functional_sensor.h"
# include "func_chain.h"
# include "linear_func.h"
# include "resistance_func.h"
# include "thermistor_func.h"
using resist = ResistanceFunc ;
using therm = ThermistorFunc ;
// Each one could be either linear or thermistor
struct FuncPair {
LinearFunc linear ;
FuncChain < resist , therm > thermistor ;
} ;
static CCM_OPTIONAL FunctionalSensor clt ( SensorType : : Clt , MS2NT ( 10 ) ) ;
static CCM_OPTIONAL FunctionalSensor iat ( SensorType : : Iat , MS2NT ( 10 ) ) ;
static CCM_OPTIONAL FunctionalSensor aux1 ( SensorType : : AuxTemp1 , MS2NT ( 10 ) ) ;
static CCM_OPTIONAL FunctionalSensor aux2 ( SensorType : : AuxTemp2 , MS2NT ( 10 ) ) ;
static FuncPair fclt , fiat , faux1 , faux2 ;
2021-10-14 15:07:35 -07:00
static void validateThermistorConfig ( const char * msg , thermistor_conf_s & cfg ) {
2021-05-06 10:58:01 -07:00
if ( cfg . tempC_1 > = cfg . tempC_2 | |
cfg . tempC_2 > = cfg . tempC_3 ) {
2021-10-14 15:07:35 -07:00
firmwareError ( OBD_Engine_Coolant_Temperature_Circuit_Malfunction , " Invalid thermistor %s configuration: please check that temperatures are in the ascending order %f %f %f " ,
msg ,
2021-05-06 10:58:01 -07:00
cfg . tempC_1 ,
cfg . tempC_2 ,
cfg . tempC_3 ) ;
2021-01-07 17:37:15 -08:00
}
}
2021-10-14 15:07:35 -07:00
static SensorConverter & configureTempSensorFunction ( const char * msg ,
thermistor_conf_s & cfg , FuncPair & p , bool isLinear ) {
2020-04-07 13:07:09 -07:00
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 */ {
2021-10-14 15:07:35 -07:00
validateThermistorConfig ( msg , cfg ) ;
2021-01-07 17:37:15 -08:00
2020-04-07 13:07:09 -07:00
p . thermistor . get < resist > ( ) . configure ( 5.0f , cfg . bias_resistor ) ;
p . thermistor . get < therm > ( ) . configure ( cfg ) ;
return p . thermistor ;
}
}
2021-10-14 15:07:35 -07:00
static void configTherm ( const char * msg ,
FunctionalSensor & sensor ,
2020-04-07 13:07:09 -07:00
FuncPair & p ,
ThermistorConf & config ,
bool isLinear ) {
2021-01-09 12:13:43 -08:00
// nothing to do if no channel
if ( ! isAdcChannelValid ( config . adcChannel ) ) {
return ;
}
2020-04-07 13:07:09 -07:00
// Configure the conversion function for this sensor
2021-10-14 15:07:35 -07:00
sensor . setFunction ( configureTempSensorFunction ( msg , config . config , p , isLinear ) ) ;
2020-04-07 13:07:09 -07:00
}
2021-10-14 15:07:35 -07:00
static void configureTempSensor ( const char * msg ,
FunctionalSensor & sensor ,
2020-04-07 13:07:09 -07:00
FuncPair & p ,
ThermistorConf & config ,
bool isLinear ) {
auto channel = config . adcChannel ;
// Only register if we have a sensor
2021-01-05 13:02:20 -08:00
if ( ! isAdcChannelValid ( channel ) ) {
2020-04-07 13:07:09 -07:00
return ;
}
2021-10-14 15:07:35 -07:00
configTherm ( msg , sensor , p , config , isLinear ) ;
2020-04-07 13:07:09 -07:00
// Register & subscribe
2020-12-06 12:00:30 -08:00
AdcSubscription : : SubscribeSensor ( sensor , channel , 2 ) ;
sensor . Register ( ) ;
2020-04-07 13:07:09 -07:00
}
2020-08-31 04:45:52 -07:00
void initThermistors ( DECLARE_CONFIG_PARAMETER_SIGNATURE ) {
2020-09-03 20:27:53 -07:00
if ( ! CONFIG ( consumeObdSensors ) ) {
2021-10-14 15:07:35 -07:00
configureTempSensor ( " clt " ,
clt ,
2020-04-07 13:07:09 -07:00
fclt ,
CONFIG ( clt ) ,
CONFIG ( useLinearCltSensor ) ) ;
2021-10-14 15:07:35 -07:00
configureTempSensor ( " iat " ,
iat ,
2020-04-07 13:07:09 -07:00
fiat ,
CONFIG ( iat ) ,
CONFIG ( useLinearIatSensor ) ) ;
2020-09-03 20:27:53 -07:00
}
2020-04-07 13:07:09 -07:00
2021-10-14 15:07:35 -07:00
configureTempSensor ( " aux1 " ,
aux1 ,
2020-04-07 13:07:09 -07:00
faux1 ,
CONFIG ( auxTempSensor1 ) ,
false ) ;
2021-10-14 15:07:35 -07:00
configureTempSensor ( " aux2 " ,
aux2 ,
2020-04-07 13:07:09 -07:00
faux2 ,
CONFIG ( auxTempSensor2 ) ,
false ) ;
}
2021-08-24 13:41:16 -07:00
void deinitThermistors ( ) {
AdcSubscription : : UnsubscribeSensor ( clt ) ;
AdcSubscription : : UnsubscribeSensor ( iat ) ;
AdcSubscription : : UnsubscribeSensor ( aux1 ) ;
AdcSubscription : : UnsubscribeSensor ( aux2 ) ;
2020-04-07 13:07:09 -07:00
}