From 767ca4cf9422f844baaf2f9439edb42710e06a58 Mon Sep 17 00:00:00 2001 From: cfif Date: Tue, 26 May 2026 15:46:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BB=D0=B0=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20V2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CanUds.c | 134 ++++++++++++++++++++++++++++++++++++++++++---------- CanUds.h | 21 ++++++-- crc32_uds.c | 15 +++--- crc32_uds.h | 3 +- 4 files changed, 134 insertions(+), 39 deletions(-) diff --git a/CanUds.c b/CanUds.c index 85689eb..48c2962 100644 --- a/CanUds.c +++ b/CanUds.c @@ -295,10 +295,10 @@ static uint16_t DiagnosticSessionControl_10(tCanUds *env) { return setResponseError(env, UDS_DiagnosticSessionControl, UDS_error_sub_functionNotSupported); } - if ((env->currentSession == UDS_session_programmingSession) && - (com->sub_function == UDS_session_extendedDiagnosticSession)) { - return setResponseError(env, UDS_DiagnosticSessionControl, UDS_error_conditionsNotCorrect); - } +// if ((env->currentSession == UDS_session_programmingSession) && +// (com->sub_function == UDS_session_extendedDiagnosticSession)) { +// return setResponseError(env, UDS_DiagnosticSessionControl, UDS_error_conditionsNotCorrect); +// } if (env->currentSession != com->sub_function) { @@ -818,7 +818,7 @@ static uint16_t UDS_ReadDTCInformation_19(tCanUds *env) { tDiagnosticDTC diagnosticDTC = {env->data->data[2], NULL}; // младший байт количества DTC - env->dataResponse[5] = diagnostic_UDS_ReadDTCInformation_19_count(env->Diagnostic, &diagnosticDTC); + env->dataResponse[5] = diagnostic_UDS_ReadDTCInformation_19_count(env->Diagnostic, &diagnosticDTC); response_size = 6; } @@ -833,7 +833,7 @@ static uint16_t UDS_ReadDTCInformation_19(tCanUds *env) { response_size = 3; tDiagnosticDTC diagnosticDTC = {env->data->data[2], &env->dataResponse[response_size]}; - response_size += diagnostic_UDS_ReadDTCInformation_19_dtc(env->Diagnostic, &diagnosticDTC); + response_size += diagnostic_UDS_ReadDTCInformation_19_dtc(env->Diagnostic, &diagnosticDTC); } @@ -847,7 +847,7 @@ static uint16_t UDS_ReadDTCInformation_19(tCanUds *env) { response_size = 3; tDiagnosticDTC diagnosticDTC = {env->data->data[2], &env->dataResponse[response_size]}; - response_size += diagnostic_UDS_ReadDTCSupportDTC_19_dtc(env->Diagnostic, &diagnosticDTC); + response_size += diagnostic_UDS_ReadDTCSupportDTC_19_dtc(env->Diagnostic, &diagnosticDTC); } return response_size; @@ -975,7 +975,10 @@ static uint16_t vUDS_routine_Compare_Checksum(tCanUds *env, eUdsRoutineControlTy setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); - uint32_t crc32_calc = UdsCrc32((uint8_t *) 0x01100000, 512 * 1024); + uint32_t crc32_calc = UdsCrc32Update(0xFFFFFFFF, (uint8_t *)env->BlockMainAdr1, env->BlockMainSize1); + crc32_calc = UdsCrc32Update(crc32_calc, (uint8_t *)env->BlockMainAdr2, env->BlockMainSize2); + crc32_calc = UdsCrc32Finalize(crc32_calc); + uint32_t crc32_received = (env->data->data[4] << 24) | (env->data->data[5] << 16) | (env->data->data[6] << 8) | env->data->data[7]; @@ -996,16 +999,42 @@ static uint16_t vUDS_routine_Erase_Memory(tCanUds *env, eUdsRoutineControlType u return 0xFF00 | UDS_error_sub_functionNotSupported; } + env->udsBlock = UDS_BlockInit; + + if ((env->data->data[4] == 0) && ((env->data->data[5] == 0))) { + env->udsBlock = UDS_BlockMain; + } else if ((env->data->data[4] == 1) && ((env->data->data[5] == 0))) { + env->udsBlock = UDS_BlockCalib; + } + + if (env->udsBlock == UDS_BlockInit) { + return 0xFF00 | UDS_error_requestSequenceError; + } + setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); -// bool result = env->clear_flash_func(0x01100000, 768 * 1024); + bool resultMain = false; + bool resultMeta = false; - bool result = FLASHDRIVER_FlashEraseBlock(FLASH_BLOCK_SELECT1); + if (env->udsBlock == UDS_BlockMain) { + resultMain = env->clear_flash_func(0x01100000, 704 * 1024); + uint32_t adrMeta = 0x01100000 + 704 * 1024 - 2048; + resultMeta = env->clear_flash_func(adrMeta, 2048); + } - if (result) { - env->dataResponse[4] = UDS_routine_RoutineStartStopSuccess; + if (env->udsBlock == UDS_BlockCalib) { + resultMain = env->clear_flash_func(0x011B0000, 192 * 1024); + resultMeta = true; + } + +// bool result = FLASHDRIVER_FlashEraseBlock(FLASH_BLOCK_SELECT1); + + if ((resultMain) && (resultMeta)) { + env->dataResponse[4] = + UDS_routine_RoutineStartStopSuccess; } else { - env->dataResponse[4] = UDS_routine_RoutineStartStopFailure; + env->dataResponse[4] = + UDS_routine_RoutineStartStopFailure; } return 4 + 1; @@ -1307,16 +1336,62 @@ static uint16_t RequestDownload_34(tCanUds *env) { return setResponseError(env, UDS_RequestDownload, UDS_error_incorrectMessageLengthOrInvalidFormat); } - if (com->dataFormatIdentifier != 0) { + if ((com->dataFormatIdentifier != 0) && (com->dataFormatIdentifier != 1)) { return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); } - if (com->memoryAddress != 0x01100000) { - return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + if (com->dataFormatIdentifier == 0) { + env->udsBlock = UDS_BlockMain; } - if (com->memorySize != 0x80000) { - return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + if (com->dataFormatIdentifier == 1) { + env->udsBlock = UDS_BlockCalib; + } + + env->AdrFlash = com->memoryAddress + 0x0100000 - (128 * 1024); + env->SizeWriteFlash = 0; + env->DownloadFirmwareSize = com->memorySize; + + + if (env->udsBlock == UDS_BlockMain) { + + if ((com->memoryAddress != 0x01020000) && (com->memoryAddress != 0x010FF800)) { + return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + } + + if ((com->memoryAddress == 0x01020000) && (com->memorySize > (704 * 1024))) { + return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + } + + if ((com->memoryAddress == 0x010FF800) && (com->memorySize > (2 * 1024))) { + return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + } + + if (com->memoryAddress == 0x01020000) { + env->BlockMainAdr1 = env->AdrFlash; + env->BlockMainSize1 = env->DownloadFirmwareSize; + } + + if (com->memoryAddress == 0x010FF800) { + env->BlockMainAdr2= env->AdrFlash; + env->BlockMainSize2 = env->DownloadFirmwareSize; + } + + } + + if (env->udsBlock == UDS_BlockCalib) { + + if (com->memoryAddress != 0x010D0000) { + return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + } + + if (com->memorySize > (192 * 1024 - 2048)) { + return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + } + + env->BlockCalibAdr1 = env->AdrFlash; + env->BlockCalibSize1 = env->DownloadFirmwareSize; + } if (env->currentSession == UDS_session_defaultSession) { @@ -1334,8 +1409,10 @@ static uint16_t RequestDownload_34(tCanUds *env) { uint8_t countBlock = 8; requestDownload_Response->maxNumberOfBlockLength = countBlock * 128 + 2; - env->AdrFlash = 0x1100000; - env->SizeWriteFlash = 0; + +#if (LOG_UDS == 1) + LoggerFormatInfo(LOGGER, LOG_SIGN, "RequestDownload: memoryAddress = %d memorySize = %d ", env->AdrFlash, env->DownloadFirmwareSize) +#endif env->stateDownload = UDS_RequestDownload; @@ -1377,7 +1454,8 @@ static uint16_t TransferData_36(tCanUds *env) { } } - if (env->SizeWriteFlash >= DownloadFirmwareSize) { + + if (env->SizeWriteFlash >= env->DownloadFirmwareSize) { return setResponseError(env, UDS_RoutineControl, UDS_error_requestSequenceError); } @@ -1402,7 +1480,8 @@ static uint16_t TransferData_36(tCanUds *env) { #if (LOG_UDS == 1) - LoggerFormatTrace(LOGGER, LOG_SIGN, "Download: %d of %d kb", env->SizeWriteFlash / 1024 , DownloadFirmwareSize / 1024) + LoggerFormatInfo(LOGGER, LOG_SIGN, "Download: %d of %d kb", env->SizeWriteFlash / 1024, + env->DownloadFirmwareSize / 1024) #endif @@ -1427,12 +1506,14 @@ static uint16_t RequestTransferExit_37(tCanUds *env) { return setResponseError(env, UDS_RoutineControl, UDS_error_conditionsNotCorrect); } - if ((env->stateDownload != UDS_TransferData) || (env->SizeWriteFlash < DownloadFirmwareSize)) { + if ((env->stateDownload != UDS_TransferData) || (env->SizeWriteFlash < env->DownloadFirmwareSize)) { return setResponseError(env, UDS_RoutineControl, UDS_error_requestSequenceError); } else { env->stateDownload = UDS_RequestTransferExit; } + + tRequestTransferExit_Request *com = (tRequestTransferExit_Request *) env->data->data; tRequestTransferExit_Response *requestTransferExit_Response = (tRequestTransferExit_Response *) env->dataResponse; @@ -1715,14 +1796,14 @@ void ReceivedTP_func(void *arg, tCanTP_data *data) { #if (LOG_UDS == 1) sendLogCanUdsHex(env, env->data->data, env->data->len); - LoggerFormatInfo(LOGGER, LOG_SIGN, "> %s [%d] %s", uds_com[com].desc, env->data->len, env->hexString) + LoggerFormatInfo(LOGGER, LOG_SIGN, "> %s [%d] %s", uds_com[com].desc, env->data->len, env->hexString) #endif uint8_t response_size = uds_com[com].func(env); if (response_size) { #if (LOG_UDS == 1) sendLogCanUdsHex(env, env->dataResponse, response_size); - LoggerFormatInfo(LOGGER, LOG_SIGN, "< %s [%d] %s", uds_com[com].desc, response_size, env->hexString) + LoggerFormatInfo(LOGGER, LOG_SIGN, "< %s [%d] %s", uds_com[com].desc, response_size, env->hexString) #endif CanSerialPortFrameTpTransmit(&env->canSerialPortFrameTp, env->dataResponse, response_size, PROTOCOL_CAN_ADR_UDS, WAIT_FRAME_WRITE); @@ -1748,7 +1829,8 @@ void CanUds(tCanUds *env) { env->currentSessionTesterPresentTimeout = 0; #if (LOG_UDS == 1) - LoggerFormatTrace(LOGGER, LOG_SIGN, "Resetting the session to default: %d", UDS_session_defaultSession) + LoggerFormatInfo(LOGGER, LOG_SIGN, "Resetting the session to default: %d", + UDS_session_defaultSession) #endif setDefaultSecurityAccess(env); set_CCU_Mute(env->canSpamTransmitter, false); diff --git a/CanUds.h b/CanUds.h index 26a0fac..a20aaa1 100644 --- a/CanUds.h +++ b/CanUds.h @@ -11,12 +11,10 @@ #include "CanSpamTransmitter.h" #include "AdcTasks.h" -#define LOG_UDS 0 +#define LOG_UDS 1 //#define CAN_US_QUEUE_SIZE 1 -#define DownloadFirmwareSize (512 * 1024) - #define MAX_ATTEMPTS_DEFAULT 3 #define BLOCK_TIME_DEFAULT 60000 @@ -137,6 +135,12 @@ typedef enum { UDS_routine_RoutineFinishSuccess = 3 } eUdsRequestRoutineResult; +typedef enum { + UDS_BlockMain = 0, + UDS_BlockCalib = 1, + UDS_BlockInit = 0xff +} eUdsBlock; + typedef struct { eUdsServices service; uint8_t sub_function; @@ -190,6 +194,17 @@ typedef struct { uint32_t AdrFlash; uint32_t SizeWriteFlash; uint8_t blockSequenceCounter; + uint32_t DownloadFirmwareSize; + eUdsBlock udsBlock; + + uint32_t BlockMainAdr1; + uint32_t BlockMainSize1; + uint32_t BlockMainAdr2; + uint32_t BlockMainSize2; + + uint32_t BlockCalibAdr1; + uint32_t BlockCalibSize1; + eUdsServices stateDownload; diff --git a/crc32_uds.c b/crc32_uds.c index 44079b4..a3238e2 100644 --- a/crc32_uds.c +++ b/crc32_uds.c @@ -51,16 +51,13 @@ static const uint32_t crc_table[256] = { }; -/** - * @brief Вычисляет CRC32 (IEEE 802.3) для блока данных. - * @param data Указатель на массив байт данных. - * @param len Длина массива данных в байтах. - * @return 32-битное значение CRC. - */ -uint32_t UdsCrc32(const uint8_t *data, uint32_t len) { - uint32_t crc = 0xFFFFFFFF; // Стандартное начальное значение +uint32_t UdsCrc32Update(uint32_t crc, const uint8_t *data, uint32_t len) { for (uint32_t i = 0; i < len; ++i) { crc = (crc >> 8) ^ crc_table[(crc ^ data[i]) & 0xFF]; } - return crc ^ 0xFFFFFFFF; // Финальное XOR-преобразование + return crc; // Без финальной инверсии +} + +uint32_t UdsCrc32Finalize(uint32_t crc) { + return crc ^ 0xFFFFFFFF; } \ No newline at end of file diff --git a/crc32_uds.h b/crc32_uds.h index ff2a7dd..25cd7da 100644 --- a/crc32_uds.h +++ b/crc32_uds.h @@ -6,6 +6,7 @@ #define HVAC_M7_CRC32_UDS_H #include "stdint.h" -uint32_t UdsCrc32(const uint8_t *data, uint32_t len); +uint32_t UdsCrc32Update(uint32_t crc, const uint8_t *data, uint32_t len); +uint32_t UdsCrc32Finalize(uint32_t crc); #endif //HVAC_M7_CRC32_UDS_H