From 17131c3a34d0fb381d4b4525b2205eb3416013c8 Mon Sep 17 00:00:00 2001 From: Rakshitavecmocon Date: Mon, 7 Oct 2024 18:32:06 +0530 Subject: [PATCH] fix: Implement error handling for CAN --- .settings/org.eclipse.core.resources.prefs | 2 ++ Core/Source/ivec_mcal_mcan.c | 41 +++++++++++++++++++--- ivec_ECU/ivec_ecu_can/src/ivec_ecu_can.c | 15 +++++--- main.c | 25 +++++++++++-- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 879d2ad..d79a71c 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -19,6 +19,8 @@ encoding//Debug/ivec_ECU/ivec_ecu_common/src/subdir_rules.mk=UTF-8 encoding//Debug/ivec_ECU/ivec_ecu_common/src/subdir_vars.mk=UTF-8 encoding//Debug/ivec_ECU/ivec_ecu_uart/src/subdir_rules.mk=UTF-8 encoding//Debug/ivec_ECU/ivec_ecu_uart/src/subdir_vars.mk=UTF-8 +encoding//Debug/ivec_cmplx_gptimer/src/subdir_rules.mk=UTF-8 +encoding//Debug/ivec_cmplx_gptimer/src/subdir_vars.mk=UTF-8 encoding//Debug/makefile=UTF-8 encoding//Debug/objects.mk=UTF-8 encoding//Debug/sources.mk=UTF-8 diff --git a/Core/Source/ivec_mcal_mcan.c b/Core/Source/ivec_mcal_mcan.c index 0c957a3..5cbb22a 100644 --- a/Core/Source/ivec_mcal_mcan.c +++ b/Core/Source/ivec_mcal_mcan.c @@ -21,6 +21,7 @@ extern uint16_t idx; uint32_t IntrStatus; static volatile uint8_t u8_MCAN_StatusFlag = IVEC_MCAL_STATUS_ERROR; uint8_t g_CanData[8]; +volatile DL_MCAN_ProtocolStatus HeaderStat; /*REQUIRED MCAN CONFIGS*/ @@ -272,6 +273,23 @@ static void ErrorHandler() void CANFD0_IRQHandler(void) { IntrStatus = DL_MCAN_getIntrStatus(CANFD0); + DL_MCAN_getProtocolStatus(CANFD0, &HeaderStat); + + if(HeaderStat.busOffStatus==1) + { + DL_MCAN_setOpMode(CANFD0, DL_MCAN_OPERATION_MODE_NORMAL); + + } + else if (HeaderStat.lastErrCode == 4) { + // Initiate bus-off recovery + int status = -1; + printf("Attempting recovery...\n"); + xECU_CanReInit(CANFD0 ,BAUD_500); + if(status == 0) + { + printf("done\n"); + } + } if (IntrStatus & DL_MCAN_INTERRUPT_TC){ __asm("nop"); @@ -323,7 +341,7 @@ void CANFD0_IRQHandler(void) DL_MCAN_getIntrStatus(CANFD0); DL_MCAN_clearIntrStatus(CANFD0, IntrStatus,DL_MCAN_INTR_SRC_MCAN_LINE_1); } - ErrorHandler(); + //ErrorHandler(); } @@ -431,6 +449,8 @@ IVEC_McalStatus_e xMCAL_MCANDeInit(MCAN_Regs* MCAN) { assert(MCAN == CANFD0); assert(b_MCAN_InitFlag != 0); + DL_MCAN_disableInterrupt(MCAN, (DL_MCAN_MSP_INTERRUPT_LINE1)); + NVIC_DisableIRQ(CANFD0_INT_IRQn); DL_MCAN_disablePower(MCAN); b_MCAN_InitFlag = 0; @@ -451,6 +471,7 @@ IVEC_McalStatus_e xMCAL_MCANTx(MCAN_Regs *MCAN, DL_MCAN_TxBufElement *TxMsg ,uin { assert(MCAN == CANFD0); assert(b_MCAN_InitFlag != 0); + int result = -1; for(int i=0;i -1) { if(ulId == 0x00) @@ -148,7 +149,26 @@ void UARTDataProcess() vECU_InitiateUartToCanTransmit(&prvUartHandle, ulId, pucBuf, 0); } else{ - xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 0); + if(count == 0){ + xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 0); + printf("%d\n",count); + count = 1; + } + else if (count == 1){ + xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 1); + printf("%d\n",count); + count = 2; + } + else if (count == 2){ + xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 2); + printf("%d\n",count); + count = 3; + } + else{ + xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 3); + printf("%d\n",count); + count = 0; + } } } @@ -165,6 +185,7 @@ void CANDataProcess() //delay(3200000); } + int main(void) { xMCAL_McuInit();