LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Host_LPC.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 
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]) {
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 
74  break;
75 
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 
89  }
90  break;
91 
93  HOST_TASK_NONBLOCK_WAIT(corenum, 100, HOST_STATE_Powered_DoReset);
94  break;
95 
97  HCD_USB_SPEED DeviceSpeed;
98  HcdRhPortReset(corenum, 1);
99  HcdGetDeviceSpeed(corenum, 1, &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 
109  ErrorCode = HOST_ENUMERROR_PipeConfigError;
110  SubErrorCode = 0;
111  break;
112  }
113 
115  break;
116 
117  case HOST_STATE_Default: {
118  USB_Descriptor_Device_t DevDescriptor;
120  {
121  .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
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 
136  HcdRhPortReset(corenum, 1);
137 
138  HOST_TASK_NONBLOCK_WAIT(corenum, 200, HOST_STATE_Default_PostReset);
139  }
140  break;
141 
146  ErrorCode = HOST_ENUMERROR_PipeConfigError;
147  SubErrorCode = 0;
148  break;
149  }
150 
152  {
153  .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
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 
166  HOST_TASK_NONBLOCK_WAIT(corenum, 100, HOST_STATE_Default_PostAddressSet);
167  break;
168 
173 
174  USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
175 
177 
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 
188 
189  USB_ResetInterface(corenum);
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;
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 
218 }
219 
220 void USB_Host_SetActiveHost(uint8_t hostid)
221 {
222  hostselected = hostid;
223 }
224 
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 
236 {
237  if (hostid <= 1) {
238  return hostportspeed[hostid];
239  }
240  else {return LOW_SPEED; }
241 }
242 
244 {
246 }
247 
248 #endif