From f32ba1a9079a8cf6b5d742a1df1e7beb1ea5dfcd Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 15 Nov 2024 22:28:23 -0500 Subject: [PATCH] lua script to turn a rusEFI into an IO-BOX for another rusEFI #7075 only:prototype --- .../controllers/lua/examples/msio-device.lua | 62 +++++++++++++++++++ .../drivers/gpio/can_gpio_msiobox.cpp | 4 +- 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 firmware/controllers/lua/examples/msio-device.lua diff --git a/firmware/controllers/lua/examples/msio-device.lua b/firmware/controllers/lua/examples/msio-device.lua new file mode 100644 index 0000000000..dc9fbd303e --- /dev/null +++ b/firmware/controllers/lua/examples/msio-device.lua @@ -0,0 +1,62 @@ +-- this script turns a rusEFI into an MSIO box + +setTickRate(300) +every50msTimer = Timer.new() + +baseId = 0x240 +isExtId = 0 + +-- MOTOROLA order, MSB (Most Significant Byte/Big Endian) comes first +function setTwoBytesMsb(data, offset, value) + value = math.floor(value) + data[offset + 1] = value >> 8 + data[offset + 2] = value & 0xff +end + +local config_packet = { 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } +local tach_packet = { 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } + +setTwoBytesMsb(config_packet, 4, 5000) +setTwoBytesMsb(config_packet, 6, 5000) + +function onPing(bus, id, dlc, data) + print('Got ping') + txCan(1, baseId + 8, isExtId, config_packet) +end + +currentState1 = 0 +currentState2 = 0 + +local frequency = 100 +startPwm(0, frequency, 0) +startPwm(1, frequency, 0) + +function onIoState(bus, id, dlc, data) + local state = data[5] + + local newState1 = ((state & 1) ~= 0) and 1 or 0 + if currentState1 ~= newState1 then + currentState1 = newState1 + print("newState1 " .. newState1) + setPwmDuty(0, currentState1) + end + + local newState2 = ((state & 2) ~= 0) and 1 or 0 + if currentState2 ~= newState2 then + currentState2 = newState2 + print("newState2 " .. newState2) + setPwmDuty(1, currentState1) + end +end + +canRxAdd(1, baseId, onPing) +canRxAdd(1, baseId + 5, onIoState) + +function onTick() + + if every50msTimer:getElapsedSeconds() > 0.05 then + every50msTimer:reset() + -- random tach packet just to show that we are alive + txCan(1, baseId + 0xB, isExtId, tach_packet) + end +end diff --git a/firmware/hw_layer/drivers/gpio/can_gpio_msiobox.cpp b/firmware/hw_layer/drivers/gpio/can_gpio_msiobox.cpp index 50e904ad37..63356e7305 100644 --- a/firmware/hw_layer/drivers/gpio/can_gpio_msiobox.cpp +++ b/firmware/hw_layer/drivers/gpio/can_gpio_msiobox.cpp @@ -155,9 +155,9 @@ static_assert(sizeof(iobox_tach) == 8); typedef enum { MSIOBOX_DISABLED = 0, MSIOBOX_WAIT_INIT, - MSIOBOX_WAIT_WHOAMI, + MSIOBOX_WAIT_WHOAMI, // 2 MSIOBOX_READY, - MSIOBOX_FAILED + MSIOBOX_FAILED // 4 } msiobox_state; class MsIoBox final : public GpioChip, public CanListener {