This program is for an autonomous robot for the competition at the Hochschule Luzern. We are one of the 32 teams. <a href="http://cruisingcrepe.wordpress.com/">http://cruisingcrepe.wordpress.com/</a> 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: <a href="http://www.dis.uniroma1.it/~labrob/pub/papers/Ramsete01.pdf">http://www.dis.uniroma1.it/~labrob/pub/papers/Ramsete01.pdf</a>

Dependencies:   mbed

Fork of autonomousRobotAndroid by Christian Burri

Committer:
chrigelburri
Date:
Fri May 03 06:30:46 2013 +0000
Revision:
17:f0a973f17917
bastel mit adb

Who changed what in which revision?

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