| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Host.c
Go to the documentation of this file.
1 /*
2 * @brief USB Host definitions for the LPC microcontrollers
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #define __INCLUDE_FROM_USB_DRIVER
33 #include "USBMode.h"
34
35 #if defined(USB_CAN_BE_HOST)
36
37 #define __INCLUDE_FROM_HOST_C
38 #include "Host.h"
39
40 //static uint8_t CurrentHostID = 0;
41 uint8_t USB_Host_ControlPipeSize[MAX_USB_CORE];
42
43 void USB_Host_SetDeviceSpeed(uint8_t hostid, HCD_USB_SPEED speed);
44
45 HCD_USB_SPEED USB_Host_GetDeviceSpeed(uint8_t hostid);
46
47 void USB_Host_ProcessNextHostState(uint8_t corenum)
48 {
49 uint8_t ErrorCode = HOST_ENUMERROR_NoError;
50 uint8_t SubErrorCode = HOST_ENUMERROR_NoError;
51
52 static uint16_t WaitMSRemaining;
53 static uint8_t PostWaitState;
54
55 switch (USB_HostState[corenum]) {
56 case HOST_STATE_WaitForDevice:
57 if (WaitMSRemaining) {
58 if ((SubErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful) {
59 USB_HostState[corenum] = PostWaitState;
60 ErrorCode = HOST_ENUMERROR_WaitStage;
61 break;
62 }
63
64 if (!(--WaitMSRemaining)) {
65 USB_HostState[corenum] = PostWaitState;
66 }
67 }
68 break;
69
70 case HOST_STATE_Powered:
71 WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
72
73 USB_HostState[corenum] = HOST_STATE_Powered_WaitForDeviceSettle;
74 break;
75
76 case HOST_STATE_Powered_WaitForDeviceSettle:
77 if (WaitMSRemaining--) {
78 Delay_MS(1);
79 break;
80 }
81 else {
82 USB_Host_VBUS_Manual_Off();
83
84 USB_OTGPAD_On();
85 USB_Host_VBUS_Auto_Enable();
86 USB_Host_VBUS_Auto_On();
87
88 USB_HostState[corenum] = HOST_STATE_Powered_WaitForConnect;
89 }
90 break;
91
92 case HOST_STATE_Powered_WaitForConnect:
93 HOST_TASK_NONBLOCK_WAIT(corenum, 100, HOST_STATE_Powered_DoReset);
94 break;
95
96 case HOST_STATE_Powered_DoReset: {
97 HCD_USB_SPEED DeviceSpeed;
98 HcdRhPortReset(corenum);
99 HcdGetDeviceSpeed(corenum,&DeviceSpeed); // skip checking status
100 USB_Host_SetDeviceSpeed(corenum, DeviceSpeed);
101 HOST_TASK_NONBLOCK_WAIT(corenum, 200, HOST_STATE_Powered_ConfigPipe);
102 }
103 break;
104
105 case HOST_STATE_Powered_ConfigPipe:
106 if (!Pipe_ConfigurePipe(corenum, PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
107 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
108 PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE) ) {
109 ErrorCode = HOST_ENUMERROR_PipeConfigError;
110 SubErrorCode = 0;
111 break;
112 }
113
114 USB_HostState[corenum] = HOST_STATE_Default;
115 break;
116
117 case HOST_STATE_Default: {
118 USB_Descriptor_Device_t DevDescriptor;
119 USB_ControlRequest = (USB_Request_Header_t)
120 {
121 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
122 .bRequest = REQ_GetDescriptor,
123 .wValue = (DTYPE_Device << 8),
124 .wIndex = 0,
125 .wLength = 8,
126 };
127
128 if ((SubErrorCode = USB_Host_SendControlRequest(corenum, &DevDescriptor)) != HOST_SENDCONTROL_Successful) {
129 ErrorCode = HOST_ENUMERROR_ControlError;
130 break;
131 }
132
133 USB_Host_ControlPipeSize[corenum] = DevDescriptor.Endpoint0Size;
134
135 Pipe_ClosePipe(corenum, PIPE_CONTROLPIPE);
136 HcdRhPortReset(corenum);
137
138 HOST_TASK_NONBLOCK_WAIT(corenum, 200, HOST_STATE_Default_PostReset);
139 }
140 break;
141
142 case HOST_STATE_Default_PostReset:
143 if (!Pipe_ConfigurePipe(corenum, PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
144 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
145 USB_Host_ControlPipeSize[corenum], PIPE_BANK_SINGLE) ) {
146 ErrorCode = HOST_ENUMERROR_PipeConfigError;
147 SubErrorCode = 0;
148 break;
149 }
150
151 USB_ControlRequest = (USB_Request_Header_t)
152 {
153 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
154 .bRequest = REQ_SetAddress,
155 .wValue = USB_HOST_DEVICEADDRESS,
156 .wIndex = 0,
157 .wLength = 0,
158 };
159
160 if ((SubErrorCode = USB_Host_SendControlRequest(corenum, NULL)) != HOST_SENDCONTROL_Successful) {
161 ErrorCode = HOST_ENUMERROR_ControlError;
162 break;
163 }
164
165 Pipe_ClosePipe(corenum, PIPE_CONTROLPIPE);
166 HOST_TASK_NONBLOCK_WAIT(corenum, 100, HOST_STATE_Default_PostAddressSet);
167 break;
168
169 case HOST_STATE_Default_PostAddressSet:
170 Pipe_ConfigurePipe(corenum, PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
171 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
172 USB_Host_ControlPipeSize[corenum], PIPE_BANK_SINGLE);
173
174 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
175
176 USB_HostState[corenum] = HOST_STATE_Addressed;
177
178 EVENT_USB_Host_DeviceEnumerationComplete(corenum);
179 break;
180 }
181
182 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState[corenum] != HOST_STATE_Unattached)) {
183 EVENT_USB_Host_DeviceEnumerationFailed(corenum, ErrorCode, SubErrorCode);
184
185 USB_Host_VBUS_Auto_Off();
186
187 EVENT_USB_Host_DeviceUnattached(corenum);
188
189 USB_ResetInterface(corenum, USB_MODE_Host);
190 }
191 }
192
193 uint8_t USB_Host_WaitMS(uint8_t MS)
194 {
195 return HOST_WAITERROR_Successful;
196 }
197
198 void USB_Host_Enumerate(uint8_t HostId) /* Part of Interrupt Service Routine */
199 {
200 // CurrentHostID = HostId;
201 // hostselected = HostId;
202 EVENT_USB_Host_DeviceAttached(HostId);
203 USB_HostState[HostId] = HOST_STATE_Powered;
204 }
205
206 void USB_Host_DeEnumerate(uint8_t HostId) /* Part of Interrupt Service Routine */
207 {
208 uint8_t i;
209
210 Pipe_ClosePipe(HostId, PIPE_CONTROLPIPE); // FIXME close only relevant pipes , take long time in ISR
211 for (i = PIPE_CONTROLPIPE + 1; i < PIPE_TOTAL_PIPES; i++)
212 if (PipeInfo[HostId][i].PipeHandle != 0) {
213 Pipe_ClosePipe(HostId, i);
214 }
215
216 EVENT_USB_Host_DeviceUnattached(HostId);
217 USB_HostState[HostId] = HOST_STATE_Unattached;
218 }
219
220 void USB_Host_SetActiveHost(uint8_t hostid)
221 {
222 hostselected = hostid;
223 }
224
225 uint8_t USB_Host_GetActiveHost(void)
226 {
227 return hostselected;
228 }
229
230 void USB_Host_SetDeviceSpeed(uint8_t hostid, HCD_USB_SPEED speed)
231 {
232 hostportspeed[hostid] = speed;
233 }
234
235 HCD_USB_SPEED USB_Host_GetDeviceSpeed(uint8_t hostid)
236 {
237 if (hostid <= 1) {
238 return hostportspeed[hostid];
239 }
240 else {return LOW_SPEED; }
241 }
242
243 uint16_t USB_Host_GetFrameNumber(void)
244 {
245 return HcdGetFrameNumber(USB_Host_GetActiveHost());
246 }
247
248 #endif