diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 8844cea..c0e5cd5 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -59,12 +59,22 @@ * */ -#include "pch.h" -#include "os_access.h" +//#include "os_access.h" + +#include + +/* configuration */ +#include "port.h" + +/* chprintf */ +#include "chprintf.h" #include "tunerstudio.h" #include "tunerstudio_impl.h" +#include "crc.h" +#include "byteswap.h" +/* #include "main_trigger_callback.h" #include "flash_main.h" @@ -84,64 +94,30 @@ #include "mmc_card.h" #include "signature.h" +*/ -#if EFI_SIMULATOR -#include "rusEfiFunctionalTest.h" -#endif /* EFI_SIMULATOR */ static void printErrorCounters() { - efiPrintf("TunerStudio size=%d / total=%d / errors=%d / H=%d / O=%d / P=%d / B=%d", - sizeof(engine->outputChannels), tsState.totalCounter, tsState.errorCounter, tsState.queryCommandCounter, - tsState.outputChannelsCommandCounter, tsState.readPageCommandsCounter, tsState.burnCommandCounter); - efiPrintf("TunerStudio W=%d / C=%d / P=%d", tsState.writeValueCommandCounter, - tsState.writeChunkCommandCounter, tsState.pageCommandCounter); +// efiPrintf("TunerStudio size=%d / total=%d / errors=%d / H=%d / O=%d / P=%d / B=%d", +// sizeof(engine->outputChannels), tsState.totalCounter, tsState.errorCounter, tsState.queryCommandCounter, +// tsState.outputChannelsCommandCounter, tsState.readPageCommandsCounter, tsState.burnCommandCounter); +// efiPrintf("TunerStudio W=%d / C=%d / P=%d", tsState.writeValueCommandCounter, +// tsState.writeChunkCommandCounter, tsState.pageCommandCounter); } -#if EFI_TUNER_STUDIO - /* 1S */ #define TS_COMMUNICATION_TIMEOUT TIME_MS2I(1000) -static efitimems_t previousWriteReportMs = 0; - -static void resetTs() { - memset(&tsState, 0, sizeof(tsState)); -} - -static void printTsStats(void) { -#if EFI_PROD_CODE -#ifdef EFI_CONSOLE_RX_BRAIN_PIN - efiPrintf("Primary UART RX", hwPortname(EFI_CONSOLE_RX_BRAIN_PIN)); - efiPrintf("Primary UART TX", hwPortname(EFI_CONSOLE_TX_BRAIN_PIN)); -#endif - - if (false) { - // todo: is this code needed somewhere else? - efiPrintf("TS RX on %s", hwPortname(engineConfiguration->binarySerialRxPin)); - - efiPrintf("TS TX on %s @%d", hwPortname(engineConfiguration->binarySerialTxPin), - engineConfiguration->tunerStudioSerialSpeed); - } -#endif /* EFI_PROD_CODE */ - - printErrorCounters(); -} - -static void setTsSpeed(int value) { - engineConfiguration->tunerStudioSerialSpeed = value; - printTsStats(); -} - -#endif // EFI_TUNER_STUDIO - void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg) { -#if EFI_TUNER_STUDIO_VERBOSE - efiPrintf("%s: %s", tsChannel->name, msg); -#endif /* EFI_TUNER_STUDIO_VERBOSE */ +//#if EFI_TUNER_STUDIO_VERBOSE +// efiPrintf("%s: %s", tsChannel->name, msg); +//#endif /* EFI_TUNER_STUDIO_VERBOSE */ } uint8_t* getWorkingPageAddr() { - return (uint8_t*)engineConfiguration; + //return (uint8_t*)&GetConfiguration(); + return NULL; + //return (uint8_t*)engineConfiguration; } void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode) { @@ -162,14 +138,8 @@ void TunerStudio::handlePageSelectCommand(TsChannelBase *tsChannel, ts_response_ sendOkResponse(tsChannel, mode); } -#if EFI_TUNER_STUDIO - +#if 0 const void * getStructAddr(live_data_e structId) { -#if EFI_UNIT_TEST - if (engine == nullptr) { - return nullptr; - } -#endif switch (structId) { case LDS_output_channels: return reinterpret_cast(&engine->outputChannels); @@ -258,8 +228,7 @@ static void handleGetStructContent(TsChannelBase* tsChannel, int structId, int s } tsChannel->sendResponse(TS_CRC, (const uint8_t *)addr, size); } - -#endif // EFI_TUNER_STUDIO +#endif bool validateOffsetCount(size_t offset, size_t count, TsChannelBase* tsChannel); @@ -273,8 +242,6 @@ void TunerStudio::handleWriteChunkCommand(TsChannelBase* tsChannel, ts_response_ void *content) { tsState.writeChunkCommandCounter++; - efiPrintf("WRITE CHUNK mode=%d o=%d s=%d", mode, offset, count); - if (validateOffsetCount(offset, count, tsChannel)) { return; } @@ -288,8 +255,6 @@ void TunerStudio::handleWriteChunkCommand(TsChannelBase* tsChannel, ts_response_ sendOkResponse(tsChannel, mode); } -#if EFI_TUNER_STUDIO - void TunerStudio::handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) { tsState.crc32CheckCommandCounter++; @@ -309,8 +274,8 @@ void TunerStudio::handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_ * @note Writing values one by one is pretty slow */ void TunerStudio::handleWriteValueCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t offset, uint8_t value) { - UNUSED(tsChannel); - UNUSED(mode); + (void)tsChannel; + (void)mode; tsState.writeValueCommandCounter++; @@ -320,12 +285,6 @@ void TunerStudio::handleWriteValueCommand(TsChannelBase* tsChannel, ts_response_ return; } - efitimems_t nowMs = currentTimeMillis(); - if (nowMs - previousWriteReportMs > 5) { - previousWriteReportMs = nowMs; - efiPrintf("offset %d: value=%d", offset, value); - } - // Skip the write if a preset was just loaded - we don't want to overwrite it if (!rebootForPresetPending) { getWorkingPageAddr()[offset] = value; @@ -340,9 +299,9 @@ void TunerStudio::handlePageReadCommand(TsChannelBase* tsChannel, ts_response_fo return; } -#if EFI_TUNER_STUDIO_VERBOSE - efiPrintf("READ mode=%d offset=%d size=%d", mode, offset, count); -#endif +//#if EFI_TUNER_STUDIO_VERBOSE +// efiPrintf("READ mode=%d offset=%d size=%d", mode, offset, count); +//#endif if (validateOffsetCount(offset, count, tsChannel)) { return; @@ -350,21 +309,13 @@ void TunerStudio::handlePageReadCommand(TsChannelBase* tsChannel, ts_response_fo const uint8_t* addr = getWorkingPageAddr() + offset; tsChannel->sendResponse(mode, addr, count); -#if EFI_TUNER_STUDIO_VERBOSE +//#if EFI_TUNER_STUDIO_VERBOSE // efiPrintf("Sending %d done", count); -#endif +//#endif } -#endif // EFI_TUNER_STUDIO - void requestBurn(void) { -#if !EFI_UNIT_TEST - onBurnRequest(); - -#if EFI_INTERNAL_FLASH - setNeedToWriteConfiguration(); -#endif -#endif // !EFI_UNIT_TEST + SaveConfiguration(); } static void sendResponseCode(ts_response_format_e mode, TsChannelBase *tsChannel, const uint8_t responseCode) { @@ -377,22 +328,13 @@ static void sendResponseCode(ts_response_format_e mode, TsChannelBase *tsChannel * 'Burn' command is a command to commit the changes */ static void handleBurnCommand(TsChannelBase* tsChannel, ts_response_format_e mode) { - efitimems_t nowMs = currentTimeMillis(); tsState.burnCommandCounter++; - efiPrintf("got B (Burn) %s", mode == TS_PLAIN ? "plain" : "CRC"); - - // Skip the burn if a preset was just loaded - we don't want to overwrite it - if (!rebootForPresetPending) { - requestBurn(); - } + SaveConfiguration(); sendResponseCode(mode, tsChannel, TS_RESPONSE_BURN_OK); - efiPrintf("BURN in %dms", currentTimeMillis() - nowMs); } -#if EFI_TUNER_STUDIO && (EFI_PROD_CODE || EFI_SIMULATOR) - static bool isKnownCommand(char command) { return command == TS_HELLO_COMMAND || command == TS_READ_COMMAND || command == TS_OUTPUT_COMMAND || command == TS_PAGE_COMMAND || command == TS_BURN_COMMAND || command == TS_SINGLE_WRITE_COMMAND @@ -413,6 +355,9 @@ static bool isKnownCommand(char command) { /** * rusEfi own test command */ + +#define VCS_VERSION "unknown" + static void handleTestCommand(TsChannelBase* tsChannel) { tsState.testCommandCounter++; char testOutputBuffer[64]; @@ -423,20 +368,17 @@ static void handleTestCommand(TsChannelBase* tsChannel) { tunerStudioDebug(tsChannel, "got T (Test)"); tsChannel->write((const uint8_t*)VCS_VERSION, sizeof(VCS_VERSION)); - chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " %d %d", engine->engineState.warnings.lastErrorCode, tsState.testCommandCounter); - tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer)); - - chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " uptime=%ds ", (int)getTimeNowSeconds()); - tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer)); - - chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), __DATE__ " %s\r\n", PROTOCOL_TEST_RESPONSE_TAG); + chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), __DATE__ "\r\n"); tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer)); +/* if (hasFirmwareError()) { const char* error = getCriticalErrorMessage(); chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), "error=%s\r\n", error); tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer)); } +*/ + tsChannel->flush(); } @@ -448,10 +390,9 @@ static void handleTestCommand(TsChannelBase* tsChannel) { */ void TunerStudio::handleQueryCommand(TsChannelBase* tsChannel, ts_response_format_e mode) { tsState.queryCommandCounter++; -#if EFI_TUNER_STUDIO_VERBOSE - efiPrintf("got S/H (queryCommand) mode=%d", mode); - printTsStats(); -#endif +//#if EFI_TUNER_STUDIO_VERBOSE +// efiPrintf("got S/H (queryCommand) mode=%d", mode); +//#endif const char *signature = getTsSignature(); tsChannel->sendResponse(mode, (const uint8_t *)signature, strlen(signature) + 1); } @@ -467,7 +408,7 @@ bool TunerStudio::handlePlainCommand(TsChannelBase* tsChannel, uint8_t command) return false; } else if (command == TS_HELLO_COMMAND || command == TS_QUERY_COMMAND) { // We interpret 'Q' as TS_HELLO_COMMAND, since TS uses hardcoded 'Q' during ECU detection (scan all serial ports) - efiPrintf("Got naked Query command"); + //efiPrintf("Got naked Query command"); handleQueryCommand(tsChannel, TS_PLAIN); return true; } else if (command == TS_TEST_COMMAND || command == 'T') { @@ -495,8 +436,6 @@ bool TunerStudio::handlePlainCommand(TsChannelBase* tsChannel, uint8_t command) TunerStudio tsInstance; static int tsProcessOne(TsChannelBase* tsChannel) { - validateStack("communication", STACK_USAGE_COMMUNICATION, 128); - if (!tsChannel->isReady()) { chThdSleepMilliseconds(10); return -1; @@ -506,9 +445,6 @@ static int tsProcessOne(TsChannelBase* tsChannel) { uint8_t firstByte; int received = tsChannel->readTimeout(&firstByte, 1, TS_COMMUNICATION_TIMEOUT); -#if EFI_SIMULATOR - logMsg("received %d\r\n", received); -#endif if (received != 1) { // tunerStudioError("ERROR: no command"); @@ -534,36 +470,32 @@ static int tsProcessOne(TsChannelBase* tsChannel) { uint16_t incomingPacketSize = firstByte << 8 | secondByte; if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(tsChannel->scratchBuffer) - CRC_WRAPPING_SIZE)) { - efiPrintf("process_ts: channel=%s invalid size: %d", tsChannel->name, incomingPacketSize); - tunerStudioError(tsChannel, "process_ts: ERROR: CRC header size"); + //efiPrintf("process_ts: channel=%s invalid size: %d", tsChannel->name, incomingPacketSize); + //tunerStudioError(tsChannel, "process_ts: ERROR: CRC header size"); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); return -1; } received = tsChannel->readTimeout((uint8_t* )tsChannel->scratchBuffer, 1, TS_COMMUNICATION_TIMEOUT); if (received != 1) { - tunerStudioError(tsChannel, "ERROR: did not receive command"); + //tunerStudioError(tsChannel, "ERROR: did not receive command"); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); return -1; } char command = tsChannel->scratchBuffer[0]; if (!isKnownCommand(command)) { - efiPrintf("unexpected command %x", command); + //efiPrintf("unexpected command %x", command); sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); return -1; } -#if EFI_SIMULATOR - logMsg("command %c\r\n", command); -#endif - int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1; received = tsChannel->readTimeout((uint8_t*)(tsChannel->scratchBuffer + 1), expectedSize, TS_COMMUNICATION_TIMEOUT); if (received != expectedSize) { - efiPrintf("Got only %d bytes while expecting %d for command %c", received, - expectedSize, command); - tunerStudioError(tsChannel, "ERROR: not enough bytes in stream"); + //efiPrintf("Got only %d bytes while expecting %d for command %c", received, + // expectedSize, command); + //tunerStudioError(tsChannel, "ERROR: not enough bytes in stream"); sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN); return -1; } @@ -574,12 +506,12 @@ static int tsProcessOne(TsChannelBase* tsChannel) { uint32_t actualCrc = crc32(tsChannel->scratchBuffer, incomingPacketSize); if (actualCrc != expectedCrc) { - efiPrintf("TunerStudio: CRC %x %x %x %x", tsChannel->scratchBuffer[incomingPacketSize + 0], - tsChannel->scratchBuffer[incomingPacketSize + 1], tsChannel->scratchBuffer[incomingPacketSize + 2], - tsChannel->scratchBuffer[incomingPacketSize + 3]); + //efiPrintf("TunerStudio: CRC %x %x %x %x", tsChannel->scratchBuffer[incomingPacketSize + 0], + // tsChannel->scratchBuffer[incomingPacketSize + 1], tsChannel->scratchBuffer[incomingPacketSize + 2], + // tsChannel->scratchBuffer[incomingPacketSize + 3]); - efiPrintf("TunerStudio: command %c actual CRC %x/expected %x", tsChannel->scratchBuffer[0], - actualCrc, expectedCrc); + //efiPrintf("TunerStudio: command %c actual CRC %x/expected %x", tsChannel->scratchBuffer[0], + // actualCrc, expectedCrc); tunerStudioError(tsChannel, "ERROR: CRC issue"); sendErrorCode(tsChannel, TS_RESPONSE_CRC_FAILURE); return -1; @@ -588,7 +520,7 @@ static int tsProcessOne(TsChannelBase* tsChannel) { int success = tsInstance.handleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize); if (!success) { - efiPrintf("got unexpected TunerStudio command %x:%c", command, command); + //efiPrintf("got unexpected TunerStudio command %x:%c", command, command); return -1; } @@ -606,15 +538,13 @@ void TunerstudioThread::ThreadTask() { // Until the end of time, process incoming messages. while (true) { if (tsProcessOne(channel) == 0) { - onDataArrived(true); + //onDataArrived(true); } else { - onDataArrived(false); + //onDataArrived(false); } } } -#endif // EFI_TUNER_STUDIO - tunerstudio_counters_s tsState; void tunerStudioError(TsChannelBase* tsChannel, const char *msg) { @@ -623,15 +553,10 @@ void tunerStudioError(TsChannelBase* tsChannel, const char *msg) { tsState.errorCounter++; } -#if EFI_TUNER_STUDIO - -#if EFI_PROD_CODE || EFI_SIMULATOR - -extern CommandHandler console_line_callback; - static void handleGetVersion(TsChannelBase* tsChannel) { char versionBuffer[32]; - chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION); + //chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION); + chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI Wideband Rev2"); tsChannel->sendResponse(TS_CRC, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1); } @@ -643,32 +568,25 @@ static void handleGetText(TsChannelBase* tsChannel) { size_t outputSize; const char* output = swapOutputBuffers(&outputSize); -#if EFI_SIMULATOR - logMsg("get test sending [%d]\r\n", outputSize); -#endif tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, reinterpret_cast(output), outputSize, true); -#if EFI_SIMULATOR - logMsg("sent [%d]\r\n", outputSize); -#endif } #endif // EFI_TEXT_LOGGING +#if 0 +extern CommandHandler console_line_callback; void TunerStudio::handleExecuteCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) { data[incomingPacketSize] = 0; char *trimmed = efiTrim(data); -#if EFI_SIMULATOR - logMsg("execute [%s]\r\n", trimmed); -#endif + (console_line_callback)(trimmed); tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, nullptr, 0); } - -static int transmitted = 0; +#endif int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) { - ScopePerf perf(PE::TunerStudioHandleCrcCommand); + (void)incomingPacketSize; char command = data[0]; data++; @@ -695,15 +613,17 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco handleGetText(tsChannel); break; #endif // EFI_TEXT_LOGGING +#if 0 case TS_EXECUTE: handleExecuteCommand(tsChannel, data, incomingPacketSize - 1); break; +#endif case TS_PAGE_COMMAND: handlePageSelectCommand(tsChannel, TS_CRC); break; - case TS_GET_STRUCT: - handleGetStructContent(tsChannel, offset, count); - break; +// case TS_GET_STRUCT: +// handleGetStructContent(tsChannel, offset, count); +// break; case TS_CHUNK_WRITE_COMMAND: handleWriteChunkCommand(tsChannel, TS_CRC, offset, count, data + sizeof(TunerStudioWriteChunkRequest)); break; @@ -727,6 +647,7 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco case 'T': handleTestCommand(tsChannel); break; +#if 0 case TS_IO_TEST_COMMAND: { uint16_t subsystem = SWAP_UINT16(data16[0]); @@ -738,81 +659,10 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco engine->outputChannels.debugIntField3 = index; } -#if EFI_PROD_CODE && EFI_ENGINE_CONTROL - executeTSCommand(subsystem, index); -#endif /* EFI_PROD_CODE */ + executeTSCommand(subsystem, index); sendOkResponse(tsChannel, TS_CRC); } break; -#if EFI_TOOTH_LOGGER - case TS_SET_LOGGER_SWITCH: - switch(data[0]) { - case TS_COMPOSITE_ENABLE: - EnableToothLogger(); - break; - case TS_COMPOSITE_DISABLE: - DisableToothLogger(); - break; - default: - // dunno what that was, send NAK - return false; - } - - sendOkResponse(tsChannel, TS_CRC); - - break; - case TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY: - - { - EnableToothLoggerIfNotEnabled(); - const uint8_t* const buffer = GetToothLoggerBuffer().Buffer; - - const uint8_t* const start = buffer + COMPOSITE_PACKET_SIZE * transmitted; - - int currentEnd = getCompositeRecordCount(); - - // set debug_mode 40 - if (engineConfiguration->debugMode == DBG_COMPOSITE_LOG) { - engine->outputChannels.debugIntField1 = currentEnd; - engine->outputChannels.debugIntField2 = transmitted; - - } - - if (currentEnd > transmitted) { - // more normal case - tail after head - tsChannel->sendResponse(TS_CRC, start, COMPOSITE_PACKET_SIZE * (currentEnd - transmitted), true); - transmitted = currentEnd; - } else if (currentEnd == transmitted) { - tsChannel->sendResponse(TS_CRC, start, 0); - } else { - // we are here if tail of buffer has reached the end of buffer and re-started from the start of buffer - // sending end of the buffer, next transmission would take care of the rest - tsChannel->sendResponse(TS_CRC, start, COMPOSITE_PACKET_SIZE * (COMPOSITE_PACKET_COUNT - transmitted), true); - transmitted = 0; - } - } - break; - case TS_GET_LOGGER_GET_BUFFER: - { - auto toothBuffer = GetToothLoggerBuffer(); - tsChannel->sendResponse(TS_CRC, toothBuffer.Buffer, toothBuffer.Length, true); - } - - break; -#endif /* EFI_TOOTH_LOGGER */ -#if ENABLE_PERF_TRACE - case TS_PERF_TRACE_BEGIN: - perfTraceEnable(); - sendOkResponse(tsChannel, TS_CRC); - break; - case TS_PERF_TRACE_GET_BUFFER: - { - auto trace = perfTraceGetBuffer(); - tsChannel->sendResponse(TS_CRC, trace.Buffer, trace.Size, true); - } - - break; -#endif /* ENABLE_PERF_TRACE */ case TS_GET_CONFIG_ERROR: { const char* configError = getCriticalErrorMessage(); #if HW_CHECK_MODE @@ -824,6 +674,7 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco tsChannel->sendResponse(TS_CRC, reinterpret_cast(configError), strlen(configError), true); break; } +#endif default: sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND); tunerStudioError(tsChannel, "ERROR: ignoring unexpected command"); @@ -833,21 +684,15 @@ int TunerStudio::handleCrcCommand(TsChannelBase* tsChannel, char *data, int inco return true; } -#endif // EFI_PROD_CODE || EFI_SIMULATOR - void startTunerStudioConnectivity(void) { // Assert tune & output channel struct sizes - static_assert(sizeof(persistent_config_s) == TOTAL_CONFIG_SIZE, "TS datapage size mismatch"); +// static_assert(sizeof(persistent_config_s) == TOTAL_CONFIG_SIZE, "TS datapage size mismatch"); // useful trick if you need to know how far off is the static_assert // char (*__kaboom)[sizeof(persistent_config_s)] = 1; // another useful trick // static_assert(offsetof (engine_configuration_s,HD44780_e) == 700); memset(&tsState, 0, sizeof(tsState)); - - addConsoleAction("tsinfo", printTsStats); - addConsoleAction("reset_ts", resetTs); - addConsoleActionI("set_ts_speed", setTsSpeed); #if EFI_BLUETOOTH_SETUP // module initialization start (it waits for disconnect and then communicates to the module) @@ -869,5 +714,3 @@ void startTunerStudioConnectivity(void) { addConsoleAction("bluetooth_cancel", bluetoothCancel); #endif /* EFI_BLUETOOTH_SETUP */ } - -#endif diff --git a/firmware/console/binary/tunerstudio.h b/firmware/console/binary/tunerstudio.h index 3c52c03..844444c 100644 --- a/firmware/console/binary/tunerstudio.h +++ b/firmware/console/binary/tunerstudio.h @@ -6,11 +6,12 @@ */ #pragma once -#include "global.h" #include "tunerstudio_io.h" -#include "electronic_throttle_generated.h" -#include "knock_controller_generated.h" -#include "FragmentEntry.h" +//#include "electronic_throttle_generated.h" +//#include "knock_controller_generated.h" +//#include "FragmentEntry.h" + +#include "thread_controller.h" typedef struct { int queryCommandCounter; @@ -33,13 +34,6 @@ void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg); void tunerStudioError(TsChannelBase* tsChannel, const char *msg); uint8_t* getWorkingPageAddr(); -const void * getStructAddr(live_data_e structId); - -#if EFI_TUNER_STUDIO -#include "thread_controller.h" -#include "thread_priority.h" - -FragmentList getFragments(); void updateTunerStudioState(); @@ -63,13 +57,13 @@ post_packed { short int count; } TunerStudioWriteChunkRequest; -#if EFI_PROD_CODE || EFI_SIMULATOR -#define CONNECTIVITY_THREAD_STACK (2 * UTILITY_THREAD_STACK_SIZE) +#define CONNECTIVITY_THREAD_STACK (512) +#define CONNECTIVITY_THREAD_PRIO (NORMALPRIO + 1) class TunerstudioThread : public ThreadController { public: TunerstudioThread(const char* name) - : ThreadController(name, PRIO_CONSOLE) + : ThreadController(name, CONNECTIVITY_THREAD_PRIO) { } @@ -79,6 +73,3 @@ public: void ThreadTask() override; }; -#endif - -#endif /* EFI_TUNER_STUDIO */ diff --git a/firmware/console/binary/tunerstudio_impl.h b/firmware/console/binary/tunerstudio_impl.h index 939c380..76ee0bf 100644 --- a/firmware/console/binary/tunerstudio_impl.h +++ b/firmware/console/binary/tunerstudio_impl.h @@ -5,6 +5,7 @@ #pragma once #include +#include struct TsChannelBase; diff --git a/firmware/console/binary/tunerstudio_io.cpp b/firmware/console/binary/tunerstudio_io.cpp index 3acac21..cbc9e43 100644 --- a/firmware/console/binary/tunerstudio_io.cpp +++ b/firmware/console/binary/tunerstudio_io.cpp @@ -5,19 +5,16 @@ * @author Andrey Belomutskiy, (c) 2012-2020 */ -#include "pch.h" -#include "os_access.h" +/* memcpy */ +#include + #include "tunerstudio_io.h" +#include "crc.h" +#include "byteswap.h" -#if EFI_SIMULATOR -#include "rusEfiFunctionalTest.h" -#endif // EFI_SIMULATOR - -#if EFI_PROD_CODE || EFI_SIMULATOR size_t TsChannelBase::read(uint8_t* buffer, size_t size) { return readTimeout(buffer, size, SR5_READ_TIMEOUT); } -#endif #define isBigPacket(size) ((size) > BLOCKING_FACTOR + 7) @@ -25,7 +22,7 @@ void TsChannelBase::copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8 auto scratchBuffer = this->scratchBuffer; // don't transmit too large a buffer - efiAssertVoid(OBD_PCM_Processor_Fault, !isBigPacket(size), "copyAndWriteSmallCrcPacket tried to transmit too large a packet") + chDbgAssert(!isBigPacket(size), "copyAndWriteSmallCrcPacket tried to transmit too large a packet"); // If transmitting data, copy it in to place in the scratch buffer // We want to prevent the data changing itself (higher priority threads could write @@ -39,7 +36,7 @@ void TsChannelBase::copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8 } void TsChannelBase::crcAndWriteBuffer(uint8_t responseCode, size_t size) { - efiAssertVoid(OBD_PCM_Processor_Fault, !isBigPacket(size), "crcAndWriteBuffer tried to transmit too large a packet") + chDbgAssert(!isBigPacket(size), "crcAndWriteBuffer tried to transmit too large a packet"); auto scratchBuffer = this->scratchBuffer; // Index 0/1 = packet size (big endian) @@ -89,9 +86,7 @@ TsChannelBase::TsChannelBase(const char *name) { } void TsChannelBase::assertPacketSize(size_t size, bool allowLongPackets) { - if (isBigPacket(size) && !allowLongPackets) { - firmwareError(OBD_PCM_Processor_Fault, "tried to send disallowed long packet of size %d", size); - } + chDbgAssert(!(isBigPacket(size) && !allowLongPackets), "tried to send disallowed long packet"); } /** diff --git a/firmware/console/binary/tunerstudio_io.h b/firmware/console/binary/tunerstudio_io.h index 8debb66..bc4738e 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -7,28 +7,110 @@ */ #pragma once -#include "global.h" + +/* to get ChibiOS hal config options */ +#include "hal.h" + #include "tunerstudio_impl.h" -#if (!TS_NO_PRIMARY && defined(TS_PRIMARY_PORT)) - #define HAS_PRIMARY true -#else - #define HAS_PRIMARY false -#endif +/* TODO: find better place */ +#define BLOCKING_FACTOR 256 -#if (!TS_NO_SECONDARY && defined(TS_SECONDARY_PORT)) - #define HAS_SECONDARY true -#else - #define HAS_SECONDARY false -#endif - -#if EFI_USB_SERIAL -#include "usbconsole.h" -#endif // EFI_USB_SERIAL - -#if EFI_PROD_CODE -#include "pin_repository.h" -#endif +/* TODO: From autogenerated, rework! */ +#define TS_BURN_COMMAND 'B' +#define TS_BURN_COMMAND_char B +#define TS_CHUNK_WRITE_COMMAND 'C' +#define TS_CHUNK_WRITE_COMMAND_char C +#define ts_command_e_TS_BENCH_CATEGORY 22 +#define ts_command_e_TS_CLEAR_WARNINGS 17 +#define ts_command_e_TS_COMMAND_1 1 +#define ts_command_e_TS_COMMAND_11 11 +#define ts_command_e_TS_COMMAND_12 12 +#define ts_command_e_TS_COMMAND_13 13 +#define ts_command_e_TS_COMMAND_14 14 +#define ts_command_e_TS_COMMAND_15 15 +#define ts_command_e_TS_COMMAND_16 16 +#define ts_command_e_TS_COMMAND_4 4 +#define ts_command_e_TS_COMMAND_5 5 +#define ts_command_e_TS_COMMAND_9 9 +#define ts_command_e_TS_CRAZY 32 +#define ts_command_e_TS_DEBUG_MODE 0 +#define ts_command_e_TS_GRAB_PEDAL_UP 6 +#define ts_command_e_TS_GRAB_PEDAL_WOT 7 +#define ts_command_e_TS_GRAB_TPS_CLOSED 2 +#define ts_command_e_TS_GRAB_TPS_WOT 3 +#define ts_command_e_TS_IGNITION_CATEGORY 18 +#define ts_command_e_TS_INJECTOR_CATEGORY 19 +#define ts_command_e_TS_RESET_TLE8888 8 +#define ts_command_e_TS_UNUSED_23 23 +#define ts_command_e_TS_UNUSED_25 25 +#define ts_command_e_TS_UNUSED_26 26 +#define ts_command_e_TS_UNUSED_27 27 +#define ts_command_e_TS_UNUSED_28 28 +#define ts_command_e_TS_UNUSED_29 29 +#define ts_command_e_TS_UNUSED_30 30 +#define ts_command_e_TS_UNUSED_31 31 +#define ts_command_e_TS_UNUSED_CJ125_CALIB 24 +#define ts_command_e_TS_WIDEBAND 21 +#define ts_command_e_TS_WRITE_FLASH 10 +#define ts_command_e_TS_X14 20 +#define TS_COMMAND_F 'F' +#define TS_COMMAND_F_char F +#define TS_COMPOSITE_DISABLE 2 +#define TS_COMPOSITE_ENABLE 1 +#define TS_CRC_CHECK_COMMAND 'k' +#define TS_CRC_CHECK_COMMAND_char k +#define TS_EXECUTE 'E' +#define TS_EXECUTE_char E +#define TS_FILE_VERSION 20210312 +#define TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY '8' +#define TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY_char 8 +#define TS_GET_CONFIG_ERROR 'e' +#define TS_GET_CONFIG_ERROR_char e +#define TS_GET_FIRMWARE_VERSION 'V' +#define TS_GET_FIRMWARE_VERSION_char V +#define TS_GET_LOGGER_GET_BUFFER 'L' +#define TS_GET_LOGGER_GET_BUFFER_char L +#define TS_GET_OUTPUTS_SIZE '4' +#define TS_GET_OUTPUTS_SIZE_char 4 +#define TS_GET_STRUCT '9' +#define TS_GET_STRUCT_char 9 +#define TS_GET_TEXT 'G' +#define TS_GET_TEXT_char G +#define TS_HELLO_COMMAND 'S' +#define TS_HELLO_COMMAND_char S +#define TS_IO_TEST_COMMAND 'Z' +#define TS_IO_TEST_COMMAND_char Z +#define TS_ONLINE_PROTOCOL 'z' +#define TS_ONLINE_PROTOCOL_char z +#define TS_OUTPUT_COMMAND 'O' +#define TS_OUTPUT_COMMAND_char O +#define TS_PAGE_COMMAND 'P' +#define TS_PAGE_COMMAND_char P +#define TS_PERF_TRACE_BEGIN '_' +#define TS_PERF_TRACE_BEGIN_char _ +#define TS_PERF_TRACE_GET_BUFFER 'b' +#define TS_PERF_TRACE_GET_BUFFER_char b +#define TS_PROTOCOL "001" +#define TS_QUERY_COMMAND 'Q' +#define TS_QUERY_COMMAND_char Q +#define TS_READ_COMMAND 'R' +#define TS_READ_COMMAND_char R +#define TS_RESPONSE_BURN_OK 4 +#define TS_RESPONSE_COMMAND_OK 7 +#define TS_RESPONSE_CRC_FAILURE 0x82 +#define TS_RESPONSE_FRAMING_ERROR 0x8D +#define TS_RESPONSE_OK 0 +#define TS_RESPONSE_OUT_OF_RANGE 0x84 +#define TS_RESPONSE_UNDERRUN 0x80 +#define TS_RESPONSE_UNRECOGNIZED_COMMAND 0x83 +#define TS_SET_LOGGER_SWITCH 'l' +#define TS_SET_LOGGER_SWITCH_char l +#define TS_SINGLE_WRITE_COMMAND 'W' +#define TS_SINGLE_WRITE_COMMAND_char W +#define TS_TEST_COMMAND 't' +#define TS_TEST_COMMAND_char t +#define TS_TOTAL_OUTPUT_SIZE 1016 class TsChannelBase { public: