This fork re-enables FRDM boards and adds WebUSB CDC functionality

Fork of USBDevice_STM32F103 by Devan Lai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBDFU.cpp Source File

USBDFU.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "stdint.h"
00020 #include "USBHAL.h"
00021 #include "USBDFU.h"
00022 #include "DFU.h"
00023 
00024 USBDFU::USBDFU(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect)
00025     : USBDevice(vendor_id, product_id, product_release),
00026       detach(no_op)
00027 {
00028     if (connect) {
00029         USBDevice::connect();
00030     }
00031 }
00032 
00033 void USBDFU::attach(Callback<void()> func) {
00034     if (func) {
00035         detach.attach(func);
00036     } else {
00037         detach.attach(no_op);
00038     }
00039 }
00040 
00041 //
00042 //  Route callbacks from lower layers to class(es)
00043 //
00044 
00045 
00046 // Called in ISR context
00047 // Called by USBDevice on Endpoint0 request
00048 // This is used to handle extensions to standard requests
00049 // and class specific requests
00050 // Return true if class handles this request
00051 bool USBDFU::USBCallback_request() {
00052     bool success = false;
00053     CONTROL_TRANSFER * transfer = getTransferPtr();
00054 
00055     // Process class-specific requests
00056     if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
00057     {
00058         switch (transfer->setup.bRequest)
00059         {
00060             case DFU_DETACH:
00061                  detach.call();
00062                  success = true;
00063             default:
00064                 break;
00065         }
00066     }
00067 
00068     return success;
00069 }
00070 
00071 
00072 #define DEFAULT_CONFIGURATION (1)
00073 
00074 
00075 // Called in ISR context
00076 // Set configuration. Return false if the
00077 // configuration is not supported
00078 bool USBDFU::USBCallback_setConfiguration(uint8_t configuration) {
00079     if (configuration != DEFAULT_CONFIGURATION) {
00080         return false;
00081     }
00082 
00083     return true;
00084 }
00085 
00086 
00087 uint8_t * USBDFU::stringIinterfaceDesc() {
00088     static uint8_t stringIinterfaceDescriptor[] = {
00089         0x08,               //bLength
00090         STRING_DESCRIPTOR,  //bDescriptorType 0x03
00091         'D',0,'F',0,'U',0,  //bString iInterface - DFU
00092     };
00093     return stringIinterfaceDescriptor;
00094 }
00095 
00096 uint8_t * USBDFU::stringIproductDesc() {
00097     static uint8_t stringIproductDescriptor[] = {
00098         0x16,                                                       //bLength
00099         STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
00100         'D',0,'F',0,'U',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - DFU device
00101     };
00102     return stringIproductDescriptor;
00103 }
00104 
00105 #define DEFAULT_CONFIGURATION (1)
00106 #define DFU_DESCRIPTOR_LENGTH (9)
00107 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
00108                                + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
00109                                + (1 * DFU_DESCRIPTOR_LENGTH))
00110 
00111 #define DETACH_TIMEOUT 255
00112 #define DFU_TRANSFER_SIZE 1024
00113 
00114 uint8_t * USBDFU::configurationDesc() {
00115     static uint8_t configurationDescriptor[] = {
00116         CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
00117         CONFIGURATION_DESCRIPTOR,       // bDescriptorType
00118         LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
00119         MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
00120         0x01,                           // bNumInterfaces
00121         DEFAULT_CONFIGURATION,          // bConfigurationValue
00122         STRING_OFFSET_ICONFIGURATION,   // iConfiguration
00123         C_RESERVED | C_SELF_POWERED,    // bmAttributes
00124         C_POWER(0),                     // bMaxPower
00125 
00126         INTERFACE_DESCRIPTOR_LENGTH,    // bLength
00127         INTERFACE_DESCRIPTOR,           // bDescriptorType
00128         0x00,                           // bInterfaceNumber
00129         0x00,                           // bAlternateSetting
00130         0x00,                           // bNumEndpoints
00131         DFU_CLASS_APP_SPECIFIC,         // bInterfaceClass
00132         DFU_SUBCLASS_DFU,               // bInterfaceSubClass
00133         DFU_PROTO_RUNTIME,              // bInterfaceProtocol
00134         STRING_OFFSET_IINTERFACE,       // iInterface
00135 
00136         DFU_DESCRIPTOR_LENGTH,          // bLength
00137         DFU_DESCRIPTOR,                 // bDescriptorType
00138         (DFU_ATTR_WILL_DETACH           // bmAttributes
00139         |DFU_ATTR_CAN_DOWNLOAD),
00140         LSB(DETACH_TIMEOUT),            // wDetachTimeOut (LSB)
00141         MSB(DETACH_TIMEOUT),            // wDetachTimeOut (MSB)
00142         LSB(DFU_TRANSFER_SIZE),         // wTransferSize (LSB)
00143         MSB(DFU_TRANSFER_SIZE),         // wTransferSize (MSB)
00144         LSB(DFU_VERSION_1_00),          // bcdDFUVersion (LSB)
00145         MSB(DFU_VERSION_1_00),          // bcdDFUVersion (MSB)
00146     };
00147     return configurationDescriptor;
00148 }