Stuck in PBE mode when leave KBOOT and jump to application code

cancel
Showing results for 
Search instead for 
Did you mean: 

Stuck in PBE mode when leave KBOOT and jump to application code

Jump to solution
1,191 Views
kyleyang
Contributor IV

The design concept of application code is support USB CDC and CAN bus.
So we create a project code support CDC and the clock configuration is source from IRC48M.
After leave KBOOT and jump to application, sometime the CDC device will not show up at device manager.
So we place several GPIO to toggle LEDs to trace the critical issue and found the system is stuck in "CLOCK_SetPbeMode" while loop to check external reference clock.

But I also try that CDC function without KBOOT, always can found CDC device in device manager.
Please help check this issue and clarify the workaround steps.
Thanks.

Original Attachment has been moved to: bm.7z.zip

Original Attachment has been moved to: Kyle_MKS22FN256xxx12.mex.zip

1 Solution
618 Views
abhishekvishwak
Contributor II

I found another helpful link and solved this problem.

This is the link:

kinetis bootloader and application startup 

It was a Hardware ERRATA problem on the chip which causes the Clock Registers to not sync up when we are moving between Clock modes (FEI - FBE- PBE - PEE).
The application clock configuration from NXP had the fix to jump with syncing and resetting the clock register ( MCG->C1,C2,C4). But the bootloader didn't had the ERRATA fix.
So when the bootloader before jumping to application, it sets the clock configuration to FEI ( base clock mode).The app while doing its clock configuration is stuck in a while loop to get to PBE mode.
The fix was to add the ERRATA fix 
#if 1 
        int dummy_var = 0;
        for(int i = 0; i < 10000; i++)
        {
            dummy_var = MCG->S;
        }
        (void)dummy_var; // disable compiler warning
        // This is required due to ERR007993
        MCG->C4 &= ~MCG_C4_DMX32_MASK ;
#endif
in the clock_mode_switch() function  in the case for :
"else if (expectedMode == kClockMode_FEI)"
in clock_config_K22F12.c
This Fixed the problem of application caught in the loop forever.

View solution in original post

10 Replies
618 Views
marek_neuzil
NXP Employee
NXP Employee

Hello,

I have checked the clock configuration and the generated code and it seems correct.  But I am not sure what the KBOOT modify on the MCU. The generated BOARD_BootClockRUN() function suppose the after reset state of all devices. When the BOARD_BootClockRUN() function is used in different context (e.g. MCG device was already initialized by the KBOOT and the MCG mode was changed) the function BOARD_BootClockRUN() may not work properly.

Best Regards,

Marek Neuzil

0 Kudos
618 Views
kyleyang
Contributor IV

Hi Marek Neuzil,

I did not modify the KBOOT for KS22, the default setting should be PEE mode and clock source from IRC48M.

So as you said, if the clock configuration of KBOOT had already set to PEE mode.

If I try to setup clock mode in PEE again, the function may not work properly, right?

0 Kudos
618 Views
marek_neuzil
NXP Employee
NXP Employee

Hi Kyle,

The problem is that the BOARD_BootClockRUN() suppose the after-reset state of the MCG (FEI mode). If you use this method in the PEE MCG mode the behavior can be undefined.

It is caused by design of the MCG device that must be driven according to the following state diagram:

pastedImage_1.png

I.e. the BOARD_BootClockRUN() function provides transition from the FEI to the FBE, PBE and PEE modes of the MCG. But when the MCG device is in the PEE mode  and the BOARD_BootClockRUN() function is executed the behavior of the MCG device is undefined.

In your use case, you can for example use directly the CLOCK_SetPbeMode() function (fsl_clock.c driver) that support transition from PEE to PBE mode and reconfiguration of the PLL device and then you can set the PEE mode by using the CLOCK_SetPeeMode() function.

Best Regards,

Marek Neuzil

0 Kudos
618 Views
kyleyang
Contributor IV

Hi Marek Neuzil,

Yes, I know this clock transition flow and need to follow it when change the clock mode.

But according to the description in "CLOCK_SetPbeMode" API as attached snap shot.

This API can directly transition to PBE from any mode except PEI/PBI.擷取.JPG

0 Kudos
618 Views
marek_neuzil
NXP Employee
NXP Employee

Hi Kyle,

You are right, you can directly use the CLOCK_SetPbeMode() function (I have already recommended it). But the BOARD_BootClockRUN() function contains a call of the CLOCK_BootToPeeMode() that also writes into the OSCSEL bitfield of the MCG_C7 register. It can cause the issue (even though it select the same clock source).

Best Regards,

Marek Neuzil

0 Kudos
618 Views
kyleyang
Contributor IV

Hi Marek Neuzil,

OK... Is there any other way can change MCG mode from FEI to PEE and not stuck in the while loop.

This issue only occur in the case that leave KBOOT and jump to application.

When only application code available in flash without KBOOT, the issue will not duplicate anymore.

擷取.JPG

0 Kudos
618 Views
abhishekvishwak
Contributor II

Hey Kyle , 

I am also facing the same issue. Only when it jumps from KBOOT to application i am getting stuck in the while loop you mentioned. 

Were you able to solve it?

Please let me know it will be great help. 

Thanks in advance.

Regards, 

Abhi

0 Kudos
618 Views
kyleyang
Contributor IV

Hi Abhishek,

Actually, no...

But I enabled a watchdog module to assure system won't keep hold by the while loop.

I am not sure this solution is accept for you, but you can try it.

0 Kudos
619 Views
abhishekvishwak
Contributor II

I found another helpful link and solved this problem.

This is the link:

kinetis bootloader and application startup 

It was a Hardware ERRATA problem on the chip which causes the Clock Registers to not sync up when we are moving between Clock modes (FEI - FBE- PBE - PEE).
The application clock configuration from NXP had the fix to jump with syncing and resetting the clock register ( MCG->C1,C2,C4). But the bootloader didn't had the ERRATA fix.
So when the bootloader before jumping to application, it sets the clock configuration to FEI ( base clock mode).The app while doing its clock configuration is stuck in a while loop to get to PBE mode.
The fix was to add the ERRATA fix 
#if 1 
        int dummy_var = 0;
        for(int i = 0; i < 10000; i++)
        {
            dummy_var = MCG->S;
        }
        (void)dummy_var; // disable compiler warning
        // This is required due to ERR007993
        MCG->C4 &= ~MCG_C4_DMX32_MASK ;
#endif
in the clock_mode_switch() function  in the case for :
"else if (expectedMode == kClockMode_FEI)"
in clock_config_K22F12.c
This Fixed the problem of application caught in the loop forever.

View solution in original post

618 Views
marek_neuzil
NXP Employee
NXP Employee

Hi Kyle,

I am sorry I am not sure what is your issue. If you need to support both use cases (application with KBOOT and without KBOOT) you need to update the generated code of the BOARD_BootClockRUN() function. The function supports the startup initialization from the reset state only. When the MCG is in a different state the BOARD_BootClockRUN() may not work. You must update this generated function to detect the MCG mode and properly do the initialization in both use cases, e.g. if you detect the MCG PEE mode just call the CLOCK_SetPbeMode() and CLOCK_SetPeeMode() functions  instead of the call CLOCK_BootToPeeMode().

Best Regards,

Marek Neuzil

0 Kudos