2020-12-10 23:48:55 -08:00
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "chprintf.h"
|
|
|
|
|
2020-12-10 23:56:18 -08:00
|
|
|
#include "lambda_conversion.h"
|
|
|
|
#include "sampling.h"
|
2022-05-11 01:41:07 -07:00
|
|
|
#include "heater_control.h"
|
|
|
|
#include "fault.h"
|
2020-12-10 23:48:55 -08:00
|
|
|
#include "uart.h"
|
|
|
|
|
2022-07-15 11:10:15 -07:00
|
|
|
#include "tunerstudio.h"
|
|
|
|
#include "tunerstudio_io.h"
|
|
|
|
#include "wideband_board_config.h"
|
|
|
|
|
|
|
|
#ifdef UART_DEBUG
|
|
|
|
// just a reminder that we have either TS connectivity or this UART_DEBUG but not both
|
2021-02-25 22:49:51 -08:00
|
|
|
|
2022-07-15 11:10:15 -07:00
|
|
|
SerialConfig cfg = {
|
2020-12-11 00:24:19 -08:00
|
|
|
.speed = 115200,
|
2020-12-10 23:48:55 -08:00
|
|
|
.cr1 = 0,
|
2022-07-15 11:10:15 -07:00
|
|
|
.cr2 = USART_CR2_STOP1_BITS | USART_CR2_LINEN,
|
|
|
|
.cr3 = 0
|
2020-12-10 23:48:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static char printBuffer[200];
|
|
|
|
|
2022-05-11 01:41:07 -07:00
|
|
|
static THD_WORKING_AREA(waUartThread, 512);
|
2020-12-10 23:48:55 -08:00
|
|
|
static void UartThread(void*)
|
|
|
|
{
|
2022-07-15 11:10:15 -07:00
|
|
|
// in UART_DEBUG mode we only support Serial - this file name here has a bit of a confusing naming
|
|
|
|
sdStart(&SD1, &cfg);
|
|
|
|
|
2020-12-10 23:56:18 -08:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
float lambda = GetLambda();
|
|
|
|
int lambdaIntPart = lambda;
|
|
|
|
int lambdaThousandths = (lambda - lambdaIntPart) * 1000;
|
2022-05-11 01:41:07 -07:00
|
|
|
int batteryVoltageMv = GetInternalBatteryVoltage() * 1000;
|
|
|
|
int duty = GetHeaterDuty() * 100;
|
2020-12-10 23:48:55 -08:00
|
|
|
|
2022-05-11 01:41:07 -07:00
|
|
|
size_t writeCount = chsnprintf(printBuffer, 200,
|
|
|
|
"%d.%03d\tAC %d mV\tR: %d\tT: %d\tIpump: %d\tVbat: %d\theater: %s (%d)\tfault: %s\r\n",
|
|
|
|
lambdaIntPart, lambdaThousandths,
|
|
|
|
(int)(GetNernstAc() * 1000.0),
|
|
|
|
(int)GetSensorInternalResistance(),
|
|
|
|
(int)GetSensorTemperature(),
|
|
|
|
(int)(GetPumpNominalCurrent() * 1000),
|
|
|
|
batteryVoltageMv,
|
|
|
|
describeHeaterState(GetHeaterState()), duty,
|
|
|
|
describeFault(GetCurrentFault()));
|
2022-07-15 11:10:15 -07:00
|
|
|
chnWrite(&SD1, (const uint8_t *)printBuffer, writeCount);
|
2020-12-10 23:56:18 -08:00
|
|
|
|
2022-05-11 01:41:07 -07:00
|
|
|
chThdSleepMilliseconds(50);
|
2020-12-10 23:56:18 -08:00
|
|
|
}
|
2020-12-10 23:48:55 -08:00
|
|
|
}
|
|
|
|
|
2022-07-15 11:10:15 -07:00
|
|
|
#elif defined(TS_PRIMARY_UART_PORT) || defined(TS_PRIMARY_SERIAL_PORT)
|
|
|
|
|
|
|
|
#ifdef TS_PRIMARY_UART_PORT
|
|
|
|
static UartTsChannel primaryChannel(TS_PRIMARY_UART_PORT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TS_PRIMARY_SERIAL_PORT
|
|
|
|
static SerialTsChannel primaryChannel(TS_PRIMARY_SERIAL_PORT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct PrimaryChannelThread : public TunerstudioThread {
|
|
|
|
PrimaryChannelThread() : TunerstudioThread("Primary TS Channel") { }
|
|
|
|
|
|
|
|
TsChannelBase* setupChannel() {
|
|
|
|
primaryChannel.start(TS_PRIMARY_BAUDRATE);
|
|
|
|
|
|
|
|
return &primaryChannel;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PrimaryChannelThread primaryChannelThread;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-12-10 23:48:55 -08:00
|
|
|
void InitUart()
|
|
|
|
{
|
2022-07-15 11:10:15 -07:00
|
|
|
#ifdef UART_DEBUG
|
2020-12-10 23:48:55 -08:00
|
|
|
chThdCreateStatic(waUartThread, sizeof(waUartThread), NORMALPRIO, UartThread, nullptr);
|
2022-07-15 11:10:15 -07:00
|
|
|
#elif defined(TS_PRIMARY_UART_PORT) || defined(TS_PRIMARY_SERIAL_PORT)
|
|
|
|
primaryChannelThread.Start();
|
|
|
|
#endif
|
2020-12-10 23:48:55 -08:00
|
|
|
}
|