This page is available in:
en
Example of Alarm Handler APIs- Time Management Functions
1 /* 2 * Description: Code to demonstrate Alarm handler APIs 3 * File: main.c 4 * 5 * (c) Viometrix Private Limited and T-Engine Developer Network (TEDN), 2008 - 2009. 6 * 7 */ 8 9 #include <basic.h> 10 #include <tk/tkernel.h> 11 #include <stdio.h> 12 13 IMPORT void task1 (INT, VP); 14 IMPORT void almhdr1 (VP); 15 16 17 ID taskid1 = -1; 18 ID almid1 = -1; 19 20 /* main: main function to launch the tasks 21 * ac >= 0 : launch the application 22 * ac < 0 : unload the application 23 */ 24 EXPORT ER 25 main (INT ac, UB * av[]) 26 { 27 T_CTSK ctsk; 28 T_CALM calm; 29 ER ercd = 0; 30 SYSTIM systime; 31 32 printf 33 ("This code demonstrates basic alarm 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 /* ---------------- Alarm Handler --------------- */ 72 /* Set up the context for the Alarm handler */ 73 calm.exinf = (VP) 0x00; /* No extended information */ 74 calm.almatr = TA_HLNG | TA_RNG0; /* Run the almhdr at Ring 0 - doesn't matter without MMU */ 75 calm.almhdr = almhdr1; /* start address of the alm handler */ 76 77 78 /* Create the task */ 79 almid1 = tk_cre_alm (&calm); 80 if (almid1 < E_OK) 81 { /* Alarm creation failed */ 82 printf ("AlmHdr creation failed. Error code = %x\n", ercd); 83 goto exit_point; 84 } 85 86 printf ("Alarm Handler created. Alm Hdr ID = %d.\n", almid1); 87 88 89 ercd = tk_get_tim (&systime); 90 printf ("Systime hi = %d lo = %ul\n", systime.hi, systime.lo); 91 92 /* Launch the Almhdr */ 93 ercd = tk_get_otm (&systime); 94 printf ("OS time hi = %d lo = %ul\n", systime.hi, systime.lo); 95 96 tk_sta_alm (almid1, 10000); 97 98 tk_dly_tsk (20000); 99 100 ercd = tk_get_otm (&systime); 101 printf ("OS time hi = %d lo = %ul\n", systime.hi, systime.lo); 102 103 //Terminating and deleting Almhdr 104 ercd = tk_stp_alm (almid1); 105 if (ercd < 0) 106 { 107 printf ("Cannot Stop Almhdr"); 108 goto exit_point; 109 } 110 else 111 { 112 printf ("Almhdr Stopped \n"); 113 } 114 115 ercd = tk_del_alm (almid1); 116 if (ercd < 0) 117 { 118 printf ("Cannot delete Almhdr\n"); 119 goto exit_point; 120 } 121 else 122 { 123 printf ("Almhdr deleted \n"); 124 } 125 //Terminating and deleting Task-1 126 ercd = tk_ter_tsk (taskid1); 127 if (ercd < 0) 128 { 129 printf ("Cannot terminate Task-1"); 130 goto exit_point; 131 } 132 else 133 { 134 printf ("Task-1 Terminated \n"); 135 } 136 ercd = tk_del_tsk (taskid1); 137 if (ercd < 0) 138 { 139 printf ("Cannot delete Task-1"); 140 goto exit_point; 141 } 142 else 143 { 144 printf ("Task-1 deleted \n"); 145 } 146 147 148 exit_point: 149 ext: 150 printf ("main ended\n"); 151 return 0; 152 } 153 154 /***************************************************************** 155 task1- Waiting forever-Wakenup by Almhdr every 5 seconds 156 ****************************************************************/ 157 IMPORT void 158 task1 (INT stacd, VP exinf) 159 { 160 161 ER ercd = 0; 162 SYSTIM systime; 163 printf ("This is task 1-waiting forever\n"); 164 while (1) 165 { 166 tk_slp_tsk (-1); 167 printf ("Task-1 Waken up\n"); 168 ercd = tk_get_tim (&systime); 169 printf ("Systime hi = %d lo = %ul\n", systime.hi, systime.lo); 170 } 171 } 172 173 /***************************************************************** 174 Almhdr wakes up task-1 after 10 seconds 175 ****************************************************************/ 176 IMPORT void 177 almhdr1 (VP exinf) 178 { 179 /* Wakes up Task 1 after the specified time */ 180 tk_wup_tsk (taskid1); 181 }

Comments
Click here to Post a Comment