diff --git a/firmware/livedata.cpp b/firmware/livedata.cpp index 4d02027..3954c90 100644 --- a/firmware/livedata.cpp +++ b/firmware/livedata.cpp @@ -5,6 +5,8 @@ #include "sampling.h" #include "pump_dac.h" #include "heater_control.h" +#include "max31855.h" +#include "fault.h" #include #include @@ -24,6 +26,8 @@ void SamplingUpdateLiveData() data->pumpCurrentTarget = GetPumpCurrent(ch); data->pumpCurrentMeasured = GetPumpNominalCurrent(ch); data->heaterDuty = GetHeaterDuty(ch); + data->fault = (uint8_t)GetCurrentFault(ch); + data->heaterState = (uint8_t)GetHeaterState(ch); } livedata_common.vbatt = GetInternalBatteryVoltage(0); @@ -42,8 +46,11 @@ const struct livedata_afr_s * getAfrLiveDataStructAddr(const int ch) } static const FragmentEntry fragments[] = { - getCommonLiveDataStructAddr(), - getAfrLiveDataStructAddr(0), + getCommonLiveDataStructAddr(), + getAfrLiveDataStructAddr(0), + getAfrLiveDataStructAddr(1), + getEgtLiveDataStructAddr(0), + getEgtLiveDataStructAddr(1) }; FragmentList getFragments() { diff --git a/firmware/livedata.h b/firmware/livedata.h index ca06aab..442c575 100644 --- a/firmware/livedata.h +++ b/firmware/livedata.h @@ -25,7 +25,9 @@ struct livedata_afr_s { float pumpCurrentTarget; float pumpCurrentMeasured; float heaterDuty; - }; + uint8_t fault; // See wbo::Fault + uint8_t heaterState; + } __attribute__((packed)); uint8_t pad[32]; }; }; diff --git a/firmware/max31855.cpp b/firmware/max31855.cpp index dc83d3c..ce97928 100644 --- a/firmware/max31855.cpp +++ b/firmware/max31855.cpp @@ -1,9 +1,9 @@ #include -#include "hal.h" #include "io_pins.h" #include "wideband_config.h" #include "bit.h" +#include "livedata.h" #include "max31855.h" @@ -74,48 +74,54 @@ int Max31855::spi_rx(uint32_t *data) int Max31855::readPacket() { uint32_t data; - state = MAX31855_NO_REPLY; int ret = spi_rx(&data); - if (ret) - return ret; - if (data == 0xffffffff) { - state = MAX31855_NO_REPLY; - return -1; - } + /* TODO: also check for 0x00000000? */ + if ((ret) || (data == 0xffffffff)) { + livedata.state = MAX31855_NO_REPLY; - if (data & BIT(16)) { + ret = -1; + } else if (data & BIT(16)) { if (data & BIT(0)) { - state = MAX31855_OPEN_CIRCUIT; + livedata.state = MAX31855_OPEN_CIRCUIT; } else if (data & BIT(1)) { - state = MAX31855_SHORT_TO_GND; + livedata.state = MAX31855_SHORT_TO_GND; } else if (data & BIT(2)) { - state = MAX31855_SHORT_TO_VCC; + livedata.state = MAX31855_SHORT_TO_VCC; } - cold_joint_temperature = NAN; - temperature = NAN; + ret = -1; + } else { + livedata.state = MAX31855_OK; - return -1; + /* D[15:4] */ + int16_t tmp = (data >> 4) & 0xfff; + /* extend sign */ + tmp = tmp << 4; + tmp = tmp >> 4; /* shifting right signed is not a good idea */ + coldJunctionTemperature = (float)tmp * 0.0625; + + /* D[31:18] */ + tmp = (data >> 18) & 0x3fff; + /* extend sign */ + tmp = tmp << 2; + tmp = tmp >> 2; /* shifting right signed is not a good idea */ + temperature = (float) tmp * 0.25; } - state = MAX31855_OK; - /* D[15:4] */ - int16_t tmp = (data >> 4) & 0xfff; - /* extend sign */ - tmp = tmp << 4; - tmp = tmp >> 4; /* shifting right signed is not a good idea */ - cold_joint_temperature = (float)tmp * 0.0625; + if (ret) { + coldJunctionTemperature = NAN; + livedata.coldJunctionTemperature = 0; + temperature = NAN; + livedata.temperature = 0; + } else { + /* update livedata: float to int */ + livedata.coldJunctionTemperature = coldJunctionTemperature; + livedata.temperature = temperature; + } - /* D[31:18] */ - tmp = (data >> 18) & 0x3fff; - /* extend sign */ - tmp = tmp << 2; - tmp = tmp >> 2; /* shifting right signed is not a good idea */ - temperature = (float) tmp * 0.25; - - return 0; + return ret; } void Max31855Thread::ThreadTask() { @@ -138,4 +144,18 @@ Max31855* getEgtDrivers() { return instances; } +const struct livedata_egt_s * getEgtLiveDataStructAddr(const int ch) +{ + if (ch < EGT_CHANNELS) + return &getEgtDrivers()[ch].livedata; + return NULL; +} + +#else + +const struct livedata_egt_s * getEgtLiveDataStructAddr(const int ch) +{ + return NULL; +} + #endif /* HAL_USE_SPI */ diff --git a/firmware/max31855.h b/firmware/max31855.h index 482a9f0..82b8b41 100644 --- a/firmware/max31855.h +++ b/firmware/max31855.h @@ -1,13 +1,10 @@ #pragma once +#include "hal.h" + #include "wideband_config.h" #include "thread_controller.h" -#if HAL_USE_SPI - -#define MAX31855_THREAD_STACK (512) -#define MAX31855_THREAD_PRIO (NORMALPRIO + 1) - typedef enum { MAX31855_OK = 0, MAX31855_OPEN_CIRCUIT = 1, @@ -16,13 +13,34 @@ typedef enum { MAX31855_NO_REPLY = 4, } Max31855State; +/* livedata: +96/112 offset, size = 16 */ +struct livedata_egt_s { + union { + struct { + float temperature; + float coldJunctionTemperature; + uint8_t state; + } __attribute__((packed)); + uint8_t pad[16]; + }; +}; + +// for all board. in case of no EGT - returns NULL +const struct livedata_egt_s * getEgtLiveDataStructAddr(const int ch); + +#if HAL_USE_SPI + +#define MAX31855_THREAD_STACK (512) +#define MAX31855_THREAD_PRIO (NORMALPRIO + 1) + class Max31855 { public: Max31855(SPIConfig *spi) { this->spi = spi; } - Max31855State state; - float cold_joint_temperature; + livedata_egt_s livedata; + /* do we need float temperatures? */ + float coldJunctionTemperature; float temperature; int readPacket(); private: diff --git a/firmware/uart.cpp b/firmware/uart.cpp index 6e8ee51..ca5d0e6 100644 --- a/firmware/uart.cpp +++ b/firmware/uart.cpp @@ -62,7 +62,7 @@ static void UartThread(void*) size_t writeCount = chsnprintf(printBuffer, 200, "EGT[%d]: %d C (int %d C)\r\n", (int)getEgtDrivers()[ch].temperature, - (int)getEgtDrivers()[ch].cold_joint_temperature); + (int)getEgtDrivers()[ch].coldJunctionTemperature); chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount); } #endif /* HAL_USE_SPI */