Problem of initialization of MQX after jumping to the start of MQX application ( _boot() )

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Problem of initialization of MQX after jumping to the start of MQX application ( _boot() )

跳至解决方案
3,240 次查看
dmitriym
Contributor II

Hello!

I have a problem with my MQX application with initializing MQX after jumping to _boot(). Actualy after jumping to _boot() the applcation starts normaly, but in mqx intitialization in function in mqx_main.c:

/* Start MQX */

   _mqx( (MQX_INITIALIZATION_STRUCT_PTR) &MQX_init_struct );

the application crashes while exetuting function:

_mem_zero((pointer) kernel_data, (_mem_size) sizeof(KERNEL_DATA_STRUCT));

And I dont understand why this is happening. Actually the same jump to the start of bareboard application without MQX and BSP and PSP libraries, executes normaly.

Disabling interrupts before jumping to _boot() in MQX app have no effect, and the application crashes in the same place. I used board TWRK70F120M, Codewarrior 10.5, Mqx 4.0.2.

Application has only one task with jumping to _boot():

void Main_task(uint_32 initial_data)

{

   uint32_t pc = ((uint32_t*)(0x00000000))[1];  // get _boot() addres from PC

   ((void(*)(void))pc)();                                    //jump to _boot()

   _mqx_exit(0);

}

So what i need to do before jumping to _boot() to make it work?


0 项奖励
回复
1 解答
1,959 次查看
dmitriym
Contributor II

I found the reason of the crash.

While debugging the application after reboot i found that ARM registers: SP - stack pointer, MSP - main stack pointer, PSP - process stack pointer, are not in the default states. This registers must be equal to the value in 0x00000000 address. After soft reset this registers resets too, but when we use soft reboot we must reset them manually to make MQX initialize correctly. So the code that makes softreboot:

void Main_task(uint_32 initial_data)

{

  /*Soft Reboot Code*/

  asm("LDR r4,=0x00");

  asm("LDR sp, [r4]");

  asm("LDR r5, [r4]");

  asm("MSR MSP, r5");

  asm("LDR r4,=0x04");

  asm("LDR r5, [r4]");

  asm("BLX r5");

  _mqx_exit(0);

}

Thanks David for your help.

在原帖中查看解决方案

0 项奖励
回复
10 回复数
1,959 次查看
qingjunwang
Contributor III

Hi Dmitriy Magazeev

I had run your code on FRDM-K64F, development environment is Keil, base on MQX 4.1. The following code:

void  SoftReboot(void)  {

  asm("LDR r4,=0x00");

  asm("LDR sp, [r4]");

  asm("LDR r5, [r4]");

  asm("MSR MSP, r5");

  asm("LDR r4, 0x04");

  asm("LDR r5, [r4]");

  asm("BLX r5");

  _mqx_exit(0);

}

The problem is  Undefined symbol asm  when i compile it .

I can't solve this problem, Can you help me? Thank you!

0 项奖励
回复
1,959 次查看
DavidS
NXP Employee
NXP Employee

Hi Wang,

Please look at:

http://www.keil.com/forum/21379/

Regards,

David

0 项奖励
回复
1,960 次查看
dmitriym
Contributor II

I found the reason of the crash.

While debugging the application after reboot i found that ARM registers: SP - stack pointer, MSP - main stack pointer, PSP - process stack pointer, are not in the default states. This registers must be equal to the value in 0x00000000 address. After soft reset this registers resets too, but when we use soft reboot we must reset them manually to make MQX initialize correctly. So the code that makes softreboot:

void Main_task(uint_32 initial_data)

{

  /*Soft Reboot Code*/

  asm("LDR r4,=0x00");

  asm("LDR sp, [r4]");

  asm("LDR r5, [r4]");

  asm("MSR MSP, r5");

  asm("LDR r4,=0x04");

  asm("LDR r5, [r4]");

  asm("BLX r5");

  _mqx_exit(0);

}

Thanks David for your help.

0 项奖励
回复
1,959 次查看
DavidS
NXP Employee
NXP Employee

Hi Dmitriy,

Awesome!  Glad to work with you.

Regards,

David

0 项奖励
回复
1,959 次查看
DavidS
NXP Employee
NXP Employee

Hi Dmitriy,

I ran a test with CW10.6+MQXLite(/w PE)+TWR-K60F120M tower.
extern void __boot(void);

void Task1_task(uint32_t task_init_data)
{
  int counter = 0;
  void (*Ptr_restart)(int);
 
  while(1) {
    counter++;

    /* Write your code here ... */
    _time_delay_ticks(100);
    if(counter == 5) {            //DES after 2.5 seconds then soft reset by jumping to __boot
       (Ptr_restart) = &__boot;
       Ptr_restart(NULL);
    }
  }
}

This works.

Baremetal version that is working too.

extern void __boot(void);
extern void __thumb_startup(void) _EWL_NAKED;

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */
         void (*Ptr_restart)(int);

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */
  unsigned int counter=0, delay;
 
  while(1) {
         counter++;
         for(delay=0;delay<100000;delay++){};
           if(counter == 5) {            //DES after counter==5 then soft reset by jumping to __boot
              (Ptr_restart) = &__thumb_startup;        //DES was __boot;
              Ptr_restart(NULL);
           }
        
  }

Hope this helps.

Regards,
David

0 项奖励
回复
1,959 次查看
dmitriym
Contributor II

Thanks, but as i have already told, i have tried to make the same jump to the application start in the project without BSP and PSP and that jump works normally. I have problems only with MQX project with BSP and PSP libraries.

0 项奖励
回复
1,959 次查看
DavidS
NXP Employee
NXP Employee

Hi Dmitriy,

Sorry.  I didn't recall you are using TWR-K70F120M.

I had run my example code on TWR-K60F120M which is similar but slightly different too.

K70F120M is gets a core lockup and take 30seconds to recover before resetting and continuing.

The MQX inherently believes the system is booting from POR and therefore the init code thinks it is using internal clocks and proceeding with the initialization sequence.  At somepoint the core lockup occurs (this is generate when the flash is being clocked too fast) during the initialization sequence.

Is there a reason to issue soft reboot rather than a soft reset using SCB_AIRCR register?

With a soft reset the entire chip is going back to default (or POR) conditions and the system will re-initialize properly.

With a soft reboot the peripherals and clocks remain the initialized and the code is trying to re-initialize them which can be problematic.  MQX currently doesn't have a "soft" shutdown function to disable the peripherals and place clocks into POR configuration prior to soft reboot.

I just tried this code on a TWR-K60D100M and it works like the TWR-K60F120M did (i.e. successful to do a soft reboot).  Not sure if it is just lucky or what.

Regards,

David

0 项奖励
回复
1,959 次查看
dmitriym
Contributor II

Hi, David!

Thanks for your reply.

Soft reset with SCB_AIRCR register works fine, and i have no problems with soft reset, actually i have problems with soft reboot.

I don't think, that the problem is in reinitializing of the clock. The clock initializing happens in function _bsp_initialize_hardware() before initializing of MQX. So after soft reset or after jumping to _boot after function _bsp_initialize_hardware() the clocks settings are equal. The only difference is that after soft reboot some periferia clocks is not disabled. And that periferia clocks are not the reason of core lockup, because disabling of them before soft reboot has no effect. Disabling of initializing periferia of all periferia in user_config.h also has no effect. So the question of core lockup after soft reboot is still have no answer for me.

Because of soft reset works fine, it is obviously that I need to set to default state some mcu registers before soft reboot to make it work.  

The problem that i discibed is not the only problem of soft reboot. If i want to use bootloader with my MQX application i will need to set to default state the same registers of mcu bufore jumping to main MQX application. If i don't do that i will have same core lockup while MQX initialization.

0 项奖励
回复
1,959 次查看
DavidS
NXP Employee
NXP Employee

Hi Dmitriy,

Ok.  Good to know the software reset is working correctly.  Thank you for your details on what you have tried.  Much appreciated.

I played more with TWR-K70F120M and thought maybe the caches needed to be disabled with soft reset but that did not seem to help.

I continuously seemed to loose control in the following function in PSP task.c:

void _task_fill_stack_internal
(
  volatile _mqx_uint_ptr addr_ptr,  //DES??? did volatile help
  volatile _mqx_uint     size   //DES??? did volatile help
)
{ /* Body */

    size = (size/sizeof(_mqx_uint)) + 1;  /* Calculate # _mqx_uints */
    while (--size)
    {
        *addr_ptr++ = MQX_STACK_MONITOR_VALUE;
    } /* Endwhile */

} /* Endbody */

I added the "volatile, re-compiled PSP, then my MQX application and now the soft reset is working.  Not sure why.  Will you please try?

My Terminal Output:

Starting soft re-boot example: . . . . .
Issueing soft re-boot now :-þ

Starting soft re-boot example: . . . . .
Issueing soft re-boot now :-þ

Starting soft re-boot example: . . . . .
Issueing soft re-boot now :-þ

Starting soft re-boot example: . . . . .
Issueing soft re-boot now :-þ

Regards,

David

1,959 次查看
dmitriym
Contributor II

Hello, David.

I tried to add "volatile" and i recompiled PSP and then the problem seems to be solved, but it is not.

I tried to debug the application to be sure and i found that problem is still not solved. The application stills gets core lockup in the same place, but it doesn't wait for 30 seconds to execute softreset, it executes softreset immediately. Then application boots and mqx initialization pass without core lockup. So soft reboot becomes a reason of immidiately soft reset and the application seems to be working in your terminal. But success softreboot still does not happen, sorry.

0 项奖励
回复