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