Sam Sneederling

My first scheduler..... ugggh

Discussion created by Sam Sneederling on Oct 14, 2011
Latest reply on Oct 16, 2011 by kef

Hey all.  I've been a casual programmer for a couple of years and have done alright but I'm taking a class on embedded microprocessors and we're learning about real time systems. We're learning about schedulers and it is so confusing. If you all know of a couple of good tutorials please let me know.

 

I'm trying to write a scheduler which will set up two threads. Each thread will require 500 ms to complete. My code will compile but I get an illegal breakpoint or Trigger A occured error message. 

 

#include <hidef.h>      /* common defines and macros */
#include <MC9S12C128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12c128"
 
void Task1(void){
  //DO SOMETHING
}

void Task2(void){  //non real time
   //DO SOMETHING
}

struct TCB{
  unsigned char *StackPt;    //Stack Pointer
  unsigned char MoreStack[91];//100 bytes if stack
  unsigned char InitialReg[7];//initial CCR,B,A,X,Y
  void (*InitialPC)(void);
};

typedef struct TCB TCBType;
TCBType *RunPt;

#define TheTask1 &sys[0] //real time
#define TheTask2 &sys[1] //non real time

TCBType sys[2]={
{ TheTask1.InitialReg[0],{ 0},{0x40,0,0,0,0,0,0},Task1},
{ TheTask2.InitialReg[0],{ 0},{0x40,0,0,0,0,0,0},Task2}
};

struct Node{
  struct Node *Next;
  TCBType *ThreadPt;
  unsigned short TimeSlice;
};

typedef struct Node NodeType;

NodeType *NodePt;
NodeType Schedule[2]={ 
{ &Schedule[1], TheTask1, 500},
{ &Schedule[0], TheTask2, 500}
};

void OS_Sleep(void){ //all this does is run non real time task
asm swi 
}

interrupt 4 void swiISR(void){
 
  asm ldx RunPt   //cooperative multitasking
  asm sts 0,X     //thread goes to sleep when it's done
  RunPt = TheTask2;  //none real time thread
  asm ldx RunPt
  asm lds 0,x
}

interrupt 11 void threadSwitchISR(void){
asm ldx RunPt
asm sts 0,x
NodePt = NodePt->Next;
RunPt = NodePt->ThreadPt;
TC3 = TC3+NodePt->TimeSlice;
TFLG1 = 0x08;
asm ldx RunPt
asm lds 0,x
}

 


void main(void) {

NodePt = &Schedule[0];
RunPt = NodePt->ThreadPt;
TIOS |= 0x08;
TSCR1 = 0x80;
TSCR2 = 0x02;
TIE |= 0x08;
TC3 = TCNT+NodePt->TimeSlice;
TFLG1 = 0x08;
 
asm ldx RunPt;
asm lds 0,x;
asm rti

}

Outcomes