I changed one line of code in the file with path name: USBDeviceHT/targets/TARGET_Maxim

Fork of USBDeviceHT by Helmut Tschemernjak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBCDC.cpp Source File

USBCDC.cpp

00001 #include "mbed.h"
00002 #include "PinMap.h"
00003 
00004 #ifdef FEATURE_USBSERIAL
00005 
00006 /* Copyright (c) 2010-2011 mbed.org, MIT License
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00009 * and associated documentation files (the "Software"), to deal in the Software without
00010 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00011 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00012 * Software is furnished to do so, subject to the following conditions:
00013 *
00014 * The above copyright notice and this permission notice shall be included in all copies or
00015 * substantial portions of the Software.
00016 *
00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00018 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00020 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 */
00023 
00024 #include "stdint.h"
00025 #include "USBCDC.h"
00026 
00027 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
00028 
00029 #define DEFAULT_CONFIGURATION (1)
00030 
00031 #define CDC_SET_LINE_CODING        0x20
00032 #define CDC_GET_LINE_CODING        0x21
00033 #define CDC_SET_CONTROL_LINE_STATE 0x22
00034 
00035 // Control Line State bits
00036 #define CLS_DTR   (1 << 0)
00037 #define CLS_RTS   (1 << 1)
00038 
00039 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
00040 
00041 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking): USBDevice(vendor_id, product_id, product_release) {
00042     terminal_connected = false;
00043     USBDevice::connect(connect_blocking);
00044 }
00045 
00046 void USBCDC::USBCallback_busReset(void) {
00047     terminal_connected = false;
00048 };
00049 
00050 bool USBCDC::USBCallback_request(void) {
00051     /* Called in ISR context */
00052 
00053     bool success = false;
00054     CONTROL_TRANSFER * transfer = getTransferPtr();
00055 
00056     /* Process class-specific requests */
00057 
00058     if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
00059         switch (transfer->setup.bRequest) {
00060             case CDC_GET_LINE_CODING:
00061                 transfer->remaining = 7;
00062                 transfer->ptr = cdc_line_coding;
00063                 transfer->direction = DEVICE_TO_HOST;
00064                 success = true;
00065                 break;
00066             case CDC_SET_LINE_CODING:
00067                 transfer->remaining = 7;
00068                 transfer->notify = true;
00069                 success = true;
00070                 break;
00071             case CDC_SET_CONTROL_LINE_STATE:
00072                 if (transfer->setup.wValue & CLS_DTR) {
00073                     terminal_connected = true;
00074                 } else {
00075                     terminal_connected = false;
00076                 }
00077                 success = true;
00078                 break;
00079             default:
00080                 break;
00081         }
00082     }
00083 
00084     return success;
00085 }
00086 
00087 void USBCDC::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) {
00088     // Request of setting line coding has 7 bytes
00089     if (length != 7) {
00090         return;
00091     }
00092 
00093     CONTROL_TRANSFER * transfer = getTransferPtr();
00094 
00095     /* Process class-specific requests */
00096     if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
00097         if (transfer->setup.bRequest == CDC_SET_LINE_CODING) {
00098             if (memcmp(cdc_line_coding, buf, 7)) {
00099                 memcpy(cdc_line_coding, buf, 7);
00100 
00101                 int baud = buf[0] + (buf[1] << 8)
00102                          + (buf[2] << 16) + (buf[3] << 24);
00103                 int stop = buf[4];
00104                 int bits = buf[6];
00105                 int parity = buf[5];
00106 
00107                 lineCodingChanged(baud, bits, parity, stop);
00108             }
00109         }
00110     }
00111 }
00112 
00113 // Called in ISR context
00114 // Set configuration. Return false if the
00115 // configuration is not supported.
00116 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
00117     if (configuration != DEFAULT_CONFIGURATION) {
00118         return false;
00119     }
00120 
00121     // Configure endpoints > 0
00122     addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
00123     addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
00124     addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00125 
00126     // We activate the endpoint to be able to recceive data
00127     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00128     return true;
00129 }
00130 
00131 bool USBCDC::send(uint8_t * buffer, uint32_t size) {
00132     return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
00133 }
00134 
00135 bool USBCDC::readEP(uint8_t * buffer, uint32_t * size) {
00136     if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
00137         return false;
00138     if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
00139         return false;
00140     return true;
00141 }
00142 
00143 bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
00144     if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
00145         return false;
00146     if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
00147         return false;
00148     return true;
00149 }
00150 
00151 
00152 const uint8_t * USBCDC::deviceDesc() {
00153     uint8_t deviceDescriptorTemp[] = {
00154         18,                   // bLength
00155         1,                    // bDescriptorType
00156         0x10, 0x01,           // bcdUSB
00157         2,                    // bDeviceClass
00158         0,                    // bDeviceSubClass
00159         0,                    // bDeviceProtocol
00160         MAX_PACKET_SIZE_EP0,  // bMaxPacketSize0
00161         (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)),  // idVendor
00162         (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct
00163         0x00, 0x01,           // bcdDevice
00164         1,                    // iManufacturer
00165         2,                    // iProduct
00166         3,                    // iSerialNumber
00167         1                     // bNumConfigurations
00168     };
00169     MBED_ASSERT(sizeof(deviceDescriptorTemp) == sizeof(deviceDescriptor));
00170     memcpy(deviceDescriptor, deviceDescriptorTemp, sizeof(deviceDescriptor));
00171     return deviceDescriptor;
00172 }
00173 
00174 const uint8_t * USBCDC::stringIinterfaceDesc() {
00175     static const uint8_t stringIinterfaceDescriptor[] = {
00176         0x08,
00177         STRING_DESCRIPTOR,
00178         'C',0,'D',0,'C',0,
00179     };
00180     return stringIinterfaceDescriptor;
00181 }
00182 
00183 const uint8_t * USBCDC::stringIproductDesc() {
00184     static const uint8_t stringIproductDescriptor[] = {
00185         0x16,
00186         STRING_DESCRIPTOR,
00187         'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
00188     };
00189     return stringIproductDescriptor;
00190 }
00191 
00192 
00193 #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
00194 
00195 const uint8_t * USBCDC::configurationDesc() {
00196     static const uint8_t configDescriptor[] = {
00197         // configuration descriptor
00198         9,                      // bLength
00199         2,                      // bDescriptorType
00200         LSB(CONFIG1_DESC_SIZE), // wTotalLength
00201         MSB(CONFIG1_DESC_SIZE),
00202         2,                      // bNumInterfaces
00203         1,                      // bConfigurationValue
00204         0,                      // iConfiguration
00205         0x80,                   // bmAttributes
00206         50,                     // bMaxPower
00207 
00208         // IAD to associate the two CDC interfaces
00209         0x08,                   // bLength
00210         0x0b,                   // bDescriptorType
00211         0x00,                   // bFirstInterface
00212         0x02,                   // bInterfaceCount
00213         0x02,                   // bFunctionClass
00214         0x02,                   // bFunctionSubClass
00215         0,                      // bFunctionProtocol
00216         0,                      // iFunction
00217 
00218         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
00219         9,                      // bLength
00220         4,                      // bDescriptorType
00221         0,                      // bInterfaceNumber
00222         0,                      // bAlternateSetting
00223         1,                      // bNumEndpoints
00224         0x02,                   // bInterfaceClass
00225         0x02,                   // bInterfaceSubClass
00226         0x01,                   // bInterfaceProtocol
00227         0,                      // iInterface
00228 
00229         // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
00230         5,                      // bFunctionLength
00231         0x24,                   // bDescriptorType
00232         0x00,                   // bDescriptorSubtype
00233         0x10, 0x01,             // bcdCDC
00234 
00235         // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
00236         5,                      // bFunctionLength
00237         0x24,                   // bDescriptorType
00238         0x01,                   // bDescriptorSubtype
00239         0x03,                   // bmCapabilities
00240         1,                      // bDataInterface
00241 
00242         // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
00243         4,                      // bFunctionLength
00244         0x24,                   // bDescriptorType
00245         0x02,                   // bDescriptorSubtype
00246         0x06,                   // bmCapabilities
00247 
00248         // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
00249         5,                      // bFunctionLength
00250         0x24,                   // bDescriptorType
00251         0x06,                   // bDescriptorSubtype
00252         0,                      // bMasterInterface
00253         1,                      // bSlaveInterface0
00254 
00255         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
00256         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00257         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00258         PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
00259         E_INTERRUPT,                    // bmAttributes (0x03=intr)
00260         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00261         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00262         16,                             // bInterval
00263 
00264 
00265 
00266 
00267         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
00268         9,                          // bLength
00269         4,                          // bDescriptorType
00270         1,                          // bInterfaceNumber
00271         0,                          // bAlternateSetting
00272         2,                          // bNumEndpoints
00273         0x0A,                       // bInterfaceClass
00274         0x00,                       // bInterfaceSubClass
00275         0x00,                       // bInterfaceProtocol
00276         0,                          // iInterface
00277 
00278         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
00279         ENDPOINT_DESCRIPTOR_LENGTH, // bLength
00280         ENDPOINT_DESCRIPTOR,        // bDescriptorType
00281         PHY_TO_DESC(EPBULK_IN),     // bEndpointAddress
00282         E_BULK,                     // bmAttributes (0x02=bulk)
00283         LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
00284         MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
00285         0,                          // bInterval
00286 
00287         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
00288         ENDPOINT_DESCRIPTOR_LENGTH, // bLength
00289         ENDPOINT_DESCRIPTOR,        // bDescriptorType
00290         PHY_TO_DESC(EPBULK_OUT),    // bEndpointAddress
00291         E_BULK,                     // bmAttributes (0x02=bulk)
00292         LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB)
00293         MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB)
00294         0                           // bInterval
00295     };
00296     return configDescriptor;
00297 }
00298 
00299 #endif // FEATURE_USBSERIAL