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"
|
|
|
|
|
2021-05-19 00:53:52 -07:00
|
|
|
Configuration configuration;
|
|
|
|
|
2020-12-10 18:08:06 -08:00
|
|
|
static THD_WORKING_AREA(waCanTxThread, 256);
|
|
|
|
void CanTxThread(void*)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2021-11-03 22:40:43 -07:00
|
|
|
SendRusefiFormat(configuration.CanIndexOffset);
|
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;
|
2021-11-08 11:01:16 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:18:20 -08:00
|
|
|
static THD_WORKING_AREA(waCanRxThread, 256);
|
|
|
|
void CanRxThread(void*)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:01:16 -08:00
|
|
|
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
|
|
|
|
|
|
|
|
// data0 contains battery voltage in tenths of a volt
|
|
|
|
float vbatt = frame.data8[0] * 0.1f;
|
|
|
|
SetBatteryVoltage(vbatt);
|
|
|
|
|
|
|
|
// data1 contains heater enable bit
|
2021-07-15 21:50:22 -07:00
|
|
|
bool heaterAllowed = (frame.data8[1] & 0x1) == 0x1;
|
|
|
|
SetHeaterAllowed(heaterAllowed);
|
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!
|
2021-11-08 11:01:16 -08:00
|
|
|
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
|
2021-11-08 11:01:16 -08:00
|
|
|
else if (frame.DLC == 1 && frame.EID == WB_MSG_SET_INDEX)
|
2021-03-14 00:22:58 -08:00
|
|
|
{
|
|
|
|
auto newCfg = GetConfiguration();
|
|
|
|
newCfg.CanIndexOffset = frame.data8[0];
|
|
|
|
SetConfiguration(newCfg);
|
2021-05-19 00:53:52 -07:00
|
|
|
configuration = GetConfiguration();
|
2021-03-14 00:22:58 -08:00
|
|
|
SendAck();
|
|
|
|
}
|
2020-12-12 20:18:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:50:55 -07:00
|
|
|
void InitCan()
|
|
|
|
{
|
2021-05-19 00:53:52 -07:00
|
|
|
configuration = GetConfiguration();
|
|
|
|
|
2020-12-13 15:06:32 -08:00
|
|
|
canStart(&CAND1, &canConfig500);
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 23:16:47 -08:00
|
|
|
#define SWAP_UINT16(x) (((x) << 8) | ((x) >> 8))
|
|
|
|
|
2021-11-03 23:12:20 -07:00
|
|
|
void SendRusefiFormat(uint8_t idx)
|
|
|
|
{
|
2022-01-01 21:05:58 -08:00
|
|
|
auto baseAddress = 0x190 + 2 * idx;
|
2021-11-06 02:20:47 -07:00
|
|
|
auto esr = GetSensorInternalResistance();
|
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() * 10000;
|
|
|
|
frame.get().Lambda = lambda;
|
|
|
|
|
2022-01-01 16:57:52 -08:00
|
|
|
// TODO: decode temperature instead of putting ESR here
|
2021-11-06 02:20:47 -07:00
|
|
|
frame.get().TemperatureC = esr;
|
2021-11-03 23:12:20 -07:00
|
|
|
|
|
|
|
frame.get().Valid = IsRunningClosedLoop() ? 0x01 : 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
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;
|
2021-11-03 23:12:20 -07:00
|
|
|
frame.get().NernstDc = GetNernstDc() * 1000;
|
|
|
|
frame.get().PumpDuty = GetPumpOutputDuty() / 4;
|
|
|
|
frame.get().Status = GetCurrentFault();
|
2022-01-04 11:16:46 -08:00
|
|
|
frame.get().HeaterDuty = GetHeaterDuty() / 4;
|
2021-11-03 23:12:20 -07:00
|
|
|
}
|
2020-09-19 16:50:55 -07:00
|
|
|
}
|