EGT: max31855: run driver as a thread instead of slow callback
So do not call SPI communication from ISR context. Also fix minor bus in driver: - one spi config struct for all chips - init spi config struct with some valid data (to be tested)
This commit is contained in:
parent
84ac47c4af
commit
94f08047df
|
@ -161,9 +161,6 @@ void Engine::periodicSlowCallback() {
|
||||||
updateVrThresholdPwm();
|
updateVrThresholdPwm();
|
||||||
|
|
||||||
updateGppwm();
|
updateGppwm();
|
||||||
#if EFI_MAX_31855
|
|
||||||
grabEgtValues();
|
|
||||||
#endif /* EFI_MAX_31855 */
|
|
||||||
|
|
||||||
engine->engineModules.apply_all([](auto & m) { m.onSlowCallback(); });
|
engine->engineModules.apply_all([](auto & m) { m.onSlowCallback(); });
|
||||||
|
|
||||||
|
|
|
@ -49,3 +49,6 @@
|
||||||
|
|
||||||
// Lua interpreter must be lowest priority, as the user's code may get stuck in an infinite loop
|
// Lua interpreter must be lowest priority, as the user's code may get stuck in an infinite loop
|
||||||
#define PRIO_LUA LOWPRIO + 10
|
#define PRIO_LUA LOWPRIO + 10
|
||||||
|
|
||||||
|
// MAX31855 driver
|
||||||
|
#define MAX31855_PRIO NORMALPRIO
|
||||||
|
|
|
@ -23,25 +23,30 @@
|
||||||
|
|
||||||
#if EFI_MAX_31855
|
#if EFI_MAX_31855
|
||||||
|
|
||||||
|
#include "thread_controller.h"
|
||||||
|
|
||||||
#define EGT_ERROR_VALUE -1000
|
#define EGT_ERROR_VALUE -1000
|
||||||
|
|
||||||
static SPIDriver *driver;
|
static SPIDriver *driver;
|
||||||
|
static egt_cs_array_t m_cs;
|
||||||
|
|
||||||
static SPIConfig spiConfig[EGT_CHANNEL_COUNT];
|
/* TODO: validate */
|
||||||
|
static SPIConfig spiConfig = {
|
||||||
static void showEgtInfo() {
|
.circular = false,
|
||||||
#if EFI_PROD_CODE
|
.end_cb = NULL,
|
||||||
printSpiState();
|
.ssport = NULL,
|
||||||
|
.sspad = 0,
|
||||||
efiPrintf("EGT spi: %d", engineConfiguration->max31855spiDevice);
|
.cr1 =
|
||||||
|
SPI_CR1_8BIT_MODE |
|
||||||
for (int i = 0; i < EGT_CHANNEL_COUNT; i++) {
|
SPI_CR1_SSM |
|
||||||
if (isBrainPinValid(engineConfiguration->max31855_cs[i])) {
|
SPI_CR1_SSI |
|
||||||
efiPrintf("%d ETG @ %s", i, hwPortname(engineConfiguration->max31855_cs[i]));
|
((3 << SPI_CR1_BR_Pos) & SPI_CR1_BR) | /* div = 16 */
|
||||||
}
|
SPI_CR1_MSTR |
|
||||||
}
|
/* SPI_CR1_CPOL | */ // = 0
|
||||||
#endif
|
SPI_CR1_CPHA | // = 1
|
||||||
}
|
0,
|
||||||
|
.cr2 = SPI_CR2_8BIT_MODE
|
||||||
|
};
|
||||||
|
|
||||||
// bits D17 and D3 are always expected to be zero
|
// bits D17 and D3 are always expected to be zero
|
||||||
#define MC_RESERVED_BITS 0x20008
|
#define MC_RESERVED_BITS 0x20008
|
||||||
|
@ -69,7 +74,7 @@ static const char * getMcCode(max_32855_code code) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static max_32855_code getResultCode(uint32_t egtPacket) {
|
static max_32855_code getResultCode(uint32_t egtPacket) {
|
||||||
if ((egtPacket & MC_RESERVED_BITS) != 0) {
|
if (((egtPacket & MC_RESERVED_BITS) != 0) || (egtPacket == 0x0)) {
|
||||||
return MC_INVALID;
|
return MC_INVALID;
|
||||||
} else if ((egtPacket & MC_OPEN_BIT) != 0) {
|
} else if ((egtPacket & MC_OPEN_BIT) != 0) {
|
||||||
return MC_OPEN;
|
return MC_OPEN;
|
||||||
|
@ -84,17 +89,23 @@ static max_32855_code getResultCode(uint32_t egtPacket) {
|
||||||
|
|
||||||
static uint32_t readEgtPacket(int egtChannel) {
|
static uint32_t readEgtPacket(int egtChannel) {
|
||||||
uint32_t egtPacket;
|
uint32_t egtPacket;
|
||||||
if (driver == NULL) {
|
brain_pin_e cs = m_cs[egtChannel];
|
||||||
|
|
||||||
|
if ((!isBrainPinValid(cs)) || (driver == NULL)) {
|
||||||
return 0xFFFFFFFF;
|
return 0xFFFFFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
spiStart(driver, &spiConfig[egtChannel]);
|
/* Set proper CS gpio */
|
||||||
|
initSpiCsNoOccupy(&spiConfig, cs);
|
||||||
|
|
||||||
|
spiStart(driver, &spiConfig);
|
||||||
spiSelect(driver);
|
spiSelect(driver);
|
||||||
|
|
||||||
spiReceive(driver, sizeof(egtPacket), &egtPacket);
|
spiReceive(driver, sizeof(egtPacket), &egtPacket);
|
||||||
|
|
||||||
spiUnselect(driver);
|
spiUnselect(driver);
|
||||||
spiStop(driver);
|
spiStop(driver);
|
||||||
|
|
||||||
egtPacket = SWAP_UINT32(egtPacket);
|
egtPacket = SWAP_UINT32(egtPacket);
|
||||||
return egtPacket;
|
return egtPacket;
|
||||||
}
|
}
|
||||||
|
@ -111,6 +122,20 @@ static uint16_t getMax31855EgtValue(int egtChannel) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void showEgtInfo() {
|
||||||
|
#if EFI_PROD_CODE
|
||||||
|
printSpiState();
|
||||||
|
|
||||||
|
efiPrintf("EGT spi: %d", engineConfiguration->max31855spiDevice);
|
||||||
|
|
||||||
|
for (int i = 0; i < EGT_CHANNEL_COUNT; i++) {
|
||||||
|
if (isBrainPinValid(engineConfiguration->max31855_cs[i])) {
|
||||||
|
efiPrintf("%d ETG @ %s", i, hwPortname(engineConfiguration->max31855_cs[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static void egtRead() {
|
static void egtRead() {
|
||||||
|
|
||||||
if (driver == NULL) {
|
if (driver == NULL) {
|
||||||
|
@ -124,10 +149,10 @@ static void egtRead() {
|
||||||
|
|
||||||
max_32855_code code = getResultCode(egtPacket);
|
max_32855_code code = getResultCode(egtPacket);
|
||||||
|
|
||||||
efiPrintf("egt %x code=%d %s", egtPacket, code, getMcCode(code));
|
efiPrintf("egt 0x%08x code=%d (%s)", egtPacket, code, getMcCode(code));
|
||||||
|
|
||||||
if (code != MC_INVALID) {
|
if (code != MC_INVALID) {
|
||||||
int refBits = ((egtPacket & 0xFFFF) / 16); // bits 15:4
|
int refBits = ((egtPacket & 0xFFF0) >> 4); // bits 15:4
|
||||||
float refTemp = refBits / 16.0;
|
float refTemp = refBits / 16.0;
|
||||||
efiPrintf("reference temperature %.2f", refTemp);
|
efiPrintf("reference temperature %.2f", refTemp);
|
||||||
|
|
||||||
|
@ -135,33 +160,56 @@ static void egtRead() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initMax31855(spi_device_e device, egt_cs_array_t max31855_cs) {
|
/* TODO: move all stuff to Max31855Read class */
|
||||||
driver = getSpiDevice(device);
|
class Max31855Read final : public ThreadController<UTILITY_THREAD_STACK_SIZE> {
|
||||||
if (driver == NULL) {
|
public:
|
||||||
// error already reported
|
Max31855Read()
|
||||||
return;
|
: ThreadController("MAX31855", MAX31855_PRIO)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo:spi device is now enabled separately - should probably be enabled here
|
int start(spi_device_e device, egt_cs_array_t cs) {
|
||||||
|
driver = getSpiDevice(device);
|
||||||
|
|
||||||
addConsoleAction("egtinfo", (Void) showEgtInfo);
|
if (driver) {
|
||||||
|
/* WARN: this will clear all other bits in cr1 */
|
||||||
|
spiConfig.cr1 = getSpiPrescaler(_5MHz, device);
|
||||||
|
for (size_t i = 0; i < EGT_CHANNEL_COUNT; i++) {
|
||||||
|
/* and mark used! */
|
||||||
|
if (isBrainPinValid(cs[i])) {
|
||||||
|
initSpiCs(&spiConfig, cs[i]);
|
||||||
|
m_cs[i] = cs[i];
|
||||||
|
} else {
|
||||||
|
m_cs[i] = Gpio::Invalid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ThreadController::start();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
addConsoleAction("egtread", (Void) egtRead);
|
void ThreadTask() override {
|
||||||
|
while (true) {
|
||||||
|
for (int i = 0; i < EGT_CHANNEL_COUNT; i++) {
|
||||||
|
// todo: migrate to SensorType framework!
|
||||||
|
engine->currentEgtValue[i] = getMax31855EgtValue(i);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < EGT_CHANNEL_COUNT; i++) {
|
chThdSleepMilliseconds(500);
|
||||||
if (isBrainPinValid(max31855_cs[i])) {
|
|
||||||
|
|
||||||
initSpiCs(&spiConfig[i], max31855_cs[i]);
|
|
||||||
|
|
||||||
spiConfig[i].cr1 = getSpiPrescaler(_5MHz, device);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void grabEgtValues() {
|
private:
|
||||||
for (int i = 0; i < EGT_CHANNEL_COUNT; i++) {
|
//brain_pin_e m_cs[EGT_CHANNEL_COUNT];
|
||||||
// todo: migrate to SensorType framework!
|
};
|
||||||
engine->currentEgtValue[i] = getMax31855EgtValue(i);
|
|
||||||
|
static Max31855Read instance;
|
||||||
|
|
||||||
|
void initMax31855(spi_device_e device, egt_cs_array_t max31855_cs) {
|
||||||
|
if (instance.start(device, max31855_cs) == 0) {
|
||||||
|
addConsoleAction("egtinfo", (Void) showEgtInfo);
|
||||||
|
addConsoleAction("egtread", (Void) egtRead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,5 +13,3 @@
|
||||||
#if HAL_USE_SPI
|
#if HAL_USE_SPI
|
||||||
void initMax31855(spi_device_e device, egt_cs_array_t max31855_cs);
|
void initMax31855(spi_device_e device, egt_cs_array_t max31855_cs);
|
||||||
#endif /* HAL_USE_SPI */
|
#endif /* HAL_USE_SPI */
|
||||||
|
|
||||||
void grabEgtValues();
|
|
||||||
|
|
Loading…
Reference in New Issue