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

📄 demo.c

📁 抢占
💻 C
字号:
/* Include necessary Nucleus PLUS files.  */

#include  "nucleus.h"


/* 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;
NU_QUEUE        Queue_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;
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);


/* 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);

    /* 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");
}


/* 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)
{

STATUS          status;


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

    /* Set the clock to 0.  This clock ticks every 18 system timer ticks. */
    Task_Time =  0;

    while(1)
    {
    
        /* Sleep for 18 timer ticks.  The value of the tick is programmable
           in INT.S and is relative to the speed of the target system.  */
        NU_Sleep(18);

        /* Increment the counter */
        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;

    /* 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.  */
    Send_Message = 0;
    
    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.  */
         status =  NU_Send_To_Queue(&Queue_0, &Send_Message, 1, NU_SUSPEND);
         
         /* Determine if the message was sent successfully.  */
         if (status == NU_SUCCESS)
             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;

    /* 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;
    
    while(1)
    {
    
         /* 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.  */
         status =  NU_Receive_From_Queue(&Queue_0, &Receive_Message, 1, 
                                &received_size, NU_SUSPEND);
         
         /* Determine if the message was received successfully.  */
         if (status == NU_SUCCESS)
             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++;
    }
}

⌨️ 快捷键说明

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