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
9 #include "fx_api.h"
10
11 #include "ux_device_class_cdc_acm.h"
12 #include "ux_device_stack.h"
13
14 #include "ux_host_class_cdc_acm.h"
15
16 #include "ux_test_dcd_sim_slave.h"
17 #include "ux_test_hcd_sim_host.h"
18 #include "ux_test_utility_sim.h"
19
20 #include "ux_host_stack.h"
21
22 /* Define constants. */
23 #define UX_DEMO_DEBUG_SIZE (4096*8)
24 #define UX_DEMO_STACK_SIZE 1024
25 #define UX_DEMO_BUFFER_SIZE (UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1)
26 #define UX_DEMO_XMIT_BUFFER_SIZE 512
27 #define UX_DEMO_RECEPTION_BUFFER_SIZE 512
28 #define UX_DEMO_FILE_BUFFER_SIZE 512
29 #define UX_DEMO_RECEPTION_BLOCK_SIZE 64
30 #define UX_DEMO_MEMORY_SIZE (64*1024)
31 #define UX_DEMO_FILE_SIZE (128 * 1024)
32 #define UX_RAM_DISK_MEMORY (256 * 1024)
33
34 #define LSB(x) ( (x) & 0x00ff)
35 #define MSB(x) (((x) & 0xff00) >> 8)
36
37 /* Configuration descriptor 9 bytes */
38 #define CFG_DESC(wTotalLength, bNumInterfaces, bmAttributes, bConfigurationValue)\
39 /* Configuration 1 descriptor 9 bytes */\
40 0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
41 (bNumInterfaces), (bConfigurationValue), 0x00,\
42 (bmAttributes), 0x00,
43 #define CFG_DESC_LEN 9
44
45 #define IAD_DESC(bIfc) \
46 /* Interface association descriptor. 8 bytes. */\
47 0x08, 0x0b, (bIfc), 0x02, 0x02, 0x02, 0x00, 0x00,
48 #define IAD_DESC_LEN 8
49
50 #define CDC_IFC_DESC_ALL(bIfc, bIntIn, bBulkIn, bBulkOut)\
51 /* Communication Class Interface Descriptor Requirement. 9 bytes. */\
52 0x09, 0x04, (bIfc), 0x00, 0x01, 0x02, 0x02, 0x01, 0x00,\
53 /* Header Functional Descriptor 5 bytes */\
54 0x05, 0x24, 0x00, 0x10, 0x01,\
55 /* ACM Functional Descriptor 4 bytes */\
56 0x04, 0x24, 0x02, 0x0f,\
57 /* Union Functional Descriptor 5 bytes */\
58 0x05, 0x24, 0x06, (bIfc), (bIfc + 1),\
59 /* Call Management Functional Descriptor 5 bytes */\
60 0x05, 0x24, 0x01, 0x03, (bIfc + 1),\
61 /* Endpoint interrupt in descriptor 7 bytes */\
62 0x07, 0x05, (bIntIn), 0x03, 0x40, 0x00, 0x10,\
63 /* Data Class Interface Descriptor Requirement 9 bytes */\
64 0x09, 0x04, (bIfc + 1), 0x00, 0x02, 0x0A, 0x00, 0x00, 0x00,\
65 /* Endpoint bulk in descriptor 7 bytes */\
66 0x07, 0x05, (bBulkIn), 0x02, 0x40, 0x00, 0x01,\
67 /* Endpoint bulk out descriptor 7 bytes */\
68 0x07, 0x05, (bBulkOut), 0x02, 0x40, 0x00, 0x01,
69 #define CDC_IFC_DESC_ALL_LEN (9+5+4+5+5+7+ 9+7+7)
70
71 /* Define local/extern function prototypes. */
72 static VOID test_thread_entry(ULONG);
73 static TX_THREAD tx_test_thread_host_simulation;
74 static TX_THREAD tx_test_thread_slave_simulation;
75 static VOID tx_test_thread_host_simulation_entry(ULONG);
76 static VOID tx_test_thread_slave_simulation_entry(ULONG);
77 static VOID test_cdc_instance_activate(VOID *cdc_instance);
78 static VOID test_cdc_instance_deactivate(VOID *cdc_instance);
79 static VOID test_cdc_instance_parameter_change(VOID *cdc_instance);
80
81 /* Define global data structures. */
82 static UCHAR usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
83 static UX_HOST_CLASS *class_driver;
84 static UX_HOST_CLASS_CDC_ACM *cdc_acm_host_control;
85 static UX_HOST_CLASS_CDC_ACM *cdc_acm_host_data;
86
87 static UX_SLAVE_CLASS_CDC_ACM *cdc_acm_slave;
88 static UCHAR cdc_acm_slave_change;
89 static UX_SLAVE_CLASS_CDC_ACM_PARAMETER parameter;
90
91 static ULONG error_counter;
92
93 static ULONG error_callback_counter;
94 static UCHAR error_callback_ignore;
95
96 static ULONG call_counter;
97
98 static UCHAR buffer[UX_DEMO_BUFFER_SIZE];
99
100 /* Define device framework. */
101
102 static unsigned char device_framework_full_speed[] = {
103
104 /* Device descriptor 18 bytes
105 0x02 bDeviceClass: CDC class code
106 0x00 bDeviceSubclass: CDC class sub code
107 0x00 bDeviceProtocol: CDC Device protocol
108
109 idVendor & idProduct - http://www.linux-usb.org/usb.ids
110 */
111 0x12, 0x01, 0x10, 0x01,
112 0xEF, 0x02, 0x01,
113 0x08,
114 0x84, 0x84, 0x00, 0x00,
115 0x00, 0x01,
116 0x01, 0x02, 03,
117 0x01, /* bNumConfigurations */
118
119 /* Configuration 1 descriptor 9 bytes, total 75 bytes */
120 CFG_DESC(CFG_DESC_LEN + IAD_DESC_LEN + CDC_IFC_DESC_ALL_LEN, 2, 0x40, 1)
121 /* IAD 8 bytes */
122 IAD_DESC(0)
123 /* CDC_ACM interfaces */
124 CDC_IFC_DESC_ALL(0, 0x83, 0x81, 0x02)
125 };
126 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
127
128 static unsigned char device_framework_high_speed[] = {
129
130 /* Device descriptor 18 bytes
131 0x02 bDeviceClass: CDC class code
132 0x00 bDeviceSubclass: CDC class sub code
133 0x00 bDeviceProtocol: CDC Device protocol
134
135 idVendor & idProduct - http://www.linux-usb.org/usb.ids
136 */
137 0x12, 0x01, 0x00, 0x02,
138 0xEF, 0x02, 0x01,
139 0x40,
140 0x84, 0x84, 0x00, 0x00,
141 0x00, 0x01,
142 0x01, 0x02, 03,
143 0x01, /* bNumConfigurations */
144
145 /* Device qualifier descriptor 10 bytes */
146 0x0a, 0x06, 0x00, 0x02,
147 0x02, 0x00, 0x00,
148 0x40,
149 0x01,
150 0x00,
151
152 /* Configuration 1 descriptor 9 bytes, total 75-8=67 bytes */
153 CFG_DESC(CFG_DESC_LEN + CDC_IFC_DESC_ALL_LEN, 2, 0x60, 1)
154 CDC_IFC_DESC_ALL(0, 0x83, 0x81, 0x02)
155 };
156 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
157
158 static unsigned char string_framework[] = {
159
160 /* Manufacturer string descriptor : Index 1 - "Express Logic" */
161 0x09, 0x04, 0x01, 0x0c,
162 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
163 0x6f, 0x67, 0x69, 0x63,
164
165 /* Product string descriptor : Index 2 - "EL Composite device" */
166 0x09, 0x04, 0x02, 0x13,
167 0x45, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x6f,
168 0x73, 0x69, 0x74, 0x65, 0x20, 0x64, 0x65, 0x76,
169 0x69, 0x63, 0x65,
170
171 /* Serial Number string descriptor : Index 3 - "0001" */
172 0x09, 0x04, 0x03, 0x04,
173 0x30, 0x30, 0x30, 0x31
174 };
175 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
176
177 /* Multiple languages are supported on the device, to add
178 a language besides english, the unicode language code must
179 be appended to the language_id_framework array and the length
180 adjusted accordingly. */
181 static unsigned char language_id_framework[] = {
182
183 /* English. */
184 0x09, 0x04
185 };
186 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
187
188 /* Define the ISR dispatch. */
189
190 extern VOID (*test_isr_dispatch)(void);
191
192
193 /* Prototype for test control return. */
194
195 void test_control_return(UINT status);
196
197
198 /* Define the ISR dispatch routine. */
199
test_isr(void)200 static void test_isr(void)
201 {
202
203 /* For further expansion of interrupt-level testing. */
204 }
205
break_on_cdc_acm_all_ready(VOID)206 static UINT break_on_cdc_acm_all_ready(VOID)
207 {
208 UINT status;
209 UINT i;
210 UX_HOST_CLASS *class;
211 UX_HOST_CLASS_CDC_ACM *cdc_acm;
212
213 /* Find the main cdc_acm container */
214 status = ux_host_stack_class_get(_ux_system_host_class_cdc_acm_name, &class);
215 if (status != UX_SUCCESS)
216 /* Do not break. */
217 return 0;
218
219 /* Find class instances. */
220 for (i = 0; i < 2; i ++)
221 {
222 status = ux_host_stack_class_instance_get(class, i, (void **) &cdc_acm);
223 if (status != UX_SUCCESS)
224 /* Do not break. */
225 return 0;
226
227 switch(cdc_acm->ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass)
228 {
229
230 case UX_HOST_CLASS_CDC_CONTROL_CLASS:
231 cdc_acm_host_control = cdc_acm;
232 break;
233
234 case UX_HOST_CLASS_CDC_DATA_CLASS:
235 cdc_acm_host_data = cdc_acm;
236 break;
237
238 default:
239 break;
240 }
241 }
242
243 if (cdc_acm_host_control == UX_NULL || cdc_acm_host_data == UX_NULL)
244 /* Do not break. */
245 return 0;
246
247 if (cdc_acm_host_control->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
248 /* Do not break. */
249 return 0;
250
251 if (cdc_acm_host_data->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
252 /* Do not break. */
253 return 0;
254
255 if (_ux_system_slave->ux_system_slave_class_array[0].ux_slave_class_instance == UX_NULL)
256 /* Do not break. */
257 return 0;
258
259 cdc_acm_slave = (UX_SLAVE_CLASS_CDC_ACM *)_ux_system_slave->ux_system_slave_class_array[0].ux_slave_class_instance;
260
261 /* All found, break. */
262 return 1;
263 }
264
break_on_removal(VOID)265 static UINT break_on_removal(VOID)
266 {
267
268 UINT status;
269 UX_DEVICE *device;
270
271 status = ux_host_stack_device_get(0, &device);
272 if (status == UX_SUCCESS)
273 /* Do not break. */
274 return UX_SUCCESS;
275
276 if (_ux_system_slave->ux_system_slave_device.ux_slave_device_state > UX_DEVICE_RESET)
277 /* Do not break. */
278 return 0;
279
280 cdc_acm_host_data = UX_NULL;
281 cdc_acm_host_control = UX_NULL;
282 cdc_acm_slave = UX_NULL;
283
284 return 1;
285 }
286
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)287 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
288 {
289
290 UX_HOST_CLASS_CDC_ACM *cdc_acm = (UX_HOST_CLASS_CDC_ACM *) inst;
291
292 switch(event)
293 {
294
295 case UX_DEVICE_INSERTION:
296
297 if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
298 cdc_acm_host_control = cdc_acm;
299 else
300 cdc_acm_host_data = cdc_acm;
301 break;
302
303 case UX_DEVICE_REMOVAL:
304
305 if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
306 cdc_acm_host_control = UX_NULL;
307 else
308 cdc_acm_host_data = UX_NULL;
309 break;
310
311 default:
312 break;
313 }
314 return 0;
315 }
316
test_cdc_instance_activate(VOID * cdc_instance)317 static VOID test_cdc_instance_activate(VOID *cdc_instance)
318 {
319
320 /* Save the CDC instance. */
321 cdc_acm_slave = (UX_SLAVE_CLASS_CDC_ACM *) cdc_instance;
322 }
test_cdc_instance_deactivate(VOID * cdc_instance)323 static VOID test_cdc_instance_deactivate(VOID *cdc_instance)
324 {
325
326 /* Reset the CDC instance. */
327 cdc_acm_slave = UX_NULL;
328 }
329
test_cdc_instance_parameter_change(VOID * cdc_instance)330 static VOID test_cdc_instance_parameter_change(VOID *cdc_instance)
331 {
332
333 /* Set CDC parameter change flag. */
334 cdc_acm_slave_change = UX_TRUE;
335 }
336
test_ux_error_callback(UINT system_level,UINT system_context,UINT error_code)337 static VOID test_ux_error_callback(UINT system_level, UINT system_context, UINT error_code)
338 {
339 error_callback_counter ++;
340
341 if (!error_callback_ignore)
342 {
343 /* Ignore UX_DEVICE_HANDLE_UNKNOWN. */
344 if (UX_DEVICE_HANDLE_UNKNOWN == error_code)
345 return;
346 {
347 /* Failed test. */
348 printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
349 // test_control_return(1);
350 }
351 }
352 }
353
354 /* Define what the initial system looks like. */
355
356 #ifdef CTEST
test_application_define(void * first_unused_memory)357 void test_application_define(void *first_unused_memory)
358 #else
359 void usbx_ux_device_class_cdc_acm_deactivate_test_application_define(void *first_unused_memory)
360 #endif
361 {
362
363 UINT status;
364 CHAR * stack_pointer;
365 CHAR * memory_pointer;
366
367 printf("Running ux_device_class_cdc_acm_deactivate Test..................... ");
368
369 /* Initialize the free memory pointer */
370 stack_pointer = (CHAR *) usbx_memory;
371 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
372
373 /* Initialize USBX Memory */
374 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
375 /* Check for error. */
376 if (status != UX_SUCCESS)
377 {
378
379 printf("ERROR #%d\n", __LINE__);
380 test_control_return(1);
381 }
382
383 /* Register the error callback. */
384 _ux_utility_error_callback_register(test_ux_error_callback);
385
386 /* The code below is required for installing the host portion of USBX */
387 status = ux_host_stack_initialize(UX_NULL);
388 if (status != UX_SUCCESS)
389 {
390
391 printf("ERROR #%d\n", __LINE__);
392 test_control_return(1);
393 }
394
395 /* Register CDC ACM class */
396 status = ux_host_stack_class_register(_ux_system_host_class_cdc_acm_name, ux_host_class_cdc_acm_entry);
397 if (status != UX_SUCCESS)
398 {
399
400 printf("ERROR #%d\n", __LINE__);
401 test_control_return(1);
402 }
403
404 /* The code below is required for installing the device portion of USBX. No call back for
405 device status change in this example. */
406 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
407 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
408 string_framework, STRING_FRAMEWORK_LENGTH,
409 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
410 if(status!=UX_SUCCESS)
411 {
412
413 printf("ERROR #%d\n", __LINE__);
414 test_control_return(1);
415 }
416
417 /* Set the parameters for callback when insertion/extraction of a CDC device. */
418 parameter.ux_slave_class_cdc_acm_instance_activate = UX_NULL;
419 parameter.ux_slave_class_cdc_acm_instance_deactivate = UX_NULL;
420 parameter.ux_slave_class_cdc_acm_parameter_change = test_cdc_instance_parameter_change;
421
422 /* Initialize the device cdc class. This class owns both interfaces starting with 0. */
423 status = ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
424 1,0, ¶meter);
425 status |= ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
426 2,0, ¶meter);
427 status |= ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
428 3,0, ¶meter);
429 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
430 if(status!=UX_SUCCESS)
431 {
432
433 printf("ERROR #%d\n", __LINE__);
434 test_control_return(1);
435 }
436 #endif
437
438 /* Initialize the simulated device controller. */
439 status = _ux_test_dcd_sim_slave_initialize();
440
441 /* Check for error. */
442 if (status != TX_SUCCESS)
443 {
444
445 printf("ERROR #%d\n", __LINE__);
446 test_control_return(1);
447 }
448
449 /* Register HCD for test */
450 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
451 if (status != UX_SUCCESS)
452 {
453
454 printf("ERROR #%d\n", __LINE__);
455 test_control_return(1);
456 }
457
458 /* Create the main host simulation thread. */
459 status = tx_thread_create(&tx_test_thread_host_simulation, "tx test host simulation", tx_test_thread_host_simulation_entry, 0,
460 stack_pointer, UX_DEMO_STACK_SIZE,
461 20, 20, 1, TX_AUTO_START);
462
463 /* Check for error. */
464 if (status != TX_SUCCESS)
465 {
466
467 printf("ERROR #%d\n", __LINE__);
468 test_control_return(1);
469 }
470
471 /* Create the main slave simulation thread. */
472 status = tx_thread_create(&tx_test_thread_slave_simulation, "tx test slave simulation", tx_test_thread_slave_simulation_entry, 0,
473 stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
474 20, 20, 1, TX_AUTO_START);
475
476 /* Check for error. */
477 if (status != TX_SUCCESS)
478 {
479
480 printf("ERROR #%d\n", __LINE__);
481 test_control_return(1);
482 }
483 }
484
tx_test_thread_host_simulation_entry(ULONG arg)485 void tx_test_thread_host_simulation_entry(ULONG arg)
486 {
487
488 UX_SLAVE_ENDPOINT *slave_interrupt_endpoint = UX_NULL;
489
490 stepinfo("\n");
491
492 /* Test connect. */
493 stepinfo(">>>>>>>>>>>>>>>> Test connect (FS)\n");
494 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
495 ux_test_breakable_sleep(100, break_on_cdc_acm_all_ready);
496 if (!(cdc_acm_host_control && cdc_acm_host_data && cdc_acm_slave))
497 {
498
499 printf("ERROR #%d: connect fail\n", __LINE__);
500 test_control_return(1);
501 }
502
503 stepinfo(">>>>>>>>>>>>>>>> Test disconnect\n");
504 ux_test_dcd_sim_slave_disconnect();
505 ux_test_hcd_sim_host_disconnect();
506
507 ux_test_breakable_sleep(100, break_on_removal);
508
509 if (cdc_acm_host_control || cdc_acm_host_data || cdc_acm_slave)
510 {
511
512 printf("ERROR #%d: disconnect fail\n", __LINE__);
513 test_control_return(1);
514 }
515
516 stepinfo(">>>>>>>>>>>>>>>> Deinitialize\n");
517
518 /* Deinitialize the class. */
519 ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
520 ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
521 ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
522
523 /* Deinitialize the device side of usbx. */
524 _ux_device_stack_uninitialize();
525
526 /* And finally the usbx system resources. */
527 _ux_system_uninitialize();
528
529 stepinfo(">>>>>>>>>>>>>>>> Dump results\n");
530
531 if (error_counter > 0)
532 {
533
534 /* Test error. */
535 printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
536 test_control_return(1);
537 }
538
539 /* Successful test. */
540 printf("SUCCESS!\n");
541 test_control_return(0);
542 }
543
tx_test_thread_slave_simulation_entry(ULONG arg)544 void tx_test_thread_slave_simulation_entry(ULONG arg)
545 {
546
547 while(1)
548 {
549
550 /* Sleep so ThreadX on Win32 will delete this thread. */
551 ux_utility_delay_ms(100);
552 }
553 }
554