Simple USBHost library for LPC4088. Backward compatibility of official-USBHost.

Dependencies:   FATFileSystem mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHost.h Source File

USBHost.h

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #pragma once
00018 #include "mbed.h"
00019 #include "USBHALHost.h"
00020 #include "USBDeviceConnected.h"
00021 #include "IUSBEnumerator.h"
00022 #include "USBHostConf.h"
00023 #include "dbg.h"
00024 #include "myvector.h"
00025 
00026 // USB STANDARD REQUEST DEFINITIONS
00027 #define  USB_DESCRIPTOR_TYPE_STRING         3
00028 #define  USB_DESCRIPTOR_TYPE_HUB         0x29
00029 
00030 #pragma pack(push,1)
00031 struct StringDescriptor {// offset
00032     uint8_t bLength;          // +0
00033     uint8_t bDescriptorType;  // +1
00034     char bString[0];          // +2
00035 }; 
00036 #pragma pack(pop)
00037 
00038 /**
00039 * USBHost class
00040 *   This class is a singleton. All drivers have a reference on the static USBHost instance
00041 */
00042 class USBHost : public USBHALHost {
00043 public:
00044     /**
00045     * Static method to create or retrieve the single USBHost instance
00046     */    
00047     static USBHost* getHostInst();
00048 
00049     /**
00050     * Control read: setup stage, data stage and status stage
00051     *
00052     * @param dev the control read will be done for this device
00053     * @param requestType request type
00054     * @param request request
00055     * @param value value
00056     * @param index index
00057     * @param buf pointer on a buffer where will be store the data received
00058     * @param len length of the transfer
00059     *
00060     * @returns status of the control read
00061     */
00062     USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
00063 
00064     /**
00065     * Control write: setup stage, data stage and status stage
00066     *
00067     * @param dev the control write will be done for this device
00068     * @param requestType request type
00069     * @param request request
00070     * @param value value
00071     * @param index index
00072     * @param buf pointer on a buffer which will be written
00073     * @param len length of the transfer
00074     *
00075     * @returns status of the control write
00076     */
00077     USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
00078 
00079     /**
00080     * Bulk read
00081     *
00082     * @param dev the bulk transfer will be done for this device
00083     * @param ep USBEndpoint which will be used to read a packet
00084     * @param buf pointer on a buffer where will be store the data received
00085     * @param len length of the transfer
00086     * @param blocking if true, the read is blocking (wait for completion)
00087     *
00088     * @returns status of the bulk read
00089     */
00090     USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00091 
00092     /**
00093     * Bulk write
00094     *
00095     * @param dev the bulk transfer will be done for this device
00096     * @param ep USBEndpoint which will be used to write a packet
00097     * @param buf pointer on a buffer which will be written
00098     * @param len length of the transfer
00099     * @param blocking if true, the write is blocking (wait for completion)
00100     *
00101     * @returns status of the bulk write
00102     */
00103     USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00104 
00105     /**
00106     * Interrupt read
00107     *
00108     * @param dev the interrupt transfer will be done for this device
00109     * @param ep USBEndpoint which will be used to write a packet
00110     * @param buf pointer on a buffer which will be written
00111     * @param len length of the transfer
00112     * @param blocking if true, the read is blocking (wait for completion)
00113     *
00114     * @returns status of the interrupt read
00115     */
00116     USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00117 
00118     /**
00119     * Interrupt write
00120     *
00121     * @param dev the interrupt transfer will be done for this device
00122     * @param ep USBEndpoint which will be used to write a packet
00123     * @param buf pointer on a buffer which will be written
00124     * @param len length of the transfer
00125     * @param blocking if true, the write is blocking (wait for completion)
00126     *
00127     * @returns status of the interrupt write
00128     */
00129     USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00130 
00131     /**
00132     * Enumerate a device.
00133     *
00134     * @param dev device which will be enumerated
00135     *
00136     * @returns status of the enumeration
00137     */
00138     USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator);
00139 
00140     /**
00141     * Get a device
00142     *
00143     * @param index index of the device which will be returned
00144     *
00145     * @returns pointer on the "index" device
00146     */
00147     USBDeviceConnected * getDevice(uint8_t index) {
00148         return index < DeviceLists.size() ? DeviceLists[index] : NULL;
00149     }
00150 
00151     /**
00152      *  register a driver into the host associated with a callback function called when the device is disconnected
00153      *
00154      *  @param dev device
00155      *  @param intf interface number
00156      *  @param tptr pointer to the object to call the member function on
00157      *  @param mptr pointer to the member function to be called
00158      */
00159     template<typename T>
00160     void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
00161     }
00162 
00163     // LPC4088-USBHost extensions
00164     USB_TYPE controlRead(USBEndpoint* ep, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t* buf, uint32_t len);
00165     USB_TYPE controlWrite(USBEndpoint* ep, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t* buf, uint32_t len);
00166     int interruptReadNB(USBEndpoint* ep, uint8_t* data, int size);
00167     int bulkReadNB(USBEndpoint*ep, uint8_t* data, int size);
00168     /**
00169      *
00170      */
00171     static void poll();
00172 
00173 private:
00174     USBHost();
00175     static USBHost* inst;
00176     virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed);
00177     void root_enumeration(USBDeviceConnected* dev);
00178     void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator);
00179     myvector<USBDeviceConnected*>DeviceLists;
00180 
00181     void task();
00182     EndpointQueue ep_queue;
00183 
00184     // USB HUB
00185     bool Hub(USBDeviceConnected* dev);
00186     int SetPortPower(USBDeviceConnected* dev, int port);
00187     int ClearPortPower(USBDeviceConnected* dev, int port);
00188     int PortReset(USBDeviceConnected* dev, int port);
00189     int SetPortFeature(USBDeviceConnected* dev, int feature, int index);
00190     int ClearPortFeature(USBDeviceConnected* dev, int feature, int index);
00191     int SetPortReset(USBDeviceConnected* dev, int port);
00192     int GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status);
00193 };
00194