Using FLEXCAN_TransferCreateHandle to create two callback functions

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using FLEXCAN_TransferCreateHandle to create two callback functions

Jump to solution
1,126 Views
CMH1
Contributor II

Hello all,

I am using the Kinetis MKE18F512 processor and trying to setup CAN0 and CAN1 callback functions. All my initialization code is based off the TWRKE18F “flexcan_loopback_transfer” example code. Everything else works with my code, transmit, ID filters but only one of the callback functions is working. Using the example code, inside my CAN0 initialization I use:

FLEXCAN_TransferCreateHandle(EXAMPLE_CAN0, &flexcanHandle, flexcan_callbackCAN0, NULL);

 

Inside my CAN1 initialization I use:

FLEXCAN_TransferCreateHandle(EXAMPLE_CAN1, &flexcanHandle, flexcan_callbackCAN1, NULL);

 

The issue is that only one of the callbacks will work, which ever one of the “TransferCreateHandle” is executed last works. It seems that just changing the base (CAN0 or CAN1) is not enough. Pretty sure I need to do something else with the flexcanhandle but I’m not sure what. Does anyone have examples of setting up two Flexcan callback functions?

 

Here are both of my initialization routines:

 

 

void InitCAN0()

{

        FLEXCAN_GetDefaultConfig250k(&flexcanConfig);

        /* Init FlexCAN module. */

        flexcanConfig.clksrc=kFLEXCAN_ClkSrcPeri;

        FLEXCAN_Init(EXAMPLE_CAN0, &flexcanConfig, EXAMPLE_CAN0_CLK_FREQ);

 

        /* Create FlexCAN handle structure and set call back function. */

        FLEXCAN_TransferCreateHandle(EXAMPLE_CAN0, &flexcanHandle, flexcan_callbackCAN0, NULL);

 

 

   /* Setup Rx Message Buffer. */

       mbConfig.format = kFLEXCAN_FrameFormatExtend;

       mbConfig.type   = kFLEXCAN_FrameTypeData;

       mbConfig.id     = FLEXCAN_ID_EXT(ETC2_PGN);         //This is where you setup the hardware mailbox

 

       FLEXCAN_SetRxMbConfig(EXAMPLE_CAN0, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);

 

        /* Start receive data through Rx Message Buffer. */

        rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM;

        rxXfer.frame = &frame;

        FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN0, &flexcanHandle, &rxXfer);

 

   //Setup second MB

 

       /* Setup Rx Message Buffer. */

       mbConfig.format = kFLEXCAN_FrameFormatExtend;

       mbConfig.type   = kFLEXCAN_FrameTypeData;

       mbConfig.id     = FLEXCAN_ID_EXT(TC1_PGN);    //This is where you setup the hardware mailbox

 

       FLEXCAN_SetRxMbConfig(EXAMPLE_CAN0, RX_MESSAGE_BUFFER_NUM2, &mbConfig, true);

 

       /* Start receive data through Rx Message Buffer. */

       rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM2;

       rxXfer.frame = &frame;

       FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN0, &flexcanHandle, &rxXfer);

 

 

 

    /* Setup Tx Message Buffer. */

 

        FLEXCAN_SetTxMbConfig(EXAMPLE_CAN0, TX_MESSAGE_BUFFER_NUM, true);

}

 

 

 

void InitCAN1()

{

    FLEXCAN_GetDefaultConfig250k(&flexcanConfig);

     /* Init FlexCAN module. */

     flexcanConfig.clksrc=kFLEXCAN_ClkSrcPeri;

     FLEXCAN_Init(EXAMPLE_CAN1, &flexcanConfig, EXAMPLE_CAN1_CLK_FREQ);

 

     /* Create FlexCAN handle structure and set call back function. */

    FLEXCAN_TransferCreateHandle(EXAMPLE_CAN1, &flexcanHandle, flexcan_callbackCAN1, NULL);

 

 

     /* Setup Rx Message Buffer. */

    mbConfig.format = kFLEXCAN_FrameFormatExtend;

    mbConfig.type   = kFLEXCAN_FrameTypeData;

    mbConfig.id     = FLEXCAN_ID_EXT(TC1_PGN);    //This is where you setup the hardware mailbox

 

    FLEXCAN_SetRxMbConfig(EXAMPLE_CAN1, RX_MESSAGE_BUFFER_NUM3, &mbConfig, true);

 

    /* Start receive data through Rx Message Buffer. */

     rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM3;

     rxXfer.frame = &frame;

     FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN1, &flexcanHandle, &rxXfer);

 

 

 /* Setup Tx Message Buffer. */

 

     FLEXCAN_SetTxMbConfig(EXAMPLE_CAN1, TX_MESSAGE_BUFFER_NUM2, true);

}

 

 

 

Labels (1)
0 Kudos
1 Solution
1,118 Views
CMH1
Contributor II

Nevermind, I found the problem.

Not only do you need to create the handle, you also have to tell “FLEXCAN_TransferCreateHandle” what handle to use, you also need to tell “FLEXCAN_TransferReceiveNonBlocking” to send the interrupt to the same handle. Whoops.

The code for my CAN1 initilization now looks like this where  “FLEXCAN_TransferCreateHandle” and “FLEXCAN_TransferReceiveNonBlocking” are both using flexcanHandle2

 

void InitCAN1()
{
    FLEXCAN_GetDefaultConfig250k(&flexcanConfig);
     /* Init FlexCAN module. */
     flexcanConfig.clksrc=kFLEXCAN_ClkSrcPeri;
     FLEXCAN_Init(EXAMPLE_CAN1, &flexcanConfig, EXAMPLE_CAN1_CLK_FREQ);

     /* Create FlexCAN handle structure and set call back function. */
    FLEXCAN_TransferCreateHandle(EXAMPLE_CAN1, &flexcanHandle2, flexcan_callback2, NULL);


     /* Setup Rx Message Buffer. */
    mbConfig.format = kFLEXCAN_FrameFormatExtend;
    mbConfig.type   = kFLEXCAN_FrameTypeData;
    mbConfig.id     = FLEXCAN_ID_EXT(TC1_PGN);    //This is where you setup the hardware mailbox

    FLEXCAN_SetRxMbConfig(EXAMPLE_CAN1, RX_MESSAGE_BUFFER_NUM3, &mbConfig, true);

    /* Start receive data through Rx Message Buffer. */
     rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM3;
     rxXfer.frame = &frame;
     FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN1, &flexcanHandle2, &rxXfer);


 /* Setup Tx Message Buffer. */

     FLEXCAN_SetTxMbConfig(EXAMPLE_CAN1, TX_MESSAGE_BUFFER_NUM2, true);

}

View solution in original post

1 Reply
1,119 Views
CMH1
Contributor II

Nevermind, I found the problem.

Not only do you need to create the handle, you also have to tell “FLEXCAN_TransferCreateHandle” what handle to use, you also need to tell “FLEXCAN_TransferReceiveNonBlocking” to send the interrupt to the same handle. Whoops.

The code for my CAN1 initilization now looks like this where  “FLEXCAN_TransferCreateHandle” and “FLEXCAN_TransferReceiveNonBlocking” are both using flexcanHandle2

 

void InitCAN1()
{
    FLEXCAN_GetDefaultConfig250k(&flexcanConfig);
     /* Init FlexCAN module. */
     flexcanConfig.clksrc=kFLEXCAN_ClkSrcPeri;
     FLEXCAN_Init(EXAMPLE_CAN1, &flexcanConfig, EXAMPLE_CAN1_CLK_FREQ);

     /* Create FlexCAN handle structure and set call back function. */
    FLEXCAN_TransferCreateHandle(EXAMPLE_CAN1, &flexcanHandle2, flexcan_callback2, NULL);


     /* Setup Rx Message Buffer. */
    mbConfig.format = kFLEXCAN_FrameFormatExtend;
    mbConfig.type   = kFLEXCAN_FrameTypeData;
    mbConfig.id     = FLEXCAN_ID_EXT(TC1_PGN);    //This is where you setup the hardware mailbox

    FLEXCAN_SetRxMbConfig(EXAMPLE_CAN1, RX_MESSAGE_BUFFER_NUM3, &mbConfig, true);

    /* Start receive data through Rx Message Buffer. */
     rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM3;
     rxXfer.frame = &frame;
     FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN1, &flexcanHandle2, &rxXfer);


 /* Setup Tx Message Buffer. */

     FLEXCAN_SetTxMbConfig(EXAMPLE_CAN1, TX_MESSAGE_BUFFER_NUM2, true);

}