echo.c
9.93 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
/*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:echo.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:00 $
***
*** Description:
*** This file contains the VUSUSB32 OTG echo application.
***
**************************************************************************
*END*********************************************************************/
#ifndef __USB_OS_MQX__
#include "types.h"
#include "otgapi.h"
#include "usb.h"
#include "devapi.h"
#include "hostapi.h"
#include "echo.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 "echo.h"
#endif
/* global variables */
_usb_device_handle dev_handle;
_usb_host_handle host_handle;
_usb_otg_handle app_otg_handle;
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 */
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;
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_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 */
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 :
*
*END*--------------------------------------------------------------------*/
void main
(
void
)
{ /* Body */
uint_8 error;
USB_OTG_INIT_STRUCT init;
uint_32 state=0;
/* 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;
/* Initialize the USB interface */
_disable_interrupts();
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 */