From 12c899ddcab89ff79a40782bc82d473f90ae6bfc Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Mon, 20 Feb 2023 08:29:25 -0800 Subject: [PATCH] wideband on second can (#5108) --- firmware/console/binary/serial_can.cpp | 2 +- firmware/controllers/can/rusefi_wideband.cpp | 18 ++++++++++++------ firmware/controllers/lua/lua_hooks.cpp | 2 +- firmware/hw_layer/drivers/can/can_msg_tx.cpp | 8 +++++++- firmware/hw_layer/drivers/can/can_msg_tx.h | 4 +++- firmware/integration/rusefi_config.txt | 2 +- firmware/tunerstudio/rusefi.input | 1 + 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/firmware/console/binary/serial_can.cpp b/firmware/console/binary/serial_can.cpp index a61bc4ff7f..f3f90217b0 100644 --- a/firmware/console/binary/serial_can.cpp +++ b/firmware/console/binary/serial_can.cpp @@ -39,7 +39,7 @@ static CanTsListener listener; int CanStreamerState::sendFrame(const IsoTpFrameHeader & header, const uint8_t *data, int num, can_sysinterval_t timeout) { int dlc = 8; // standard 8 bytes - CanTxMessage txmsg(CanCategory::SERIAL, CAN_ECU_SERIAL_TX_ID, dlc, false); + CanTxMessage txmsg(CanCategory::SERIAL, CAN_ECU_SERIAL_TX_ID, dlc, 0, false); // fill the frame data according to the CAN-TP protocol (ISO 15765-2) txmsg[0] = (uint8_t)((header.frameType & 0xf) << 4); diff --git a/firmware/controllers/can/rusefi_wideband.cpp b/firmware/controllers/can/rusefi_wideband.cpp index 6b044323c8..8cec3f4ab3 100644 --- a/firmware/controllers/can/rusefi_wideband.cpp +++ b/firmware/controllers/can/rusefi_wideband.cpp @@ -25,7 +25,13 @@ bool waitAck() { return chEvtWaitAnyTimeout(EVT_BOOTLOADER_ACK, TIME_MS2I(1000)) != 0; } +static size_t getWidebandBus() { + return engineConfiguration->widebandOnSecondBus ? 1 : 0; +} + void updateWidebandFirmware() { + size_t bus = getWidebandBus(); + // Clear any pending acks for this thread chEvtGetAndClearEvents(EVT_BOOTLOADER_ACK); @@ -43,7 +49,7 @@ void updateWidebandFirmware() { for (int i = 0; i < 2; i++) { { // Send bootloader entry command - CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF0'0000, 0, true); + CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF0'0000, 0, bus, true); } if (!waitAck()) { @@ -59,7 +65,7 @@ void updateWidebandFirmware() { { // Erase flash - opcode 1, magic value 0x5A5A - CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF1'5A5A, 0, true); + CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF1'5A5A, 0, bus, true); } if (!waitAck()) { @@ -74,7 +80,7 @@ void updateWidebandFirmware() { // Send flash data 8 bytes at a time for (size_t i = 0; i < totalSize; i += 8) { { - CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF2'0000 + i, 8, true); + CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF2'0000 + i, 8, bus, true); memcpy(&m[0], build_wideband_image_bin + i, 8); } @@ -88,7 +94,7 @@ void updateWidebandFirmware() { { // Reboot to firmware! - CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF3'0000, 0, true); + CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF3'0000, 0, bus, true); } waitAck(); @@ -109,7 +115,7 @@ void setWidebandOffset(uint8_t index) { efiPrintf("Setting all connected widebands to index %d...", index); { - CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF4'0000, 1, true); + CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF4'0000, 1, getWidebandBus(), true); m[0] = index; } @@ -121,7 +127,7 @@ void setWidebandOffset(uint8_t index) { } void sendWidebandInfo() { - CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF5'0000, 2, true); + CanTxMessage m(CanCategory::WBO_SERVICE, 0xEF5'0000, 2, getWidebandBus(), true); float vbatt = Sensor::getOrZero(SensorType::BatteryVoltage) * 10; diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 05c61cba2f..09220116dd 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -174,7 +174,7 @@ static int lua_txCan(lua_State* l) { } // conform ext parameter to true/false - CanTxMessage msg(CanCategory::LUA, id, 8, ext == 0 ? false : true); + CanTxMessage msg(CanCategory::LUA, id, 8, 0, ext == 0 ? false : true); msg.busIndex = bus; // Unfortunately there is no way to inspect the length of a table, diff --git a/firmware/hw_layer/drivers/can/can_msg_tx.cpp b/firmware/hw_layer/drivers/can/can_msg_tx.cpp index e423a759cc..9493803eb5 100644 --- a/firmware/hw_layer/drivers/can/can_msg_tx.cpp +++ b/firmware/hw_layer/drivers/can/can_msg_tx.cpp @@ -22,7 +22,7 @@ } #endif // EFI_CAN_SUPPORT -CanTxMessage::CanTxMessage(CanCategory category, uint32_t eid, uint8_t dlc, bool isExtended) { +CanTxMessage::CanTxMessage(CanCategory category, uint32_t eid, uint8_t dlc, size_t bus, bool isExtended) { this->category = category; #if HAL_USE_CAN || EFI_UNIT_TEST #ifndef STM32H7XX @@ -43,6 +43,8 @@ CanTxMessage::CanTxMessage(CanCategory category, uint32_t eid, uint8_t dlc, bool setDlc(dlc); + setBus(0); + memset(m_frame.data8, 0, sizeof(m_frame.data8)); #endif // HAL_USE_CAN || EFI_UNIT_TEST } @@ -92,6 +94,10 @@ void CanTxMessage::setDlc(uint8_t dlc) { m_frame.DLC = dlc; } +void CanTxMessage::setBus(size_t bus) { + busIndex = bus; +} + void CanTxMessage::setShortValue(uint16_t value, size_t offset) { m_frame.data8[offset] = value & 0xFF; m_frame.data8[offset + 1] = value >> 8; diff --git a/firmware/hw_layer/drivers/can/can_msg_tx.h b/firmware/hw_layer/drivers/can/can_msg_tx.h index d77a0d0055..d876e11164 100644 --- a/firmware/hw_layer/drivers/can/can_msg_tx.h +++ b/firmware/hw_layer/drivers/can/can_msg_tx.h @@ -29,7 +29,7 @@ public: /** * Create a new CAN message, with the specified extended ID. */ - explicit CanTxMessage(CanCategory category, uint32_t eid, uint8_t dlc = 8, bool isExtended = false); + explicit CanTxMessage(CanCategory category, uint32_t eid, uint8_t dlc = 8, size_t bus = 0, bool isExtended = false); /** * Destruction of an instance of CanTxMessage will transmit the message over the wire. @@ -64,6 +64,8 @@ public: void setDlc(uint8_t dlc); + void setBus(size_t bus); + #if HAL_USE_CAN || EFI_UNIT_TEST const CANTxFrame *getFrame() const { return &m_frame; diff --git a/firmware/integration/rusefi_config.txt b/firmware/integration/rusefi_config.txt index c7141f79ff..ffe54fba3b 100644 --- a/firmware/integration/rusefi_config.txt +++ b/firmware/integration/rusefi_config.txt @@ -1014,7 +1014,7 @@ end_struct uint16_t tps2SecondaryMin;;"ADC", 1, 0, 0, 1000, 0 uint16_t tps2SecondaryMax;;"ADC", 1, 0, 0, 1000, 0 - bit unusedBitDisablePrimaryUart + bit widebandOnSecondBus,"2","1";Select which bus the wideband controller is attached to. bit fuelClosedLoopCorrectionEnabled;Enables lambda sensor closed loop feedback for fuelling. bit isVerboseIAC;Print details into rusEFI console\nenable verbose_idle bit boardUseTachPullUp,"12v","5v" diff --git a/firmware/tunerstudio/rusefi.input b/firmware/tunerstudio/rusefi.input index bc47177370..6a58712d44 100644 --- a/firmware/tunerstudio/rusefi.input +++ b/firmware/tunerstudio/rusefi.input @@ -4109,6 +4109,7 @@ dialog = tcuControls, "Transmission Settings" field = "!Disconnect all controllers you don't want to set!" commandButton = "Set Index 0", cmd_set_wideband_idx_0 commandButton = "Set Index 1", cmd_set_wideband_idx_1 + field = "Wideband CAN bus", widebandOnSecondBus field = "Force O2 sensor heating", forceO2Heating dialog = engineTypeDialog, "Popular vehicles"