diff --git a/CanUds.c b/CanUds.c index 32d6662..b64c05c 100644 --- a/CanUds.c +++ b/CanUds.c @@ -15,6 +15,8 @@ #include "DownloadFile.h" #include "crc32_uds.h" #include "EraseMemory.h" +#include "InternalFlashPage.h" +#include "BootJump.h" #define LOG_SIGN "CAN_UDS" #define LOGGER env->logger @@ -994,12 +996,8 @@ static uint16_t vUDS_routine_Compare_Checksum(tCanUds *env, eUdsRoutineControlTy setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); -// uint32_t crc32_calc = UdsCrc32Update(0xFFFFFFFF, (uint8_t *) env->CurrentBlockAdr, env->CurrentBlockSize, -// SendUpdatePendingCrc, env, 200); -// crc32_calc = UdsCrc32Finalize(crc32_calc); - uint32_t crc32_calc = UdsCrc32Update_Hardware(0xFFFFFFFF, (uint8_t *) env->CurrentBlockAdr, env->CurrentBlockSize, - SendUpdatePendingCrc, env, 200); + SendUpdatePendingCrc, env, 200); uint32_t crc32_received = (env->data->data[4] << 24) | (env->data->data[5] << 16) | (env->data->data[6] << 8) | env->data->data[7]; @@ -1014,9 +1012,12 @@ static uint16_t vUDS_routine_Compare_Checksum(tCanUds *env, eUdsRoutineControlTy return 4 + 1; } +#include "fc7xxx_driver_fmc.h" + static uint16_t vUDS_routine_Erase_Memory(tCanUds *env, eUdsRoutineControlType udsRoutineControlType) { + if (udsRoutineControlType != UDS_routine_StartRoutine) { return 0xFF00 | UDS_error_sub_functionNotSupported; } @@ -1031,7 +1032,9 @@ static uint16_t vUDS_routine_Erase_Memory(tCanUds *env, eUdsRoutineControlType u env->udsBlock = UDS_BlockInit; - if (eraseMemory->memoryAddress == ADR_HEX_BlockMain) { + if (eraseMemory->memoryAddress == ADR_HEX_BlockBoot) { + env->udsBlock = UDS_BlockBoot; + } else if (eraseMemory->memoryAddress == ADR_HEX_BlockMain) { env->udsBlock = UDS_BlockMain; } else if (eraseMemory->memoryAddress == ADR_HEX_BlockCalib) { env->udsBlock = UDS_BlockCalib; @@ -1043,38 +1046,53 @@ static uint16_t vUDS_routine_Erase_Memory(tCanUds *env, eUdsRoutineControlType u setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); - bool result1 = false; - bool result2 = false; + if (env->udsBlock == UDS_BlockBoot) { + env->timeoutPendingClear = SystemGetMs() + 200; + bool result1 = env->clear_flash_func(ADR_HEX_Mapped_BlockBoot, SIZE_HEX_BlockBoot, SendUpdatePendingFlashClear, + env); + + if (result1) { + env->dataResponse[4] = UDS_routine_RoutineStartStopSuccess; + + return 4 + 1; + } + } if (env->udsBlock == UDS_BlockMain) { env->timeoutPendingClear = SystemGetMs() + 200; - result1 = env->clear_flash_func(ADR_HEX_Mapped_BlockMain, (SIZE_HEX_BlockMain / 2), SendUpdatePendingFlashClear, env); + bool result1 = env->clear_flash_func(ADR_HEX_Mapped_BlockMain, (SIZE_HEX_BlockMain / 2), + SendUpdatePendingFlashClear, env); setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); - result2 = env->clear_flash_func(ADR_HEX_Mapped_BlockMain + (SIZE_HEX_BlockMain / 2), (SIZE_HEX_BlockMain / 2), SendUpdatePendingFlashClear, env); + bool result2 = env->clear_flash_func(ADR_HEX_Mapped_BlockMain + (SIZE_HEX_BlockMain / 2), + (SIZE_HEX_BlockMain / 2), SendUpdatePendingFlashClear, env); + + if ((result1) && (result2)) { + env->dataResponse[4] = UDS_routine_RoutineStartStopSuccess; + + return 4 + 1; + } + } if (env->udsBlock == UDS_BlockCalib) { env->timeoutPendingClear = SystemGetMs() + 200; - result1 = env->clear_flash_func(ADR_HEX_Mapped_BlockCalib, SIZE_HEX_BlockCalib, SendUpdatePendingFlashClear, env); + bool result1 = env->clear_flash_func(ADR_HEX_Mapped_BlockCalib, SIZE_HEX_BlockCalib, + SendUpdatePendingFlashClear, env); + + if (result1) { + env->dataResponse[4] = UDS_routine_RoutineStartStopSuccess; + + return 4 + 1; + } } -// bool result = FLASHDRIVER_FlashEraseBlock(FLASH_BLOCK_SELECT1); - - if ((result1) || (result2)) { - env->dataResponse[4] = - UDS_routine_RoutineStartStopSuccess; - } else { - env->dataResponse[4] = - UDS_routine_RoutineStartStopFailure; - } + env->dataResponse[4] = UDS_routine_RoutineStartStopFailure; return 4 + 1; } -extern const char version_id[]; - static uint16_t vUDS_routine_Check_Programming_Dependancies(tCanUds *env, eUdsRoutineControlType udsRoutineControlType) { @@ -1082,24 +1100,13 @@ vUDS_routine_Check_Programming_Dependancies(tCanUds *env, eUdsRoutineControlType return 0xFF00 | UDS_error_sub_functionNotSupported; } -// char *version_id_main = (char *) (ADR_HEX_BlockMain + 0x400); -// int result = memcmp(version_id_main, version_id, 9); -// if (result != 0) { -// LoggerErrorStatic(LOGGER, LOG_SIGN, "Software incompatibility"); -// env->dataResponse[4] = 3; -// return 4 + 1; -// } - - setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); -// uint32_t crc32_calc = UdsCrc32Update(0xFFFFFFFF, (uint8_t *) ADR_HEX_Mapped_BlockMain, SIZE_HEX_BlockMain - 4, -// SendUpdatePendingCrc, env, 200); -// crc32_calc = UdsCrc32Finalize(crc32_calc); - uint32_t crc32_calc = UdsCrc32Update_Hardware(0xFFFFFFFF, (uint8_t *) ADR_HEX_Mapped_BlockMain, SIZE_HEX_BlockMain - 4, - SendUpdatePendingCrc, env, 200); + uint32_t crc32_calc = UdsCrc32Update_Hardware(0xFFFFFFFF, (uint8_t *) ADR_HEX_Mapped_BlockBoot, + SIZE_HEX_BlockBoot - 4, + SendUpdatePendingCrc, env, 200); - uint32_t crc32_file = (*(uint32_t *) (ADR_HEX_Mapped_BlockMain + SIZE_HEX_BlockMain - 4)); + uint32_t crc32_file = (*(uint32_t *) (ADR_HEX_Mapped_BlockBoot + SIZE_HEX_BlockBoot - 4)); if (crc32_calc != crc32_file) { env->dataResponse[4] = UDS_routine_RoutineStartStopFailure; @@ -1108,12 +1115,20 @@ vUDS_routine_Check_Programming_Dependancies(tCanUds *env, eUdsRoutineControlType } setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); -// crc32_calc = UdsCrc32Update(0xFFFFFFFF, (uint8_t *) ADR_HEX_Mapped_BlockCalib, SIZE_HEX_BlockCalib - 4, -// SendUpdatePendingCrc, env, 200); -// crc32_calc = UdsCrc32Finalize(crc32_calc); + crc32_calc = UdsCrc32Update_Hardware(0xFFFFFFFF, (uint8_t *) ADR_HEX_Mapped_BlockMain, SIZE_HEX_BlockMain - 4, + SendUpdatePendingCrc, env, 200); + crc32_file = (*(uint32_t *) (ADR_HEX_Mapped_BlockMain + SIZE_HEX_BlockMain - 4)); + + if (crc32_calc != crc32_file) { + env->dataResponse[4] = UDS_routine_RoutineStartStopFailure; + + return 4 + 1; + } + + setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); crc32_calc = UdsCrc32Update_Hardware(0xFFFFFFFF, (uint8_t *) ADR_HEX_Mapped_BlockCalib, SIZE_HEX_BlockCalib - 4, - SendUpdatePendingCrc, env, 200); + SendUpdatePendingCrc, env, 200); crc32_file = (*(uint32_t *) (ADR_HEX_Mapped_BlockCalib + SIZE_HEX_BlockCalib - 4)); @@ -1123,6 +1138,32 @@ vUDS_routine_Check_Programming_Dependancies(tCanUds *env, eUdsRoutineControlType return 4 + 1; } + FMC_API_ACTIVE_BLOCK_TYPE ActiveBank = FMCDRIVER_GetActiveBlock(FMC_INSTANCE_1); + + uint64_t NumberBank; + + if (ActiveBank == FMC_Active0) { + NumberBank = 1; + } else { + NumberBank = 0; + } + + bool result = D_bInternalFlashPage_Clear(0x04000000, NULL, NULL); + + if (result == false) { + env->dataResponse[4] = UDS_routine_RoutineStartStopFailure; + + return 4 + 1; + } + + size_t size = D_sInternalFlashPage_Write(0x04000000, 0, (uint8_t *) &NumberBank, 8); + + if (size != 8) { + env->dataResponse[4] = UDS_routine_RoutineStartStopFailure; + + return 4 + 1; + } + env->dataResponse[4] = UDS_routine_RoutineStartStopSuccess; return 4 + 1; @@ -1419,6 +1460,10 @@ static uint16_t RequestDownload_34(tCanUds *env) { env->udsBlock = UDS_BlockInit; + if (com->memoryAddress == ADR_HEX_BlockBoot) { + env->udsBlock = UDS_BlockBoot; + } + if (com->memoryAddress == ADR_HEX_BlockMain) { env->udsBlock = UDS_BlockMain; } @@ -1431,10 +1476,20 @@ static uint16_t RequestDownload_34(tCanUds *env) { return setResponseError(env, UDS_RequestDownload, UDS_error_requestSequenceError); } - env->AdrFlash = com->memoryAddress + 0x00100000 - (128 * 1024); + FMC_API_ACTIVE_BLOCK_TYPE activeBlock = FMCDRIVER_GetActiveBlock(FMC_INSTANCE_1); + + env->AdrFlash = com->memoryAddress + 0x00100000; + env->SizeWriteFlash = 0; env->DownloadFirmwareSize = com->memorySize; + if (env->udsBlock == UDS_BlockBoot) { + + if (com->memorySize > SIZE_HEX_BlockBoot) { + return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); + } + } + if (env->udsBlock == UDS_BlockMain) { if (com->memorySize > SIZE_HEX_BlockMain) { @@ -1466,7 +1521,7 @@ static uint16_t RequestDownload_34(tCanUds *env) { requestDownload_Response->ServiceId = UDS_RequestDownload | 0b1000000; requestDownload_Response->lengthFormatIdentifier = 0x20; // 2 байта - длина поля maxNumberOfBlockLength - uint8_t countBlock = 8;//8; + uint8_t countBlock = 8;//31; requestDownload_Response->maxNumberOfBlockLength = countBlock * 128 + 2; @@ -1853,7 +1908,7 @@ void ReceivedTP_func(void *arg, tCanTP_data *data) { if (uds_com[com].func != NULL) { // if (osMutexAcquire(env->access, 1000) == osOK) { - env->currentSessionTesterPresentTimeout = SystemGetMs() + timeout_session_S3_Server; + env->currentSessionTesterPresentTimeout = SystemGetMs() + timeout_session_S3_Server; // osMutexRelease(env->access); // } else { // LoggerErrorStatic(LOGGER, LOG_SIGN, "Access error ReceivedTP_func"); @@ -1888,8 +1943,9 @@ void ReceivedTP_func(void *arg, tCanTP_data *data) { } if (env->isEcuReset) { -// SystemDelayMs(100); - NVIC_SystemReset(); + + Reset(); + //NVIC_SystemReset(); } } diff --git a/CanUds.h b/CanUds.h index a75bbd5..688557a 100644 --- a/CanUds.h +++ b/CanUds.h @@ -20,14 +20,17 @@ #define timeout_session_S3_Server 7000 -#define ADR_HEX_BlockMain 0x01020000 -#define ADR_HEX_BlockCalib 0x010D0000 +#define ADR_HEX_BlockBoot 0x01000000 +#define ADR_HEX_BlockMain 0x01010000 +#define ADR_HEX_BlockCalib 0x010C0000 -#define ADR_HEX_Mapped_BlockMain 0x01100000 -#define ADR_HEX_Mapped_BlockCalib 0x011B0000 +#define ADR_HEX_Mapped_BlockBoot 0x01100000 +#define ADR_HEX_Mapped_BlockMain 0x01110000 +#define ADR_HEX_Mapped_BlockCalib 0x011C0000 +#define SIZE_HEX_BlockBoot (64 * 1024) #define SIZE_HEX_BlockMain (704 * 1024) -#define SIZE_HEX_BlockCalib (192 * 1024) +#define SIZE_HEX_BlockCalib (256 * 1024) #define LEN_DEBUG_UDS_BUFF 128 @@ -147,8 +150,9 @@ typedef enum { } eUdsRequestRoutineResult; typedef enum { - UDS_BlockMain = 0, - UDS_BlockCalib = 1, + UDS_BlockBoot = 0, + UDS_BlockMain = 1, + UDS_BlockCalib = 2, UDS_BlockInit = 0xff } eUdsBlock;