/*
	VWDOG.C

   PK21xx/Rugged Giant and PK22xx/Little Star Sample Program illustrating
   the use of virtual watchdogs and the KEYREQUEST directive.
*/

#use "default.h"
#use cplc.lib
#use rtk.lib

#if !(BOARD_TYPE==CPLC_BOARD || BOARD_TYPE==L_STAR)
#fatal "This program only runs on a PK21xx/Rugged Giant or a PK22xx/Little Star"
#endif

/*** Begin Globals real_time_kernel  */
int task0(void), task1(void), task2(void), task3(void), 
    task4(void), task5(void), backgnd(void);

int (*Ftask[]) () =
{
    task0, task1, task2, task3, task4, task5, backgnd
};

#define NTASKS 7

#define TASK0 0
#define TASK1 1
#define TASK2 2
#define TASK3 3
#define TASK4 4
#define TASK5 5

/*** End Globals real_time_kernel */

#define RUNKERNEL  1  // Invoke the real time kernel
#define N_WATCHDOG 2  // Number of virtual watchdog requested
                      // User software has to make sure that these
                      // virtual watchdogs are reloaded before any timer
                      // counts down to zero. If any virtual watchdog counts
                      // down to zero, a hardware watchdog reset is forced.

#define KEYREQUEST 3         // If any key is pressed, task 3 is requested.
                             // This option will only run with the real time
                             // kernel.

main()
{
    uplc_init();             // init background drivers
    up_wdoghit(1, 60);       // virtual watchdog 1 for 60 RTK ticks
    up_wdoghit(2, 60);       // virtual watchdog 2 for 60 RTK ticks
    backgnd();               // background task of RTK
}

indirect task0(void)
{
                             // task not used

}

// Task 1 reloads virtual timer 1.  Task 1 should be run faster than 
// the 60 ticks.  If count in run_every(TASK1,count) is greater or equal 
// to 60, the virtual watchdog 1 will time out and a hardware watchdog 
// reset will be forced.
indirect task1(void)
{
    up_wdoghit(1, 60);       
}


// Task 2 should be run faster than 60 ticks.  If count in 
// run_every(TASK2,count) is greater or equal to 60, the virtual 
// watchdog 1 will time out and a hardware watchdog reset is forced.
indirect task2(void)
{
    up_wdoghit(2, 60);       // task 2 reloads the virtual timer 2
}


// When a key is pressed, the KEYREQUEST directive will cause task3 to 
// be executed.
indirect task3(void)
{
    lc_kxget(0);
    lcd_printf(0L, "key request !!!");
    printf("key request !!!\n");
}

indirect task4(void)
{
    printf("Hello there\n");
}

indirect task5(void)
{
                             // task not used
}

indirect backgnd(void)
{
    int count1, count2;
    
    count1 = 58;
    count2 = 58;
    run_every(TASK2, count2);
    run_every(TASK1, count1);
    run_every(TASK4, 1000);
    // count1 and count2 have to be lower than the reload values
    // for the respective virtual watchdog serviced by TASK1 and TASK2.
    // If any virtual watchdog is allowed to count down to zero, a 
    // hardware watchdog reset is forced.
    while (1) 
        runwatch();          // monitor with the watch window

}                            // end of background

