📄 demo.c
字号:
/* Include necessary files. */#include "nucleus.h"#include "NCL\inc\nu_ncl.h"#include "NCL\inc\stdlib.h"#include "NCL\inc\string.h"/* Defines for Nucleus Plus Demonstration */#define SERIAL_IO NU_TRUE /* Change to NU_FALSE for no serial input/output */#define SYSTEM_MEMORY_SIZE 25000 /* Size of system memory pool */#define TASK_STACK_SIZE 2000 /* Size of task stacks */#if (SERIAL_IO)/* Using serial IO */#include "nu_sd.h" /* Nucleus Serial Driver interface */#endif /* SERIAL_IO *//* The following two defines allow the demo to activate the LED display during execution of the code. */#define LEDREG 0x1A000004#define LED (volatile int *) LEDREG#define DISPLAYREG 0x1A000000#define DISPLAY (volatile int *) DISPLAYREG#define DISPLAYSTAT 0x00000001#define DISPLAY3 0x0000019E#define DISPLAY4 0x000001CC/* Application 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;/* Global Variables */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;#if (SERIAL_IO)/* Serial port structure */NU_SERIAL_PORT UART_Port;#endif/* External variable declarations */extern UNSIGNED TMD_System_Clock;/* External function declarations */extern VOID ERC_System_Error (INT);/* Function Prototypes */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; STATUS status; /* Turn all the LEDs OFF */ *LED = 0x00; /* Create a system memory pool that will be used to allocate task stacks, queue areas, etc. */ status = NU_Create_Memory_Pool(&System_Memory, "SYSMEM", first_available_memory, SYSTEM_MEMORY_SIZE, 50, NU_FIFO); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create each task in the system. */ /* Create task 0. */ NU_Allocate_Memory(&System_Memory, &pointer, TASK_STACK_SIZE, NU_NO_SUSPEND); status = NU_Create_Task(&Task_0, "TASK 0", task_0, 0, NU_NULL, pointer, TASK_STACK_SIZE, 1, 20, NU_PREEMPT, NU_START); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create task 1. */ NU_Allocate_Memory(&System_Memory, &pointer, TASK_STACK_SIZE, NU_NO_SUSPEND); status = NU_Create_Task(&Task_1, "TASK 1", task_1, 0, NU_NULL, pointer, TASK_STACK_SIZE, 10, 5, NU_PREEMPT, NU_START); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create task 2. */ NU_Allocate_Memory(&System_Memory, &pointer, TASK_STACK_SIZE, NU_NO_SUSPEND); status = NU_Create_Task(&Task_2, "TASK 2", task_2, 0, NU_NULL, pointer, TASK_STACK_SIZE, 10, 5, NU_PREEMPT, NU_START); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create task 3. Note that task 4 uses the same instruction area. */ NU_Allocate_Memory(&System_Memory, &pointer, TASK_STACK_SIZE, NU_NO_SUSPEND); status = NU_Create_Task(&Task_3, "TASK 3", task_3_and_4, 0, NU_NULL, pointer, TASK_STACK_SIZE, 5, 0, NU_PREEMPT, NU_START); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create task 4. Note that task 3 uses the same instruction area. */ NU_Allocate_Memory(&System_Memory, &pointer, TASK_STACK_SIZE, NU_NO_SUSPEND); status = NU_Create_Task(&Task_4, "TASK 4", task_3_and_4, 0, NU_NULL, pointer, TASK_STACK_SIZE, 5, 0, NU_PREEMPT, NU_START); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create task 5. */ NU_Allocate_Memory(&System_Memory, &pointer, TASK_STACK_SIZE, NU_NO_SUSPEND); status = NU_Create_Task(&Task_5, "TASK 5", task_5, 0, NU_NULL, pointer, TASK_STACK_SIZE, 7, 0, NU_PREEMPT, NU_START); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create communication queue. */ NU_Allocate_Memory(&System_Memory, &pointer, 100*sizeof(UNSIGNED),NU_NO_SUSPEND); status = NU_Create_Queue(&Queue_0, "QUEUE 0", pointer, 100, NU_FIXED_SIZE, 1, NU_FIFO); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create synchronization semaphore. */ status = NU_Create_Semaphore(&Semaphore_0, "SEM 0", 1, NU_FIFO); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); } /* Create event flag group. */ status = NU_Create_Event_Group(&Event_Group_0, "EVGROUP0"); /* Check to see if previous operation successful */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0x0F; /* Call error handling routine */ ERC_System_Error(status); }}/* 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;#if (SERIAL_IO) CHAR msg[60]; CHAR buffer[12]; CHAR ch; INT i; INT n;#endif /* SERIAL_IO */#if (SERIAL_IO) /* Init the serial port structure. */ UART_Port.com_port = DEFAULT_UART_PORT; UART_Port.baud_rate = DEFAULT_UART_BAUD; UART_Port.data_bits = DEFAULT_UART_DATA; UART_Port.stop_bits = DEFAULT_UART_STOP; UART_Port.parity = DEFAULT_UART_PARITY; UART_Port.data_mode = DEFAULT_UART_MODE; UART_Port.communication_mode = SERIAL_MODE; UART_Port.sd_buffer_size = DEFAULT_UART_BUFFER; /* Call serial port initialization routine */ status = NU_SD_Init_Port (&UART_Port); /* Ensure serial port initialized successfully */ if (status != NU_SUCCESS) { /* Turn all the LEDs ON if an error occurs */ *LED = 0xF0; /* Call error handling function */ ERC_System_Error(status); }#endif /* SERIAL_IO */ /* Continue this process forever. */ while(1) { /* Sleep for 100 timer ticks. */ NU_Sleep(100);/* If SERIAL_IO is NU_TRUE, it sends scrolling output to serial port */#if (SERIAL_IO) /* Output Release Information / Title of scrolling output */ NU_SD_Put_String("\n\r****************************************",&UART_Port); NU_SD_Put_String("***************************************\n\r",&UART_Port); NU_SD_Put_String(NU_Release_Information(),&UART_Port); NU_SD_Put_String("\n\r",&UART_Port); NU_SD_Put_String("****************************************",&UART_Port); NU_SD_Put_String("***************************************\n\n\r",&UART_Port); NU_SD_Put_String("System Variable Status: \n\n\r",&UART_Port); /* Build a string with current task 0 time */ strcpy(msg, "Task 0 time: "); ultoa(Task_Time, 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 Task 0 Time */ NU_SD_Put_String(msg,&UART_Port); /* Build a string with number of event detections */ strcpy(msg, "Event detections: "); ultoa(Event_Detections, buffer, 10); n = strlen(buffer); if (n>=8) { strcat(msg, buffer); strcat(msg, "\n\n\n\r"); } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -