Fork BlueUSB lib for NHK robot contest 2014 TEU JAPAN

Dependencies:   C12832_lcd FatFileSystem TextLCD mbed

Fork of PS3_BlueUSB by Bart Janssens

Committer:
BartJanssens
Date:
Tue Apr 26 16:09:17 2011 +0000
Revision:
0:99a111b75cb4

        

Who changed what in which revision?

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