1 /* This test is designed to test the ux_utility_event_flags.  */
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_event_flags_test_application_define(void *first_unused_memory)
81 #endif
82 {
83 
84 UINT status;
85 
86     /* Inform user.  */
87     printf("Running ux_utility_event_flags... 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_EVENT_FLAGS_GROUP test_flags;
129 
130     error_callback_ignore = UX_TRUE;
131 
132     status = _ux_utility_event_flags_create(&test_flags, "test flags");
133     if (status != UX_SUCCESS)
134     {
135         printf("ERROR #%d: error 0x%x\n", __LINE__, status);
136         error_counter ++;
137     }
138 
139     status = _ux_utility_event_flags_create(&test_flags, "test flags");
140     if (status == UX_SUCCESS)
141     {
142         printf("ERROR #%d: expect fail\n", __LINE__);
143         error_counter ++;
144     }
145 
146     /* Check for errors.  */
147     if (error_counter)
148     {
149 
150         /* Test error.  */
151         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
152         test_control_return(1);
153     }
154     else
155     {
156 
157         /* Successful test.  */
158         printf("SUCCESS!\n");
159         test_control_return(0);
160     }
161 }
162 
ux_test_thread_simulation_1_entry(ULONG arg)163 static void  ux_test_thread_simulation_1_entry(ULONG arg)
164 {
165     while(1)
166     {
167         _ux_utility_delay_ms(100);
168     }
169 }
170