Android Open Accessory Library化してみました。 バルク転送の使い方が分かっていないため、相変わらず動作は不安定。 I do not understand the usage of the bulk transfer. Please teach someone.

Dependencies:   mbed

Committer:
abe00makoto
Date:
Fri Jun 03 17:43:15 2011 +0000
Revision:
0:4fcfc05943ff
Android Open Accessory Library+ demokit

Who changed what in which revision?

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