Example of Cyclic Handler APIs- Time Management Functions

  1 /*
  2  *        Project: Time mgt APIs
  3  *  Description: Code to demonstrate Cyclic handler APIs
  4  *  File: main.c
  5  *
  6  *        (c) Viometrix Private Limited and T-Engine Developer Network (TEDN), 2008 - 2009.
  7  *
  8  */
  9 
 10 #include <basic.h>
 11 #include <tk/tkernel.h>
 12 #include <stdio.h>
 13 
 14 IMPORT void task1 (INT, VP);
 15 IMPORT void cychdr1 (VP);
 16 
 17 
 18 ID taskid1 = -1;
 19 ID cychdrid1 = -1;
 20 
 21 /* main: main function to launch the tasks 
 22  * ac >= 0 : launch the application
 23  * ac < 0  : unload the application
 24  */
 25 EXPORT ER
 26 main (INT ac, UB * av[])
 27 {
 28   T_CTSK ctsk;
 29   T_CCYC ccyc;
 30   ER ercd = 0;
 31 
 32   printf
 33     ("This code demonstrates basic cyclic handler APIs:\n cre, sta, del\n");
 34 
 35   if (ac < 0)
 36     {
 37       if (taskid1 >= 0)
 38         {
 39           ercd = tk_ter_tsk (taskid1);
 40           if (ercd < 0)
 41             printf ("tk_ter_tsk err = %x\n", ercd);
 42           ercd = tk_del_tsk (taskid1);
 43           if (ercd < 0)
 44             printf ("tk_del_tsk err = %x\n", ercd);
 45         }
 46       goto ext;
 47     }
 48 
 49   /* ---------------- TASK 1 --------------- */
 50   /* Set up the context for the task */
 51   ctsk.exinf = (VP) 0x00;        /* No extended information */
 52   ctsk.tskatr = TA_HLNG | TA_RNG0;        /* Run the task at Ring 0 - doesn't matter without MMU */
 53   ctsk.task = task1;                /* start address of the task */
 54   ctsk.itskpri = 80;                /* task initial priority */
 55   ctsk.stksz = 8192;                /* stack size */
 56 
 57   /* Create the task */
 58   taskid1 = tk_cre_tsk (&ctsk);
 59   if (taskid1 < E_OK)
 60     {                                /* task creation failed */
 61       printf ("Task creation failed.  Error code = %x\n", ercd);
 62       goto exit_point;
 63     }
 64 
 65   printf ("Task 1 created.  Task ID = %d.\n", taskid1);
 66 
 67   /* Launch the task */
 68   printf ("Launching Task 1 now.\n");
 69   tk_sta_tsk (taskid1, 0);
 70 
 71   /* ---------------- Cyclic Handler --------------- */
 72   /* Set up the context for the cyclic handler */
 73   ccyc.exinf = (VP) 0x00;        /* No extended information */
 74   ccyc.cycatr = TA_HLNG | TA_RNG0;        /* Run the cychdr at Ring 0 - doesn't matter without MMU */
 75   ccyc.cychdr = cychdr1;        /* start address of the cyc handler */
 76   ccyc.cyctim = 4000;                /* cycle time */
 77   ccyc.cycphs = 1000;                /* cycle phase */
 78 
 79   /* Create the task */
 80   cychdrid1 = tk_cre_cyc (&ccyc);
 81   if (cychdrid1 < E_OK)
 82     {                                /* task creation failed */
 83       printf ("CycHdr creation failed.  Error code = %x\n", ercd);
 84       goto exit_point;
 85     }
 86 
 87   printf ("Cyclic Handler created.  Cyc Hdr ID = %d.\n", cychdrid1);
 88 
 89   /* Launch the task */
 90   //printf ("Launching Task 2 now.\n");
 91   tk_sta_cyc (cychdrid1);
 92 
 93   tk_dly_tsk (20000);
 94 
 95 
 96   //Terminating and deleting Cychdr
 97   ercd = tk_stp_cyc (cychdrid1);
 98   if (ercd < 0)
 99     {
100       printf ("Cannot Stop Cychdr");
101       goto exit_point;
102     }
103   else
104     printf ("Cychdr Stopped \n");
105 
106   ercd = tk_del_cyc (cychdrid1);
107   if (ercd < 0)
108     {
109       printf ("Cannot delete Cychdr\n");
110       goto exit_point;
111     }
112   else
113     printf ("Cychdr deleted \n");
114 
115   //Terminating and deleting Task-1
116   ercd = tk_ter_tsk (taskid1);
117   if (ercd < 0)
118     {
119       printf ("Cannot terminate Task-1");
120       goto exit_point;
121     }
122   else
123     printf ("Task-1 Terminated \n");
124 
125   ercd = tk_del_tsk (taskid1);
126   if (ercd < 0)
127     {
128       printf ("Cannot delete Task-1");
129       goto exit_point;
130     }
131   else
132     printf ("Task-1 deleted \n");
133 
134 
135 
136 exit_point:
137 ext:
138   printf ("main ended\n");
139   return 0;
140 }
141 
142 /*****************************************************************
143 task1- Waiting forever-Wakenup by Cychdr every 5 seconds
144  ****************************************************************/
145 IMPORT void
146 task1 (INT stacd, VP exinf)
147 {
148 
149   printf ("This is task 1-waiting forever\n");
150   while (1)
151     {
152       tk_slp_tsk (-1);
153       printf ("Task-1 Waken up\n");
154     }
155 }
156 
157 /*****************************************************************
158 Cychdr wakes up task-1 every 5 seconds
159  ****************************************************************/
160 IMPORT void
161 cychdr1 (VP exinf)
162 {
163   printf ("\nCyc hdr starts Periodic execution\n");
164   tk_wup_tsk (taskid1);
165   printf ("\nCyc hdr Periodic execution\n");
166 }

Comments

Click here to Post a Comment