Simple USBHost library for STM32F746NG Discovery board. Only either the Fastspeed or the Highspeed port can be used( not both together)

Dependents:   DISCO-F746NG_USB_Host

Fork of KL46Z-USBHost by Norimasa Okamoto

Committer:
DieterGraef
Date:
Mon Jun 13 17:21:07 2016 +0000
Revision:
24:5396b6a93262
Parent:
18:61554f238584
Child:
25:7d6d9fc471bf
USB Host for STM32F746 DISCO Board. At the moment you can only use either the High Speed Port or the Fast Speed Port.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 12:b91fdea8c0a7 1 /* mbed USBHost Library
va009039 12:b91fdea8c0a7 2 * Copyright (c) 2006-2013 ARM Limited
va009039 12:b91fdea8c0a7 3 *
va009039 12:b91fdea8c0a7 4 * Licensed under the Apache License, Version 2.0 (the "License");
va009039 12:b91fdea8c0a7 5 * you may not use this file except in compliance with the License.
va009039 12:b91fdea8c0a7 6 * You may obtain a copy of the License at
va009039 12:b91fdea8c0a7 7 *
va009039 12:b91fdea8c0a7 8 * http://www.apache.org/licenses/LICENSE-2.0
va009039 12:b91fdea8c0a7 9 *
va009039 12:b91fdea8c0a7 10 * Unless required by applicable law or agreed to in writing, software
va009039 12:b91fdea8c0a7 11 * distributed under the License is distributed on an "AS IS" BASIS,
va009039 12:b91fdea8c0a7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
va009039 12:b91fdea8c0a7 13 * See the License for the specific language governing permissions and
va009039 12:b91fdea8c0a7 14 * limitations under the License.
va009039 12:b91fdea8c0a7 15 */
va009039 12:b91fdea8c0a7 16
va009039 12:b91fdea8c0a7 17 #pragma once
va009039 12:b91fdea8c0a7 18 #include "mbed.h"
va009039 12:b91fdea8c0a7 19 #include "USBHALHost.h"
va009039 12:b91fdea8c0a7 20 #include "USBDeviceConnected.h"
va009039 12:b91fdea8c0a7 21 #include "IUSBEnumerator.h"
va009039 12:b91fdea8c0a7 22 #include "USBHostConf.h"
va009039 12:b91fdea8c0a7 23 #include "dbg.h"
va009039 12:b91fdea8c0a7 24 #include "myvector.h"
va009039 12:b91fdea8c0a7 25
va009039 12:b91fdea8c0a7 26 /**
va009039 12:b91fdea8c0a7 27 * USBHost class
va009039 12:b91fdea8c0a7 28 * This class is a singleton. All drivers have a reference on the static USBHost instance
va009039 12:b91fdea8c0a7 29 */
va009039 12:b91fdea8c0a7 30 class USBHost : public USBHALHost {
va009039 12:b91fdea8c0a7 31 public:
va009039 12:b91fdea8c0a7 32 /**
va009039 12:b91fdea8c0a7 33 * Static method to create or retrieve the single USBHost instance
va009039 12:b91fdea8c0a7 34 */
DieterGraef 24:5396b6a93262 35 static USBHost* getHostInst(int IF_Number);
va009039 12:b91fdea8c0a7 36
va009039 12:b91fdea8c0a7 37 /**
va009039 12:b91fdea8c0a7 38 * Control read: setup stage, data stage and status stage
va009039 12:b91fdea8c0a7 39 *
va009039 12:b91fdea8c0a7 40 * @param dev the control read will be done for this device
va009039 12:b91fdea8c0a7 41 * @param requestType request type
va009039 12:b91fdea8c0a7 42 * @param request request
va009039 12:b91fdea8c0a7 43 * @param value value
va009039 12:b91fdea8c0a7 44 * @param index index
va009039 12:b91fdea8c0a7 45 * @param buf pointer on a buffer where will be store the data received
va009039 12:b91fdea8c0a7 46 * @param len length of the transfer
va009039 12:b91fdea8c0a7 47 *
va009039 12:b91fdea8c0a7 48 * @returns status of the control read
va009039 12:b91fdea8c0a7 49 */
va009039 12:b91fdea8c0a7 50 USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
va009039 12:b91fdea8c0a7 51
va009039 12:b91fdea8c0a7 52 /**
va009039 12:b91fdea8c0a7 53 * Control write: setup stage, data stage and status stage
va009039 12:b91fdea8c0a7 54 *
va009039 12:b91fdea8c0a7 55 * @param dev the control write will be done for this device
va009039 12:b91fdea8c0a7 56 * @param requestType request type
va009039 12:b91fdea8c0a7 57 * @param request request
va009039 12:b91fdea8c0a7 58 * @param value value
va009039 12:b91fdea8c0a7 59 * @param index index
va009039 12:b91fdea8c0a7 60 * @param buf pointer on a buffer which will be written
va009039 12:b91fdea8c0a7 61 * @param len length of the transfer
va009039 12:b91fdea8c0a7 62 *
va009039 12:b91fdea8c0a7 63 * @returns status of the control write
va009039 12:b91fdea8c0a7 64 */
va009039 12:b91fdea8c0a7 65 USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
va009039 12:b91fdea8c0a7 66
va009039 12:b91fdea8c0a7 67 /**
va009039 12:b91fdea8c0a7 68 * Bulk read
va009039 12:b91fdea8c0a7 69 *
va009039 12:b91fdea8c0a7 70 * @param dev the bulk transfer will be done for this device
va009039 12:b91fdea8c0a7 71 * @param ep USBEndpoint which will be used to read a packet
va009039 12:b91fdea8c0a7 72 * @param buf pointer on a buffer where will be store the data received
va009039 12:b91fdea8c0a7 73 * @param len length of the transfer
va009039 12:b91fdea8c0a7 74 * @param blocking if true, the read is blocking (wait for completion)
va009039 12:b91fdea8c0a7 75 *
va009039 12:b91fdea8c0a7 76 * @returns status of the bulk read
va009039 12:b91fdea8c0a7 77 */
va009039 12:b91fdea8c0a7 78 USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 12:b91fdea8c0a7 79
va009039 12:b91fdea8c0a7 80 /**
va009039 12:b91fdea8c0a7 81 * Bulk write
va009039 12:b91fdea8c0a7 82 *
va009039 12:b91fdea8c0a7 83 * @param dev the bulk transfer will be done for this device
va009039 12:b91fdea8c0a7 84 * @param ep USBEndpoint which will be used to write a packet
va009039 12:b91fdea8c0a7 85 * @param buf pointer on a buffer which will be written
va009039 12:b91fdea8c0a7 86 * @param len length of the transfer
va009039 12:b91fdea8c0a7 87 * @param blocking if true, the write is blocking (wait for completion)
va009039 12:b91fdea8c0a7 88 *
va009039 12:b91fdea8c0a7 89 * @returns status of the bulk write
va009039 12:b91fdea8c0a7 90 */
va009039 12:b91fdea8c0a7 91 USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 12:b91fdea8c0a7 92
va009039 12:b91fdea8c0a7 93 /**
va009039 12:b91fdea8c0a7 94 * Interrupt read
va009039 12:b91fdea8c0a7 95 *
va009039 12:b91fdea8c0a7 96 * @param dev the interrupt transfer will be done for this device
va009039 12:b91fdea8c0a7 97 * @param ep USBEndpoint which will be used to write a packet
va009039 12:b91fdea8c0a7 98 * @param buf pointer on a buffer which will be written
va009039 12:b91fdea8c0a7 99 * @param len length of the transfer
va009039 12:b91fdea8c0a7 100 * @param blocking if true, the read is blocking (wait for completion)
va009039 12:b91fdea8c0a7 101 *
va009039 12:b91fdea8c0a7 102 * @returns status of the interrupt read
va009039 12:b91fdea8c0a7 103 */
va009039 12:b91fdea8c0a7 104 USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 12:b91fdea8c0a7 105
va009039 12:b91fdea8c0a7 106 /**
va009039 12:b91fdea8c0a7 107 * Interrupt write
va009039 12:b91fdea8c0a7 108 *
va009039 12:b91fdea8c0a7 109 * @param dev the interrupt transfer will be done for this device
va009039 12:b91fdea8c0a7 110 * @param ep USBEndpoint which will be used to write a packet
va009039 12:b91fdea8c0a7 111 * @param buf pointer on a buffer which will be written
va009039 12:b91fdea8c0a7 112 * @param len length of the transfer
va009039 12:b91fdea8c0a7 113 * @param blocking if true, the write is blocking (wait for completion)
va009039 12:b91fdea8c0a7 114 *
va009039 12:b91fdea8c0a7 115 * @returns status of the interrupt write
va009039 12:b91fdea8c0a7 116 */
va009039 12:b91fdea8c0a7 117 USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 12:b91fdea8c0a7 118
va009039 12:b91fdea8c0a7 119 /**
va009039 12:b91fdea8c0a7 120 * Isochronous read
va009039 12:b91fdea8c0a7 121 *
va009039 12:b91fdea8c0a7 122 * @param dev the isochronous transfer will be done for this device
va009039 12:b91fdea8c0a7 123 * @param ep USBEndpoint which will be used to write a packet
va009039 12:b91fdea8c0a7 124 * @param buf pointer on a buffer which will be written
va009039 12:b91fdea8c0a7 125 * @param len length of the transfer
va009039 12:b91fdea8c0a7 126 * @param blocking if true, the read is blocking (wait for completion)
va009039 12:b91fdea8c0a7 127 *
va009039 12:b91fdea8c0a7 128 * @returns status of the interrupt read
va009039 12:b91fdea8c0a7 129 */
va009039 12:b91fdea8c0a7 130 USB_TYPE isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking = true);
va009039 12:b91fdea8c0a7 131
va009039 12:b91fdea8c0a7 132 /**
va009039 12:b91fdea8c0a7 133 * Enumerate a device.
va009039 12:b91fdea8c0a7 134 *
va009039 12:b91fdea8c0a7 135 * @param dev device which will be enumerated
va009039 12:b91fdea8c0a7 136 *
va009039 12:b91fdea8c0a7 137 * @returns status of the enumeration
va009039 12:b91fdea8c0a7 138 */
va009039 12:b91fdea8c0a7 139 USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator);
va009039 12:b91fdea8c0a7 140
va009039 12:b91fdea8c0a7 141 /**
va009039 12:b91fdea8c0a7 142 * Get a device
va009039 12:b91fdea8c0a7 143 *
va009039 12:b91fdea8c0a7 144 * @param index index of the device which will be returned
va009039 12:b91fdea8c0a7 145 *
va009039 12:b91fdea8c0a7 146 * @returns pointer on the "index" device
va009039 12:b91fdea8c0a7 147 */
va009039 12:b91fdea8c0a7 148 USBDeviceConnected * getDevice(uint8_t index) {
va009039 12:b91fdea8c0a7 149 return index < DeviceLists.size() ? DeviceLists[index] : NULL;
va009039 12:b91fdea8c0a7 150 }
va009039 12:b91fdea8c0a7 151
va009039 12:b91fdea8c0a7 152 /**
va009039 12:b91fdea8c0a7 153 * register a driver into the host associated with a callback function called when the device is disconnected
va009039 12:b91fdea8c0a7 154 *
va009039 12:b91fdea8c0a7 155 * @param dev device
va009039 12:b91fdea8c0a7 156 * @param intf interface number
va009039 12:b91fdea8c0a7 157 * @param tptr pointer to the object to call the member function on
va009039 12:b91fdea8c0a7 158 * @param mptr pointer to the member function to be called
va009039 12:b91fdea8c0a7 159 */
va009039 12:b91fdea8c0a7 160 template<typename T>
va009039 12:b91fdea8c0a7 161 void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
va009039 12:b91fdea8c0a7 162 }
va009039 12:b91fdea8c0a7 163
va009039 12:b91fdea8c0a7 164 // KL46Z-USBHost extensions
va009039 12:b91fdea8c0a7 165 int interruptReadNB(USBEndpoint* ep, uint8_t* data, int size);
va009039 12:b91fdea8c0a7 166 int bulkReadNB(USBEndpoint*ep, uint8_t* data, int size);
va009039 12:b91fdea8c0a7 167 int isochronousReadNB(USBEndpoint*ep, uint8_t* data, int size);
va009039 18:61554f238584 168
va009039 18:61554f238584 169 /**
va009039 18:61554f238584 170 * non-blocking processing
va009039 18:61554f238584 171 */
va009039 12:b91fdea8c0a7 172 static void poll();
va009039 12:b91fdea8c0a7 173
va009039 12:b91fdea8c0a7 174 private:
DieterGraef 24:5396b6a93262 175 USBHost(int InterfaceNumber);
va009039 12:b91fdea8c0a7 176 static USBHost* inst;
va009039 12:b91fdea8c0a7 177 virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed);
va009039 12:b91fdea8c0a7 178 void root_enumeration(USBDeviceConnected* dev);
va009039 12:b91fdea8c0a7 179 void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator);
va009039 12:b91fdea8c0a7 180 myvector<USBDeviceConnected*>DeviceLists;
va009039 12:b91fdea8c0a7 181 void task();
va009039 12:b91fdea8c0a7 182 EndpointQueue ep_queue;
va009039 12:b91fdea8c0a7 183
va009039 12:b91fdea8c0a7 184 // USB HUB
va009039 12:b91fdea8c0a7 185 bool Hub(USBDeviceConnected* dev);
va009039 12:b91fdea8c0a7 186 int SetPortPower(USBDeviceConnected* dev, int port);
va009039 12:b91fdea8c0a7 187 int ClearPortPower(USBDeviceConnected* dev, int port);
va009039 12:b91fdea8c0a7 188 int PortReset(USBDeviceConnected* dev, int port);
va009039 12:b91fdea8c0a7 189 int SetPortFeature(USBDeviceConnected* dev, int feature, int index);
va009039 12:b91fdea8c0a7 190 int ClearPortFeature(USBDeviceConnected* dev, int feature, int index);
va009039 12:b91fdea8c0a7 191 int SetPortReset(USBDeviceConnected* dev, int port);
va009039 12:b91fdea8c0a7 192 int GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status);
va009039 12:b91fdea8c0a7 193 };
va009039 12:b91fdea8c0a7 194