AnsweredAssumed Answered

Flexcan bus off, interrupt transfer example, K60

Question asked by Xinggao Xia on Jan 9, 2017
Latest reply on Oct 19, 2018 by Alexandre V.D.P.

I am using MK60DN512. I take SDK example: interrupt transfter to set up can controller. However, program got blocked in checking txComplete status. flexcan_callback function is never called. TXERRCNT increases and FLTCONF shows Bus Off. My program is attached below,

*******************************************************************************
 * Definitions
 ******************************************************************************/
#define EXAMPLE_CAN CAN1
#define EXAMPLE_CAN_CLKSRC kCLOCK_BusClk
#define RX_MESSAGE_BUFFER_NUM (9)
#define TX_MESSAGE_BUFFER_NUM (8)

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/
flexcan_handle_t flexcanHandle;
volatile bool txComplete = false;
volatile bool rxComplete = false;
flexcan_mb_transfer_t txXfer, rxXfer;
flexcan_frame_t frame;
uint32_t txIdentifier;
uint32_t rxIdentifier;

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief FlexCAN Call Back function
 */
static void flexcan_callback(CAN_Type *base, flexcan_handle_t *handle, status_t status, uint32_t result, void *userData)
{
    switch (status)
    {
        case kStatus_FLEXCAN_RxIdle:
            if (RX_MESSAGE_BUFFER_NUM == result)
            {
                rxComplete = true;
            }
            break;

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

        default:
            break;
    }
}

/*!
 * @brief Main function
 */
int main(void)
{
    flexcan_config_t flexcanConfig;
    flexcan_rx_mb_config_t mbConfig;

    /* Initialize board hardware. */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    /* Select mailbox ID. */
    txIdentifier = 0x321;
    rxIdentifier = 0x123;

    /* Get FlexCAN module default Configuration. */
    /*
     * flexcanConfig.clkSrc = kFLEXCAN_ClkSrcOsc;
     * flexcanConfig.baudRate = 125000U;
     * flexcanConfig.maxMbNum = 16;
     * flexcanConfig.enableLoopBack = false;
     * flexcanConfig.enableSelfWakeup = false;
     * flexcanConfig.enableIndividMask = false;
     * flexcanConfig.enableDoze = false;
     */
    FLEXCAN_GetDefaultConfig(&flexcanConfig);

    /* Init FlexCAN module. */
    flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri;
    FLEXCAN_Init(EXAMPLE_CAN, &flexcanConfig, CLOCK_GetFreq(EXAMPLE_CAN_CLKSRC));

    /* 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(rxIdentifier, 0, 0));

    /* Setup Rx Message Buffer. */
    mbConfig.format = kFLEXCAN_FrameFormatStandard;
    mbConfig.type = kFLEXCAN_FrameTypeData;
    mbConfig.id = FLEXCAN_ID_STD(rxIdentifier);
    FLEXCAN_SetRxMbConfig(EXAMPLE_CAN, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);

    /* Setup Tx Message Buffer. */
    FLEXCAN_SetTxMbConfig(EXAMPLE_CAN, TX_MESSAGE_BUFFER_NUM, true);

    frame.dataByte0 = 0;

    while (1)
    {
        frame.id = FLEXCAN_ID_STD(txIdentifier);
        frame.format = kFLEXCAN_FrameFormatStandard;
        frame.type = kFLEXCAN_FrameTypeData;
        frame.length = 1;
        txXfer.frame = &frame;
        txXfer.mbIdx = TX_MESSAGE_BUFFER_NUM;
        FLEXCAN_TransferSendNonBlocking(EXAMPLE_CAN, &flexcanHandle, &txXfer);

        while (!txComplete) <-----------my program stops here
        {
        };
        txComplete = false;

        /* Start receive data through Rx Message Buffer. */
        rxXfer.frame = &frame;
        rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM;
        FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN, &flexcanHandle, &rxXfer);

        /* Wait until Rx MB full. */
        while (!rxComplete)
        {
        };
        rxComplete = false;

        frame.dataByte0++;
    }
}

 

Thank you!

Outcomes