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