Setjmp ( return from ISR )

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

Setjmp ( return from ISR )

Jump to solution
1,539 Views
CCandido
Contributor V

Hi,

using setjmp, I need it to do the longjmp, returning from a ISR (systick 1ms). lonjmp when it happens the first time, it disables all ISR.

I need all ISR, working properly. my qustion.

KDS300

FRDM-K20D50

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

PE_ISR(_Systick)  // function in Flash

{

          if( ++ dly == 200){

                dly = 0;

                spt ++;

                LED1_Neg();

 

            if(    ! Exit_test ){

 

                Exit = TRUE;  // exit RAM execute

                Exit_test = TRUE;  // run = 1

 

            }else{

 

                Exit = FALSE;

                return;

            }

            }

 

          if( Exit ){                                              // case, error or watchdog timer, in code execute ram.

              longjmp(CPU_STD , -1 );               //  restored cpu,   return to  __ISR_times_1oous_main

                                                                   // in ASM list( bl    longjmp ) not ( BX LR ) to RTI

          }

 

}

Original Attachment has been moved to: K20_Setjmp_isr.zip

Tags (1)
0 Kudos
1 Solution
1,040 Views
ndavies
Contributor V

You are correct , It doesn't work. There is no way to make this work the way you are doing it.

You need to find another way to do it.

What you are trying to do is difficult and I don't have enough time to guide you through it.

However, Here are some hints as to how I would do it:

You need to look at using multitasking techniques. You will need to use both the handler stack pointer and the thread stack pointer. You will need to create at least a couple of stack frames. You will need to change the thread stack pointer to point to different stack frames in the interrupt.  By modifying the thread stack pointer before you exit the ISR you should be able to get the ISR to return to the place you want.

View solution in original post

0 Kudos
9 Replies
1,040 Views
yasuhikokoumoto
Senior Contributor I

Hello,

I think my past post Re: Stack Pointer / Program counter will help you.

Your experiment under no interrupts will not go well because LR contains EXC_RETURN code and the true return address exists on the stack.

Best regards,

Yasuhiko Koumoto.

0 Kudos
1,040 Views
CCandido
Contributor V

Hi Koumoto,

I tested your code, it returns yes, however disables all ISR.
for this reason I tried to make setjmp.

Thanks,

Carlos.

0 Kudos
1,040 Views
ndavies
Contributor V

Have you downloaded  the Cortex™-M4 Devices Generic User Guide form the ARM.com website. It explains the things you need to do to get out of interrupt mode. It's in section 2.3.7 of that document.

The  there's only a few commands that will release you from the interrupt. The bl command does not release you from interrupt mode.

0 Kudos
1,040 Views
CCandido
Contributor V

Hi Norm,

I am studying the manual, but this hidden code to me.

return to another location is easy, the problem is that disables all interrupts.

I need only solve the problems (Disable all ISR).

still I know very little of ARM.

thanks,

Carlos.

0 Kudos
1,040 Views
ndavies
Contributor V

When you enter the interrupt the M4 pushes all its registers onto the stack, the M4 then enters "HANDLER" mode. it starts running your ISR. in handler mode all interrupts of a lower or equal priority are blocked.

Only a few commands will let you exit "HANDLER" mode. In your code you are doing a branch long. By doing a branch long the M4 thinks you are still in "HANDLER" mode and continues to block lower level interrupts.

The only commands that will let you leave "HANDLER" mode and have the interrupts enabled are listed in section 2.3.7 of the manual.

From section 2.3.7

Exception return

Exception return occurs when the processor is in Handler mode and executes one of the

following instructions to load the EXC_RETURN value into the PC:

•        an LDM or POP instruction that loads the PC

•        an LDR instruction with PC as the destination

•        a BX instruction using any register.

To get interrupts back on and the data that was pushed onto the stack, off the stack, you must run one of the commands listed in the copied section. Basically you must push the EXC_RETURN code into the program counter.

0 Kudos
1,040 Views
CCandido
Contributor V

Hi Norm,

longjmp not run ok.

see code Disassembly KDS.

---------------------------------------------

PE_ISR(_Systick)  // function in Flash

  {

             mov    r0, sp

               bic    r1, r0, #7

              mov    sp, r1

              push    {r0, r3, r7, lr}

             blablabla.....my test here

}

             mov    sp, r7

                 pop    {r0, r3, r7, lr}

           mov    sp, r0

             bx    lr            exit

-----------------------------------------------

do my testing width bx,PC, isr Disable.

tks,

Carlos.

0 Kudos
1,041 Views
ndavies
Contributor V

You are correct , It doesn't work. There is no way to make this work the way you are doing it.

You need to find another way to do it.

What you are trying to do is difficult and I don't have enough time to guide you through it.

However, Here are some hints as to how I would do it:

You need to look at using multitasking techniques. You will need to use both the handler stack pointer and the thread stack pointer. You will need to create at least a couple of stack frames. You will need to change the thread stack pointer to point to different stack frames in the interrupt.  By modifying the thread stack pointer before you exit the ISR you should be able to get the ISR to return to the place you want.

0 Kudos
1,040 Views
CCandido
Contributor V

Hi Norm,

I thought this way would be easier than multi tasks, because I need to run programs, not just a function.

I will need then to develop two tasks.

thanks for your help.

Carlos.

0 Kudos
1,040 Views
ndavies
Contributor V

Hi Carlos. if you need to run multiple programs you definitely need to take the multitasking route. Pulling control back from a spawned program that has failed requires multitasking techniques. There's no way around it.

Be thankful the M4 core didn't allow you implement your algorithm. Your code would have created an unstable program. Every time you detected an error and took the long jump, the ISR would have stranded bytes on your stack. You would have the equivalent of a stack memory leak.  After time this would create a stack failure. It would have been very difficult to find.

Norm

0 Kudos