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 "pump_dac.h"
#include "heater_control.h"
#include "max31855.h"
#include "fault.h"
#include <rusefi/arrays.h>
#include <rusefi/fragments.h>
@ -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() {

View File

@ -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];
};
};

View File

@ -1,9 +1,9 @@
#include <math.h>
#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 */

View File

@ -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:

View File

@ -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 */