⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtest.c

📁 各种硬件平台上的us OS移植实例(arm6)
💻 C
字号:
/*> qtest.c <*//*---------------------------------------------------------------------------*//* Program to test the uC/OS "Q" interface. * * $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	4	/* must be number in range 1 to 62 */#define		ESC			27#define		QDEPTH		32	/* number of entries in queue */#define		T_O_IN		50	/* number of ticks before timeout (apox .5 sec)*/#define		T_O_OUT		400	/* number of ticks before refilling queue *//* * P r o t o t y p e s */void	TaskColor(void *id);void	TaskBrightColor(void *id);uint	OneFill(void *id);uint	OneEmpty(void *id);void	TaskFill(void *id);void	TaskEmpty(void *id);void	TaskEmptyFill(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;OS_EVENT	*pQecb;		/* queue pointer to event control block */uint		 TaskStk[NUM_TASKS+1][TASK_STK_SIZE];uint		 Qmsg[QDEPTH];IRQHandlerFn prev_hand;	    /* function pointer to previous handler */void		*pZERO = 0;		/* need a pointer to value of 0 */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 C o l o r */void	TaskColor(void *id){	switch(*(int *)id)		{		case '1' :			SWI_Write0(RED);			break;		case '2' :			SWI_Write0(BLUE);			break;		case '3' :			SWI_Write0(GREEN);			break;		case '4' :			SWI_Write0(MAGENTA);			break;		default :			SWI_Write0(WHITE);		}}/* * T a s k B r i g h t C o l o r */void	TaskBrightColor(void *id){	switch(*(int *)id)		{		case '1' :			SWI_Write0(BR_RED);			break;		case '2' :			SWI_Write0(BR_BLUE);			break;		case '3' :			SWI_Write0(BR_GREEN);			break;		case '4' :			SWI_Write0(BR_MAGENTA);			break;		default :			SWI_Write0(BR_WHITE);		}}/* * O n e F i l l */uint	OneFill(void *id){ uint	err; uint	derr;	OSSemPend(DispSem, 0, &derr);	/* before POST'ing be sure we have the display */	err = OSQPost(pQecb, id);	TaskBrightColor(id);	switch( err )		{		case OS_NO_ERR :	/* this is what should normally happen */			SWI_WriteC(*(int *)id);			/* print task's id */			break;		case OS_Q_FULL :	/* this might happen */			SWI_Write0("\nQueue FULL in Fill id=\0");			SWI_WriteC(*(int *)id);			/* print task's id */			SWI_WriteC('\n');			break;		default :			SWI_Write0("\nUnknown value of err in Fill\0");		}	OSSemPost(DispSem);	if( err == OS_Q_FULL )		{		OSTimeDly(T_O_OUT);		/* Delay a while */		} return( err );}/* * T a s k F i l l * * This task will only fill the queue */void	TaskFill(void *id){	while(FOREVER)		{		OneFill(id);		}}/* * O n e E m p t y */uint	OneEmpty(void *id){ uint	derr; uint	err; int	*pmsg;  	OSTimeDly(4);			/* let's get everyone on line first */	pmsg = (int *)OSQPend(pQecb, T_O_IN, &err);	OSSemPend(DispSem, 0, &derr);	/* do not need display until PEND'ed */			TaskColor(id);	switch( err )		{		case OS_NO_ERR :		/* this is what should normally happen */			SWI_WriteC(*pmsg);	/* print msg */			break;		case OS_TIMEOUT :	/* this might happen */			SWI_Write0("\nTime Out in Empty id=\0");			SWI_WriteC(*(int *)id);			/* print task's id */			SWI_WriteC('\n');			break;		default :			SWI_Write0("\nUnknown value of err in Empty\0");		}	OSSemPost(DispSem); return( err );}/* * T a s k E m p t y * * This task will only empty the queue */void	TaskEmpty(void *id){	while(FOREVER)		{		OneEmpty(id);		}}/* * T a s k E m p t y F i l l * * This task will first empty and then fill the queue */void	TaskEmptyFill(void *id){ uint	err;	while(FOREVER)		{		do	{			err = OneEmpty(id);			} while( err != OS_NO_ERR);	/* do NOT post until properly pend'ed */					OneFill(id);		}}/* * 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("\nQueue test using uC/OS for PID600\n\n\0");	SWI_Write0(CYAN);	SWI_Write0("\nThere are five tasks. Tasks 1, 2, 3, and 4 display as numbers\0");	SWI_Write0("\nand the last (idle) displays as a '.' (dot).\n\0");	SWI_Write0(RED);	SWI_Write0("\nTask 1 will alternatively empty and then fill fill the queue.\0");	SWI_Write0(BLUE);	SWI_Write0("\nTask 2 will alternatively empty and then fill fill the queue.\0");	SWI_Write0(GREEN);	SWI_Write0("\nTask 3 will alternatively empty and then fill fill the queue.\0");	SWI_Write0(MAGENTA);	SWI_Write0("\nTask 4 will only fill the queue.\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 */	SWI_Write0("\nCreating a semaphore...\0");		DispSem = OSSemCreate( 1 );	/* Display semaphore */	SWI_Write0("\nCreating a queue...\0");		pQecb = OSQCreate((void **)Qmsg, QDEPTH );	/* queue */	SWI_Write0("\nCreating the tasks...\0");	j=0;	OSTaskCreate(TaskEmptyFill, (void *)&id[j], (void *)&TaskStk[j][TASK_STK_SIZE], j+1);	j++;	OSTaskCreate(TaskEmptyFill, (void *)&id[j], (void *)&TaskStk[j][TASK_STK_SIZE], j+1);	j++;	OSTaskCreate(TaskEmptyFill, (void *)&id[j], (void *)&TaskStk[j][TASK_STK_SIZE], j+1);	j++;	OSTaskCreate(TaskFill, (void *)&id[j], (void *)&TaskStk[j][TASK_STK_SIZE], j+1);	j++;	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 */    return ;}/*---------------------------------------------------------------------------*//*> EOF qtest.c <*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -