Robert Lewis

Flexcan CAN0 Tx Fails - SDK v2.2, MK20DX256VLL10

Discussion created by Robert Lewis on May 7, 2017
Latest reply on May 15, 2017 by Robert Lewis

I have a working MQX 4.0.1 system for Tx of CAN0 messages on an in-house board using the MK20DX256VLL10.

 

I cannot get the correct timing for the messages using code from the example for flexcan on the same board, SDK v2.2. We are using the MCG in PEE mode. A 16Mhz xtal, clocked up to 96Mhz. This is working correctly I believe as it passes all the wait on LOCK tests.

 

We need to Tx on CAN0 at 500,000. When I look at the mcg registers between the mqx setup and sdk setup, they are identical for the key bits. There are some alerts and trim values that are different but the timing portions for dividers and clock selects are identical.

 

However the data viewed on the logic analyzer is completely wrong for the FreeRTOS & SDK v2.2 code. The values are wrong I believe because the baud rate is not right.

 

If I use kFLEXCAN_ClkSrcPeri as the clock, the code hangs waiting for exit from low power tests in fsl_clock.c. I have to use  kFLEXCAN_ClkSrcOsc

 

We are unable to set the clksrc as per the example, flexcanConfig.clkSrc            = kFLEXCAN_ClkSrcOsc; This gives a bit time on the first dominant bit of .75usec, in the working code the first bit is 2 usec and the data is totally wrong. Here is the working capture.

 

 

Here is the Flexcan from my SDK v2.2 code, with the same values for data. 

 

Sorry about the size; I can't get this web page to display it larger. As can be seen the data is not even close. I don't understand why I am even getting any response given the data is wrong?

 

If anyone can tell me what is wrong it would be very appreciated. I am assuming that flexcanConfig.length = 1, is an 8 byte frame. I can't find any documentation on this field other than the type. Is this correct?

 

I have no clue how to set these clocks based on the documentation. The doc set tells nothing I can find for the definitions for meaning and why a particular field should be one value or another, only programming data types. Can you tell me which clock output the CAN system should use if we are using the PLL in PEE mode, with an external xtal of 16Mhz, clocked to 96Mhz for the core? In MQX we just picked the baudrate and the RTOS driver set the correct dividers. Why do we have to set a baudrate and also a clock source, this seems counter intuitive since not all clock sources will work apparently. Which clock source should I use and how do I select it?

 

We generated the clock code using the Configuration Tool as well as the mux pin assignments. Thank you.

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

char c_tx_buff[] = {0x02, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00};
char static c_rx_buff[8], c_test[8];

flexcan_handle_t flexcanHandle;
flexcan_mb_transfer_t txXfer;
flexcan_mb_transfer_t rxXfer;


flexcan_config_t flexcanConfig;
flexcan_rx_mb_config_t mbConfig;

flexcan_frame_t rxFrame;
flexcan_frame_t txFrame;

uint32_t u32_freq_bus, u32_freq_periph, u32_freq_osc_clk; // just for debug validation

FLEXCAN_GetDefaultConfig(&flexcanConfig);

flexcanConfig.baudRate = 500000U;
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri; // default was kFLEXCAN_ClkOscOsc;

// u32_freq_osc_clk = flexcanConfig.clkSrc;
// u32_freq_periph = kFLEXCAN_ClkSrcPeri;
// u32_freq_bus = CLOCK_GetFreq(kCLOCK_BusClk);

FLEXCAN_Init(CAN0,
&flexcanConfig,
CLOCK_GetFreq(kCLOCK_BusClk));

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

FLEXCAN_SetRxMbGlobalMask(CAN0, // Set Rx Masking mechanism
FLEXCAN_RX_MB_STD_MASK(CAN0_RX_IDENTIFIER, 0, 0));

mbConfig.format = kFLEXCAN_FrameFormatStandard; // Setup Rx Message Buffer
mbConfig.type = kFLEXCAN_FrameTypeData; // remote frame data type
mbConfig.id = FLEXCAN_ID_STD(CAN0_RX_IDENTIFIER);

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

FLEXCAN_SetTxMbConfig(CAN0, // Setup Tx Message Buffer
CAN0_TX_MAILBOX_NUM,
true);

txFrame.id = FLEXCAN_ID_STD(CAN0_TX_IDENTIFIER);
txFrame.format = kFLEXCAN_FrameFormatStandard;
txFrame.type = kFLEXCAN_FrameTypeData;
txFrame.length = 1;

memcpy(&txFrame.dataWord0, &c_tx_buff[0], 4);
memcpy(&txFrame.dataWord1, &c_tx_buff[4], 4);

txXfer.frame = &txFrame;
txXfer.mbIdx = TX_MESSAGE_BUFFER_NUM; // might be 1 of 16 buffers on CAN device, no doc on this

while(1){
FLEXCAN_TransferSendNonBlocking(CAN0,
&flexcanHandle,
&txXfer);

{
uint32_t u32_i = 0, u32_j = 0;
for(u32_i = 0; u32_i < 0xFFFFFF; u32_i++){
u32_j++;
}
}
}

Outcomes