MK22FX512 FlexCan init problem

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

MK22FX512 FlexCan init problem

623 Views
pauvilanova
Contributor III

Hi,

I'm trying to work with the flexcan module of my mk22 using the MCUxpresso drivers provided by nxp, but when I try to initialize the flexcan it gets stuck on the following while:

static inline void FLEXCAN_Enable(CAN_Type *base, bool enable)
{
    if (enable)
    {
        base->MCR &= ~CAN_MCR_MDIS_MASK;
        /* Wait FlexCAN exit from low-power mode. */
        while (base->MCR & CAN_MCR_LPMACK_MASK)
        {
        }...

When enabling CAN, it waits until the lowpowermodule is off, but this bit is never 0. How could that be?

I had CAN working on the KDS so i went to check in there if the driver checks that bit, but it doesn't, enabling can on the kds driver just changes MDIS bit without waiting the lpm to set off.

My mcuxpresso initialization is the following:

    flexcan_config_t flexcanConfig;
    FLEXCAN_GetDefaultConfig(&flexcanConfig);
    FLEXCAN_Init(FLEXCAN0, &flexcanConfig, 8000000UL);

Thanks.

Labels (1)
Tags (1)
0 Kudos
1 Reply

395 Views
mjbcswitzerland
Specialist V

Hi

The order of writing clock settings is important - and it will not leave disabled mode if not respected and in freeze mode.
This is the solution from the uTasker project as reference:

    ulCanCtrValue = fnOptimalCAN_clock(pars->usMode, pars->ulSpeed);
    ptrCAN_control->CAN_CTRL1 = (CLK_SRC_PERIPH_CLK & ulCanCtrValue);    // select the clock source while the module is in disable mode
                                                                         // note that the crystal can be used and has better jitter performance than the PLL
    ptrCAN_control->CAN_MCR &= ~CAN_MDIS;                                // move from disabled to freeze mode (un-synchronised to the CAN bus)
    while ((ptrCAN_control->CAN_MCR & CAN_LPMACK) != 0) {                // wait for CAN controller to leave disabled mode
    }
    ptrCAN_control->CAN_CTRL1 = ulCanCtrValue;                           // write the clock setting

Regards

Mark

CAN:
Document: http://www.utasker.com/docs/uTasker/uTaskerCAN.PDF
Video: https://www.youtube.com/watch?v=Ha8cv_XEvco


uTasker developer and supporter (+5'000 hours experience on +60 Kinetis derivatives in +80 product developments)
Kinetis: http://www.utasker.com/kinetis.html