This program is for an autonomous robot for the competition at the Hochschule Luzern. http://cruisingcrepe.wordpress.com/ We are one of the 32 teams. http://cruisingcrepe.wordpress.com/ The postition control is based on this Documentation: Control of Wheeled Mobile Robots: An Experimental Overview from Alessandro De Luca, Giuseppe Oriolo, Marilena Vendittelli. For more information see here: http://www.dis.uniroma1.it/~labrob/pub/papers/Ramsete01.pdf

Dependencies:   mbed

Fork of autonomous Robot Android by Christian Burri

Committer:
chrigelburri
Date:
Mon Jun 10 14:40:37 2013 +0000
Revision:
39:a4fd6206da89
Parent:
18:306d362d692b
V1.0

Who changed what in which revision?

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