This example demonstrates the reading of the USB Gamepad in the Nucleo.
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
This circuit diagram was created by fritzing.
USB con. | Nucleo |
---|---|
GND | GND |
+ | PA_12 |
- | PA_11 |
5V | 5V |
Original Library
- F401RE-USBHost by Norimasa Okamoto
- USBHostGamepad by Yuuichi Akagawa
USBHost/USBDeviceConnected.h@0:b5f79b4f741d, 2016-03-15 (annotated)
- Committer:
- beaglescout007
- Date:
- Tue Mar 15 11:39:04 2016 +0000
- Revision:
- 0:b5f79b4f741d
Release
Who changed what in which revision?
User | Revision | Line number | New 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 | #pragma once |
beaglescout007 | 0:b5f79b4f741d | 17 | |
beaglescout007 | 0:b5f79b4f741d | 18 | #include "USBEndpoint.h" |
beaglescout007 | 0:b5f79b4f741d | 19 | #include "USBHostConf.h" |
beaglescout007 | 0:b5f79b4f741d | 20 | #include "myvector.h" |
beaglescout007 | 0:b5f79b4f741d | 21 | #include "mymap.h" |
beaglescout007 | 0:b5f79b4f741d | 22 | |
beaglescout007 | 0:b5f79b4f741d | 23 | class USBEndpoint; |
beaglescout007 | 0:b5f79b4f741d | 24 | |
beaglescout007 | 0:b5f79b4f741d | 25 | struct INTERFACE { |
beaglescout007 | 0:b5f79b4f741d | 26 | INTERFACE(uint8_t _class, uint8_t _subclass, uint8_t _protocol) { |
beaglescout007 | 0:b5f79b4f741d | 27 | intf_class = _class; |
beaglescout007 | 0:b5f79b4f741d | 28 | intf_subclass = _subclass; |
beaglescout007 | 0:b5f79b4f741d | 29 | intf_protocol = _protocol; |
beaglescout007 | 0:b5f79b4f741d | 30 | } |
beaglescout007 | 0:b5f79b4f741d | 31 | uint8_t intf_class; |
beaglescout007 | 0:b5f79b4f741d | 32 | uint8_t intf_subclass; |
beaglescout007 | 0:b5f79b4f741d | 33 | uint8_t intf_protocol; |
beaglescout007 | 0:b5f79b4f741d | 34 | myvector<USBEndpoint*>ep; |
beaglescout007 | 0:b5f79b4f741d | 35 | }; |
beaglescout007 | 0:b5f79b4f741d | 36 | |
beaglescout007 | 0:b5f79b4f741d | 37 | /** |
beaglescout007 | 0:b5f79b4f741d | 38 | * USBDeviceConnected class |
beaglescout007 | 0:b5f79b4f741d | 39 | */ |
beaglescout007 | 0:b5f79b4f741d | 40 | class USBDeviceConnected { |
beaglescout007 | 0:b5f79b4f741d | 41 | public: |
beaglescout007 | 0:b5f79b4f741d | 42 | |
beaglescout007 | 0:b5f79b4f741d | 43 | /** |
beaglescout007 | 0:b5f79b4f741d | 44 | * Constructor |
beaglescout007 | 0:b5f79b4f741d | 45 | */ |
beaglescout007 | 0:b5f79b4f741d | 46 | USBDeviceConnected(); |
beaglescout007 | 0:b5f79b4f741d | 47 | |
beaglescout007 | 0:b5f79b4f741d | 48 | /** |
beaglescout007 | 0:b5f79b4f741d | 49 | * Attach an USBEndpoint to this device |
beaglescout007 | 0:b5f79b4f741d | 50 | * |
beaglescout007 | 0:b5f79b4f741d | 51 | * @param intf_nb interface number |
beaglescout007 | 0:b5f79b4f741d | 52 | * @param ep pointeur on the USBEndpoint which will be attached |
beaglescout007 | 0:b5f79b4f741d | 53 | * @returns true if successful, false otherwise |
beaglescout007 | 0:b5f79b4f741d | 54 | */ |
beaglescout007 | 0:b5f79b4f741d | 55 | bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep); |
beaglescout007 | 0:b5f79b4f741d | 56 | |
beaglescout007 | 0:b5f79b4f741d | 57 | /** |
beaglescout007 | 0:b5f79b4f741d | 58 | * Retrieve an USBEndpoint by its TYPE and DIRECTION |
beaglescout007 | 0:b5f79b4f741d | 59 | * |
beaglescout007 | 0:b5f79b4f741d | 60 | * @param intf_nb the interface on which to lookup the USBEndpoint |
beaglescout007 | 0:b5f79b4f741d | 61 | * @param type type of the USBEndpoint looked for |
beaglescout007 | 0:b5f79b4f741d | 62 | * @param dir direction of the USBEndpoint looked for |
beaglescout007 | 0:b5f79b4f741d | 63 | * @param index the index of the USBEndpoint whitin the interface |
beaglescout007 | 0:b5f79b4f741d | 64 | * @returns pointer on the USBEndpoint if found, NULL otherwise |
beaglescout007 | 0:b5f79b4f741d | 65 | */ |
beaglescout007 | 0:b5f79b4f741d | 66 | USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0); |
beaglescout007 | 0:b5f79b4f741d | 67 | |
beaglescout007 | 0:b5f79b4f741d | 68 | /** |
beaglescout007 | 0:b5f79b4f741d | 69 | * Retrieve an USBEndpoint by its index |
beaglescout007 | 0:b5f79b4f741d | 70 | * |
beaglescout007 | 0:b5f79b4f741d | 71 | * @param intf_nb interface number |
beaglescout007 | 0:b5f79b4f741d | 72 | * @param index index of the USBEndpoint |
beaglescout007 | 0:b5f79b4f741d | 73 | * @returns pointer on the USBEndpoint if found, NULL otherwise |
beaglescout007 | 0:b5f79b4f741d | 74 | */ |
beaglescout007 | 0:b5f79b4f741d | 75 | USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index); |
beaglescout007 | 0:b5f79b4f741d | 76 | |
beaglescout007 | 0:b5f79b4f741d | 77 | /** |
beaglescout007 | 0:b5f79b4f741d | 78 | * Add a new interface to this device |
beaglescout007 | 0:b5f79b4f741d | 79 | * |
beaglescout007 | 0:b5f79b4f741d | 80 | * @param intf_nb interface number |
beaglescout007 | 0:b5f79b4f741d | 81 | * @param intf_class interface class |
beaglescout007 | 0:b5f79b4f741d | 82 | * @param intf_subclass interface subclass |
beaglescout007 | 0:b5f79b4f741d | 83 | * @param intf_protocol interface protocol |
beaglescout007 | 0:b5f79b4f741d | 84 | * @returns true if successful, false otherwise |
beaglescout007 | 0:b5f79b4f741d | 85 | */ |
beaglescout007 | 0:b5f79b4f741d | 86 | bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); |
beaglescout007 | 0:b5f79b4f741d | 87 | |
beaglescout007 | 0:b5f79b4f741d | 88 | /** |
beaglescout007 | 0:b5f79b4f741d | 89 | * Disconnect the device by calling a callback function registered by a driver |
beaglescout007 | 0:b5f79b4f741d | 90 | */ |
beaglescout007 | 0:b5f79b4f741d | 91 | void disconnect(); |
beaglescout007 | 0:b5f79b4f741d | 92 | |
beaglescout007 | 0:b5f79b4f741d | 93 | void init(USBDeviceConnected* parent, uint8_t _port, bool _lowSpeed); |
beaglescout007 | 0:b5f79b4f741d | 94 | void setAddress(uint8_t addr_) { addr = addr_; }; |
beaglescout007 | 0:b5f79b4f741d | 95 | void setVid(uint16_t vid_) { vid = vid_; }; |
beaglescout007 | 0:b5f79b4f741d | 96 | void setPid(uint16_t pid_) { pid = pid_; }; |
beaglescout007 | 0:b5f79b4f741d | 97 | void setClass(uint8_t device_class_) { device_class = device_class_; } |
beaglescout007 | 0:b5f79b4f741d | 98 | void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; }; |
beaglescout007 | 0:b5f79b4f741d | 99 | void setProtocol(uint8_t pr) { proto = pr; }; |
beaglescout007 | 0:b5f79b4f741d | 100 | void setEnumerated() { enumerated = true; }; |
beaglescout007 | 0:b5f79b4f741d | 101 | void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; }; |
beaglescout007 | 0:b5f79b4f741d | 102 | void setSpeed(bool _lowSpeed) { lowSpeed = _lowSpeed; } |
beaglescout007 | 0:b5f79b4f741d | 103 | void setName(const char * name_, uint8_t intf_nb) { return; }; |
beaglescout007 | 0:b5f79b4f741d | 104 | void setEpCtl(USBEndpoint* ep) { ep_ctl = ep; } |
beaglescout007 | 0:b5f79b4f741d | 105 | |
beaglescout007 | 0:b5f79b4f741d | 106 | static int getNewAddress() { |
beaglescout007 | 0:b5f79b4f741d | 107 | static int addr = 1; |
beaglescout007 | 0:b5f79b4f741d | 108 | return addr++; |
beaglescout007 | 0:b5f79b4f741d | 109 | } |
beaglescout007 | 0:b5f79b4f741d | 110 | uint8_t getAddress() { return addr; }; |
beaglescout007 | 0:b5f79b4f741d | 111 | uint16_t getVid() { return vid; }; |
beaglescout007 | 0:b5f79b4f741d | 112 | uint16_t getPid() { return pid; }; |
beaglescout007 | 0:b5f79b4f741d | 113 | uint8_t getClass() { return device_class; }; |
beaglescout007 | 0:b5f79b4f741d | 114 | bool getSpeed() { return lowSpeed; } |
beaglescout007 | 0:b5f79b4f741d | 115 | bool isEnumerated() { return enumerated; }; |
beaglescout007 | 0:b5f79b4f741d | 116 | USBEndpoint* getEpCtl() { return ep_ctl; } |
beaglescout007 | 0:b5f79b4f741d | 117 | |
beaglescout007 | 0:b5f79b4f741d | 118 | private: |
beaglescout007 | 0:b5f79b4f741d | 119 | USBDeviceConnected* hub_parent; |
beaglescout007 | 0:b5f79b4f741d | 120 | mymap<int,INTERFACE*>intf; |
beaglescout007 | 0:b5f79b4f741d | 121 | uint8_t port; |
beaglescout007 | 0:b5f79b4f741d | 122 | uint16_t vid; |
beaglescout007 | 0:b5f79b4f741d | 123 | uint16_t pid; |
beaglescout007 | 0:b5f79b4f741d | 124 | uint8_t addr; |
beaglescout007 | 0:b5f79b4f741d | 125 | uint8_t device_class; |
beaglescout007 | 0:b5f79b4f741d | 126 | uint8_t device_subclass; |
beaglescout007 | 0:b5f79b4f741d | 127 | uint8_t proto; |
beaglescout007 | 0:b5f79b4f741d | 128 | bool lowSpeed; |
beaglescout007 | 0:b5f79b4f741d | 129 | bool enumerated; |
beaglescout007 | 0:b5f79b4f741d | 130 | uint8_t nb_interf; |
beaglescout007 | 0:b5f79b4f741d | 131 | USBEndpoint* ep_ctl; |
beaglescout007 | 0:b5f79b4f741d | 132 | void init(); |
beaglescout007 | 0:b5f79b4f741d | 133 | }; |
beaglescout007 | 0:b5f79b4f741d | 134 |