1 /* This test is designed to test application timer activation/deactivation services
2    from threads...  Make sure the remaining ticks are being saved/restored properly.  */
3 
4 #include   <stdio.h>
5 #include   "tx_api.h"
6 
7 //static unsigned long   expected_time;
8 static unsigned long   thread_0_counter =  0;
9 static TX_THREAD       thread_0;
10 
11 static unsigned long   timer_0_counter =  0;
12 static TX_TIMER        timer_0;
13 
14 
15 /* Define thread prototypes.  */
16 
17 static void    thread_0_entry(ULONG thread_input);
18 static void    timer_0_expiration(ULONG timer_input);
19 
20 
21 /* Prototype for test control return.  */
22 void  test_control_return(UINT status);
23 
24 
25 /* Define what the initial system looks like.  */
26 
27 #ifdef CTEST
test_application_define(void * first_unused_memory)28 void test_application_define(void *first_unused_memory)
29 #else
30 void    threadx_timer_deactivate_accuracy_application_define(void *first_unused_memory)
31 #endif
32 {
33 
34 UINT    status;
35 CHAR    *pointer;
36 
37 
38     /* Put first available memory address into a character pointer.  */
39     pointer =  (CHAR *) first_unused_memory;
40 
41     /* Put system definition stuff in here, e.g. thread creates and other assorted
42        create information.  */
43 
44     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
45             pointer, TEST_STACK_SIZE_PRINTF,
46             16, 16, 3, TX_AUTO_START);
47     pointer = pointer + TEST_STACK_SIZE_PRINTF;
48 
49     /* Check for status.  */
50     if (status != TX_SUCCESS)
51     {
52 
53         printf("Running Timer Activation/Deactivation Accuracy Test................. ERROR #1\n");
54         test_control_return(1);
55     }
56 
57     status =  tx_timer_create(&timer_0, "timer 0", timer_0_expiration, 0x1234,
58                         29, 31, TX_NO_ACTIVATE);
59 
60     /* Check for status.  */
61     if (status != TX_SUCCESS)
62     {
63 
64         printf("Running Timer Activation/Deactivation Accuracy Test................. ERROR #2\n");
65         test_control_return(1);
66     }
67 }
68 
69 
70 
71 /* Define the test threads.  */
72 
thread_0_entry(ULONG thread_input)73 static void    thread_0_entry(ULONG thread_input)
74 {
75 
76 UINT    status;
77 
78     /* Inform user.  */
79     printf("Running Timer Activation/Deactivation Accuracy Test................. ");
80 
81     /* Sleep so we have to handle the wrap condition.  */
82     tx_thread_sleep(10);
83 
84     /* Activate the timer.  */
85     status =  tx_timer_activate(&timer_0);
86 
87     /* Check for status.  */
88     if (status != TX_SUCCESS)
89     {
90 
91         /* Application timer error.  */
92         printf("ERROR #3\n");
93         test_control_return(1);
94     }
95 
96     /* Sleep */
97     tx_thread_sleep(2);
98 
99     /* Deactivate and activate the timer.  */
100     status =  tx_timer_deactivate(&timer_0);
101 
102     /* Check for status.  */
103     if (status != TX_SUCCESS)
104     {
105 
106         /* Application timer error.  */
107         printf("ERROR #4\n");
108         test_control_return(1);
109     }
110 
111     status =  tx_timer_activate(&timer_0);
112 
113     /* Check for status.  */
114     if (status != TX_SUCCESS)
115     {
116 
117         /* Application timer error.  */
118         printf("ERROR #5\n");
119         test_control_return(1);
120     }
121 
122     /* Sleep */
123     tx_thread_sleep(3);
124 
125     /* Deactivate and activate the timer.  */
126     status =  tx_timer_deactivate(&timer_0);
127 
128     /* Check for status.  */
129     if (status != TX_SUCCESS)
130     {
131 
132         /* Application timer error.  */
133         printf("ERROR #6\n");
134         test_control_return(1);
135     }
136 
137     status =  tx_timer_activate(&timer_0);
138 
139     /* Check for status.  */
140     if (status != TX_SUCCESS)
141     {
142 
143         /* Application timer error.  */
144         printf("ERROR #7\n");
145         test_control_return(1);
146     }
147 
148     /* Sleep */
149     tx_thread_sleep(4);
150 
151     /* Deactivate and activate the timer.  */
152     status =  tx_timer_deactivate(&timer_0);
153 
154     /* Check for status.  */
155     if (status != TX_SUCCESS)
156     {
157 
158         /* Application timer error.  */
159         printf("ERROR #8\n");
160         test_control_return(1);
161     }
162 
163     status =  tx_timer_activate(&timer_0);
164 
165     /* Check for status.  */
166     if (status != TX_SUCCESS)
167     {
168 
169         /* Application timer error.  */
170         printf("ERROR #9\n");
171         test_control_return(1);
172     }
173 
174     /* Sleep */
175     tx_thread_sleep(5);
176 
177     /* Deactivate and activate the timer.  */
178     status =  tx_timer_deactivate(&timer_0);
179 
180     /* Check for status.  */
181     if (status != TX_SUCCESS)
182     {
183 
184         /* Application timer error.  */
185         printf("ERROR #10\n");
186         test_control_return(1);
187     }
188 
189     status =  tx_timer_activate(&timer_0);
190 
191     /* Check for status.  */
192     if (status != TX_SUCCESS)
193     {
194 
195         /* Application timer error.  */
196         printf("ERROR #11\n");
197         test_control_return(1);
198     }
199 
200     /* Sleep */
201     tx_thread_sleep(6);
202 
203     /* Deactivate and activate the timer.  */
204     status =  tx_timer_deactivate(&timer_0);
205 
206     /* Check for status.  */
207     if (status != TX_SUCCESS)
208     {
209 
210         /* Application timer error.  */
211         printf("ERROR #12\n");
212         test_control_return(1);
213     }
214 
215     status =  tx_timer_activate(&timer_0);
216 
217     /* Check for status.  */
218     if (status != TX_SUCCESS)
219     {
220 
221         /* Application timer error.  */
222         printf("ERROR #13\n");
223         test_control_return(1);
224     }
225 
226     /* Sleep */
227     tx_thread_sleep(7);
228 
229     /* Deactivate and activate the timer.  */
230     status =  tx_timer_deactivate(&timer_0);
231 
232     /* Check for status.  */
233     if (status != TX_SUCCESS)
234     {
235 
236         /* Application timer error.  */
237         printf("ERROR #14\n");
238         test_control_return(1);
239     }
240 
241     status =  tx_timer_activate(&timer_0);
242 
243     /* Check for status.  */
244     if ((status != TX_SUCCESS) || (timer_0_counter))
245     {
246 
247         /* Application timer error.  */
248         printf("ERROR #15\n");
249         test_control_return(1);
250     }
251 
252     /* Sleep */
253     tx_thread_sleep(2);
254 
255     /* At this point the timer should have ran!  */
256     status =  tx_timer_deactivate(&timer_0);
257 
258     /* Increment thread 0 counter.  */
259     thread_0_counter++;
260 
261     /* Check for status.  */
262     if ((status != TX_SUCCESS) || (timer_0_counter != 1))
263     {
264 
265         /* Application timer error.  */
266         printf("ERROR #16\n");
267         test_control_return(1);
268     }
269     else
270     {
271 
272         /* Successful Re-activate test.  */
273         printf("SUCCESS!\n");
274         test_control_return(0);
275     }
276 }
277 
278 
timer_0_expiration(ULONG timer_input)279 static void    timer_0_expiration(ULONG timer_input)
280 {
281 
282 
283     /* Process timer expiration.  */
284     timer_0_counter++;
285 }
286 
287