Kalibriersoftware Stromwerte

Dependencies:   Matrix mbed

Committer:
Racer01014
Date:
Sat Nov 28 13:50:31 2015 +0000
Revision:
1:ea5f520dcdc1
Parent:
0:5e35c180ed4a
-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Racer01014 0:5e35c180ed4a 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Racer01014 0:5e35c180ed4a 2 *
Racer01014 0:5e35c180ed4a 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Racer01014 0:5e35c180ed4a 4 * and associated documentation files (the "Software"), to deal in the Software without
Racer01014 0:5e35c180ed4a 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Racer01014 0:5e35c180ed4a 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Racer01014 0:5e35c180ed4a 7 * Software is furnished to do so, subject to the following conditions:
Racer01014 0:5e35c180ed4a 8 *
Racer01014 0:5e35c180ed4a 9 * The above copyright notice and this permission notice shall be included in all copies or
Racer01014 0:5e35c180ed4a 10 * substantial portions of the Software.
Racer01014 0:5e35c180ed4a 11 *
Racer01014 0:5e35c180ed4a 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Racer01014 0:5e35c180ed4a 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Racer01014 0:5e35c180ed4a 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Racer01014 0:5e35c180ed4a 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Racer01014 0:5e35c180ed4a 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Racer01014 0:5e35c180ed4a 17 */
Racer01014 0:5e35c180ed4a 18
Racer01014 0:5e35c180ed4a 19 #include "stdint.h"
Racer01014 0:5e35c180ed4a 20
Racer01014 0:5e35c180ed4a 21 #include "USBEndpoints.h"
Racer01014 0:5e35c180ed4a 22 #include "USBDevice.h"
Racer01014 0:5e35c180ed4a 23 #include "USBDescriptor.h"
Racer01014 0:5e35c180ed4a 24
Racer01014 0:5e35c180ed4a 25 //#define DEBUG
Racer01014 0:5e35c180ed4a 26
Racer01014 0:5e35c180ed4a 27 /* Device status */
Racer01014 0:5e35c180ed4a 28 #define DEVICE_STATUS_SELF_POWERED (1U<<0)
Racer01014 0:5e35c180ed4a 29 #define DEVICE_STATUS_REMOTE_WAKEUP (1U<<1)
Racer01014 0:5e35c180ed4a 30
Racer01014 0:5e35c180ed4a 31 /* Endpoint status */
Racer01014 0:5e35c180ed4a 32 #define ENDPOINT_STATUS_HALT (1U<<0)
Racer01014 0:5e35c180ed4a 33
Racer01014 0:5e35c180ed4a 34 /* Standard feature selectors */
Racer01014 0:5e35c180ed4a 35 #define DEVICE_REMOTE_WAKEUP (1)
Racer01014 0:5e35c180ed4a 36 #define ENDPOINT_HALT (0)
Racer01014 0:5e35c180ed4a 37
Racer01014 0:5e35c180ed4a 38 /* Macro to convert wIndex endpoint number to physical endpoint number */
Racer01014 0:5e35c180ed4a 39 #define WINDEX_TO_PHYSICAL(endpoint) (((endpoint & 0x0f) << 1) + \
Racer01014 0:5e35c180ed4a 40 ((endpoint & 0x80) ? 1 : 0))
Racer01014 0:5e35c180ed4a 41
Racer01014 0:5e35c180ed4a 42
Racer01014 0:5e35c180ed4a 43 bool USBDevice::requestGetDescriptor(void)
Racer01014 0:5e35c180ed4a 44 {
Racer01014 0:5e35c180ed4a 45 bool success = false;
Racer01014 0:5e35c180ed4a 46 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 47 printf("get descr: type: %d\r\n", DESCRIPTOR_TYPE(transfer.setup.wValue));
Racer01014 0:5e35c180ed4a 48 #endif
Racer01014 0:5e35c180ed4a 49 switch (DESCRIPTOR_TYPE(transfer.setup.wValue))
Racer01014 0:5e35c180ed4a 50 {
Racer01014 0:5e35c180ed4a 51 case DEVICE_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 52 if (deviceDesc() != NULL)
Racer01014 0:5e35c180ed4a 53 {
Racer01014 0:5e35c180ed4a 54 if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \
Racer01014 0:5e35c180ed4a 55 && (deviceDesc()[1] == DEVICE_DESCRIPTOR))
Racer01014 0:5e35c180ed4a 56 {
Racer01014 0:5e35c180ed4a 57 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 58 printf("device descr\r\n");
Racer01014 0:5e35c180ed4a 59 #endif
Racer01014 0:5e35c180ed4a 60 transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
Racer01014 0:5e35c180ed4a 61 transfer.ptr = deviceDesc();
Racer01014 0:5e35c180ed4a 62 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 63 success = true;
Racer01014 0:5e35c180ed4a 64 }
Racer01014 0:5e35c180ed4a 65 }
Racer01014 0:5e35c180ed4a 66 break;
Racer01014 0:5e35c180ed4a 67 case CONFIGURATION_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 68 if (configurationDesc() != NULL)
Racer01014 0:5e35c180ed4a 69 {
Racer01014 0:5e35c180ed4a 70 if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH) \
Racer01014 0:5e35c180ed4a 71 && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR))
Racer01014 0:5e35c180ed4a 72 {
Racer01014 0:5e35c180ed4a 73 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 74 printf("conf descr request\r\n");
Racer01014 0:5e35c180ed4a 75 #endif
Racer01014 0:5e35c180ed4a 76 /* Get wTotalLength */
Racer01014 0:5e35c180ed4a 77 transfer.remaining = configurationDesc()[2] \
Racer01014 0:5e35c180ed4a 78 | (configurationDesc()[3] << 8);
Racer01014 0:5e35c180ed4a 79
Racer01014 0:5e35c180ed4a 80 transfer.ptr = configurationDesc();
Racer01014 0:5e35c180ed4a 81 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 82 success = true;
Racer01014 0:5e35c180ed4a 83 }
Racer01014 0:5e35c180ed4a 84 }
Racer01014 0:5e35c180ed4a 85 break;
Racer01014 0:5e35c180ed4a 86 case STRING_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 87 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 88 printf("str descriptor\r\n");
Racer01014 0:5e35c180ed4a 89 #endif
Racer01014 0:5e35c180ed4a 90 switch (DESCRIPTOR_INDEX(transfer.setup.wValue))
Racer01014 0:5e35c180ed4a 91 {
Racer01014 0:5e35c180ed4a 92 case STRING_OFFSET_LANGID:
Racer01014 0:5e35c180ed4a 93 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 94 printf("1\r\n");
Racer01014 0:5e35c180ed4a 95 #endif
Racer01014 0:5e35c180ed4a 96 transfer.remaining = stringLangidDesc()[0];
Racer01014 0:5e35c180ed4a 97 transfer.ptr = stringLangidDesc();
Racer01014 0:5e35c180ed4a 98 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 99 success = true;
Racer01014 0:5e35c180ed4a 100 break;
Racer01014 0:5e35c180ed4a 101 case STRING_OFFSET_IMANUFACTURER:
Racer01014 0:5e35c180ed4a 102 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 103 printf("2\r\n");
Racer01014 0:5e35c180ed4a 104 #endif
Racer01014 0:5e35c180ed4a 105 transfer.remaining = stringImanufacturerDesc()[0];
Racer01014 0:5e35c180ed4a 106 transfer.ptr = stringImanufacturerDesc();
Racer01014 0:5e35c180ed4a 107 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 108 success = true;
Racer01014 0:5e35c180ed4a 109 break;
Racer01014 0:5e35c180ed4a 110 case STRING_OFFSET_IPRODUCT:
Racer01014 0:5e35c180ed4a 111 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 112 printf("3\r\n");
Racer01014 0:5e35c180ed4a 113 #endif
Racer01014 0:5e35c180ed4a 114 transfer.remaining = stringIproductDesc()[0];
Racer01014 0:5e35c180ed4a 115 transfer.ptr = stringIproductDesc();
Racer01014 0:5e35c180ed4a 116 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 117 success = true;
Racer01014 0:5e35c180ed4a 118 break;
Racer01014 0:5e35c180ed4a 119 case STRING_OFFSET_ISERIAL:
Racer01014 0:5e35c180ed4a 120 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 121 printf("4\r\n");
Racer01014 0:5e35c180ed4a 122 #endif
Racer01014 0:5e35c180ed4a 123 transfer.remaining = stringIserialDesc()[0];
Racer01014 0:5e35c180ed4a 124 transfer.ptr = stringIserialDesc();
Racer01014 0:5e35c180ed4a 125 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 126 success = true;
Racer01014 0:5e35c180ed4a 127 break;
Racer01014 0:5e35c180ed4a 128 case STRING_OFFSET_ICONFIGURATION:
Racer01014 0:5e35c180ed4a 129 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 130 printf("5\r\n");
Racer01014 0:5e35c180ed4a 131 #endif
Racer01014 0:5e35c180ed4a 132 transfer.remaining = stringIConfigurationDesc()[0];
Racer01014 0:5e35c180ed4a 133 transfer.ptr = stringIConfigurationDesc();
Racer01014 0:5e35c180ed4a 134 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 135 success = true;
Racer01014 0:5e35c180ed4a 136 break;
Racer01014 0:5e35c180ed4a 137 case STRING_OFFSET_IINTERFACE:
Racer01014 0:5e35c180ed4a 138 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 139 printf("6\r\n");
Racer01014 0:5e35c180ed4a 140 #endif
Racer01014 0:5e35c180ed4a 141 transfer.remaining = stringIinterfaceDesc()[0];
Racer01014 0:5e35c180ed4a 142 transfer.ptr = stringIinterfaceDesc();
Racer01014 0:5e35c180ed4a 143 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 144 success = true;
Racer01014 0:5e35c180ed4a 145 break;
Racer01014 0:5e35c180ed4a 146 }
Racer01014 0:5e35c180ed4a 147 break;
Racer01014 0:5e35c180ed4a 148 case INTERFACE_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 149 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 150 printf("interface descr\r\n");
Racer01014 0:5e35c180ed4a 151 #endif
Racer01014 0:5e35c180ed4a 152 case ENDPOINT_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 153 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 154 printf("endpoint descr\r\n");
Racer01014 0:5e35c180ed4a 155 #endif
Racer01014 0:5e35c180ed4a 156 /* TODO: Support is optional, not implemented here */
Racer01014 0:5e35c180ed4a 157 break;
Racer01014 0:5e35c180ed4a 158 default:
Racer01014 0:5e35c180ed4a 159 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 160 printf("ERROR\r\n");
Racer01014 0:5e35c180ed4a 161 #endif
Racer01014 0:5e35c180ed4a 162 break;
Racer01014 0:5e35c180ed4a 163 }
Racer01014 0:5e35c180ed4a 164
Racer01014 0:5e35c180ed4a 165 return success;
Racer01014 0:5e35c180ed4a 166 }
Racer01014 0:5e35c180ed4a 167
Racer01014 0:5e35c180ed4a 168 void USBDevice::decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet)
Racer01014 0:5e35c180ed4a 169 {
Racer01014 0:5e35c180ed4a 170 /* Fill in the elements of a SETUP_PACKET structure from raw data */
Racer01014 0:5e35c180ed4a 171 packet->bmRequestType.dataTransferDirection = (data[0] & 0x80) >> 7;
Racer01014 0:5e35c180ed4a 172 packet->bmRequestType.Type = (data[0] & 0x60) >> 5;
Racer01014 0:5e35c180ed4a 173 packet->bmRequestType.Recipient = data[0] & 0x1f;
Racer01014 0:5e35c180ed4a 174 packet->bRequest = data[1];
Racer01014 0:5e35c180ed4a 175 packet->wValue = (data[2] | (uint16_t)data[3] << 8);
Racer01014 0:5e35c180ed4a 176 packet->wIndex = (data[4] | (uint16_t)data[5] << 8);
Racer01014 0:5e35c180ed4a 177 packet->wLength = (data[6] | (uint16_t)data[7] << 8);
Racer01014 0:5e35c180ed4a 178 }
Racer01014 0:5e35c180ed4a 179
Racer01014 0:5e35c180ed4a 180
Racer01014 0:5e35c180ed4a 181 bool USBDevice::controlOut(void)
Racer01014 0:5e35c180ed4a 182 {
Racer01014 0:5e35c180ed4a 183 /* Control transfer data OUT stage */
Racer01014 0:5e35c180ed4a 184 uint8_t buffer[MAX_PACKET_SIZE_EP0];
Racer01014 0:5e35c180ed4a 185 uint32_t packetSize;
Racer01014 0:5e35c180ed4a 186
Racer01014 0:5e35c180ed4a 187 /* Check we should be transferring data OUT */
Racer01014 0:5e35c180ed4a 188 if (transfer.direction != HOST_TO_DEVICE)
Racer01014 0:5e35c180ed4a 189 {
Racer01014 0:5e35c180ed4a 190 return false;
Racer01014 0:5e35c180ed4a 191 }
Racer01014 0:5e35c180ed4a 192
Racer01014 0:5e35c180ed4a 193 /* Read from endpoint */
Racer01014 0:5e35c180ed4a 194 packetSize = EP0getReadResult(buffer);
Racer01014 0:5e35c180ed4a 195
Racer01014 0:5e35c180ed4a 196 /* Check if transfer size is valid */
Racer01014 0:5e35c180ed4a 197 if (packetSize > transfer.remaining)
Racer01014 0:5e35c180ed4a 198 {
Racer01014 0:5e35c180ed4a 199 /* Too big */
Racer01014 0:5e35c180ed4a 200 return false;
Racer01014 0:5e35c180ed4a 201 }
Racer01014 0:5e35c180ed4a 202
Racer01014 0:5e35c180ed4a 203 /* Update transfer */
Racer01014 0:5e35c180ed4a 204 transfer.ptr += packetSize;
Racer01014 0:5e35c180ed4a 205 transfer.remaining -= packetSize;
Racer01014 0:5e35c180ed4a 206
Racer01014 0:5e35c180ed4a 207 /* Check if transfer has completed */
Racer01014 0:5e35c180ed4a 208 if (transfer.remaining == 0)
Racer01014 0:5e35c180ed4a 209 {
Racer01014 0:5e35c180ed4a 210 /* Transfer completed */
Racer01014 0:5e35c180ed4a 211 if (transfer.notify)
Racer01014 0:5e35c180ed4a 212 {
Racer01014 0:5e35c180ed4a 213 /* Notify class layer. */
Racer01014 0:5e35c180ed4a 214 USBCallback_requestCompleted(buffer, packetSize);
Racer01014 0:5e35c180ed4a 215 transfer.notify = false;
Racer01014 0:5e35c180ed4a 216 }
Racer01014 0:5e35c180ed4a 217 /* Status stage */
Racer01014 0:5e35c180ed4a 218 EP0write(NULL, 0);
Racer01014 0:5e35c180ed4a 219 }
Racer01014 0:5e35c180ed4a 220 else
Racer01014 0:5e35c180ed4a 221 {
Racer01014 0:5e35c180ed4a 222 EP0read();
Racer01014 0:5e35c180ed4a 223 }
Racer01014 0:5e35c180ed4a 224
Racer01014 0:5e35c180ed4a 225 return true;
Racer01014 0:5e35c180ed4a 226 }
Racer01014 0:5e35c180ed4a 227
Racer01014 0:5e35c180ed4a 228 bool USBDevice::controlIn(void)
Racer01014 0:5e35c180ed4a 229 {
Racer01014 0:5e35c180ed4a 230 /* Control transfer data IN stage */
Racer01014 0:5e35c180ed4a 231 uint32_t packetSize;
Racer01014 0:5e35c180ed4a 232
Racer01014 0:5e35c180ed4a 233 /* Check if transfer has completed (status stage transactions */
Racer01014 0:5e35c180ed4a 234 /* also have transfer.remaining == 0) */
Racer01014 0:5e35c180ed4a 235 if (transfer.remaining == 0)
Racer01014 0:5e35c180ed4a 236 {
Racer01014 0:5e35c180ed4a 237 if (transfer.zlp)
Racer01014 0:5e35c180ed4a 238 {
Racer01014 0:5e35c180ed4a 239 /* Send zero length packet */
Racer01014 0:5e35c180ed4a 240 EP0write(NULL, 0);
Racer01014 0:5e35c180ed4a 241 transfer.zlp = false;
Racer01014 0:5e35c180ed4a 242 }
Racer01014 0:5e35c180ed4a 243
Racer01014 0:5e35c180ed4a 244 /* Transfer completed */
Racer01014 0:5e35c180ed4a 245 if (transfer.notify)
Racer01014 0:5e35c180ed4a 246 {
Racer01014 0:5e35c180ed4a 247 /* Notify class layer. */
Racer01014 0:5e35c180ed4a 248 USBCallback_requestCompleted(NULL, 0);
Racer01014 0:5e35c180ed4a 249 transfer.notify = false;
Racer01014 0:5e35c180ed4a 250 }
Racer01014 0:5e35c180ed4a 251
Racer01014 0:5e35c180ed4a 252 EP0read();
Racer01014 0:5e35c180ed4a 253 EP0readStage();
Racer01014 0:5e35c180ed4a 254
Racer01014 0:5e35c180ed4a 255 /* Completed */
Racer01014 0:5e35c180ed4a 256 return true;
Racer01014 0:5e35c180ed4a 257 }
Racer01014 0:5e35c180ed4a 258
Racer01014 0:5e35c180ed4a 259 /* Check we should be transferring data IN */
Racer01014 0:5e35c180ed4a 260 if (transfer.direction != DEVICE_TO_HOST)
Racer01014 0:5e35c180ed4a 261 {
Racer01014 0:5e35c180ed4a 262 return false;
Racer01014 0:5e35c180ed4a 263 }
Racer01014 0:5e35c180ed4a 264
Racer01014 0:5e35c180ed4a 265 packetSize = transfer.remaining;
Racer01014 0:5e35c180ed4a 266
Racer01014 0:5e35c180ed4a 267 if (packetSize > MAX_PACKET_SIZE_EP0)
Racer01014 0:5e35c180ed4a 268 {
Racer01014 0:5e35c180ed4a 269 packetSize = MAX_PACKET_SIZE_EP0;
Racer01014 0:5e35c180ed4a 270 }
Racer01014 0:5e35c180ed4a 271
Racer01014 0:5e35c180ed4a 272 /* Write to endpoint */
Racer01014 0:5e35c180ed4a 273 EP0write(transfer.ptr, packetSize);
Racer01014 0:5e35c180ed4a 274
Racer01014 0:5e35c180ed4a 275 /* Update transfer */
Racer01014 0:5e35c180ed4a 276 transfer.ptr += packetSize;
Racer01014 0:5e35c180ed4a 277 transfer.remaining -= packetSize;
Racer01014 0:5e35c180ed4a 278
Racer01014 0:5e35c180ed4a 279 return true;
Racer01014 0:5e35c180ed4a 280 }
Racer01014 0:5e35c180ed4a 281
Racer01014 0:5e35c180ed4a 282 bool USBDevice::requestSetAddress(void)
Racer01014 0:5e35c180ed4a 283 {
Racer01014 0:5e35c180ed4a 284 /* Set the device address */
Racer01014 0:5e35c180ed4a 285 setAddress(transfer.setup.wValue);
Racer01014 0:5e35c180ed4a 286
Racer01014 0:5e35c180ed4a 287 if (transfer.setup.wValue == 0)
Racer01014 0:5e35c180ed4a 288 {
Racer01014 0:5e35c180ed4a 289 device.state = DEFAULT;
Racer01014 0:5e35c180ed4a 290 }
Racer01014 0:5e35c180ed4a 291 else
Racer01014 0:5e35c180ed4a 292 {
Racer01014 0:5e35c180ed4a 293 device.state = ADDRESS;
Racer01014 0:5e35c180ed4a 294 }
Racer01014 0:5e35c180ed4a 295
Racer01014 0:5e35c180ed4a 296 return true;
Racer01014 0:5e35c180ed4a 297 }
Racer01014 0:5e35c180ed4a 298
Racer01014 0:5e35c180ed4a 299 bool USBDevice::requestSetConfiguration(void)
Racer01014 0:5e35c180ed4a 300 {
Racer01014 0:5e35c180ed4a 301
Racer01014 0:5e35c180ed4a 302 device.configuration = transfer.setup.wValue;
Racer01014 0:5e35c180ed4a 303 /* Set the device configuration */
Racer01014 0:5e35c180ed4a 304 if (device.configuration == 0)
Racer01014 0:5e35c180ed4a 305 {
Racer01014 0:5e35c180ed4a 306 /* Not configured */
Racer01014 0:5e35c180ed4a 307 unconfigureDevice();
Racer01014 0:5e35c180ed4a 308 device.state = ADDRESS;
Racer01014 0:5e35c180ed4a 309 }
Racer01014 0:5e35c180ed4a 310 else
Racer01014 0:5e35c180ed4a 311 {
Racer01014 0:5e35c180ed4a 312 if (USBCallback_setConfiguration(device.configuration))
Racer01014 0:5e35c180ed4a 313 {
Racer01014 0:5e35c180ed4a 314 /* Valid configuration */
Racer01014 0:5e35c180ed4a 315 configureDevice();
Racer01014 0:5e35c180ed4a 316 device.state = CONFIGURED;
Racer01014 0:5e35c180ed4a 317 }
Racer01014 0:5e35c180ed4a 318 else
Racer01014 0:5e35c180ed4a 319 {
Racer01014 0:5e35c180ed4a 320 return false;
Racer01014 0:5e35c180ed4a 321 }
Racer01014 0:5e35c180ed4a 322 }
Racer01014 0:5e35c180ed4a 323
Racer01014 0:5e35c180ed4a 324 return true;
Racer01014 0:5e35c180ed4a 325 }
Racer01014 0:5e35c180ed4a 326
Racer01014 0:5e35c180ed4a 327 bool USBDevice::requestGetConfiguration(void)
Racer01014 0:5e35c180ed4a 328 {
Racer01014 0:5e35c180ed4a 329 /* Send the device configuration */
Racer01014 0:5e35c180ed4a 330 transfer.ptr = &device.configuration;
Racer01014 0:5e35c180ed4a 331 transfer.remaining = sizeof(device.configuration);
Racer01014 0:5e35c180ed4a 332 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 333 return true;
Racer01014 0:5e35c180ed4a 334 }
Racer01014 0:5e35c180ed4a 335
Racer01014 0:5e35c180ed4a 336 bool USBDevice::requestGetInterface(void)
Racer01014 0:5e35c180ed4a 337 {
Racer01014 0:5e35c180ed4a 338 /* Return the selected alternate setting for an interface */
Racer01014 0:5e35c180ed4a 339
Racer01014 0:5e35c180ed4a 340 if (device.state != CONFIGURED)
Racer01014 0:5e35c180ed4a 341 {
Racer01014 0:5e35c180ed4a 342 return false;
Racer01014 0:5e35c180ed4a 343 }
Racer01014 0:5e35c180ed4a 344
Racer01014 0:5e35c180ed4a 345 /* Send the alternate setting */
Racer01014 0:5e35c180ed4a 346 transfer.setup.wIndex = currentInterface;
Racer01014 0:5e35c180ed4a 347 transfer.ptr = &currentAlternate;
Racer01014 0:5e35c180ed4a 348 transfer.remaining = sizeof(currentAlternate);
Racer01014 0:5e35c180ed4a 349 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 350 return true;
Racer01014 0:5e35c180ed4a 351 }
Racer01014 0:5e35c180ed4a 352
Racer01014 0:5e35c180ed4a 353 bool USBDevice::requestSetInterface(void)
Racer01014 0:5e35c180ed4a 354 {
Racer01014 0:5e35c180ed4a 355 bool success = false;
Racer01014 0:5e35c180ed4a 356 if(USBCallback_setInterface(transfer.setup.wIndex, transfer.setup.wValue))
Racer01014 0:5e35c180ed4a 357 {
Racer01014 0:5e35c180ed4a 358 success = true;
Racer01014 0:5e35c180ed4a 359 currentInterface = transfer.setup.wIndex;
Racer01014 0:5e35c180ed4a 360 currentAlternate = transfer.setup.wValue;
Racer01014 0:5e35c180ed4a 361 }
Racer01014 0:5e35c180ed4a 362 return success;
Racer01014 0:5e35c180ed4a 363 }
Racer01014 0:5e35c180ed4a 364
Racer01014 0:5e35c180ed4a 365 bool USBDevice::requestSetFeature()
Racer01014 0:5e35c180ed4a 366 {
Racer01014 0:5e35c180ed4a 367 bool success = false;
Racer01014 0:5e35c180ed4a 368
Racer01014 0:5e35c180ed4a 369 if (device.state != CONFIGURED)
Racer01014 0:5e35c180ed4a 370 {
Racer01014 0:5e35c180ed4a 371 /* Endpoint or interface must be zero */
Racer01014 0:5e35c180ed4a 372 if (transfer.setup.wIndex != 0)
Racer01014 0:5e35c180ed4a 373 {
Racer01014 0:5e35c180ed4a 374 return false;
Racer01014 0:5e35c180ed4a 375 }
Racer01014 0:5e35c180ed4a 376 }
Racer01014 0:5e35c180ed4a 377
Racer01014 0:5e35c180ed4a 378 switch (transfer.setup.bmRequestType.Recipient)
Racer01014 0:5e35c180ed4a 379 {
Racer01014 0:5e35c180ed4a 380 case DEVICE_RECIPIENT:
Racer01014 0:5e35c180ed4a 381 /* TODO: Remote wakeup feature not supported */
Racer01014 0:5e35c180ed4a 382 break;
Racer01014 0:5e35c180ed4a 383 case ENDPOINT_RECIPIENT:
Racer01014 0:5e35c180ed4a 384 if (transfer.setup.wValue == ENDPOINT_HALT)
Racer01014 0:5e35c180ed4a 385 {
Racer01014 0:5e35c180ed4a 386 /* TODO: We should check that the endpoint number is valid */
Racer01014 0:5e35c180ed4a 387 stallEndpoint(
Racer01014 0:5e35c180ed4a 388 WINDEX_TO_PHYSICAL(transfer.setup.wIndex));
Racer01014 0:5e35c180ed4a 389 success = true;
Racer01014 0:5e35c180ed4a 390 }
Racer01014 0:5e35c180ed4a 391 break;
Racer01014 0:5e35c180ed4a 392 default:
Racer01014 0:5e35c180ed4a 393 break;
Racer01014 0:5e35c180ed4a 394 }
Racer01014 0:5e35c180ed4a 395
Racer01014 0:5e35c180ed4a 396 return success;
Racer01014 0:5e35c180ed4a 397 }
Racer01014 0:5e35c180ed4a 398
Racer01014 0:5e35c180ed4a 399 bool USBDevice::requestClearFeature()
Racer01014 0:5e35c180ed4a 400 {
Racer01014 0:5e35c180ed4a 401 bool success = false;
Racer01014 0:5e35c180ed4a 402
Racer01014 0:5e35c180ed4a 403 if (device.state != CONFIGURED)
Racer01014 0:5e35c180ed4a 404 {
Racer01014 0:5e35c180ed4a 405 /* Endpoint or interface must be zero */
Racer01014 0:5e35c180ed4a 406 if (transfer.setup.wIndex != 0)
Racer01014 0:5e35c180ed4a 407 {
Racer01014 0:5e35c180ed4a 408 return false;
Racer01014 0:5e35c180ed4a 409 }
Racer01014 0:5e35c180ed4a 410 }
Racer01014 0:5e35c180ed4a 411
Racer01014 0:5e35c180ed4a 412 switch (transfer.setup.bmRequestType.Recipient)
Racer01014 0:5e35c180ed4a 413 {
Racer01014 0:5e35c180ed4a 414 case DEVICE_RECIPIENT:
Racer01014 0:5e35c180ed4a 415 /* TODO: Remote wakeup feature not supported */
Racer01014 0:5e35c180ed4a 416 break;
Racer01014 0:5e35c180ed4a 417 case ENDPOINT_RECIPIENT:
Racer01014 0:5e35c180ed4a 418 /* TODO: We should check that the endpoint number is valid */
Racer01014 0:5e35c180ed4a 419 if (transfer.setup.wValue == ENDPOINT_HALT)
Racer01014 0:5e35c180ed4a 420 {
Racer01014 0:5e35c180ed4a 421 unstallEndpoint( WINDEX_TO_PHYSICAL(transfer.setup.wIndex));
Racer01014 0:5e35c180ed4a 422 success = true;
Racer01014 0:5e35c180ed4a 423 }
Racer01014 0:5e35c180ed4a 424 break;
Racer01014 0:5e35c180ed4a 425 default:
Racer01014 0:5e35c180ed4a 426 break;
Racer01014 0:5e35c180ed4a 427 }
Racer01014 0:5e35c180ed4a 428
Racer01014 0:5e35c180ed4a 429 return success;
Racer01014 0:5e35c180ed4a 430 }
Racer01014 0:5e35c180ed4a 431
Racer01014 0:5e35c180ed4a 432 bool USBDevice::requestGetStatus(void)
Racer01014 0:5e35c180ed4a 433 {
Racer01014 0:5e35c180ed4a 434 static uint16_t status;
Racer01014 0:5e35c180ed4a 435 bool success = false;
Racer01014 0:5e35c180ed4a 436
Racer01014 0:5e35c180ed4a 437 if (device.state != CONFIGURED)
Racer01014 0:5e35c180ed4a 438 {
Racer01014 0:5e35c180ed4a 439 /* Endpoint or interface must be zero */
Racer01014 0:5e35c180ed4a 440 if (transfer.setup.wIndex != 0)
Racer01014 0:5e35c180ed4a 441 {
Racer01014 0:5e35c180ed4a 442 return false;
Racer01014 0:5e35c180ed4a 443 }
Racer01014 0:5e35c180ed4a 444 }
Racer01014 0:5e35c180ed4a 445
Racer01014 0:5e35c180ed4a 446 switch (transfer.setup.bmRequestType.Recipient)
Racer01014 0:5e35c180ed4a 447 {
Racer01014 0:5e35c180ed4a 448 case DEVICE_RECIPIENT:
Racer01014 0:5e35c180ed4a 449 /* TODO: Currently only supports self powered devices */
Racer01014 0:5e35c180ed4a 450 status = DEVICE_STATUS_SELF_POWERED;
Racer01014 0:5e35c180ed4a 451 success = true;
Racer01014 0:5e35c180ed4a 452 break;
Racer01014 0:5e35c180ed4a 453 case INTERFACE_RECIPIENT:
Racer01014 0:5e35c180ed4a 454 status = 0;
Racer01014 0:5e35c180ed4a 455 success = true;
Racer01014 0:5e35c180ed4a 456 break;
Racer01014 0:5e35c180ed4a 457 case ENDPOINT_RECIPIENT:
Racer01014 0:5e35c180ed4a 458 /* TODO: We should check that the endpoint number is valid */
Racer01014 0:5e35c180ed4a 459 if (getEndpointStallState(
Racer01014 0:5e35c180ed4a 460 WINDEX_TO_PHYSICAL(transfer.setup.wIndex)))
Racer01014 0:5e35c180ed4a 461 {
Racer01014 0:5e35c180ed4a 462 status = ENDPOINT_STATUS_HALT;
Racer01014 0:5e35c180ed4a 463 }
Racer01014 0:5e35c180ed4a 464 else
Racer01014 0:5e35c180ed4a 465 {
Racer01014 0:5e35c180ed4a 466 status = 0;
Racer01014 0:5e35c180ed4a 467 }
Racer01014 0:5e35c180ed4a 468 success = true;
Racer01014 0:5e35c180ed4a 469 break;
Racer01014 0:5e35c180ed4a 470 default:
Racer01014 0:5e35c180ed4a 471 break;
Racer01014 0:5e35c180ed4a 472 }
Racer01014 0:5e35c180ed4a 473
Racer01014 0:5e35c180ed4a 474 if (success)
Racer01014 0:5e35c180ed4a 475 {
Racer01014 0:5e35c180ed4a 476 /* Send the status */
Racer01014 0:5e35c180ed4a 477 transfer.ptr = (uint8_t *)&status; /* Assumes little endian */
Racer01014 0:5e35c180ed4a 478 transfer.remaining = sizeof(status);
Racer01014 0:5e35c180ed4a 479 transfer.direction = DEVICE_TO_HOST;
Racer01014 0:5e35c180ed4a 480 }
Racer01014 0:5e35c180ed4a 481
Racer01014 0:5e35c180ed4a 482 return success;
Racer01014 0:5e35c180ed4a 483 }
Racer01014 0:5e35c180ed4a 484
Racer01014 0:5e35c180ed4a 485 bool USBDevice::requestSetup(void)
Racer01014 0:5e35c180ed4a 486 {
Racer01014 0:5e35c180ed4a 487 bool success = false;
Racer01014 0:5e35c180ed4a 488
Racer01014 0:5e35c180ed4a 489 /* Process standard requests */
Racer01014 0:5e35c180ed4a 490 if ((transfer.setup.bmRequestType.Type == STANDARD_TYPE))
Racer01014 0:5e35c180ed4a 491 {
Racer01014 0:5e35c180ed4a 492 switch (transfer.setup.bRequest)
Racer01014 0:5e35c180ed4a 493 {
Racer01014 0:5e35c180ed4a 494 case GET_STATUS:
Racer01014 0:5e35c180ed4a 495 success = requestGetStatus();
Racer01014 0:5e35c180ed4a 496 break;
Racer01014 0:5e35c180ed4a 497 case CLEAR_FEATURE:
Racer01014 0:5e35c180ed4a 498 success = requestClearFeature();
Racer01014 0:5e35c180ed4a 499 break;
Racer01014 0:5e35c180ed4a 500 case SET_FEATURE:
Racer01014 0:5e35c180ed4a 501 success = requestSetFeature();
Racer01014 0:5e35c180ed4a 502 break;
Racer01014 0:5e35c180ed4a 503 case SET_ADDRESS:
Racer01014 0:5e35c180ed4a 504 success = requestSetAddress();
Racer01014 0:5e35c180ed4a 505 break;
Racer01014 0:5e35c180ed4a 506 case GET_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 507 success = requestGetDescriptor();
Racer01014 0:5e35c180ed4a 508 break;
Racer01014 0:5e35c180ed4a 509 case SET_DESCRIPTOR:
Racer01014 0:5e35c180ed4a 510 /* TODO: Support is optional, not implemented here */
Racer01014 0:5e35c180ed4a 511 success = false;
Racer01014 0:5e35c180ed4a 512 break;
Racer01014 0:5e35c180ed4a 513 case GET_CONFIGURATION:
Racer01014 0:5e35c180ed4a 514 success = requestGetConfiguration();
Racer01014 0:5e35c180ed4a 515 break;
Racer01014 0:5e35c180ed4a 516 case SET_CONFIGURATION:
Racer01014 0:5e35c180ed4a 517 success = requestSetConfiguration();
Racer01014 0:5e35c180ed4a 518 break;
Racer01014 0:5e35c180ed4a 519 case GET_INTERFACE:
Racer01014 0:5e35c180ed4a 520 success = requestGetInterface();
Racer01014 0:5e35c180ed4a 521 break;
Racer01014 0:5e35c180ed4a 522 case SET_INTERFACE:
Racer01014 0:5e35c180ed4a 523 success = requestSetInterface();
Racer01014 0:5e35c180ed4a 524 break;
Racer01014 0:5e35c180ed4a 525 default:
Racer01014 0:5e35c180ed4a 526 break;
Racer01014 0:5e35c180ed4a 527 }
Racer01014 0:5e35c180ed4a 528 }
Racer01014 0:5e35c180ed4a 529
Racer01014 0:5e35c180ed4a 530 return success;
Racer01014 0:5e35c180ed4a 531 }
Racer01014 0:5e35c180ed4a 532
Racer01014 0:5e35c180ed4a 533 bool USBDevice::controlSetup(void)
Racer01014 0:5e35c180ed4a 534 {
Racer01014 0:5e35c180ed4a 535 bool success = false;
Racer01014 0:5e35c180ed4a 536
Racer01014 0:5e35c180ed4a 537 /* Control transfer setup stage */
Racer01014 0:5e35c180ed4a 538 uint8_t buffer[MAX_PACKET_SIZE_EP0];
Racer01014 0:5e35c180ed4a 539
Racer01014 0:5e35c180ed4a 540 EP0setup(buffer);
Racer01014 0:5e35c180ed4a 541
Racer01014 0:5e35c180ed4a 542 /* Initialise control transfer state */
Racer01014 0:5e35c180ed4a 543 decodeSetupPacket(buffer, &transfer.setup);
Racer01014 0:5e35c180ed4a 544 transfer.ptr = NULL;
Racer01014 0:5e35c180ed4a 545 transfer.remaining = 0;
Racer01014 0:5e35c180ed4a 546 transfer.direction = 0;
Racer01014 0:5e35c180ed4a 547 transfer.zlp = false;
Racer01014 0:5e35c180ed4a 548 transfer.notify = false;
Racer01014 0:5e35c180ed4a 549
Racer01014 0:5e35c180ed4a 550 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 551 printf("dataTransferDirection: %d\r\nType: %d\r\nRecipient: %d\r\nbRequest: %d\r\nwValue: %d\r\nwIndex: %d\r\nwLength: %d\r\n",transfer.setup.bmRequestType.dataTransferDirection,
Racer01014 0:5e35c180ed4a 552 transfer.setup.bmRequestType.Type,
Racer01014 0:5e35c180ed4a 553 transfer.setup.bmRequestType.Recipient,
Racer01014 0:5e35c180ed4a 554 transfer.setup.bRequest,
Racer01014 0:5e35c180ed4a 555 transfer.setup.wValue,
Racer01014 0:5e35c180ed4a 556 transfer.setup.wIndex,
Racer01014 0:5e35c180ed4a 557 transfer.setup.wLength);
Racer01014 0:5e35c180ed4a 558 #endif
Racer01014 0:5e35c180ed4a 559
Racer01014 0:5e35c180ed4a 560 /* Class / vendor specific */
Racer01014 0:5e35c180ed4a 561 success = USBCallback_request();
Racer01014 0:5e35c180ed4a 562
Racer01014 0:5e35c180ed4a 563 if (!success)
Racer01014 0:5e35c180ed4a 564 {
Racer01014 0:5e35c180ed4a 565 /* Standard requests */
Racer01014 0:5e35c180ed4a 566 if (!requestSetup())
Racer01014 0:5e35c180ed4a 567 {
Racer01014 0:5e35c180ed4a 568 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 569 printf("fail!!!!\r\n");
Racer01014 0:5e35c180ed4a 570 #endif
Racer01014 0:5e35c180ed4a 571 return false;
Racer01014 0:5e35c180ed4a 572 }
Racer01014 0:5e35c180ed4a 573 }
Racer01014 0:5e35c180ed4a 574
Racer01014 0:5e35c180ed4a 575 /* Check transfer size and direction */
Racer01014 0:5e35c180ed4a 576 if (transfer.setup.wLength>0)
Racer01014 0:5e35c180ed4a 577 {
Racer01014 0:5e35c180ed4a 578 if (transfer.setup.bmRequestType.dataTransferDirection \
Racer01014 0:5e35c180ed4a 579 == DEVICE_TO_HOST)
Racer01014 0:5e35c180ed4a 580 {
Racer01014 0:5e35c180ed4a 581 /* IN data stage is required */
Racer01014 0:5e35c180ed4a 582 if (transfer.direction != DEVICE_TO_HOST)
Racer01014 0:5e35c180ed4a 583 {
Racer01014 0:5e35c180ed4a 584 return false;
Racer01014 0:5e35c180ed4a 585 }
Racer01014 0:5e35c180ed4a 586
Racer01014 0:5e35c180ed4a 587 /* Transfer must be less than or equal to the size */
Racer01014 0:5e35c180ed4a 588 /* requested by the host */
Racer01014 0:5e35c180ed4a 589 if (transfer.remaining > transfer.setup.wLength)
Racer01014 0:5e35c180ed4a 590 {
Racer01014 0:5e35c180ed4a 591 transfer.remaining = transfer.setup.wLength;
Racer01014 0:5e35c180ed4a 592 }
Racer01014 0:5e35c180ed4a 593 }
Racer01014 0:5e35c180ed4a 594 else
Racer01014 0:5e35c180ed4a 595 {
Racer01014 0:5e35c180ed4a 596
Racer01014 0:5e35c180ed4a 597 /* OUT data stage is required */
Racer01014 0:5e35c180ed4a 598 if (transfer.direction != HOST_TO_DEVICE)
Racer01014 0:5e35c180ed4a 599 {
Racer01014 0:5e35c180ed4a 600 return false;
Racer01014 0:5e35c180ed4a 601 }
Racer01014 0:5e35c180ed4a 602
Racer01014 0:5e35c180ed4a 603 /* Transfer must be equal to the size requested by the host */
Racer01014 0:5e35c180ed4a 604 if (transfer.remaining != transfer.setup.wLength)
Racer01014 0:5e35c180ed4a 605 {
Racer01014 0:5e35c180ed4a 606 return false;
Racer01014 0:5e35c180ed4a 607 }
Racer01014 0:5e35c180ed4a 608 }
Racer01014 0:5e35c180ed4a 609 }
Racer01014 0:5e35c180ed4a 610 else
Racer01014 0:5e35c180ed4a 611 {
Racer01014 0:5e35c180ed4a 612 /* No data stage; transfer size must be zero */
Racer01014 0:5e35c180ed4a 613 if (transfer.remaining != 0)
Racer01014 0:5e35c180ed4a 614 {
Racer01014 0:5e35c180ed4a 615 return false;
Racer01014 0:5e35c180ed4a 616 }
Racer01014 0:5e35c180ed4a 617 }
Racer01014 0:5e35c180ed4a 618
Racer01014 0:5e35c180ed4a 619 /* Data or status stage if applicable */
Racer01014 0:5e35c180ed4a 620 if (transfer.setup.wLength>0)
Racer01014 0:5e35c180ed4a 621 {
Racer01014 0:5e35c180ed4a 622 if (transfer.setup.bmRequestType.dataTransferDirection \
Racer01014 0:5e35c180ed4a 623 == DEVICE_TO_HOST)
Racer01014 0:5e35c180ed4a 624 {
Racer01014 0:5e35c180ed4a 625 /* Check if we'll need to send a zero length packet at */
Racer01014 0:5e35c180ed4a 626 /* the end of this transfer */
Racer01014 0:5e35c180ed4a 627 if (transfer.setup.wLength > transfer.remaining)
Racer01014 0:5e35c180ed4a 628 {
Racer01014 0:5e35c180ed4a 629 /* Device wishes to transfer less than host requested */
Racer01014 0:5e35c180ed4a 630 if ((transfer.remaining % MAX_PACKET_SIZE_EP0) == 0)
Racer01014 0:5e35c180ed4a 631 {
Racer01014 0:5e35c180ed4a 632 /* Transfer is a multiple of EP0 max packet size */
Racer01014 0:5e35c180ed4a 633 transfer.zlp = true;
Racer01014 0:5e35c180ed4a 634 }
Racer01014 0:5e35c180ed4a 635 }
Racer01014 0:5e35c180ed4a 636
Racer01014 0:5e35c180ed4a 637 /* IN stage */
Racer01014 0:5e35c180ed4a 638 controlIn();
Racer01014 0:5e35c180ed4a 639 }
Racer01014 0:5e35c180ed4a 640 else
Racer01014 0:5e35c180ed4a 641 {
Racer01014 0:5e35c180ed4a 642 /* OUT stage */
Racer01014 0:5e35c180ed4a 643 EP0read();
Racer01014 0:5e35c180ed4a 644 }
Racer01014 0:5e35c180ed4a 645 }
Racer01014 0:5e35c180ed4a 646 else
Racer01014 0:5e35c180ed4a 647 {
Racer01014 0:5e35c180ed4a 648 /* Status stage */
Racer01014 0:5e35c180ed4a 649 EP0write(NULL, 0);
Racer01014 0:5e35c180ed4a 650 }
Racer01014 0:5e35c180ed4a 651
Racer01014 0:5e35c180ed4a 652 return true;
Racer01014 0:5e35c180ed4a 653 }
Racer01014 0:5e35c180ed4a 654
Racer01014 0:5e35c180ed4a 655 void USBDevice::busReset(void)
Racer01014 0:5e35c180ed4a 656 {
Racer01014 0:5e35c180ed4a 657 device.state = DEFAULT;
Racer01014 0:5e35c180ed4a 658 device.configuration = 0;
Racer01014 0:5e35c180ed4a 659 device.suspended = false;
Racer01014 0:5e35c180ed4a 660
Racer01014 0:5e35c180ed4a 661 /* Call class / vendor specific busReset function */
Racer01014 0:5e35c180ed4a 662 USBCallback_busReset();
Racer01014 0:5e35c180ed4a 663 }
Racer01014 0:5e35c180ed4a 664
Racer01014 0:5e35c180ed4a 665 void USBDevice::EP0setupCallback(void)
Racer01014 0:5e35c180ed4a 666 {
Racer01014 0:5e35c180ed4a 667 /* Endpoint 0 setup event */
Racer01014 0:5e35c180ed4a 668 if (!controlSetup())
Racer01014 0:5e35c180ed4a 669 {
Racer01014 0:5e35c180ed4a 670 /* Protocol stall */
Racer01014 0:5e35c180ed4a 671 EP0stall();
Racer01014 0:5e35c180ed4a 672 }
Racer01014 0:5e35c180ed4a 673
Racer01014 0:5e35c180ed4a 674 /* Return true if an OUT data stage is expected */
Racer01014 0:5e35c180ed4a 675 }
Racer01014 0:5e35c180ed4a 676
Racer01014 0:5e35c180ed4a 677 void USBDevice::EP0out(void)
Racer01014 0:5e35c180ed4a 678 {
Racer01014 0:5e35c180ed4a 679 /* Endpoint 0 OUT data event */
Racer01014 0:5e35c180ed4a 680 if (!controlOut())
Racer01014 0:5e35c180ed4a 681 {
Racer01014 0:5e35c180ed4a 682 /* Protocol stall; this will stall both endpoints */
Racer01014 0:5e35c180ed4a 683 EP0stall();
Racer01014 0:5e35c180ed4a 684 }
Racer01014 0:5e35c180ed4a 685 }
Racer01014 0:5e35c180ed4a 686
Racer01014 0:5e35c180ed4a 687 void USBDevice::EP0in(void)
Racer01014 0:5e35c180ed4a 688 {
Racer01014 0:5e35c180ed4a 689 #ifdef DEBUG
Racer01014 0:5e35c180ed4a 690 printf("EP0IN\r\n");
Racer01014 0:5e35c180ed4a 691 #endif
Racer01014 0:5e35c180ed4a 692 /* Endpoint 0 IN data event */
Racer01014 0:5e35c180ed4a 693 if (!controlIn())
Racer01014 0:5e35c180ed4a 694 {
Racer01014 0:5e35c180ed4a 695 /* Protocol stall; this will stall both endpoints */
Racer01014 0:5e35c180ed4a 696 EP0stall();
Racer01014 0:5e35c180ed4a 697 }
Racer01014 0:5e35c180ed4a 698 }
Racer01014 0:5e35c180ed4a 699
Racer01014 0:5e35c180ed4a 700 bool USBDevice::configured(void)
Racer01014 0:5e35c180ed4a 701 {
Racer01014 0:5e35c180ed4a 702 /* Returns true if device is in the CONFIGURED state */
Racer01014 0:5e35c180ed4a 703 return (device.state == CONFIGURED);
Racer01014 0:5e35c180ed4a 704 }
Racer01014 0:5e35c180ed4a 705
Racer01014 0:5e35c180ed4a 706 void USBDevice::connect(void)
Racer01014 0:5e35c180ed4a 707 {
Racer01014 0:5e35c180ed4a 708 /* Connect device */
Racer01014 0:5e35c180ed4a 709 USBHAL::connect();
Racer01014 0:5e35c180ed4a 710 /* Block if not configured */
Racer01014 0:5e35c180ed4a 711 while (!configured());
Racer01014 0:5e35c180ed4a 712 }
Racer01014 0:5e35c180ed4a 713
Racer01014 0:5e35c180ed4a 714 void USBDevice::disconnect(void)
Racer01014 0:5e35c180ed4a 715 {
Racer01014 0:5e35c180ed4a 716 /* Disconnect device */
Racer01014 0:5e35c180ed4a 717 USBHAL::disconnect();
Racer01014 0:5e35c180ed4a 718 }
Racer01014 0:5e35c180ed4a 719
Racer01014 0:5e35c180ed4a 720 CONTROL_TRANSFER * USBDevice::getTransferPtr(void)
Racer01014 0:5e35c180ed4a 721 {
Racer01014 0:5e35c180ed4a 722 return &transfer;
Racer01014 0:5e35c180ed4a 723 }
Racer01014 0:5e35c180ed4a 724
Racer01014 0:5e35c180ed4a 725 bool USBDevice::addEndpoint(uint8_t endpoint, uint32_t maxPacket)
Racer01014 0:5e35c180ed4a 726 {
Racer01014 0:5e35c180ed4a 727 return realiseEndpoint(endpoint, maxPacket, 0);
Racer01014 0:5e35c180ed4a 728 }
Racer01014 0:5e35c180ed4a 729
Racer01014 0:5e35c180ed4a 730 bool USBDevice::addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket)
Racer01014 0:5e35c180ed4a 731 {
Racer01014 0:5e35c180ed4a 732 /* For interrupt endpoints only */
Racer01014 0:5e35c180ed4a 733 return realiseEndpoint(endpoint, maxPacket, RATE_FEEDBACK_MODE);
Racer01014 0:5e35c180ed4a 734 }
Racer01014 0:5e35c180ed4a 735
Racer01014 0:5e35c180ed4a 736 uint8_t * USBDevice::findDescriptor(uint8_t descriptorType)
Racer01014 0:5e35c180ed4a 737 {
Racer01014 0:5e35c180ed4a 738 /* Find a descriptor within the list of descriptors */
Racer01014 0:5e35c180ed4a 739 /* following a configuration descriptor. */
Racer01014 0:5e35c180ed4a 740 uint16_t wTotalLength;
Racer01014 0:5e35c180ed4a 741 uint8_t *ptr;
Racer01014 0:5e35c180ed4a 742
Racer01014 0:5e35c180ed4a 743 if (configurationDesc() == NULL)
Racer01014 0:5e35c180ed4a 744 {
Racer01014 0:5e35c180ed4a 745 return NULL;
Racer01014 0:5e35c180ed4a 746 }
Racer01014 0:5e35c180ed4a 747
Racer01014 0:5e35c180ed4a 748 /* Check this is a configuration descriptor */
Racer01014 0:5e35c180ed4a 749 if ((configurationDesc()[0] != CONFIGURATION_DESCRIPTOR_LENGTH) \
Racer01014 0:5e35c180ed4a 750 || (configurationDesc()[1] != CONFIGURATION_DESCRIPTOR))
Racer01014 0:5e35c180ed4a 751 {
Racer01014 0:5e35c180ed4a 752 return NULL;
Racer01014 0:5e35c180ed4a 753 }
Racer01014 0:5e35c180ed4a 754
Racer01014 0:5e35c180ed4a 755 wTotalLength = configurationDesc()[2] | (configurationDesc()[3] << 8);
Racer01014 0:5e35c180ed4a 756
Racer01014 0:5e35c180ed4a 757 /* Check there are some more descriptors to follow */
Racer01014 0:5e35c180ed4a 758 if (wTotalLength <= (CONFIGURATION_DESCRIPTOR_LENGTH+2))
Racer01014 0:5e35c180ed4a 759 /* +2 is for bLength and bDescriptorType of next descriptor */
Racer01014 0:5e35c180ed4a 760 {
Racer01014 0:5e35c180ed4a 761 return false;
Racer01014 0:5e35c180ed4a 762 }
Racer01014 0:5e35c180ed4a 763
Racer01014 0:5e35c180ed4a 764 /* Start at first descriptor after the configuration descriptor */
Racer01014 0:5e35c180ed4a 765 ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]);
Racer01014 0:5e35c180ed4a 766
Racer01014 0:5e35c180ed4a 767 do {
Racer01014 0:5e35c180ed4a 768 if (ptr[1] /* bDescriptorType */ == descriptorType)
Racer01014 0:5e35c180ed4a 769 {
Racer01014 0:5e35c180ed4a 770 /* Found */
Racer01014 0:5e35c180ed4a 771 return ptr;
Racer01014 0:5e35c180ed4a 772 }
Racer01014 0:5e35c180ed4a 773
Racer01014 0:5e35c180ed4a 774 /* Skip to next descriptor */
Racer01014 0:5e35c180ed4a 775 ptr += ptr[0]; /* bLength */
Racer01014 0:5e35c180ed4a 776 } while (ptr < (configurationDesc() + wTotalLength));
Racer01014 0:5e35c180ed4a 777
Racer01014 0:5e35c180ed4a 778 /* Reached end of the descriptors - not found */
Racer01014 0:5e35c180ed4a 779 return NULL;
Racer01014 0:5e35c180ed4a 780 }
Racer01014 0:5e35c180ed4a 781
Racer01014 0:5e35c180ed4a 782
Racer01014 0:5e35c180ed4a 783 void USBDevice::connectStateChanged(unsigned int connected)
Racer01014 0:5e35c180ed4a 784 {
Racer01014 0:5e35c180ed4a 785 }
Racer01014 0:5e35c180ed4a 786
Racer01014 0:5e35c180ed4a 787 void USBDevice::suspendStateChanged(unsigned int suspended)
Racer01014 0:5e35c180ed4a 788 {
Racer01014 0:5e35c180ed4a 789 }
Racer01014 0:5e35c180ed4a 790
Racer01014 0:5e35c180ed4a 791
Racer01014 0:5e35c180ed4a 792 USBDevice::USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release){
Racer01014 0:5e35c180ed4a 793 VENDOR_ID = vendor_id;
Racer01014 0:5e35c180ed4a 794 PRODUCT_ID = product_id;
Racer01014 0:5e35c180ed4a 795 PRODUCT_RELEASE = product_release;
Racer01014 0:5e35c180ed4a 796
Racer01014 0:5e35c180ed4a 797 /* Set initial device state */
Racer01014 0:5e35c180ed4a 798 device.state = POWERED;
Racer01014 0:5e35c180ed4a 799 device.configuration = 0;
Racer01014 0:5e35c180ed4a 800 device.suspended = false;
Racer01014 0:5e35c180ed4a 801 };
Racer01014 0:5e35c180ed4a 802
Racer01014 0:5e35c180ed4a 803
Racer01014 0:5e35c180ed4a 804 bool USBDevice::readStart(uint8_t endpoint, uint32_t maxSize)
Racer01014 0:5e35c180ed4a 805 {
Racer01014 0:5e35c180ed4a 806 return endpointRead(endpoint, maxSize) == EP_PENDING;
Racer01014 0:5e35c180ed4a 807 }
Racer01014 0:5e35c180ed4a 808
Racer01014 0:5e35c180ed4a 809
Racer01014 0:5e35c180ed4a 810 bool USBDevice::write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize)
Racer01014 0:5e35c180ed4a 811 {
Racer01014 0:5e35c180ed4a 812 EP_STATUS result;
Racer01014 0:5e35c180ed4a 813
Racer01014 0:5e35c180ed4a 814 if (size > maxSize)
Racer01014 0:5e35c180ed4a 815 {
Racer01014 0:5e35c180ed4a 816 return false;
Racer01014 0:5e35c180ed4a 817 }
Racer01014 0:5e35c180ed4a 818
Racer01014 0:5e35c180ed4a 819
Racer01014 0:5e35c180ed4a 820 if(!configured()) {
Racer01014 0:5e35c180ed4a 821 return false;
Racer01014 0:5e35c180ed4a 822 }
Racer01014 0:5e35c180ed4a 823
Racer01014 0:5e35c180ed4a 824 /* Send report */
Racer01014 0:5e35c180ed4a 825 result = endpointWrite(endpoint, buffer, size);
Racer01014 0:5e35c180ed4a 826
Racer01014 0:5e35c180ed4a 827 if (result != EP_PENDING)
Racer01014 0:5e35c180ed4a 828 {
Racer01014 0:5e35c180ed4a 829 return false;
Racer01014 0:5e35c180ed4a 830 }
Racer01014 0:5e35c180ed4a 831
Racer01014 0:5e35c180ed4a 832 /* Wait for completion */
Racer01014 0:5e35c180ed4a 833 do {
Racer01014 0:5e35c180ed4a 834 result = endpointWriteResult(endpoint);
Racer01014 0:5e35c180ed4a 835 } while ((result == EP_PENDING) && configured());
Racer01014 0:5e35c180ed4a 836
Racer01014 0:5e35c180ed4a 837 return (result == EP_COMPLETED);
Racer01014 0:5e35c180ed4a 838 }
Racer01014 0:5e35c180ed4a 839
Racer01014 0:5e35c180ed4a 840
Racer01014 0:5e35c180ed4a 841 bool USBDevice::writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize)
Racer01014 0:5e35c180ed4a 842 {
Racer01014 0:5e35c180ed4a 843 EP_STATUS result;
Racer01014 0:5e35c180ed4a 844
Racer01014 0:5e35c180ed4a 845 if (size > maxSize)
Racer01014 0:5e35c180ed4a 846 {
Racer01014 0:5e35c180ed4a 847 return false;
Racer01014 0:5e35c180ed4a 848 }
Racer01014 0:5e35c180ed4a 849
Racer01014 0:5e35c180ed4a 850 if(!configured()) {
Racer01014 0:5e35c180ed4a 851 return false;
Racer01014 0:5e35c180ed4a 852 }
Racer01014 0:5e35c180ed4a 853
Racer01014 0:5e35c180ed4a 854 /* Send report */
Racer01014 0:5e35c180ed4a 855 result = endpointWrite(endpoint, buffer, size);
Racer01014 0:5e35c180ed4a 856
Racer01014 0:5e35c180ed4a 857 if (result != EP_PENDING)
Racer01014 0:5e35c180ed4a 858 {
Racer01014 0:5e35c180ed4a 859 return false;
Racer01014 0:5e35c180ed4a 860 }
Racer01014 0:5e35c180ed4a 861
Racer01014 0:5e35c180ed4a 862 result = endpointWriteResult(endpoint);
Racer01014 0:5e35c180ed4a 863
Racer01014 0:5e35c180ed4a 864 return (result == EP_COMPLETED);
Racer01014 0:5e35c180ed4a 865 }
Racer01014 0:5e35c180ed4a 866
Racer01014 0:5e35c180ed4a 867
Racer01014 0:5e35c180ed4a 868
Racer01014 0:5e35c180ed4a 869 bool USBDevice::readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize)
Racer01014 0:5e35c180ed4a 870 {
Racer01014 0:5e35c180ed4a 871 EP_STATUS result;
Racer01014 0:5e35c180ed4a 872
Racer01014 0:5e35c180ed4a 873 if(!configured()) {
Racer01014 0:5e35c180ed4a 874 return false;
Racer01014 0:5e35c180ed4a 875 }
Racer01014 0:5e35c180ed4a 876
Racer01014 0:5e35c180ed4a 877 /* Wait for completion */
Racer01014 0:5e35c180ed4a 878 do {
Racer01014 0:5e35c180ed4a 879 result = endpointReadResult(endpoint, buffer, size);
Racer01014 0:5e35c180ed4a 880 } while ((result == EP_PENDING) && configured());
Racer01014 0:5e35c180ed4a 881
Racer01014 0:5e35c180ed4a 882 return (result == EP_COMPLETED);
Racer01014 0:5e35c180ed4a 883 }
Racer01014 0:5e35c180ed4a 884
Racer01014 0:5e35c180ed4a 885
Racer01014 0:5e35c180ed4a 886 bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize)
Racer01014 0:5e35c180ed4a 887 {
Racer01014 0:5e35c180ed4a 888 EP_STATUS result;
Racer01014 0:5e35c180ed4a 889
Racer01014 0:5e35c180ed4a 890 if(!configured()) {
Racer01014 0:5e35c180ed4a 891 return false;
Racer01014 0:5e35c180ed4a 892 }
Racer01014 0:5e35c180ed4a 893
Racer01014 0:5e35c180ed4a 894 result = endpointReadResult(endpoint, buffer, size);
Racer01014 0:5e35c180ed4a 895
Racer01014 0:5e35c180ed4a 896 return (result == EP_COMPLETED);
Racer01014 0:5e35c180ed4a 897 }
Racer01014 0:5e35c180ed4a 898
Racer01014 0:5e35c180ed4a 899
Racer01014 0:5e35c180ed4a 900
Racer01014 0:5e35c180ed4a 901 uint8_t * USBDevice::deviceDesc() {
Racer01014 0:5e35c180ed4a 902 static uint8_t deviceDescriptor[] = {
Racer01014 0:5e35c180ed4a 903 DEVICE_DESCRIPTOR_LENGTH, /* bLength */
Racer01014 0:5e35c180ed4a 904 DEVICE_DESCRIPTOR, /* bDescriptorType */
Racer01014 0:5e35c180ed4a 905 LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */
Racer01014 0:5e35c180ed4a 906 MSB(USB_VERSION_2_0), /* bcdUSB (MSB) */
Racer01014 0:5e35c180ed4a 907 0x00, /* bDeviceClass */
Racer01014 0:5e35c180ed4a 908 0x00, /* bDeviceSubClass */
Racer01014 0:5e35c180ed4a 909 0x00, /* bDeviceprotocol */
Racer01014 0:5e35c180ed4a 910 MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */
Racer01014 0:5e35c180ed4a 911 LSB(VENDOR_ID), /* idVendor (LSB) */
Racer01014 0:5e35c180ed4a 912 MSB(VENDOR_ID), /* idVendor (MSB) */
Racer01014 0:5e35c180ed4a 913 LSB(PRODUCT_ID), /* idProduct (LSB) */
Racer01014 0:5e35c180ed4a 914 MSB(PRODUCT_ID), /* idProduct (MSB) */
Racer01014 0:5e35c180ed4a 915 LSB(PRODUCT_RELEASE), /* bcdDevice (LSB) */
Racer01014 0:5e35c180ed4a 916 MSB(PRODUCT_RELEASE), /* bcdDevice (MSB) */
Racer01014 0:5e35c180ed4a 917 STRING_OFFSET_IMANUFACTURER, /* iManufacturer */
Racer01014 0:5e35c180ed4a 918 STRING_OFFSET_IPRODUCT, /* iProduct */
Racer01014 0:5e35c180ed4a 919 STRING_OFFSET_ISERIAL, /* iSerialNumber */
Racer01014 0:5e35c180ed4a 920 0x01 /* bNumConfigurations */
Racer01014 0:5e35c180ed4a 921 };
Racer01014 0:5e35c180ed4a 922 return deviceDescriptor;
Racer01014 0:5e35c180ed4a 923 }
Racer01014 0:5e35c180ed4a 924
Racer01014 0:5e35c180ed4a 925 uint8_t * USBDevice::stringLangidDesc() {
Racer01014 0:5e35c180ed4a 926 static uint8_t stringLangidDescriptor[] = {
Racer01014 0:5e35c180ed4a 927 0x04, /*bLength*/
Racer01014 0:5e35c180ed4a 928 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
Racer01014 0:5e35c180ed4a 929 0x09,0x00, /*bString Lang ID - 0x009 - English*/
Racer01014 0:5e35c180ed4a 930 };
Racer01014 0:5e35c180ed4a 931 return stringLangidDescriptor;
Racer01014 0:5e35c180ed4a 932 }
Racer01014 0:5e35c180ed4a 933
Racer01014 0:5e35c180ed4a 934 uint8_t * USBDevice::stringImanufacturerDesc() {
Racer01014 0:5e35c180ed4a 935 static uint8_t stringImanufacturerDescriptor[] = {
Racer01014 0:5e35c180ed4a 936 0x12, /*bLength*/
Racer01014 0:5e35c180ed4a 937 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
Racer01014 0:5e35c180ed4a 938 'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/
Racer01014 0:5e35c180ed4a 939 };
Racer01014 0:5e35c180ed4a 940 return stringImanufacturerDescriptor;
Racer01014 0:5e35c180ed4a 941 }
Racer01014 0:5e35c180ed4a 942
Racer01014 0:5e35c180ed4a 943 uint8_t * USBDevice::stringIserialDesc() {
Racer01014 0:5e35c180ed4a 944 static uint8_t stringIserialDescriptor[] = {
Racer01014 0:5e35c180ed4a 945 0x16, /*bLength*/
Racer01014 0:5e35c180ed4a 946 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
Racer01014 0:5e35c180ed4a 947 '0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/
Racer01014 0:5e35c180ed4a 948 };
Racer01014 0:5e35c180ed4a 949 return stringIserialDescriptor;
Racer01014 0:5e35c180ed4a 950 }
Racer01014 0:5e35c180ed4a 951
Racer01014 0:5e35c180ed4a 952 uint8_t * USBDevice::stringIConfigurationDesc() {
Racer01014 0:5e35c180ed4a 953 static uint8_t stringIconfigurationDescriptor[] = {
Racer01014 0:5e35c180ed4a 954 0x06, /*bLength*/
Racer01014 0:5e35c180ed4a 955 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
Racer01014 0:5e35c180ed4a 956 '0',0,'1',0, /*bString iConfiguration - 01*/
Racer01014 0:5e35c180ed4a 957 };
Racer01014 0:5e35c180ed4a 958 return stringIconfigurationDescriptor;
Racer01014 0:5e35c180ed4a 959 }
Racer01014 0:5e35c180ed4a 960
Racer01014 0:5e35c180ed4a 961 uint8_t * USBDevice::stringIinterfaceDesc() {
Racer01014 0:5e35c180ed4a 962 static uint8_t stringIinterfaceDescriptor[] = {
Racer01014 0:5e35c180ed4a 963 0x08, /*bLength*/
Racer01014 0:5e35c180ed4a 964 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
Racer01014 0:5e35c180ed4a 965 'U',0,'S',0,'B',0, /*bString iInterface - USB*/
Racer01014 0:5e35c180ed4a 966 };
Racer01014 0:5e35c180ed4a 967 return stringIinterfaceDescriptor;
Racer01014 0:5e35c180ed4a 968 }
Racer01014 0:5e35c180ed4a 969
Racer01014 0:5e35c180ed4a 970 uint8_t * USBDevice::stringIproductDesc() {
Racer01014 0:5e35c180ed4a 971 static uint8_t stringIproductDescriptor[] = {
Racer01014 0:5e35c180ed4a 972 0x16, /*bLength*/
Racer01014 0:5e35c180ed4a 973 STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
Racer01014 0:5e35c180ed4a 974 'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/
Racer01014 0:5e35c180ed4a 975 };
Racer01014 0:5e35c180ed4a 976 return stringIproductDescriptor;
Racer01014 0:5e35c180ed4a 977 }