Stack Pointer / Program counter

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

Stack Pointer / Program counter

5,916 Views
CCandido
Contributor V

Hi,

how to control the PC, to return to a function address ?.

funtion ( my_task, absolute addres )

set Stack Pointer initial value

set program  counter

Pccounter.bmp

goal is when there is an error in the program.

the ISR_timer  resets and returns the program,  to the beginning.

hope you understand, thank you.

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

3,101 Views
yasuhikokoumoto
Senior Contributor I

Hello carlos candido,

although I cannot catch your intention, do you wat to change the return addresses of the __ISR_times_1oous()?

The __ISR_times_1oous should be invoked by the intermediate function as the following.

void __attribute__ (( naked )) __ISR_times_1oous(void) /* for normal gcc */

{

asm volatile(

"movs r0, #4¥t¥n"

"mov r1, lr¥t¥n"

"tst r0, r1¥t¥n" /* Check EXC_RETURN[2] */

"beq 1f¥t¥n"

"mrs r0, psp¥t¥n"

"ldr r1,=__ISR_times_1oous_main¥t¥n"

"bx r1¥t¥n"

"1:mrs r0,msp¥t¥n"

"ldr r1,=__ISR_times_1oous_main¥t¥n"

: /* no output */

: /* no input */

: "r0" /* clobber *

);

or

void __stackless __ISR_times_1oous(void) /* for IAR gcc */

{

asm volatile(

"movs r0, #4¥t¥n"

"mov r1, lr¥t¥n"

"tst r0, r1¥t¥n" /* Check EXC_RETURN[2] */

"beq 1f¥t¥n"

"mrs r0, psp¥t¥n"

"ldr r1,=__ISR_times_1oous_main¥t¥n"

"bx r1¥t¥n"

"1:mrs r0,msp¥t¥n"

"ldr r1,=__ISR_times_1oous_main¥t¥n"

: /* no output */

: /* no input */

: "r0" /* clobber *

);

and, in both cases,

void __ISR_times_1oous_main(unsigned int *intr_args)

{

     intr_args[6]="return address"

}

I hope these will help you.

Best regards,

Yasuhiko Koumoto.

0 Kudos
Reply

3,101 Views
CCandido
Contributor V

Hi  Yasuhiko Koumoto,

I used your code, PEX projets.

void __ISR_times_1oous_main(unsigned int *intr_args) 

      uint32_t a=0,b=0;

                           TI1_Enable( ); // timer off

                LED1_On();

  

                     //return from ram....

  

                    // how to , clear all register to re-start ?

  for(;;) {

                            // load_my_code()];

                       // call_execute_code_in_ram ();

  

                   a++;

                   b++;

   }

}

//---------------------------------

void TI1_OnInterrupt(void)             timer 1 ms PE project

{

    asm volatile(

  

     "MOVS r0, #4\t\n"

          "MOV  r1, LR\t\n" 

          "TST  r0, r1\t\n"  /// Check EXC_RETURN[2] 

          "BEQ  1f\t\n"

          "MRS  r0, PSP\t\n"

           "LDR  r1,=__ISR_times_1oous_main\t\n"

    

          "BX   r1\t\n"

          "1:   MRS  r0, MSP\t\n" 

          "LDR  r1,=__ISR_times_1oous_main\t\n"

  );

}

//--------------------------------------------------

lack only reset the variables and registers, to launch the application again.

I have to turn off the timer in return because it causes error.

tks,

Carlos.

0 Kudos
Reply

3,101 Views
yasuhikokoumoto
Senior Contributor I

Hello,

you should not describe asm statement in a normal function.

The function will use the stack and then the asm statement will be useless.

Untill reach the __ISR_times_1oous_main, the stack pointer must not change.

So I added '__stackless__' attribute to the function.

The attribute will be valid for IAR's EWARM,

If you use GCC, the attribute would be '_attribute__((naked))'.

Best regards,

Yasuhiko Koumoto.

0 Kudos
Reply

3,101 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi Carlos,

Please let us know if your issue be resolved?

Thank you for the attention.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

3,101 Views
CCandido
Contributor V

Hi Ma,

I tested your code, step 1 = ok.

lack only reset the variables and registers, to launch the application again.

I have to turn off the timer in return because it causes error.

if you can do the reset ALL, thank you.

see code( simulator ).

tks.

Carlos.

0 Kudos
Reply

3,101 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi Carlos,

Please check below code about how to jumper to application code:

//-----------------------------------------------------------------------------

// FUNCTION:    JumpToUserApplication

// SCOPE:       Bootloader application system function

// DESCRIPTION: The function startup user application

//             

// PARAMETERS:  pointer on user vector table

//             

// RETURNS:     function never go back

//----------------------------------------------------------------------------- 

void JumpToUserApplication(LWord userSP, LWord userStartup)

{

    // set up stack pointer

  __asm("msr msp, r0");

  __asm("msr psp, r0");

  // Jump to PC (r1)

  __asm("mov pc, r1");

}

The Kinetis Initial PC located at address 0x0000_0004;

the initial Stack Pointer at address 0x0000_0000;

Wish it helps.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

3,101 Views
CCandido
Contributor V

Hi Ma,

my problems it Returned from ( execute code ram ) case have error.

ISR time periodic in Flash, case error ( exit to ) flash programa monitor.

//---------------------------------------------------------

void TI1_isr (void){  my timer in flash = 100uS.

-----

-----

return      _Nomals

or

return             _exit_to_flash_address

}

//---------------------------------------------------------------------

            void TI1_isr (void){            <-----Disassembly ( cw10.5 )

           push {r7}             <----- r7 = PC program counter ?  saved

          add r7, sp, #0

             }

     mov sp, r7

      pop {r7}

      bx lr

      nop

whats register load PC ( address pointer my code flash to return )?

tks,

Carlos.

0 Kudos
Reply

3,101 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi Carlos,

From <<Cortex™-M4 Devices Generic User Guide>> page16, there provides the processor core registers info below:

The PC at R15:

2015-06-16_15-08-18.png


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

3,101 Views
CCandido
Contributor V

Hi Ma,

I need an equivalent way to use the HC08, but with Kinetis.

you can make the output of Ram program, within a timer ISR.

the variables does not matter anymore, go back to the task and restart it.

See example exit:

__interrup_timer_100us_isr()

....  if(  exit  ) {

   __asm ldhx  , #_my_address_task_in_flash             // load Stack

   __asm txs;                 // store H:X into the SP

    __asm rti;                 // return from interrupt

           }

}

tks,

Carlos.

0 Kudos
Reply

3,101 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi Carlos,

Please also consider the ARM Thumb bit: function pointers (and vectors pointers) have the Thumb bit set in the address, to mark that they are thumb functions.

So when using the ARM Thumb instruction, the really PC address should plus 0x1.

Wish it helps.


Have a great day,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply