This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[Cortex-M3] PendSV, svc and priorities

Note: This was originally posted on 15th June 2012 at http://forums.arm.com

Is it possible a case when PendSV will execute earlier than SVcall when both have the same priority? The cpu is Cortex-M3 STM32F1xx

I had an issue when PendSV and SVC have the same priority levels (lowest) with this code:

ePendSV_handler:
mrs r0,PSP
stmdb r0!,{r4-r11,lr}

; ... load ... load new PSP valu here to R0

ldmia r0!,{r4-r11,lr}
msr PSP,r0
bx lr


void eSVCall(void){
unsigned int a0,a1,a2,a3,svc,*psp;

psp=(U32*)__get_PSP();
a0=psp[0];a1=psp[1];a2=psp[2];a3=psp[3]; //get registers from user stack
svc=psp[4]; // service call ID

a0=service(svc,a0,a1,a2,a3);

psp[0]=a0; //overwrite R0(return value) in user stack
}

SVC usage in thread level code:
mov R12, #1 ; service call ID
mov R0, #123 ; arg
svc #0
; now R0 holds return value


PENDSVSET can be set asynchronously from timer interrupt or from service() function. Sometimes I had a situations when  ePendSV was called before  eSVCall and overwrited PSP register and then  eSVCall fails because new PSP is not tend to the thread which called svc instruction. When I incrased priority of SVcall exception the code above began working correct.

Can some exception execute and terminate executiong svc instruction when svc instruction is fetched but not executed yet?

sorry for bad english...
Parents
  • Note: This was originally posted on 15th June 2012 at http://forums.arm.com

    Thanks.
    I created some testcase for this situation
    __svc(42) void svc00(void);

    SCB->SHPR[11-4]=0xF0;  // system handler 11, SVCall
    SCB->SHPR[14-4]=0xF0;  // system handler 14, PendSV
    __cpsie_i();
    SCB->ICSR=SCB_ICSR_PENDSVSET;
    svc00();
    while(1);


    ePendSV
    bx lr

    eSVCall
    bx lr


    Initial stage:

    There is a breakpoints on  ePendSV and eSVCall.
    Then pressing F5. Now we are in PendSV(14) handler but SVCall(13) is in pending state!

    So the  SVCall will be tail-chained which results incorrect behaviour(using the stack pointer of thread which has not called SVC) within my previous example.

    Increasing priority of SVCall (setting SCB->SHPR[11-4]=0xE0; ) and running the code again

    Now we are in SVCall(13) handler and PendSV(14) is in pending state. The behaviour will be correct.
Reply
  • Note: This was originally posted on 15th June 2012 at http://forums.arm.com

    Thanks.
    I created some testcase for this situation
    __svc(42) void svc00(void);

    SCB->SHPR[11-4]=0xF0;  // system handler 11, SVCall
    SCB->SHPR[14-4]=0xF0;  // system handler 14, PendSV
    __cpsie_i();
    SCB->ICSR=SCB_ICSR_PENDSVSET;
    svc00();
    while(1);


    ePendSV
    bx lr

    eSVCall
    bx lr


    Initial stage:

    There is a breakpoints on  ePendSV and eSVCall.
    Then pressing F5. Now we are in PendSV(14) handler but SVCall(13) is in pending state!

    So the  SVCall will be tail-chained which results incorrect behaviour(using the stack pointer of thread which has not called SVC) within my previous example.

    Increasing priority of SVCall (setting SCB->SHPR[11-4]=0xE0; ) and running the code again

    Now we are in SVCall(13) handler and PendSV(14) is in pending state. The behaviour will be correct.
Children
No data