Android Open Accessory Library化してみました。 バルク転送の使い方が分かっていないため、相変わらず動作は不安定。 I do not understand the usage of the bulk transfer. Please teach someone.

Dependencies:   mbed

Committer:
abe00makoto
Date:
Fri Jun 03 17:43:15 2011 +0000
Revision:
0:4fcfc05943ff
Android Open Accessory Library+ demokit

Who changed what in which revision?

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