Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBCDC.cpp Source File

USBCDC.cpp

00001 #include "stdint.h"
00002 #include "USBCDC.h"
00003 #include "USBBusInterface.h"
00004 #include "USBDevice.h"
00005 
00006 void USBCDC::attach(void (*fptr)(char*, int)) {
00007     cdc_evt = fptr;
00008 }
00009 
00010 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
00011     cdc_evt = NULL;
00012 }
00013 
00014 bool USBCDC::EPBULK_OUT_callback() {
00015     uint8_t buf[64];
00016     uint16_t len;
00017 
00018     read(EPBULK_OUT, buf, &len, MAX_PACKET_SIZE_EPBULK);
00019 
00020     if (cdc_evt) {
00021         cdc_evt((char*)buf, len);
00022     }
00023 
00024     // We reactivate the endpoint to receive next characters
00025     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00026     return true;
00027 }
00028 
00029 bool USBCDC::USBCallback_request () {
00030     bool success = false;
00031     CONTROL_TRANSFER *transfer = getTransferPtr();
00032     
00033 //    DBG("USBCallback_request: type %x, request %x\r\n", transfer->setup.bmRequestType.Type, transfer->setup.bRequest);
00034     /* Process standard requests */
00035     if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
00036     {
00037         switch (transfer->setup.bRequest)
00038         {
00039             default:
00040                 break;
00041         }
00042     }
00043 
00044     if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
00045     {
00046         switch (transfer->setup.bRequest)
00047         {
00048             case 0x20: // CDC set line coding
00049             case 0x22: // CDC set control line state
00050             case 0x23: // CDC Alt-B
00051 //                DBG("val=%x idx=%x len=%x\r\n", transfer->setup.wValue, transfer->setup.wIndex, transfer->setup.wLength);
00052                 success = true;
00053                 break;
00054             case 0x21: // CDC get line coding
00055                 static uint8_t LineConfig[] = {0x00, 0xc0, 0x01, 0x00, 0, 0, 8};
00056                 transfer->remaining = 7;
00057                 transfer->ptr = LineConfig;
00058                 transfer->direction = DEVICE_TO_HOST;
00059                 success = true;
00060                 break;
00061             default:
00062                 break;
00063         }
00064     }
00065 
00066     return success;
00067 }
00068 
00069 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
00070     // Called in ISR context
00071     // Set configuration. Return false if the
00072     // configuration is not supported.
00073     if (configuration != DEFAULT_CONFIGURATION) {
00074         return false;
00075     }
00076 
00077     addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
00078 
00079     // Configure endpoints > 0
00080     addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
00081     addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00082 
00083     // We activate the endpoint to be able to receive data
00084     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00085 
00086     return true;
00087 }
00088 
00089 
00090 uint8_t * USBCDC::stringIinterfaceDesc() {
00091     static uint8_t stringIinterfaceDescriptor[] = {
00092         14,                           //bLength
00093         STRING_DESCRIPTOR,              //bDescriptorType 0x03
00094         'C',0,'D',0,'C',0,'D',0,'E',0,'V',0   //bString iInterface - Audio
00095     };
00096     return stringIinterfaceDescriptor;
00097 }
00098 
00099 uint8_t * USBCDC::stringIproductDesc() {
00100     static uint8_t stringIproductDescriptor[] = {
00101         24,                                                       //bLength
00102         STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
00103         'M',0,'b',0,'e',0,'d',0,' ',0,'D',0,'e',0,'v',0,'i',0,'c',0,'e',0 //bString iProduct - Mbed Audio
00104     };
00105     return stringIproductDescriptor;
00106 }
00107 
00108 
00109 uint8_t * USBCDC::configurationDesc() {
00110     static uint8_t configDescriptor[] = {
00111         // configuration descriptor
00112         0x09, 0x02, 67, 0x00, 0x02, 0x01, 0x00, 0xC0, 0x32,
00113 
00114         // CDC
00115         // control class interface
00116         0x09, 0x04, 0x00, 0x00, 0x01, 0x02, 0x02, 0x01, 0x00,
00117         // header functional descriptor
00118         0x05, 0x24, 0x00, 0x10, 0x01,
00119         // call management functional descriptor
00120         0x05, 0x24, 0x01, 0x01, 0x01,
00121         // ACM functional descriptor
00122         0x04, 0x24, 0x02, 0x02,
00123         // union functional descriptor
00124         0x05, 0x24, 0x06, 0x00, 0x01,
00125         // notification EP
00126         0x07, 0x05, 0x81, 0x03, 0x10, 0x00, 0x0A,
00127         // data class interface descriptor
00128         0x09, 0x04, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x00, 0x00,
00129         // data EP OUT
00130         0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
00131         // data EP IN
00132         0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
00133     };
00134     return configDescriptor;
00135 }
00136 
00137 uint8_t * USBCDC::deviceDesc() {
00138     static uint8_t deviceDescriptor[] = {
00139         DEVICE_DESCRIPTOR_LENGTH,       /* bLength */
00140         DEVICE_DESCRIPTOR,              /* bDescriptorType */
00141         LSB(0x01),           /* bcdUSB (LSB) */
00142         MSB(0x01),           /* bcdUSB (MSB) */
00143         0x02,                           /* bDeviceClass */
00144         0x00,                           /* bDeviceSubClass */
00145         0x00,                           /* bDeviceprotocol */
00146         MAX_PACKET_SIZE_EP0,            /* bMaxPacketSize0 */
00147         LSB(VENDOR_ID),                 /* idVendor (LSB) */
00148         MSB(VENDOR_ID),                 /* idVendor (MSB) */
00149         LSB(PRODUCT_ID),                /* idProduct (LSB) */
00150         MSB(PRODUCT_ID),                /* idProduct (MSB) */
00151         LSB(PRODUCT_RELEASE),           /* bcdDevice (LSB) */
00152         MSB(PRODUCT_RELEASE),           /* bcdDevice (MSB) */
00153         STRING_OFFSET_IMANUFACTURER,    /* iManufacturer */
00154         STRING_OFFSET_IPRODUCT,         /* iProduct */
00155         STRING_OFFSET_ISERIAL,          /* iSerialNumber */
00156         0x01                            /* bNumConfigurations */
00157     };
00158     return deviceDescriptor;
00159 }