?? chap5.c
字號(hào):
// Chapter 5 6811 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 5.4. C code for the two main programs and shared subroutine.int Sub(int j){ int i; PORTC=1; /* Port C=program is being executed */ i=j+1; return(i);}void ProgA(){ int i; i=5; while(1) { PORTC=2; i=Sub(i);}}void ProgB(){ int i; i=6; while(1) { PORTC=4; i=Sub(i);}}// Program 5.5. C code for the thread control block.struct TCB{ struct TCB *Next; /* Link to Next TCB */ unsigned char *SP; /* Stack Pointer when not running */ unsigned int Id; /* output to PortB visualizing active thread */ unsigned char MoreStack[49]; /* more stack */ unsigned char CCR; /* Initial CCR */ unsigned char RegB; /* Initial RegB */ unsigned char RegA; /* Initial RegA */ unsigned int RegX; /* Initial RegX */ unsigned int RegY; /* Initial RegY */ void (*PC)(void); /* Initial PC */};typedef struct TCB TCBType;typedef TCBType * TCBPtr;TCBType sys[3]={ { &sys[1], /* Pointer to Next */ &sys[0].MoreStack[49], /* Initial SP */ 1, /* Id */ { 0}, 0x40,0,0,0,0, /* CCR,B,A,X,Y */ ProgA, }, /* Initial PC */ { &sys[2], /* Pointer to Next */ &sys[1].MoreStack[49], /* Initial SP */ 2, /* Id */ { 0}, 0x40,0,0,0,0, /* CCR,B,A,X,Y */ ProgA, }, /* Initial PC */ { &sys[0], /* Pointer to Next */ &sys[2].MoreStack[49], /* Initial SP */ 4, /* Id */ { 0}, 0x40,0,0,0,0, /* CCR,B,A,X,Y */ ProgB, } }; /* Initial PC */// Program 5.6. C code for the thread switcher.TCBPtr RunPt; /* Pointer to current thread */#pragma interrupt_handler ThreadSwitch()void ThreadSwitch(){asm(" ldx _RunPt\n" " sts 2,x"); RunPt=RunPt->Next; PORTB=RunPt->Id; /* PortB=active thread */asm(" ldx _RunPt\n" " lds 2,x"); TOC3=TCNT+20000; /* Thread runs for 10 ms */ TFLG1=0x20; } /* ack by clearing TOC3F */void main(void){ DDRC=0xFF; /* PortC outputs specify that program is running */ RunPt=&sys[0]; /* Specify first thread */asm(" sei"); TOC3vector=&ThreadSwitch; TFLG1 = 0x20; /* Clear OC3F */ TMSK1 = 0x20; /* Arm TOC3 */ TOC3=TCNT+20000; PORTB=RunPt->Id;asm(" ldx _RunPt\n" " lds 2,x\n" " cli\n" " rti");} /* Launch First Thread */// Program 5.7. C function to create a new thread.void create(void (*program)(void), int TheId){ TCBPtr NewPt; // pointer to new thread control block NewPt=(TCBPtr)malloc(sizeof(TCBType)); // space for new TCB if(NewPt==0)return; NewPt->SP=&(NewPt->CCR-1); /* 6811 Stack Pointer when not running */ NewPt->Id=TheId; /* used to visualize active thread */ NewPt->CCR=0x40; /* Initial CCR, I=0 */ NewPt->RegB=0; /* Initial RegB */ NewPt->RegA=0; /* Initial RegA */ NewPt->RegX=0; /* Initial RegX */ NewPt->RegY=0; /* Initial RegY */ NewPt->PC=program; /* Initial PC */ if(RunPt){ NewPt->Next=RunPt->Next; RunPt->Next=NewPt;} /* will run Next */ else RunPt=NewPt; /* the first and only thread */// Program 5.11. C code for a counting semaphore.struct sema4 // counting semaphore based on 3 binary semaphores{ int value; // semaphore value char s1; // binary semaphore char s2; // binary semaphore char s3; // binary semaphore};typedef struct sema4 sema4Type;typedef sema4Type * sema4Ptr;void Wait(sema4Ptr semaphore){ bWait(&semaphore->s3); // wait if other caller to Wait gets here first bWait(&semaphore->s1); // mutual exclusive access to value (semaphore->value)--; // basic function of Wait if((semaphore->value)<0){ bSignal(&semaphore->s1); // end of mutual exclusive access to value bWait(&semaphore->s2); // wait for value to go above 0 } else bSignal(&semaphore->s1); // end of mutual exclusive access to value bWait(&semaphore->s3); // let other callers to Wait get in} void Signal(sema4Ptr semaphore){ bWait(&semaphore->s1); // mutual exclusive access to value (semaphore->value)++; // basic function of Signal if((semaphore->value)<=0) bSignal(&semaphore->s2); // allow S2 spinner to continue bSignal(&semaphore->s1); // end of mutual exclusive access to value}void Initialize(sema4Ptr semaphore, int initial){ semaphore->s1=1; // first one to bWait(s1) continues semaphore->s2=0; // first one to bWait(s2) spins semaphore->s3=1; // first one to bWait(s3) continues semaphore->value=initial;}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -