AnsweredAssumed Answered

Unable to Send/Receive can messages to PCAN adapter from K66F Flexcan

Question asked by Steffen Probst on Aug 7, 2019
Latest reply on Aug 8, 2019 by Robin_Shen

Hello,

 

I am unable to send/receive CAN msgs to PCAN adapter from K66F microcontroller, my connection setup is as follows,

K66F is connected to CAN-Transceiver TJA1050 (120 ohms terminated) to CANH and CANL of Peak can adapter(120 ohms terminated). I am using SDK Flexcan loopback example and modified can bus timing and also made sure that baud rate is 50 kbps when I connect CANH and CANL to oscilloscope all I see is error frames, bit stuff error frames and unknown CAN message with unknown ID.  I think the problem is with setting bus timing. Please find the modified K66F flex can loopback code.

 

static void flexcan_callback(CAN_Type *base, flexcan_handle_t *handle, status_t status, uint32_t result, void *userData) {

switch (status) {

/* Process FlexCAN Rx event. */

   case kStatus_FLEXCAN_RxIdle:

    if (RX_MESSAGE_BUFFER_NUM == result) { rxComplete = true; }

    break;

/* Process FlexCAN Tx event. */

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();

PRINTF("\r\n==FlexCAN loopback example -- Start.==\r\n\r\n");

FLEXCAN_GetDefaultConfig(&flexcanConfig);

#if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcOsc;
#else
#if defined(CAN_CTRL1_CLKSRC_MASK)
if (!FSL_FEATURE_FLEXCAN_INSTANCE_SUPPORT_ENGINE_CLK_SEL_REMOVEn(EXAMPLE_CAN))
{
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri;
}
#endif
#endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */

flexcanConfig.enableLoopBack = false;
flexcanConfig.baudRate = 50000;
flexcanConfig.timingConfig.phaseSeg1 = 3;
flexcanConfig.timingConfig.phaseSeg2 = 2;
flexcanConfig.timingConfig.propSeg = 1;
flexcanConfig.timingConfig.rJumpwidth = 1;
#if (defined(USE_CANFD) && USE_CANFD)
FLEXCAN_FDInit(EXAMPLE_CAN, &flexcanConfig, EXAMPLE_CAN_CLK_FREQ, BYTES_IN_MB, false);
#else
FLEXCAN_Init(EXAMPLE_CAN, &flexcanConfig, 12000000UL);
#endif

/* Setup Rx Message Buffer. */
mbConfig.format = kFLEXCAN_FrameFormatStandard;
mbConfig.type = kFLEXCAN_FrameTypeData;
mbConfig.id = FLEXCAN_ID_STD(0x321);
#if (defined(USE_CANFD) && USE_CANFD)
FLEXCAN_SetFDRxMbConfig(EXAMPLE_CAN, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);
#else
FLEXCAN_SetRxMbConfig(EXAMPLE_CAN, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);
#endif

/* Setup Tx Message Buffer. */
#if (defined(USE_CANFD) && USE_CANFD)
FLEXCAN_SetFDTxMbConfig(EXAMPLE_CAN, TX_MESSAGE_BUFFER_NUM, true);
#else
FLEXCAN_SetTxMbConfig(EXAMPLE_CAN, TX_MESSAGE_BUFFER_NUM, true);
#endif

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

/* Start receive data through Rx Message Buffer. */
rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM;
#if (defined(USE_CANFD) && USE_CANFD)
rxXfer.framefd = &rxFrame;
FLEXCAN_TransferFDReceiveNonBlocking(EXAMPLE_CAN, &flexcanHandle, &rxXfer);
#else
rxXfer.frame = &rxFrame;
FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN, &flexcanHandle, &rxXfer);
#endif

/* Prepare Tx Frame for sending. */
txFrame.format = kFLEXCAN_FrameFormatStandard;
txFrame.type = kFLEXCAN_FrameTypeData;
txFrame.id = FLEXCAN_ID_STD(0x7B);
txFrame.length = DLC;
#if (defined(USE_CANFD) && USE_CANFD)
txFrame.brs = 1;
#endif
#if (defined(USE_CANFD) && USE_CANFD)
uint8_t i = 0;
for (i = 0; i < DWORD_IN_MB; i++)
{
txFrame.dataWord[i] = i;
}
#else
txFrame.dataWord0 = CAN_WORD0_DATA_BYTE_0(0x11) | CAN_WORD0_DATA_BYTE_1(0x22) | CAN_WORD0_DATA_BYTE_2(0x33) |
CAN_WORD0_DATA_BYTE_3(0x44);
txFrame.dataWord1 = CAN_WORD1_DATA_BYTE_4(0x55) | CAN_WORD1_DATA_BYTE_5(0x66) | CAN_WORD1_DATA_BYTE_6(0x77) |
CAN_WORD1_DATA_BYTE_7(0x88);
#endif

PRINTF("Send message from MB%d to MB%d\r\n", TX_MESSAGE_BUFFER_NUM, RX_MESSAGE_BUFFER_NUM);
#if (defined(USE_CANFD) && USE_CANFD)
for (i = 0; i < DWORD_IN_MB; i++)
{
PRINTF("tx word%d = 0x%x\r\n", i, txFrame.dataWord[i]);
}
#else
PRINTF("tx word0 = 0x%x\r\n", txFrame.dataWord0);
PRINTF("tx word1 = 0x%x\r\n", txFrame.dataWord1);
#endif

while (1)
{
/* Send data through Tx Message Buffer. */
txXfer.mbIdx = TX_MESSAGE_BUFFER_NUM;
#if (defined(USE_CANFD) && USE_CANFD)
txXfer.framefd = &txFrame;
FLEXCAN_TransferFDSendNonBlocking(EXAMPLE_CAN, &flexcanHandle, &txXfer);
#else
txXfer.frame = &txFrame;
FLEXCAN_TransferSendNonBlocking(EXAMPLE_CAN, &flexcanHandle, &txXfer);
#endif
/* Waiting for Rx Message finish. */
while ((!rxComplete) || (!txComplete))
{
};


PRINTF("\r\nReceived message from MB%d\r\n", RX_MESSAGE_BUFFER_NUM);
#if (defined(USE_CANFD) && USE_CANFD)
for (i = 0; i < DWORD_IN_MB; i++)
{
PRINTF("rx word%d = 0x%x\r\n", i, rxFrame.dataWord[i]);
}
#else
PRINTF("rx word0 = 0x%x\r\n", rxFrame.dataWord0);
PRINTF("rx word1 = 0x%x\r\n", rxFrame.dataWord1);
#endif

PRINTF("\r\n==FlexCAN loopback example -- Finish.==\r\n");

}
}

 

Please, can anyone help me? Thanks in advance. 

Outcomes