This is a for debugging \\\\\\\"BLUE USB\\\\\\\". You can connect with HCI mode. How to connect White Wizard Board TANK *White Wizard Board - Motor Driver Board * p 21 - IN_R1 * p 22 - IN_R2 * p 23 - IN_L2 * p 24 - IN_L1

Dependencies:   mbed

Committer:
halfpitch
Date:
Wed Aug 31 11:10:18 2011 +0000
Revision:
1:c56059923036
Parent:
0:a6476c138e84
Rev.B

Who changed what in which revision?

UserRevisionLine numberNew contents of line
halfpitch 0:a6476c138e84 1
halfpitch 0:a6476c138e84 2 /*
halfpitch 0:a6476c138e84 3 Copyright (c) 2010 Peter Barrett
halfpitch 0:a6476c138e84 4
halfpitch 0:a6476c138e84 5 Permission is hereby granted, free of charge, to any person obtaining a copy
halfpitch 0:a6476c138e84 6 of this software and associated documentation files (the "Software"), to deal
halfpitch 0:a6476c138e84 7 in the Software without restriction, including without limitation the rights
halfpitch 0:a6476c138e84 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
halfpitch 0:a6476c138e84 9 copies of the Software, and to permit persons to whom the Software is
halfpitch 0:a6476c138e84 10 furnished to do so, subject to the following conditions:
halfpitch 0:a6476c138e84 11
halfpitch 0:a6476c138e84 12 The above copyright notice and this permission notice shall be included in
halfpitch 0:a6476c138e84 13 all copies or substantial portions of the Software.
halfpitch 0:a6476c138e84 14
halfpitch 0:a6476c138e84 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
halfpitch 0:a6476c138e84 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
halfpitch 0:a6476c138e84 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
halfpitch 0:a6476c138e84 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
halfpitch 0:a6476c138e84 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
halfpitch 0:a6476c138e84 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
halfpitch 0:a6476c138e84 21 THE SOFTWARE.
halfpitch 0:a6476c138e84 22 */
halfpitch 0:a6476c138e84 23
halfpitch 0:a6476c138e84 24 #include "mbed.h"
halfpitch 0:a6476c138e84 25 #include "USBHost.h"
halfpitch 0:a6476c138e84 26
halfpitch 0:a6476c138e84 27 // Config (default uses x bytes)
halfpitch 0:a6476c138e84 28 #define MAX_DEVICES 8 // Max number of devices
halfpitch 0:a6476c138e84 29 #define MAX_ENDPOINTS_TOTAL 16 // Max number of endpoints total
halfpitch 0:a6476c138e84 30 #define MAX_ENDPOINTS_PER_DEVICE 8 // Max number of endpoints for any one device
halfpitch 0:a6476c138e84 31
halfpitch 0:a6476c138e84 32 #define USBLOG 1
halfpitch 0:a6476c138e84 33 #if USBLOG
halfpitch 0:a6476c138e84 34 #define LOG(...) printf(__VA_ARGS__)
halfpitch 0:a6476c138e84 35 #else
halfpitch 0:a6476c138e84 36 #define LOG(...) do {} while(0)
halfpitch 0:a6476c138e84 37 #endif
halfpitch 0:a6476c138e84 38
halfpitch 0:a6476c138e84 39 // USB host structures
halfpitch 0:a6476c138e84 40
halfpitch 0:a6476c138e84 41 #define USB_RAM_SIZE 16*1024 // AHB SRAM block 1 TODO MACHINE DEPENDENT
halfpitch 0:a6476c138e84 42 #define USB_RAM_BASE 0x2007C000
halfpitch 0:a6476c138e84 43
halfpitch 0:a6476c138e84 44 #define TOKEN_SETUP 0
halfpitch 0:a6476c138e84 45 #define TOKEN_IN 1
halfpitch 0:a6476c138e84 46 #define TOKEN_OUT 2
halfpitch 0:a6476c138e84 47
halfpitch 0:a6476c138e84 48 // Status flags from hub
halfpitch 0:a6476c138e84 49 #define PORT_CONNECTION 0
halfpitch 0:a6476c138e84 50 #define PORT_ENABLE 1
halfpitch 0:a6476c138e84 51 #define PORT_SUSPEND 2
halfpitch 0:a6476c138e84 52 #define PORT_OVER_CURRENT 3
halfpitch 0:a6476c138e84 53 #define PORT_RESET 4
halfpitch 0:a6476c138e84 54 #define PORT_POWER 8
halfpitch 0:a6476c138e84 55 #define PORT_LOW_SPEED 9
halfpitch 0:a6476c138e84 56
halfpitch 0:a6476c138e84 57 #define C_PORT_CONNECTION 16
halfpitch 0:a6476c138e84 58 #define C_PORT_ENABLE 17
halfpitch 0:a6476c138e84 59 #define C_PORT_SUSPEND 18
halfpitch 0:a6476c138e84 60 #define C_PORT_OVER_CURRENT 19
halfpitch 0:a6476c138e84 61 #define C_PORT_RESET 20
halfpitch 0:a6476c138e84 62
halfpitch 0:a6476c138e84 63 typedef struct {
halfpitch 0:a6476c138e84 64 u8 bm_request_type;
halfpitch 0:a6476c138e84 65 u8 b_request;
halfpitch 0:a6476c138e84 66 u16 w_value;
halfpitch 0:a6476c138e84 67 u16 w_index;
halfpitch 0:a6476c138e84 68 u16 w_length;
halfpitch 0:a6476c138e84 69 } Setup;
halfpitch 0:a6476c138e84 70
halfpitch 0:a6476c138e84 71
halfpitch 0:a6476c138e84 72 // Hub stuff is kept private just to keep api simple
halfpitch 0:a6476c138e84 73 int SetPortFeature(int device, int feature, int index);
halfpitch 0:a6476c138e84 74 int ClearPortFeature(int device, int feature, int index);
halfpitch 0:a6476c138e84 75 int SetPortPower(int device, int port);
halfpitch 0:a6476c138e84 76 int SetPortReset(int device, int port);
halfpitch 0:a6476c138e84 77 int GetPortStatus(int device, int port, u32* status);
halfpitch 0:a6476c138e84 78
halfpitch 0:a6476c138e84 79 //===================================================================
halfpitch 0:a6476c138e84 80 //===================================================================
halfpitch 0:a6476c138e84 81 // Hardware defines
halfpitch 0:a6476c138e84 82
halfpitch 0:a6476c138e84 83 // HcControl
halfpitch 0:a6476c138e84 84 #define PeriodicListEnable 0x00000004
halfpitch 0:a6476c138e84 85 #define IsochronousEnable 0x00000008
halfpitch 0:a6476c138e84 86 #define ControlListEnable 0x00000010
halfpitch 0:a6476c138e84 87 #define BulkListEnable 0x00000020
halfpitch 0:a6476c138e84 88 #define OperationalMask 0x00000080
halfpitch 0:a6476c138e84 89 #define HostControllerFunctionalState 0x000000C0
halfpitch 0:a6476c138e84 90
halfpitch 0:a6476c138e84 91 // HcCommandStatus
halfpitch 0:a6476c138e84 92 #define HostControllerReset 0x00000001
halfpitch 0:a6476c138e84 93 #define ControlListFilled 0x00000002
halfpitch 0:a6476c138e84 94 #define BulkListFilled 0x00000004
halfpitch 0:a6476c138e84 95
halfpitch 0:a6476c138e84 96 // HcInterruptStatus Register
halfpitch 0:a6476c138e84 97 #define WritebackDoneHead 0x00000002
halfpitch 0:a6476c138e84 98 #define StartofFrame 0x00000004
halfpitch 0:a6476c138e84 99 #define ResumeDetected 0x00000008
halfpitch 0:a6476c138e84 100 #define UnrecoverableError 0x00000010
halfpitch 0:a6476c138e84 101 #define FrameNumberOverflow 0x00000020
halfpitch 0:a6476c138e84 102 #define RootHubStatusChange 0x00000040
halfpitch 0:a6476c138e84 103 #define OwnershipChange 0x00000080
halfpitch 0:a6476c138e84 104 #define MasterInterruptEnable 0x80000000
halfpitch 0:a6476c138e84 105
halfpitch 0:a6476c138e84 106 // HcRhStatus
halfpitch 0:a6476c138e84 107 #define SetGlobalPower 0x00010000
halfpitch 0:a6476c138e84 108 #define DeviceRemoteWakeupEnable 0x00008000
halfpitch 0:a6476c138e84 109
halfpitch 0:a6476c138e84 110 // HcRhPortStatus (hub 0, port 1)
halfpitch 0:a6476c138e84 111 #define CurrentConnectStatus 0x00000001
halfpitch 0:a6476c138e84 112 #define PortEnableStatus 0x00000002
halfpitch 0:a6476c138e84 113 #define PortSuspendStatus 0x00000004
halfpitch 0:a6476c138e84 114 #define PortOverCurrentIndicator 0x00000008
halfpitch 0:a6476c138e84 115 #define PortResetStatus 0x00000010
halfpitch 0:a6476c138e84 116
halfpitch 0:a6476c138e84 117 #define PortPowerStatus 0x00000100
halfpitch 0:a6476c138e84 118 #define LowspeedDevice 0x00000200
halfpitch 0:a6476c138e84 119 #define HighspeedDevice 0x00000400
halfpitch 0:a6476c138e84 120
halfpitch 0:a6476c138e84 121 #define ConnectStatusChange (CurrentConnectStatus << 16)
halfpitch 0:a6476c138e84 122 #define PortResetStatusChange (PortResetStatus << 16)
halfpitch 0:a6476c138e84 123
halfpitch 0:a6476c138e84 124
halfpitch 0:a6476c138e84 125 #define TD_ROUNDING (u32)0x00040000
halfpitch 0:a6476c138e84 126 #define TD_SETUP (u32)0x00000000
halfpitch 0:a6476c138e84 127 #define TD_IN (u32)0x00100000
halfpitch 0:a6476c138e84 128 #define TD_OUT (u32)0x00080000
halfpitch 0:a6476c138e84 129 #define TD_DELAY_INT(x) (u32)((x) << 21)
halfpitch 0:a6476c138e84 130 #define TD_TOGGLE_0 (u32)0x02000000
halfpitch 0:a6476c138e84 131 #define TD_TOGGLE_1 (u32)0x03000000
halfpitch 0:a6476c138e84 132 #define TD_CC (u32)0xF0000000
halfpitch 0:a6476c138e84 133
halfpitch 0:a6476c138e84 134 // HostController EndPoint Descriptor
halfpitch 0:a6476c138e84 135 typedef struct {
halfpitch 0:a6476c138e84 136 volatile u32 Control;
halfpitch 0:a6476c138e84 137 volatile u32 TailTd;
halfpitch 0:a6476c138e84 138 volatile u32 HeadTd;
halfpitch 0:a6476c138e84 139 volatile u32 Next;
halfpitch 0:a6476c138e84 140 } HCED;
halfpitch 0:a6476c138e84 141
halfpitch 0:a6476c138e84 142 // HostController Transfer Descriptor
halfpitch 0:a6476c138e84 143 typedef struct {
halfpitch 0:a6476c138e84 144 volatile u32 Control;
halfpitch 0:a6476c138e84 145 volatile u32 CurrBufPtr;
halfpitch 0:a6476c138e84 146 volatile u32 Next;
halfpitch 0:a6476c138e84 147 volatile u32 BufEnd;
halfpitch 0:a6476c138e84 148 } HCTD;
halfpitch 0:a6476c138e84 149
halfpitch 0:a6476c138e84 150 // Host Controller Communication Area
halfpitch 0:a6476c138e84 151 typedef struct {
halfpitch 0:a6476c138e84 152 volatile u32 InterruptTable[32];
halfpitch 0:a6476c138e84 153 volatile u16 FrameNumber;
halfpitch 0:a6476c138e84 154 volatile u16 FrameNumberPad;
halfpitch 0:a6476c138e84 155 volatile u32 DoneHead;
halfpitch 0:a6476c138e84 156 volatile u8 Reserved[120];
halfpitch 0:a6476c138e84 157 } HCCA;
halfpitch 0:a6476c138e84 158
halfpitch 0:a6476c138e84 159 //====================================================================================
halfpitch 0:a6476c138e84 160 //====================================================================================
halfpitch 0:a6476c138e84 161
halfpitch 0:a6476c138e84 162 class HostController;
halfpitch 0:a6476c138e84 163 class Endpoint;
halfpitch 0:a6476c138e84 164 class Device;
halfpitch 0:a6476c138e84 165
halfpitch 0:a6476c138e84 166 // must be 3*16 bytes long
halfpitch 0:a6476c138e84 167 class Endpoint
halfpitch 0:a6476c138e84 168 {
halfpitch 0:a6476c138e84 169 public:
halfpitch 0:a6476c138e84 170 HCED EndpointDescriptor; // Pointer to EndpointDescriptor == Pointer to Endpoint
halfpitch 0:a6476c138e84 171 HCTD TDHead;
halfpitch 0:a6476c138e84 172
halfpitch 0:a6476c138e84 173 enum State
halfpitch 0:a6476c138e84 174 {
halfpitch 0:a6476c138e84 175 Free,
halfpitch 0:a6476c138e84 176 NotQueued,
halfpitch 0:a6476c138e84 177 Idle,
halfpitch 0:a6476c138e84 178 SetupQueued,
halfpitch 0:a6476c138e84 179 DataQueued,
halfpitch 0:a6476c138e84 180 StatusQueued,
halfpitch 0:a6476c138e84 181 CallbackPending
halfpitch 0:a6476c138e84 182 };
halfpitch 0:a6476c138e84 183
halfpitch 0:a6476c138e84 184 volatile u8 CurrentState;
halfpitch 0:a6476c138e84 185 u8 Flags; // 0x80 In, 0x03 mask endpoint type
halfpitch 0:a6476c138e84 186
halfpitch 0:a6476c138e84 187 u16 Length;
halfpitch 0:a6476c138e84 188 u8* Data;
halfpitch 0:a6476c138e84 189 USBCallback Callback; // Must be a multiple of 16 bytes long
halfpitch 0:a6476c138e84 190 void* UserData;
halfpitch 0:a6476c138e84 191
halfpitch 0:a6476c138e84 192 int Address()
halfpitch 0:a6476c138e84 193 {
halfpitch 0:a6476c138e84 194 int ep = (EndpointDescriptor.Control >> 7) & 0xF;
halfpitch 0:a6476c138e84 195 if (ep)
halfpitch 0:a6476c138e84 196 ep |= Flags & 0x80;
halfpitch 0:a6476c138e84 197 return ep;
halfpitch 0:a6476c138e84 198 }
halfpitch 0:a6476c138e84 199
halfpitch 0:a6476c138e84 200 int Device()
halfpitch 0:a6476c138e84 201 {
halfpitch 0:a6476c138e84 202 return EndpointDescriptor.Control & 0x7F;
halfpitch 0:a6476c138e84 203 }
halfpitch 0:a6476c138e84 204
halfpitch 0:a6476c138e84 205 int Status()
halfpitch 0:a6476c138e84 206 {
halfpitch 0:a6476c138e84 207 return (TDHead.Control >> 28) & 0xF;
halfpitch 0:a6476c138e84 208 }
halfpitch 0:a6476c138e84 209
halfpitch 0:a6476c138e84 210 u32 Enqueue(u32 head)
halfpitch 0:a6476c138e84 211 {
halfpitch 0:a6476c138e84 212 if (CurrentState == NotQueued)
halfpitch 0:a6476c138e84 213 {
halfpitch 0:a6476c138e84 214 EndpointDescriptor.Next = head;
halfpitch 0:a6476c138e84 215 head = (u32)&EndpointDescriptor;
halfpitch 0:a6476c138e84 216 CurrentState = Idle;
halfpitch 0:a6476c138e84 217 }
halfpitch 0:a6476c138e84 218 return head;
halfpitch 0:a6476c138e84 219 }
halfpitch 0:a6476c138e84 220 };
halfpitch 0:a6476c138e84 221
halfpitch 0:a6476c138e84 222 class Device
halfpitch 0:a6476c138e84 223 {
halfpitch 0:a6476c138e84 224 public:
halfpitch 0:a6476c138e84 225 u8 _endpointMap[MAX_ENDPOINTS_PER_DEVICE*2];
halfpitch 0:a6476c138e84 226 u8 Hub;
halfpitch 0:a6476c138e84 227 u8 Port;
halfpitch 0:a6476c138e84 228 u8 Addr;
halfpitch 0:a6476c138e84 229 u8 Pad;
halfpitch 0:a6476c138e84 230
halfpitch 0:a6476c138e84 231 // Only if this device is a hub
halfpitch 0:a6476c138e84 232 u8 HubPortCount; // nonzero if this is a hub
halfpitch 0:a6476c138e84 233 u8 HubInterruptData;
halfpitch 0:a6476c138e84 234 u8 HubMap;
halfpitch 0:a6476c138e84 235 u8 HubMask;
halfpitch 0:a6476c138e84 236
halfpitch 0:a6476c138e84 237 int Flags; // 1 = Disconnected
halfpitch 0:a6476c138e84 238
halfpitch 0:a6476c138e84 239 Setup SetupBuffer;
halfpitch 0:a6476c138e84 240
halfpitch 0:a6476c138e84 241 // Allocate endpoint zero
halfpitch 0:a6476c138e84 242 int Init(DeviceDescriptor* d, int hub, int port, int addr, int lowSpeed)
halfpitch 0:a6476c138e84 243 {
halfpitch 0:a6476c138e84 244 Hub = hub;
halfpitch 0:a6476c138e84 245 Port = port;
halfpitch 0:a6476c138e84 246 Addr = addr;
halfpitch 0:a6476c138e84 247 Flags = lowSpeed;
halfpitch 0:a6476c138e84 248 memset(_endpointMap,0xFF,sizeof(_endpointMap));
halfpitch 0:a6476c138e84 249 return 0;
halfpitch 0:a6476c138e84 250 }
halfpitch 0:a6476c138e84 251
halfpitch 0:a6476c138e84 252 int SetEndpointIndex(int ep, int endpointIndex)
halfpitch 0:a6476c138e84 253 {
halfpitch 0:a6476c138e84 254 for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2)
halfpitch 0:a6476c138e84 255 {
halfpitch 0:a6476c138e84 256 if (_endpointMap[i] == 0xFF) // Add endpoint to map
halfpitch 0:a6476c138e84 257 {
halfpitch 0:a6476c138e84 258 _endpointMap[i] = ep;
halfpitch 0:a6476c138e84 259 _endpointMap[i+1] = endpointIndex;
halfpitch 0:a6476c138e84 260 return 0;
halfpitch 0:a6476c138e84 261 }
halfpitch 0:a6476c138e84 262 }
halfpitch 0:a6476c138e84 263 return ERR_ENDPOINT_NONE_LEFT;
halfpitch 0:a6476c138e84 264 }
halfpitch 0:a6476c138e84 265
halfpitch 0:a6476c138e84 266 int GetEndpointIndex(int ep)
halfpitch 0:a6476c138e84 267 {
halfpitch 0:a6476c138e84 268 for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2)
halfpitch 0:a6476c138e84 269 {
halfpitch 0:a6476c138e84 270 if (_endpointMap[i] == ep)
halfpitch 0:a6476c138e84 271 return _endpointMap[i+1];
halfpitch 0:a6476c138e84 272 if (_endpointMap[i] == 0xFF)
halfpitch 0:a6476c138e84 273 break;
halfpitch 0:a6476c138e84 274 }
halfpitch 0:a6476c138e84 275 return -1;
halfpitch 0:a6476c138e84 276 }
halfpitch 0:a6476c138e84 277 };
halfpitch 0:a6476c138e84 278
halfpitch 0:a6476c138e84 279 class HostController
halfpitch 0:a6476c138e84 280 {
halfpitch 0:a6476c138e84 281 public:
halfpitch 0:a6476c138e84 282 HCCA CommunicationArea;
halfpitch 0:a6476c138e84 283 Endpoint Endpoints[MAX_ENDPOINTS_TOTAL]; // Multiple of 16
halfpitch 0:a6476c138e84 284
halfpitch 0:a6476c138e84 285 Endpoint EndpointZero; // For device enumeration
halfpitch 0:a6476c138e84 286 HCTD _commonTail;
halfpitch 0:a6476c138e84 287 Setup _setupZero;
halfpitch 0:a6476c138e84 288
halfpitch 0:a6476c138e84 289 Device Devices[MAX_DEVICES];
halfpitch 0:a6476c138e84 290 u32 _frameNumber; // 32 bit ms counter
halfpitch 0:a6476c138e84 291
halfpitch 0:a6476c138e84 292 u8 _callbacksPending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue
halfpitch 0:a6476c138e84 293 u8 _rootHubStatusChange; // Root hub status has changed, set from ISR
halfpitch 0:a6476c138e84 294 u8 _unused0;
halfpitch 0:a6476c138e84 295 u8 _unused1;
halfpitch 0:a6476c138e84 296
halfpitch 0:a6476c138e84 297 u8 _connectPending; // Reset has initiated a connect
halfpitch 0:a6476c138e84 298 u8 _connectCountdown; // Number of ms left after reset before we can connect
halfpitch 0:a6476c138e84 299 u8 _connectHub; // Will connect on this hub
halfpitch 0:a6476c138e84 300 u8 _connectPort; // ... and this port
halfpitch 0:a6476c138e84 301
halfpitch 0:a6476c138e84 302 u8 SRAM[0]; // Start of free SRAM
halfpitch 0:a6476c138e84 303
halfpitch 0:a6476c138e84 304 void Loop()
halfpitch 0:a6476c138e84 305 {
halfpitch 0:a6476c138e84 306 u16 elapsed = CommunicationArea.FrameNumber - (u16)_frameNumber; // extend to 32 bits
halfpitch 0:a6476c138e84 307 _frameNumber += elapsed;
halfpitch 0:a6476c138e84 308
halfpitch 0:a6476c138e84 309 // Do callbacks, if any
halfpitch 0:a6476c138e84 310 while (_callbacksPending)
halfpitch 0:a6476c138e84 311 {
halfpitch 0:a6476c138e84 312 for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++)
halfpitch 0:a6476c138e84 313 {
halfpitch 0:a6476c138e84 314 Endpoint* endpoint = Endpoints + i;
halfpitch 0:a6476c138e84 315 if (endpoint->CurrentState == Endpoint::CallbackPending)
halfpitch 0:a6476c138e84 316 {
halfpitch 0:a6476c138e84 317 _callbacksPending--;
halfpitch 0:a6476c138e84 318 endpoint->CurrentState = Endpoint::Idle;
halfpitch 0:a6476c138e84 319 endpoint->Callback(endpoint->Device(),endpoint->Address(),endpoint->Status(),endpoint->Data,endpoint->Length,endpoint->UserData);
halfpitch 0:a6476c138e84 320 }
halfpitch 0:a6476c138e84 321 }
halfpitch 0:a6476c138e84 322 }
halfpitch 0:a6476c138e84 323
halfpitch 0:a6476c138e84 324 // Deal with changes on the root hub
halfpitch 0:a6476c138e84 325 if (_rootHubStatusChange)
halfpitch 0:a6476c138e84 326 {
halfpitch 0:a6476c138e84 327 u32 status = LPC_USB->HcRhPortStatus1;
halfpitch 0:a6476c138e84 328 _rootHubStatusChange = 0;
halfpitch 0:a6476c138e84 329 if (status >> 16)
halfpitch 0:a6476c138e84 330 {
halfpitch 0:a6476c138e84 331 HubStatusChange(0,1,status);
halfpitch 0:a6476c138e84 332 LPC_USB->HcRhPortStatus1 = status & 0xFFFF0000; // clear status changes
halfpitch 0:a6476c138e84 333 }
halfpitch 0:a6476c138e84 334 }
halfpitch 0:a6476c138e84 335
halfpitch 0:a6476c138e84 336 // Connect after reset timeout
halfpitch 0:a6476c138e84 337 if (_connectCountdown)
halfpitch 0:a6476c138e84 338 {
halfpitch 0:a6476c138e84 339 if (elapsed >= _connectCountdown)
halfpitch 0:a6476c138e84 340 {
halfpitch 0:a6476c138e84 341 _connectCountdown = 0;
halfpitch 0:a6476c138e84 342 Connect(_connectHub,_connectPort & 0x7F,_connectPort & 0x80);
halfpitch 0:a6476c138e84 343 } else
halfpitch 0:a6476c138e84 344 _connectCountdown -= elapsed;
halfpitch 0:a6476c138e84 345 }
halfpitch 0:a6476c138e84 346 }
halfpitch 0:a6476c138e84 347
halfpitch 0:a6476c138e84 348 // HubInterrupt - bitmap in dev->HubInterruptData
halfpitch 0:a6476c138e84 349 void HubInterrupt(int device)
halfpitch 0:a6476c138e84 350 {
halfpitch 0:a6476c138e84 351 Device* dev = &Devices[device-1];
halfpitch 0:a6476c138e84 352 for (int i = 0; i < dev->HubPortCount; i++)
halfpitch 0:a6476c138e84 353 {
halfpitch 0:a6476c138e84 354 int port = i+1;
halfpitch 0:a6476c138e84 355 if (dev->HubInterruptData & (1 << port))
halfpitch 0:a6476c138e84 356 {
halfpitch 0:a6476c138e84 357 u32 status = 0;
halfpitch 0:a6476c138e84 358 GetPortStatus(device,port,&status);
halfpitch 0:a6476c138e84 359 if (status >> 16)
halfpitch 0:a6476c138e84 360 {
halfpitch 0:a6476c138e84 361 if (_connectPending && (status & ConnectStatusChange))
halfpitch 0:a6476c138e84 362 continue; // Don't connect again until previous device has been added and addressed
halfpitch 0:a6476c138e84 363
halfpitch 0:a6476c138e84 364 HubStatusChange(device,port,status);
halfpitch 0:a6476c138e84 365 if (status & ConnectStatusChange)
halfpitch 0:a6476c138e84 366 ClearPortFeature(device,C_PORT_CONNECTION,port);
halfpitch 0:a6476c138e84 367 if (status & PortResetStatusChange)
halfpitch 0:a6476c138e84 368 ClearPortFeature(device,C_PORT_RESET,port);
halfpitch 0:a6476c138e84 369 }
halfpitch 0:a6476c138e84 370 }
halfpitch 0:a6476c138e84 371 }
halfpitch 0:a6476c138e84 372 }
halfpitch 0:a6476c138e84 373
halfpitch 0:a6476c138e84 374 static void HubInterruptCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
halfpitch 0:a6476c138e84 375 {
halfpitch 0:a6476c138e84 376 HostController* controller = (HostController*)userData;
halfpitch 0:a6476c138e84 377 if (status == 0)
halfpitch 0:a6476c138e84 378 controller->HubInterrupt(device);
halfpitch 0:a6476c138e84 379 USBInterruptTransfer(device,endpoint,data,1,HubInterruptCallback,userData);
halfpitch 0:a6476c138e84 380 }
halfpitch 0:a6476c138e84 381
halfpitch 0:a6476c138e84 382 int InitHub(int device)
halfpitch 0:a6476c138e84 383 {
halfpitch 0:a6476c138e84 384 u8 buf[16];
halfpitch 0:a6476c138e84 385 int r= USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_DEVICE,GET_DESCRIPTOR,(DESCRIPTOR_TYPE_HUB << 8),0,buf,sizeof(buf));
halfpitch 0:a6476c138e84 386 if (r < 0)
halfpitch 0:a6476c138e84 387 return ERR_HUB_INIT_FAILED;
halfpitch 0:a6476c138e84 388
halfpitch 0:a6476c138e84 389 // turn on power on the hubs ports
halfpitch 0:a6476c138e84 390 Device* dev = &Devices[device-1];
halfpitch 0:a6476c138e84 391 int ports = buf[2];
halfpitch 0:a6476c138e84 392 dev->HubPortCount = ports;
halfpitch 0:a6476c138e84 393 for (int i = 0; i < ports; i++)
halfpitch 0:a6476c138e84 394 SetPortPower(device,i+1);
halfpitch 0:a6476c138e84 395
halfpitch 0:a6476c138e84 396 // Enable hub change interrupts
halfpitch 0:a6476c138e84 397 return USBInterruptTransfer(device,0x81,&dev->HubInterruptData,1,HubInterruptCallback,this);
halfpitch 0:a6476c138e84 398 }
halfpitch 0:a6476c138e84 399
halfpitch 0:a6476c138e84 400 int AddEndpoint(int device, int ep, int attributes, int maxPacketSize, int interval)
halfpitch 0:a6476c138e84 401 {
halfpitch 0:a6476c138e84 402 LOG("AddEndpoint D:%02X A:%02X T:%02X P:%04X I:%02X\n",device,ep,attributes,maxPacketSize,interval);
halfpitch 0:a6476c138e84 403 Device* dev = &Devices[device-1];
halfpitch 0:a6476c138e84 404 Endpoint* endpoint = AllocateEndpoint(device,ep,attributes,maxPacketSize);
halfpitch 0:a6476c138e84 405 if (!endpoint)
halfpitch 0:a6476c138e84 406 return ERR_ENDPOINT_NONE_LEFT;
halfpitch 0:a6476c138e84 407 dev->SetEndpointIndex(ep,endpoint - Endpoints);
halfpitch 0:a6476c138e84 408 endpoint->EndpointDescriptor.Control |= dev->Flags; // Map in slow speed
halfpitch 0:a6476c138e84 409 return 0; // TODO ed->bInterval
halfpitch 0:a6476c138e84 410 }
halfpitch 0:a6476c138e84 411
halfpitch 0:a6476c138e84 412 int AddEndpoint(int device, EndpointDescriptor* ed)
halfpitch 0:a6476c138e84 413 {
halfpitch 0:a6476c138e84 414 return AddEndpoint(device,ed->bEndpointAddress,ed->bmAttributes,ed->wMaxPacketSize,ed->bInterval);
halfpitch 0:a6476c138e84 415 }
halfpitch 0:a6476c138e84 416
halfpitch 0:a6476c138e84 417 // allocate a endpoint
halfpitch 0:a6476c138e84 418 Endpoint* AllocateEndpoint(int device, int endpointAddress, int type, int maxPacketSize)
halfpitch 0:a6476c138e84 419 {
halfpitch 0:a6476c138e84 420 for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++)
halfpitch 0:a6476c138e84 421 {
halfpitch 0:a6476c138e84 422 Endpoint* ep = &Endpoints[i];
halfpitch 0:a6476c138e84 423 if (ep->CurrentState == 0)
halfpitch 0:a6476c138e84 424 {
halfpitch 0:a6476c138e84 425 //LOG("Allocated endpoint %d to %02X:%02X\n",i,device,endpointAddress);
halfpitch 0:a6476c138e84 426 ep->Flags = (endpointAddress & 0x80) | (type & 3);
halfpitch 0:a6476c138e84 427 ep->CurrentState = Endpoint::NotQueued;
halfpitch 0:a6476c138e84 428 ep->EndpointDescriptor.Control = (maxPacketSize << 16) | ((endpointAddress & 0x7F) << 7) | device;
halfpitch 0:a6476c138e84 429 return ep;
halfpitch 0:a6476c138e84 430 }
halfpitch 0:a6476c138e84 431 }
halfpitch 0:a6476c138e84 432 return 0;
halfpitch 0:a6476c138e84 433 }
halfpitch 0:a6476c138e84 434
halfpitch 0:a6476c138e84 435 Endpoint* GetEndpoint(int device, int ep)
halfpitch 0:a6476c138e84 436 {
halfpitch 0:a6476c138e84 437 if (device == 0)
halfpitch 0:a6476c138e84 438 {
halfpitch 0:a6476c138e84 439 //printf("WARNING: USING DEVICE 0\n");
halfpitch 0:a6476c138e84 440 return &EndpointZero;
halfpitch 0:a6476c138e84 441 }
halfpitch 0:a6476c138e84 442 if (device > MAX_DEVICES)
halfpitch 0:a6476c138e84 443 return 0;
halfpitch 0:a6476c138e84 444 int i = Devices[device-1].GetEndpointIndex(ep);
halfpitch 0:a6476c138e84 445 if (i == -1)
halfpitch 0:a6476c138e84 446 return 0;
halfpitch 0:a6476c138e84 447 return Endpoints + i;
halfpitch 0:a6476c138e84 448 }
halfpitch 0:a6476c138e84 449
halfpitch 0:a6476c138e84 450 int Transfer(Endpoint* endpoint, int token, u8* data, int len, int state)
halfpitch 0:a6476c138e84 451 {
halfpitch 0:a6476c138e84 452 //LOG("Transfer %02X T:%d Len:%d S:%d\n",endpoint->Address(),token,len,state);
halfpitch 0:a6476c138e84 453
halfpitch 0:a6476c138e84 454 int toggle = 0;
halfpitch 0:a6476c138e84 455 if (endpoint->Address() == 0)
halfpitch 0:a6476c138e84 456 toggle = (token == TOKEN_SETUP) ? TD_TOGGLE_0 : TD_TOGGLE_1;
halfpitch 0:a6476c138e84 457
halfpitch 0:a6476c138e84 458 if (token != TOKEN_SETUP)
halfpitch 0:a6476c138e84 459 token = (token == TOKEN_IN ? TD_IN : TD_OUT);
halfpitch 0:a6476c138e84 460
halfpitch 0:a6476c138e84 461 HCTD* head = &endpoint->TDHead;
halfpitch 0:a6476c138e84 462 HCTD* tail = &_commonTail;
halfpitch 0:a6476c138e84 463
halfpitch 0:a6476c138e84 464 head->Control = TD_ROUNDING | token | TD_DELAY_INT(0) | toggle | TD_CC;
halfpitch 0:a6476c138e84 465 head->CurrBufPtr = (u32)data;
halfpitch 0:a6476c138e84 466 head->BufEnd = (u32)(data + len - 1);
halfpitch 0:a6476c138e84 467 head->Next = (u32)tail;
halfpitch 0:a6476c138e84 468
halfpitch 0:a6476c138e84 469 HCED* ed = &endpoint->EndpointDescriptor;
halfpitch 0:a6476c138e84 470 ed->HeadTd = (u32)head | (ed->HeadTd & 0x00000002); // carry toggle
halfpitch 0:a6476c138e84 471 ed->TailTd = (u32)tail;
halfpitch 0:a6476c138e84 472
halfpitch 0:a6476c138e84 473 //HCTD* td = head;
halfpitch 0:a6476c138e84 474 //LOG("%04X TD %08X %08X %08X Next:%08X\n",CommunicationArea.FrameNumber,td->Control,td->CurrBufPtr,td->BufEnd,td->Next);
halfpitch 0:a6476c138e84 475 //LOG("%04X ED %08X %08X %08X\n",CommunicationArea.FrameNumber,ed->Control,ed->HeadTd,ed->TailTd);
halfpitch 0:a6476c138e84 476
halfpitch 0:a6476c138e84 477 switch (endpoint->Flags & 3)
halfpitch 0:a6476c138e84 478 {
halfpitch 0:a6476c138e84 479 case ENDPOINT_CONTROL:
halfpitch 0:a6476c138e84 480 LPC_USB->HcControlHeadED = endpoint->Enqueue(LPC_USB->HcControlHeadED); // May change state NotQueued->Idle
halfpitch 0:a6476c138e84 481 endpoint->CurrentState = state; // Get in before an int
halfpitch 0:a6476c138e84 482 LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | ControlListFilled;
halfpitch 0:a6476c138e84 483 LPC_USB->HcControl = LPC_USB->HcControl | ControlListEnable;
halfpitch 0:a6476c138e84 484 break;
halfpitch 0:a6476c138e84 485
halfpitch 0:a6476c138e84 486 case ENDPOINT_BULK:
halfpitch 0:a6476c138e84 487 LPC_USB->HcBulkHeadED = endpoint->Enqueue(LPC_USB->HcBulkHeadED);
halfpitch 0:a6476c138e84 488 endpoint->CurrentState = state;
halfpitch 0:a6476c138e84 489 LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | BulkListFilled;
halfpitch 0:a6476c138e84 490 LPC_USB->HcControl = LPC_USB->HcControl | BulkListEnable;
halfpitch 0:a6476c138e84 491 break;
halfpitch 0:a6476c138e84 492
halfpitch 0:a6476c138e84 493 case ENDPOINT_INTERRUPT:
halfpitch 0:a6476c138e84 494 CommunicationArea.InterruptTable[0] = endpoint->Enqueue(CommunicationArea.InterruptTable[0]);
halfpitch 0:a6476c138e84 495 endpoint->CurrentState = state;
halfpitch 0:a6476c138e84 496 LPC_USB->HcControl |= PeriodicListEnable;
halfpitch 0:a6476c138e84 497 break;
halfpitch 0:a6476c138e84 498 }
halfpitch 0:a6476c138e84 499 return 0;
halfpitch 0:a6476c138e84 500 }
halfpitch 0:a6476c138e84 501
halfpitch 0:a6476c138e84 502 // Remove an endpoint from an active queue
halfpitch 0:a6476c138e84 503 bool Remove(HCED* ed, volatile HCED** queue)
halfpitch 0:a6476c138e84 504 {
halfpitch 0:a6476c138e84 505 if (*queue == 0)
halfpitch 0:a6476c138e84 506 return false;
halfpitch 0:a6476c138e84 507 if (*queue == (volatile HCED*)ed)
halfpitch 0:a6476c138e84 508 {
halfpitch 0:a6476c138e84 509 *queue = (volatile HCED*)ed->Next; // At head of queue
halfpitch 0:a6476c138e84 510 return true;
halfpitch 0:a6476c138e84 511 }
halfpitch 0:a6476c138e84 512
halfpitch 0:a6476c138e84 513 volatile HCED* head = *queue;
halfpitch 0:a6476c138e84 514 while (head)
halfpitch 0:a6476c138e84 515 {
halfpitch 0:a6476c138e84 516 if (head->Next == (u32)ed)
halfpitch 0:a6476c138e84 517 {
halfpitch 0:a6476c138e84 518 head->Next = ed->Next;
halfpitch 0:a6476c138e84 519 return true;
halfpitch 0:a6476c138e84 520 }
halfpitch 0:a6476c138e84 521 head = (volatile HCED*)head->Next;
halfpitch 0:a6476c138e84 522 }
halfpitch 0:a6476c138e84 523 return false;
halfpitch 0:a6476c138e84 524 }
halfpitch 0:a6476c138e84 525
halfpitch 0:a6476c138e84 526 void Release(Endpoint* endpoint)
halfpitch 0:a6476c138e84 527 {
halfpitch 0:a6476c138e84 528 if (endpoint->CurrentState == Endpoint::NotQueued)
halfpitch 0:a6476c138e84 529 {
halfpitch 0:a6476c138e84 530 // Never event used it, nothing to do
halfpitch 0:a6476c138e84 531 }
halfpitch 0:a6476c138e84 532 else
halfpitch 0:a6476c138e84 533 {
halfpitch 0:a6476c138e84 534 HCED* ed = (HCED*)endpoint;
halfpitch 0:a6476c138e84 535 ed->Control |= 0x4000; // SKIP
halfpitch 0:a6476c138e84 536 switch (endpoint->Flags & 0x03)
halfpitch 0:a6476c138e84 537 {
halfpitch 0:a6476c138e84 538 case ENDPOINT_CONTROL:
halfpitch 0:a6476c138e84 539 Remove(ed,(volatile HCED**)&LPC_USB->HcControlHeadED);
halfpitch 0:a6476c138e84 540 break;
halfpitch 0:a6476c138e84 541 case ENDPOINT_BULK:
halfpitch 0:a6476c138e84 542 Remove(ed,(volatile HCED**)&LPC_USB->HcBulkHeadED);
halfpitch 0:a6476c138e84 543 break;
halfpitch 0:a6476c138e84 544 case ENDPOINT_INTERRUPT:
halfpitch 0:a6476c138e84 545 for (int i = 0; i < 32; i++)
halfpitch 0:a6476c138e84 546 Remove(ed,(volatile HCED**)&CommunicationArea.InterruptTable[i]);
halfpitch 0:a6476c138e84 547 break;
halfpitch 0:a6476c138e84 548 }
halfpitch 0:a6476c138e84 549
halfpitch 0:a6476c138e84 550 u16 fn = CommunicationArea.FrameNumber;
halfpitch 0:a6476c138e84 551 while (fn == CommunicationArea.FrameNumber)
halfpitch 0:a6476c138e84 552 ; // Wait for next frame
halfpitch 0:a6476c138e84 553
halfpitch 0:a6476c138e84 554 }
halfpitch 0:a6476c138e84 555
halfpitch 0:a6476c138e84 556 // In theory, the endpoint is now dead.
halfpitch 0:a6476c138e84 557 // TODO: Will Callbacks ever be pending? BUGBUG
halfpitch 0:a6476c138e84 558 memset(endpoint,0,sizeof(Endpoint));
halfpitch 0:a6476c138e84 559 }
halfpitch 0:a6476c138e84 560
halfpitch 0:a6476c138e84 561 // Pop the last TD from the list
halfpitch 0:a6476c138e84 562 HCTD* Reverse(HCTD* current)
halfpitch 0:a6476c138e84 563 {
halfpitch 0:a6476c138e84 564 HCTD *result = NULL,*temp;
halfpitch 0:a6476c138e84 565 while (current)
halfpitch 0:a6476c138e84 566 {
halfpitch 0:a6476c138e84 567 temp = (HCTD*)current->Next;
halfpitch 0:a6476c138e84 568 current->Next = (u32)result;
halfpitch 0:a6476c138e84 569 result = current;
halfpitch 0:a6476c138e84 570 current = temp;
halfpitch 0:a6476c138e84 571 }
halfpitch 0:a6476c138e84 572 return result;
halfpitch 0:a6476c138e84 573 }
halfpitch 0:a6476c138e84 574
halfpitch 0:a6476c138e84 575 // Called from interrupt...
halfpitch 0:a6476c138e84 576 // Control endpoints use a state machine to progress through the transfers
halfpitch 0:a6476c138e84 577 void ProcessDoneQueue(u32 tdList)
halfpitch 0:a6476c138e84 578 {
halfpitch 0:a6476c138e84 579 HCTD* list = Reverse((HCTD*)tdList);
halfpitch 0:a6476c138e84 580 while (list)
halfpitch 0:a6476c138e84 581 {
halfpitch 0:a6476c138e84 582 Endpoint* endpoint = (Endpoint*)(list-1);
halfpitch 0:a6476c138e84 583 list = (HCTD*)list->Next;
halfpitch 0:a6476c138e84 584 int ep = endpoint->Address();
halfpitch 0:a6476c138e84 585 bool in = endpoint->Flags & 0x80;
halfpitch 0:a6476c138e84 586 int status = (endpoint->TDHead.Control >> 28) & 0xF;
halfpitch 0:a6476c138e84 587
halfpitch 0:a6476c138e84 588 //LOG("ProcessDoneQueue %02X %08X\n",ep,endpoint->TDHead.Control);
halfpitch 0:a6476c138e84 589
halfpitch 0:a6476c138e84 590 if (status != 0)
halfpitch 0:a6476c138e84 591 {
halfpitch 0:a6476c138e84 592 LOG("ProcessDoneQueue status %02X %d\n",ep,status);
halfpitch 0:a6476c138e84 593 endpoint->CurrentState = Endpoint::Idle;
halfpitch 0:a6476c138e84 594 } else {
halfpitch 0:a6476c138e84 595 switch (endpoint->CurrentState)
halfpitch 0:a6476c138e84 596 {
halfpitch 0:a6476c138e84 597 case Endpoint::SetupQueued:
halfpitch 0:a6476c138e84 598 if (endpoint->Length == 0)
halfpitch 0:a6476c138e84 599 Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Skip Data Phase
halfpitch 0:a6476c138e84 600 else
halfpitch 0:a6476c138e84 601 Transfer(endpoint,in ? TOKEN_IN : TOKEN_OUT,endpoint->Data,endpoint->Length, Endpoint::DataQueued); // Setup is done, now Data
halfpitch 0:a6476c138e84 602 break;
halfpitch 0:a6476c138e84 603
halfpitch 0:a6476c138e84 604 case Endpoint::DataQueued:
halfpitch 0:a6476c138e84 605 if (endpoint->TDHead.CurrBufPtr)
halfpitch 0:a6476c138e84 606 endpoint->Length = endpoint->TDHead.CurrBufPtr - (u32)endpoint->Data;
halfpitch 0:a6476c138e84 607
halfpitch 0:a6476c138e84 608 if (ep == 0)
halfpitch 0:a6476c138e84 609 Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Data is done, now Status, Control only
halfpitch 0:a6476c138e84 610 else
halfpitch 0:a6476c138e84 611 endpoint->CurrentState = Endpoint::Idle;
halfpitch 0:a6476c138e84 612 break;
halfpitch 0:a6476c138e84 613
halfpitch 0:a6476c138e84 614 case Endpoint::StatusQueued: // Transaction is done
halfpitch 0:a6476c138e84 615 endpoint->CurrentState = Endpoint::Idle;
halfpitch 0:a6476c138e84 616 break;
halfpitch 0:a6476c138e84 617 }
halfpitch 0:a6476c138e84 618 }
halfpitch 0:a6476c138e84 619
halfpitch 0:a6476c138e84 620 // Complete, flag if we need a callback
halfpitch 0:a6476c138e84 621 if (endpoint->Callback && endpoint->CurrentState == Endpoint::Idle)
halfpitch 0:a6476c138e84 622 {
halfpitch 0:a6476c138e84 623 endpoint->CurrentState = Endpoint::CallbackPending;
halfpitch 0:a6476c138e84 624 _callbacksPending++;
halfpitch 0:a6476c138e84 625 }
halfpitch 0:a6476c138e84 626 }
halfpitch 0:a6476c138e84 627 }
halfpitch 0:a6476c138e84 628
halfpitch 0:a6476c138e84 629 // Hack to reset devices that don't want to connect
halfpitch 0:a6476c138e84 630 int AddDevice(int hub, int port, bool isLowSpeed)
halfpitch 0:a6476c138e84 631 {
halfpitch 0:a6476c138e84 632 int device = AddDeviceCore(hub,port,isLowSpeed);
halfpitch 0:a6476c138e84 633 if (device < 0)
halfpitch 0:a6476c138e84 634 {
halfpitch 0:a6476c138e84 635 LOG("========RETRY ADD DEVICE========\n"); // This will go for ever.. TODO power cycle root?
halfpitch 0:a6476c138e84 636 Disconnect(hub,port); // Could not read descriptor at assigned address, reset this port and try again
halfpitch 0:a6476c138e84 637 ResetPort(hub,port); // Cheap bluetooth dongles often need this on a hotplug
halfpitch 0:a6476c138e84 638 return -1;
halfpitch 0:a6476c138e84 639 }
halfpitch 0:a6476c138e84 640 return device;
halfpitch 0:a6476c138e84 641 }
halfpitch 0:a6476c138e84 642
halfpitch 0:a6476c138e84 643 int AddDeviceCore(int hub, int port, bool isLowSpeed)
halfpitch 0:a6476c138e84 644 {
halfpitch 0:a6476c138e84 645 int lowSpeed = isLowSpeed ? 0x2000 : 0;
halfpitch 0:a6476c138e84 646 DeviceDescriptor desc;
halfpitch 0:a6476c138e84 647 EndpointZero.EndpointDescriptor.Control = (8 << 16) | lowSpeed; // MaxPacketSize == 8
halfpitch 0:a6476c138e84 648 int r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,8);
halfpitch 0:a6476c138e84 649 if (r < 0)
halfpitch 0:a6476c138e84 650 {
halfpitch 0:a6476c138e84 651 LOG("FAILED TO LOAD DESCRIPTOR FOR DEVICE 0\n");
halfpitch 0:a6476c138e84 652 return r;
halfpitch 0:a6476c138e84 653 }
halfpitch 0:a6476c138e84 654
halfpitch 0:a6476c138e84 655 EndpointZero.EndpointDescriptor.Control = (desc.bMaxPacketSize << 16) | lowSpeed; // Actual MaxPacketSize
halfpitch 0:a6476c138e84 656 r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc));
halfpitch 0:a6476c138e84 657 if (r < 0)
halfpitch 0:a6476c138e84 658 return r;
halfpitch 0:a6476c138e84 659
halfpitch 0:a6476c138e84 660 LOG("\nClass %02X found %04X:%04X\n\n",desc.bDeviceClass,desc.idVendor,desc.idProduct);
halfpitch 0:a6476c138e84 661
halfpitch 0:a6476c138e84 662 // Now assign the device an address, move off EndpointZero
halfpitch 0:a6476c138e84 663 int device = 0;
halfpitch 0:a6476c138e84 664 for (int i = 0; i < MAX_DEVICES; i++)
halfpitch 0:a6476c138e84 665 {
halfpitch 0:a6476c138e84 666 if (Devices[i].Port == 0)
halfpitch 0:a6476c138e84 667 {
halfpitch 0:a6476c138e84 668 device = i+1;
halfpitch 0:a6476c138e84 669 break;
halfpitch 0:a6476c138e84 670 }
halfpitch 0:a6476c138e84 671 }
halfpitch 0:a6476c138e84 672 if (!device)
halfpitch 0:a6476c138e84 673 return ERR_DEVICE_NONE_LEFT;
halfpitch 0:a6476c138e84 674
halfpitch 0:a6476c138e84 675 r = SetAddress(0,device);
halfpitch 0:a6476c138e84 676 if (r)
halfpitch 0:a6476c138e84 677 return r;
halfpitch 0:a6476c138e84 678 DelayMS(2);
halfpitch 0:a6476c138e84 679
halfpitch 0:a6476c138e84 680 // Now at a nonzero address, create control endpoint
halfpitch 0:a6476c138e84 681 Device* dev = &Devices[device-1];
halfpitch 0:a6476c138e84 682 dev->Init(&desc,hub,port,device,lowSpeed);
halfpitch 0:a6476c138e84 683 AddEndpoint(device,0,ENDPOINT_CONTROL,desc.bMaxPacketSize,0);
halfpitch 0:a6476c138e84 684 _connectPending = 0;
halfpitch 0:a6476c138e84 685
halfpitch 0:a6476c138e84 686 // Verify this all works
halfpitch 0:a6476c138e84 687 r = GetDescriptor(device,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc));
halfpitch 0:a6476c138e84 688 if (r < 0)
halfpitch 0:a6476c138e84 689 return r;
halfpitch 0:a6476c138e84 690
halfpitch 0:a6476c138e84 691 // Set to interface 0 by default
halfpitch 0:a6476c138e84 692 // Calls LoadDevice if interface is found
halfpitch 0:a6476c138e84 693 r = SetConfigurationAndInterface(device,1,0,&desc);
halfpitch 0:a6476c138e84 694
halfpitch 0:a6476c138e84 695 if (desc.bDeviceClass == CLASS_HUB)
halfpitch 0:a6476c138e84 696 InitHub(device); // Handle hubs in this code
halfpitch 0:a6476c138e84 697
halfpitch 0:a6476c138e84 698 return device;
halfpitch 0:a6476c138e84 699 }
halfpitch 0:a6476c138e84 700
halfpitch 0:a6476c138e84 701 // Walk descriptors and create endpoints for a given device
halfpitch 0:a6476c138e84 702 // TODO configuration !=1, alternate settings etc.
halfpitch 0:a6476c138e84 703 int SetConfigurationAndInterface(int device, int configuration, int interfaceNumber, DeviceDescriptor* desc)
halfpitch 0:a6476c138e84 704 {
halfpitch 0:a6476c138e84 705 u8 buffer[255];
halfpitch 0:a6476c138e84 706 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,sizeof(buffer));
halfpitch 0:a6476c138e84 707 if (err < 0)
halfpitch 0:a6476c138e84 708 return err;
halfpitch 0:a6476c138e84 709
halfpitch 0:a6476c138e84 710 err = SetConfiguration(device,configuration);
halfpitch 0:a6476c138e84 711 if (err < 0)
halfpitch 0:a6476c138e84 712 return err;
halfpitch 0:a6476c138e84 713
halfpitch 0:a6476c138e84 714 // Add the endpoints for this interface
halfpitch 0:a6476c138e84 715 int len = buffer[2] | (buffer[3] << 8);
halfpitch 0:a6476c138e84 716 u8* d = buffer;
halfpitch 0:a6476c138e84 717 u8* end = d + len;
halfpitch 0:a6476c138e84 718 InterfaceDescriptor* found = 0;
halfpitch 0:a6476c138e84 719 while (d < end)
halfpitch 0:a6476c138e84 720 {
halfpitch 0:a6476c138e84 721 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
halfpitch 0:a6476c138e84 722 {
halfpitch 0:a6476c138e84 723 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
halfpitch 0:a6476c138e84 724 if (id->bInterfaceNumber == interfaceNumber)
halfpitch 0:a6476c138e84 725 {
halfpitch 0:a6476c138e84 726 found = id;
halfpitch 0:a6476c138e84 727 d += d[0];
halfpitch 0:a6476c138e84 728 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
halfpitch 0:a6476c138e84 729 {
halfpitch 0:a6476c138e84 730 switch (d[1])
halfpitch 0:a6476c138e84 731 {
halfpitch 0:a6476c138e84 732 case DESCRIPTOR_TYPE_ENDPOINT:
halfpitch 0:a6476c138e84 733 AddEndpoint(device,(EndpointDescriptor*)d);
halfpitch 0:a6476c138e84 734 break;
halfpitch 0:a6476c138e84 735 default:
halfpitch 0:a6476c138e84 736 LOG("Skipping descriptor %02X (%d bytes)\n",d[1],d[0]);
halfpitch 0:a6476c138e84 737 }
halfpitch 0:a6476c138e84 738 d += d[0];
halfpitch 0:a6476c138e84 739 }
halfpitch 0:a6476c138e84 740 }
halfpitch 0:a6476c138e84 741 }
halfpitch 0:a6476c138e84 742 d += d[0];
halfpitch 0:a6476c138e84 743 }
halfpitch 0:a6476c138e84 744
halfpitch 0:a6476c138e84 745 if (!found)
halfpitch 0:a6476c138e84 746 return ERR_INTERFACE_NOT_FOUND;
halfpitch 0:a6476c138e84 747 OnLoadDevice(device,desc,found);
halfpitch 0:a6476c138e84 748 return 0;
halfpitch 0:a6476c138e84 749 }
halfpitch 0:a6476c138e84 750
halfpitch 0:a6476c138e84 751 void Init()
halfpitch 0:a6476c138e84 752 {
halfpitch 0:a6476c138e84 753 LOG("USB INIT (Controller is %d bytes)\n",sizeof(*this));
halfpitch 0:a6476c138e84 754 memset(this,0,sizeof(HostController));
halfpitch 0:a6476c138e84 755 EndpointZero.CurrentState = Endpoint::NotQueued;
halfpitch 0:a6476c138e84 756 HWInit(&CommunicationArea);
halfpitch 0:a6476c138e84 757 DelayMS(10);
halfpitch 0:a6476c138e84 758 }
halfpitch 0:a6476c138e84 759
halfpitch 0:a6476c138e84 760 void ResetPort(int hub, int port)
halfpitch 0:a6476c138e84 761 {
halfpitch 0:a6476c138e84 762 LOG("ResetPort Hub:%d Port:%d\n",hub,port);
halfpitch 0:a6476c138e84 763 _connectPending++; // Only reset/add 1 device at a time
halfpitch 0:a6476c138e84 764 if (hub == 0)
halfpitch 0:a6476c138e84 765 LPC_USB->HcRhPortStatus1 = PortResetStatus; // Reset Root Hub, port 1
halfpitch 0:a6476c138e84 766 else
halfpitch 0:a6476c138e84 767 SetPortReset(hub,port); // or reset other hub
halfpitch 0:a6476c138e84 768 }
halfpitch 0:a6476c138e84 769
halfpitch 0:a6476c138e84 770 void Disconnect(int hub, int port)
halfpitch 0:a6476c138e84 771 {
halfpitch 0:a6476c138e84 772 LOG("Disconnect Hub:%d Port:%d\n",hub,port); // Mark a device for destruction
halfpitch 0:a6476c138e84 773 for (int i = 0; i < MAX_DEVICES; i++)
halfpitch 0:a6476c138e84 774 {
halfpitch 0:a6476c138e84 775 Device* dev = Devices + i;
halfpitch 0:a6476c138e84 776 if (dev->Port == port && dev->Hub == hub)
halfpitch 0:a6476c138e84 777 {
halfpitch 0:a6476c138e84 778 // Disconnect everything that is attached to this device if it is a hub
halfpitch 0:a6476c138e84 779 for (int p = 0; p < dev->HubPortCount; p++)
halfpitch 0:a6476c138e84 780 Disconnect(i+1,p+1);
halfpitch 0:a6476c138e84 781
halfpitch 0:a6476c138e84 782 // Now release endpoints
halfpitch 0:a6476c138e84 783 for (int j = 1; j < MAX_ENDPOINTS_PER_DEVICE*2; j += 2)
halfpitch 0:a6476c138e84 784 {
halfpitch 0:a6476c138e84 785 u8 endpointIndex = dev->_endpointMap[j];
halfpitch 0:a6476c138e84 786 if (endpointIndex != 0xFF)
halfpitch 0:a6476c138e84 787 Release(Endpoints + endpointIndex);
halfpitch 0:a6476c138e84 788 }
halfpitch 0:a6476c138e84 789 dev->Port = 0; // Device is now free
halfpitch 0:a6476c138e84 790 dev->Flags = 0;
halfpitch 0:a6476c138e84 791 return;
halfpitch 0:a6476c138e84 792 }
halfpitch 0:a6476c138e84 793 }
halfpitch 0:a6476c138e84 794 }
halfpitch 0:a6476c138e84 795
halfpitch 0:a6476c138e84 796 // called after reset
halfpitch 0:a6476c138e84 797 void Connect(int hub, int port, bool lowspeed)
halfpitch 0:a6476c138e84 798 {
halfpitch 0:a6476c138e84 799 LOG("Connect Hub:%d Port:%d %s\n",hub,port,lowspeed ? "slow" : "full");
halfpitch 0:a6476c138e84 800 AddDevice(hub,port,lowspeed);
halfpitch 0:a6476c138e84 801 }
halfpitch 0:a6476c138e84 802
halfpitch 0:a6476c138e84 803 // Called from interrupt
halfpitch 0:a6476c138e84 804 void HubStatusChange(int hub, int port, u32 status)
halfpitch 0:a6476c138e84 805 {
halfpitch 0:a6476c138e84 806 LOG("HubStatusChange Hub:%d Port:%d %08X\n",hub,port,status);
halfpitch 0:a6476c138e84 807 if (status & ConnectStatusChange)
halfpitch 0:a6476c138e84 808 {
halfpitch 0:a6476c138e84 809 if (status & CurrentConnectStatus) // Connecting
halfpitch 0:a6476c138e84 810 ResetPort(hub,port); // Reset to initiate connect (state machine?)
halfpitch 0:a6476c138e84 811 else
halfpitch 0:a6476c138e84 812 Disconnect(hub,port);
halfpitch 0:a6476c138e84 813 }
halfpitch 0:a6476c138e84 814
halfpitch 0:a6476c138e84 815 if (status & PortResetStatusChange)
halfpitch 0:a6476c138e84 816 {
halfpitch 0:a6476c138e84 817 if (!(status & PortResetStatus))
halfpitch 0:a6476c138e84 818 {
halfpitch 0:a6476c138e84 819 _connectCountdown = 200; // Schedule a connection in 200ms
halfpitch 0:a6476c138e84 820 if (status & LowspeedDevice)
halfpitch 0:a6476c138e84 821 port |= 0x80;
halfpitch 0:a6476c138e84 822 _connectHub = hub;
halfpitch 0:a6476c138e84 823 _connectPort = port;
halfpitch 0:a6476c138e84 824 }
halfpitch 0:a6476c138e84 825 }
halfpitch 0:a6476c138e84 826 }
halfpitch 0:a6476c138e84 827
halfpitch 0:a6476c138e84 828 #define HOST_CLK_EN (1<<0)
halfpitch 0:a6476c138e84 829 #define PORTSEL_CLK_EN (1<<3)
halfpitch 0:a6476c138e84 830 #define AHB_CLK_EN (1<<4)
halfpitch 0:a6476c138e84 831 #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN)
halfpitch 0:a6476c138e84 832
halfpitch 0:a6476c138e84 833 #define FRAMEINTERVAL (12000-1) // 1ms
halfpitch 0:a6476c138e84 834 #define DEFAULT_FMINTERVAL ((((6 * (FRAMEINTERVAL - 210)) / 7) << 16) | FRAMEINTERVAL)
halfpitch 0:a6476c138e84 835
halfpitch 0:a6476c138e84 836 void DelayMS(int ms)
halfpitch 0:a6476c138e84 837 {
halfpitch 0:a6476c138e84 838 u16 f = ms + CommunicationArea.FrameNumber;
halfpitch 0:a6476c138e84 839 while (f != CommunicationArea.FrameNumber)
halfpitch 0:a6476c138e84 840 ;
halfpitch 0:a6476c138e84 841 }
halfpitch 0:a6476c138e84 842
halfpitch 0:a6476c138e84 843 static void HWInit(HCCA* cca)
halfpitch 0:a6476c138e84 844 {
halfpitch 0:a6476c138e84 845 NVIC_DisableIRQ(USB_IRQn);
halfpitch 0:a6476c138e84 846
halfpitch 0:a6476c138e84 847 // turn on power for USB
halfpitch 0:a6476c138e84 848 LPC_SC->PCONP |= (1UL<<31);
halfpitch 0:a6476c138e84 849 // Enable USB host clock, port selection and AHB clock
halfpitch 0:a6476c138e84 850 LPC_USB->USBClkCtrl |= CLOCK_MASK;
halfpitch 0:a6476c138e84 851 // Wait for clocks to become available
halfpitch 0:a6476c138e84 852 while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK)
halfpitch 0:a6476c138e84 853 ;
halfpitch 0:a6476c138e84 854
halfpitch 0:a6476c138e84 855 // We are a Host
halfpitch 0:a6476c138e84 856 LPC_USB->OTGStCtrl |= 1;
halfpitch 0:a6476c138e84 857 LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; // we don't need port selection clock until we do OTG
halfpitch 0:a6476c138e84 858
halfpitch 0:a6476c138e84 859 // configure USB pins
halfpitch 0:a6476c138e84 860 LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28));
halfpitch 0:a6476c138e84 861 LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // USB D+/D-
halfpitch 0:a6476c138e84 862
halfpitch 0:a6476c138e84 863 LPC_PINCON->PINSEL3 &= ~((3 << 6) | (3 << 22)); // USB_PPWR, USB_OVRCR
halfpitch 0:a6476c138e84 864 LPC_PINCON->PINSEL3 |= ((2 << 6) | (2 << 22));
halfpitch 0:a6476c138e84 865
halfpitch 0:a6476c138e84 866 LPC_PINCON->PINSEL4 &= ~(3 << 18); // USB_CONNECT
halfpitch 0:a6476c138e84 867 LPC_PINCON->PINSEL4 |= (1 << 18);
halfpitch 0:a6476c138e84 868
halfpitch 0:a6476c138e84 869 // Reset OHCI block
halfpitch 0:a6476c138e84 870 LPC_USB->HcControl = 0;
halfpitch 0:a6476c138e84 871 LPC_USB->HcControlHeadED = 0;
halfpitch 0:a6476c138e84 872 LPC_USB->HcBulkHeadED = 0;
halfpitch 0:a6476c138e84 873
halfpitch 0:a6476c138e84 874 LPC_USB->HcCommandStatus = HostControllerReset;
halfpitch 0:a6476c138e84 875 LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL;
halfpitch 0:a6476c138e84 876 LPC_USB->HcPeriodicStart = FRAMEINTERVAL*90/100;
halfpitch 0:a6476c138e84 877
halfpitch 0:a6476c138e84 878 LPC_USB->HcControl = (LPC_USB->HcControl & (~HostControllerFunctionalState)) | OperationalMask;
halfpitch 0:a6476c138e84 879 LPC_USB->HcRhStatus = SetGlobalPower;
halfpitch 0:a6476c138e84 880
halfpitch 0:a6476c138e84 881 LPC_USB->HcHCCA = (u32)cca;
halfpitch 0:a6476c138e84 882 LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus;
halfpitch 0:a6476c138e84 883 LPC_USB->HcInterruptEnable = MasterInterruptEnable | WritebackDoneHead | RootHubStatusChange | FrameNumberOverflow;
halfpitch 0:a6476c138e84 884
halfpitch 0:a6476c138e84 885 NVIC_SetPriority(USB_IRQn, 0);
halfpitch 0:a6476c138e84 886 NVIC_EnableIRQ(USB_IRQn);
halfpitch 0:a6476c138e84 887 while (cca->FrameNumber < 10)
halfpitch 0:a6476c138e84 888 ; // 10ms delay before diving in
halfpitch 0:a6476c138e84 889 }
halfpitch 0:a6476c138e84 890 };
halfpitch 0:a6476c138e84 891
halfpitch 0:a6476c138e84 892 //====================================================================================
halfpitch 0:a6476c138e84 893 //====================================================================================
halfpitch 0:a6476c138e84 894 // Host controller instance and Interrupt handler
halfpitch 0:a6476c138e84 895
halfpitch 0:a6476c138e84 896 static HostController _controller __attribute__((at(USB_RAM_BASE)));
halfpitch 0:a6476c138e84 897
halfpitch 0:a6476c138e84 898 extern "C" void USB_IRQHandler(void) __irq;
halfpitch 0:a6476c138e84 899 void USB_IRQHandler (void) __irq
halfpitch 0:a6476c138e84 900 {
halfpitch 0:a6476c138e84 901 u32 int_status = LPC_USB->HcInterruptStatus;
halfpitch 0:a6476c138e84 902
halfpitch 0:a6476c138e84 903 if (int_status & RootHubStatusChange) // Root hub status change
halfpitch 0:a6476c138e84 904 _controller._rootHubStatusChange++; // Just flag the controller, will be processed in USBLoop
halfpitch 0:a6476c138e84 905
halfpitch 0:a6476c138e84 906 u32 head = 0;
halfpitch 0:a6476c138e84 907 if (int_status & WritebackDoneHead)
halfpitch 0:a6476c138e84 908 {
halfpitch 0:a6476c138e84 909 head = _controller.CommunicationArea.DoneHead; // Writeback Done
halfpitch 0:a6476c138e84 910 _controller.CommunicationArea.DoneHead = 0;
halfpitch 0:a6476c138e84 911 }
halfpitch 0:a6476c138e84 912 LPC_USB->HcInterruptStatus = int_status;
halfpitch 0:a6476c138e84 913
halfpitch 0:a6476c138e84 914 if (head)
halfpitch 0:a6476c138e84 915 _controller.ProcessDoneQueue(head); // TODO - low bit can be set BUGBUG
halfpitch 0:a6476c138e84 916 }
halfpitch 0:a6476c138e84 917
halfpitch 0:a6476c138e84 918 //====================================================================================
halfpitch 0:a6476c138e84 919 //====================================================================================
halfpitch 0:a6476c138e84 920 // API Methods
halfpitch 0:a6476c138e84 921
halfpitch 0:a6476c138e84 922 void USBInit()
halfpitch 0:a6476c138e84 923 {
halfpitch 0:a6476c138e84 924 return _controller.Init();
halfpitch 0:a6476c138e84 925 }
halfpitch 0:a6476c138e84 926
halfpitch 0:a6476c138e84 927 void USBLoop()
halfpitch 0:a6476c138e84 928 {
halfpitch 0:a6476c138e84 929 return _controller.Loop();
halfpitch 0:a6476c138e84 930 }
halfpitch 0:a6476c138e84 931
halfpitch 0:a6476c138e84 932 u8* USBGetBuffer(u32* len)
halfpitch 0:a6476c138e84 933 {
halfpitch 0:a6476c138e84 934 *len = USB_RAM_SIZE - sizeof(HostController);
halfpitch 0:a6476c138e84 935 return _controller.SRAM;
halfpitch 0:a6476c138e84 936 }
halfpitch 0:a6476c138e84 937
halfpitch 0:a6476c138e84 938 static Setup* GetSetup(int device)
halfpitch 0:a6476c138e84 939 {
halfpitch 0:a6476c138e84 940 if (device == 0)
halfpitch 0:a6476c138e84 941 return &_controller._setupZero;
halfpitch 0:a6476c138e84 942
halfpitch 0:a6476c138e84 943 if (device < 1 || device > MAX_DEVICES)
halfpitch 0:a6476c138e84 944 return 0;
halfpitch 0:a6476c138e84 945 return &_controller.Devices[device-1].SetupBuffer;
halfpitch 0:a6476c138e84 946 }
halfpitch 0:a6476c138e84 947
halfpitch 0:a6476c138e84 948 // Loop until IO on endpoint is complete
halfpitch 0:a6476c138e84 949 static int WaitIODone(Endpoint* endpoint)
halfpitch 0:a6476c138e84 950 {
halfpitch 0:a6476c138e84 951 if (endpoint->CurrentState == Endpoint::NotQueued)
halfpitch 0:a6476c138e84 952 return 0;
halfpitch 0:a6476c138e84 953 while (endpoint->CurrentState != Endpoint::Idle)
halfpitch 0:a6476c138e84 954 USBLoop(); // May generate callbacks, mount or unmount devices etc
halfpitch 0:a6476c138e84 955 int status = endpoint->Status();
halfpitch 0:a6476c138e84 956 if (status == 0)
halfpitch 0:a6476c138e84 957 return endpoint->Length;
halfpitch 0:a6476c138e84 958 return -status;
halfpitch 0:a6476c138e84 959 }
halfpitch 0:a6476c138e84 960
halfpitch 0:a6476c138e84 961 int USBTransfer(int device, int ep, u8 flags, u8* data, int length, USBCallback callback, void* userData)
halfpitch 0:a6476c138e84 962 {
halfpitch 0:a6476c138e84 963 Endpoint* endpoint = _controller.GetEndpoint(device,ep);
halfpitch 0:a6476c138e84 964 if (!endpoint)
halfpitch 0:a6476c138e84 965 return ERR_ENDPOINT_NOT_FOUND;
halfpitch 0:a6476c138e84 966
halfpitch 0:a6476c138e84 967 WaitIODone(endpoint);
halfpitch 0:a6476c138e84 968 endpoint->Flags = flags;
halfpitch 0:a6476c138e84 969 endpoint->Data = data;
halfpitch 0:a6476c138e84 970 endpoint->Length = length;
halfpitch 0:a6476c138e84 971 endpoint->Callback = callback;
halfpitch 0:a6476c138e84 972 endpoint->UserData = userData;
halfpitch 0:a6476c138e84 973 if (ep == 0)
halfpitch 0:a6476c138e84 974 _controller.Transfer(endpoint,TOKEN_SETUP,(u8*)GetSetup(device),8,Endpoint::SetupQueued);
halfpitch 0:a6476c138e84 975 else
halfpitch 0:a6476c138e84 976 _controller.Transfer(endpoint,flags & 0x80 ? TOKEN_IN : TOKEN_OUT,data,length,Endpoint::DataQueued);
halfpitch 0:a6476c138e84 977 if (callback)
halfpitch 0:a6476c138e84 978 return IO_PENDING;
halfpitch 0:a6476c138e84 979 return WaitIODone(endpoint);
halfpitch 0:a6476c138e84 980 }
halfpitch 0:a6476c138e84 981
halfpitch 0:a6476c138e84 982 int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback, void * userData)
halfpitch 0:a6476c138e84 983 {
halfpitch 0:a6476c138e84 984 Setup* setup = GetSetup(device);
halfpitch 0:a6476c138e84 985 if (!setup)
halfpitch 0:a6476c138e84 986 return ERR_DEVICE_NOT_FOUND;
halfpitch 0:a6476c138e84 987
halfpitch 0:a6476c138e84 988 // Async control calls may overwrite setup buffer of previous call, so we need to wait before setting up next call
halfpitch 0:a6476c138e84 989 WaitIODone(_controller.GetEndpoint(device,0));
halfpitch 0:a6476c138e84 990
halfpitch 0:a6476c138e84 991 setup->bm_request_type = request_type;
halfpitch 0:a6476c138e84 992 setup->b_request = request;
halfpitch 0:a6476c138e84 993 setup->w_value = value;
halfpitch 0:a6476c138e84 994 setup->w_index = index;
halfpitch 0:a6476c138e84 995 setup->w_length = length;
halfpitch 0:a6476c138e84 996 return USBTransfer(device,0,request_type & DEVICE_TO_HOST,data,length,callback,userData);
halfpitch 0:a6476c138e84 997 }
halfpitch 0:a6476c138e84 998
halfpitch 0:a6476c138e84 999 int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
halfpitch 0:a6476c138e84 1000 {
halfpitch 0:a6476c138e84 1001 return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_INTERRUPT,data,length,callback,userData);
halfpitch 0:a6476c138e84 1002 }
halfpitch 0:a6476c138e84 1003
halfpitch 0:a6476c138e84 1004 int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
halfpitch 0:a6476c138e84 1005 {
halfpitch 0:a6476c138e84 1006 return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_BULK,data,length,callback,userData);
halfpitch 0:a6476c138e84 1007 }
halfpitch 0:a6476c138e84 1008
halfpitch 0:a6476c138e84 1009 int GetDescriptor(int device, int descType,int descIndex, u8* data, int length)
halfpitch 0:a6476c138e84 1010 {
halfpitch 0:a6476c138e84 1011 return USBControlTransfer(device,DEVICE_TO_HOST | RECIPIENT_DEVICE, GET_DESCRIPTOR,(descType << 8)|(descIndex), 0, data, length, 0);
halfpitch 0:a6476c138e84 1012 }
halfpitch 0:a6476c138e84 1013
halfpitch 0:a6476c138e84 1014 int GetString(int device, int index, char* dst, int length)
halfpitch 0:a6476c138e84 1015 {
halfpitch 0:a6476c138e84 1016 u8 buffer[255];
halfpitch 0:a6476c138e84 1017 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,index,buffer,sizeof(buffer));
halfpitch 0:a6476c138e84 1018 if (le < 0)
halfpitch 0:a6476c138e84 1019 return le;
halfpitch 0:a6476c138e84 1020 if (length < 1)
halfpitch 0:a6476c138e84 1021 return -1;
halfpitch 0:a6476c138e84 1022 length <<= 1;
halfpitch 0:a6476c138e84 1023 if (le > length)
halfpitch 0:a6476c138e84 1024 le = length;
halfpitch 0:a6476c138e84 1025 for (int j = 2; j < le; j += 2)
halfpitch 0:a6476c138e84 1026 *dst++ = buffer[j];
halfpitch 0:a6476c138e84 1027 *dst = 0;
halfpitch 0:a6476c138e84 1028 return (le>>1)-1;
halfpitch 0:a6476c138e84 1029 }
halfpitch 0:a6476c138e84 1030
halfpitch 0:a6476c138e84 1031 int SetAddress(int device, int new_addr)
halfpitch 0:a6476c138e84 1032 {
halfpitch 0:a6476c138e84 1033 return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_ADDRESS, new_addr, 0, 0, 0, 0);
halfpitch 0:a6476c138e84 1034 }
halfpitch 0:a6476c138e84 1035
halfpitch 0:a6476c138e84 1036 int SetConfiguration(int device, int configNum)
halfpitch 0:a6476c138e84 1037 {
halfpitch 0:a6476c138e84 1038 return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_CONFIGURATION, configNum, 0, 0, 0, 0);
halfpitch 0:a6476c138e84 1039 }
halfpitch 0:a6476c138e84 1040
halfpitch 0:a6476c138e84 1041 int SetInterface(int device, int ifNum, int altNum)
halfpitch 0:a6476c138e84 1042 {
halfpitch 0:a6476c138e84 1043 return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_INTERFACE, SET_INTERFACE, altNum, ifNum, 0, 0, 0);
halfpitch 0:a6476c138e84 1044 }
halfpitch 0:a6476c138e84 1045
halfpitch 0:a6476c138e84 1046 // HUB stuff
halfpitch 0:a6476c138e84 1047 int SetPortFeature(int device, int feature, int index)
halfpitch 0:a6476c138e84 1048 {
halfpitch 0:a6476c138e84 1049 return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,SET_FEATURE,feature,index,0,0);
halfpitch 0:a6476c138e84 1050 }
halfpitch 0:a6476c138e84 1051
halfpitch 0:a6476c138e84 1052 int ClearPortFeature(int device, int feature, int index)
halfpitch 0:a6476c138e84 1053 {
halfpitch 0:a6476c138e84 1054 return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,CLEAR_FEATURE,feature,index,0,0);
halfpitch 0:a6476c138e84 1055 }
halfpitch 0:a6476c138e84 1056
halfpitch 0:a6476c138e84 1057 int SetPortPower(int device, int port)
halfpitch 0:a6476c138e84 1058 {
halfpitch 0:a6476c138e84 1059 int r = SetPortFeature(device,PORT_POWER,port);
halfpitch 0:a6476c138e84 1060 _controller.DelayMS(20); // 80ms to turn on a hubs power... DESCRIPTOR? todo
halfpitch 0:a6476c138e84 1061 return r;
halfpitch 0:a6476c138e84 1062 }
halfpitch 0:a6476c138e84 1063
halfpitch 0:a6476c138e84 1064 int SetPortReset(int device, int port)
halfpitch 0:a6476c138e84 1065 {
halfpitch 0:a6476c138e84 1066 return SetPortFeature(device,PORT_RESET,port);
halfpitch 0:a6476c138e84 1067 }
halfpitch 0:a6476c138e84 1068
halfpitch 0:a6476c138e84 1069 int GetPortStatus(int device, int port, u32* status)
halfpitch 0:a6476c138e84 1070 {
halfpitch 0:a6476c138e84 1071 return USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,GET_STATUS,0,port,(u8*)status,4);
halfpitch 0:a6476c138e84 1072 }