rusefi/firmware/config/engines/bmw_n73.cpp

273 lines
7.4 KiB
C++
Raw Normal View History

2021-10-02 10:25:40 -07:00
/*
* bmw_n73.cpp
*
* @date Oct 2, 2021
* @author Andrey Belomutskiy, (c) 2012-2021
*/
#include "bmw_n73.h"
void setEngineProteusBMW_N73_GDI() {
2021-10-02 10:25:40 -07:00
}
2022-02-09 14:28:57 -08:00
/**
* set engine_type 9
*/
2021-12-23 13:59:30 -08:00
void setEngineProteusGearboxManInTheMiddle() {
strncpy(config->luaScript, R"(
2022-02-12 21:09:00 -08:00
function getTwoBytes(data, offset, factor)
2022-02-09 21:53:27 -08:00
return (data[offset + 2] * 256 + data[offset + 1]) * factor
2021-12-24 14:35:27 -08:00
end
2022-02-12 21:09:00 -08:00
function setTwoBytes(data, offset, value)
data[offset + 1] = value % 255
data[offset + 2] = (value >> 8) % 255
end
2022-02-21 09:35:32 -08:00
function getBitRange(data, bitIndex, bitWidth)
byteIndex = bitIndex >> 3
shift = bitIndex - byteIndex * 8
value = data[1 + byteIndex]
if (shift + bitIndex > 8) then
value = value + data[2 + byteIndex] * 256
end
mask = (1 << bitWidth) - 1
return (value >> shift) & mask
end
2022-02-19 17:38:08 -08:00
function bmwChecksum(canID, data, offset, length)
checksum = canID
for i = offset, offset + length - 1,1
do
checksum = checksum + data[i]
end
checksum = (checksum >> 8) + (checksum & 0xff)
return checksum
end
2022-02-10 17:00:52 -08:00
-- this controls onCanRx rate as well!
setTickRate(300)
2022-02-04 13:39:26 -08:00
--
-- crazy copy-pasta, at some point move to sets/bags
2022-02-19 17:38:08 -08:00
ignitionKeyByte0 = 256
2022-02-09 21:53:27 -08:00
tcuMessages = { }
2022-02-04 13:39:26 -08:00
-- 'ecu' means not-TCU
2022-02-09 21:53:27 -08:00
ecuMessages = { }
2022-02-04 13:39:26 -08:00
2021-12-24 14:35:27 -08:00
CAN_BMW_E90_TORQUE_1 = 0x0A8
CAN_BMW_E90_TORQUE_2 = 0x0A9
2022-02-22 17:42:48 -08:00
counterCAN_BMW_E90_RPM_THROTTLE = 0
2021-12-23 22:40:31 -08:00
CAN_BMW_E90_RPM_THROTTLE = 0x0AA
2022-02-04 13:39:26 -08:00
CAN_BMW_E90_DSC_TORQUE_DEMAND = 0x0B6
CAN_BMW_E90_WHEEL_SPEED = 0x0CE
2021-12-23 22:40:31 -08:00
CAN_BMW_E90_IGNITION_KEY = 0x130
CAN_BMW_E65_GEAR_SELECTOR = 0x192
2022-02-04 13:39:26 -08:00
CAN_BMW_E90_DSC_STATUS = 0x19E
CAN_BMW_E90_DSC_SPEED = 0x1A0
2021-12-23 22:40:31 -08:00
CAN_BMW_E90_COOLANT = 0x1D0
2022-02-05 21:43:29 -08:00
-- 1E1 klima?
2021-12-28 21:50:17 -08:00
CAN_BMW_E90_LOCKING = 0x2FC
2022-02-09 20:56:43 -08:00
CAN_BMW_E90_MSA = 0x308
2022-02-05 21:43:29 -08:00
-- 310 outside temperature Kombi
-- 330 fuel, range
2021-12-23 22:40:31 -08:00
CAN_BMW_E90_DASH_ON = 0x332
2022-02-05 21:43:29 -08:00
-- 34F
-- 0x3EF OBD Daten Motor ECU
2022-02-09 20:56:43 -08:00
CAN_BMW_E90_ECU_NETWORK = 0x492
2022-02-05 21:43:29 -08:00
-- CAN_BMW_E90_ECU2_NETWORK = 0x493
2022-02-21 14:34:21 -08:00
CAN_BMW_E90_INPA_TCU = 0x618
2021-12-23 22:40:31 -08:00
CAN_BMW_GEAR_TORQUE_DEMAND2 = 0x0B5
2021-12-23 20:24:36 -08:00
CAN_BMW_GEAR_TRANSMISSION_DATA = 0x0BA
2021-12-23 22:40:31 -08:00
CAN_BMW_GEAR_GEARBOX_DATA_2 = 0x1A2
2021-12-23 20:24:36 -08:00
CAN_BMW_GEAR_TRANSMISSION_DISP = 0x1D2
2021-12-23 22:40:31 -08:00
CAN_BMW_GEAR_GANG_STATUS = 0x304
CAN_BMW_GEAR_NETWORK = 0x498
CAN_BMW_GEAR_SERVICE = 0x598
2022-02-21 14:34:21 -08:00
CAN_BMW_GEAR_INPA_RESPONSE = 0x6f1
2021-12-23 13:59:30 -08:00
ECU_BUS = 1
GEAR_BUS = 2
2022-02-04 13:39:26 -08:00
canRxAdd(CAN_BMW_E90_TORQUE_1)
canRxAdd(CAN_BMW_E90_TORQUE_2)
2021-12-23 13:59:30 -08:00
canRxAdd(CAN_BMW_E90_RPM_THROTTLE)
2022-02-04 13:39:26 -08:00
canRxAdd(CAN_BMW_E90_DSC_TORQUE_DEMAND)
canRxAdd(CAN_BMW_E90_WHEEL_SPEED)
2021-12-23 13:59:30 -08:00
canRxAdd(CAN_BMW_E90_IGNITION_KEY)
canRxAdd(CAN_BMW_E65_GEAR_SELECTOR)
2022-02-04 13:39:26 -08:00
canRxAdd(CAN_BMW_E90_DSC_STATUS)
canRxAdd(CAN_BMW_E90_DSC_SPEED)
2021-12-23 13:59:30 -08:00
canRxAdd(CAN_BMW_E90_COOLANT)
2021-12-28 21:50:17 -08:00
canRxAdd(CAN_BMW_E90_LOCKING)
2022-02-09 20:56:43 -08:00
canRxAdd(CAN_BMW_E90_MSA)
2021-12-23 22:40:31 -08:00
canRxAdd(CAN_BMW_E90_DASH_ON)
2022-02-09 20:56:43 -08:00
canRxAdd(CAN_BMW_E90_ECU_NETWORK)
2022-02-21 14:34:21 -08:00
canRxAdd(CAN_BMW_E90_INPA_TCU)
2021-12-23 22:40:31 -08:00
canRxAdd(CAN_BMW_GEAR_TORQUE_DEMAND2)
canRxAdd(CAN_BMW_GEAR_TRANSMISSION_DATA)
canRxAdd(CAN_BMW_GEAR_GEARBOX_DATA_2)
2021-12-23 20:24:36 -08:00
canRxAdd(CAN_BMW_GEAR_TRANSMISSION_DISP)
canRxAdd(CAN_BMW_GEAR_GANG_STATUS)
2021-12-23 22:40:31 -08:00
canRxAdd(CAN_BMW_GEAR_NETWORK)
canRxAdd(CAN_BMW_GEAR_SERVICE)
2022-02-21 14:34:21 -08:00
canRxAdd(CAN_BMW_GEAR_INPA_RESPONSE)
2021-12-23 22:40:31 -08:00
2021-12-23 13:59:30 -08:00
2021-12-28 21:50:17 -08:00
function relayToTcu(id, data)
txCan(GEAR_BUS, id, 0, data) -- relay non-TCU message to TCU
end
2022-02-04 13:39:26 -08:00
function relayToEcu(id, data)
txCan(ECU_BUS, id, 0, data) -- relay non-ECU message to ECU
end
2021-12-28 21:50:17 -08:00
function printDebug(msg)
2022-02-09 21:53:27 -08:00
print(msg)
2021-12-28 21:50:17 -08:00
end
2022-02-22 19:20:03 -08:00
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
function decimalToHex(num)
if num == 0 then
return '0'
end
local result = ""
while num > 0 do
local n = num % 16
result = hexstr[n + 1] ..result
num = math.floor(num / 16)
end
return result
end
function print_array(arr)
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..decimalToHex(arr[index])
index = index + 1
end
return str
end
2021-12-23 20:09:52 -08:00
function onCanRx(bus, id, dlc, data)
id = id % 2048
-- local output = string.format("%x", id)
2022-02-04 13:39:26 -08:00
if id == CAN_BMW_E90_TORQUE_1 then
2022-02-12 21:09:00 -08:00
TORQ_AVL = 0.5 * (getTwoBytes(data, 1, 1) >> 4)
TORQ_AVL_DMEE = 0.5 * (getTwoBytes(data, 3, 1) >> 4)
2022-02-09 21:53:27 -08:00
print('CAN_BMW_E90_TORQUE_1 TORQ_AVL=' ..TORQ_AVL ..' TORQ_AVL_DMEE=' ..TORQ_AVL_DMEE)
2021-12-28 21:50:17 -08:00
relayToTcu(id, data)
2022-02-09 21:53:27 -08:00
elseif id == CAN_BMW_E90_TORQUE_2 then
2021-12-28 21:50:17 -08:00
printDebug('CAN_BMW_E90_TORQUE_2')
relayToTcu(id, data)
2022-02-04 13:39:26 -08:00
elseif id == CAN_BMW_E90_RPM_THROTTLE then
2022-02-12 21:09:00 -08:00
rpm = getTwoBytes(data, 4, 0.25)
2022-02-22 17:42:48 -08:00
pedal = data [1 + 3] * 100 / 256.0
print('CAN_BMW_E90_RPM_THROTTLE rpm=' .. rpm .. ' pedal=' .. pedal)
output = {0, 0, 0, data [1 + 3], data [1 + 4], data [1 + 5], 0, 0}
2022-02-22 20:13:32 -08:00
counterCAN_BMW_E90_RPM_THROTTLE = (counterCAN_BMW_E90_RPM_THROTTLE + 1) % 15
output[2] = counterCAN_BMW_E90_RPM_THROTTLE
output[1] = bmwChecksum(CAN_BMW_E90_RPM_THROTTLE, output, 2, 7)
2022-02-22 17:42:48 -08:00
2021-12-28 21:50:17 -08:00
relayToTcu(id, data)
2022-02-09 21:53:27 -08:00
elseif id == CAN_BMW_E90_DSC_TORQUE_DEMAND then
2022-02-04 13:39:26 -08:00
printDebug('CAN_BMW_E90_DSC_TORQUE_DEMAND')
2021-12-28 21:50:17 -08:00
relayToTcu(id, data)
2022-02-09 21:53:27 -08:00
elseif id == CAN_BMW_E90_WHEEL_SPEED then
2022-02-04 13:39:26 -08:00
printDebug('CAN_BMW_E90_WHEEL_SPEED')
2021-12-28 21:50:17 -08:00
relayToTcu(id, data)
2022-02-09 21:53:27 -08:00
elseif id == CAN_BMW_E90_IGNITION_KEY then
2022-02-22 17:42:48 -08:00
if ignitionKeyByte0 ~= data[1] then
2022-02-19 17:38:08 -08:00
ignitionKeyByte0 = data[1]
2022-02-22 17:42:48 -08:00
if ignitionKeyByte0 == 0x55 then
printDebug('!!!!!!!!!!!!! CAN_BMW_E90_IGNITION_KEY CRANKING')
else
2022-02-19 17:38:08 -08:00
printDebug('!!!!!!!!!!!!! CAN_BMW_E90_IGNITION_KEY ' .. ignitionKeyByte0)
end
end
2022-02-09 21:53:27 -08:00
relayToTcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_E65_GEAR_SELECTOR then
2021-12-28 21:50:17 -08:00
printDebug('CAN_BMW_E65_GEAR_SELECTOR')
relayToTcu(id, data)
2022-02-04 13:39:26 -08:00
elseif id == CAN_BMW_E90_DSC_STATUS then
printDebug('CAN_BMW_E90_DSC_STATUS')
relayToTcu(id, data)
elseif id == CAN_BMW_E90_DSC_SPEED then
printDebug('CAN_BMW_E90_DSC_SPEED')
2021-12-28 21:50:17 -08:00
relayToTcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_E90_COOLANT then
2021-12-28 21:50:17 -08:00
printDebug('CAN_BMW_E90_COOLANT')
2022-02-20 06:53:58 -08:00
cltByte = data[1 + 0]
mapByte = data[1 + 3]
fuelWord = getTwoBytes(data, 4, 1)
2021-12-28 21:50:17 -08:00
relayToTcu(id, data)
elseif id == CAN_BMW_E90_LOCKING then
printDebug('CAN_BMW_E90_LOCKING')
relayToTcu(id, data)
2022-02-09 20:56:43 -08:00
elseif id == CAN_BMW_E90_MSA then
printDebug('CAN_BMW_E90_MSA')
relayToTcu(id, data)
2022-02-09 21:53:27 -08:00
elseif id == CAN_BMW_E90_DASH_ON then
2022-02-04 13:39:26 -08:00
printDebug('CAN_BMW_E90_DASH_ON')
relayToTcu(id, data)
2022-02-09 21:53:27 -08:00
elseif id == CAN_BMW_E90_ECU_NETWORK then
2022-02-09 20:56:43 -08:00
printDebug('CAN_BMW_E90_ECU_NETWORK')
relayToTcu(id, data)
2022-02-21 14:34:21 -08:00
elseif id == CAN_BMW_E90_INPA_TCU then
print('CAN_BMW_E90_INPA_TCU')
relayToTcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_GEAR_TORQUE_DEMAND2 then
2022-02-21 09:35:32 -08:00
TORQ_TAR_EGS = getBitRange(data, 12, 12)
ST_TORQ_TAR_EGS = getBitRange(data, 36, 2)
printDebug('*******CAN_BMW_GEAR_TORQUE_DEMAND2 ' .. TORQ_TAR_EGS .. ' ' .. ST_TORQ_TAR_EGS)
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_GEAR_TRANSMISSION_DATA then
2021-12-28 21:50:17 -08:00
printDebug('*******CAN_BMW_GEAR_TRANSMISSION_DATA')
2022-02-19 17:38:08 -08:00
gearBits = data[1] & 0xF
2022-02-22 17:42:48 -08:00
if (gearBits >= 5 and gearBits <= 0xA) then
2022-02-19 17:38:08 -08:00
printDebug('*******CAN_BMW_GEAR_TRANSMISSION_DATA gear ' .. (gearBits - 4))
end
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
elseif id == CAN_BMW_GEAR_GEARBOX_DATA_2 then
2022-02-09 21:53:27 -08:00
printDebug('*******CAN_BMW_GEAR_GEARBOX_DATA_2')
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_GEAR_TRANSMISSION_DISP then
2021-12-28 21:50:17 -08:00
printDebug('*******CAN_BMW_GEAR_TRANSMISSION_DISP')
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_GEAR_GANG_STATUS then
2021-12-28 21:50:17 -08:00
printDebug('*******CAN_BMW_GEAR_GANG_STATUS')
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_GEAR_NETWORK then
2022-02-09 21:53:27 -08:00
printDebug('*******CAN_BMW_GEAR_NETWORK')
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
2021-12-23 22:40:31 -08:00
elseif id == CAN_BMW_GEAR_SERVICE then
2022-02-09 21:53:27 -08:00
printDebug('*******CAN_BMW_GEAR_SERVICE')
2022-02-04 13:39:26 -08:00
relayToEcu(id, data)
2022-02-21 14:34:21 -08:00
elseif id == CAN_BMW_GEAR_INPA_RESPONSE then
print('*******CAN_BMW_GEAR_INPA_RESPONSE')
relayToEcu(id, data)
2021-12-23 20:09:52 -08:00
else
2022-02-09 21:53:27 -08:00
print('No handler for ' ..id)
2021-12-23 20:09:52 -08:00
end
2022-02-09 21:53:27 -08:00
-- printDebug('got CAN id=' ..id ..' dlc=' ..dlc)
2021-12-23 22:40:31 -08:00
end
2021-12-23 20:09:52 -08:00
2021-12-23 13:59:30 -08:00
function onTick()
end
2021-12-23 22:40:31 -08:00
2021-12-23 13:59:30 -08:00
)", efi::size(config->luaScript));
}