?? demo2.c
字號:
/***************************************************************************
* demo2.c *
* *
* task_1、task_2和Task_3基于時間片輪轉調度,三個任務為同優先級的任務 *
* 在這個demo中驗證了NU_Suspend_Task()和NU_Resume_Task()兩個函數。同時也 *
* 驗證了NU_Terminate_Task()和NU_Reset_Task()兩個函數。注意Task狀態的轉變 *
* *
* designed by bobey *
***************************************************************************/
/* Include necessary files. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include"2410addr.h"
#include"2410lib.h"
/* Include necessary Nucleus PLUS files. */
#include "nucleus.h"
extern void ENABLE_INTERRUPT(void);
/* Application Structures */
NU_TASK Task_1;
NU_TASK Task_2;
NU_TASK Task_3;
NU_TASK Task_4;
NU_MEMORY_POOL System_Memory;
extern int ERC_System_Error(int);
/* Function Prototypes */
VOID task_1(UNSIGNED argc, VOID *argv);
VOID task_2(UNSIGNED argc, VOID *argv);
VOID task_3(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;
/*--------------------------uart initialize---------------------------------*/
ChangeClockDivider(1,1);
ChangeMPllValue(0xa1,0x3,0x1);
Port_Init();
Uart_Select(0);
Uart_Init(0,115200);
Uart_Printf("Nucleus is running!\n");
EnableTimer0();
/*----------------------------end-------------------------------------------*/
/* 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, 25000, 50, NU_FIFO);
if (status != NU_SUCCESS)
{
ERC_System_Error(status);
}
else
{
Uart_Printf("System_Memory have been cteated!\n");
}
/* Create each task in the system. */
/* Create task1.*/
NU_Allocate_Memory(&System_Memory, &pointer, 512, NU_NO_SUSPEND);
status=NU_Create_Task(&Task_1, "TASK 1", task_1, 0, NULL, pointer,
512, 3, 10, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
ERC_System_Error(status);
}
else
{
Uart_Printf("Task1 have been created\n");
}
/* Create task2.*/
NU_Allocate_Memory(&System_Memory, &pointer, 512, NU_NO_SUSPEND);
status=NU_Create_Task(&Task_2, "TASK 2", task_2, 0, NULL, pointer,
512, 3, 10, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
ERC_System_Error(status);
}
else
{
Uart_Printf("task2 have been created\n");
}
/* Create task3.*/
NU_Allocate_Memory(&System_Memory, &pointer, 512, NU_NO_SUSPEND);
status=NU_Create_Task(&Task_3, "TASK 3", task_3, 0, NULL, pointer,
512, 3, 10, NU_PREEMPT, NU_START);
if (status != NU_SUCCESS)
{
ERC_System_Error(status);
}
else
{
Uart_Printf("task3 have been created\n");
}
}
//===================================================================
/* Task_1 */
void task_1(UNSIGNED argc, VOID *argv)
{
STATUS status;
int i=1;
status = (STATUS) argc + (STATUS) argv;
while(1)
{
i++;
Uart_Printf("task1 is:%d\n",i);
NU_Sleep(8); // Suspend Task_1 for 8 ticks
}
}
//===================================================================
/* Task_2 */
void task_2(UNSIGNED argc, VOID *argv)
{
STATUS status;
int i=1;
status = (STATUS) argc + (STATUS) argv;
while(1)
{
i++;
Uart_Printf("task2 is:%d\n",i);
NU_Sleep(8); // Suspend Task_2 for 8 ticks
if(i==30){
NU_Resume_Task(&Task_3); //當i=30的時候,Task_3 Resume,
} //處于ready狀態
if(i==90){
NU_Reset_Task(&Task_3,0,NULL);//當i=90的時候,Task_3 Reset,
//使之處于Suspended狀態,reset命令
//只能用來處理中止或完成的任務(即
//Terminated狀態的Task)
NU_Resume_Task(&Task_3); //Reseume Task_3,使之由Suspended狀態
//到Ready狀態
}
}
}
//==========================================================================
/* Task_3 */
void task_3(UNSIGNED argc, VOID *argv)
{
STATUS status;
int i=1;
status = (STATUS) argc + (STATUS) argv;
while(1)
{
i++;
Uart_Printf("task3 is:%d\n",i);
NU_Sleep(8); // Suspend Task_3 for 8 ticks
if(i==10){
NU_Suspend_Task(&Task_3); //當i=10的時候,Task_3自掛起,處于
} //suspended狀態.
if(i==50){
NU_Terminate_Task(&Task_3);//中止Task_3,使之處于Terminated狀態
//只有通過Reset,才能進入Suspended狀態
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -