FLEXCAN unique ID filtering in iMX-rt1050

cancel
Showing results for 
Search instead for 
Did you mean: 

FLEXCAN unique ID filtering in iMX-rt1050

206 Views
arjunsagar
Contributor I

Hello,

I need help in filtering Unique IDs in CAN. I have used global mask FLEXCAN_SetRxMbGlobalMask, which is used to receive range of Tx_ids. FLEXCAN_SetRxIndividualMask will filter the rx_MBs. I want to filter 13 unique ids, what i have implemented for 3 unique ids is , using flexcan_rx_mb_config_t  3 times, can someone please suggest an efficient way of filtering multiple unique ids(13). Which API  should i configure to achieve this?

Thanks

3 Replies

178 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @arjunsagar ,

   Can you define your filtered unique ID or not?

   If yes, an  efficient way for you, eg, to the ID, you need to check all the bit, this is used to filter one uique ID. But, if to some ID bit, you don't check it, then it means you can use two uique ID pass the filter, then don't check 2 bits, it is 4 unique ID , 3 bits, it is 8 uique ID, etc.

   I mean this:

kerryzhou_0-1648104256386.png

Then, to your 13, you may use 3 bit ignore for 8 ID in one MB, and 2 bit ignore for 4 ID,  and one normal MB. So, you can configure 3 MB, if you can define your received ID. But if the ID totally can't share the bit, then you may define it one by one.

 

Wish it helps you!

Best Regards,

Kerry

 

0 Kudos

176 Views
arjunsagar
Contributor I

Hey Kerry,

Thanks for your response, here is what i tried for 2 unique ids:

/*************************************************************************************************************/
#include "fsl_debug_console.h"
#include "fsl_flexcan.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"

/*******************************************************************************
* Definitions
******************************************************************************/
#define EXAMPLE_CAN CAN2

/* Considering that the first valid MB must be used as Reserved TX MB for ERR005829,
* if RX FIFO enables (RFEN bit in MCE set as 1) and RFFN in CTRL2 is set default as zero,
* the first valid TX MB Number shall be 8;
* if RX FIFO enables (RFEN bit in MCE set as 1) and RFFN in CTRL2 is set by other values (0x1~0xF),
* the user should consider to detail the first valid MB number;
* if RX FIFO disables (RFEN bit in MCE set as 0) , the first valid MB number would be zero.
*/
#define RX_MESSAGE_BUFFER_NUM (16)
#define RX_MESSAGE_BUFFER_NUM1 (17)
#define RX_MESSAGE_BUFFER_NUM2 (18)
#define RX_MESSAGE_BUFFER_NUM3 (19)
#define RX_MESSAGE_BUFFER_NUM4 (20)
#define TX_MESSAGE_BUFFER_NUM (9)
#define NUMBER_OF_RX (2)
#define DLC (8)

/* Select 60M clock divided by USB1 PLL (480 MHz) as master flexcan clock source */
#define FLEXCAN_CLOCK_SOURCE_SELECT (0U)
/* Clock divider for master flexcan clock source */
#define FLEXCAN_CLOCK_SOURCE_DIVIDER (2U)
/* Get frequency of flexcan clock */
#define EXAMPLE_CAN_CLK_FREQ ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / / (FLEXCAN_CLOCK_SOURCE_DIVIDER + 1U))
/* Set USE_IMPROVED_TIMING_CONFIG macro to use api to calculates the improved CAN / CAN FD timing values. */
#define USE_IMPROVED_TIMING_CONFIG (1U)
/* Fix MISRA_C-2012 Rule 17.7. */
#define LOG_INFO (void)PRINTF
/*******************************************************************************
* Prototypes
******************************************************************************/
uint8_t buff(uint8_t buff_no);
/*******************************************************************************
* Variables
******************************************************************************/
flexcan_handle_t flexcanHandle;
volatile bool txComplete = false;
volatile bool rxComplete = false;
volatile bool wakenUp = false;
flexcan_mb_transfer_t txXfer;
#if (defined(USE_CANFD) && USE_CANFD)
//flexcan_fd_frame_t frame;
#else
//flexcan_frame_t frame, frame1;
#endif
uint32_t txIdentifier;
uint32_t rxIdentifier;
uint32_t rxIdentifier1;
uint8_t rx_id_num;
/*array of structure for multiple receive id*/
struct rx_parameter
{
flexcan_frame_t frame;
flexcan_mb_transfer_t rxxfer;
flexcan_rx_mb_config_t mbcon;
uint32_t rx_id;

};
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief FlexCAN Call Back function
*/
static FLEXCAN_CALLBACK(flexcan_callback)
{
switch (status)
{
case kStatus_FLEXCAN_RxIdle:
if ((RX_MESSAGE_BUFFER_NUM == result))
{
rxComplete = true;
rx_id_num = 1;

}
else if(RX_MESSAGE_BUFFER_NUM1 == result)
{
rxComplete= true;
rx_id_num = 2;

}
else if(RX_MESSAGE_BUFFER_NUM2 == result)
{
rxComplete= true;
rx_id_num = 3;

}
break;

case kStatus_FLEXCAN_TxIdle:
if (TX_MESSAGE_BUFFER_NUM == result)
{
txComplete = true;
}
break;

case kStatus_FLEXCAN_WakeUp:
wakenUp = true;
break;

default:
break;
}
}
uint8_t buff(uint8_t i)
{
uint8_t mb_num;
switch(i)
{
case 0: mb_num = RX_MESSAGE_BUFFER_NUM;
break;
case 1: mb_num = RX_MESSAGE_BUFFER_NUM1;
break ;
}
return mb_num;
}

/*!
* @brief Main function
*/
int main(void)
{
flexcan_config_t flexcanConfig;
uint8_t node_type;
struct rx_parameter receive[NUMBER_OF_RX];
/*input by the application*/
uint32_t rx_id_value[NUMBER_OF_RX]={0x320, 0x326 };

/* Initialize board hardware. */
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();

/*Clock setting for FLEXCAN*/
CLOCK_SetMux(kCLOCK_CanMux, FLEXCAN_CLOCK_SOURCE_SELECT);
CLOCK_SetDiv(kCLOCK_CanDiv, FLEXCAN_CLOCK_SOURCE_DIVIDER);

LOG_INFO("********* FLEXCAN Interrupt EXAMPLE *********\r\n");
LOG_INFO(" Message format: Standard (11 bit id)\r\n");
LOG_INFO(" Message buffer %d used for Rx.\r\n", RX_MESSAGE_BUFFER_NUM);
LOG_INFO(" Interrupt Mode: Enabled\r\n");
LOG_INFO("*********************************************\r\n\r\n");
txIdentifier = 0x111;
/*assigning rx id member of structure*/
for(uint32_t i=0;i<NUMBER_OF_RX;i++)
receive[i].rx_id=rx_id_value[i];

/* Get FlexCAN module default Configuration. */
/*
* flexcanConfig.clksrc=kFLEXCAN_ClkSrc0;
* flexcanConfig.baudRate = 1000000U;
* flexcanConfig.baudRateFD = 2000000U;
* flexcanConfig.maxMbNum = 16;
* flexcanConfig.enableLoopBack = false;
* flexcanConfig.enableSelfWakeup = false;
* flexcanConfig.enableIndividMask = false;
* flexcanConfig.disableSelfReception = false;
* flexcanConfig.enableListenOnlyMode = false;
* flexcanConfig.enableDoze = false;
*/
FLEXCAN_GetDefaultConfig(&flexcanConfig);
flexcanConfig.enableIndividMask = false;
flexcanConfig.maxMbNum = 63;
flexcanConfig.clksrc=kFLEXCAN_ClkSrc0;
flexcanConfig.baudRate = 1000000U;
flexcanConfig.enableLoopBack = false;
flexcanConfig.enableSelfWakeup = false;
flexcanConfig.disableSelfReception = true;
flexcanConfig.enableListenOnlyMode = false;

/* If special quantum setting is needed, set the timing parameters. */

#if (defined(USE_IMPROVED_TIMING_CONFIG) && USE_IMPROVED_TIMING_CONFIG)
flexcan_timing_config_t timing_config;
memset(&timing_config, 0, sizeof(flexcan_timing_config_t));
if (FLEXCAN_CalculateImprovedTimingValues(EXAMPLE_CAN, flexcanConfig.baudRate, EXAMPLE_CAN_CLK_FREQ,
&timing_config))
{
/* Update the improved timing configuration*/
memcpy(&(flexcanConfig.timingConfig), &timing_config, sizeof(flexcan_timing_config_t));
}
else
{
LOG_INFO("No found Improved Timing Configuration. Just used default configuration\r\n\r\n");
}

FLEXCAN_Init(EXAMPLE_CAN, &flexcanConfig, EXAMPLE_CAN_CLK_FREQ);

/* Create FlexCAN handle structure and set call back function. */
FLEXCAN_TransferCreateHandle(EXAMPLE_CAN, &flexcanHandle, flexcan_callback, NULL);

/* Set Rx Masking mechanism. */
// FLEXCAN_SetRxMbGlobalMask(EXAMPLE_CAN, FLEXCAN_RX_MB_STD_MASK(0x7ff, 0, 0));
// FLEXCAN_SetRxIndividualMask(EXAMPLE_CAN, RX_MESSAGE_BUFFER_NUM , 0x7ff);
/* Setup Rx Message Buffer. */
for(uint8_t i =0;i<NUMBER_OF_RX;i++)
{
receive[i].mbcon.format = kFLEXCAN_FrameFormatStandard;
receive[i].mbcon.type = kFLEXCAN_FrameTypeData;
receive[i].mbcon.id = FLEXCAN_ID_STD(receive[i].rx_id);
FLEXCAN_SetRxMbConfig(EXAMPLE_CAN, buff(i), &receive[i].mbcon, true);
}

/* Setup Tx Message Buffer. */
FLEXCAN_SetTxMbConfig(EXAMPLE_CAN, TX_MESSAGE_BUFFER_NUM, true);
LOG_INFO("Enter any key to receive data from Node \r\n\r\n");
GETCHAR();
LOG_INFO("CAN receive initiated\r\n");
while (true)
{

/* Start receive data through Rx Message Buffer. */
for(uint8_t j=0;j<NUMBER_OF_RX;j++)
{
receive[j].rxxfer.mbIdx = buff(j);
receive[j].rxxfer.frame = &receive[j].frame;
(void)FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN, &flexcanHandle, &receive[j].rxxfer);

}
/* Wait until Rx receive full. */
while (!rxComplete)
{
//Do nothing
};
rxComplete = false;
LOG_INFO("\r\nRx MB ID: 0x%3x, Rx MB data: 0x%x, Time stamp: %d\r\n", receive[rx_id_num-1].frame.id >> CAN_ID_STD_SHIFT,
receive[rx_id_num-1].frame.dataByte0, receive[rx_id_num-1].frame.timestamp);
}
}

If i have to use 13 unique ids. Should i configure 13 buffers? unique meaning the ids that don't share the bits RXMGMASK.

 

 

0 Kudos

161 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @arjunsagar ,

  If totally unique, don't share bits, yes, I think you need to use 13 MBs.

Best Regards,

Kerry

0 Kudos