From fe3030bb2301cec5fc5725df14745ff3f7d208ad Mon Sep 17 00:00:00 2001 From: shadowm60 Date: Wed, 22 Apr 2020 03:48:37 +0300 Subject: [PATCH] Vss over Can implementation proposal (#1340) * Vss over Can implementation proposal Working for BMW e46 * fixed Firmware CI? * kinetis fix. * minor fixes --- firmware/controllers/algo/rusefi_enums.h | 3 +- firmware/controllers/can/can_rx.cpp | 4 + firmware/controllers/can/can_vss.cpp | 108 +++++++++++++++++++++++ firmware/controllers/can/can_vss.h | 13 +++ firmware/controllers/controllers.mk | 1 + firmware/controllers/settings.cpp | 3 - firmware/hw_layer/drivers/can/can_hw.h | 1 + firmware/hw_layer/hardware.cpp | 8 ++ firmware/hw_layer/vehicle_speed.cpp | 9 +- 9 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 firmware/controllers/can/can_vss.cpp create mode 100644 firmware/controllers/can/can_vss.h diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index cd3ffdd9bb..977c1ff249 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -597,7 +597,8 @@ typedef enum __attribute__ ((__packed__)) { } spi_device_e; typedef enum { - VVS_OOPS = 2, + BMW_e46 = 0, + W202 = 1, Force_4_bytes_size_can_vss_nbc_e = ENUM_32_BITS, } can_vss_nbc_e; diff --git a/firmware/controllers/can/can_rx.cpp b/firmware/controllers/can/can_rx.cpp index ca895752c5..b76ff4b9d9 100644 --- a/firmware/controllers/can/can_rx.cpp +++ b/firmware/controllers/can/can_rx.cpp @@ -15,6 +15,7 @@ #include "obd2.h" #include "engine.h" #include "can_sensor.h" +#include "can_vss.h" EXTERN_ENGINE; @@ -51,6 +52,9 @@ void processCanRxMessage(const CANRxFrame& frame, Logging* logger, efitick_t now serviceCanSubscribers(frame, nowNt); + //Vss is configurable, should we handle it here: + processCanRxVss(frame, nowNt); + // TODO: if/when we support multiple lambda sensors, sensor N // has address 0x0180 + N where N = [0, 15] if (frame.SID == 0x0180) { diff --git a/firmware/controllers/can/can_vss.cpp b/firmware/controllers/can/can_vss.cpp new file mode 100644 index 0000000000..b10b1167ba --- /dev/null +++ b/firmware/controllers/can/can_vss.cpp @@ -0,0 +1,108 @@ +/** + * @file can_vss.cpp + * + * This file handles incomming vss values from can. + * + * @date Apr 19, 2020 + * @author Alex Miculescu, (c) 2020 + */ + +#include "globalaccess.h" +#if EFI_CAN_SUPPORT +#include "can.h" +#include "engine_configuration.h" +#include "engine.h" +#include "vehicle_speed.h" + +EXTERN_ENGINE; + +static Logging *logger; +static bool isInit = false; +static uint16_t filterCanID = 0; +static efitick_t frameTime; +static float vssSpeed = 0; +/* keep 16 bit address since till now we do not have 28bit address request */ +uint32_t canIDs[] = { 0x01F0 /* BMW e46 ABS Message */, + 0x0200 /* W202 C180 ABS signal */ + }; + +#define look_up_can_id(X) canIDs[X] + +/* Module specitifc processing functions */ +/* source: http://z4evconversion.blogspot.com/2016/07/completely-forgot-but-it-does-live-on.html */ +void processBMW_e46(const CANRxFrame& frame) { + + uint16_t tmp; + + /* left front wheel speed is used here */ + tmp = ((frame.data8[1] & 0x0f) << 8 ); + tmp |= frame.data8[0]; + + vssSpeed = tmp / 16; + + +} + +void processW202(const CANRxFrame& frame) { + + (void)frame; +} + +/* End of specific processing functions */ + +void canVssInfo(void) { + scheduleMsg(logger, "vss using can option selected %x", CONFIG(canVssNbcType) ); +} + +void processCanRxVss(const CANRxFrame& frame, efitick_t nowNt) { + if ((CONFIG(enableCanVss)) || (!isInit)) { + return; + } + + //filter it we need to process the can message or not + if ( frame.SID != filterCanID ) { + return; + } + + frameTime = nowNt; + switch (CONFIG(canVssNbcType)){ + case BMW_e46: + processBMW_e46(frame); + break; + case W202: + processW202(frame); + break; + default: + scheduleMsg(logger, "vss unsupported can option selected %x", CONFIG(canVssNbcType) ); + break; + } + +} + +float getVehicleCanSpeed(void) { + + efitick_t nowNt = getTimeNowNt(); + + if ((nowNt - frameTime ) > NT_PER_SECOND) { + return 0; /* can timeout? */ + } else { + return vssSpeed; + } +} + +void initCanVssSupport(Logging *logger_ptr) { + + if (CONFIG(enableCanVss)) { + logger = logger_ptr; + isInit = true; + filterCanID = look_up_can_id(CONFIG(canVssNbcType)); + } + +} + +void setCanVss(int type) { + engineConfiguration->canVssNbcType = (can_vss_nbc_e)type; + canVssInfo(); +} + +#endif // EFI_CAN_SUPPORT \ No newline at end of file diff --git a/firmware/controllers/can/can_vss.h b/firmware/controllers/can/can_vss.h new file mode 100644 index 0000000000..7f80119be9 --- /dev/null +++ b/firmware/controllers/can/can_vss.h @@ -0,0 +1,13 @@ +/** + * @file can_vss.h + * + * @date Apr 19, 2020 + * @author Alex Miculescu, (c) 2020 + */ + +#pragma once + +float getVehicleCanSpeed(void); +void processCanRxVss(const CANRxFrame& frame, efitick_t nowNt); +void initCanVssSupport(Logging *logger_ptr); +void setCanVss(int type); \ No newline at end of file diff --git a/firmware/controllers/controllers.mk b/firmware/controllers/controllers.mk index a1085a2341..b4e322af7c 100644 --- a/firmware/controllers/controllers.mk +++ b/firmware/controllers/controllers.mk @@ -39,6 +39,7 @@ CONTROLLERS_SRC_CPP = \ $(CONTROLLERS_DIR)/can/can_rx.cpp \ $(CONTROLLERS_DIR)/can/can_tx.cpp \ $(CONTROLLERS_DIR)/can/can_dash.cpp \ + $(CONTROLLERS_DIR)/can/can_vss.cpp \ $(CONTROLLERS_DIR)/engine_controller.cpp \ $(CONTROLLERS_DIR)/engine_controller_misc.cpp \ $(CONTROLLERS_DIR)/persistent_store.cpp \ diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 952f0be5b8..bddc430f65 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -1269,10 +1269,7 @@ const command_i_s commandsI[] = {{"ignition_mode", setIgnitionMode}, {"bor", setBor}, #if EFI_CAN_SUPPORT {"can_mode", setCanType}, -/* - * TODO: uncomment onse we actually have setCanVss {"can_vss", setCanVss}, -*/ #endif /* EFI_CAN_SUPPORT */ #if EFI_IDLE_CONTROL {"idle_position", setIdleValvePosition}, diff --git a/firmware/hw_layer/drivers/can/can_hw.h b/firmware/hw_layer/drivers/can/can_hw.h index 459b0fef75..3d5038d191 100644 --- a/firmware/hw_layer/drivers/can/can_hw.h +++ b/firmware/hw_layer/drivers/can/can_hw.h @@ -14,6 +14,7 @@ void initCan(void); void setCanType(int type); +void setCanVss(int type); #if EFI_CAN_SUPPORT void stopCanPins(DECLARE_ENGINE_PARAMETER_SIGNATURE); diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index ed487ebe9b..cbef286ff2 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -61,6 +61,10 @@ #include "flash_main.h" #endif +#if EFI_CAN_SUPPORT +#include "can_vss.h" +#endif + EXTERN_ENGINE; static mutex_t spiMtx; @@ -573,6 +577,10 @@ void initHardware(Logging *l) { initVehicleSpeed(sharedLogger); #endif +#if EFI_CAN_SUPPORT + initCanVssSupport(sharedLogger); +#endif + #if EFI_CDM_INTEGRATION cdmIonInit(); #endif diff --git a/firmware/hw_layer/vehicle_speed.cpp b/firmware/hw_layer/vehicle_speed.cpp index 9e9aabb136..580cdda4f7 100644 --- a/firmware/hw_layer/vehicle_speed.cpp +++ b/firmware/hw_layer/vehicle_speed.cpp @@ -12,6 +12,7 @@ #include "engine.h" #include "digital_input_icu.h" #include "pin_repository.h" +#include "can_vss.h" EXTERN_ENGINE; @@ -33,6 +34,11 @@ void setMockVehicleSpeed(float speedKPH) { float getVehicleSpeed(void) { if (mockVehicleSpeed != DEFAULT_MOCK_SPEED) return mockVehicleSpeed; +#if EFI_CAN_SUPPORT + if ( CONFIG(enableCanVss) == true ) { + return getVehicleCanSpeed(); + } +#endif /* EFI_CAN_SUPPORT */ if (!hasVehicleSpeedSensor()) return 0; efitick_t nowNt = getTimeNowNt(); @@ -86,7 +92,8 @@ void initVehicleSpeed(Logging *l) { #else /* EFI_VEHICLE_SPEED */ float getVehicleSpeed(void) { + // no VSS support - return 0; + return 0; } #endif /* EFI_VEHICLE_SPEED */