[ FORK ] basic DAP library forked from va009039/USBDAP/

Dependents:   11u35_usbLocalFilesystem

Fork of USBDAP by Norimasa Okamoto

Committer:
k4zuki
Date:
Fri Mar 04 10:33:25 2016 +0000
Revision:
3:13462db266a8
Parent:
2:7dee016756ce
remove LPC1768 switch; remove ATD45DB161D; replace W25X40BV; add loader.h; add mystorage.h/.cpp; trying to implement W25X40BV device to interface as USBLocalFileSystem

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:6b9154ed73c6 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
va009039 0:6b9154ed73c6 2 *
va009039 0:6b9154ed73c6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
va009039 0:6b9154ed73c6 4 * and associated documentation files (the "Software"), to deal in the Software without
va009039 0:6b9154ed73c6 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
va009039 0:6b9154ed73c6 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
va009039 0:6b9154ed73c6 7 * Software is furnished to do so, subject to the following conditions:
va009039 0:6b9154ed73c6 8 *
va009039 0:6b9154ed73c6 9 * The above copyright notice and this permission notice shall be included in all copies or
va009039 0:6b9154ed73c6 10 * substantial portions of the Software.
va009039 0:6b9154ed73c6 11 *
va009039 0:6b9154ed73c6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
va009039 0:6b9154ed73c6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
va009039 0:6b9154ed73c6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
va009039 0:6b9154ed73c6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
va009039 0:6b9154ed73c6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
va009039 0:6b9154ed73c6 17 */
va009039 0:6b9154ed73c6 18
va009039 0:6b9154ed73c6 19 #include "stdint.h"
va009039 0:6b9154ed73c6 20 #include "USBHAL.h"
va009039 0:6b9154ed73c6 21 #include "USBDAP.h"
va009039 0:6b9154ed73c6 22
va009039 2:7dee016756ce 23 USBDAP::USBDAP(const char *product, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)
va009039 0:6b9154ed73c6 24 {
va009039 2:7dee016756ce 25 buildStringIproductDescriptor(product);
k4zuki 3:13462db266a8 26
va009039 0:6b9154ed73c6 27 if(connect) {
va009039 0:6b9154ed73c6 28 USBDevice::connect();
va009039 0:6b9154ed73c6 29 }
va009039 0:6b9154ed73c6 30 }
va009039 0:6b9154ed73c6 31
va009039 2:7dee016756ce 32 USBDAP::USBDAP(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)
va009039 2:7dee016756ce 33 {
va009039 2:7dee016756ce 34 const char* product = "CMSIS-DAP";
va009039 2:7dee016756ce 35 buildStringIproductDescriptor(product);
k4zuki 3:13462db266a8 36
va009039 2:7dee016756ce 37 if(connect) {
va009039 2:7dee016756ce 38 USBDevice::connect();
va009039 2:7dee016756ce 39 }
va009039 2:7dee016756ce 40 }
va009039 2:7dee016756ce 41
va009039 2:7dee016756ce 42 void USBDAP::buildStringIproductDescriptor(const char* s)
va009039 2:7dee016756ce 43 {
va009039 2:7dee016756ce 44 int len = 2+strlen(s)*2;
va009039 2:7dee016756ce 45 _stringIproductDescriptor = new uint8_t[len];
k4zuki 3:13462db266a8 46 _stringIproductDescriptor[0] = len;
va009039 2:7dee016756ce 47 _stringIproductDescriptor[1] = STRING_DESCRIPTOR;
va009039 2:7dee016756ce 48 for (int i = 0; i < strlen(s); i++) {
va009039 2:7dee016756ce 49 _stringIproductDescriptor[2+i*2] = s[i];
va009039 2:7dee016756ce 50 _stringIproductDescriptor[3+i*2] = 0;
va009039 2:7dee016756ce 51 }
va009039 2:7dee016756ce 52 }
va009039 0:6b9154ed73c6 53
va009039 0:6b9154ed73c6 54 bool USBDAP::send(HID_REPORT *report)
va009039 0:6b9154ed73c6 55 {
va009039 0:6b9154ed73c6 56 return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
va009039 0:6b9154ed73c6 57 }
va009039 0:6b9154ed73c6 58
va009039 0:6b9154ed73c6 59 bool USBDAP::sendNB(HID_REPORT *report)
va009039 0:6b9154ed73c6 60 {
va009039 0:6b9154ed73c6 61 return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
va009039 0:6b9154ed73c6 62 }
va009039 0:6b9154ed73c6 63
va009039 0:6b9154ed73c6 64
va009039 0:6b9154ed73c6 65 bool USBDAP::read(HID_REPORT *report)
va009039 0:6b9154ed73c6 66 {
va009039 0:6b9154ed73c6 67 uint32_t bytesRead = 0;
va009039 0:6b9154ed73c6 68 bool result;
va009039 0:6b9154ed73c6 69 result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
va009039 0:6b9154ed73c6 70 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
va009039 0:6b9154ed73c6 71 return false;
va009039 0:6b9154ed73c6 72 report->length = bytesRead;
va009039 0:6b9154ed73c6 73 return result;
va009039 0:6b9154ed73c6 74 }
va009039 0:6b9154ed73c6 75
va009039 0:6b9154ed73c6 76
va009039 0:6b9154ed73c6 77 bool USBDAP::readNB(HID_REPORT *report)
va009039 0:6b9154ed73c6 78 {
va009039 0:6b9154ed73c6 79 uint32_t bytesRead = 0;
va009039 0:6b9154ed73c6 80 bool result;
va009039 0:6b9154ed73c6 81 result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
va009039 1:366f6f649a7d 82 // if readEP_NB did not succeed, does not issue a readStart
va009039 1:366f6f649a7d 83 if (!result)
va009039 1:366f6f649a7d 84 return false;
va009039 0:6b9154ed73c6 85 report->length = bytesRead;
va009039 0:6b9154ed73c6 86 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
va009039 0:6b9154ed73c6 87 return false;
va009039 0:6b9154ed73c6 88 return result;
va009039 0:6b9154ed73c6 89 }
va009039 0:6b9154ed73c6 90
va009039 0:6b9154ed73c6 91
k4zuki 3:13462db266a8 92 uint16_t USBDAP::reportDescLength()
k4zuki 3:13462db266a8 93 {
va009039 0:6b9154ed73c6 94 reportDesc();
va009039 0:6b9154ed73c6 95 return reportLength;
va009039 0:6b9154ed73c6 96 }
va009039 0:6b9154ed73c6 97
va009039 0:6b9154ed73c6 98
va009039 0:6b9154ed73c6 99
va009039 0:6b9154ed73c6 100 //
va009039 0:6b9154ed73c6 101 // Route callbacks from lower layers to class(es)
va009039 0:6b9154ed73c6 102 //
va009039 0:6b9154ed73c6 103
va009039 0:6b9154ed73c6 104
va009039 0:6b9154ed73c6 105 // Called in ISR context
va009039 0:6b9154ed73c6 106 // Called by USBDevice on Endpoint0 request
va009039 0:6b9154ed73c6 107 // This is used to handle extensions to standard requests
va009039 0:6b9154ed73c6 108 // and class specific requests
va009039 0:6b9154ed73c6 109 // Return true if class handles this request
k4zuki 3:13462db266a8 110 bool USBDAP::USBCallback_request()
k4zuki 3:13462db266a8 111 {
va009039 0:6b9154ed73c6 112 bool success = false;
va009039 0:6b9154ed73c6 113 CONTROL_TRANSFER * transfer = getTransferPtr();
va009039 0:6b9154ed73c6 114 uint8_t *hidDescriptor;
va009039 0:6b9154ed73c6 115
va009039 0:6b9154ed73c6 116 // Process additional standard requests
va009039 0:6b9154ed73c6 117
k4zuki 3:13462db266a8 118 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) {
k4zuki 3:13462db266a8 119 switch (transfer->setup.bRequest) {
va009039 0:6b9154ed73c6 120 case GET_DESCRIPTOR:
k4zuki 3:13462db266a8 121 switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) {
va009039 0:6b9154ed73c6 122 case REPORT_DESCRIPTOR:
va009039 0:6b9154ed73c6 123 if ((reportDesc() != NULL) \
k4zuki 3:13462db266a8 124 && (reportDescLength() != 0)) {
va009039 0:6b9154ed73c6 125 transfer->remaining = reportDescLength();
va009039 0:6b9154ed73c6 126 transfer->ptr = reportDesc();
va009039 0:6b9154ed73c6 127 transfer->direction = DEVICE_TO_HOST;
va009039 0:6b9154ed73c6 128 success = true;
va009039 0:6b9154ed73c6 129 }
va009039 0:6b9154ed73c6 130 break;
va009039 0:6b9154ed73c6 131 case HID_DESCRIPTOR:
k4zuki 3:13462db266a8 132 // Find the HID descriptor, after the configuration descriptor
k4zuki 3:13462db266a8 133 hidDescriptor = findDescriptor(HID_DESCRIPTOR);
k4zuki 3:13462db266a8 134 if (hidDescriptor != NULL) {
k4zuki 3:13462db266a8 135 transfer->remaining = HID_DESCRIPTOR_LENGTH;
k4zuki 3:13462db266a8 136 transfer->ptr = hidDescriptor;
k4zuki 3:13462db266a8 137 transfer->direction = DEVICE_TO_HOST;
k4zuki 3:13462db266a8 138 success = true;
k4zuki 3:13462db266a8 139 }
k4zuki 3:13462db266a8 140 break;
k4zuki 3:13462db266a8 141
va009039 0:6b9154ed73c6 142 default:
va009039 0:6b9154ed73c6 143 break;
va009039 0:6b9154ed73c6 144 }
va009039 0:6b9154ed73c6 145 break;
va009039 0:6b9154ed73c6 146 default:
va009039 0:6b9154ed73c6 147 break;
va009039 0:6b9154ed73c6 148 }
va009039 0:6b9154ed73c6 149 }
va009039 0:6b9154ed73c6 150
va009039 0:6b9154ed73c6 151 // Process class-specific requests
va009039 0:6b9154ed73c6 152
k4zuki 3:13462db266a8 153 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
k4zuki 3:13462db266a8 154 switch (transfer->setup.bRequest) {
k4zuki 3:13462db266a8 155 case SET_REPORT:
va009039 0:6b9154ed73c6 156 // First byte will be used for report ID
va009039 0:6b9154ed73c6 157 outputReport.data[0] = transfer->setup.wValue & 0xff;
va009039 0:6b9154ed73c6 158 outputReport.length = transfer->setup.wLength + 1;
va009039 0:6b9154ed73c6 159
va009039 0:6b9154ed73c6 160 transfer->remaining = sizeof(outputReport.data) - 1;
va009039 0:6b9154ed73c6 161 transfer->ptr = &outputReport.data[1];
va009039 0:6b9154ed73c6 162 transfer->direction = HOST_TO_DEVICE;
va009039 0:6b9154ed73c6 163 transfer->notify = true;
va009039 0:6b9154ed73c6 164 success = true;
va009039 0:6b9154ed73c6 165 default:
va009039 0:6b9154ed73c6 166 break;
va009039 0:6b9154ed73c6 167 }
va009039 0:6b9154ed73c6 168 }
va009039 0:6b9154ed73c6 169
va009039 0:6b9154ed73c6 170 return success;
va009039 0:6b9154ed73c6 171 }
va009039 0:6b9154ed73c6 172
va009039 0:6b9154ed73c6 173
va009039 0:6b9154ed73c6 174 #define DEFAULT_CONFIGURATION (1)
va009039 0:6b9154ed73c6 175
va009039 0:6b9154ed73c6 176
va009039 0:6b9154ed73c6 177 // Called in ISR context
va009039 0:6b9154ed73c6 178 // Set configuration. Return false if the
va009039 0:6b9154ed73c6 179 // configuration is not supported
k4zuki 3:13462db266a8 180 bool USBDAP::USBCallback_setConfiguration(uint8_t configuration)
k4zuki 3:13462db266a8 181 {
va009039 0:6b9154ed73c6 182 if (configuration != DEFAULT_CONFIGURATION) {
va009039 0:6b9154ed73c6 183 return false;
va009039 0:6b9154ed73c6 184 }
va009039 0:6b9154ed73c6 185
va009039 0:6b9154ed73c6 186 // Configure endpoints > 0
va009039 0:6b9154ed73c6 187 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
va009039 0:6b9154ed73c6 188 addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
va009039 0:6b9154ed73c6 189
va009039 0:6b9154ed73c6 190 // We activate the endpoint to be able to recceive data
va009039 0:6b9154ed73c6 191 readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
va009039 0:6b9154ed73c6 192 return true;
va009039 0:6b9154ed73c6 193 }
va009039 0:6b9154ed73c6 194
va009039 0:6b9154ed73c6 195
k4zuki 3:13462db266a8 196 uint8_t * USBDAP::stringIinterfaceDesc()
k4zuki 3:13462db266a8 197 {
va009039 0:6b9154ed73c6 198 static uint8_t stringIinterfaceDescriptor[] = {
va009039 0:6b9154ed73c6 199 0x08, //bLength
va009039 0:6b9154ed73c6 200 STRING_DESCRIPTOR, //bDescriptorType 0x03
va009039 0:6b9154ed73c6 201 'H',0,'I',0,'D',0, //bString iInterface - HID
va009039 0:6b9154ed73c6 202 };
va009039 0:6b9154ed73c6 203 return stringIinterfaceDescriptor;
va009039 0:6b9154ed73c6 204 }
va009039 0:6b9154ed73c6 205
k4zuki 3:13462db266a8 206 uint8_t * USBDAP::stringIproductDesc()
k4zuki 3:13462db266a8 207 {
va009039 2:7dee016756ce 208 return _stringIproductDescriptor;
va009039 0:6b9154ed73c6 209 }
va009039 0:6b9154ed73c6 210
va009039 0:6b9154ed73c6 211 uint8_t * USBDAP::reportDesc() {
va009039 0:6b9154ed73c6 212 static uint8_t reportDescriptor[] = {
va009039 0:6b9154ed73c6 213 0x06, 0x00, 0xff,
va009039 0:6b9154ed73c6 214 0x09, 0x01, // usage
va009039 0:6b9154ed73c6 215 0xA1, 0x01, // Collection 0x01
va009039 0:6b9154ed73c6 216 0x15, 0x00, // logical minimum = 0
va009039 0:6b9154ed73c6 217 0x26, 0xFF, 0x00, // logical maximum = 255
va009039 0:6b9154ed73c6 218 0x75, 0x08, // report size = 8 bits
va009039 0:6b9154ed73c6 219 0x95, 0x40, // report count
va009039 0:6b9154ed73c6 220 0x09, 0x01, // usage
va009039 0:6b9154ed73c6 221 0x81, 0x02, // Input (array)
va009039 0:6b9154ed73c6 222 0x95, 0x40, // report count
va009039 0:6b9154ed73c6 223 0x09, 0x01, // usage
va009039 0:6b9154ed73c6 224 0x91, 0x02, // Output (array)
va009039 0:6b9154ed73c6 225 0x95, 0x01, // report count
va009039 0:6b9154ed73c6 226 0x09, 0x01, // usage
va009039 0:6b9154ed73c6 227 0xb1, 0x02,
va009039 0:6b9154ed73c6 228 0xC0 // end collection
va009039 0:6b9154ed73c6 229 };
va009039 0:6b9154ed73c6 230 reportLength = sizeof(reportDescriptor);
va009039 0:6b9154ed73c6 231 return reportDescriptor;
va009039 0:6b9154ed73c6 232 }
va009039 0:6b9154ed73c6 233
va009039 0:6b9154ed73c6 234 #define DEFAULT_CONFIGURATION (1)
va009039 0:6b9154ed73c6 235 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
va009039 0:6b9154ed73c6 236 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
va009039 0:6b9154ed73c6 237 + (1 * HID_DESCRIPTOR_LENGTH) \
va009039 0:6b9154ed73c6 238 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
va009039 0:6b9154ed73c6 239
k4zuki 3:13462db266a8 240 uint8_t * USBDAP::configurationDesc()
k4zuki 3:13462db266a8 241 {
va009039 0:6b9154ed73c6 242 static uint8_t configurationDescriptor[] = {
va009039 0:6b9154ed73c6 243 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
va009039 0:6b9154ed73c6 244 CONFIGURATION_DESCRIPTOR, // bDescriptorType
va009039 0:6b9154ed73c6 245 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
va009039 0:6b9154ed73c6 246 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
va009039 0:6b9154ed73c6 247 0x01, // bNumInterfaces
va009039 0:6b9154ed73c6 248 DEFAULT_CONFIGURATION, // bConfigurationValue
va009039 0:6b9154ed73c6 249 0x00, // iConfiguration
va009039 0:6b9154ed73c6 250 C_RESERVED | C_SELF_POWERED, // bmAttributes
va009039 0:6b9154ed73c6 251 C_POWER(0), // bMaxPower
va009039 0:6b9154ed73c6 252
va009039 0:6b9154ed73c6 253 INTERFACE_DESCRIPTOR_LENGTH, // bLength
va009039 0:6b9154ed73c6 254 INTERFACE_DESCRIPTOR, // bDescriptorType
va009039 0:6b9154ed73c6 255 0x00, // bInterfaceNumber
va009039 0:6b9154ed73c6 256 0x00, // bAlternateSetting
va009039 0:6b9154ed73c6 257 0x02, // bNumEndpoints
va009039 0:6b9154ed73c6 258 HID_CLASS, // bInterfaceClass
va009039 0:6b9154ed73c6 259 HID_SUBCLASS_NONE, // bInterfaceSubClass
va009039 0:6b9154ed73c6 260 HID_PROTOCOL_NONE, // bInterfaceProtocol
va009039 0:6b9154ed73c6 261 0x00, // iInterface
va009039 0:6b9154ed73c6 262 HID_DESCRIPTOR_LENGTH, // bLength
va009039 0:6b9154ed73c6 263 HID_DESCRIPTOR, // bDescriptorType
va009039 0:6b9154ed73c6 264 LSB(HID_VERSION_1_11), // bcdHID (LSB)
va009039 0:6b9154ed73c6 265 MSB(HID_VERSION_1_11), // bcdHID (MSB)
va009039 0:6b9154ed73c6 266 0x00, // bCountryCode
va009039 0:6b9154ed73c6 267 0x01, // bNumDescriptors
va009039 0:6b9154ed73c6 268 REPORT_DESCRIPTOR, // bDescriptorType
va009039 0:6b9154ed73c6 269 LSB(this->reportDescLength()), // wDescriptorLength (LSB)
va009039 0:6b9154ed73c6 270 MSB(this->reportDescLength()), // wDescriptorLength (MSB)
va009039 0:6b9154ed73c6 271
va009039 0:6b9154ed73c6 272 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
va009039 0:6b9154ed73c6 273 ENDPOINT_DESCRIPTOR, // bDescriptorType
va009039 0:6b9154ed73c6 274 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
va009039 0:6b9154ed73c6 275 E_INTERRUPT, // bmAttributes
va009039 0:6b9154ed73c6 276 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
va009039 0:6b9154ed73c6 277 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
va009039 0:6b9154ed73c6 278 1, // bInterval (milliseconds)
va009039 0:6b9154ed73c6 279
va009039 0:6b9154ed73c6 280 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
va009039 0:6b9154ed73c6 281 ENDPOINT_DESCRIPTOR, // bDescriptorType
va009039 0:6b9154ed73c6 282 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
va009039 0:6b9154ed73c6 283 E_INTERRUPT, // bmAttributes
va009039 0:6b9154ed73c6 284 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
va009039 0:6b9154ed73c6 285 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
va009039 0:6b9154ed73c6 286 1, // bInterval (milliseconds)
va009039 0:6b9154ed73c6 287 };
va009039 0:6b9154ed73c6 288 return configurationDescriptor;
va009039 0:6b9154ed73c6 289 }