FRDM-KW36 Software Development Kit (SDK) includes drivers and examples of FlexCAN module for KW36 which can be easily configured for a custom communication. For example, if user want to change the default baud rate from FlexCAN driver demo examples then the only needed change is the default value on "config->baudRate" and "config->baudRateFD" from "FLEXCAN_GetDefaultConfig" function (See Figure 1). Segments within a bit time will be automatically configured to obtain the desired baud rate. By default, demos are configured to work with CAN FD communication.
Figure 1. FRDM-KW36's default baudrate from flexcan_interrupt_transfer driver example
Even so, there are cases where segments within a bit time are not well configured and it's necessary that user configure segments manually.
An example occurs by setting the maximum FD baud rate "3.2MHz" using the 32MHz xtal or "2.6MHz" using a 26MHz xtal where demo reports an error. See Figure 2.
Figure 2. Error by setting maximum baud rate
When this error occurs, the fix is on setting the timing config parameters correctly by including the definition of SET_CAN_QUANTUM on application source file (see Figure 3) and then declare and initialize the timing config parameters shown in Figure 4.
Figure 3. SET_CAN_QUANTUM define
Figure 4. Custom timing config parameters
For this example we are going to show how to calculate timing config parameters in an scenario where a CAN FD communication is used with baud rate of 500kHz on nominal phase and 3.2MHz on FD phase. See Figure 5. To do it, we need to calculate Time Quanta and value of segments within the bit time.
Figure 5. Custom CAN FD baudrate
KW36 Reference Manual in chapter "184.108.40.206 Protocol timing" shows the segments within a bit time for CAN nominal phase configured in "CAN_CTRL1" register (see Figure 6), and segments for FD phase configured in CAN_FDCBT register (see Figure 7).
Figure 6. Segment within a bit time for CAN nominal phase
Figure 7. Segment within a bit time for CAN FD phase
Before calculating the value of segments, first we need to calculate the Time Quanta which is the atomic number of time handled by the CAN engine.
The formula to calculate Time Quanta is shown in Figure 8 taken from KW36 Reference Manual.
Figure 8. Time Quanta Formula
CANCLK can be selected by CLKSRC bits on CAN_CTRL1 register as shown in Figure 9, where the options are Peripheral clock=20MHz or Oscillator clock (16MHz if using 32MHz xtal or 13MHz if using 26MHz xtal). The recomiendation is to use the Oscillator clock due to peripheral clock can have jitter that affect communication.
Figure 9. CAN clocks
To select the Oscillator clock, search for flexcanConfig.clkSrc definition and set it to kFLEXCAN_ClkSrcOsc as shown in Figure 10.
Figure 10. CANCLK selection
Next step is selecting the PRESDIV value for nominal phase and FPRESDIV for FD phase. You have to select the right value to achieve the TQ needed to obtain the configured baudrate. For this example, let's set FPRESDIV value to 0 and PRESDIV value to 3.