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

📄 demo2.c

📁 关于nucleus系统的教程文档
💻 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 + -