the rtos’s thread module is complex and i can’t make it work,so
i wrote my own createthread function that similar to the rtos ,and this is my code:
typedef void (*Thread_t)();
void CreatThread( Thread_t func, unsigned char *stack )
{
unsigned char *temp = stack;
unsigned char *begin = stack;
temp--; //SP reg
temp--;
*temp = ( unsigned long )func; //page reg
temp--;
*temp = ( unsigned long )func >> 8; //function address
temp--;
*temp = ( unsigned long )func >> 16; //function address
temp--;
*temp = 0xff; //Y reg
temp--;
*temp = 0xee; //Y reg
temp--;
*temp = 0xdd; //X reg
temp--;
*temp = 0xcc; //X reg
temp--;
*temp = 0; //A reg
temp--;
*temp = 0; //B reg
temp--;
*temp = 0; //CCR
temp--; //
*begin = (unsigned short)temp; //top of the stack that the cpu can capture the return address
begin--;
*begin = (unsigned short)temp >> 8; //top of the stack that the cpu can capture the return address
}
short temp;
interrupt void overflow(void)
{
DisableInterrupts;
if(ECT_TFLG2_TOF ==1)
{
portSAVE_CONTEXT();
CurCtx = (CurCtx+1)%2;
pxCurrentTCB = &Stacks[CurCtx][498];
portRESTORE_CONTEXT();
temp=ECT_TCNT; //clear the flag of TOF
}
EnableInterrupts;
}
main function:
DisableInterrupts;
CreatThread( TestThread, &Stacks[0][499] );
CreatThread( NewMainTimer, &Stacks[1][499] );
CurCtx = 0;
pxCurrentTCB = &Stacks[CurCtx][498];
TickTimer_Enable();
portRESTORE_CONTEXT();
__asm(“rti”);
it sometimes can work and sometimes doesn’t .
thanks for any help.