fix: Implement error handling for CAN

stable
Rakshitavecmocon 2024-10-07 18:32:06 +05:30
parent 85a15dc97f
commit 17131c3a34
4 changed files with 72 additions and 11 deletions

View File

@ -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_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_rules.mk=UTF-8
encoding//Debug/ivec_ECU/ivec_ecu_uart/src/subdir_vars.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/makefile=UTF-8
encoding//Debug/objects.mk=UTF-8 encoding//Debug/objects.mk=UTF-8
encoding//Debug/sources.mk=UTF-8 encoding//Debug/sources.mk=UTF-8

View File

@ -21,6 +21,7 @@ extern uint16_t idx;
uint32_t IntrStatus; uint32_t IntrStatus;
static volatile uint8_t u8_MCAN_StatusFlag = IVEC_MCAL_STATUS_ERROR; static volatile uint8_t u8_MCAN_StatusFlag = IVEC_MCAL_STATUS_ERROR;
uint8_t g_CanData[8]; uint8_t g_CanData[8];
volatile DL_MCAN_ProtocolStatus HeaderStat;
/*REQUIRED MCAN CONFIGS*/ /*REQUIRED MCAN CONFIGS*/
@ -272,6 +273,23 @@ static void ErrorHandler()
void CANFD0_IRQHandler(void) void CANFD0_IRQHandler(void)
{ {
IntrStatus = DL_MCAN_getIntrStatus(CANFD0); 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){ if (IntrStatus & DL_MCAN_INTERRUPT_TC){
__asm("nop"); __asm("nop");
@ -323,7 +341,7 @@ void CANFD0_IRQHandler(void)
DL_MCAN_getIntrStatus(CANFD0); DL_MCAN_getIntrStatus(CANFD0);
DL_MCAN_clearIntrStatus(CANFD0, IntrStatus,DL_MCAN_INTR_SRC_MCAN_LINE_1); 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(MCAN == CANFD0);
assert(b_MCAN_InitFlag != 0); assert(b_MCAN_InitFlag != 0);
DL_MCAN_disableInterrupt(MCAN, (DL_MCAN_MSP_INTERRUPT_LINE1));
NVIC_DisableIRQ(CANFD0_INT_IRQn);
DL_MCAN_disablePower(MCAN); DL_MCAN_disablePower(MCAN);
b_MCAN_InitFlag = 0; 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(MCAN == CANFD0);
assert(b_MCAN_InitFlag != 0); assert(b_MCAN_InitFlag != 0);
int result = -1;
for(int i=0;i<Bytes;i++) for(int i=0;i<Bytes;i++)
{ {
@ -460,13 +481,23 @@ IVEC_McalStatus_e xMCAL_MCANTx(MCAN_Regs *MCAN, DL_MCAN_TxBufElement *TxMsg ,uin
// uint32_t txPendingStatus; // uint32_t txPendingStatus;
// //
// //Check if the Tx Buffer is available (no pending request) // //Check if the Tx Buffer is available (no pending request)
//txPendingStatus = DL_MCAN_getTxBufReqPend(MCAN); // txPendingStatus = DL_MCAN_getTxBufReqPend(MCAN);
//
// Check if the specified buffer (BufNum) is available // //Check if the specified buffer (BufNum) is available
// if ((txPendingStatus & (1 << BufNum)) == 0) // if ((txPendingStatus & (1 << BufNum)) == 0)
// { // {
DL_MCAN_writeMsgRam(MCAN, DL_MCAN_MEM_TYPE_BUF, BufNum , TxMsg); DL_MCAN_writeMsgRam(MCAN, DL_MCAN_MEM_TYPE_BUF, BufNum , TxMsg);
DL_MCAN_TXBufAddReq(MCAN, BufNum); result = DL_MCAN_TXBufAddReq(MCAN, BufNum);
if(result == 0)
{
printf("pass\n");
}
else
{
printf("result %d\n",result);
printf("fail\n");
}
// } // }
// else // else
// { // {

View File

@ -48,17 +48,23 @@ IVEC_EcuCommonErr_e xECU_WriteDataOverCAN(uint8_t* pucBuf, uint32_t ulId, int re
/* Message Marker. */ /* Message Marker. */
xFrame.mm = 0xAAU; xFrame.mm = 0xAAU;
uint16_t TxData[DL_MCAN_MAX_PAYLOAD_BYTES]; // Define a buffer for the CAN payload data uint8_t TxData[DL_MCAN_MAX_PAYLOAD_BYTES]; // Define a buffer for the CAN payload data
//
// // Copy the data you want to transmit (from pucBuf) into TxData
// //memcpy(TxData, &pucBuf[PKT_HEADER], retCode);
//
// Loop through pucBuf starting at PKT_HEADER and XOR each byte with 0x0000
for (int i = 0; i < retCode; i++) {
TxData[i] = pucBuf[PKT_HEADER + i];
}
// Copy the data you want to transmit (from pucBuf) into TxData
memcpy(TxData, &pucBuf[PKT_HEADER], retCode);
// Set up the buffer number // Set up the buffer number
//uint32_t BufNum = 0; // Example: Use buffer number 0 //uint32_t BufNum = 0; // Example: Use buffer number 0
// Number of bytes to transmit (retCode holds DLC) // Number of bytes to transmit (retCode holds DLC)
int Bytes = retCode; int Bytes = retCode;
printf("bufnum %d\n",BufNum);
l_i32Ret = xMCAL_MCANTx(CANFD0, &xFrame, TxData, BufNum, Bytes); l_i32Ret = xMCAL_MCANTx(CANFD0, &xFrame, TxData, BufNum, Bytes);
if(l_i32Ret == IVEC_MCAL_STATUS_SUCCESS) if(l_i32Ret == IVEC_MCAL_STATUS_SUCCESS)
{ {
@ -95,6 +101,7 @@ IVEC_EcuCommonErr_e xECU_CanReInit(MCAN_Regs* MCAN,uint16_t speed)
l_xFuncStatus = commonECU_DEINIT_FAIL; l_xFuncStatus = commonECU_DEINIT_FAIL;
} }
//vCanConfigFilter(); //vCanConfigFilter();
DL_UART_Main_enablePower(MCAN);
l_i32Ret = xMCAL_MCANInit(MCAN,speed); l_i32Ret = xMCAL_MCANInit(MCAN,speed);
if(l_i32Ret != IVEC_MCAL_STATUS_SUCCESS) if(l_i32Ret != IVEC_MCAL_STATUS_SUCCESS)
{ {

25
main.c
View File

@ -132,7 +132,7 @@ DL_MCAN_RxBufElement RxMsg;
flashStatus_t ret_UID,ret_MID; flashStatus_t ret_UID,ret_MID;
McalUartHandle_s prvUartHandle; McalUartHandle_s prvUartHandle;
static volatile int count = 0;
void UARTDataProcess() void UARTDataProcess()
{ {
@ -141,6 +141,7 @@ void UARTDataProcess()
uint32_t ulId = 0xffffffff; uint32_t ulId = 0xffffffff;
retCode= xECU_ReadCANDataOverUART(&prvUartHandle,pucBuf,&ulId); retCode= xECU_ReadCANDataOverUART(&prvUartHandle,pucBuf,&ulId);
printf("length %d\n",retCode);
if(retCode > -1) if(retCode > -1)
{ {
if(ulId == 0x00) if(ulId == 0x00)
@ -148,7 +149,26 @@ void UARTDataProcess()
vECU_InitiateUartToCanTransmit(&prvUartHandle, ulId, pucBuf, 0); vECU_InitiateUartToCanTransmit(&prvUartHandle, ulId, pucBuf, 0);
} }
else{ 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); //delay(3200000);
} }
int main(void) int main(void)
{ {
xMCAL_McuInit(); xMCAL_McuInit();