This example demonstrates the reading of the USB Gamepad in the Nucleo.

Dependencies:   mbed

Intro

This example demonstrates the reading of the USB Gamepad in the Nucleo.

Parts

STM32 Nucleo F446RE
USB Connector
LED 2pcs
Register 470 ohm 2pcs
Breadboard
Wires

Wiring diagram

/media/uploads/beaglescout007/nucleo_ex04_usbpad.png This circuit diagram was created by fritzing.

/media/uploads/beaglescout007/usbcon.jpg

USB con.Nucleo
GNDGND
+PA_12
-PA_11
5V5V

https://youtu.be/EYIukjwJSew

Original Library

Committer:
beaglescout007
Date:
Tue Mar 15 11:39:04 2016 +0000
Revision:
0:b5f79b4f741d
Release

Who changed what in which revision?

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