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

📄 demo.c

📁 Nucleus实时操作系统是Accelerater Technology公司开发的嵌入式RTOS产品,Nucleus的核心是一个实时的多任务内核——Nucleus PLUS
💻 C
字号:
/* Include necessary Nucleus PLUS files.  */

#include  "nucleus.h"
#include  "string.h"
#include  "stdlib.h"

struct LAPDL{
	int	length;
	char *info;
}*rdl,*dl0;

struct q921{
	unsigned char link;
	unsigned char status;
	unsigned char self;
	unsigned char peer;
	unsigned char lap3;
	unsigned char  rej;
	unsigned char ack;
	unsigned char channalc;
	unsigned char rc;
	unsigned char pf;
	unsigned char pending;
	unsigned char vs;
	unsigned char vr;
	unsigned char va;
	int i,j;
}pstn;

/* Define Application data structures.  */

NU_TASK         Task_0;
NU_TASK         Task_1;
NU_TASK         Task_2;
NU_TASK         Task_3;
NU_TASK         Task_4;
NU_TASK         Task_5;


UNSIGNED        *LEDADDR=(UNSIGNED *)0x60000;
NU_QUEUE        Queue_0;
NU_PIPE	Pipe_0;
NU_SEMAPHORE    Semaphore_0;
NU_EVENT_GROUP  Event_Group_0;
NU_MEMORY_POOL  System_Memory;


/* Allocate global counters. */
UNSIGNED  Task_Time;
UNSIGNED  Task_2_messages_received;
UNSIGNED  Task_2_invalid_messages;
UNSIGNED  Task_1_messages_sent;
NU_TASK  *Who_has_the_resource;
NU_TIMER timer;
UNSIGNED  Event_Detections;


/* Define prototypes for function references.  */
void    task_0(UNSIGNED argc, VOID *argv);
void    task_1(UNSIGNED argc, VOID *argv);
void    task_2(UNSIGNED argc, VOID *argv);
void    task_3_and_4(UNSIGNED argc, VOID *argv);
void    task_5(UNSIGNED argc, VOID *argv);
void 	timer_expire();

/* Define the Application_Initialize routine that determines the initial
   Nucleus PLUS application environment.  */
   
void    Application_Initialize(void *first_available_memory)
{

VOID           *pointer;


    /* Create a system memory pool that will be used to allocate task stacks,
       queue areas, etc.  */
    NU_Create_Memory_Pool(&System_Memory, "SYSMEM", 
                        first_available_memory, 4096, 50, NU_FIFO);
                        
    /* Create each task in the system.  */
    
    /* Create task 0.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 500, NU_NO_SUSPEND);
    NU_Create_Task(&Task_0, "TASK 0", task_0, 0, NU_NULL, pointer, 500, 1, 20,
                                                      NU_PREEMPT, NU_START);

    /* Create task 1.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 500, NU_NO_SUSPEND);
    NU_Create_Task(&Task_1, "TASK 1", task_1, 0, NU_NULL, pointer, 500, 10, 5,
                                                      NU_PREEMPT, NU_START);

    /* Create task 2.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 500, NU_NO_SUSPEND);
    NU_Create_Task(&Task_2, "TASK 2", task_2, 0, NU_NULL, pointer, 500, 10, 5,
                                                      NU_PREEMPT, NU_START);
	
    /* Create task 3.  Note that task 4 uses the same instruction area.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 500, NU_NO_SUSPEND);
    NU_Create_Task(&Task_3, "TASK 3", task_3_and_4, 0, NU_NULL, pointer, 
                                        500, 5, 0, NU_PREEMPT, NU_START);

    /* Create task 4.  Note that task 3 uses the same instruction area.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 500, NU_NO_SUSPEND);
    NU_Create_Task(&Task_4, "TASK 4", task_3_and_4, 0, NU_NULL, pointer, 
                                        500, 5, 0, NU_PREEMPT, NU_START);

    /* Create task 5.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 500, NU_NO_SUSPEND);
    NU_Create_Task(&Task_5, "TASK 5", task_5, 0, NU_NULL, pointer, 500, 7, 0, 
                                                      NU_PREEMPT, NU_START);


    /* Create communication queue.  */
    NU_Allocate_Memory(&System_Memory, &pointer, 100*sizeof(UNSIGNED), 
                                                        NU_NO_SUSPEND);
    NU_Create_Queue(&Queue_0, "QUEUE 0", pointer, 100, NU_FIXED_SIZE, 1, 
                                                                      NU_FIFO);

    NU_Create_Pipe(&Pipe_0,"PIPE 0",pointer,2600,NU_VARIABLE_SIZE,10,NU_FIFO);
    /* Create synchronization semaphore.  */
    NU_Create_Semaphore(&Semaphore_0, "SEM 0", 1, NU_FIFO);
    
    /* Create event flag group.  */
    NU_Create_Event_Group(&Event_Group_0, "EVGROUP0");
	NU_Create_Timer(&timer,"any name",timer_expire,0,23,5,NU_ENABLE_TIMER);
}


/* Define the system timer task.  More complicated systems might use a 
   routine like this to perform periodic message sending and other time
   oriented functions.  */


void   task_0(UNSIGNED argc, VOID *argv)
{
	int i;
	UNSIGNED send_message;
STATUS          status;
/* STATUS	led;
int 	i; */

    /* Access argc and argv just to avoid compilation warnings.  */
    status =  (STATUS) argc + (STATUS) argv;
/*	led=0;*/
    /* Set the clock to 0.  This clock ticks every 18 system timer ticks. */
    Task_Time =  0;
/*	i=1;*/
    while(1)
    {
    
        /* Sleep for 18 timer ticks.  The value of the tick is programmable
           in INT_SIM.S and is relative to the speed of the target system.  */
        NU_Sleep(18);
        		NU_Sleep(100);
		
//		status=NU_Send_To_Pipe(&pipe_ifr,frm0,sizeof(struct frame),NU_SUSPEND);
//		status=NU_Send_To_Queue(&queue_dl,&send_message,1,NU_SUSPEND);
		i=4;
		send_message=100;
		
		if(i==4){
			pstn.status=8;
			NU_Send_To_Queue(&Queue_0,&send_message,1,NU_SUSPEND);
			NU_Sleep(1);
			i=3;
		}
		
		if(i==3){
			pstn.status=7;
			NU_Send_To_Queue(&Queue_0,&send_message,1,NU_SUSPEND);
			NU_Sleep(1);
			i=2;
		}

		if(i==2){
			pstn.status=5;
			NU_Send_To_Queue(&Queue_0,&send_message,1,NU_SUSPEND);
			NU_Sleep(1);
			i=1;
		}

		if(i==1){
			pstn.status=9;
			NU_Send_To_Queue(&Queue_0,&send_message,1,NU_SUSPEND);
			NU_Sleep(1);
			i=4;
		}
/*        *LEDADDR=i;
		i=i<<1;
		if(i>=64) i=1;        */

        /* Increment the time.  */
        Task_Time++;
        
        /* Set an event flag to lift the suspension on task 5.  */
        status =  NU_Set_Events(&Event_Group_0, 1, NU_OR);
    }
}


/* Define the queue sending task.  Note that the only things that cause
   this task to suspend are queue full conditions and the time slice
   specified in the configuration file.  */

void   task_1(UNSIGNED argc, VOID *argv)
{

STATUS         status; 
UNSIGNED       Send_Message;
char *ch="1234567890";
int i;
    /* Access argc and argv just to avoid compilation warnings.  */
    status =  (STATUS) argc + (STATUS) argv;

    /* Initialize the message counter.  */
    Task_1_messages_sent =  0;

    /* Initialize the message contents.  The receiver will examine the 
       message contents for errors.  */
	rdl=malloc(sizeof(struct LAPDL));
	Send_Message = 0;
	i=0;    
	rdl->length=10;
	rdl->info=malloc(strlen(ch)*sizeof(char));
	rdl->info=strcpy(rdl->info,"1234567890");
    while(1)
    {
    
         /* Send the message to Queue_0, which task 2 reads from.  Note
            that if the destination queue fills up this task suspends until
            room becomes available.  */
         NU_Sleep(18);
         status=NU_Send_To_Pipe(&Pipe_0,rdl,sizeof(struct LAPDL),NU_SUSPEND);
/*         status =  NU_Send_To_Queue(&Queue_0, &Send_Message, 1, NU_SUSPEND);*/
         
         /* Determine if the message was sent successfully.  */
/*         if (status == NU_SUCCESS){*/
/*       		*LEDADDR=i;*/
/*       		i++;
       		if(i>63) i=0;*/
       		
       		if(status==NU_SUCCESS){
       			*LEDADDR=i;
       			i++;
       			if(i>63) i=0;
       		}
             Task_1_messages_sent++;
         
             
         /* Modify the contents of the next message to send.  */
         Send_Message++;
    }
}


/* Define the queue receiving task.  Note that the only things that cause
   this task to suspend are queue empty conditions and the time slice
   specified in the configuration file.   */

void   task_2(UNSIGNED argc, VOID *argv)
{

STATUS         status; 
UNSIGNED       Receive_Message;
UNSIGNED       received_size;
UNSIGNED       message_expected;
CHAR  name[8];
VOID *start_address;
UNSIGNED pipe_size,available,messages,message_size,tasks_waiting;
OPTION message_type,suspend_type;
NU_TASK *first_task;
int i;
    /* Access argc and argv just to avoid compilation warnings.  */
    status =  (STATUS) argc + (STATUS) argv;

    /* Initialize the message counter.  */
    Task_2_messages_received =  0;

    /* Initialize the message error counter.  */
    Task_2_invalid_messages =  0;

    /* Initialize the message contents to expect.  */
    message_expected =  0;
    dl0=malloc(sizeof(struct LAPDL));
    i=0;
    while(1)
    {
    
	NU_Sleep(50);         
/* Retrieve a message from Queue_0, which task 1 writes to.  Note
            that if the source queue is empty this task suspends until
            something becomes available.  */
         NU_Pipe_Information(&Pipe_0,name,&start_address,&pipe_size,&available,&messages,
         	&message_type,&message_size,&suspend_type,&tasks_waiting,&first_task);
 
         status =  NU_Receive_From_Pipe(&Pipe_0, dl0, sizeof(struct LAPDL), 
                                &received_size, NU_SUSPEND);
        
         /* Determine if the message was received successfully.  */
         if (status == NU_SUCCESS){
/*        		*LEDADDR=i;*/
        		i++;
        		if(i>63) i=0; 
             Task_2_messages_received++;
         }    
         /* Check the contents of the message against what this task
            is expecting.  */
         if ((received_size != 1) ||
             (Receive_Message != message_expected))
             Task_2_invalid_messages++;
         
         /* Modify the expected contents of the next message.  */
         message_expected++;
    }
}


/* Tasks 3 and 4 want a single resource.  Once one of the tasks gets the
   resource, it keeps it for 30 clock ticks before releasing it.  During
   this time the other task suspends waiting for the resource.  Note that
   both task 3 and 4 use the same instruction areas but have different 
   stacks.  */
   
void  task_3_and_4(UNSIGNED argc, VOID *argv)
{

STATUS  status;

    /* Access argc and argv just to avoid compilation warnings.  */
    status =  (STATUS) argc + (STATUS) argv;

    /* Loop to allocate and deallocate the resource.  */
    while(1)
    {
    
         /* Allocate the resource.  Suspend until it becomes available.  */
         status =  NU_Obtain_Semaphore(&Semaphore_0, NU_SUSPEND);
         
         /* If the status is successful, show that this task owns the 
            resource.  */
         if (status ==  NU_SUCCESS)
         {
         
             Who_has_the_resource =  NU_Current_Task_Pointer();
             
             /* Sleep for 100 ticks to cause the other task to suspend on 
                the resource.  */
             NU_Sleep(100);
             
             /* Release the semaphore.  */
             NU_Release_Semaphore(&Semaphore_0);
        }
    }
}

	
/* Define the task that waits for the event to be set by task 0.  */

void  task_5(UNSIGNED argc, VOID *argv)
{

STATUS        status;
UNSIGNED      event_group;


    /* Access argc and argv just to avoid compilation warnings.  */
    status =  (STATUS) argc + (STATUS) argv;

    /* Initialize the event detection counter.  */
    Event_Detections =  0;
    
    /* Continue this process forever.  */
    while(1)
    {
    
        /* Wait for an event and consume it.  */
        status =  NU_Retrieve_Events(&Event_Group_0, 1, NU_OR_CONSUME,
                                     &event_group, NU_SUSPEND);
                          
        /* If the status is okay, increment the counter.  */
        if (status == NU_SUCCESS)
           Event_Detections++;
    }
}

void timer_expire()
{
	;
}


⌨️ 快捷键说明

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