📄 demo.c
字号:
{ for (i=0;i<(8-n);i++) strcat(msg, " "); strcat(msg, buffer); strcat(msg, "\n\n\n\r"); } /* Output event detections */ NU_SD_Put_String(msg,&UART_Port); /* Build a string with number of messages sent */ strcpy(msg, "Task 1 messages sent: "); ultoa(Task_1_messages_sent, buffer, 10); n = strlen(buffer); if (n>=8) { strcat(msg, buffer); strcat(msg, "\n\r"); } else { for (i=0;i<(8-n);i++) strcat(msg, " "); strcat(msg, buffer); strcat(msg, "\n\r"); } /* Output number of messages sent */ NU_SD_Put_String(msg,&UART_Port); /* Build a string with number of messages received */ strcpy(msg, "Task 2 messages received: "); ultoa(Task_2_messages_received, buffer, 10); n = strlen(buffer); if (n>=8) { strcat(msg, buffer); strcat(msg, "\n\n\r"); } else { for (i=0;i<(8-n);i++) strcat(msg, " "); strcat(msg, buffer); strcat(msg, "\n\n\r"); } /* Ouput number of messages received */ NU_SD_Put_String(msg,&UART_Port); /* Build a string with number of invalid messages */ strcpy(msg, "Task 2 invalid messages: "); ultoa(Task_2_invalid_messages, buffer, 10); n = strlen(buffer); if (n>=8) { strcat(msg, buffer); strcat(msg, "\n\n\r"); } else { for (i=0;i<(8-n);i++) strcat(msg, " "); strcat(msg, buffer); strcat(msg, "\n\n\r"); } /* Output number of invalid messages received */ NU_SD_Put_String(msg,&UART_Port); /* Output which task (3 or 4) has the semaphore */ if (Who_has_the_resource == &Task_3) { /* Task 3 has the resource */ NU_SD_Put_String("Who has the resource: Task 3\n\n\n\r",&UART_Port); } else if (Who_has_the_resource == &Task_4) { /* Task 4 has the resource */ NU_SD_Put_String("Who has the resource: Task 4\n\n\n\r",&UART_Port); } else { /* No task has the resource - shouldn't get here... */ NU_SD_Put_String("Who has the resource: Nobody\n\n\n\r",&UART_Port); } /* Build a string containing the number of timer interrupts that have occurred */ strcpy(msg, "Timer Interrupts: "); ultoa(TMD_System_Clock, buffer, 10); n = strlen(buffer); if (n>=8) { strcat(msg, buffer); strcat(msg, "\n\n\r"); } else { for (i=0;i<(8-n);i++) { strcat(msg, " "); } strcat(msg, buffer); strcat(msg, "\n\n\r"); } /* Output the number of timer interrupt which have occurred */ NU_SD_Put_String(msg,&UART_Port); /* Output any data received from the serial port */ NU_SD_Put_String("Buffer: ",&UART_Port); /* As long as received data is in the buffer, loop */ while (NU_SD_Data_Ready(&UART_Port)) { /* Get a character from the serial port */ ch = NU_SD_Get_Char(&UART_Port); /* Write character back to serial UART_Port */ NU_SD_Put_Char(ch,&UART_Port); } /* Send a couple of carriage returns and a line-feed in preparation for next output */ NU_SD_Put_String("\n\n\r",&UART_Port);#endif /* SERIAL_IO */ /* Increment the time. */ Task_Time++; /* Toggle LED 0 everytime Task_Time increases */ while (*DISPLAY & DISPLAYSTAT); *LED = *LED ^ 0x01; /* Set an event flag to lift the suspension on task 5. */ status = NU_Set_Events(&Event_Group_0, 1, NU_OR); /* If event creates an error, call system error */ if (status != NU_SUCCESS) { /* Call error handling function */ ERC_System_Error (status); } } /* while (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(UNSIGNED argc, VOID *argv){ STATUS status; UNSIGNED send_message; /* Initialize the message contents. The receiver will examine the message contents for errors. */ send_message = 0; /* Continue this process forever. */ 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) { /* Increment the number of messages sent counter */ Task_1_messages_sent++; } /* Modify the contents of the next message to send. */ send_message++; /* Toggle LED 1 every 10000 messages sent by Task_1 */ if (!(Task_1_messages_sent % 10000)) { while (*DISPLAY & DISPLAYSTAT); *LED = *LED ^ 0x02; } }}/* 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; /* Initialize the message contents to expect. */ message_expected = 0; /* Continue this process forever. */ 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) { /* Increment number of messages received counter */ Task_2_messages_received++; } /* Check the contents of the message against what this task is expecting. */ if ((received_size != 1) || (receive_message != message_expected)) { /* Increment invalid message counter */ Task_2_invalid_messages++; } /* Modify the expected contents of the next message. */ message_expected++; /* Toggle LED 2 for every 10000 Task_2 messages received */ if (!(Task_2_messages_received % 10000)) { while (*DISPLAY & DISPLAYSTAT); *LED = *LED ^ 0x04; } }}/* Tasks 3 and 4 want a single resource. Once one of the tasks gets the resource, it keeps it for 100 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; /* 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) { /* Find which task (3 or 4) has the semaphore */ 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; /* 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) { /* Increment the number of events detected counter */ Event_Detections++; /* Blink LED 3 every time an event is detected */ while (*DISPLAY & DISPLAYSTAT); *LED = *LED ^ 0x08; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -