BlueUSB with USB-> SERIAL (CP210x) GPIO pins working.

Dependencies:   mbed

Committer:
tecnosys
Date:
Fri Apr 23 05:04:28 2010 +0000
Revision:
0:a14eaa2e1445

        

Who changed what in which revision?

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