fork
Fork of USBDevice_STM32F103 by
USBDFU/USBDFU.cpp@71:9950749ae475, 2018-02-28 (annotated)
- Committer:
- lorded
- Date:
- Wed Feb 28 22:05:36 2018 +0000
- Revision:
- 71:9950749ae475
- Parent:
- 69:8d72761d52fd
USB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
devanlai | 66:390c4a31db54 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
devanlai | 66:390c4a31db54 | 2 | * |
devanlai | 66:390c4a31db54 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
devanlai | 66:390c4a31db54 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
devanlai | 66:390c4a31db54 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
devanlai | 66:390c4a31db54 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
devanlai | 66:390c4a31db54 | 7 | * Software is furnished to do so, subject to the following conditions: |
devanlai | 66:390c4a31db54 | 8 | * |
devanlai | 66:390c4a31db54 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
devanlai | 66:390c4a31db54 | 10 | * substantial portions of the Software. |
devanlai | 66:390c4a31db54 | 11 | * |
devanlai | 66:390c4a31db54 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
devanlai | 66:390c4a31db54 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
devanlai | 66:390c4a31db54 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
devanlai | 66:390c4a31db54 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
devanlai | 66:390c4a31db54 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
devanlai | 66:390c4a31db54 | 17 | */ |
devanlai | 66:390c4a31db54 | 18 | |
devanlai | 66:390c4a31db54 | 19 | #include "stdint.h" |
devanlai | 66:390c4a31db54 | 20 | #include "USBHAL.h" |
devanlai | 66:390c4a31db54 | 21 | #include "USBDFU.h" |
devanlai | 67:39396cc073f2 | 22 | #include "DFU.h" |
devanlai | 66:390c4a31db54 | 23 | |
devanlai | 66:390c4a31db54 | 24 | USBDFU::USBDFU(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect) |
devanlai | 66:390c4a31db54 | 25 | : USBDevice(vendor_id, product_id, product_release), |
devanlai | 66:390c4a31db54 | 26 | detach(no_op) |
devanlai | 66:390c4a31db54 | 27 | { |
devanlai | 66:390c4a31db54 | 28 | if (connect) { |
devanlai | 66:390c4a31db54 | 29 | USBDevice::connect(); |
devanlai | 66:390c4a31db54 | 30 | } |
devanlai | 66:390c4a31db54 | 31 | } |
devanlai | 66:390c4a31db54 | 32 | |
devanlai | 66:390c4a31db54 | 33 | void USBDFU::attach(Callback<void()> func) { |
devanlai | 69:8d72761d52fd | 34 | if (func) { |
devanlai | 66:390c4a31db54 | 35 | detach.attach(func); |
devanlai | 66:390c4a31db54 | 36 | } else { |
devanlai | 66:390c4a31db54 | 37 | detach.attach(no_op); |
devanlai | 66:390c4a31db54 | 38 | } |
devanlai | 66:390c4a31db54 | 39 | } |
devanlai | 66:390c4a31db54 | 40 | |
devanlai | 66:390c4a31db54 | 41 | // |
devanlai | 66:390c4a31db54 | 42 | // Route callbacks from lower layers to class(es) |
devanlai | 66:390c4a31db54 | 43 | // |
devanlai | 66:390c4a31db54 | 44 | |
devanlai | 66:390c4a31db54 | 45 | |
devanlai | 66:390c4a31db54 | 46 | // Called in ISR context |
devanlai | 66:390c4a31db54 | 47 | // Called by USBDevice on Endpoint0 request |
devanlai | 66:390c4a31db54 | 48 | // This is used to handle extensions to standard requests |
devanlai | 66:390c4a31db54 | 49 | // and class specific requests |
devanlai | 66:390c4a31db54 | 50 | // Return true if class handles this request |
devanlai | 66:390c4a31db54 | 51 | bool USBDFU::USBCallback_request() { |
devanlai | 66:390c4a31db54 | 52 | bool success = false; |
devanlai | 66:390c4a31db54 | 53 | CONTROL_TRANSFER * transfer = getTransferPtr(); |
devanlai | 66:390c4a31db54 | 54 | |
devanlai | 66:390c4a31db54 | 55 | // Process class-specific requests |
devanlai | 66:390c4a31db54 | 56 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) |
devanlai | 66:390c4a31db54 | 57 | { |
devanlai | 66:390c4a31db54 | 58 | switch (transfer->setup.bRequest) |
devanlai | 66:390c4a31db54 | 59 | { |
devanlai | 66:390c4a31db54 | 60 | case DFU_DETACH: |
devanlai | 66:390c4a31db54 | 61 | detach.call(); |
devanlai | 66:390c4a31db54 | 62 | success = true; |
devanlai | 66:390c4a31db54 | 63 | default: |
devanlai | 66:390c4a31db54 | 64 | break; |
devanlai | 66:390c4a31db54 | 65 | } |
devanlai | 66:390c4a31db54 | 66 | } |
devanlai | 66:390c4a31db54 | 67 | |
devanlai | 66:390c4a31db54 | 68 | return success; |
devanlai | 66:390c4a31db54 | 69 | } |
devanlai | 66:390c4a31db54 | 70 | |
devanlai | 66:390c4a31db54 | 71 | |
devanlai | 66:390c4a31db54 | 72 | #define DEFAULT_CONFIGURATION (1) |
devanlai | 66:390c4a31db54 | 73 | |
devanlai | 66:390c4a31db54 | 74 | |
devanlai | 66:390c4a31db54 | 75 | // Called in ISR context |
devanlai | 66:390c4a31db54 | 76 | // Set configuration. Return false if the |
devanlai | 66:390c4a31db54 | 77 | // configuration is not supported |
devanlai | 66:390c4a31db54 | 78 | bool USBDFU::USBCallback_setConfiguration(uint8_t configuration) { |
devanlai | 66:390c4a31db54 | 79 | if (configuration != DEFAULT_CONFIGURATION) { |
devanlai | 66:390c4a31db54 | 80 | return false; |
devanlai | 66:390c4a31db54 | 81 | } |
devanlai | 66:390c4a31db54 | 82 | |
devanlai | 66:390c4a31db54 | 83 | return true; |
devanlai | 66:390c4a31db54 | 84 | } |
devanlai | 66:390c4a31db54 | 85 | |
devanlai | 66:390c4a31db54 | 86 | |
devanlai | 66:390c4a31db54 | 87 | uint8_t * USBDFU::stringIinterfaceDesc() { |
devanlai | 66:390c4a31db54 | 88 | static uint8_t stringIinterfaceDescriptor[] = { |
devanlai | 66:390c4a31db54 | 89 | 0x08, //bLength |
devanlai | 66:390c4a31db54 | 90 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
devanlai | 66:390c4a31db54 | 91 | 'D',0,'F',0,'U',0, //bString iInterface - DFU |
devanlai | 66:390c4a31db54 | 92 | }; |
devanlai | 66:390c4a31db54 | 93 | return stringIinterfaceDescriptor; |
devanlai | 66:390c4a31db54 | 94 | } |
devanlai | 66:390c4a31db54 | 95 | |
devanlai | 66:390c4a31db54 | 96 | uint8_t * USBDFU::stringIproductDesc() { |
devanlai | 66:390c4a31db54 | 97 | static uint8_t stringIproductDescriptor[] = { |
devanlai | 66:390c4a31db54 | 98 | 0x16, //bLength |
devanlai | 66:390c4a31db54 | 99 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
devanlai | 66:390c4a31db54 | 100 | 'D',0,'F',0,'U',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - DFU device |
devanlai | 66:390c4a31db54 | 101 | }; |
devanlai | 66:390c4a31db54 | 102 | return stringIproductDescriptor; |
devanlai | 66:390c4a31db54 | 103 | } |
devanlai | 66:390c4a31db54 | 104 | |
devanlai | 66:390c4a31db54 | 105 | #define DEFAULT_CONFIGURATION (1) |
devanlai | 66:390c4a31db54 | 106 | #define DFU_DESCRIPTOR_LENGTH (9) |
devanlai | 66:390c4a31db54 | 107 | #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ |
devanlai | 66:390c4a31db54 | 108 | + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ |
devanlai | 66:390c4a31db54 | 109 | + (1 * DFU_DESCRIPTOR_LENGTH)) |
devanlai | 66:390c4a31db54 | 110 | |
devanlai | 66:390c4a31db54 | 111 | #define DETACH_TIMEOUT 255 |
devanlai | 66:390c4a31db54 | 112 | #define DFU_TRANSFER_SIZE 1024 |
devanlai | 66:390c4a31db54 | 113 | |
devanlai | 66:390c4a31db54 | 114 | uint8_t * USBDFU::configurationDesc() { |
devanlai | 66:390c4a31db54 | 115 | static uint8_t configurationDescriptor[] = { |
devanlai | 66:390c4a31db54 | 116 | CONFIGURATION_DESCRIPTOR_LENGTH,// bLength |
devanlai | 66:390c4a31db54 | 117 | CONFIGURATION_DESCRIPTOR, // bDescriptorType |
devanlai | 66:390c4a31db54 | 118 | LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) |
devanlai | 66:390c4a31db54 | 119 | MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) |
devanlai | 66:390c4a31db54 | 120 | 0x01, // bNumInterfaces |
devanlai | 66:390c4a31db54 | 121 | DEFAULT_CONFIGURATION, // bConfigurationValue |
devanlai | 66:390c4a31db54 | 122 | STRING_OFFSET_ICONFIGURATION, // iConfiguration |
devanlai | 66:390c4a31db54 | 123 | C_RESERVED | C_SELF_POWERED, // bmAttributes |
devanlai | 66:390c4a31db54 | 124 | C_POWER(0), // bMaxPower |
devanlai | 66:390c4a31db54 | 125 | |
devanlai | 66:390c4a31db54 | 126 | INTERFACE_DESCRIPTOR_LENGTH, // bLength |
devanlai | 66:390c4a31db54 | 127 | INTERFACE_DESCRIPTOR, // bDescriptorType |
devanlai | 66:390c4a31db54 | 128 | 0x00, // bInterfaceNumber |
devanlai | 66:390c4a31db54 | 129 | 0x00, // bAlternateSetting |
devanlai | 66:390c4a31db54 | 130 | 0x00, // bNumEndpoints |
devanlai | 66:390c4a31db54 | 131 | DFU_CLASS_APP_SPECIFIC, // bInterfaceClass |
devanlai | 66:390c4a31db54 | 132 | DFU_SUBCLASS_DFU, // bInterfaceSubClass |
devanlai | 66:390c4a31db54 | 133 | DFU_PROTO_RUNTIME, // bInterfaceProtocol |
devanlai | 66:390c4a31db54 | 134 | STRING_OFFSET_IINTERFACE, // iInterface |
devanlai | 66:390c4a31db54 | 135 | |
devanlai | 66:390c4a31db54 | 136 | DFU_DESCRIPTOR_LENGTH, // bLength |
devanlai | 66:390c4a31db54 | 137 | DFU_DESCRIPTOR, // bDescriptorType |
devanlai | 66:390c4a31db54 | 138 | (DFU_ATTR_WILL_DETACH // bmAttributes |
devanlai | 66:390c4a31db54 | 139 | |DFU_ATTR_CAN_DOWNLOAD), |
devanlai | 66:390c4a31db54 | 140 | LSB(DETACH_TIMEOUT), // wDetachTimeOut (LSB) |
devanlai | 66:390c4a31db54 | 141 | MSB(DETACH_TIMEOUT), // wDetachTimeOut (MSB) |
devanlai | 66:390c4a31db54 | 142 | LSB(DFU_TRANSFER_SIZE), // wTransferSize (LSB) |
devanlai | 66:390c4a31db54 | 143 | MSB(DFU_TRANSFER_SIZE), // wTransferSize (MSB) |
devanlai | 66:390c4a31db54 | 144 | LSB(DFU_VERSION_1_00), // bcdDFUVersion (LSB) |
devanlai | 66:390c4a31db54 | 145 | MSB(DFU_VERSION_1_00), // bcdDFUVersion (MSB) |
devanlai | 66:390c4a31db54 | 146 | }; |
devanlai | 66:390c4a31db54 | 147 | return configurationDescriptor; |
devanlai | 66:390c4a31db54 | 148 | } |