手動機アーム、mbed基盤のspiをおくるだけのプログラムです(9/4)

Dependencies:   SPI_master_arm_shudouki mbed

Fork of SPI_master_arm_shudouki2 by F^3 RC 2班

Committer:
yoka06
Date:
Mon Aug 21 08:49:06 2017 +0000
Revision:
0:76d1c7f13415
a

Who changed what in which revision?

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