www.pudn.com > ucosii_for_m128_template.rar > app.c, change:2004-12-06,size:9820b


/* 
************************************************************************************************************* 
*                                                uC/OS-II 
*                                          The Real-Time Kernel 
* 
*                                         ATmega128  Sample code 
* 
* File : APP.C 
* By   : Jean J. Labrosse 
************************************************************************************************************* 
*/ 
 
#include  <includes.h> 
 
/* 
************************************************************************************************************** 
*                                               CONSTANTS 
* 
* Note(s) : 1) See OS_CFG.H for the default stack size: 'OS_TASK_STK_SIZE' 
************************************************************************************************************** 
*/ 
 
#define  CPU_CLK_FREQ                  3684000L 
 
 
#define  OS_TASK_START_STK_SIZE        OS_TASK_STK_SIZE 
#define  OS_TASK_START_HARD_STK_SIZE   OS_TASK_HARD_STK_SIZE 
 
#define  OS_TASK_1_STK_SIZE            OS_TASK_STK_SIZE 
#define  OS_TASK_1_HARD_STK_SIZE       OS_TASK_HARD_STK_SIZE 
 
#define  OS_TASK_2_STK_SIZE            OS_TASK_STK_SIZE 
#define  OS_TASK_2_HARD_STK_SIZE       OS_TASK_HARD_STK_SIZE 
 
 
/* 
************************************************************************************************************** 
*                                               VARIABLES 
************************************************************************************************************** 
*/ 
 
OS_STK  AppTaskStartStk[OS_TASK_START_STK_SIZE]; 
OS_STK  AppTask1Stk[OS_TASK_1_STK_SIZE]; 
OS_STK  AppTask2Stk[OS_TASK_2_STK_SIZE]; 
 
/* 
************************************************************************************************************** 
*                                           FUNCTION PROTOTYPES 
************************************************************************************************************** 
*/ 
 
       void  main(void); 
        
static void  AppTaskStart(void *p_arg); 
static void  AppTaskCreate(void); 
static void  AppTask1(void *p_arg); 
static void  AppTask2(void *p_arg); 
 
static void  AppIOInit(void); 
 
static void  LED_Toggle(INT8U led); 
  
/* 
************************************************************************************************************** 
*                                                MAIN 
* 
* Note(s): 1) You SHOULD use OS_TASK_STK_SIZE (see OS_CFG.H) when setting OSTaskStkSize prior to calling  
*             OSInit() because OS_TASK_IDLE_STK_SIZE and OS_TASK_STAT_STK_SIZE are set to this value in 
*             OS_CFG.H. 
************************************************************************************************************** 
*/ 
 
void  main (void) 
{ 
    /*---- Any initialization code prior to calling OSInit() goes HERE --------------------------------*/ 
 
                                                /* IMPORTANT: MUST be setup before calling 'OSInit()'  */ 
    OSTaskStkSize     = OS_TASK_STK_SIZE;       /* Setup the default stack size                        */ 
    OSTaskHardStkSize = OS_TASK_HARD_STK_SIZE;  /* Setup the default hardware stack size               */ 
 
    OSInit();                                   /* Initialize "uC/OS-II, The Real-Time Kernel"         */ 
 
    /*---- Any initialization code before starting multitasking ---------------------------------------*/ 
 
    OSTaskStkSize     = OS_TASK_START_STK_SIZE;       /* Setup the total stack size                    */ 
    OSTaskHardStkSize = OS_TASK_START_HARD_STK_SIZE;  /* Setup the hardware stack size                 */ 
    OSTaskCreate(AppTaskStart, (void *)0, (OS_STK *)&AppTaskStartStk[OSTaskStkSize - 1], 0); 
 
    /*---- Create any other task you want before we start multitasking --------------------------------*/ 
 
    OSStart();                                  /* Start multitasking (i.e. give control to uC/OS-II)  */ 
} 
 
/* 
********************************************************************************************************* 
*                                          STARTUP TASK 
* 
* Description : This is an example of a startup task.  As mentioned in the book's text, you MUST 
*               initialize the ticker only once multitasking has started. 
* 
* Arguments   : p_arg   is the argument passed to 'AppStartTask()' by 'OSTaskCreate()'. 
* 
* Notes       : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not 
*                  used.  The compiler should not generate any code for this statement. 
********************************************************************************************************* 
*/ 
 
static void  AppTaskStart (void *p_arg) 
{ 
    p_arg = p_arg;                               /* Prevent compiler warnings                          */ 
 
    OSTickISR_Init();                            /* Initialize the ticker                              */ 
 
    AppIOInit();                                 /* Initialize the I/Os                                */ 
 
    AppTaskCreate(); 
 
    while (TRUE) {                               /* Task body, always written as an infinite loop.     */ 
        LED_Toggle(1); 
        OSTimeDly(OS_TICKS_PER_SEC / 10); 
    } 
} 
 
 
/* 
********************************************************************************************************* 
*                                    CREATE APPLICATION TASKS 
* 
* Description : This function creates the application tasks. 
* 
* Arguments   : p_arg   is the argument passed to 'AppStartTask()' by 'OSTaskCreate()'. 
* 
* Notes       : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not 
*                  used.  The compiler should not generate any code for this statement. 
********************************************************************************************************* 
*/ 
 
static  void  AppTaskCreate (void) 
{ 
    /*---- Task initialization code goes HERE! --------------------------------------------------------*/ 
    OSTaskStkSize     = OS_TASK_1_STK_SIZE;        /* Setup the default stack size                     */ 
    OSTaskHardStkSize = OS_TASK_1_HARD_STK_SIZE;   /* Setup the default hardware stack size            */ 
    OSTaskCreate(AppTask1, (void *)0, (OS_STK *)&AppTask1Stk[OSTaskStkSize - 1], 1); 
 
    OSTaskStkSize     = OS_TASK_2_STK_SIZE;        /* Setup the default stack size                     */ 
    OSTaskHardStkSize = OS_TASK_2_HARD_STK_SIZE;   /* Setup the default hardware stack size            */ 
    OSTaskCreate(AppTask2, (void *)0, (OS_STK *)&AppTask2Stk[OSTaskStkSize - 1], 2); 
} 
 
/* 
********************************************************************************************************* 
*                                              TASK #1 
********************************************************************************************************* 
*/ 
 
static void  AppTask1(void *p_arg) 
{ 
    p_arg = p_arg; 
    while (TRUE) { 
        LED_Toggle(2); 
        OSTimeDly(OS_TICKS_PER_SEC / 5); 
    } 
} 
 
/* 
********************************************************************************************************* 
*                                              TASK #2 
********************************************************************************************************* 
*/ 
 
static void  AppTask2(void *p_arg) 
{ 
    p_arg = p_arg; 
    while (TRUE) { 
        LED_Toggle(3); 
        OSTimeDly(OS_TICKS_PER_SEC / 2); 
    } 
} 
 
/* 
********************************************************************************************************* 
*                                            SETUP THE I/Os 
********************************************************************************************************* 
*/ 
 
static void AppIOInit (void) 
{ 
    DDRD        = 0xFF;                                 /* All PORTD pins are outputs                  */ 
    PORTD       = 0xFF; 
} 
 
/* 
********************************************************************************************************* 
*                                        SETUP THE TICK RATE 
********************************************************************************************************* 
*/ 
 
void  OSTickISR_Init (void) 
{ 
    TCCR0 = 0x07;                                       /* Set TIMER0 prescaler to CLK/1024            */ 
    TIMSK = 0x01;                                       /* Enable TIMER0 overflow interrupt            */ 
} 
 
 
/* 
********************************************************************************************************* 
*                                        SETUP THE TICK RATE 
********************************************************************************************************* 
*/ 
 
void  OSTickISR_Handler (void) 
{ 
    TCNT0 = 256 - (CPU_CLK_FREQ / OS_TICKS_PER_SEC / 1024); 
    OSTimeTick(); 
} 
 
/* 
********************************************************************************************************* 
*                                          TOGGLE LED 
********************************************************************************************************* 
*/ 
 
static  void  LED_Toggle (INT8U led) 
{ 
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */ 
    OS_CPU_SR  cpu_sr; 
#endif 
 
 
    OS_ENTER_CRITICAL(); 
    switch (led) { 
        case 1: 
             PORTD ^= 0x01; 
             break; 
 
        case 2: 
             PORTD ^= 0x02; 
             break; 
 
        case 3: 
             PORTD ^= 0x04; 
             break; 
 
        case 4: 
             PORTD ^= 0x08; 
             break; 
    } 
    OS_EXIT_CRITICAL(); 
}