USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Fri Nov 11 15:22:53 2011 +0000
Revision:
2:27a7e7f8d399
we have 2MB with the sdcard!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:27a7e7f8d399 1 // USBHID.c
samux 2:27a7e7f8d399 2 // Human Interface Device (HID) class
samux 2:27a7e7f8d399 3 // Copyright (c) 2011 ARM Limited. All rights reserved.
samux 2:27a7e7f8d399 4
samux 2:27a7e7f8d399 5 #include "stdint.h"
samux 2:27a7e7f8d399 6 #include "USBBusInterface.h"
samux 2:27a7e7f8d399 7 #include "USBHID.h"
samux 2:27a7e7f8d399 8
samux 2:27a7e7f8d399 9
samux 2:27a7e7f8d399 10 USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release)
samux 2:27a7e7f8d399 11 {
samux 2:27a7e7f8d399 12 output_length = output_report_length;
samux 2:27a7e7f8d399 13 input_length = input_report_length;
samux 2:27a7e7f8d399 14 }
samux 2:27a7e7f8d399 15
samux 2:27a7e7f8d399 16
samux 2:27a7e7f8d399 17 bool USBHID::send(HID_REPORT *report)
samux 2:27a7e7f8d399 18 {
samux 2:27a7e7f8d399 19 return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
samux 2:27a7e7f8d399 20 }
samux 2:27a7e7f8d399 21
samux 2:27a7e7f8d399 22
samux 2:27a7e7f8d399 23 bool USBHID::read(HID_REPORT *report)
samux 2:27a7e7f8d399 24 {
samux 2:27a7e7f8d399 25 uint16_t bytesRead = 0;
samux 2:27a7e7f8d399 26 bool result;
samux 2:27a7e7f8d399 27 result = USBDevice::read(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
samux 2:27a7e7f8d399 28 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
samux 2:27a7e7f8d399 29 return false;
samux 2:27a7e7f8d399 30 report->length = bytesRead;
samux 2:27a7e7f8d399 31 return result;
samux 2:27a7e7f8d399 32 }
samux 2:27a7e7f8d399 33
samux 2:27a7e7f8d399 34
samux 2:27a7e7f8d399 35 bool USBHID::readNB(HID_REPORT *report)
samux 2:27a7e7f8d399 36 {
samux 2:27a7e7f8d399 37 uint16_t bytesRead = 0;
samux 2:27a7e7f8d399 38 bool result;
samux 2:27a7e7f8d399 39 result = USBDevice::readNB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
samux 2:27a7e7f8d399 40 report->length = bytesRead;
samux 2:27a7e7f8d399 41 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
samux 2:27a7e7f8d399 42 return false;
samux 2:27a7e7f8d399 43 return result;
samux 2:27a7e7f8d399 44 }
samux 2:27a7e7f8d399 45
samux 2:27a7e7f8d399 46
samux 2:27a7e7f8d399 47 uint16_t USBHID::reportDescLength() {
samux 2:27a7e7f8d399 48 reportDesc();
samux 2:27a7e7f8d399 49 return reportLength;
samux 2:27a7e7f8d399 50 }
samux 2:27a7e7f8d399 51
samux 2:27a7e7f8d399 52
samux 2:27a7e7f8d399 53
samux 2:27a7e7f8d399 54 //
samux 2:27a7e7f8d399 55 // Route callbacks from lower layers to class(es)
samux 2:27a7e7f8d399 56 //
samux 2:27a7e7f8d399 57
samux 2:27a7e7f8d399 58
samux 2:27a7e7f8d399 59 // Called in ISR context
samux 2:27a7e7f8d399 60 // Called by USBDevice on Endpoint0 request
samux 2:27a7e7f8d399 61 // This is used to handle extensions to standard requests
samux 2:27a7e7f8d399 62 // and class specific requests
samux 2:27a7e7f8d399 63 // Return true if class handles this request
samux 2:27a7e7f8d399 64 bool USBHID::USBCallback_request() {
samux 2:27a7e7f8d399 65 bool success = false;
samux 2:27a7e7f8d399 66 CONTROL_TRANSFER * transfer = getTransferPtr();
samux 2:27a7e7f8d399 67 uint8_t *hidDescriptor;
samux 2:27a7e7f8d399 68
samux 2:27a7e7f8d399 69 // Process additional standard requests
samux 2:27a7e7f8d399 70
samux 2:27a7e7f8d399 71 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
samux 2:27a7e7f8d399 72 {
samux 2:27a7e7f8d399 73 switch (transfer->setup.bRequest)
samux 2:27a7e7f8d399 74 {
samux 2:27a7e7f8d399 75 case GET_DESCRIPTOR:
samux 2:27a7e7f8d399 76 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
samux 2:27a7e7f8d399 77 {
samux 2:27a7e7f8d399 78 case REPORT_DESCRIPTOR:
samux 2:27a7e7f8d399 79 if ((reportDesc() != NULL) \
samux 2:27a7e7f8d399 80 && (reportDescLength() != 0))
samux 2:27a7e7f8d399 81 {
samux 2:27a7e7f8d399 82 transfer->remaining = reportDescLength();
samux 2:27a7e7f8d399 83 transfer->ptr = reportDesc();
samux 2:27a7e7f8d399 84 transfer->direction = DEVICE_TO_HOST;
samux 2:27a7e7f8d399 85 success = true;
samux 2:27a7e7f8d399 86 }
samux 2:27a7e7f8d399 87 break;
samux 2:27a7e7f8d399 88 case HID_DESCRIPTOR:
samux 2:27a7e7f8d399 89 // Find the HID descriptor, after the configuration descriptor
samux 2:27a7e7f8d399 90 hidDescriptor = findDescriptor(HID_DESCRIPTOR);
samux 2:27a7e7f8d399 91 if (hidDescriptor != NULL)
samux 2:27a7e7f8d399 92 {
samux 2:27a7e7f8d399 93 transfer->remaining = HID_DESCRIPTOR_LENGTH;
samux 2:27a7e7f8d399 94 transfer->ptr = hidDescriptor;
samux 2:27a7e7f8d399 95 transfer->direction = DEVICE_TO_HOST;
samux 2:27a7e7f8d399 96 success = true;
samux 2:27a7e7f8d399 97 }
samux 2:27a7e7f8d399 98 break;
samux 2:27a7e7f8d399 99
samux 2:27a7e7f8d399 100 default:
samux 2:27a7e7f8d399 101 break;
samux 2:27a7e7f8d399 102 }
samux 2:27a7e7f8d399 103 break;
samux 2:27a7e7f8d399 104 default:
samux 2:27a7e7f8d399 105 break;
samux 2:27a7e7f8d399 106 }
samux 2:27a7e7f8d399 107 }
samux 2:27a7e7f8d399 108
samux 2:27a7e7f8d399 109 // Process class-specific requests
samux 2:27a7e7f8d399 110
samux 2:27a7e7f8d399 111 if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
samux 2:27a7e7f8d399 112 {
samux 2:27a7e7f8d399 113 switch (transfer->setup.bRequest)
samux 2:27a7e7f8d399 114 {
samux 2:27a7e7f8d399 115 case SET_REPORT:
samux 2:27a7e7f8d399 116 // First byte will be used for report ID
samux 2:27a7e7f8d399 117 outputReport.data[0] = transfer->setup.wValue & 0xff;
samux 2:27a7e7f8d399 118 outputReport.length = transfer->setup.wLength + 1;
samux 2:27a7e7f8d399 119
samux 2:27a7e7f8d399 120 transfer->remaining = sizeof(outputReport.data) - 1;
samux 2:27a7e7f8d399 121 transfer->ptr = &outputReport.data[1];
samux 2:27a7e7f8d399 122 transfer->direction = HOST_TO_DEVICE;
samux 2:27a7e7f8d399 123 transfer->notify = true;
samux 2:27a7e7f8d399 124 success = true;
samux 2:27a7e7f8d399 125 default:
samux 2:27a7e7f8d399 126 break;
samux 2:27a7e7f8d399 127 }
samux 2:27a7e7f8d399 128 }
samux 2:27a7e7f8d399 129
samux 2:27a7e7f8d399 130 return success;
samux 2:27a7e7f8d399 131 }
samux 2:27a7e7f8d399 132
samux 2:27a7e7f8d399 133
samux 2:27a7e7f8d399 134 // Called in ISR context
samux 2:27a7e7f8d399 135 // Called by USBDevice on Endpoint0 request completion
samux 2:27a7e7f8d399 136 // if the 'notify' flag has been set to true
samux 2:27a7e7f8d399 137 // In this case it is used to indicate that a HID report has
samux 2:27a7e7f8d399 138 // been received from the host on endpoint 0
samux 2:27a7e7f8d399 139 void USBHID::USBCallback_requestCompleted() {
samux 2:27a7e7f8d399 140 HID_callbackSetReport(&outputReport);
samux 2:27a7e7f8d399 141 }
samux 2:27a7e7f8d399 142
samux 2:27a7e7f8d399 143 #define DEFAULT_CONFIGURATION (1)
samux 2:27a7e7f8d399 144
samux 2:27a7e7f8d399 145
samux 2:27a7e7f8d399 146 // Called in ISR context
samux 2:27a7e7f8d399 147 // Set configuration. Return false if the
samux 2:27a7e7f8d399 148 // configuration is not supported
samux 2:27a7e7f8d399 149 bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
samux 2:27a7e7f8d399 150 if (configuration != DEFAULT_CONFIGURATION) {
samux 2:27a7e7f8d399 151 return false;
samux 2:27a7e7f8d399 152 }
samux 2:27a7e7f8d399 153
samux 2:27a7e7f8d399 154 // Configure endpoints > 0
samux 2:27a7e7f8d399 155 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
samux 2:27a7e7f8d399 156 addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
samux 2:27a7e7f8d399 157
samux 2:27a7e7f8d399 158 // We activate the endpoint to be able to recceive data
samux 2:27a7e7f8d399 159 readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
samux 2:27a7e7f8d399 160 return true;
samux 2:27a7e7f8d399 161 }
samux 2:27a7e7f8d399 162
samux 2:27a7e7f8d399 163 uint8_t * USBHID::stringIinterfaceDesc() {
samux 2:27a7e7f8d399 164 static uint8_t stringIinterfaceDescriptor[] = {
samux 2:27a7e7f8d399 165 0x08, //bLength
samux 2:27a7e7f8d399 166 STRING_DESCRIPTOR, //bDescriptorType 0x03
samux 2:27a7e7f8d399 167 'H',0,'I',0,'D',0, //bString iInterface - HID
samux 2:27a7e7f8d399 168 };
samux 2:27a7e7f8d399 169 return stringIinterfaceDescriptor;
samux 2:27a7e7f8d399 170 }
samux 2:27a7e7f8d399 171
samux 2:27a7e7f8d399 172 uint8_t * USBHID::stringIproductDesc() {
samux 2:27a7e7f8d399 173 static uint8_t stringIproductDescriptor[] = {
samux 2:27a7e7f8d399 174 0x16, //bLength
samux 2:27a7e7f8d399 175 STRING_DESCRIPTOR, //bDescriptorType 0x03
samux 2:27a7e7f8d399 176 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
samux 2:27a7e7f8d399 177 };
samux 2:27a7e7f8d399 178 return stringIproductDescriptor;
samux 2:27a7e7f8d399 179 }
samux 2:27a7e7f8d399 180
samux 2:27a7e7f8d399 181
samux 2:27a7e7f8d399 182
samux 2:27a7e7f8d399 183 uint8_t * USBHID::reportDesc() {
samux 2:27a7e7f8d399 184 static uint8_t reportDescriptor[] = {
samux 2:27a7e7f8d399 185 0x06, LSB(0xFFAB), MSB(0xFFAB),
samux 2:27a7e7f8d399 186 0x0A, LSB(0x0200), MSB(0x0200),
samux 2:27a7e7f8d399 187 0xA1, 0x01, // Collection 0x01
samux 2:27a7e7f8d399 188 0x75, 0x08, // report size = 8 bits
samux 2:27a7e7f8d399 189 0x15, 0x00, // logical minimum = 0
samux 2:27a7e7f8d399 190 0x26, 0xFF, 0x00, // logical maximum = 255
samux 2:27a7e7f8d399 191 0x95, input_length, // report count
samux 2:27a7e7f8d399 192 0x09, 0x01, // usage
samux 2:27a7e7f8d399 193 0x81, 0x02, // Input (array)
samux 2:27a7e7f8d399 194 0x95, output_length, // report count
samux 2:27a7e7f8d399 195 0x09, 0x02, // usage
samux 2:27a7e7f8d399 196 0x91, 0x02, // Output (array)
samux 2:27a7e7f8d399 197 0xC0 // end collection
samux 2:27a7e7f8d399 198
samux 2:27a7e7f8d399 199 };
samux 2:27a7e7f8d399 200 reportLength = sizeof(reportDescriptor);
samux 2:27a7e7f8d399 201 return reportDescriptor;
samux 2:27a7e7f8d399 202 }
samux 2:27a7e7f8d399 203
samux 2:27a7e7f8d399 204 #define DEFAULT_CONFIGURATION (1)
samux 2:27a7e7f8d399 205 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
samux 2:27a7e7f8d399 206 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
samux 2:27a7e7f8d399 207 + (1 * HID_DESCRIPTOR_LENGTH) \
samux 2:27a7e7f8d399 208 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
samux 2:27a7e7f8d399 209
samux 2:27a7e7f8d399 210 uint8_t * USBHID::configurationDesc() {
samux 2:27a7e7f8d399 211 static uint8_t configurationDescriptor[] = {
samux 2:27a7e7f8d399 212 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
samux 2:27a7e7f8d399 213 CONFIGURATION_DESCRIPTOR, // bDescriptorType
samux 2:27a7e7f8d399 214 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
samux 2:27a7e7f8d399 215 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
samux 2:27a7e7f8d399 216 0x01, // bNumInterfaces
samux 2:27a7e7f8d399 217 DEFAULT_CONFIGURATION, // bConfigurationValue
samux 2:27a7e7f8d399 218 0x00, // iConfiguration
samux 2:27a7e7f8d399 219 C_RESERVED | C_SELF_POWERED, // bmAttributes
samux 2:27a7e7f8d399 220 C_POWER(0), // bMaxPower
samux 2:27a7e7f8d399 221
samux 2:27a7e7f8d399 222 INTERFACE_DESCRIPTOR_LENGTH, // bLength
samux 2:27a7e7f8d399 223 INTERFACE_DESCRIPTOR, // bDescriptorType
samux 2:27a7e7f8d399 224 0x00, // bInterfaceNumber
samux 2:27a7e7f8d399 225 0x00, // bAlternateSetting
samux 2:27a7e7f8d399 226 0x02, // bNumEndpoints
samux 2:27a7e7f8d399 227 HID_CLASS, // bInterfaceClass
samux 2:27a7e7f8d399 228 HID_SUBCLASS_NONE, // bInterfaceSubClass
samux 2:27a7e7f8d399 229 HID_PROTOCOL_NONE, // bInterfaceProtocol
samux 2:27a7e7f8d399 230 0x00, // iInterface
samux 2:27a7e7f8d399 231
samux 2:27a7e7f8d399 232 HID_DESCRIPTOR_LENGTH, // bLength
samux 2:27a7e7f8d399 233 HID_DESCRIPTOR, // bDescriptorType
samux 2:27a7e7f8d399 234 LSB(HID_VERSION_1_11), // bcdHID (LSB)
samux 2:27a7e7f8d399 235 MSB(HID_VERSION_1_11), // bcdHID (MSB)
samux 2:27a7e7f8d399 236 0x00, // bCountryCode
samux 2:27a7e7f8d399 237 0x01, // bNumDescriptors
samux 2:27a7e7f8d399 238 REPORT_DESCRIPTOR, // bDescriptorType
samux 2:27a7e7f8d399 239 LSB(this->reportDescLength()), // wDescriptorLength (LSB)
samux 2:27a7e7f8d399 240 MSB(this->reportDescLength()), // wDescriptorLength (MSB)
samux 2:27a7e7f8d399 241
samux 2:27a7e7f8d399 242 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
samux 2:27a7e7f8d399 243 ENDPOINT_DESCRIPTOR, // bDescriptorType
samux 2:27a7e7f8d399 244 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
samux 2:27a7e7f8d399 245 E_INTERRUPT, // bmAttributes
samux 2:27a7e7f8d399 246 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
samux 2:27a7e7f8d399 247 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
samux 2:27a7e7f8d399 248 10, // bInterval (milliseconds)
samux 2:27a7e7f8d399 249
samux 2:27a7e7f8d399 250 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
samux 2:27a7e7f8d399 251 ENDPOINT_DESCRIPTOR, // bDescriptorType
samux 2:27a7e7f8d399 252 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
samux 2:27a7e7f8d399 253 E_INTERRUPT, // bmAttributes
samux 2:27a7e7f8d399 254 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
samux 2:27a7e7f8d399 255 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
samux 2:27a7e7f8d399 256 10, // bInterval (milliseconds)
samux 2:27a7e7f8d399 257 };
samux 2:27a7e7f8d399 258 return configurationDescriptor;
samux 2:27a7e7f8d399 259 }