Обновление платы на V2

This commit is contained in:
cfif 2026-05-26 15:46:02 +03:00
parent 4231b5761e
commit 767ca4cf94
4 changed files with 134 additions and 39 deletions

120
CanUds.c
View File

@ -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) {
@ -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,18 +1336,64 @@ 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) {
if (com->dataFormatIdentifier == 0) {
env->udsBlock = UDS_BlockMain;
}
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->memorySize != 0x80000) {
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) {
return setResponseError(env, UDS_RoutineControl, UDS_error_conditionsNotCorrect);
}
@ -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;
@ -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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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