'EXC_RETURN does not work in PendSV on ARM Cortex-M3

I want to use EXC_RETURN in PendSV but I always get HardFault with INVPC (Invalid PC load UsageFault, caused by an invalid PC load by EXC_RETURN). This works, if I move the assembly code to Systick_Handler().

What do I miss?

I use NXP LPC1769 with the latest MCUXpressoIDE.

void Systick_Handler(void){
    SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
}

__attribute__ ((naked)) void Pendsv_Handler(void) {
    __ASM volatile (
    "NOP    \n"
    "ldr r0, =0xFFFFFFFD \n"
    "bx r0"
    );
}

int main(void) {

    SysTick_Config(100);

    __enable_irq();

    __set_PSP(0x10007800);

    __set_CONTROL(0x3); // Thread mode with PSP

    while(1) {
        __NOP();
    }
    return 0 ;
}

The Pendsv_Handler() compiled to:

          Pendsv_Handler:
000003b4:   nop     
000003b6:   mvn.w   r0, #2
000003ba:   bx      r0
27        }
000003bc:   nop    

Update: Based on Tom V comments. But still not working.

Solution

I missed to set the priority of SysTick and PendSV, like:

NVIC_SetPriority(PendSV_IRQn, 0xFF);
NVIC_SetPriority(SysTick_IRQn, 0x00);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source