Livedata update (#163)

* max31855: add EGT to livedata

* livedata: getEgtLiveDataStructAddr() returns NULL for boards with no EGT

* livedata: report AFR sensor(s) state

* livedata: report heater state

* uart: fix for max31855 update
This commit is contained in:
Andrey G 2022-09-21 00:50:06 +03:00 committed by GitHub
parent adae19db09
commit 516747b629
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 41 deletions

View File

@ -5,6 +5,8 @@
#include "sampling.h" #include "sampling.h"
#include "pump_dac.h" #include "pump_dac.h"
#include "heater_control.h" #include "heater_control.h"
#include "max31855.h"
#include "fault.h"
#include <rusefi/arrays.h> #include <rusefi/arrays.h>
#include <rusefi/fragments.h> #include <rusefi/fragments.h>
@ -24,6 +26,8 @@ void SamplingUpdateLiveData()
data->pumpCurrentTarget = GetPumpCurrent(ch); data->pumpCurrentTarget = GetPumpCurrent(ch);
data->pumpCurrentMeasured = GetPumpNominalCurrent(ch); data->pumpCurrentMeasured = GetPumpNominalCurrent(ch);
data->heaterDuty = GetHeaterDuty(ch); data->heaterDuty = GetHeaterDuty(ch);
data->fault = (uint8_t)GetCurrentFault(ch);
data->heaterState = (uint8_t)GetHeaterState(ch);
} }
livedata_common.vbatt = GetInternalBatteryVoltage(0); livedata_common.vbatt = GetInternalBatteryVoltage(0);
@ -44,6 +48,9 @@ const struct livedata_afr_s * getAfrLiveDataStructAddr(const int ch)
static const FragmentEntry fragments[] = { static const FragmentEntry fragments[] = {
getCommonLiveDataStructAddr(), getCommonLiveDataStructAddr(),
getAfrLiveDataStructAddr(0), getAfrLiveDataStructAddr(0),
getAfrLiveDataStructAddr(1),
getEgtLiveDataStructAddr(0),
getEgtLiveDataStructAddr(1)
}; };
FragmentList getFragments() { FragmentList getFragments() {

View File

@ -25,7 +25,9 @@ struct livedata_afr_s {
float pumpCurrentTarget; float pumpCurrentTarget;
float pumpCurrentMeasured; float pumpCurrentMeasured;
float heaterDuty; float heaterDuty;
}; uint8_t fault; // See wbo::Fault
uint8_t heaterState;
} __attribute__((packed));
uint8_t pad[32]; uint8_t pad[32];
}; };
}; };

View File

@ -1,9 +1,9 @@
#include <math.h> #include <math.h>
#include "hal.h"
#include "io_pins.h" #include "io_pins.h"
#include "wideband_config.h" #include "wideband_config.h"
#include "bit.h" #include "bit.h"
#include "livedata.h"
#include "max31855.h" #include "max31855.h"
@ -74,39 +74,33 @@ int Max31855::spi_rx(uint32_t *data)
int Max31855::readPacket() int Max31855::readPacket()
{ {
uint32_t data; uint32_t data;
state = MAX31855_NO_REPLY;
int ret = spi_rx(&data); int ret = spi_rx(&data);
if (ret)
return ret;
if (data == 0xffffffff) { /* TODO: also check for 0x00000000? */
state = MAX31855_NO_REPLY; if ((ret) || (data == 0xffffffff)) {
return -1; livedata.state = MAX31855_NO_REPLY;
}
if (data & BIT(16)) { ret = -1;
} else if (data & BIT(16)) {
if (data & BIT(0)) { if (data & BIT(0)) {
state = MAX31855_OPEN_CIRCUIT; livedata.state = MAX31855_OPEN_CIRCUIT;
} else if (data & BIT(1)) { } else if (data & BIT(1)) {
state = MAX31855_SHORT_TO_GND; livedata.state = MAX31855_SHORT_TO_GND;
} else if (data & BIT(2)) { } else if (data & BIT(2)) {
state = MAX31855_SHORT_TO_VCC; livedata.state = MAX31855_SHORT_TO_VCC;
} }
cold_joint_temperature = NAN; ret = -1;
temperature = NAN; } else {
livedata.state = MAX31855_OK;
return -1;
}
state = MAX31855_OK;
/* D[15:4] */ /* D[15:4] */
int16_t tmp = (data >> 4) & 0xfff; int16_t tmp = (data >> 4) & 0xfff;
/* extend sign */ /* extend sign */
tmp = tmp << 4; tmp = tmp << 4;
tmp = tmp >> 4; /* shifting right signed is not a good idea */ tmp = tmp >> 4; /* shifting right signed is not a good idea */
cold_joint_temperature = (float)tmp * 0.0625; coldJunctionTemperature = (float)tmp * 0.0625;
/* D[31:18] */ /* D[31:18] */
tmp = (data >> 18) & 0x3fff; tmp = (data >> 18) & 0x3fff;
@ -114,8 +108,20 @@ int Max31855::readPacket()
tmp = tmp << 2; tmp = tmp << 2;
tmp = tmp >> 2; /* shifting right signed is not a good idea */ tmp = tmp >> 2; /* shifting right signed is not a good idea */
temperature = (float) tmp * 0.25; temperature = (float) tmp * 0.25;
}
return 0; if (ret) {
coldJunctionTemperature = NAN;
livedata.coldJunctionTemperature = 0;
temperature = NAN;
livedata.temperature = 0;
} else {
/* update livedata: float to int */
livedata.coldJunctionTemperature = coldJunctionTemperature;
livedata.temperature = temperature;
}
return ret;
} }
void Max31855Thread::ThreadTask() { void Max31855Thread::ThreadTask() {
@ -138,4 +144,18 @@ Max31855* getEgtDrivers() {
return instances; 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 */ #endif /* HAL_USE_SPI */

View File

@ -1,13 +1,10 @@
#pragma once #pragma once
#include "hal.h"
#include "wideband_config.h" #include "wideband_config.h"
#include "thread_controller.h" #include "thread_controller.h"
#if HAL_USE_SPI
#define MAX31855_THREAD_STACK (512)
#define MAX31855_THREAD_PRIO (NORMALPRIO + 1)
typedef enum { typedef enum {
MAX31855_OK = 0, MAX31855_OK = 0,
MAX31855_OPEN_CIRCUIT = 1, MAX31855_OPEN_CIRCUIT = 1,
@ -16,13 +13,34 @@ typedef enum {
MAX31855_NO_REPLY = 4, MAX31855_NO_REPLY = 4,
} Max31855State; } 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 { class Max31855 {
public: public:
Max31855(SPIConfig *spi) { Max31855(SPIConfig *spi) {
this->spi = spi; this->spi = spi;
} }
Max31855State state; livedata_egt_s livedata;
float cold_joint_temperature; /* do we need float temperatures? */
float coldJunctionTemperature;
float temperature; float temperature;
int readPacket(); int readPacket();
private: private:

View File

@ -62,7 +62,7 @@ static void UartThread(void*)
size_t writeCount = chsnprintf(printBuffer, 200, size_t writeCount = chsnprintf(printBuffer, 200,
"EGT[%d]: %d C (int %d C)\r\n", "EGT[%d]: %d C (int %d C)\r\n",
(int)getEgtDrivers()[ch].temperature, (int)getEgtDrivers()[ch].temperature,
(int)getEgtDrivers()[ch].cold_joint_temperature); (int)getEgtDrivers()[ch].coldJunctionTemperature);
chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount); chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount);
} }
#endif /* HAL_USE_SPI */ #endif /* HAL_USE_SPI */