Add max31855 driver

This commit is contained in:
Andrey Gusakov 2022-06-07 22:35:21 +03:00
parent e80792c3f0
commit b727ba8858
6 changed files with 186 additions and 0 deletions

View File

@ -154,6 +154,7 @@ CPPSRC = $(ALLCPPSRC) \
console/binary/tunerstudio_io_serial.cpp \
console/binary/tunerstudio_commands.cpp \
console/binary/FragmentEntry.cpp \
max31855.cpp \
main.cpp
# List ASM source files here.

View File

@ -10,6 +10,9 @@
#include "uart.h"
#include "io_pins.h"
#include "auxout.h"
#include "max31855.h"
#include "wideband_config.h"
using namespace wbo;
@ -35,6 +38,10 @@ int main() {
InitUart();
#endif
#if (EGT_CHANNELS > 0)
EgtThread.Start();
#endif
while(true)
{
auto fault = GetCurrentFault();

135
firmware/max31855.cpp Normal file
View File

@ -0,0 +1,135 @@
#include <math.h>
#include "hal.h"
#include "io_pins.h"
#include "wideband_config.h"
#include "bit.h"
#include "max31855.h"
#ifdef HAL_USE_SPI
static SPIConfig spi_config[2] =
{
{
.circular = false,
.end_cb = NULL,
.ssport = EGT_CS0_PORT,
.sspad = EGT_CS0_PIN,
.cr1 =
/* SPI_CR1_LSBFIRST | */
((3 << SPI_CR1_BR_Pos) & SPI_CR1_BR) | /* div = 16 */
/* SPI_CR1_CPOL | */ // = 0
SPI_CR1_CPHA | // = 1
0,
.cr2 = 0
},
{
.circular = false,
.end_cb = NULL,
.ssport = EGT_CS1_PORT,
.sspad = EGT_CS1_PIN,
.cr1 =
/* SPI_CR1_LSBFIRST | */
((3 << SPI_CR1_BR_Pos) & SPI_CR1_BR) | /* div = 16 */
/* SPI_CR1_CPOL | */ // = 0
SPI_CR1_CPHA | // = 1
0,
.cr2 = 0
}
};
int Max31855Thread::spi_rx(SPIDriver *spi, int ch, uint32_t *data)
{
uint8_t rx[4];
/* Acquire ownership of the bus. */
spiAcquireBus(spi);
/* Setup transfer parameters. */
spiStart(spi, &spi_config[ch]);
/* Slave Select assertion. */
spiSelect(spi);
//spiExchange(spi, 4, tx, rx);
spiReceive(spi, 4, rx);
/* Slave Select de-assertion. */
spiUnselect(spi);
/* Ownership release. */
spiReleaseBus(spi);
if (data) {
*data = (rx[0] << 24) |
(rx[1] << 16) |
(rx[2] << 8) |
(rx[3] << 0);
}
/* no errors for now */
return 0;
}
int Max31855Thread::Read(int ch)
{
int ret;
uint32_t data;
ret = spi_rx(&EGT_SPI_PORT, ch, &data);
if (ret)
return ret;
if (data == 0xffffffff) {
state = MAX31855_NO_REPLY;
return -1;
}
if (data & BIT(16)) {
if (data & BIT(0))
state = MAX31855_OPEN_CIRCUIT;
else if (data & BIT(1))
state = MAX31855_SHORT_TO_GND;
else if (data & BIT(2))
state = MAX31855_SHORT_TO_VCC;
int_temp[ch] = NAN;
temp[ch] = NAN;
return -1;
}
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 */
int_temp[ch] = (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 */
temp[ch] = (float) tmp * 0.25;
return 0;
}
void Max31855Thread::ThreadTask() {
state = MAX31855_NO_REPLY;
while (true) {
int ch;
for (ch = 0; ch < EGT_CHANNELS; ch++) {
int ret = Read(ch);
if (ret)
continue;
}
chThdSleepMilliseconds(500);
}
}
Max31855Thread EgtThread("egt");
#endif /* HAL_USE_SPI */

33
firmware/max31855.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include "wideband_config.h"
#include "thread_controller.h"
#define MAX31855_THREAD_STACK (512)
#define MAX31855_THREAD_PRIO (NORMALPRIO + 1)
typedef enum {
MAX31855_OK = 0,
MAX31855_OPEN_CIRCUIT = 1,
MAX31855_SHORT_TO_GND = 2,
MAX31855_SHORT_TO_VCC = 3,
MAX31855_NO_REPLY = 4,
} Max31855State;
class Max31855Thread : public ThreadController<MAX31855_THREAD_STACK> {
private:
int spi_rx(SPIDriver *spi, int ch, uint32_t *data);
int Read(int ch);
public:
Max31855State state;
float int_temp[EGT_CHANNELS];
float temp[EGT_CHANNELS];
Max31855Thread(const char* name)
: ThreadController(name, MAX31855_THREAD_PRIO)
{
}
void ThreadTask() override;
};
extern Max31855Thread EgtThread;

View File

@ -5,6 +5,7 @@
#include "lambda_conversion.h"
#include "sampling.h"
#include "heater_control.h"
#include "max31855.h"
#include "fault.h"
#include "uart.h"
@ -47,7 +48,12 @@ static void UartThread(void*)
describeHeaterState(GetHeaterState()), duty,
describeFault(GetCurrentFault()));
chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount);
chThdSleepMilliseconds(50);
writeCount = chsnprintf(printBuffer, 200,
"EGT: %d C (int %d C)\r\n",
(int)EgtThread.temp[0], (int)EgtThread.int_temp[0]);
chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount);
chThdSleepMilliseconds(50);
}
}

View File

@ -10,6 +10,10 @@
#define AFR_CHANNELS 1
#endif
#ifndef EGT_CHANNELS
#define EGT_CHANNELS 0
#endif
// *******************************
// Nernst voltage & ESR sense
// *******************************