USB Device library for the STM32F103 with USB Device Firmware Upgrade (DFU) runtime support.
Dependents: STM32F103C8T6_WebUSBDFU STM32F103C8T6_USBDFU STM32F103C8T6_USBDFU dfu_usb_stm32f103
Fork of USBDevice_STM32F103 by
USBDFU/USBDFU.h@66:390c4a31db54, 2016-08-29 (annotated)
- Committer:
- devanlai
- Date:
- Mon Aug 29 00:47:17 2016 +0000
- Revision:
- 66:390c4a31db54
- Child:
- 67:39396cc073f2
Add USB DFU Runtime implementation (only supports detach requests)
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 | #ifndef USB_DFU_H |
devanlai | 66:390c4a31db54 | 20 | #define USB_DFU_H |
devanlai | 66:390c4a31db54 | 21 | |
devanlai | 66:390c4a31db54 | 22 | /* These headers are included for child class. */ |
devanlai | 66:390c4a31db54 | 23 | #include "USBEndpoints.h" |
devanlai | 66:390c4a31db54 | 24 | #include "USBDescriptor.h" |
devanlai | 66:390c4a31db54 | 25 | #include "USBDevice_Types.h" |
devanlai | 66:390c4a31db54 | 26 | |
devanlai | 66:390c4a31db54 | 27 | #include "USBDevice.h" |
devanlai | 66:390c4a31db54 | 28 | |
devanlai | 66:390c4a31db54 | 29 | #include "Callback.h" |
devanlai | 66:390c4a31db54 | 30 | |
devanlai | 66:390c4a31db54 | 31 | |
devanlai | 66:390c4a31db54 | 32 | class USBDFU: public USBDevice { |
devanlai | 66:390c4a31db54 | 33 | public: |
devanlai | 66:390c4a31db54 | 34 | |
devanlai | 66:390c4a31db54 | 35 | /** |
devanlai | 66:390c4a31db54 | 36 | * Constructor |
devanlai | 66:390c4a31db54 | 37 | * |
devanlai | 66:390c4a31db54 | 38 | * @param vendor_id Your vendor_id |
devanlai | 66:390c4a31db54 | 39 | * @param product_id Your product_id |
devanlai | 66:390c4a31db54 | 40 | * @param product_release Your product_release |
devanlai | 66:390c4a31db54 | 41 | * @param connect Connect the device |
devanlai | 66:390c4a31db54 | 42 | */ |
devanlai | 66:390c4a31db54 | 43 | USBDFU(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001, bool connect = true); |
devanlai | 66:390c4a31db54 | 44 | |
devanlai | 66:390c4a31db54 | 45 | /** |
devanlai | 66:390c4a31db54 | 46 | * Attach a callback called when a DFU detach request is received |
devanlai | 66:390c4a31db54 | 47 | * |
devanlai | 66:390c4a31db54 | 48 | * @param tptr pointer to the object to call the member function on |
devanlai | 66:390c4a31db54 | 49 | * @param mptr pointer to the member function to be called |
devanlai | 66:390c4a31db54 | 50 | */ |
devanlai | 66:390c4a31db54 | 51 | template<typename T> |
devanlai | 66:390c4a31db54 | 52 | void attach(T* tptr, void (T::*mptr)(void)) { |
devanlai | 66:390c4a31db54 | 53 | if((mptr != NULL) && (tptr != NULL)) { |
devanlai | 66:390c4a31db54 | 54 | detach.attach(tptr, mptr); |
devanlai | 66:390c4a31db54 | 55 | } |
devanlai | 66:390c4a31db54 | 56 | } |
devanlai | 66:390c4a31db54 | 57 | |
devanlai | 66:390c4a31db54 | 58 | /** |
devanlai | 66:390c4a31db54 | 59 | * Attach a callback called when a DFU detach request is received |
devanlai | 66:390c4a31db54 | 60 | * |
devanlai | 66:390c4a31db54 | 61 | * @param func function pointer |
devanlai | 66:390c4a31db54 | 62 | */ |
devanlai | 66:390c4a31db54 | 63 | void attach(Callback<void()> func); |
devanlai | 66:390c4a31db54 | 64 | |
devanlai | 66:390c4a31db54 | 65 | protected: |
devanlai | 66:390c4a31db54 | 66 | uint16_t reportLength; |
devanlai | 66:390c4a31db54 | 67 | |
devanlai | 66:390c4a31db54 | 68 | /* |
devanlai | 66:390c4a31db54 | 69 | * Get string product descriptor |
devanlai | 66:390c4a31db54 | 70 | * |
devanlai | 66:390c4a31db54 | 71 | * @returns pointer to the string product descriptor |
devanlai | 66:390c4a31db54 | 72 | */ |
devanlai | 66:390c4a31db54 | 73 | virtual uint8_t * stringIproductDesc(); |
devanlai | 66:390c4a31db54 | 74 | |
devanlai | 66:390c4a31db54 | 75 | /* |
devanlai | 66:390c4a31db54 | 76 | * Get string interface descriptor |
devanlai | 66:390c4a31db54 | 77 | * |
devanlai | 66:390c4a31db54 | 78 | * @returns pointer to the string interface descriptor |
devanlai | 66:390c4a31db54 | 79 | */ |
devanlai | 66:390c4a31db54 | 80 | virtual uint8_t * stringIinterfaceDesc(); |
devanlai | 66:390c4a31db54 | 81 | |
devanlai | 66:390c4a31db54 | 82 | /* |
devanlai | 66:390c4a31db54 | 83 | * Get configuration descriptor |
devanlai | 66:390c4a31db54 | 84 | * |
devanlai | 66:390c4a31db54 | 85 | * @returns pointer to the configuration descriptor |
devanlai | 66:390c4a31db54 | 86 | */ |
devanlai | 66:390c4a31db54 | 87 | virtual uint8_t * configurationDesc(); |
devanlai | 66:390c4a31db54 | 88 | |
devanlai | 66:390c4a31db54 | 89 | /* |
devanlai | 66:390c4a31db54 | 90 | * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context |
devanlai | 66:390c4a31db54 | 91 | * This is used to handle extensions to standard requests |
devanlai | 66:390c4a31db54 | 92 | * and class specific requests |
devanlai | 66:390c4a31db54 | 93 | * |
devanlai | 66:390c4a31db54 | 94 | * @returns true if class handles this request |
devanlai | 66:390c4a31db54 | 95 | */ |
devanlai | 66:390c4a31db54 | 96 | virtual bool USBCallback_request(); |
devanlai | 66:390c4a31db54 | 97 | |
devanlai | 66:390c4a31db54 | 98 | |
devanlai | 66:390c4a31db54 | 99 | /* |
devanlai | 66:390c4a31db54 | 100 | * Called by USBDevice layer. Set configuration of the device. |
devanlai | 66:390c4a31db54 | 101 | * For instance, you can add all endpoints that you need on this function. |
devanlai | 66:390c4a31db54 | 102 | * |
devanlai | 66:390c4a31db54 | 103 | * @param configuration Number of the configuration |
devanlai | 66:390c4a31db54 | 104 | * @returns true if class handles this request |
devanlai | 66:390c4a31db54 | 105 | */ |
devanlai | 66:390c4a31db54 | 106 | virtual bool USBCallback_setConfiguration(uint8_t configuration); |
devanlai | 66:390c4a31db54 | 107 | |
devanlai | 66:390c4a31db54 | 108 | enum DFURequest { |
devanlai | 66:390c4a31db54 | 109 | DFU_DETACH, |
devanlai | 66:390c4a31db54 | 110 | DFU_DNLOAD, |
devanlai | 66:390c4a31db54 | 111 | DFU_UPLOAD, |
devanlai | 66:390c4a31db54 | 112 | DFU_GETSTATUS, |
devanlai | 66:390c4a31db54 | 113 | DFU_CLRSTATUS, |
devanlai | 66:390c4a31db54 | 114 | DFU_GETSTATE, |
devanlai | 66:390c4a31db54 | 115 | DFU_ABORT, |
devanlai | 66:390c4a31db54 | 116 | }; |
devanlai | 66:390c4a31db54 | 117 | |
devanlai | 66:390c4a31db54 | 118 | enum DFUClass { |
devanlai | 66:390c4a31db54 | 119 | DFU_CLASS_APP_SPECIFIC = 0xFE, |
devanlai | 66:390c4a31db54 | 120 | }; |
devanlai | 66:390c4a31db54 | 121 | |
devanlai | 66:390c4a31db54 | 122 | enum DFUSubClass { |
devanlai | 66:390c4a31db54 | 123 | DFU_SUBCLASS_DFU = 0x01, |
devanlai | 66:390c4a31db54 | 124 | }; |
devanlai | 66:390c4a31db54 | 125 | |
devanlai | 66:390c4a31db54 | 126 | enum DFUProtocol { |
devanlai | 66:390c4a31db54 | 127 | DFU_PROTO_RUNTIME = 0x01, |
devanlai | 66:390c4a31db54 | 128 | DFU_PROTO_DFU = 0x02 |
devanlai | 66:390c4a31db54 | 129 | }; |
devanlai | 66:390c4a31db54 | 130 | |
devanlai | 66:390c4a31db54 | 131 | enum DFUDescriptorType { |
devanlai | 66:390c4a31db54 | 132 | DFU_DESCRIPTOR = 0x21, |
devanlai | 66:390c4a31db54 | 133 | }; |
devanlai | 66:390c4a31db54 | 134 | |
devanlai | 66:390c4a31db54 | 135 | enum DFUAttributes { |
devanlai | 66:390c4a31db54 | 136 | DFU_ATTR_CAN_DOWNLOAD = 0x01, |
devanlai | 66:390c4a31db54 | 137 | DFU_ATTR_CAN_UPLOAD = 0x02, |
devanlai | 66:390c4a31db54 | 138 | DFU_ATTR_MANIFEST_TOLERANT = 0x04, |
devanlai | 66:390c4a31db54 | 139 | DFU_ATTR_WILL_DETACH = 0x08, |
devanlai | 66:390c4a31db54 | 140 | }; |
devanlai | 66:390c4a31db54 | 141 | |
devanlai | 66:390c4a31db54 | 142 | enum DFUVersion { |
devanlai | 66:390c4a31db54 | 143 | DFU_VERSION_1_00 = 0x0100, |
devanlai | 66:390c4a31db54 | 144 | DFUSE_VERSION_1_1A = 0x011A, |
devanlai | 66:390c4a31db54 | 145 | }; |
devanlai | 66:390c4a31db54 | 146 | |
devanlai | 66:390c4a31db54 | 147 | private: |
devanlai | 66:390c4a31db54 | 148 | Callback<void()> detach; |
devanlai | 66:390c4a31db54 | 149 | |
devanlai | 66:390c4a31db54 | 150 | static void no_op() {}; |
devanlai | 66:390c4a31db54 | 151 | }; |
devanlai | 66:390c4a31db54 | 152 | |
devanlai | 66:390c4a31db54 | 153 | #endif |