1 /* This test is designed to test the simple dpump host/device class operation.  */
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 #include "ux_host_class_dummy.h"
9 #include "ux_device_class_dummy.h"
10 #include "ux_test.h"
11 
12 
13 /* Define USBX demo constants.  */
14 
15 #define UX_DEMO_STACK_SIZE      4096
16 #define UX_DEMO_BUFFER_SIZE     2048
17 #define UX_DEMO_RUN             1
18 #define UX_DEMO_MEMORY_SIZE     (64*1024)
19 
20 
21 /* Define the counters used in the demo application...  */
22 
23 static ULONG                           thread_0_counter;
24 static ULONG                           thread_1_counter;
25 static ULONG                           error_counter;
26 
27 
28 /* Define USBX demo global variables.  */
29 
30 static UX_HOST_CLASS                   *class_driver;
31 static UX_HOST_CLASS_DUMMY             *dummy;
32 static UX_DEVICE_CLASS_DUMMY           *dummy_slave;
33 
34 static UINT                             expected_error;
35 
36 #define _W0(w)      ( (w)       & 0xFF)
37 #define _W1(w)      (((w) >> 8) & 0xFF)
38 
39 #define _DEVICE_DESCRIPTOR(cls, sub, protocol, pktsize, vid, pid, n_cfg)        \
40     0x12, 0x01, 0x10, 0x01,                                                     \
41     (cls), (sub), (protocol), (pktsize),                                        \
42     _W0(vid), _W1(vid), _W0(pid), _W1(pid),                                     \
43     0x00, 0x00, 0x00, 0x00, 0x00, (n_cfg),
44 
45 #define _QUALIFIER_DESCRIPTOR(cls, sub, protocol, n_cfg)                        \
46     0x0a, 0x06, 0x00, 0x02,                                                     \
47     (cls), (sub), (protocol), 0x40, (n_cfg), 0x00,
48 
49 #define _CONFIGURATION_DESCRIPTOR(total_len, n_ifc, cfg_val)                    \
50     0x09, 0x02, _W0(total_len), _W1(total_len), (n_ifc), (cfg_val),             \
51     0x00, 0xc0, 0x32,
52 
53 #define _INTERFACE_DESCRIPTOR(ifc_n, alt, n_ep, cls, sub, protocol)             \
54     0x09, 0x04, (ifc_n), (alt), (n_ep), (cls), (sub), (protocol), 0x00,
55 
56 #define _ENDPOINT_DESCRIPTOR(addr, attr, pktsize, interval)                     \
57     0x07, 0x05, (addr), (attr), _W0(pktsize), _W1(pktsize), (interval),
58 
59 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
60 static UCHAR device_framework_full_speed[] = {
61     _DEVICE_DESCRIPTOR(0x99, 0x99, 0x99, 8, 0x08EC, 0x0001, 1)
62     _CONFIGURATION_DESCRIPTOR(32, 1, 1)
63     _INTERFACE_DESCRIPTOR(0, 0, 2, 0x99, 0x99, 0x99)
64     _ENDPOINT_DESCRIPTOR(0x01, 0x02, 64, 0x00)
65     _ENDPOINT_DESCRIPTOR(0x82, 0x02, 64, 0x00)
66 };
67 
68 
69 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
70 static UCHAR device_framework_high_speed[] = {
71     _DEVICE_DESCRIPTOR(0x99, 0x99, 0x99, 64, 0x08EC, 0x0001, 1)
72     _QUALIFIER_DESCRIPTOR(0, 0, 0, 1)
73     _CONFIGURATION_DESCRIPTOR(32, 1, 1)
74     _INTERFACE_DESCRIPTOR(0, 0, 2, 0x99, 0x99, 0x99)
75     _ENDPOINT_DESCRIPTOR(0x01, 0x02, 512, 0x00)
76     _ENDPOINT_DESCRIPTOR(0x82, 0x02, 512, 0x00)
77 };
78 
79 /* String Device Framework :
80     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
81     Byte 2       : Byte containing the index of the descriptor
82     Byte 3       : Byte containing the length of the descriptor string
83 */
84 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
85 static UCHAR string_framework[] = {
86 
87     /* Manufacturer string descriptor : Index 1 */
88     0x09, 0x04, 0x01, 0x0c,
89     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
90     0x6f, 0x67, 0x69, 0x63,
91 
92     /* Product string descriptor : Index 2 */
93     0x09, 0x04, 0x02, 0x0c,
94     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
95     0x44, 0x65, 0x6d, 0x6f,
96 
97     /* Serial Number string descriptor : Index 3 */
98     0x09, 0x04, 0x03, 0x04,
99     0x30, 0x30, 0x30, 0x31
100 };
101 
102 
103 /* Multiple languages are supported on the device, to add
104     a language besides English, the unicode language code must
105     be appended to the language_id_framework array and the length
106     adjusted accordingly. */
107 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
108 static UCHAR language_id_framework[] = {
109 
110     /* English. */
111     0x09, 0x04
112 };
113 
114 
115 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
116 
117 static VOID                tx_demo_instance_activate(VOID  *dummy_instance);
118 static VOID                tx_demo_instance_deactivate(VOID *dummy_instance);
119 
120 #if defined(UX_HOST_STANDALONE)
121 static UINT                tx_demo_host_change_function(ULONG e, UX_HOST_CLASS *c, VOID *p);
122 #else
123 #define                     tx_demo_host_change_function UX_NULL
124 #endif
125 
126 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
127 
128 static TX_THREAD           tx_demo_thread_host_simulation;
129 static TX_THREAD           tx_demo_thread_slave_simulation;
130 static void                tx_demo_thread_host_simulation_entry(ULONG);
131 static void                tx_demo_thread_slave_simulation_entry(ULONG);
132 
133 
134 /* Define the ISR dispatch.  */
135 
136 extern VOID    (*test_isr_dispatch)(void);
137 
138 
139 /* Prototype for test control return.  */
140 
141 void  test_control_return(UINT status);
142 
143 
144 /* Define the ISR dispatch routine.  */
145 
test_isr(void)146 static void    test_isr(void)
147 {
148 
149     /* For further expansion of interrupt-level testing.  */
150 }
151 
152 
error_callback(UINT system_level,UINT system_context,UINT error_code)153 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
154 {
155     if (expected_error == 0 || error_code != expected_error)
156     {
157         /* Failed test.  */
158         printf("Error on line %d, system_level: %d, system_context: %d, error code: %x\n", __LINE__, system_level, system_context, error_code);
159         // test_control_return(1);
160     }
161 }
162 
163 /* Define what the initial system looks like.  */
164 
165 #ifdef CTEST
test_application_define(void * first_unused_memory)166 void test_application_define(void *first_unused_memory)
167 #else
168 void    usbx_class_device_enumeration_test_application_define(void *first_unused_memory)
169 #endif
170 {
171 
172 UINT status;
173 CHAR                            *stack_pointer;
174 CHAR                            *memory_pointer;
175 UX_DEVICE_CLASS_DUMMY_PARAMETER  parameter;
176 
177 
178     printf("Running Basic Device Class Enumeration Test......................... ");
179 
180     /* Initialize the free memory pointer.  */
181     stack_pointer = (CHAR *) first_unused_memory;
182     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
183 
184     /* Initialize USBX Memory.  */
185     status =  ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
186 
187     /* Check for error.  */
188     if (status != UX_SUCCESS)
189     {
190 
191         printf("ERROR #%d\n", __LINE__);
192         test_control_return(1);
193     }
194 
195     /* Register the error callback. */
196     _ux_utility_error_callback_register(error_callback);
197 
198     /* The code below is required for installing the host portion of USBX.  */
199     status =  ux_host_stack_initialize(tx_demo_host_change_function);
200 
201     /* Check for error.  */
202     if (status != UX_SUCCESS)
203     {
204 
205         printf("ERROR #%d\n", __LINE__);
206         test_control_return(1);
207     }
208 
209     /* Register all the host class drivers for this USBX implementation.  */
210     status =  ux_host_stack_class_register(_ux_host_class_dummy_name, _ux_host_class_dummy_device_entry);
211 
212     /* Check for error.  */
213     if (status != UX_SUCCESS)
214     {
215 
216         printf("ERROR #%d\n", __LINE__);
217         test_control_return(1);
218     }
219 
220     /* The code below is required for installing the device portion of USBX */
221     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
222                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
223                                        string_framework, STRING_FRAMEWORK_LENGTH,
224                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
225 
226     /* Check for error.  */
227     if (status != UX_SUCCESS)
228     {
229 
230         printf("ERROR #%d\n", __LINE__);
231         test_control_return(1);
232     }
233 
234     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
235     _ux_utility_memory_set((void *)&parameter, 0x00, sizeof(parameter));
236     parameter.ux_device_class_dummy_parameter_callbacks.ux_device_class_dummy_instance_activate   =  tx_demo_instance_activate;
237     parameter.ux_device_class_dummy_parameter_callbacks.ux_device_class_dummy_instance_deactivate =  tx_demo_instance_deactivate;
238 
239     /* Initialize the device dpump class. The class is connected with interface 0 */
240     status =  ux_device_stack_class_register(_ux_device_class_dummy_name, _ux_device_class_dummy_entry,
241                                               1, 0, &parameter);
242 
243     /* Check for error.  */
244     if (status != UX_SUCCESS)
245     {
246 
247         printf("ERROR #%d\n", __LINE__);
248         test_control_return(1);
249     }
250 
251     /* Initialize the simulated device controller.  */
252     status =  _ux_dcd_sim_slave_initialize();
253 
254     /* Check for error.  */
255     if (status != UX_SUCCESS)
256     {
257 
258         printf("ERROR #%d\n", __LINE__);
259         test_control_return(1);
260     }
261 
262     /* Register all the USB host controllers available in this system */
263     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
264 
265     /* Check for error.  */
266     if (status != UX_SUCCESS)
267     {
268 
269         printf("ERROR #%d\n", __LINE__);
270         test_control_return(1);
271     }
272 
273     /* Create the main host simulation thread.  */
274     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
275             stack_pointer, UX_DEMO_STACK_SIZE,
276             20, 20, 1, TX_AUTO_START);
277 
278     /* Check for error.  */
279     if (status != TX_SUCCESS)
280     {
281 
282         printf("ERROR #%d\n", __LINE__);
283         test_control_return(1);
284     }
285 
286     /* Create the main demo thread.  */
287     status =  tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
288             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
289             20, 20, 1, TX_AUTO_START);
290 
291     /* Check for error.  */
292     if (status != TX_SUCCESS)
293     {
294 
295         printf("ERROR #%d\n", __LINE__);
296         test_control_return(1);
297     }
298 }
299 
300 
tx_demo_thread_host_simulation_entry(ULONG arg)301 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
302 {
303 
304 UINT status;
305 ULONG           actual_length;
306 UCHAR           current_char;
307 UX_HOST_CLASS   *cls;
308 UINT            i;
309 
310     stepinfo(">>>>> Dummy Class Get\n");
311     status = ux_host_stack_class_get(_ux_host_class_dummy_name, &cls);
312     if (status != UX_SUCCESS)
313     {
314         printf("ERROR #%d: 0x%x\n", __LINE__, status);
315         test_control_return(1);
316     }
317 
318     stepinfo(">>>>> Dummy Class Instance Wait\n");
319     do
320     {
321         status =  ux_host_stack_class_instance_get(cls, 0, (VOID **) &dummy);
322 #if defined(UX_HOST_STANDALONE)
323         ux_system_tasks_run();
324 #endif
325         tx_thread_relinquish();
326     } while (status != UX_SUCCESS);
327 
328     stepinfo(">>>>> Dummy Class State Wait\n");
329     while(dummy -> ux_host_class_dummy_state != UX_HOST_CLASS_INSTANCE_LIVE)
330     {
331 #if defined(UX_HOST_STANDALONE)
332         ux_system_tasks_run();
333 #endif
334         tx_thread_relinquish();
335     }
336 
337     expected_error = 0;
338 
339     /* Sleep for a tick to make sure everything is complete.  */
340     tx_thread_sleep(1);
341 
342     /* Check for errors from other threads.  */
343     if (error_counter)
344     {
345 
346         /* DPUMP error.  */
347         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
348         test_control_return(1);
349     }
350     else
351     {
352 
353         /* Successful test.  */
354         printf("SUCCESS!\n");
355         test_control_return(0);
356     }
357 }
358 
359 
tx_demo_thread_slave_simulation_entry(ULONG arg)360 static void  tx_demo_thread_slave_simulation_entry(ULONG arg)
361 {
362 
363 UINT    status;
364 ULONG   actual_length;
365 
366 
367     while(1)
368     {
369 #if defined(UX_DEVICE_STANDALONE)
370 
371         /* Run device tasks.  */
372         ux_system_tasks_run();
373 #endif
374         /* Increment thread counter.  */
375         thread_1_counter++;
376 
377         /* Relinquish to other thread.  */
378         tx_thread_relinquish();
379     }
380 }
381 
tx_demo_instance_activate(VOID * inst)382 static VOID  tx_demo_instance_activate(VOID *inst)
383 {
384     dummy_slave = (UX_DEVICE_CLASS_DUMMY *)inst;
385 }
386 
tx_demo_instance_deactivate(VOID * inst)387 static VOID  tx_demo_instance_deactivate(VOID *inst)
388 {
389     dummy_slave = UX_NULL;
390 }
391 
392 #if defined(UX_HOST_STANDALONE)
tx_demo_host_change_function(ULONG e,UX_HOST_CLASS * c,VOID * p)393 static UINT  tx_demo_host_change_function(ULONG e, UX_HOST_CLASS *c, VOID *p)
394 {
395     if (e == UX_STANDALONE_WAIT_BACKGROUND_TASK)
396     {
397         tx_thread_relinquish();
398     }
399 }
400 #endif
401