📄 example5.c
字号:
/*> example5.c <*//*---------------------------------------------------------------------------*//* This shows how to send a message from either a TIMER IRQ or a PANIC * IRQ based on "example4.c". Requires uC/OS Real-time multitasking * kernel for the ARM processor. * * $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 specific definitons */#define NUM_TASKS 2 /* must be number in range 1 to 62 */#define ESC 27#define P_BOX '1'#define I_BOX '2'/* * 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 timer handler */void *pZERO=(void *)0; /* need a pointer to value of 0 */void *pPmsg;void *pTmsg;OS_EVENT *pPevt;OS_EVENT *pTevt;char BLACK[] = {ESC,'[','0',';','3','0','m',0};char RED[] = {ESC,'[','0',';','3','1','m',0};char GREEN[] = {ESC,'[','0',';','3','2','m',0};char BROWN[] = {ESC,'[','0',';','3','3','m',0};char BLUE[] = {ESC,'[','0',';','3','4','m',0};char MAGENTA[] = {ESC,'[','0',';','3','5','m',0};char CYAN[] = {ESC,'[','0',';','3','6','m',0};char WHITE[] = {ESC,'[','0',';','3','7','m',0};char YELLOW[] = {ESC,'[','1',';','3','3','m',0};char BR_RED[] = {ESC,'[','1',';','3','1','m',0};char BR_GREEN[] = {ESC,'[','1',';','3','2','m',0};char BR_BLUE[] = {ESC,'[','1',';','3','4','m',0};char BR_MAGENTA[] = {ESC,'[','1',';','3','5','m',0};char BR_CYAN[] = {ESC,'[','1',';','3','6','m',0};char BR_WHITE[] = {ESC,'[','1',';','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) { for(j=0; j<NUM_TASKS; j++) { switch(*(int *)id) { case P_BOX : OSMboxPend(pPevt, 0, &err); /* wait for message from PANIC */ break; case I_BOX : OSMboxPend(pTevt, 0, &err); /* wait for message from TIMER */ break; } OSSemPend(DispSem, 0, &err); switch(*(int *)id) { case P_BOX : SWI_Write0(RED); SWI_WriteC(*(int *)id); /* print task's id */ SWI_WriteC((int)pPmsg); /* print task's msg */ break; case I_BOX : SWI_Write0(BLUE); SWI_WriteC(*(int *)id); /* print task's id */ SWI_WriteC((int)pTmsg); /* print task's msg */ break; } OSSemPost(DispSem); } /* ***OSTimeDly(((*(int *)id&0x1F)<<2));*** */ /* Delay a while */ }}/* * 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 5 using uC/OS for PID600\n\n\0"); SWI_Write0(CYAN); SWI_Write0("\nThere are three tasks. Tasks 1 and 2 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); } pPmsg = pZERO; pPevt = OSMboxCreate(pPmsg); pTmsg = pZERO; pTevt = OSMboxCreate(pTmsg); pPmsg = (void *)'A'; IRQMboxPost(pPevt,pPmsg); pTmsg = (void *)'B'; IRQMboxPost(pTevt,pTmsg); OSTaskCreate(IdleTask,(void *)&pZERO,(void *)&TaskStk[j][TASK_STK_SIZE], j+1); /* * Now HOOK the DEMON's PANIC button to uC/OS */ SWI_Write0("\nHooking into the C-DEMON's PANIC button...\0"); /* * 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){ static int msg; union gp { unsigned char *b; unsigned int *w; } p; p.b = (unsigned char *) IOBase; *(p.b + IRQRST) = Panic; /* reset any PANIC interrupt */ msg ++; msg &= 7; /* keep cycling 0,1,2,3,4,5,6,7,0,1,2... */ msg |= '0'; pPmsg = (void *)msg; IRQMboxPost(pPevt,pPmsg); /* Post the message */ SWI_Write0(CYAN); SWI_Write0("***OUCH***\0"); return ;}/****************************************************************************** * * T i m e r _ I R Q * * ENTER: void * EXIT: void * * Timer interrupt routine */void Timer_IRQ(void){ static int msg; static int cnt; 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 */ cnt++; cnt &= 0x0F; /* once every 10mS X 16 or every 0.16 seconds */ if(cnt == 0) { msg++; msg &= 7; /* keep cycling 0,1,2,3,4,5,6,7,0,1,2... */ msg |= '0'; pTmsg = (void *)msg; IRQMboxPost(pTevt,pTmsg); /* Post the message */ } return ;}/*---------------------------------------------------------------------------*//*> EOF example5.c <*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -