ADC Niose test Connect four analog signals to your MBED. and then run the Windows app. The four traces are displayed on an oscilloscope like display. I have used a USB HID DEVICE link, so connections to D+, D- are required. The MBED code is otherwise quite basic, So you can modify it to your own test needs. Additionaly, there is a 16 bit count value, in my MBED code Mainly to test if MSB & LSB are correct.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHID.cpp Source File

USBHID.cpp

00001 // USBHID.c
00002 // Human Interface Device (HID) class
00003 // Copyright (c) 2011 ARM Limited. All rights reserved.
00004 
00005 #include "stdint.h"
00006 #include "USBBusInterface.h"
00007 #include "USBHID.h"
00008 
00009 
00010 USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release)
00011 {
00012     output_length = output_report_length;
00013     input_length = input_report_length;
00014 }
00015 
00016 
00017 bool USBHID::send(HID_REPORT *report)
00018 {
00019     return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
00020 }
00021 
00022 
00023 bool USBHID::read(HID_REPORT *report)
00024 {
00025     uint16_t bytesRead = 0;
00026     bool result;
00027     result = USBDevice::read(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
00028     if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
00029         return false;
00030     report->length = bytesRead;
00031     return result;
00032 }
00033 
00034 
00035 bool USBHID::readNB(HID_REPORT *report)
00036 {
00037     uint16_t bytesRead = 0;
00038     bool result;
00039     result = USBDevice::readNB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
00040     report->length = bytesRead;
00041     if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
00042         return false;
00043     return result;
00044 }
00045 
00046 
00047 uint16_t USBHID::reportDescLength() {
00048     reportDesc();
00049     return reportLength;
00050 }
00051 
00052 
00053 
00054 //
00055 //  Route callbacks from lower layers to class(es)
00056 //
00057 
00058 
00059 // Called in ISR context
00060 // Called by USBDevice on Endpoint0 request
00061 // This is used to handle extensions to standard requests
00062 // and class specific requests
00063 // Return true if class handles this request
00064 bool USBHID::USBCallback_request() {
00065     bool success = false;
00066     CONTROL_TRANSFER * transfer = getTransferPtr();
00067     uint8_t *hidDescriptor;
00068 
00069     // Process additional standard requests
00070 
00071     if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
00072     {
00073         switch (transfer->setup.bRequest)
00074         {
00075             case GET_DESCRIPTOR:
00076                 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
00077                 {
00078                     case REPORT_DESCRIPTOR:
00079                         if ((reportDesc() != NULL) \
00080                             && (reportDescLength() != 0))
00081                         {
00082                             transfer->remaining = reportDescLength();
00083                             transfer->ptr = reportDesc();
00084                             transfer->direction = DEVICE_TO_HOST;
00085                             success = true;
00086                         }
00087                         break;
00088                     case HID_DESCRIPTOR:
00089                             // Find the HID descriptor, after the configuration descriptor
00090                             hidDescriptor = findDescriptor(HID_DESCRIPTOR);
00091                             if (hidDescriptor != NULL)
00092                             {
00093                                 transfer->remaining = HID_DESCRIPTOR_LENGTH;
00094                                 transfer->ptr = hidDescriptor;
00095                                 transfer->direction = DEVICE_TO_HOST;
00096                                 success = true;
00097                             }
00098                             break;
00099                      
00100                     default:
00101                         break;
00102                 }
00103                 break;
00104             default:
00105                 break;
00106         }
00107     }
00108 
00109     // Process class-specific requests
00110 
00111     if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
00112     {
00113         switch (transfer->setup.bRequest)
00114         {
00115              case SET_REPORT:
00116                 // First byte will be used for report ID
00117                 outputReport.data[0] = transfer->setup.wValue & 0xff;
00118                 outputReport.length = transfer->setup.wLength + 1;
00119 
00120                 transfer->remaining = sizeof(outputReport.data) - 1;
00121                 transfer->ptr = &outputReport.data[1];
00122                 transfer->direction = HOST_TO_DEVICE;
00123                 transfer->notify = true;
00124                 success = true;
00125             default:
00126                 break;
00127         }
00128     }
00129 
00130     return success;
00131 }
00132 
00133 
00134 // Called in ISR context
00135 // Called by USBDevice on Endpoint0 request completion
00136 // if the 'notify' flag has been set to true
00137 // In this case it is used to indicate that a HID report has
00138 // been received from the host on endpoint 0
00139 void USBHID::USBCallback_requestCompleted() {
00140     HID_callbackSetReport(&outputReport);
00141 }
00142 
00143 #define DEFAULT_CONFIGURATION (1)
00144 
00145 
00146 // Called in ISR context
00147 // Set configuration. Return false if the
00148 // configuration is not supported
00149 bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
00150     if (configuration != DEFAULT_CONFIGURATION) {
00151         return false;
00152     }
00153 
00154     // Configure endpoints > 0
00155     addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
00156     addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
00157 
00158     // We activate the endpoint to be able to recceive data
00159     readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
00160     return true;
00161 }
00162 
00163 uint8_t * USBHID::stringIinterfaceDesc() {
00164     static uint8_t stringIinterfaceDescriptor[] = {
00165         0x08,               //bLength
00166         STRING_DESCRIPTOR,  //bDescriptorType 0x03
00167         'H',0,'I',0,'D',0,  //bString iInterface - HID
00168     };
00169     return stringIinterfaceDescriptor;
00170 }
00171 
00172 uint8_t * USBHID::stringIproductDesc() {
00173     static uint8_t stringIproductDescriptor[] = {
00174         0x16,                                                       //bLength
00175         STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
00176         'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
00177     };
00178     return stringIproductDescriptor;
00179 }
00180 
00181 
00182 
00183 uint8_t * USBHID::reportDesc() {
00184     static uint8_t reportDescriptor[] = {
00185         0x06, LSB(0xFFAB), MSB(0xFFAB),
00186         0x0A, LSB(0x0200), MSB(0x0200),
00187         0xA1, 0x01,         // Collection 0x01
00188         0x75, 0x08,         // report size = 8 bits
00189         0x15, 0x00,         // logical minimum = 0
00190         0x26, 0xFF, 0x00,   // logical maximum = 255
00191         0x95, input_length,           // report count
00192         0x09, 0x01,         // usage
00193         0x81, 0x02,         // Input (array)
00194         0x95, output_length,           // report count
00195         0x09, 0x02,         // usage
00196         0x91, 0x02,         // Output (array)
00197         0xC0                // end collection
00198 
00199     };
00200     reportLength = sizeof(reportDescriptor);
00201     return reportDescriptor;
00202 }
00203 
00204 #define DEFAULT_CONFIGURATION (1)
00205 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
00206                                + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
00207                                + (1 * HID_DESCRIPTOR_LENGTH) \
00208                                + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
00209 
00210 uint8_t * USBHID::configurationDesc() {
00211     static uint8_t configurationDescriptor[] = {
00212         CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
00213         CONFIGURATION_DESCRIPTOR,       // bDescriptorType
00214         LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
00215         MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
00216         0x01,                           // bNumInterfaces
00217         DEFAULT_CONFIGURATION,          // bConfigurationValue
00218         0x00,                           // iConfiguration
00219         C_RESERVED | C_SELF_POWERED,    // bmAttributes
00220         C_POWER(0),                     // bMaxPower
00221 
00222         INTERFACE_DESCRIPTOR_LENGTH,    // bLength
00223         INTERFACE_DESCRIPTOR,           // bDescriptorType
00224         0x00,                           // bInterfaceNumber
00225         0x00,                           // bAlternateSetting
00226         0x02,                           // bNumEndpoints
00227         HID_CLASS,                      // bInterfaceClass
00228         HID_SUBCLASS_NONE,              // bInterfaceSubClass
00229         HID_PROTOCOL_NONE,              // bInterfaceProtocol
00230         0x00,                           // iInterface
00231 
00232         HID_DESCRIPTOR_LENGTH,          // bLength
00233         HID_DESCRIPTOR,                 // bDescriptorType
00234         LSB(HID_VERSION_1_11),          // bcdHID (LSB)
00235         MSB(HID_VERSION_1_11),          // bcdHID (MSB)
00236         0x00,                           // bCountryCode
00237         0x01,                           // bNumDescriptors
00238         REPORT_DESCRIPTOR,              // bDescriptorType
00239         LSB(this->reportDescLength()),  // wDescriptorLength (LSB)
00240         MSB(this->reportDescLength()),  // wDescriptorLength (MSB)
00241 
00242         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00243         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00244         PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
00245         E_INTERRUPT,                    // bmAttributes
00246         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00247         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00248         10,                             // bInterval (milliseconds)
00249 
00250         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00251         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00252         PHY_TO_DESC(EPINT_OUT),          // bEndpointAddress
00253         E_INTERRUPT,                    // bmAttributes
00254         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00255         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00256         10,                             // bInterval (milliseconds)
00257     };
00258     return configurationDescriptor;
00259 }