cancel
Showing results for 
Search instead for 
Did you mean: 

How to setup filtration for CANFD messages on STM32G474RE correctly?

OKvas.1
Associate

I am working with mbedOS and I am trying to set up filtration of CANFD messages. There is a function which is performing all configurations.

int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle)
{
    UNUSED(handle); 
 
    FDCAN_FilterTypeDef sFilterConfig = {0};
 
    if (format == CANStandard) {    	
        sFilterConfig.IdType = FDCAN_STANDARD_ID;
 
    } else if (format == CANExtended) {
        sFilterConfig.IdType = FDCAN_EXTENDED_ID;
 
    } else { // Filter for CANAny format cannot be configured for STM32
        return 0;
    }
 
    sFilterConfig.FilterIndex = handle;
	sFilterConfig.FilterType = FDCAN_FILTER_MASK;
	sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
	sFilterConfig.FilterID1 = id;
	sFilterConfig.FilterID2 = mask;
 
    if (HAL_FDCAN_ConfigFilter(&obj->CanHandle, &sFilterConfig) != HAL_OK) {
        return 0;
    }
 
    return 1;
}

This function is called twice from CAN init to accept all CAN messages by default.

int can_internal_init(can_t *obj)
{
    if (HAL_FDCAN_Init(&obj->CanHandle) != HAL_OK) {
        error("HAL_FDCAN_Init error\n");
    }
 
    if (can_filter(obj, 0, 0, CANStandard, 0) == 0) {
        error("can_filter error\n");
    }
 
    if (can_filter(obj, 0, 0, CANExtended, 0) == 0) {
        error("can_filter error\n");
    }
 
    if (HAL_FDCAN_ConfigGlobalFilter(&obj->CanHandle, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK) {
        error("HAL_FDCAN_ConfigGlobalFilter error\n");
    }
 
    if (HAL_FDCAN_Start(&obj->CanHandle) != HAL_OK) {
        error("HAL_FDCAN_Start error\n");
    }
 
    return 1;
}

When i am trying to overwrite filter with ID 0, messageID = 0x126 and mask 0xFFF it seems that it is not applyed and i am still receiving all mesages. What could be a problem.

P.S if I am hardcoding val 0x126 and mask 0xFFF in can_filter(), then I can receive only specified message. It seems to be a problem with override of filter with ID 0.

0 REPLIES 0