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_storage.h"
12 #include "ux_device_stack.h"
13 #include "ux_host_stack.h"
14 #include "ux_host_class_storage.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 /* Define constants. */
21 #define UX_DEMO_STACK_SIZE 2048
22 #define UX_DEMO_MEMORY_SIZE (256*1024)
23 #define UX_DEMO_BUFFER_SIZE 2048
24
25 #define UX_RAM_DISK_SIZE (200 * 1024)
26 #define UX_RAM_DISK_LAST_LBA ((UX_RAM_DISK_SIZE / 512) -1)
27
28 /* Define local/extern function prototypes. */
29
30 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
31 void _fx_ram_drive_format(ULONG disk_size, UINT sector_size, UINT sectors_per_cluster,
32 UINT fat_entries, UINT root_directory_entries);
33
34 static void demo_thread_entry(ULONG);
35 static TX_THREAD tx_demo_thread_host_simulation;
36 static TX_THREAD tx_demo_thread_slave_simulation;
37 static void tx_demo_thread_host_simulation_entry(ULONG);
38
39 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
40 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
41 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status);
42 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status);
43
44 /* Define global data structures. */
45
46 static UCHAR usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
47 static UCHAR buffer[UX_DEMO_BUFFER_SIZE];
48
49 static ULONG error_counter;
50
51 static TX_THREAD demo_thread;
52
53 static UX_HOST_CLASS_STORAGE *storage;
54 static UX_SLAVE_CLASS_STORAGE_PARAMETER global_storage_parameter;
55
56 static FX_MEDIA ram_disk_media1;
57 static FX_MEDIA ram_disk_media2;
58 static CHAR ram_disk_buffer1[512];
59 static CHAR ram_disk_buffer2[512];
60 static CHAR ram_disk_memory1[UX_RAM_DISK_SIZE];
61 static CHAR ram_disk_memory2[UX_RAM_DISK_SIZE];
62 static CHAR *ram_disk_memory[] =
63 {
64 ram_disk_memory1, ram_disk_memory2
65 };
66 static UINT ram_disk_status = UX_SUCCESS;
67 static ULONG ram_disk_media_status = 0;
68 static CHAR ram_disk_status_sent = 0;
69
70 static CHAR ram_disk_flush = 0;
71 static UINT ram_disk_flush_status = UX_SUCCESS;
72
73 static ULONG set_cfg_counter;
74
75 static ULONG rsc_mem_alloc_cnt_on_set_cfg;
76 static ULONG rsc_sem_on_set_cfg;
77 static ULONG rsc_sem_get_on_set_cfg;
78 static ULONG rsc_mutex_on_set_cfg;
79
80 static ULONG rsc_enum_sem_usage;
81 static ULONG rsc_enum_sem_get_count;
82 static ULONG rsc_enum_mutex_usage;
83 static ULONG rsc_enum_mem_alloc_count;
84
85 static ULONG rsc_storage_sem_usage;
86 static ULONG rsc_storage_sem_get_count;
87 static ULONG rsc_storage_mutex_usage;
88 static ULONG rsc_storage_mem_alloc_count;
89
90 static ULONG interaction_count;
91
92 static UCHAR error_callback_ignore = UX_TRUE;
93 static ULONG error_callback_counter;
94
95 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 50
96 static UCHAR device_framework_full_speed[] = {
97
98 /* Device descriptor */
99 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
100 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
101 0x03, 0x01,
102
103 /* Configuration descriptor */
104 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
105 0x32,
106
107 /* Interface descriptor */
108 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50,
109 0x00,
110
111 /* Endpoint descriptor (Bulk Out) */
112 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
113
114 /* Endpoint descriptor (Bulk In) */
115 0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00,
116
117 };
118
119
120 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 60
121 static UCHAR device_framework_high_speed[] = {
122
123 /* Device descriptor */
124 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
125 0x81, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x02,
126 0x03, 0x01,
127
128 /* Device qualifier descriptor */
129 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
130 0x01, 0x00,
131
132 /* Configuration descriptor */
133 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
134 0x32,
135
136 /* Interface descriptor */
137 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50,
138 0x00,
139
140 /* Endpoint descriptor (Bulk Out) */
141 0x07, 0x05, 0x02, 0x02, 0x00, 0x01, 0x00,
142
143 /* Endpoint descriptor (Bulk In) */
144 0x07, 0x05, 0x81, 0x02, 0x00, 0x01, 0x00,
145
146 };
147
148
149 /* String Device Framework :
150 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
151 Byte 2 : Byte containing the index of the descriptor
152 Byte 3 : Byte containing the length of the descriptor string
153 */
154
155 #define STRING_FRAMEWORK_LENGTH 38
156 static UCHAR string_framework[] = {
157
158 /* Manufacturer string descriptor : Index 1 */
159 0x09, 0x04, 0x01, 0x0c,
160 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
161 0x6f, 0x67, 0x69, 0x63,
162
163 /* Product string descriptor : Index 2 */
164 0x09, 0x04, 0x02, 0x0a,
165 0x46, 0x6c, 0x61, 0x73, 0x68, 0x20, 0x44, 0x69,
166 0x73, 0x6b,
167
168 /* Serial Number string descriptor : Index 3 */
169 0x09, 0x04, 0x03, 0x04,
170 0x30, 0x30, 0x30, 0x31
171 };
172
173
174 /* Multiple languages are supported on the device, to add
175 a language besides english, the unicode language code must
176 be appended to the language_id_framework array and the length
177 adjusted accordingly. */
178 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
179 static UCHAR language_id_framework[] = {
180
181 /* English. */
182 0x09, 0x04
183 };
184
185
186 /* Define the ISR dispatch. */
187
188 extern VOID (*test_isr_dispatch)(void);
189
190
191 /* Prototype for test control return. */
192
193 void test_control_return(UINT status);
194
195
196 /* Define the ISR dispatch routine. */
197
test_isr(void)198 static void test_isr(void)
199 {
200
201 /* For further expansion of interrupt-level testing. */
202 }
203
204
error_callback(UINT system_level,UINT system_context,UINT error_code)205 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
206 {
207
208 error_callback_counter ++;
209
210 if (!error_callback_ignore)
211 {
212 {
213 /* Failed test. */
214 printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
215 test_control_return(1);
216 }
217 }
218 }
219
host_storage_instance_get(ULONG timeout_x10ms)220 static UINT host_storage_instance_get(ULONG timeout_x10ms)
221 {
222
223 UINT status;
224 UX_HOST_CLASS *class;
225
226
227 /* Find the main storage container */
228 status = ux_host_stack_class_get(_ux_system_host_class_storage_name, &class);
229 if (status != UX_SUCCESS)
230 return(status);
231
232 /* Get storage instance, wait it to be live and media attached. */
233 do
234 {
235 if (timeout_x10ms)
236 {
237 ux_utility_delay_ms(10);
238 if (timeout_x10ms != 0xFFFFFFFF)
239 timeout_x10ms --;
240 }
241
242 status = ux_host_stack_class_instance_get(class, 0, (void **) &storage);
243 if (status == UX_SUCCESS)
244 {
245 if (storage -> ux_host_class_storage_state == UX_HOST_CLASS_INSTANCE_LIVE &&
246 class -> ux_host_class_media != UX_NULL)
247 return(UX_SUCCESS);
248 }
249
250 } while(timeout_x10ms > 0);
251
252 return(UX_ERROR);
253 }
254
sleep_break_on_error(VOID)255 static UINT sleep_break_on_error(VOID)
256 {
257
258 if (error_callback_counter >= 3)
259 return error_callback_counter;
260
261 return UX_SUCCESS;
262 }
263
264
265 /* Define what the initial system looks like. */
266
267 #ifdef CTEST
test_application_define(void * first_unused_memory)268 void test_application_define(void *first_unused_memory)
269 #else
270 void usbx_ux_host_class_storage_request_sense_test_application_define(void *first_unused_memory)
271 #endif
272 {
273
274 UINT status;
275 CHAR * stack_pointer;
276 CHAR * memory_pointer;
277
278
279 /* Inform user. */
280 printf("Running ux_host_class_storage_request_sense Test.................... ");
281 stepinfo("\n");
282
283 /* Initialize the free memory pointer */
284 stack_pointer = (CHAR *) usbx_memory;
285 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
286
287 /* Initialize USBX. Memory */
288 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
289
290 /* Check for error. */
291 if (status != UX_SUCCESS)
292 {
293
294 printf("ERROR #%d\n", __LINE__);
295 test_control_return(1);
296 }
297
298 /* Register the error callback. */
299 _ux_utility_error_callback_register(error_callback);
300
301 /* Reset ram disks memory. */
302 ux_utility_memory_set(ram_disk_memory1, 0, UX_RAM_DISK_SIZE);
303 ux_utility_memory_set(ram_disk_memory2, 0, UX_RAM_DISK_SIZE);
304
305 /* Initialize FileX. */
306 fx_system_initialize();
307
308 /* Change the ram drive values. */
309 fx_media_format(&ram_disk_media1, _fx_ram_driver, ram_disk_memory1, ram_disk_buffer1, 512, "RAM DISK1", 2, 512, 0, UX_RAM_DISK_SIZE/512, 512, 4, 1, 1);
310 fx_media_format(&ram_disk_media2, _fx_ram_driver, ram_disk_memory2, ram_disk_buffer2, 512, "RAM DISK2", 2, 512, 0, UX_RAM_DISK_SIZE/512, 512, 4, 1, 1);
311
312 /* The code below is required for installing the device portion of USBX.
313 In this demo, DFU is possible and we have a call back for state change. */
314 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
315 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
316 string_framework, STRING_FRAMEWORK_LENGTH,
317 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
318 if(status!=UX_SUCCESS)
319 {
320
321 printf("ERROR #%d\n", __LINE__);
322 test_control_return(1);
323 }
324
325 /* Store the number of LUN in this device storage instance. */
326 global_storage_parameter.ux_slave_class_storage_parameter_number_lun = 1;
327
328 /* Initialize the storage class parameters for reading/writing to the first Flash Disk. */
329 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
330 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length = 512;
331 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_type = 0;
332 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_removable_flag = 0x80;
333 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read = demo_thread_media_read;
334 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_write = demo_thread_media_write;
335 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_status = demo_thread_media_status;
336 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_flush = demo_thread_media_flush;
337
338 /* Initialize the storage class parameters for reading/writing to the second Flash Disk. */
339 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
340 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_block_length = 512;
341 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_type = 0;
342 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_removable_flag = 0x80;
343 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read = demo_thread_media_read;
344 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_write = demo_thread_media_write;
345 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_status = demo_thread_media_status;
346 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_flush = demo_thread_media_flush;
347
348 /* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
349 status = ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
350 1, 0, (VOID *)&global_storage_parameter);
351 if(status!=UX_SUCCESS)
352 {
353
354 printf("ERROR #%d\n", __LINE__);
355 test_control_return(1);
356 }
357
358 /* Initialize the simulated device controller. */
359 // status = _ux_test_dcd_sim_slave_initialize();
360 status = ux_dcd_sim_slave_initialize();
361
362 /* Check for error. */
363 if (status != UX_SUCCESS)
364 {
365
366 printf("ERROR #%d\n", __LINE__);
367 test_control_return(1);
368 }
369
370 /* The code below is required for installing the host portion of USBX */
371 status = ux_host_stack_initialize(UX_NULL);
372 if (status != UX_SUCCESS)
373 {
374
375 printf("ERROR #%d\n", __LINE__);
376 test_control_return(1);
377 }
378
379 /* Register storage class. */
380 status = ux_host_stack_class_register(_ux_system_host_class_storage_name, ux_host_class_storage_entry);
381 if (status != UX_SUCCESS)
382 {
383
384 printf("ERROR #%d\n", __LINE__);
385 test_control_return(1);
386 }
387
388 /* Register all the USB host controllers available in this system */
389 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
390 // status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
391
392 /* Check for error. */
393 if (status != UX_SUCCESS)
394 {
395
396 printf("ERROR #%d\n", __LINE__);
397 test_control_return(1);
398 }
399
400 /* Create the main host simulation thread. */
401 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
402 stack_pointer, UX_DEMO_STACK_SIZE,
403 20, 20, 1, TX_AUTO_START);
404
405 /* Check for error. */
406 if (status != TX_SUCCESS)
407 {
408
409 printf("ERROR #%d\n", __LINE__);
410 test_control_return(1);
411 }
412
413 }
414
storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA * storage_media,ULONG status,ULONG timeout)415 static UINT storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA *storage_media, ULONG status, ULONG timeout)
416 {
417
418 while(1)
419 {
420 #if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
421 if (storage_media->ux_host_class_storage_media_status == status)
422 return UX_SUCCESS;
423 #else
424 if ((status == UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED &&
425 storage_media->ux_host_class_storage_media_storage != UX_NULL) ||
426 (status == UX_HOST_CLASS_STORAGE_MEDIA_UNMOUNTED &&
427 storage_media->ux_host_class_storage_media_storage == UX_NULL))
428 return(UX_SUCCESS);
429 #endif
430 if (timeout == 0)
431 break;
432 if (timeout != 0xFFFFFFFF)
433 timeout --;
434 _ux_utility_delay_ms(10);
435 }
436 return UX_ERROR;
437 }
438
439
tx_demo_thread_host_simulation_entry(ULONG arg)440 static void tx_demo_thread_host_simulation_entry(ULONG arg)
441 {
442
443 UINT status;
444 UX_HOST_CLASS *class;
445 UX_HOST_CLASS_STORAGE_MEDIA *storage_media;
446 UX_ENDPOINT *control_endpoint;
447 UX_TRANSFER *transfer_request;
448 UX_DEVICE *device;
449
450
451 /* Find the storage class. */
452 status = host_storage_instance_get(100);
453 if (status != UX_SUCCESS)
454 {
455 printf("ERROR #%d\n", __LINE__);
456 test_control_return(1);
457 }
458
459 status = ux_host_stack_device_get(0, &device);
460 if (status != UX_SUCCESS)
461 {
462
463 printf("ERROR #%d: device_get fail\n", __LINE__);
464 test_control_return(1);
465 }
466 control_endpoint = &device->ux_device_control_endpoint;
467 transfer_request = &control_endpoint->ux_endpoint_transfer_request;
468
469 /* Wait enough time for media mounting. */
470 _ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY);
471
472 class = storage->ux_host_class_storage_class;
473 storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *)class->ux_host_class_media;
474
475 /* Confirm media enum done. */
476 status = storage_media_status_wait(storage_media, UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED, 100);
477 if (status != UX_SUCCESS)
478 {
479 printf("ERROR #%d\n", __LINE__);
480 test_control_return(1);
481 }
482
483 /* Pause the class driver thread. */
484 _ux_utility_thread_suspend(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
485
486 stepinfo(">>>>>>>>>>>>>>> ux_host_class_storage_request_sense - UX_SUCCESS\n");
487 status = _ux_host_class_storage_request_sense(storage);
488 if (status != UX_SUCCESS)
489 {
490 printf("ERROR #%d, code 0x%x\n", __LINE__, status);
491 test_control_return(1);
492 }
493
494 stepinfo(">>>>>>>>>>>>>>> ux_host_class_storage_request_sense - UX_MEMORY_INSUFFICIENT\n");
495 ux_test_utility_sim_mem_alloc_fail_all_start();
496 status = _ux_host_class_storage_request_sense(storage);
497 if (status != UX_MEMORY_INSUFFICIENT)
498 {
499 printf("ERROR #%d, code 0x%x\n", __LINE__, status);
500 test_control_return(1);
501 }
502 ux_test_utility_sim_mem_alloc_fail_all_stop();
503
504 /* Wait a while so the thread goes. */
505 _ux_utility_delay_ms(10);
506
507 /* Finally disconnect the device. */
508 ux_device_stack_disconnect();
509
510 /* And deinitialize the class. */
511 status = ux_device_stack_class_unregister(_ux_system_slave_class_storage_name, ux_device_class_storage_entry);
512
513 /* Deinitialize the device side of usbx. */
514 _ux_device_stack_uninitialize();
515
516 /* And finally the usbx system resources. */
517 _ux_system_uninitialize();
518
519 /* Successful test. */
520 printf("SUCCESS!\n");
521 test_control_return(0);
522 }
523
524
demo_thread_media_status(VOID * storage,ULONG lun,ULONG media_id,ULONG * media_status)525 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
526 {
527
528 UINT status = ram_disk_status;
529
530
531 (void)storage;
532 (void)media_id;
533
534 if (media_status)
535 *media_status = ram_disk_media_status;
536
537 /* If there is attention, it must be changed to ready after reported. */
538 if (ram_disk_media_status == (UX_SLAVE_CLASS_STORAGE_SENSE_KEY_UNIT_ATTENTION | (0x28 << 8)))
539 {
540 ram_disk_status = UX_SUCCESS;
541 ram_disk_media_status = 0;
542 }
543 else
544 ram_disk_status_sent = UX_TRUE;
545
546 return status;
547 }
548
demo_thread_media_read(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)549 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
550 {
551 (void)storage;
552 (void)media_status;
553
554 if (lun > 1)
555 return UX_ERROR;
556
557 ux_utility_memory_copy(data_pointer, &ram_disk_memory[lun][lba * 512], number_blocks * 512);
558
559 return UX_SUCCESS;
560 }
561
demo_thread_media_write(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)562 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
563 {
564 (void)storage;
565 (void)media_status;
566
567 if (lun > 1)
568 return UX_ERROR;
569
570 ux_utility_memory_copy(&ram_disk_memory[lun][lba * 512], data_pointer, number_blocks * 512);
571
572 return UX_SUCCESS;
573 }
574
demo_thread_media_flush(VOID * storage,ULONG lun,ULONG number_blocks,ULONG lba,ULONG * media_status)575 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status)
576 {
577 (void)storage;
578 (void)number_blocks;
579 (void)lba;
580 (void)media_status;
581
582 if (lun > 1)
583 return UX_ERROR;
584
585 ram_disk_flush = UX_TRUE;
586 return ram_disk_flush_status;
587 }