Suga koubou / Mbed 2 deprecated USB_CDC_MSD_Hello

Dependencies:   ChaNFSSD mbed ChaNFS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBCDC.cpp Source File

USBCDC.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 "USBCDC.h"
00021 #include "USBBusInterface.h"
00022 
00023 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
00024 
00025 #define DEFAULT_CONFIGURATION (1)
00026 
00027 #define CDC_SET_LINE_CODING        0x20
00028 #define CDC_GET_LINE_CODING        0x21
00029 #define CDC_SET_CONTROL_LINE_STATE 0x22
00030 
00031 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
00032 
00033 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
00034     USBDevice::connect();
00035 }
00036 
00037 bool USBCDC::USBCallback_request(void) {
00038     /* Called in ISR context */
00039 
00040     bool success = false;
00041     CONTROL_TRANSFER * transfer = getTransferPtr();
00042 
00043     /* Process class-specific requests */
00044 
00045     if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
00046         switch (transfer->setup.bRequest) {
00047             case CDC_GET_LINE_CODING:
00048                 transfer->remaining = 7;
00049                 transfer->ptr = cdc_line_coding;
00050                 transfer->direction = DEVICE_TO_HOST;
00051                 success = true;
00052                 break;
00053             case CDC_SET_LINE_CODING:
00054                 transfer->remaining = 7;
00055                 success = true;
00056                 break;
00057             case CDC_SET_CONTROL_LINE_STATE:
00058                 success = true;
00059                 break;
00060             default:
00061                 break;
00062         }
00063     }
00064 
00065     return success;
00066 }
00067 
00068 
00069 // Called in ISR context
00070 // Set configuration. Return false if the
00071 // configuration is not supported.
00072 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
00073     if (configuration != DEFAULT_CONFIGURATION) {
00074         return false;
00075     }
00076 
00077     // Configure endpoints > 0
00078     addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
00079     addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
00080     addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00081 
00082     // We activate the endpoint to be able to recceive data
00083     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00084     return true;
00085 }
00086 
00087 bool USBCDC::send(uint8_t * buffer, uint16_t size) {
00088     return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
00089 }
00090 
00091 bool USBCDC::readEP(uint8_t * buffer, uint16_t * size) {
00092     if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
00093         return false;
00094     if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
00095         return false;
00096     return true;
00097 }
00098 
00099 bool USBCDC::readEP_NB(uint8_t * buffer, uint16_t * size) {
00100     if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
00101         return false;
00102     if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
00103         return false;
00104     return true;
00105 }
00106 
00107 
00108 uint8_t * USBCDC::deviceDesc() {
00109     static uint8_t deviceDescriptor[] = {
00110         18,                   // bLength
00111         1,                    // bDescriptorType
00112         0x10, 0x01,           // bcdUSB
00113         2,                    // bDeviceClass
00114         0,                    // bDeviceSubClass
00115         0,                    // bDeviceProtocol
00116         MAX_PACKET_SIZE_EP0,  // bMaxPacketSize0
00117         LSB(VENDOR_ID), MSB(VENDOR_ID),  // idVendor
00118         LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
00119         0x00, 0x01,           // bcdDevice
00120         1,                    // iManufacturer
00121         2,                    // iProduct
00122         3,                    // iSerialNumber
00123         1                     // bNumConfigurations
00124     };
00125     return deviceDescriptor;
00126 }
00127 
00128 uint8_t * USBCDC::stringIinterfaceDesc() {
00129     static uint8_t stringIinterfaceDescriptor[] = {
00130         0x08,
00131         STRING_DESCRIPTOR,
00132         'C',0,'D',0,'C',0,
00133     };
00134     return stringIinterfaceDescriptor;
00135 }
00136 
00137 uint8_t * USBCDC::stringIproductDesc() {
00138     static uint8_t stringIproductDescriptor[] = {
00139         0x16,
00140         STRING_DESCRIPTOR,
00141         'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
00142     };
00143     return stringIproductDescriptor;
00144 }
00145 
00146 
00147 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
00148 
00149 uint8_t * USBCDC::configurationDesc() {
00150     static uint8_t configDescriptor[] = {
00151         9,                      // bLength;
00152         2,                      // bDescriptorType;
00153         LSB(CONFIG1_DESC_SIZE), // wTotalLength
00154         MSB(CONFIG1_DESC_SIZE),
00155         2,                      // bNumInterfaces
00156         1,                      // bConfigurationValue
00157         0,                      // iConfiguration
00158         0x80,                   // bmAttributes
00159         50,                     // bMaxPower
00160 
00161         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
00162         9,                      // bLength
00163         4,                      // bDescriptorType
00164         0,                      // bInterfaceNumber
00165         0,                      // bAlternateSetting
00166         1,                      // bNumEndpoints
00167         0x02,                   // bInterfaceClass
00168         0x02,                   // bInterfaceSubClass
00169         0x01,                   // bInterfaceProtocol
00170         0,                      // iInterface
00171 
00172         // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
00173         5,                      // bFunctionLength
00174         0x24,                   // bDescriptorType
00175         0x00,                   // bDescriptorSubtype
00176         0x10, 0x01,             // bcdCDC
00177 
00178         // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
00179         5,                      // bFunctionLength
00180         0x24,                   // bDescriptorType
00181         0x01,                   // bDescriptorSubtype
00182         0x03,                   // bmCapabilities
00183         1,                      // bDataInterface
00184 
00185         // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
00186         4,                      // bFunctionLength
00187         0x24,                   // bDescriptorType
00188         0x02,                   // bDescriptorSubtype
00189         0x06,                   // bmCapabilities
00190 
00191         // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
00192         5,                      // bFunctionLength
00193         0x24,                   // bDescriptorType
00194         0x06,                   // bDescriptorSubtype
00195         0,                      // bMasterInterface
00196         1,                      // bSlaveInterface0
00197 
00198         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
00199         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00200         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00201         PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
00202         E_INTERRUPT,                    // bmAttributes (0x03=intr)
00203         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00204         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00205         16,                             // bInterval
00206 
00207 
00208 
00209 
00210         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
00211         9,          // bLength
00212         4,          // bDescriptorType
00213         1,          // bInterfaceNumber
00214         0,          // bAlternateSetting
00215         2,          // bNumEndpoints
00216         0x0A,       // bInterfaceClass
00217         0x00,       // bInterfaceSubClass
00218         0x00,       // bInterfaceProtocol
00219         0,          // iInterface
00220 
00221         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
00222         7,                      // bLength
00223         5,                      // bDescriptorType
00224         PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
00225         0x02,                   // bmAttributes (0x02=bulk)
00226         LSB(MAX_PACKET_SIZE_EPBULK),    // wMaxPacketSize (LSB)
00227         MSB(MAX_PACKET_SIZE_EPBULK),    // wMaxPacketSize (MSB)
00228         0,                      // bInterval
00229 
00230         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
00231         7,                      // bLength
00232         5,                      // bDescriptorType
00233         PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress
00234         0x02,                   // bmAttributes (0x02=bulk)
00235         LSB(MAX_PACKET_SIZE_EPBULK),    // wMaxPacketSize (LSB)
00236         MSB(MAX_PACKET_SIZE_EPBULK),     // wMaxPacketSize (MSB)
00237         0                       // bInterval
00238     };
00239     return configDescriptor;
00240 }