spi_temperature: Separate out fault handling from temperature calculation

Introduce a new handle_fault() method to handle processing of sensor
faults.  This simplifies the calc_temp() methods.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-09-19 14:04:18 -04:00
parent 4e930294b8
commit ec5719585e
1 changed files with 35 additions and 26 deletions

View File

@ -51,12 +51,15 @@ class SensorBase:
self.oid, clock, self._report_clock, self.oid, clock, self._report_clock,
self.min_sample_value, self.max_sample_value), is_init=True) self.min_sample_value, self.max_sample_value), is_init=True)
def _handle_spi_response(self, params): def _handle_spi_response(self, params):
temp = self.calc_temp(params['value'], params['fault']) if params['fault']:
self.handle_fault(params['value'], params['fault'])
return
temp = self.calc_temp(params['value'])
next_clock = self.mcu.clock32_to_clock64(params['next_clock']) next_clock = self.mcu.clock32_to_clock64(params['next_clock'])
last_read_clock = next_clock - self._report_clock last_read_clock = next_clock - self._report_clock
last_read_time = self.mcu.clock_to_print_time(last_read_clock) last_read_time = self.mcu.clock_to_print_time(last_read_clock)
self._callback(last_read_time, temp) self._callback(last_read_time, temp)
def fault(self, msg): def report_fault(self, msg):
self.printer.invoke_async_shutdown(msg) self.printer.invoke_async_shutdown(msg)
@ -120,23 +123,24 @@ class MAX31856(SensorBase):
def __init__(self, config): def __init__(self, config):
SensorBase.__init__(self, config, "MAX31856", SensorBase.__init__(self, config, "MAX31856",
self.build_spi_init(config)) self.build_spi_init(config))
def calc_temp(self, adc, fault): def handle_fault(self, adc, fault):
if fault & MAX31856_FAULT_CJRANGE: if fault & MAX31856_FAULT_CJRANGE:
self.fault("Max31856: Cold Junction Range Fault") self.report_fault("Max31856: Cold Junction Range Fault")
if fault & MAX31856_FAULT_TCRANGE: if fault & MAX31856_FAULT_TCRANGE:
self.fault("Max31856: Thermocouple Range Fault") self.report_fault("Max31856: Thermocouple Range Fault")
if fault & MAX31856_FAULT_CJHIGH: if fault & MAX31856_FAULT_CJHIGH:
self.fault("Max31856: Cold Junction High Fault") self.report_fault("Max31856: Cold Junction High Fault")
if fault & MAX31856_FAULT_CJLOW: if fault & MAX31856_FAULT_CJLOW:
self.fault("Max31856: Cold Junction Low Fault") self.report_fault("Max31856: Cold Junction Low Fault")
if fault & MAX31856_FAULT_TCHIGH: if fault & MAX31856_FAULT_TCHIGH:
self.fault("Max31856: Thermocouple High Fault") self.report_fault("Max31856: Thermocouple High Fault")
if fault & MAX31856_FAULT_TCLOW: if fault & MAX31856_FAULT_TCLOW:
self.fault("Max31856: Thermocouple Low Fault") self.report_fault("Max31856: Thermocouple Low Fault")
if fault & MAX31856_FAULT_OVUV: if fault & MAX31856_FAULT_OVUV:
self.fault("Max31856: Over/Under Voltage Fault") self.report_fault("Max31856: Over/Under Voltage Fault")
if fault & MAX31856_FAULT_OPEN: if fault & MAX31856_FAULT_OPEN:
self.fault("Max31856: Thermocouple Open Fault") self.report_fault("Max31856: Thermocouple Open Fault")
def calc_temp(self, adc):
adc = adc >> MAX31856_SCALE adc = adc >> MAX31856_SCALE
# Fix sign bit: # Fix sign bit:
if adc & 0x40000: if adc & 0x40000:
@ -192,13 +196,14 @@ MAX31855_MULT = 0.25
class MAX31855(SensorBase): class MAX31855(SensorBase):
def __init__(self, config): def __init__(self, config):
SensorBase.__init__(self, config, "MAX31855", spi_mode=0) SensorBase.__init__(self, config, "MAX31855", spi_mode=0)
def calc_temp(self, adc, fault): def handle_fault(self, adc, fault):
if fault & 0x1: if fault & 0x1:
self.fault("MAX31855 : Open Circuit") self.report_fault("MAX31855 : Open Circuit")
if fault & 0x2: if fault & 0x2:
self.fault("MAX31855 : Short to GND") self.report_fault("MAX31855 : Short to GND")
if fault & 0x4: if fault & 0x4:
self.fault("MAX31855 : Short to Vcc") self.report_fault("MAX31855 : Short to Vcc")
def calc_temp(self, adc):
adc = adc >> MAX31855_SCALE adc = adc >> MAX31855_SCALE
# Fix sign bit: # Fix sign bit:
if adc & 0x2000: if adc & 0x2000:
@ -221,11 +226,12 @@ MAX6675_MULT = 0.25
class MAX6675(SensorBase): class MAX6675(SensorBase):
def __init__(self, config): def __init__(self, config):
SensorBase.__init__(self, config, "MAX6675", spi_mode=0) SensorBase.__init__(self, config, "MAX6675", spi_mode=0)
def calc_temp(self, adc, fault): def handle_fault(self, adc, fault):
if fault & 0x02: if fault & 0x02:
self.fault("Max6675 : Device ID error") self.report_fault("Max6675 : Device ID error")
if fault & 0x04: if fault & 0x04:
self.fault("Max6675 : Thermocouple Open Fault") self.report_fault("Max6675 : Thermocouple Open Fault")
def calc_temp(self, adc):
adc = adc >> MAX6675_SCALE adc = adc >> MAX6675_SCALE
# Fix sign bit: # Fix sign bit:
if adc & 0x2000: if adc & 0x2000:
@ -279,22 +285,25 @@ class MAX31865(SensorBase):
self.adc_to_resist_div_nominal = adc_to_resist / rtd_nominal_r self.adc_to_resist_div_nominal = adc_to_resist / rtd_nominal_r
SensorBase.__init__(self, config, "MAX31865", SensorBase.__init__(self, config, "MAX31865",
self.build_spi_init(config)) self.build_spi_init(config))
def calc_temp(self, adc, fault): def handle_fault(self, adc, fault):
if fault & 0x80: if fault & 0x80:
self.fault("Max31865 RTD input is disconnected") self.report_fault("Max31865 RTD input is disconnected")
if fault & 0x40: if fault & 0x40:
self.fault("Max31865 RTD input is shorted") self.report_fault("Max31865 RTD input is shorted")
if fault & 0x20: if fault & 0x20:
self.fault( self.report_fault(
"Max31865 VREF- is greater than 0.85 * VBIAS, FORCE- open") "Max31865 VREF- is greater than 0.85 * VBIAS, FORCE- open")
if fault & 0x10: if fault & 0x10:
self.fault("Max31865 VREF- is less than 0.85 * VBIAS, FORCE- open") self.report_fault(
"Max31865 VREF- is less than 0.85 * VBIAS, FORCE- open")
if fault & 0x08: if fault & 0x08:
self.fault("Max31865 VRTD- is less than 0.85 * VBIAS, FORCE- open") self.report_fault(
"Max31865 VRTD- is less than 0.85 * VBIAS, FORCE- open")
if fault & 0x04: if fault & 0x04:
self.fault("Max31865 Overvoltage or undervoltage fault") self.report_fault("Max31865 Overvoltage or undervoltage fault")
if not fault & 0xfc: if not fault & 0xfc:
self.fault("Max31865 Unspecified error") self.report_fault("Max31865 Unspecified error")
def calc_temp(self, adc):
adc = adc >> 1 # remove fault bit adc = adc >> 1 # remove fault bit
R_div_nominal = adc * self.adc_to_resist_div_nominal R_div_nominal = adc * self.adc_to_resist_div_nominal
# Resistance (relative to rtd_nominal_r) is calculated using: # Resistance (relative to rtd_nominal_r) is calculated using: