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