Skip to content

Commit

Permalink
CAN: protocol configuration to settings
Browse files Browse the repository at this point in the history
(cherry picked from commit 0fa06cf)
  • Loading branch information
dron0gus authored and rusefillc committed Feb 4, 2025
1 parent 0c169da commit ee5c4cc
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 18 deletions.
16 changes: 8 additions & 8 deletions firmware/boards/f0_module/port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ Configuration* GetConfiguration()

// See https://github.com/mck1117/wideband/issues/11 to explain this madness
switch (3 * sel1 + sel2) {
case 0: config.CanIndexOffset = 2; break;
case 1: config.CanIndexOffset = 0; break;
case 2: config.CanIndexOffset = 3; break;
case 3: config.CanIndexOffset = 4; break;
case 0: config.afr[0].RusEfiIdOffset = 2; break;
case 1: config.afr[0].RusEfiIdOffset = 0; break;
case 2: config.afr[0].RusEfiIdOffset = 3; break;
case 3: config.afr[0].RusEfiIdOffset = 4; break;
case 4: /* both floating, do nothing */ break;
case 5: config.CanIndexOffset = 1; break;
case 6: config.CanIndexOffset = 5; break;
case 7: config.CanIndexOffset = 6; break;
case 8: config.CanIndexOffset = 7; break;
case 5: config.afr[0].RusEfiIdOffset = 1; break;
case 6: config.afr[0].RusEfiIdOffset = 5; break;
case 7: config.afr[0].RusEfiIdOffset = 6; break;
case 8: config.afr[0].RusEfiIdOffset = 7; break;
default: break;
}

Expand Down
26 changes: 25 additions & 1 deletion firmware/boards/f1_common/f1_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void Configuration::LoadDefaults()
{
int i;

CanIndexOffset = 0;
*this = {};

NoLongerUsed0 = 0;
sensorType = BOARD_DEFAULT_SENSOR_TYPE;

/* default auxout curve is 0..5V for AFR 8.5 to 18.0
Expand All @@ -67,6 +69,28 @@ void Configuration::LoadDefaults()
auxOutputSource[0] = AuxOutputMode::Afr0;
auxOutputSource[1] = AuxOutputMode::Afr1;

for (i = 0; i < AFR_CHANNELS; i++) {
// enable RusEFI protocol
afr[i].RusEfiTx = true;
afr[i].RusEfiTxDiag = true;
afr[i].RusEfiIdOffset = 2 * i;

// Disable AemNet
afr[i].AemNetTx = false;
afr[i].AemNetIdOffset = i;
}

for (i = 0; i < EGT_CHANNELS; i++) {
// disable RusEFI protocol - not implemented
egt[i].RusEfiTx = false;
egt[i].RusEfiTxDiag = false;
egt[i].RusEfiIdOffset = i;

// Enable AemNet
egt[i].AemNetTx = true;
egt[i].AemNetIdOffset = i;
}

/* Finaly */
Tag = ExpectedTag;
}
Expand Down
24 changes: 23 additions & 1 deletion firmware/boards/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,35 @@ class Configuration {
// Actual configuration data
union {
struct {
uint8_t CanIndexOffset = 0;
uint8_t NoLongerUsed0 = 0;
// AUX0 and AUX1 curves
float auxOutBins[2][8];
float auxOutValues[2][8];
AuxOutputMode auxOutputSource[2];

SensorType sensorType;

// per AFR channel settings
struct {
bool RusEfiTx:1;
bool RusEfiTxDiag:1;
bool AemNetTx:1;

uint8_t RusEfiIdOffset;
uint8_t AemNetIdOffset;
uint8_t pad[5];
} afr[2];

// per EGT channel settings
struct {
bool RusEfiTx:1;
bool RusEfiTxDiag:1;
bool AemNetTx:1;

uint8_t RusEfiIdOffset;
uint8_t AemNetIdOffset;
uint8_t pad[5];
} egt[2];
} __attribute__((packed));

// pad to 256 bytes including tag
Expand Down
18 changes: 12 additions & 6 deletions firmware/can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void CanRxThread(void*)
else if ((frame.DLC == 0 || frame.DLC == 1) && CAN_ID(frame) == WB_BL_ENTER)
{
// If 0xFF (force update all) or our ID, reset to bootloader, otherwise ignore
if (frame.DLC == 0 || frame.data8[0] == 0xFF || frame.data8[0] == GetConfiguration()->CanIndexOffset)
if (frame.DLC == 0 || frame.data8[0] == 0xFF || frame.data8[0] == GetConfiguration()->afr[0].RusEfiIdOffset)
{
SendAck();

Expand All @@ -119,8 +119,14 @@ void CanRxThread(void*)
// Check if it's an "index set" message
else if (frame.DLC == 1 && CAN_ID(frame) == WB_MSG_SET_INDEX)
{
int offset = frame.data8[0];
configuration = GetConfiguration();
configuration->CanIndexOffset = frame.data8[0];
for (int i = 0; i < AFR_CHANNELS; i++) {
configuration->afr[i].RusEfiIdOffset = offset + i * 2;
}
for (int i = 0; i < EGT_CHANNELS; i++) {
configuration->egt[i].RusEfiIdOffset = offset + i;
}
SetConfiguration();
SendAck();
}
Expand Down Expand Up @@ -148,7 +154,7 @@ void InitCan()

void SendRusefiFormat(uint8_t ch)
{
auto baseAddress = WB_DATA_BASE_ADDR + 2 * (ch + configuration->CanIndexOffset);
auto baseAddress = WB_DATA_BASE_ADDR + configuration->afr[ch].RusEfiIdOffset;

const auto& sampler = GetSampler(ch);
const auto& heater = GetHeaterController(ch);
Expand All @@ -166,7 +172,7 @@ void SendRusefiFormat(uint8_t ch)
pumpDuty > 0.1f && pumpDuty < 0.9f &&
lambda > 0.6f;

{
if (configuration->afr[ch].RusEfiTx) {
CanTxTyped<wbo::StandardData> frame(baseAddress + 0);

// The same header is imported by the ECU and checked against this data in the frame
Expand All @@ -179,8 +185,8 @@ void SendRusefiFormat(uint8_t ch)
frame.get().Valid = (heaterClosedLoop && lambdaValid) ? 0x01 : 0x00;
}

{
CanTxTyped<wbo::DiagData> frame(baseAddress + 1);
if (configuration->afr[ch].RusEfiTxDiag) {
CanTxTyped<wbo::DiagData> frame(baseAddress + 1);;

frame.get().Esr = sampler.GetSensorInternalResistance();
frame.get().NernstDc = nernstDc * 1000;
Expand Down
1 change: 1 addition & 0 deletions firmware/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ float GetRemoteBatteryVoltage();
// implement this for your board if you want some non-standard behavior
// default implementation simply calls SendRusefiFormat
void SendCanForChannel(uint8_t ch);
void SendCanEgtForChannel(uint8_t ch);

// Helpers to support both bxCAN and CANFD peripherals
#ifdef STM32G4XX
Expand Down
70 changes: 68 additions & 2 deletions firmware/ini/wideband_dual.ini
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,35 @@ Aux0InputSel = bits, U08, 133, [0:3], "AFR 0", "AFR 1", "Lambda 0", "L
Aux1InputSel = bits, U08, 134, [0:3], "AFR 0", "AFR 1", "Lambda 0", "Lambda 1", "EGT 0", "EGT 1"
LsuSensorType = bits, U08, 135, [0:2], "LSU 4.9", "LSU 4.2", "LSU ADV", "INVALID", "INVALID", "INVALID", "INVALID", "INVALID"

RusEfiTx0 = bits, U08, 136, [0:0], "Disable", "Enable"
RusEfiTxDiag0 = bits, U08, 136, [1:1], "Disable", "Enable"
AemNetTx0 = bits, U08, 136, [2:2], "Disable", "Enable"
RusEfiIdOffset0= scalar, U08, 137, "", 1, 0, 0, 255, 0
AemNetIdOffset0= scalar, U08, 138, "", 1, 0, 0, 255, 0

RusEfiTx1 = bits, U08, 144, [0:0], "Disable", "Enable"
RusEfiTxDiag1 = bits, U08, 144, [1:1], "Disable", "Enable"
AemNetTx1 = bits, U08, 144, [2:2], "Disable", "Enable"
RusEfiIdOffset1= scalar, U08, 145, "", 1, 0, 0, 255, 0
AemNetIdOffset1= scalar, U08, 146, "", 1, 0, 0, 255, 0

AemNetEgtTx0 = bits, U08, 152, [2:2], "Disable", "Enable"
AemNetIdEgOff0 = scalar, U08, 154, "", 1, 0, 0, 255, 0

AemNetEgtTx1 = bits, U08, 160, [2:2], "Disable", "Enable"
AemNetIdEgOff1 = scalar, U08, 162, "", 1, 0, 0, 255, 0

page = 2 ; this is a RAM only page with no burnable flash
; name = class, type, offset, [shape], units, scale, translate, min, max, digits
highSpeedOffsets = array, U16, 0, [32], "", 1, 0, 0, 65535, 0, noMsqSave

[SettingContextHelp]
RusEfiIdOffset0 = "Defines CAN ID offset for RusEFI AFR format packet channel 0 (left). Data packet ID = (0x190 + this offset), Diagnostic packed ID = (0x190 + 1 + this offset)."
RusEfiIdOffset1 = "Defines CAN ID offset for RusEFI AFR format packet channel 1 (right). Data packet ID = (0x190 + this offset), Diagnostic packed ID = (0x190 + 1 + this offset)."
AemNetIdOffset0 = "Defines CAN ID offset for AemNET USEGO format packet channel 0 (left). Packet ID = (0x180 + this offset)."
AemNetIdOffset1 = "Defines CAN ID offset for AemNET USEGO format packet channel 1 (right). Packet ID = (0x180 + this offset)."
AemNetIdEgOff0 = "Defines CAN ID offset for AemNET EGT format packet channel 0 (left). Packed ID = (0xA0305 + this offset)."
AemNetIdEgOff1 = "Defines CAN ID offset for AemNET EGT format packet channel 1 (right). Packed ID = (0xA0305 + this offset)."

[Tuning]

Expand Down Expand Up @@ -297,7 +321,8 @@ entry = EGT1_commErrors, "EGT 1: comm errors", int, "%d"
menuDialog = main
menu = "&Settings"
subMenu = sensor_settings, "Sensor settings"
subMenu = can_settings, "CAN settings"
subMenu = can_settings, "CAN AFR settings"
subMenu = can_egt_settings, "CAN EGT settings"

menu = "Outputs"
subMenu = auxOut0, "AUX analog output 0"
Expand Down Expand Up @@ -328,9 +353,50 @@ cmd_openblt = "Z\x00\xbc\x00\x00"
dialog = sensor_settings, "Sensor Settings"
field = "Sensor Type", LsuSensorType

dialog = can_settings, "CAN Settings"
dialog = afr0_can_settings, "AFR 0 (left) channel CAN Settings"
field = "RusEFI protocol:"
field = "Output AFR", RusEfiTx0
field = "Output AFR diagnostic", RusEfiTxDiag0
field = "CAN ID offset (base ID is 0x190) multiple of two", RusEfiIdOffset0, { (RusEfiTx0 == 1) || (RusEfiTxDiag0 == 1)}
field = "AemNet protocol:"
field = "Output AFR", AemNetTx0
field = "CAN ID offset (base ID is 0x180)", AemNetIdOffset0, { (AemNetTx0 == 1) }

dialog = afr1_can_settings, "AFR 1 (right) channel CAN Settings"
field = "RusEFI protocol:"
field = "Output AFR", RusEfiTx1
field = "Output AFR diagnostic", RusEfiTxDiag1
field = "CAN ID offset (base ID is 0x190) multiple of two", RusEfiIdOffset1, { (RusEfiTx1 == 1) || (RusEfiTxDiag1 == 1)}
field = "AemNet protocol:"
field = "Output AFR", AemNetTx1
field = "CAN ID offset (base ID is 0x180)", AemNetIdOffset1, { (AemNetTx1 == 1) }

dialog = egt0_can_settings, "EGT 0 (left) channel CAN Settings"
field = "RusEFI protocol:"
field = "not implemented yet"
field = "AemNet protocol:"
field = "Output EGT", AemNetEgtTx0
field = "CAN ID offset (base ID is 0xA0305)", AemNetIdEgOff0, { (AemNetEgtTx0 == 1) }, { 1 }, displayInHex

dialog = egt1_can_settings, "EGT 1 (right) channel CAN Settings"
field = "RusEFI protocol:"
field = "not implemented yet"
field = "AemNet protocol:"
field = "Output EGT", AemNetEgtTx1
field = "CAN ID offset (base ID is 0xA0305)", AemNetIdEgOff1, { (AemNetEgtTx1 == 1) }, { 1 }, displayInHex

dialog = common_can_settings, "CAN Settings"
field = "CAN message ID offset", CanIndexOffset

dialog = can_settings, "CAN AFR Settings", border
panel = common_can_settings, North
panel = afr0_can_settings, West
panel = afr1_can_settings, East

dialog = can_egt_settings, "CAN AFR Settings", border
panel = egt0_can_settings, West
panel = egt1_can_settings, East

dialog = auxOut0, "AUX analog out 0 Settings"
field = "Signal", Aux0InputSel
panel = auxOut0Curve
Expand Down

0 comments on commit ee5c4cc

Please sign in to comment.