📄 demo.c
字号:
/* Include necessary Nucleus definitions. */
#include "nu_defs.h"
#include "nu_extr.h"
/* Define Application constants. */
#define QUEUE_0 0
#define RESOURCE_0 0
#define PARTITION_0 0
#define EVENT_GROUP_0 0
#define EVENT_FLAG 0
/* Allocate global counters. */
unsigned long Task_Time;
unsigned long Task_2_messages_received;
unsigned long Task_2_invalid_messages;
unsigned long Task_1_messages_sent;
unsigned long Who_has_the_resource;
unsigned long Event_Detections;
/* Define the system timer task. More complicated systems might use a
routine like this to perform periodic message sending and other time
oriented functions. This demo flashes lights on the RISM 80c186 board. */
void task_0()
{
/* Set the clock to 0. This clock ticks every 25 system timer ticks. */
Task_Time = 0;
while(1)
{
/* Sleep for 25 timer ticks. The value of the tick is programmable
in IND.ASM and is relative to the speed of the target system. */
NU_Sleep(25);
/* Increment the time. */
Task_Time++;
/* Set an event flag to lift the suspension on task 5. */
NU_Set_Events(EVENT_GROUP_0, NU_EVENT_OR, 1);
}
}
/* 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()
{
int status;
unsigned int Send_Message[2];
/* 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] = 0;
Send_Message[1] = 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_Item(QUEUE_0, Send_Message, NU_WAIT_FOREVER);
/* 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[0]++;
Send_Message[1]++;
}
}
/* 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()
{
int status;
unsigned int Receive_Message[2];
unsigned int message_expected;
/* 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_Retrieve_Item(QUEUE_0, Receive_Message,NU_WAIT_FOREVER);
/* 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 ((Receive_Message[0] != message_expected) ||
(Receive_Message[1] != 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()
{
int status;
/* Loop to allocate and deallocate the resource. */
while(1)
{
/* Allocate the resource. Suspend until it becomes available. */
status = NU_Request_Resource(RESOURCE_0, NU_WAIT_FOREVER);
/* If the status is successful, show that this task owns the
resource. */
if (status == NU_SUCCESS)
{
Who_has_the_resource = NU_Current_Task_ID();
/* Sleep for 100 ticks to cause the other task to suspend on
the resource. */
NU_Sleep(100);
/* Release the resource. */
NU_Release_Resource(RESOURCE_0);
}
}
}
/* Define the task that waits for the event to be set by task 0. */
void task_5()
{
int status;
unsigned int event_group;
/* Initialize the event detection counter. */
Event_Detections = 0;
/* Continue this process forever. */
while(1)
{
/* Wait for an event and consume it. */
status = NU_Wait_For_Events(EVENT_GROUP_0, NU_EVENT_AND_CONSUME, 1,
&event_group, NU_WAIT_FOREVER);
/* If the status is okay, increment the counter. */
if (status == NU_SUCCESS)
Event_Detections++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -