AnsweredAssumed Answered

Bug in "fsl_flexcan.c" / wrong treatment of the 'FDRATE'-bit

Question asked by Wolfgang Buescher on Aug 21, 2019
Latest reply on Aug 23, 2019 by Kerry Zhou

Greetings all,

Just about to get my feet wet with CAN FD in an RT1064, and had a first look at what was provided by NXP.

I think there's a bug in the treatment of the 'FDRATE'-bit (31) in register 'FDCTRL', module "fsl_flexcan.c" (at least with the one used by the "canfd_interrupt_transfer" demo provided for the RT1064 evaluation board). It's in this function:

 

void FLEXCAN_FDInit( CAN_Type *base, const flexcan_config_t *config, uint32_t sourceClock_Hz, flexcan_mb_size_t dataSize, bool brs)

Here, "bool brs" means "bitrate switch enable" (TRUE=allow transmission and reception of CAN FD messages, FALSE=only allow normal "CAN" frames regardless of the mailbox/message-buffer configuration). I guess this should be mapped to register 'FDCTRL', bit 'FDRATE' ("brs" = "FDRATE", oh well...) .

 

Here is what I think is wrong - C++ style comments were added by myself, when trying to understand what this code was supposed to do:

 

/* Enable FD operation and set bitrate switch. */
    if (brs)
     { 
        base->FDCTRL &= CAN_FDCTRL_FDRATE_MASK;  // CLEAR anything except 'FDRATE' (bit 31) ? ? ?
     }
    else // disable "FD"-operation (allow "normal" CAN frames only)
     {
        base->FDCTRL &= ~CAN_FDCTRL_FDRATE_MASK; // CLEAR the 'FDRATE'-bit (31)
     }

 

What *I guess* should be done in FLEXCAN_FDInit() is :

 

  /* Enable FD operation and set bitrate switch. */
    if (brs)
     { 
        base->FDCTRL |= CAN_FDCTRL_FDRATE_MASK;  // SET 'FDRATE' but leave other bits unchanged
     }
    else // disable "FD"-operation (allow "normal" CAN frames only)
     {
        base->FDCTRL &= ~CAN_FDCTRL_FDRATE_MASK; // CLEAR the 'FDRATE'-bit (31)
     }

Strangely, even with the original code (which either clears ANYTHING BUT 'FDRATE' or clears only 'FDRATE', but never sets that bit), I am able to transmit "FD"-frames besides normal CAN frames.

 

Maybe NXP can correct this, or (if the strange bitwise-and to clear ANYTHING BUT 'FDRATE' was intended this way), add a helpful hint / comment in the sourcecode, documenting why the bit isn't bitwise OR-ed to "set" it in Mr. FlexCAN(3).

 

  Cheers, Wolfgang .

Outcomes