From 718fc19753380dc40fed05eef4bf66223679678c Mon Sep 17 00:00:00 2001 From: Rakshitavecmocon Date: Tue, 5 Nov 2024 13:12:57 +0530 Subject: [PATCH] feat: Apply CAN filters for both standard and extended IDs - Standard ID filters: - Filter 1: Allow IDs from 100 to 200 - Filter 2: Allow IDs from 300 to 400 - Extended ID filters: - Filter 1: Allow IDs from 3000 to 3500 - Filter 2: Allow IDs from 4000 to 4500 --- .settings/org.eclipse.core.resources.prefs | 4 +- Core/Source/ivec_mcal_mcan.c | 37 +++-- ivec_RTE/src/ivec_rte.c | 180 ++++++++++++++++++++- 3 files changed, 202 insertions(+), 19 deletions(-) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 970a25f..651c549 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -38,6 +38,8 @@ encoding//Debug/SDK/drivers/Src/timers/subdir_rules.mk=UTF-8 encoding//Debug/SDK/drivers/Src/timers/subdir_vars.mk=UTF-8 encoding//Debug/TM1650_SDK/src/subdir_rules.mk=UTF-8 encoding//Debug/TM1650_SDK/src/subdir_vars.mk=UTF-8 +encoding//Debug/ivec_APP/ivec_cmplx_gptimer/src/subdir_rules.mk=UTF-8 +encoding//Debug/ivec_APP/ivec_cmplx_gptimer/src/subdir_vars.mk=UTF-8 encoding//Debug/ivec_APP/src/subdir_rules.mk=UTF-8 encoding//Debug/ivec_APP/src/subdir_vars.mk=UTF-8 encoding//Debug/ivec_ECU/ivec_ecu_can/src/subdir_rules.mk=UTF-8 @@ -48,8 +50,6 @@ 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_RTE/src/subdir_rules.mk=UTF-8 encoding//Debug/ivec_RTE/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 9716823..79372aa 100644 --- a/Core/Source/ivec_mcal_mcan.c +++ b/Core/Source/ivec_mcal_mcan.c @@ -25,7 +25,7 @@ volatile uint32_t l_canTransmitTimeout; volatile uint8_t u8CallBack_buff[8]={0}; volatile IVEC_McalStatus_e l_statusFlag = IVEC_MCAL_STATUS_SUCCESS; extern volatile uint8_t u8CANDataAct; -#define MCAN_FILTER_SIZE 0u +#define MCAN_FILTER_SIZE 2u /*REQUIRED MCAN CONFIGS*/ @@ -72,10 +72,10 @@ static DL_MCAN_ConfigParams gMCAN0ConfigParams={ .timeoutCntEnable = false, .filterConfig.rrfs = false, .filterConfig.rrfe = false, - .filterConfig.anfe = 0, - .filterConfig.anfs = 0, -// .filterConfig.anfe = 3, -// .filterConfig.anfs = 3, +// .filterConfig.anfe = 0, +// .filterConfig.anfs = 0, + .filterConfig.anfe = 3, + .filterConfig.anfs = 3, }; @@ -137,7 +137,7 @@ static DL_MCAN_MsgRAMConfigParams gMCAN0MsgRAMConfigParams ={ /* Extended ID Filter List Start Address. */ .flesa = 48 , /* List Size: Extended ID. */ - .lse = 0 , + .lse = 2 , /* Tx Buffers Start Address. */ .txStartAddr = 10 , /* Number of Dedicated Transmit Buffers. */ @@ -218,11 +218,18 @@ static DL_MCAN_BitTimingParams gMCAN0BitTimes_250 = { static const DL_MCAN_StdMsgIDFilterElement gMCAN0StdFiltelem1 = { .sfec = 001, - .sft = 10, + .sft = 0x00, .sfid1 = 0, .sfid2 = 100, }; +//static const DL_MCAN_StdMsgIDFilterElement gMCAN0StdFiltelem1 = { +// .sfec = 001, +// .sft = 10, +// .sfid1 = 0xc8, +// .sfid2 = 0x7fe, +//}; // apply mask pass only 200 and 201 id + static const DL_MCAN_StdMsgIDFilterElement gMCAN0StdFiltelem2 = { .sfec = 0x1, .sft = 0x00, @@ -254,15 +261,15 @@ static const DL_MCAN_StdMsgIDFilterElement gMCAN0StdFiltelem5 = { static const DL_MCAN_ExtMsgIDFilterElement gMCAN0ExtFiltelem1 = { .efec = 0x1, .eft = 0x0, - .efid1 = 30000, - .efid2 = 40000, + .efid1 = 3000, + .efid2 = 3500, }; static const DL_MCAN_ExtMsgIDFilterElement gMCAN0ExtFiltelem2 = { .efec = 0x1, .eft = 0x0, - .efid1 = 0x1CECFF23, - .efid2 = 0x1CECFF23, + .efid1 = 4000, + .efid2 = 4500, }; static const DL_MCAN_ExtMsgIDFilterElement gMCAN0ExtFiltelem3 = { @@ -460,9 +467,9 @@ IVEC_McalStatus_e xMCAL_MCANInit(MCAN_Regs* MCAN, xCAN_baud_t BAUD) DL_MCAN_msgRAMConfig(MCAN, (DL_MCAN_MsgRAMConfigParams*) &gMCAN0MsgRAMConfigParams); /* Configure Standard ID filter element */ - // DL_MCAN_addStdMsgIDFilter(MCAN, 0U, (DL_MCAN_StdMsgIDFilterElement *) &gMCAN0StdFiltelem1); + DL_MCAN_addStdMsgIDFilter(MCAN, 0U, (DL_MCAN_StdMsgIDFilterElement *) &gMCAN0StdFiltelem1); // - // DL_MCAN_addStdMsgIDFilter(MCAN, 1U, (DL_MCAN_StdMsgIDFilterElement *) &gMCAN0StdFiltelem2); + DL_MCAN_addStdMsgIDFilter(MCAN, 1U, (DL_MCAN_StdMsgIDFilterElement *) &gMCAN0StdFiltelem2); // // DL_MCAN_addStdMsgIDFilter(MCAN, 2U, (DL_MCAN_StdMsgIDFilterElement *) &gMCAN0StdFiltelem3); // @@ -470,9 +477,9 @@ IVEC_McalStatus_e xMCAL_MCANInit(MCAN_Regs* MCAN, xCAN_baud_t BAUD) // // DL_MCAN_addStdMsgIDFilter(MCAN, 4U, (DL_MCAN_StdMsgIDFilterElement *) &gMCAN0StdFiltelem5); // - // DL_MCAN_addExtMsgIDFilter(MCAN, 0U,(DL_MCAN_ExtMsgIDFilterElement *)&gMCAN0ExtFiltelem1); + DL_MCAN_addExtMsgIDFilter(MCAN, 0U,(DL_MCAN_ExtMsgIDFilterElement *)&gMCAN0ExtFiltelem1); // -// DL_MCAN_addExtMsgIDFilter(MCAN, 1U,(DL_MCAN_ExtMsgIDFilterElement *)&gMCAN0ExtFiltelem2); + DL_MCAN_addExtMsgIDFilter(MCAN, 1U,(DL_MCAN_ExtMsgIDFilterElement *)&gMCAN0ExtFiltelem2); // // DL_MCAN_addExtMsgIDFilter(MCAN, 2U,(DL_MCAN_ExtMsgIDFilterElement *)&gMCAN0ExtFiltelem3); diff --git a/ivec_RTE/src/ivec_rte.c b/ivec_RTE/src/ivec_rte.c index d5c5e2e..88ffbb1 100644 --- a/ivec_RTE/src/ivec_rte.c +++ b/ivec_RTE/src/ivec_rte.c @@ -18,11 +18,68 @@ McalUartHandle_s g_xUartHandle; uint32_t g_u32UartSpeed = 0; uint16_t g_u16CanSpeed = 0; +#define MAX_FILTERS 10 +uint32_t maskValues[MAX_FILTERS]; +uint32_t filterValues[MAX_FILTERS]; +bool isExtendedID[MAX_FILTERS]; + +uint32_t maskCount = 0; +// Buffers to store parsed data +uint32_t filterCount = 0; + static uint8_t __gprv_u8Idx = 0; extern ExtU_socTouchDisplay_T socTouchDisplay_U; extern ExtY_socTouchDisplay_T socTouchDisplay_Y; + +// New configuration instance to override filter settings dynamically +static DL_MCAN_ConfigParams gMCAN0ConfigParamsFiltered = { + .monEnable = false, + .asmEnable = false, + .tsPrescalar = 15, + .tsSelect = 0, + .timeoutSelect = DL_MCAN_TIMEOUT_SELECT_CONT, + .timeoutPreload = 65535, + .timeoutCntEnable = false, + // Dynamic filter settings based on need + .filterConfig.rrfs = false, + .filterConfig.rrfe = false, + .filterConfig.anfe = 3, // Accept extended ID filter elements + .filterConfig.anfs = 3, // Accept standard ID filter elements +}; + +// Adjust MsgRAMConfigParams as necessary for filter list sizes +static DL_MCAN_MsgRAMConfigParams gMCAN0MsgRAMConfigParamsFiltered = { + .flssa = 1, + .lss = 0, // Adjust according to actual standard filter count + .flesa = 48, + .lse = 0, // Adjust according to actual extended filter count + .txStartAddr = 10, + .txBufNum = 10, + .txFIFOSize = 10, + .txBufMode = 0, + .txBufElemSize = DL_MCAN_ELEM_SIZE_8BYTES, + .txEventFIFOStartAddr = 640, + .txEventFIFOSize = 10, + .txEventFIFOWaterMark = 0, + .rxFIFO0startAddr = 170, + .rxFIFO0size = 10, + .rxFIFO0waterMark = 0, + .rxFIFO0OpMode = 0, + .rxFIFO1startAddr = 190, + .rxFIFO1size = 10, + .rxFIFO1waterMark = 10, + .rxFIFO1OpMode = 0, + .rxBufStartAddr = 208, + .rxBufElemSize = DL_MCAN_ELEM_SIZE_8BYTES, + .rxFIFO0ElemSize = DL_MCAN_ELEM_SIZE_8BYTES, + .rxFIFO1ElemSize = DL_MCAN_ELEM_SIZE_8BYTES, +}; + + + + /** * @brief Function to use SDA pin of TM1650 * @@ -131,6 +188,96 @@ void vRTE_InitUARTCANEcho(void) xECU_CANInit(CANFD0,g_u16CanSpeed); } +void vCanFilterMaskSaveVal(uint8_t ucIdx, uint32_t mask, bool isExtended) +{ + maskCount = ucIdx; + maskValues[maskCount] = mask; + isExtendedID[maskCount++] = isExtended; +} + +void vCanFilterSaveVal(uint8_t ucIdx, uint32_t Filter, bool isExtended) +{ + filterCount = ucIdx; + // Store filter value + filterValues[filterCount] = Filter; + isExtendedID[filterCount++] = isExtended; +} + + + +void vCanFilterReset() { + uint32_t i; + + // Disable CAN before reconfiguring filters + xECU_CANDeInit(CANFD0); + + // Reset all standard ID filters + for (i = 0; i < MAX_FILTERS; i++) { + DL_MCAN_StdMsgIDFilterElement stdFilterElement = {0}; + stdFilterElement.sfec = 001; // Disable filter element + stdFilterElement.sft = 11; // Disable filter type + DL_MCAN_addStdMsgIDFilter(CANFD0, i, &stdFilterElement); + } + + // Reset all extended ID filters + for (i = 0; i < MAX_FILTERS; i++) { + DL_MCAN_ExtMsgIDFilterElement extFilterElement = {0}; + extFilterElement.efec = 000; // Disable filter element + extFilterElement.eft = 11; // Disable filter type + DL_MCAN_addExtMsgIDFilter(CANFD0, i, &extFilterElement); + } + + // Re-enable CAN after resetting filters + xECU_CANInit(CANFD0,g_u16CanSpeed); +} + +// Function to configure CAN filters +static void vCanConfigFilter() { + + // Disable CAN before configuring filters + xECU_CANDeInit(CANFD0); + uint16_t extendedFilter = 0; + uint16_t standardFilter = 0; + + for (int i = 0; i < maskCount; i++) { + if (isExtendedID[i]) { + // Extended ID filter + DL_MCAN_ExtMsgIDFilterElement extFilterElement; + extFilterElement.efid1 = filterValues[i]; + extFilterElement.efid2 = maskValues[i]; + extFilterElement.efec = 001; + extFilterElement.eft = 10; + DL_MCAN_addExtMsgIDFilter(CANFD0, i, &extFilterElement); + extendedFilter++; + } + else { + // Standard ID filter + DL_MCAN_StdMsgIDFilterElement stdFilterElement; + stdFilterElement.sfid1 = filterValues[i]; + stdFilterElement.sfid2 = maskValues[i]; + stdFilterElement.sfec = 001; + stdFilterElement.sft = 10; + DL_MCAN_addStdMsgIDFilter(CANFD0, i, &stdFilterElement); + standardFilter++; + } + } + + + + gMCAN0MsgRAMConfigParamsFiltered.lse = standardFilter; // Set dynamically based on actual filters + gMCAN0MsgRAMConfigParamsFiltered.lss = extendedFilter; // Set dynamically based on actual filters + DL_MCAN_config(CANFD0, (DL_MCAN_ConfigParams*) &gMCAN0ConfigParamsFiltered); + DL_MCAN_msgRAMConfig(CANFD0, &gMCAN0MsgRAMConfigParamsFiltered); + + // Reset counters after applying filters + maskCount = 0; + filterCount = 0; + + // Re-enable CAN after filters + xECU_CANInit(CANFD0,g_u16CanSpeed); + +} + void vRTE_UARTDataProcess(void) { PacketRetCode_t retCode = PACKET_FAIL; @@ -149,12 +296,41 @@ void vRTE_UARTDataProcess(void) if( mode == 0 ) { g_u32UartSpeed = baudrate; -// xECU_UARTReInit(&g_xUartHandle, g_u32UartSpeed); + xECU_UARTReInit(&g_xUartHandle, g_u32UartSpeed); } else if( mode == 1 ) { g_u16CanSpeed = (uint16_t)baudrate; -// xECU_CanReInit(CANFD0, g_u16CanSpeed); + xECU_CanReInit(CANFD0, g_u16CanSpeed); + } + else if( mode == 2 ) + { + + if( pucBuf[PKT_HEADER+1] != 0 ){//22(20+2) rx filter available send each id in a frame + uint32_t filterId = 0; + memcpy(&filterId, &pucBuf[PKT_HEADER+3], sizeof(uint32_t)); + bool isExtended = (filterId > 0x7FF); // Standard IDs are <= 0x7FF + vCanFilterSaveVal((pucBuf[PKT_HEADER+1] - 1), filterId, isExtended); + if( pucBuf[PKT_HEADER+2] )//All filter received. Trigger Filter Settings + { + vCanConfigFilter(); + } + else + return; + } + else{ + vCanFilterReset(); + vCanConfigFilter(); + } + } + else if ( mode == 3 ) + { + if( (pucBuf[PKT_HEADER+1] < 1) || (pucBuf[PKT_HEADER+1] > 8) )//0-7 maximum received pkts + return; + uint32_t maskId = 0; + memcpy(&maskId, &pucBuf[PKT_HEADER+3], sizeof(uint32_t)); + bool isExtended = (maskId > 0x7FF); // Standard IDs are <= 0x7FF + vCanFilterMaskSaveVal((pucBuf[PKT_HEADER+1] - 1), maskId, isExtended); } vMCAL_DelayTicks(100); vECU_InitiateUartToCanTransmit(&g_xUartHandle, 0x01, pucBuf, 0);