This is the project for the Old Model Robots for OU's Dr. Davis's Configurable Robots Research. This is being published so future robots can be set up easily.

Dependencies:   FatFileSystem MCP3008 Motor PinDetect QTR_8A SRF05 SSD1308_128x64_I2C mbed

Committer:
DrewSchaef
Date:
Wed Nov 01 15:57:59 2017 +0000
Revision:
0:bcad524c1856
Published the project to allow access for future work on the Configurable Robots Research Project(s).

Who changed what in which revision?

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