1+ /* This test is designed to test the ux_utility_time_elapsed macro.... */
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 "ux_host_stack.h"
10+ #include "ux_device_stack.h"
11+
12+ #include "ux_device_class_cdc_acm.h"
13+ #include "ux_host_class_cdc_acm.h"
14+
15+ #include "ux_host_class_dpump.h"
16+ #include "ux_device_class_dpump.h"
17+
18+ #include "ux_host_class_hid.h"
19+ #include "ux_device_class_hid.h"
20+
21+ #include "ux_host_class_storage.h"
22+ #include "ux_device_class_storage.h"
23+
24+ #include "ux_test_dcd_sim_slave.h"
25+ #include "ux_test_hcd_sim_host.h"
26+ #include "ux_test_utility_sim.h"
27+
28+
29+ /* Define USBX test constants. */
30+
31+ #define UX_TEST_STACK_SIZE 4096
32+ #define UX_TEST_BUFFER_SIZE 2048
33+ #define UX_TEST_RUN 1
34+ #define UX_TEST_MEMORY_SIZE (64*1024)
35+
36+ #define LSB (x ) ( (x) & 0x00ff)
37+ #define MSB (x ) (((x) & 0xff00) >> 8)
38+
39+ /* Configuration descriptor 9 bytes */
40+ #define CFG_DESC (wTotalLength , bNumInterfaces , bConfigurationValue )\
41+ /* Configuration 1 descriptor 9 bytes */ \
42+ 0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
43+ (bNumInterfaces), (bConfigurationValue), 0x00,\
44+ 0x40, 0x00,
45+ #define CFG_DESC_LEN 9
46+
47+ /* DPUMP interface descriptors. */
48+ #define DPUMP_IFC_DESC (ifc , alt , nb_ep ) \
49+ /* Interface descriptor */ \
50+ 0x09, 0x04, (ifc), (alt), (nb_ep), 0x99, 0x99, 0x99, 0x00,
51+
52+ #define DPUMP_IFC_EP_DESC (epaddr , eptype , epsize ) \
53+ /* Endpoint descriptor */ \
54+ 0x07, 0x05, (epaddr), (eptype), LSB(epsize), MSB(epsize), 0x01,
55+
56+ #define DPUMP_IFC_DESC_ALL_LEN (nb_ep ) (9 + (nb_ep) * 7)
57+
58+ #define CFG_DESC_ALL_LEN (CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN(4))
59+
60+ #define CFG_DESC_ALL \
61+ CFG_DESC(CFG_DESC_ALL_LEN, 1, 1)\
62+ DPUMP_IFC_DESC(0, 0, 4)\
63+ DPUMP_IFC_EP_DESC(0x81, 2, 64)\
64+ DPUMP_IFC_EP_DESC(0x02, 2, 64)\
65+ DPUMP_IFC_EP_DESC(0x83, 1, 64)\
66+ DPUMP_IFC_EP_DESC(0x84, 3, 64)\
67+
68+ /* Define the counters used in the test application... */
69+
70+ static ULONG thread_0_counter ;
71+ static ULONG thread_1_counter ;
72+ static ULONG error_counter ;
73+
74+ static UCHAR error_callback_ignore = UX_FALSE ;
75+ static ULONG error_callback_counter ;
76+
77+ static UCHAR buffer [UX_TEST_BUFFER_SIZE ];
78+
79+ static TX_TIMER test_timer ;
80+
81+ /* Define USBX test global variables. */
82+
83+ static UX_HOST_CLASS * class_driver ;
84+ static UX_HOST_CLASS_DPUMP * dpump ;
85+ static UX_SLAVE_CLASS_DPUMP * dpump_slave = UX_NULL ;
86+
87+ static UCHAR device_framework_full_speed [] = {
88+
89+ /* Device descriptor 18 bytes */
90+ 0x12 , 0x01 , 0x10 , 0x01 , 0x00 , 0x00 , 0x00 , 0x08 ,
91+ 0xec , 0x08 , 0x10 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
92+ 0x00 , 0x01 ,
93+
94+ CFG_DESC_ALL
95+ };
96+ #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
97+
98+ static UCHAR device_framework_high_speed [] = {
99+
100+ /* Device descriptor */
101+ 0x12 , 0x01 , 0x00 , 0x02 , 0x00 , 0x00 , 0x00 , 0x40 ,
102+ 0x0a , 0x07 , 0x25 , 0x40 , 0x01 , 0x00 , 0x01 , 0x02 ,
103+ 0x03 , 0x01 ,
104+
105+ /* Device qualifier descriptor */
106+ 0x0a , 0x06 , 0x00 , 0x02 , 0x00 , 0x00 , 0x00 , 0x40 ,
107+ 0x01 , 0x00 ,
108+
109+ CFG_DESC_ALL
110+ };
111+ #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
112+
113+ /* String Device Framework :
114+ Byte 0 and 1 : Word containing the language ID : 0x0904 for US
115+ Byte 2 : Byte containing the index of the descriptor
116+ Byte 3 : Byte containing the length of the descriptor string
117+ */
118+
119+ #define NUM_TEST_VALUES 2
120+ #define TEST_VALUES_LENGTH 3
121+
122+ static UINT test_values [NUM_TEST_VALUES ][TEST_VALUES_LENGTH ] =
123+ {
124+ {0xFU , 0x1U , 0xFFFFFFF2U },
125+ {0xFFFFFFF0 , 0x1U , 0x11U }
126+ };
127+
128+ static UCHAR string_framework [] = {
129+
130+ /* Manufacturer string descriptor : Index 1 */
131+ 0x09 , 0x04 , 0x01 , 0x0c ,
132+ 0x45 , 0x78 , 0x70 , 0x72 ,0x65 , 0x73 , 0x20 , 0x4c ,
133+ 0x6f , 0x67 , 0x69 , 0x63 ,
134+
135+ /* Product string descriptor : Index 2 */
136+ 0x09 , 0x04 , 0x02 , 0x0c ,
137+ 0x44 , 0x61 , 0x74 , 0x61 , 0x50 , 0x75 , 0x6d , 0x70 ,
138+ 0x44 , 0x65 , 0x6d , 0x6f ,
139+
140+ /* Serial Number string descriptor : Index 3 */
141+ 0x09 , 0x04 , 0x03 , 0x04 ,
142+ 0x30 , 0x30 , 0x30 , 0x31
143+ };
144+ #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
145+
146+ /* Multiple languages are supported on the device, to add
147+ a language besides English, the unicode language code must
148+ be appended to the language_id_framework array and the length
149+ adjusted accordingly. */
150+ static UCHAR language_id_framework [] = {
151+
152+ /* English. */
153+ 0x09 , 0x04
154+ };
155+ #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
156+
157+ /* Define prototypes for external Host Controller's (HCDs), classes and clients. */
158+
159+ static VOID ux_test_instance_activate (VOID * dpump_instance );
160+ static VOID ux_test_instance_deactivate (VOID * dpump_instance );
161+
162+ UINT _ux_host_class_dpump_entry (UX_HOST_CLASS_COMMAND * command );
163+ UINT ux_hcd_sim_initialize (UX_HCD * hcd );
164+ UINT _ux_host_class_dpump_write (UX_HOST_CLASS_DPUMP * dpump , UCHAR * data_pointer ,
165+ ULONG requested_length , ULONG * actual_length );
166+ UINT _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP * dpump , UCHAR * data_pointer ,
167+ ULONG requested_length , ULONG * actual_length );
168+
169+ static TX_THREAD ux_test_thread_simulation_0 ;
170+ static TX_THREAD ux_test_thread_simulation_1 ;
171+ static void ux_test_thread_simulation_0_entry (ULONG );
172+ static void ux_test_thread_simulation_1_entry (ULONG );
173+
174+
175+ /* Define the ISR dispatch. */
176+
177+ extern VOID (* test_isr_dispatch )(void );
178+
179+
180+ /* Prototype for test control return. */
181+
182+ void test_control_return (UINT status );
183+
184+ /* Simulator actions. */
185+
186+ static UX_TEST_HCD_SIM_ACTION endpoint0x83_create_del_skip [] = {
187+ /* function, request to match,
188+ port action, port status,
189+ request action, request EP, request data, request actual length, request status,
190+ status, additional callback,
191+ no_return */
192+ { UX_HCD_CREATE_ENDPOINT , NULL ,
193+ UX_FALSE , 0 ,
194+ UX_TEST_MATCH_EP , 0x83 , UX_NULL , 0 , 0 ,
195+ UX_SUCCESS },
196+ { UX_HCD_CREATE_ENDPOINT , NULL ,
197+ UX_FALSE , 0 ,
198+ UX_TEST_MATCH_EP , 0x83 , UX_NULL , 0 , 0 ,
199+ UX_SUCCESS },
200+ { 0 }
201+ };
202+
203+ /* Define the ISR dispatch routine. */
204+
205+ static void test_isr (void )
206+ {
207+
208+ /* For further expansion of interrupt-level testing. */
209+ }
210+
211+
212+ static VOID error_callback (UINT system_level , UINT system_context , UINT error_code )
213+ {
214+
215+ error_callback_counter ++ ;
216+
217+ if (!error_callback_ignore )
218+ {
219+ {
220+ /* Failed test. */
221+ printf ("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n" , __LINE__ , system_level , system_context , error_code );
222+ // test_control_return(1);
223+ }
224+ }
225+ }
226+
227+ static UINT break_on_dpump_ready (VOID )
228+ {
229+
230+ UINT status ;
231+ UX_HOST_CLASS * class ;
232+
233+ /* Find the main data pump container. */
234+ status = ux_host_stack_class_get (_ux_system_host_class_dpump_name , & class );
235+ if (status != UX_SUCCESS )
236+ /* Do not break. */
237+ return UX_SUCCESS ;
238+
239+ /* Find the instance. */
240+ status = ux_host_stack_class_instance_get (class , 0 , (VOID * * ) & dpump );
241+ if (status != UX_SUCCESS )
242+ /* Do not break. */
243+ return UX_SUCCESS ;
244+
245+ if (dpump -> ux_host_class_dpump_state != UX_HOST_CLASS_INSTANCE_LIVE )
246+ /* Do not break. */
247+ return UX_SUCCESS ;
248+
249+ return 1 ;
250+ }
251+
252+ static UINT break_on_removal (VOID )
253+ {
254+
255+ UINT status ;
256+ UX_DEVICE * device ;
257+
258+ status = ux_host_stack_device_get (0 , & device );
259+ if (status == UX_SUCCESS )
260+ /* Do not break. */
261+ return UX_SUCCESS ;
262+
263+ return 1 ;
264+ }
265+
266+
267+ static UINT test_ux_device_class_dpump_entry (UX_SLAVE_CLASS_COMMAND * command )
268+ {
269+ switch (command -> ux_slave_class_command_request )
270+ {
271+ case UX_SLAVE_CLASS_COMMAND_INITIALIZE :
272+ case UX_SLAVE_CLASS_COMMAND_QUERY :
273+ case UX_SLAVE_CLASS_COMMAND_CHANGE :
274+ return UX_SUCCESS ;
275+
276+ default :
277+ return UX_NO_CLASS_MATCH ;
278+ }
279+ }
280+
281+ static UINT test_ux_host_class_dpump_entry (UX_HOST_CLASS_COMMAND * command )
282+ {
283+ switch (command -> ux_host_class_command_request )
284+ {
285+ case UX_HOST_CLASS_COMMAND_QUERY :
286+ default :
287+ return _ux_host_class_dpump_entry (command );
288+ }
289+ }
290+
291+ /* Define what the initial system looks like. */
292+
293+ #ifdef CTEST
294+ void test_application_define (void * first_unused_memory )
295+ #else
296+ void usbx_ux_utility_timer_test_application_define (void * first_unused_memory )
297+ #endif
298+ {
299+
300+ UINT status ;
301+ CHAR * stack_pointer ;
302+ CHAR * memory_pointer ;
303+ CHAR * rpool_start ;
304+ CHAR * cpool_start ;
305+ const CHAR flags [] = {
306+ UX_REGULAR_MEMORY , UX_CACHE_SAFE_MEMORY , 0xFF
307+ };
308+ const CHAR expect_error [] = {
309+ UX_FALSE , UX_FALSE , UX_TRUE
310+ };
311+
312+ /* Inform user. */
313+ printf ("Running ux_utility_time_elapsed... Test................................... " );
314+
315+ /* Initialize the free memory pointer. */
316+ stack_pointer = (CHAR * ) first_unused_memory ;
317+ memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2 );
318+
319+ rpool_start = memory_pointer ;
320+ cpool_start = memory_pointer + UX_TEST_MEMORY_SIZE ;
321+
322+ /* Initialize USBX Memory. */
323+ status = ux_system_initialize (rpool_start , UX_TEST_MEMORY_SIZE , cpool_start , UX_TEST_MEMORY_SIZE );
324+
325+ /* Check for error. */
326+ if (status != UX_SUCCESS )
327+ {
328+
329+ printf ("ERROR #%d\n" , __LINE__ );
330+ test_control_return (1 );
331+ }
332+
333+ /* Register the error callback. */
334+ _ux_utility_error_callback_register (error_callback );
335+
336+ /* Create the simulation thread. */
337+ status = tx_thread_create (& ux_test_thread_simulation_0 , "test simulation" , ux_test_thread_simulation_0_entry , 0 ,
338+ stack_pointer , UX_TEST_STACK_SIZE ,
339+ 20 , 20 , 1 , TX_AUTO_START );
340+
341+ /* Check for error. */
342+ if (status != TX_SUCCESS )
343+ {
344+
345+ printf ("ERROR #%d\n" , __LINE__ );
346+ test_control_return (1 );
347+ }
348+ }
349+
350+ static void ux_test_thread_simulation_0_entry (ULONG arg )
351+ {
352+
353+
354+ for (UINT i = 0 ; i < NUM_TEST_VALUES ; ++ i )
355+ {
356+ if (ux_utility_time_elapsed (test_values [i ][0 ], test_values [i ][1 ]) != test_values [i ][2 ])
357+ {
358+ printf ("ERROR #%d: time elapsed incorrect\n" , __LINE__ );
359+ test_control_return (1 );
360+ }
361+ }
362+
363+ /* Successful test. */
364+ printf ("SUCCESS!\n" );
365+ test_control_return (0 );
366+ }
0 commit comments