Simple USBHost library for Nucleo F446RE/F411RE/F401RE FRDM-KL46Z/KL25Z/F64F LPC4088/LPC1768

Dependencies:   FATFileSystem

Dependents:   F401RE-BTstack_example F401RE-USBHostMSD_HelloWorld

Fork of KL46Z-USBHost by Norimasa Okamoto

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 /**
00027 * USBHost class
00028 *   This class is a singleton. All drivers have a reference on the static USBHost instance
00029 */
00030 class USBHost : public USBHALHost {
00031 public:
00032     /**
00033     * Static method to create or retrieve the single USBHost instance
00034     */
00035     static USBHost* getHostInst();
00036 
00037     /**
00038     * Control read: setup stage, data stage and status stage
00039     *
00040     * @param dev the control read will be done for this device
00041     * @param requestType request type
00042     * @param request request
00043     * @param value value
00044     * @param index index
00045     * @param buf pointer on a buffer where will be store the data received
00046     * @param len length of the transfer
00047     *
00048     * @returns status of the control read
00049     */
00050     USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
00051 
00052     /**
00053     * Control write: setup stage, data stage and status stage
00054     *
00055     * @param dev the control write will be done for this device
00056     * @param requestType request type
00057     * @param request request
00058     * @param value value
00059     * @param index index
00060     * @param buf pointer on a buffer which will be written
00061     * @param len length of the transfer
00062     *
00063     * @returns status of the control write
00064     */
00065     USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
00066 
00067     /**
00068     * Bulk read
00069     *
00070     * @param dev the bulk transfer will be done for this device
00071     * @param ep USBEndpoint which will be used to read a packet
00072     * @param buf pointer on a buffer where will be store the data received
00073     * @param len length of the transfer
00074     * @param blocking if true, the read is blocking (wait for completion)
00075     *
00076     * @returns status of the bulk read
00077     */
00078     USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00079 
00080     /**
00081     * Bulk write
00082     *
00083     * @param dev the bulk transfer will be done for this device
00084     * @param ep USBEndpoint which will be used to write a packet
00085     * @param buf pointer on a buffer which will be written
00086     * @param len length of the transfer
00087     * @param blocking if true, the write is blocking (wait for completion)
00088     *
00089     * @returns status of the bulk write
00090     */
00091     USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00092 
00093     /**
00094     * Interrupt read
00095     *
00096     * @param dev the interrupt transfer will be done for this device
00097     * @param ep USBEndpoint which will be used to write a packet
00098     * @param buf pointer on a buffer which will be written
00099     * @param len length of the transfer
00100     * @param blocking if true, the read is blocking (wait for completion)
00101     *
00102     * @returns status of the interrupt read
00103     */
00104     USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00105 
00106     /**
00107     * Interrupt write
00108     *
00109     * @param dev the interrupt transfer will be done for this device
00110     * @param ep USBEndpoint which will be used to write a packet
00111     * @param buf pointer on a buffer which will be written
00112     * @param len length of the transfer
00113     * @param blocking if true, the write is blocking (wait for completion)
00114     *
00115     * @returns status of the interrupt write
00116     */
00117     USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
00118 
00119     /**
00120     * Isochronous read
00121     *
00122     * @param dev the isochronous transfer will be done for this device
00123     * @param ep USBEndpoint which will be used to write a packet
00124     * @param buf pointer on a buffer which will be written
00125     * @param len length of the transfer
00126     * @param blocking if true, the read is blocking (wait for completion)
00127     *
00128     * @returns status of the interrupt read
00129     */
00130     USB_TYPE isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking = true);
00131 
00132     /**
00133     * Enumerate a device.
00134     *
00135     * @param dev device which will be enumerated
00136     *
00137     * @returns status of the enumeration
00138     */
00139     USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator);
00140 
00141     /**
00142     * Get a device
00143     *
00144     * @param index index of the device which will be returned
00145     *
00146     * @returns pointer on the "index" device
00147     */
00148     USBDeviceConnected * getDevice(uint8_t index) {
00149         return index < DeviceLists.size() ? DeviceLists[index] : NULL;
00150     }
00151 
00152     /**
00153      *  register a driver into the host associated with a callback function called when the device is disconnected
00154      *
00155      *  @param dev device
00156      *  @param intf interface number
00157      *  @param tptr pointer to the object to call the member function on
00158      *  @param mptr pointer to the member function to be called
00159      */
00160     template<typename T>
00161     void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
00162     }
00163 
00164     // KL46Z-USBHost extensions
00165     int interruptReadNB(USBEndpoint* ep, uint8_t* data, int size);
00166     int bulkReadNB(USBEndpoint*ep, uint8_t* data, int size);
00167     int isochronousReadNB(USBEndpoint*ep, uint8_t* data, int size);
00168 
00169     /**
00170      * non-blocking processing
00171      */
00172     static void poll();
00173 
00174 private:
00175     USBHost();
00176     static USBHost* inst;
00177     virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed);
00178     void root_enumeration(USBDeviceConnected* dev);
00179     void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator);
00180     myvector<USBDeviceConnected*>DeviceLists;
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