demo.c
16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*HEADER******************************************************************
**************************************************************************
***
*** Copyright (c) 2001-2002 ARC International.
*** All rights reserved
***
*** This software embodies materials and concepts which are
*** confidential to ARC International and is made
*** available solely pursuant to the terms of a written license
*** agreement with ARC International
***
*** $Workfile:demo.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:00 $
***
*** Description:
*** This file contains the VUSB32 OTG demo
***
**************************************************************************
*END*********************************************************************/
#ifndef __USB_OS_MQX__
#include "types.h"
#include "otgapi.h"
#include "usb.h"
#include "devapi.h"
#include "hostapi.h"
#include "demo.h"
#include "demo_d.h"
#include "test_h.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#else
#include "mqx.h"
#include "bsp.h"
#include "devapi.h"
#include "hostapi.h"
#include "demo.h"
#include "demo_d.h"
#endif
/* global variables */
_usb_device_handle dev_handle;
_usb_host_handle host_handle;
_usb_otg_handle app_otg_handle;
uchar MASS_STORAGE_DISK[TOTAL_LOGICAL_ADDRESS_BLOCKS*LENGTH_OF_EACH_LAB];
uchar BOOT_SECTOR_AREA[512] = {
/* Block 0 is the boot sector. Following is the data in the boot sector */
/* 80x86 "short: jump instruction, indicating that the disk is formatted */
0xEB,
/* 8-bit displacement */
0x3C,
/* NOP OPCode */
0x90,
/* 8-bytes for OEM identification: "ARC 4.3 " */
0x41, 0x52, 0x43, 0x20, 0x34, 0x2E, 0x33, 0x20,
/* bytes/sector: 512 bytes (0x0200) */
0x00, 0x02,
/* Sectors/allocation unit */
0x01,
/* Reserved sectors: 0x0001 */
0x01, 0x00,
/* Number of File Allocation Tables (FATs): 2 */
0x02,
/* Number of root directory entries */
0x70, 0x00,
/* Total sectors in logical volume: 720 */
USB_uint_16_low(TOTAL_LOGICAL_ADDRESS_BLOCKS),
USB_uint_16_high(TOTAL_LOGICAL_ADDRESS_BLOCKS),
/* Media descriptor byte: 0xF8: Fixed disk */
0xFD,
/* Sectors/FAT: 3 (Each FAT starts at a new sector) */
0x03, 0x00,
/* Sectors/track: 9 */
0x09, 0x00,
/* Number of heads */
0x02, 0x00,
/* Number of hidden sectors: 0 */
0x00, 0x00, 0x00, 0x00,
/* Total sectors in logical volume */
0x00, 0x00, 0x00, 0x00,
/* Physical drive number */
0x00,
/* Reserved */
0x00,
/* Extended boot signature record: 0x29 */
0x29,
/* 32-bit binary volume ID */
0x01, 0x02, 0x03, 0x04,
/* Volume label */
0x56, 0x41, 0x75, 0x74, 0x6F, 0x6D, 0x61, 0x74, 0x69, 0x6F, 0x6E,
/* Reserved */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Bootstrap */
0x33, 0xC0, 0x8E, 0xD0, 0xBC, 0x00, 0x7C, 0xFC, 0xE8, 0x45, 0x00,
/* String: \r\nNon-System disk\r\nPress any key to reboot\r\n" */
0x0D, 0x0A, 0x4E, 0x6F, 0x6E, 0x2D, 0x53, 0x79, 0x73, 0x74, 0x65,
0x6D, 0x20, 0x64, 0x69, 0x73, 0x6B, 0x0D, 0x0A, 0x50, 0x72, 0x65,
0x73, 0x73, 0x20, 0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20,
0x74, 0x6F, 0x20, 0x72, 0x65, 0x62, 0x6F, 0x6F, 0x74, 0x0D, 0x0A,
0x5E, 0xEB, 0x02, 0xCD, 0x10, 0xB4, 0x0E, 0xBB, 0x07, 0x00, 0x2E,
0xAC, 0x84, 0xC0, 0x75, 0xF3, 0x98, 0xCD, 0x16, 0xCD, 0x19, 0xEB,
0xB1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Partition descriptors */
0x00, 0x01, 0x01, 0x00, 0x01, 0x04, 0x02, 0xC8, 0x11, 0x00, 0x00,
0x00, 0x02, 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
};
uchar FAT16_SPECIAL_BYTES[3] = {
/* FAT ID: Same as Media descriptor */
0xFD, 0xFF, 0xFF
};
extern volatile APP_GLOBAL_STRUCT app_test_struct;
extern void device_main(_usb_device_handle dev_handle);
extern void host_main(_usb_host_handle host_handle);
uint_32 EVENT[20] = {0};
uint_32 event_count = 0;
volatile boolean HOST_REGISTER_SERVICES = TRUE;
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : otg_device_startup
* Returned Value : None
* Comments :
* Start OTG in device mode.
*
*END*--------------------------------------------------------------------*/
uint_8 otg_device_startup
(
void
)
{ /* Body */
uint_8 error;
uint_32 state=0;
/* Initialize the USB interface */
error = _usb_device_init(0, &dev_handle, 4);
if (error != USB_OK) {
printf("\nUSB Device Initialization failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_device_register_service(dev_handle, USB_SERVICE_EP0,
service_ep0);
if (error != USB_OK) {
printf("\nUSB Device Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_device_register_service(dev_handle, USB_SERVICE_BUS_RESET,
reset_ep0);
if (error != USB_OK) {
printf("\nUSB Device Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
_usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
if (state < B_IDLE) {
error = _usb_device_register_service(dev_handle, USB_SERVICE_SOF,
bus_sof);
if (error != USB_OK) {
printf("\nUSB Device Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
} /* Endif */
return error;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : otg_device_shutdown
* Returned Value : None
* Comments :
* Start OTG in device shutdown
*
*END*--------------------------------------------------------------------*/
uint_8 otg_device_shutdown
(
void
)
{ /* Body */
uint_8 error;
error = _usb_device_unregister_service(dev_handle, USB_SERVICE_BUS_RESET);
if (error != USB_OK) {
printf("\nUSB Service Un Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_device_unregister_service(dev_handle, USB_SERVICE_EP0);
if (error != USB_OK) {
printf("\nUSB Service un Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
/* shutdown the USB interface */
_usb_device_shutdown(dev_handle);
return error;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : otg_host_startup
* Returned Value : None
* Comments :
* Start OTG in host mode.
*
*END*--------------------------------------------------------------------*/
uint_8 otg_host_startup
(
void
)
{ /* Body */
uint_8 error;
error = _usb_host_init(0, 10, &host_handle);
if (error != USB_OK) {
printf("\nUSB Host Initialization failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_host_register_service(host_handle, USB_SERVICE_ATTACH,
app_process_attach);
if (error != USB_OK) {
printf("\nUSB Host Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_host_register_service(host_handle, USB_SERVICE_DETACH,
app_process_detach);
if (error != USB_OK) {
printf("\nUSB Host Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,
app_process_host_resume);
if (error != USB_OK) {
printf("\nUSB Host Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_host_register_service(host_handle, USB_SERVICE_STALL_PACKET,
app_process_packet_stall);
if (error != USB_OK) {
printf("\nUSB Host Service Registration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
HOST_REGISTER_SERVICES = TRUE;
return error;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : otg_host_shutdown
* Returned Value : None
* Comments :
* Shutdown the OTG in host mode.
*
*END*--------------------------------------------------------------------*/
uint_8 otg_host_shutdown
(
void
)
{ /* Body */
uint_8 error;
error = _usb_host_unregister_service(host_handle, USB_SERVICE_DETACH);
if (error != USB_OK) {
printf("\nUSB Service Unregistration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_host_unregister_service(host_handle, USB_SERVICE_ATTACH);
if (error != USB_OK) {
printf("\nUSB Service Unregistration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
error = _usb_host_unregister_service(host_handle, USB_SERVICE_HOST_RESUME);
if (error != USB_OK) {
printf("\nUSB Service Unregistration failed. Error: %x", error);
fflush(stdout);
} /* Endif */
_usb_host_shutdown(host_handle);
return error;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : otg_service
* Returned Value : None
* Comments :
* Called upon OTG service state change.
*
*END*--------------------------------------------------------------------*/
void otg_service
(
/* [IN] Handle of the USB device */
_usb_otg_handle handle,
/* [IN] service type */
uint_32 type
)
{ /* Body */
uint_8 error;
uint_32 state;
EVENT[event_count++] = type;
if (event_count>19) {
event_count = 0;
} /* Endif */
switch (type) {
case USB_OTG_HOST_UP:
error = otg_host_startup();
if (error == USB_OK) {
_usb_otg_set_status(handle, USB_OTG_HOST_ACTIVE, TRUE);
_usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
if (state < B_IDLE) {
_usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_DROP, FALSE);
_usb_otg_set_status(app_otg_handle, USB_OTG_A_BUS_REQ, TRUE);
} /* Endif */
} /* Endif */
break;
case USB_OTG_DEVICE_UP:
error = otg_device_startup();
if (error== USB_OK) {
_usb_otg_set_status(handle, USB_OTG_DEVICE_ACTIVE, TRUE);
} /* Endif */
break;
case USB_OTG_HOST_DOWN:
error = otg_host_shutdown();
if (error == USB_OK) {
_usb_otg_set_status(handle, USB_OTG_HOST_ACTIVE, FALSE);
} /* Endif */
app_process_detach(host_handle, 0);
break;
case USB_OTG_DEVICE_DOWN:
error = otg_device_shutdown();
if (error == USB_OK) {
_usb_otg_set_status(handle, USB_OTG_DEVICE_ACTIVE, FALSE);
} /* Endif */
reset_ep0(dev_handle, FALSE, 0, NULL, 0);
break;
case USB_OTG_SRP_ACTIVE:
break;
case USB_OTG_SRP_FAIL:
break;
case USB_OTG_OVER_CURRENT:
break;
default:
break;
} /* Endswitch */
return;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : main
* Returned Value : None
* Comments :
* First function called. Initialises the USB and registers Chapter 9
* callback functions.
*
*END*--------------------------------------------------------------------*/
void main
(
void
)
{ /* Body */
uint_8 error;
USB_OTG_INIT_STRUCT init;
volatile uint_32 i = 0;
uint_32 state=0;
char c;
/* default configuration: A will be host B will be peripheral */
init.A_BUS_DROP = FALSE;
init.A_BUS_REQ = TRUE;
init.B_BUS_REQ = FALSE;
init.SERVICE = otg_service;
app_test_struct.DO_HNP = FALSE;
app_test_struct.DO_SRP = FALSE;
app_test_struct.DO_NORMAL = TRUE;
_disable_interrupts();
memset(MASS_STORAGE_DISK, 0,
(TOTAL_LOGICAL_ADDRESS_BLOCKS*LENGTH_OF_EACH_LAB));
/* Format the "disk" */
memcpy(MASS_STORAGE_DISK, BOOT_SECTOR_AREA, 512);
memcpy(MASS_STORAGE_DISK + 512, FAT16_SPECIAL_BYTES, 3);
memcpy(MASS_STORAGE_DISK + 512*4, FAT16_SPECIAL_BYTES, 3);
printf("\nDo you want to start SRP? (y/n):");
c = getchar();
if ((c == 'y') || (c == 'Y')) {
init.A_BUS_REQ = FALSE; /* do not start A; wait for B to do SRP */
init.B_BUS_REQ = TRUE; /* do SRP */
app_test_struct.DO_HNP = FALSE;
app_test_struct.DO_SRP = TRUE;
app_test_struct.DO_NORMAL = FALSE;
} /* Endif */
#ifdef HOSTLINK_IO
c = getchar();
#endif
/* Initialize the USB interface */
error = _usb_otg_init(0, &init, &app_otg_handle);
if (error != USB_OK) {
printf("\nUSB OTG Initialization failed. Error: %x", error);
fflush(stdout);
} /* Endif */
_enable_interrupts();
while (TRUE) {
_usb_otg_get_status(app_otg_handle, USB_OTG_STATE, &state);
if ((state == A_HOST) || (state == B_HOST)) {
host_main(host_handle);
} /* Endif */
if ((state == A_PERIPHERAL) || (state == B_PERIPHERAL)) {
device_main(dev_handle);
} /* Endif */
} /* Endwhile */
} /* Endbody */
/* EOF */