1 /* This test is designed to test the ux_utility_semaphore.  */
2 
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "ux_api.h"
6 #include "ux_system.h"
7 #include "ux_utility.h"
8 
9 /* Define USBX test constants.  */
10 
11 #define UX_TEST_STACK_SIZE      4096
12 #define UX_TEST_BUFFER_SIZE     2048
13 #define UX_TEST_RUN             1
14 #define UX_TEST_MEMORY_SIZE     (64*1024)
15 
16 /* Define the counters used in the test application...  */
17 
18 static CHAR                            *stack_pointer;
19 static CHAR                            *memory_pointer;
20 
21 static ULONG                           thread_0_counter;
22 static ULONG                           thread_1_counter;
23 static ULONG                           error_counter;
24 
25 static UCHAR                           error_callback_ignore = UX_FALSE;
26 static ULONG                           error_callback_counter;
27 
28 static UCHAR                           buffer[UX_TEST_BUFFER_SIZE];
29 
30 /* Define USBX test global variables.  */
31 
32 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
33 
34 static TX_THREAD           ux_test_thread_simulation_0;
35 static TX_THREAD           ux_test_thread_simulation_1;
36 static void                ux_test_thread_simulation_0_entry(ULONG);
37 static void                ux_test_thread_simulation_1_entry(ULONG);
38 
39 
40 /* Define the ISR dispatch.  */
41 
42 extern VOID    (*test_isr_dispatch)(void);
43 
44 
45 /* Prototype for test control return.  */
46 
47 void  test_control_return(UINT status);
48 
49 /* Simulator actions. */
50 
51 /* Define the ISR dispatch routine.  */
52 
test_isr(void)53 static void    test_isr(void)
54 {
55 
56     /* For further expansion of interrupt-level testing.  */
57 }
58 
59 
error_callback(UINT system_level,UINT system_context,UINT error_code)60 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
61 {
62 
63     error_callback_counter ++;
64 
65     if (!error_callback_ignore)
66     {
67         {
68             /* Failed test.  */
69             printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
70             // test_control_return(1);
71         }
72     }
73 }
74 
75 /* Define what the initial system looks like.  */
76 
77 #ifdef CTEST
test_application_define(void * first_unused_memory)78 void test_application_define(void *first_unused_memory)
79 #else
80 void    usbx_ux_utility_semaphore_test_application_define(void *first_unused_memory)
81 #endif
82 {
83 
84 UINT status;
85 
86     /* Inform user.  */
87     printf("Running ux_utility_semaphore... Test................................ ");
88 
89     /* Initialize the free memory pointer.  */
90     stack_pointer = (CHAR *) first_unused_memory;
91     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
92 
93     /* Initialize USBX Memory.  */
94     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
95 
96     /* Check for error.  */
97     if (status != UX_SUCCESS)
98     {
99 
100         printf("ERROR #%d\n", __LINE__);
101         test_control_return(1);
102     }
103 
104     /* Register the error callback. */
105     _ux_utility_error_callback_register(error_callback);
106 
107     /* Create the simulation thread.  */
108     status =  tx_thread_create(&ux_test_thread_simulation_0, "test simulation 0", ux_test_thread_simulation_0_entry, 0,
109             stack_pointer, UX_TEST_STACK_SIZE,
110             20, 20, 1, TX_AUTO_START);
111 
112     /* Check for error.  */
113     if (status != TX_SUCCESS)
114     {
115 
116         printf("ERROR #%d: thread create fail 0x%x\n", __LINE__, status);
117         test_control_return(1);
118     }
119 
120     /* Reset threads. */
121     _ux_utility_memory_set(&ux_test_thread_simulation_1, 0, sizeof(ux_test_thread_simulation_1));
122 }
123 
124 
ux_test_thread_simulation_0_entry(ULONG arg)125 static void  ux_test_thread_simulation_0_entry(ULONG arg)
126 {
127 UINT         status;
128 TX_SEMAPHORE test_semaphore;
129 
130     error_callback_ignore = UX_TRUE;
131 
132     status = _ux_utility_semaphore_create(&test_semaphore, "test semaphore", 1);
133     if (status != UX_SUCCESS)
134     {
135 
136         printf("ERROR #%d: sem_create 0x%x\n", __LINE__, status);
137         error_counter ++;
138     }
139 
140     status = _ux_utility_semaphore_create(&test_semaphore, "test semaphore", 1);
141     if (status == UX_SUCCESS)
142     {
143 
144         printf("ERROR #%d: sem_create must fail\n", __LINE__);
145         error_counter ++;
146     }
147 
148     status = _ux_utility_semaphore_get(&test_semaphore, 100);
149     if (status != UX_SUCCESS)
150     {
151 
152         printf("ERROR #%d: sem_get 0x%x\n", __LINE__, status);
153         error_counter ++;
154     }
155 
156     status = _ux_utility_semaphore_get(&test_semaphore, 10);
157     if (status == UX_SUCCESS)
158     {
159 
160         printf("ERROR #%d: sem_get 0x%x\n", __LINE__, status);
161         error_counter ++;
162     }
163 
164     /* Check for errors.  */
165     if (error_counter)
166     {
167 
168         /* Test error.  */
169         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
170         test_control_return(1);
171     }
172     else
173     {
174 
175         /* Successful test.  */
176         printf("SUCCESS!\n");
177         test_control_return(0);
178     }
179 }
180 
ux_test_thread_simulation_1_entry(ULONG arg)181 static void  ux_test_thread_simulation_1_entry(ULONG arg)
182 {
183     while(1)
184     {
185         _ux_utility_delay_ms(100);
186     }
187 }
188