Flexcan bus off, interrupt transfer example, K60

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

Flexcan bus off, interrupt transfer example, K60

2,580 Views
xinggaoxia
Contributor I

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!

Labels (1)
0 Kudos
5 Replies

1,772 Views
avppoian
Contributor I

I have the same problem, with two boards I can communicate but with other microcontroller like ST I can not communicate, stays on while(!txComplete) like the guy told. I configured the can bit timing correctly and sended the ID. I am thinking that they create a problem using the function FLEX_CAN_ST(identifier) because it makes a mask. I tryed to remove this and put like this frame.id = 0x89 for example and not result in anything. There is a problem with the drive, it was created only to communicate between two boards with MCUXpresso. I think this is the problem and I don´t know how to solve this, only the author of NXP.

0 Kudos

1,772 Views
xinggaoxia
Contributor I

Does anyone face flexcan setting problem using KSDK?

0 Kudos

1,772 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, XingGao,

I think it is okay to connect K60 CAN bus and USBCAN device via CAN bus, the USBCAN can provide the ACK signal.

Pls check whether the baudrate, address match each other.

BR

XiangJun Rong

0 Kudos

1,772 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

If you use the example, you have to use two CAN board and connect them via transceiver, one is transmitter/receiver boards, another is receiver/transmitter boards. You can not use only one board to transmit data, as you know that CAN bus needs the Ack signal which must be from receiver, in other words, if you use only one board to transmit data without receiver, because transmitter can not get ACK signal(Acknowledge) from receiver, the transmitter process can not be complete.

Hope it can help you

BR

XiangJun Rong

0 Kudos

1,773 Views
xinggaoxia
Contributor I

Hi, Xiangjun,

Thank you for reply.

Can USBCAN be considered as a transceiver? I connet the K60 can bus to USBCAN box, set up as 125kbps. I can not see anything from USBCAN GUI tool.

I worked in HCS08 project before. USBCAN works just fine for that. Are there any differences in CAN controller setting between HCS08 and K60? I did not set any ACK signal from my USBCAN tool for HCS08. I do not know how to ACK to K60. Please help!

Best Regards!

0 Kudos