I have a problem with my context switch routine in Arm Cortex m3.
I have been trying to make a Delay function, but doesn't work.
When I put a Delay function inside of Task or internal variables, the context switch doesn't work and my task either.
Without Delay function and internal variables, the context works fine.
I think the problem is related with stack save but I don't know how solve.
Anybody have any idea how solve this question?
Below I put the main functions in process.
Thank you in advance.
Code: Select all
int main (void)
{
NVIC_SetPriority(SysTick_IRQn, 0xff);
NVIC_SetPriority(PendSV_IRQn, 0xff);
SystemCoreClockUpdate();
if (SysTick_Config(SystemCoreClock / 100)) {
while (1);
}
tss = (TSS *) (heap + (4096 - sizeof(TSS)));
tss2 = (TSS *) (heap2 + (4096 - sizeof(TSS)));
create_task(tss, vTask);
create_task(tss2, vTask2);
asm volatile ("MOV R2, R0");
asm volatile ("MOVS R0, #0");
asm volatile ("MSR PSP, R0");
*(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
while(1);
return 0;
}
Code: Select all
void create_task(TSS * tssr, void (*eip)(void))
{
tssr->psr = 0x21000000;
tssr->pc = (unsigned int) eip;
tssr->lr = 0;
tssr->r12 = 0;
}
Code: Select all
void Delay(unsigned int dlyTicks)
{
unsigned int curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks);
}
Code: Select all
void PendSV_Handler(void)
{
asm volatile ("CPSID I");
asm volatile ("MRS R0, PSP");
asm volatile ("STMDB R0!, {R4-R11}");
asm volatile ("PUSH {R14}");
asm volatile ("BL OS_ContextSwitchHook");
asm volatile ("POP {R14}");
asm volatile ("LDMIA R0!, {R4-R11}");
asm volatile ("MSR PSP, R0");
asm volatile ("ORR LR, LR, #0x04");
asm volatile ("CPSIE I");
asm volatile ("BX R14");
}
Code: Select all
void SysTick_Handler(void)
{
asm volatile ("CPSID I");
msTicks++;
if ( SysTick->CTRL & (1<16) )
{
*(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
}
asm volatile ("CPSIE I");
}
Code: Select all
void vTask(void)
{
LPC_GPIO1->FIODIR |= (1 << 18);
for (a = 0; a < MAX_T; a++)
{
LPC_GPIO1->FIOCLR = (1 << 18);
// for(c = 0; c < MAX_R; c++);
Delay(100);
LPC_GPIO1->FIOSET = (1 << 18);
// for(c = 0; c < MAX_R; c++);
Delay(100);
}
while(1);
}