From 8646d10067ea303d845f53920a8bca537fab668f Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Sun, 11 Jun 2023 15:51:41 -0400 Subject: [PATCH] OpenBLT reset functionality (#259) * ts: support reset and reset to OpenBLT commands (cherry picked from commit 2f2fca401d7dc9a29e806013efec4d41570dd613) * f1_xxx: ini: reset commands (cherry picked from commit 628994ba0a70901edb9b59360646aa195b3307cf) * typo * f1_xxx: ini: update signature (cherry picked from commit 7aa57fe09c6c684855838e36c9b65d8ae3618a7a) --------- Co-authored-by: Andrey Gusakov --- firmware/boards/f1_common/f1_port.cpp | 17 +++ .../boards/f1_dual/wideband_board_config.h | 2 +- .../f1_dual_rev1/wideband_board_config.h | 2 +- .../boards/f1_rev2/wideband_board_config.h | 2 +- .../boards/f1_rev3/wideband_board_config.h | 2 +- firmware/boards/port.h | 3 + firmware/console/binary/tunerstudio.cpp | 111 +++++++++++++----- firmware/console/binary/tunerstudio.h | 6 + firmware/console/binary/tunerstudio_io.h | 1 + firmware/ini/wideband_dual.ini | 17 ++- firmware/ini/wideband_f1.ini | 14 ++- 11 files changed, 137 insertions(+), 40 deletions(-) diff --git a/firmware/boards/f1_common/f1_port.cpp b/firmware/boards/f1_common/f1_port.cpp index fd30b4f..f91fa76 100644 --- a/firmware/boards/f1_common/f1_port.cpp +++ b/firmware/boards/f1_common/f1_port.cpp @@ -125,6 +125,23 @@ SensorType GetSensorType() return cfg.sensorType; } +void rebootNow() +{ + NVIC_SystemReset(); +} + +void rebootToOpenblt() +{ +#if USE_OPENBLT + /* safe to call on already inited shares area */ + SharedParamsInit(); + /* Store flag to stay in OpenBLT */ + SharedParamsWriteByIndex(0, 0x01); + + rebootNow(); +#endif +} + void ToggleESRDriver(SensorType sensor) { switch (sensor) { diff --git a/firmware/boards/f1_dual/wideband_board_config.h b/firmware/boards/f1_dual/wideband_board_config.h index c3e5392..ec3f3dc 100644 --- a/firmware/boards/f1_dual/wideband_board_config.h +++ b/firmware/boards/f1_dual/wideband_board_config.h @@ -1,7 +1,7 @@ #pragma once // TS settings -#define TS_SIGNATURE "rusEFI 2023.03.23.wideband_dual" +#define TS_SIGNATURE "rusEFI 2023.05.10.wideband_dual" // This board implements two channels #define AFR_CHANNELS 2 diff --git a/firmware/boards/f1_dual_rev1/wideband_board_config.h b/firmware/boards/f1_dual_rev1/wideband_board_config.h index 518017f..c3325d1 100644 --- a/firmware/boards/f1_dual_rev1/wideband_board_config.h +++ b/firmware/boards/f1_dual_rev1/wideband_board_config.h @@ -1,7 +1,7 @@ #pragma once // TS settings -#define TS_SIGNATURE "rusEFI 2023.03.23.wideband_dual" +#define TS_SIGNATURE "rusEFI 2023.05.10.wideband_dual" // This board implements two channels #define AFR_CHANNELS 2 diff --git a/firmware/boards/f1_rev2/wideband_board_config.h b/firmware/boards/f1_rev2/wideband_board_config.h index f274009..9937b32 100644 --- a/firmware/boards/f1_rev2/wideband_board_config.h +++ b/firmware/boards/f1_rev2/wideband_board_config.h @@ -1,7 +1,7 @@ #pragma once // TS settings -#define TS_SIGNATURE "rusEFI 2023.03.23.wideband_f1" +#define TS_SIGNATURE "rusEFI 2023.05.10.wideband_f1" // Fundamental board constants #define VCC_VOLTS (3.3f) diff --git a/firmware/boards/f1_rev3/wideband_board_config.h b/firmware/boards/f1_rev3/wideband_board_config.h index f274009..9937b32 100644 --- a/firmware/boards/f1_rev3/wideband_board_config.h +++ b/firmware/boards/f1_rev3/wideband_board_config.h @@ -1,7 +1,7 @@ #pragma once // TS settings -#define TS_SIGNATURE "rusEFI 2023.03.23.wideband_f1" +#define TS_SIGNATURE "rusEFI 2023.05.10.wideband_f1" // Fundamental board constants #define VCC_VOLTS (3.3f) diff --git a/firmware/boards/port.h b/firmware/boards/port.h index ebfe778..22d4530 100644 --- a/firmware/boards/port.h +++ b/firmware/boards/port.h @@ -74,6 +74,9 @@ size_t GetConfigurationSize(); int SaveConfiguration(); const char *getTsSignature(); +void rebootNow(); +void rebootToOpenblt(); + // LSU4.2, LSU4.9 or LSU_ADV SensorType GetSensorType(); void SetupESRDriver(SensorType sensor); diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index f6f8f6f..61909e3 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -204,13 +204,41 @@ static void handleBurnCommand(TsChannelBase* tsChannel, ts_response_format_e mod sendResponseCode(mode, tsChannel, TS_RESPONSE_BURN_OK); } +static void handleIoTestCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t subsystem, uint16_t /* index */) { + /* index is not used yet */ + + switch (subsystem) { +#if 0 + /* DFU */ + case 0xba: + jump_to_bootloader(); + break; +#endif + + case 0xbb: + rebootNow(); + break; + +#if USE_OPENBLT + case 0xbc: + /* Jump to OpenBLT if present */ + rebootToOpenblt(); + break; +#endif + + default: + tunerStudioError(tsChannel, "Unexpected IoTest command"); + } +} + static bool isKnownCommand(char command) { return command == TS_HELLO_COMMAND || command == TS_READ_COMMAND || command == TS_OUTPUT_COMMAND || command == TS_BURN_COMMAND || command == TS_CHUNK_WRITE_COMMAND || command == TS_GET_SCATTERED_GET_COMMAND || command == TS_CRC_CHECK_COMMAND - || command == TS_GET_FIRMWARE_VERSION; + || command == TS_GET_FIRMWARE_VERSION + || command == TS_IO_TEST_COMMAND; } /** @@ -503,43 +531,62 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, size_t i if (handled) return true; + /* check if we can extract subsystem and index for IoTest command */ + if (incomingPacketSize < sizeof(TunerStudioCmdPacketHeader)) { + sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); + tunerStudioError(tsChannel, "ERROR: underrun"); + return false; + } else { + const TunerStudioCmdPacketHeader* header = reinterpret_cast(data); + handled = true; + + switch (command) { + case TS_IO_TEST_COMMAND: + handleIoTestCommand(tsChannel, TS_CRC, SWAP_UINT16(header->subsystem), SWAP_UINT16(header->index)); + break; + default: + /* noone of simple commands */ + handled = false; + } + } + /* check if we can extract page, offset and count */ if (incomingPacketSize < sizeof(TunerStudioDataPacketHeader)) { sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); tunerStudioError(tsChannel, "ERROR: underrun"); return false; - } + } else { + const TunerStudioDataPacketHeader* header = reinterpret_cast(data); - const TunerStudioDataPacketHeader* header = reinterpret_cast(data); - - switch(command) - { - case TS_OUTPUT_COMMAND: - tsState.outputChannelsCommandCounter++; - cmdOutputChannels(tsChannel, header->offset, header->count); - break; - case TS_CHUNK_WRITE_COMMAND: - if (header->page == 0) - handleWriteChunkCommand(tsChannel, TS_CRC, header->offset, header->count, data + sizeof(TunerStudioDataPacketHeader)); - else - handleScatterListWriteCommand(tsChannel, header->offset, header->count, data + sizeof(TunerStudioDataPacketHeader)); - break; - case TS_CRC_CHECK_COMMAND: - if (header->page == 0) - handleCrc32Check(tsChannel, TS_CRC, header->offset, header->count); - else - handleScatterListCrc32Check(tsChannel, header->offset, header->count); - break; - case TS_READ_COMMAND: - if (header->page == 0) - handlePageReadCommand(tsChannel, TS_CRC, header->offset, header->count); - else - handleScatterListReadCommand(tsChannel, header->offset, header->count); - break; - default: - sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); - tunerStudioError(tsChannel, "ERROR: ignoring unexpected command"); - return false; + switch(command) + { + case TS_OUTPUT_COMMAND: + tsState.outputChannelsCommandCounter++; + cmdOutputChannels(tsChannel, header->offset, header->count); + break; + case TS_CHUNK_WRITE_COMMAND: + if (header->page == 0) + handleWriteChunkCommand(tsChannel, TS_CRC, header->offset, header->count, data + sizeof(TunerStudioDataPacketHeader)); + else + handleScatterListWriteCommand(tsChannel, header->offset, header->count, data + sizeof(TunerStudioDataPacketHeader)); + break; + case TS_CRC_CHECK_COMMAND: + if (header->page == 0) + handleCrc32Check(tsChannel, TS_CRC, header->offset, header->count); + else + handleScatterListCrc32Check(tsChannel, header->offset, header->count); + break; + case TS_READ_COMMAND: + if (header->page == 0) + handlePageReadCommand(tsChannel, TS_CRC, header->offset, header->count); + else + handleScatterListReadCommand(tsChannel, header->offset, header->count); + break; + default: + sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); + tunerStudioError(tsChannel, "ERROR: ignoring unexpected command"); + return false; + } } return true; diff --git a/firmware/console/binary/tunerstudio.h b/firmware/console/binary/tunerstudio.h index d2c7513..2308594 100644 --- a/firmware/console/binary/tunerstudio.h +++ b/firmware/console/binary/tunerstudio.h @@ -29,6 +29,12 @@ uint8_t* getWorkingPageAddr(); void startTunerStudioConnectivity(void); +typedef struct { + uint8_t cmd; + uint16_t subsystem; + uint16_t index; +} __attribute__((packed)) TunerStudioCmdPacketHeader; + typedef struct { uint8_t cmd; uint16_t page; diff --git a/firmware/console/binary/tunerstudio_io.h b/firmware/console/binary/tunerstudio_io.h index ec335f7..fc05e8c 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -28,6 +28,7 @@ #define TS_READ_COMMAND 'R' #define TS_TEST_COMMAND 't' #define TS_GET_SCATTERED_GET_COMMAND '9' +#define TS_IO_TEST_COMMAND 'Z' #define TS_RESPONSE_BURN_OK 4 #define TS_RESPONSE_COMMAND_OK 7 diff --git a/firmware/ini/wideband_dual.ini b/firmware/ini/wideband_dual.ini index a8a239d..dbb5377 100644 --- a/firmware/ini/wideband_dual.ini +++ b/firmware/ini/wideband_dual.ini @@ -12,12 +12,12 @@ enable2ndByteCanID = false [MegaTune] ; https://rusefi.com/forum/viewtopic.php?p=36201#p36201 - signature = "rusEFI 2023.03.23.wideband_dual" + signature = "rusEFI 2023.05.10.wideband_dual" [TunerStudio] queryCommand = "S" versionInfo = "V" ; firmware version for title bar. - signature = "rusEFI 2023.03.23.wideband_dual" ; signature is expected to be 7 or more characters. + signature = "rusEFI 2023.05.10.wideband_dual" ; signature is expected to be 7 or more characters. ; TS will try to use legacy temp units in some cases, showing "deg F" on a CLT gauge that's actually deg C useLegacyFTempUnits = false @@ -297,6 +297,9 @@ menuDialog = main subMenu = auxOut0, "AUX analog output 0" subMenu = auxOut1, "AUX analog output 1" + menu = "&Controller" + subMenu = ecuTools, "ECU tools" + [ControllerCommands] ; commandName = command1, command2, commandn... ; command in standard ini format, a command name can be assigned to 1 to n commands that will be executed in order. @@ -311,6 +314,8 @@ menuDialog = main cmd_reset_controller = "Z\x00\xbb\x00\x00" ; jump to DFU mode cmd_dfu = "Z\x00\xba\x00\x00" +; restart to OpenBlt +cmd_openblt = "Z\x00\xbc\x00\x00" [UserDefined] @@ -328,4 +333,12 @@ dialog = auxOut1, "AUX analog out 1 Settings" field = "Signal", Aux1InputSel panel = auxOut1Curve +dialog = ecuReset, "Reset" + commandButton = "Reset ECU", cmd_reset_controller + commandButton = "Reset to DFU", cmd_dfu + commandButton = "Reset to OpenBLT", cmd_openblt + +dialog = ecuTools, "ECU tools and Commands", xAxis + panel = ecuReset + [Tools] diff --git a/firmware/ini/wideband_f1.ini b/firmware/ini/wideband_f1.ini index df038a8..facef15 100644 --- a/firmware/ini/wideband_f1.ini +++ b/firmware/ini/wideband_f1.ini @@ -12,12 +12,12 @@ enable2ndByteCanID = false [MegaTune] ; https://rusefi.com/forum/viewtopic.php?p=36201#p36201 - signature = "rusEFI 2023.03.23.wideband_f1" + signature = "rusEFI 2023.05.10.wideband_f1" [TunerStudio] queryCommand = "S" versionInfo = "V" ; firmware version for title bar. - signature = "rusEFI 2023.03.23.wideband_f1" ; signature is expected to be 7 or more characters. + signature = "rusEFI 2023.05.10.wideband_f1" ; signature is expected to be 7 or more characters. ; TS will try to use legacy temp units in some cases, showing "deg F" on a CLT gauge that's actually deg C useLegacyFTempUnits = false @@ -192,6 +192,8 @@ menuDialog = main cmd_reset_controller = "Z\x00\xbb\x00\x00" ; jump to DFU mode cmd_dfu = "Z\x00\xba\x00\x00" +; restart to OpenBlt +cmd_openblt = "Z\x00\xbc\x00\x00" [UserDefined] @@ -201,4 +203,12 @@ dialog = sensor_settings, "Sensor Settings" dialog = can_settings, "CAN Settings" field = "CAN message ID offset", CanIndexOffset +dialog = ecuReset, "Reset" + commandButton = "Reset ECU", cmd_reset_controller + commandButton = "Reset to DFU", cmd_dfu + commandButton = "Reset to OpenBLT", cmd_openblt + +dialog = ecuTools, "ECU tools and Commands", xAxis + panel = ecuReset + [Tools]