wideband/firmware/can.cpp

170 lines
4.3 KiB
C++
Raw Normal View History

2020-09-19 16:50:55 -07:00
#include "can.h"
#include "hal.h"
2021-11-03 23:12:20 -07:00
#include "fault.h"
2020-09-19 16:50:55 -07:00
#include "can_helper.h"
2020-12-08 23:16:47 -08:00
#include "heater_control.h"
2020-12-10 18:08:06 -08:00
#include "lambda_conversion.h"
#include "sampling.h"
2020-12-15 15:01:38 -08:00
#include "pump_dac.h"
2021-02-25 22:57:44 -08:00
#include "port.h"
2020-09-19 16:50:55 -07:00
2021-11-06 02:12:29 -07:00
// this same header is imported by rusEFI to get struct layouts and firmware version
#include "../for_rusefi/wideband_can.h"
static Configuration* configuration;
2021-05-19 00:53:52 -07:00
2020-12-10 18:08:06 -08:00
static THD_WORKING_AREA(waCanTxThread, 256);
void CanTxThread(void*)
{
chRegSetThreadName("CAN Tx");
2020-12-10 18:08:06 -08:00
while(1)
{
for (int ch = 0; ch < AFR_CHANNELS; ch++) {
2023-03-28 00:01:21 -07:00
SendCanForChannel(ch);
}
2020-12-10 18:08:06 -08:00
chThdSleepMilliseconds(10);
}
}
2021-03-14 00:22:58 -08:00
static void SendAck()
{
CANTxFrame frame;
frame.IDE = CAN_IDE_EXT;
frame.EID = WB_ACK;
2021-03-14 00:22:58 -08:00
frame.RTR = CAN_RTR_DATA;
frame.DLC = 0;
canTransmitTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE);
}
// Start in Unknown state. If no CAN message is ever received, we operate
// on internal battery sense etc.
static HeaterAllow heaterAllow = HeaterAllow::Unknown;
static float remoteBatteryVoltage = 0;
static THD_WORKING_AREA(waCanRxThread, 512);
2020-12-12 20:18:20 -08:00
void CanRxThread(void*)
{
chRegSetThreadName("CAN Rx");
2020-12-12 20:18:20 -08:00
while(1)
{
CANRxFrame frame;
msg_t msg = canReceiveTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE);
// Ignore non-ok results...
if (msg != MSG_OK)
{
continue;
}
2020-12-19 14:56:22 -08:00
// Ignore std frames, only listen to ext
if (frame.IDE != CAN_IDE_EXT)
{
continue;
}
if (frame.DLC == 2 && frame.EID == WB_MGS_ECU_STATUS) {
2021-07-12 15:31:02 -07:00
// This is status from ECU - battery voltage and heater enable signal
// data1 contains heater enable bit
if ((frame.data8[1] & 0x1) == 0x1)
{
heaterAllow = HeaterAllow::Allowed;
}
else
{
heaterAllow = HeaterAllow::NotAllowed;
}
2021-07-12 15:31:02 -07:00
// data0 contains battery voltage in tenths of a volt
float vbatt = frame.data8[0] * 0.1f;
if (vbatt < 5)
{
// provided vbatt is bogus, default to 14v nominal
remoteBatteryVoltage = 14;
}
else
{
remoteBatteryVoltage = vbatt;
}
2021-07-12 15:31:02 -07:00
}
2020-12-12 20:18:20 -08:00
// If it's a bootloader entry request, reboot to the bootloader!
else if (frame.DLC == 0 && frame.EID == WB_BL_ENTER)
2020-12-12 20:18:20 -08:00
{
2021-03-14 00:22:58 -08:00
SendAck();
2020-12-12 20:18:20 -08:00
// Let the message get out before we reset the chip
chThdSleep(50);
NVIC_SystemReset();
}
2021-03-14 00:22:58 -08:00
// Check if it's an "index set" message
else if (frame.DLC == 1 && frame.EID == WB_MSG_SET_INDEX)
2021-03-14 00:22:58 -08:00
{
2021-05-19 00:53:52 -07:00
configuration = GetConfiguration();
configuration->CanIndexOffset = frame.data8[0];
SetConfiguration();
2021-03-14 00:22:58 -08:00
SendAck();
}
2020-12-12 20:18:20 -08:00
}
}
HeaterAllow GetHeaterAllowed()
{
return heaterAllow;
}
float GetRemoteBatteryVoltage()
{
return remoteBatteryVoltage;
}
2020-09-19 16:50:55 -07:00
void InitCan()
{
2021-05-19 00:53:52 -07:00
configuration = GetConfiguration();
2023-03-15 18:07:31 -07:00
canStart(&CAND1, &GetCanConfig());
2020-12-10 18:08:06 -08:00
chThdCreateStatic(waCanTxThread, sizeof(waCanTxThread), NORMALPRIO, CanTxThread, nullptr);
2020-12-12 20:18:20 -08:00
chThdCreateStatic(waCanRxThread, sizeof(waCanRxThread), NORMALPRIO - 4, CanRxThread, nullptr);
2020-09-19 16:50:55 -07:00
}
void SendRusefiFormat(uint8_t ch)
2021-11-03 23:12:20 -07:00
{
auto baseAddress = WB_DATA_BASE_ADDR + 2 * (ch + configuration->CanIndexOffset);
2021-11-06 02:17:07 -07:00
2021-11-03 23:12:20 -07:00
{
2021-11-06 02:17:07 -07:00
CanTxTyped<wbo::StandardData> frame(baseAddress + 0);
2021-11-06 02:12:29 -07:00
// The same header is imported by the ECU and checked against this data in the frame
frame.get().Version = RUSEFI_WIDEBAND_VERSION;
2021-11-03 23:12:20 -07:00
uint16_t lambda = GetLambda(ch) * 10000;
2021-11-03 23:12:20 -07:00
frame.get().Lambda = lambda;
frame.get().TemperatureC = GetSensorTemperature(ch);
frame.get().Valid = IsRunningClosedLoop(ch) ? 0x01 : 0x00;
2021-11-03 23:12:20 -07:00
}
{
auto esr = GetSensorInternalResistance(ch);
2021-11-06 02:17:07 -07:00
CanTxTyped<wbo::DiagData> frame(baseAddress + 1);
2021-11-03 23:12:20 -07:00
2021-11-06 02:20:47 -07:00
frame.get().Esr = esr;
frame.get().NernstDc = GetNernstDc(ch) * 1000;
frame.get().PumpDuty = GetPumpOutputDuty(ch) * 255;
frame.get().Status = GetCurrentFault(ch);
frame.get().HeaterDuty = GetHeaterDuty(ch) * 255;
2021-11-03 23:12:20 -07:00
}
2020-09-19 16:50:55 -07:00
}
2023-03-28 00:01:21 -07:00
// Weak link so boards can override it
__attribute__((weak)) void SendCanForChannel(uint8_t ch)
{
SendRusefiFormat(ch);
}