📄 example2.c
字号:
/*> example2.c <*//*---------------------------------------------------------------------------*//* This is a modified version of example1 from the uC/OS book. * * $Revision: 0.1 $ * $Author: jsmith $ * $Date: 941103 $ * * Copyright (c) 1994, VLSI Technology Inc. All Rights Reserved. *//*---------------------------------------------------------------------------*/#include "ucos.h" /* uC/OS interface */#include "osdefs.h" /* uC/OS ARM interface */#define NUM_TASKS 3 /* must be number in range 1 to 62 */#define ESC 27/* * P r o t o t y p e s */void Task(void *id);void IdleTask( void *id);void Timer_IRQ( void );void Panic_IRQ( void );/* allocate memory for tasks' stacks */#define TASK_STK_SIZE 300/* * G l o b a l V a r i a b l e s */OS_EVENT *DispSem;uint TaskStk[NUM_TASKS+1][TASK_STK_SIZE];IRQHandlerFn prev_hand; /* function pointer to previous handler */void *pZERO = 0; /* need a pointer to value of 0 */char BLACK[] = {ESC,'[','3','0','m',0};char RED[] = {ESC,'[','3','1','m',0};char GREEN[] = {ESC,'[','3','2','m',0};char BROWN[] = {ESC,'[','3','3','m',0};char BLUE[] = {ESC,'[','3','4','m',0};char MAGENTA[] = {ESC,'[','3','5','m',0};char CYAN[] = {ESC,'[','3','6','m',0};char WHITE[] = {ESC,'[','3','7','m',0};/* * T a s k * * This is being used by NUM_TASKS tasks. */void Task(void *id){ uint err; int j; while(FOREVER) { OSTimeDly(((*(int *)id&0x1F)<<2)); /* Delay a while */ for(j=0; j<20; j++) { OSSemPend(DispSem, 0, &err); switch(*(int *)id) { case '1' : SWI_Write0(RED); SWI_WriteC(*(int *)id); /* print task's id */ break; case '2' : SWI_Write0(BLUE); SWI_WriteC(*(int *)id); /* print task's id */ break; case '3' : SWI_Write0(GREEN); SWI_WriteC(*(int *)id); /* print task's id */ break; default: SWI_Write0(WHITE); SWI_WriteC(*(int *)id); /* print task's id */ } OSSemPost(DispSem); } }}/* * I d l e T a s k * * idle task */void IdleTask(void *id){ uint err; while(FOREVER) { OSSemPend(DispSem, 0, &err); SWI_Write0(WHITE); SWI_WriteC('.'); OSSemPost(DispSem); OSSched(); }}/* * m a i n */int main( void ){ int id[NUM_TASKS]; int j; union gp { unsigned char *b; unsigned int *w; } p; p.b = (unsigned char *) IOBase; SWI_Write0("\nExample 2 using uC/OS for PID600\n\n\0"); SWI_Write0(CYAN); SWI_Write0("\nThere are four tasks. Tasks 1, 2, and 3 display as numbers\0"); SWI_Write0("\nand the last (idle) displays as a '.' (dot). This routine\0"); SWI_Write0("\ngraphically displays how the highest ready to run task can\0"); SWI_Write0("\npreempt a lower running task.\n\0"); SWI_Write0("\nP.S. Press the PANIC button to induce an async. interrupt.\n\n\0"); SWI_Write0(WHITE); SWI_Write0("\nGenerating ID's...\0"); for(j=0; j<NUM_TASKS; j++) /* generate the ID's */ { id[j] = (int)'1' + j; /* create an id we can see */ } SWI_Write0("\nDoing an OSInit()...\0"); OSInit(); /* needed by uC/OS */ DispSem = OSSemCreate( 1 ); /* Display semaphore */ SWI_Write0("\nCreating the tasks...\0"); for(j=0; j<NUM_TASKS; j++) /* generate the tasks */ { OSTaskCreate(Task, (void *)&id[j], (void *)&TaskStk[j][TASK_STK_SIZE], j+1); } OSTaskCreate(IdleTask,(void *)&pZERO,(void *)&TaskStk[j][TASK_STK_SIZE], j+1); SWI_Write0("\nHooking into the C-DEMON's PANIC button...\0"); /* * Now HOOK the DEMON's PANIC button to uC/OS */ /* * PANIC is bit position 7 in IRQ (vecnum = IRQbitvector+bit1 = 0x20 + 7) */ if (ARMInstallIRQHandler(7,Panic_IRQ) == NULL) { SWI_Write0("\nError: Unable to install PANIC handler.\n") ; } SWI_Write0("\nHooking into the C-DEMON's TIMER...\0"); /* * Now HOOK the DEMON's TIMER to uC/OS */ *(p.b + IRQM) = 0; /* stop the TIMER interrupt */ /* * TIMER is bit position 1 in IRQ (vecnum = IRQbitvector+bit1 = 0x20 + 1) */ if ((prev_hand = ARMInstallIRQHandler(1,Timer_IRQ)) == NULL) { SWI_Write0("\nError: Unable to install timer handler.\n") ; } *(p.b + IRQM) = TimerIT; /* restart the TIMER interrupt */ SWI_Write0("\nDoing an OSStart()\n\n\0"); OSStart(); /* start the game */ /*----------------- never reached ----------------------------------------- * * Following code is here to show how to restore the original timer vector */ *(p.b + IRQM) = 0; /* remove the TIMER interrupt */ /* * TIMER is bit position 1 in IRQ (vecnum = IRQbitvector+bit1 = 0x20 + 1) */ if (ARMInstallIRQHandler(1,prev_hand) == NULL) { SWI_Write0("\nError: Unable to reinstall handler.\n") ; } *(p.b + IRQM) = TimerIT; /* restart the TIMER interrupt */}/****************************************************************************** * * P a n i c _ I R Q * * ENTER: void * EXIT: void * * When the PANIC button is pushed, issue a message */void Panic_IRQ( void ){ union gp { unsigned char *b; unsigned int *w; } p; p.b = (unsigned char *) IOBase; *(p.b + IRQRST) = Panic; /* reset any PANIC interrupt */ SWI_Write0(CYAN); SWI_Write0("***OUCH***\0");}/****************************************************************************** * * T i m e r _ I R Q * * ENTER: void * EXIT: void * * Timer interrupt routine */void Timer_IRQ( void ){ union gp { unsigned char *b; unsigned int *w; } p; p.b = (unsigned char *) IOBase; *(p.b + IRQRST) = TimerIT; /* reset Timer interrupt */ OSTimeTick(); /* do uC/OS tick routine */}/*---------------------------------------------------------------------------*//*> EOF example2.c <*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -