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 /*
beaglescout007 0:b5f79b4f741d 2 * mbed USBHost Gamepad driver sample
beaglescout007 0:b5f79b4f741d 3 * Copyright (c) 2014 Yuuichi Akagawa
beaglescout007 0:b5f79b4f741d 4 *
beaglescout007 0:b5f79b4f741d 5 * modified from mbed USBHostMouse
beaglescout007 0:b5f79b4f741d 6 *
beaglescout007 0:b5f79b4f741d 7 * mbed USBHost Library
beaglescout007 0:b5f79b4f741d 8 * Copyright (c) 2006-2013 ARM Limited
beaglescout007 0:b5f79b4f741d 9 *
beaglescout007 0:b5f79b4f741d 10 * Licensed under the Apache License, Version 2.0 (the "License");
beaglescout007 0:b5f79b4f741d 11 * you may not use this file except in compliance with the License.
beaglescout007 0:b5f79b4f741d 12 * You may obtain a copy of the License at
beaglescout007 0:b5f79b4f741d 13 *
beaglescout007 0:b5f79b4f741d 14 * http://www.apache.org/licenses/LICENSE-2.0
beaglescout007 0:b5f79b4f741d 15 *
beaglescout007 0:b5f79b4f741d 16 * Unless required by applicable law or agreed to in writing, software
beaglescout007 0:b5f79b4f741d 17 * distributed under the License is distributed on an "AS IS" BASIS,
beaglescout007 0:b5f79b4f741d 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
beaglescout007 0:b5f79b4f741d 19 * See the License for the specific language governing permissions and
beaglescout007 0:b5f79b4f741d 20 * limitations under the License.
beaglescout007 0:b5f79b4f741d 21 */
beaglescout007 0:b5f79b4f741d 22
beaglescout007 0:b5f79b4f741d 23 #include "USBHostGamepad.h"
beaglescout007 0:b5f79b4f741d 24 //#if USBHOST_GAMEPAD
beaglescout007 0:b5f79b4f741d 25
beaglescout007 0:b5f79b4f741d 26 USBHostGamepad::USBHostGamepad() {
beaglescout007 0:b5f79b4f741d 27 host = USBHost::getHostInst();
beaglescout007 0:b5f79b4f741d 28 init();
beaglescout007 0:b5f79b4f741d 29 }
beaglescout007 0:b5f79b4f741d 30
beaglescout007 0:b5f79b4f741d 31 void USBHostGamepad::init() {
beaglescout007 0:b5f79b4f741d 32 dev = NULL;
beaglescout007 0:b5f79b4f741d 33 int_in = NULL;
beaglescout007 0:b5f79b4f741d 34 dev_connected = false;
beaglescout007 0:b5f79b4f741d 35 gamepad_device_found = false;
beaglescout007 0:b5f79b4f741d 36 gamepad_intf = -1;
beaglescout007 0:b5f79b4f741d 37 }
beaglescout007 0:b5f79b4f741d 38
beaglescout007 0:b5f79b4f741d 39 bool USBHostGamepad::connected() {
beaglescout007 0:b5f79b4f741d 40 return dev_connected;
beaglescout007 0:b5f79b4f741d 41 }
beaglescout007 0:b5f79b4f741d 42
beaglescout007 0:b5f79b4f741d 43 bool USBHostGamepad::connect() {
beaglescout007 0:b5f79b4f741d 44 if (dev_connected) {
beaglescout007 0:b5f79b4f741d 45 return true;
beaglescout007 0:b5f79b4f741d 46 }
beaglescout007 0:b5f79b4f741d 47
beaglescout007 0:b5f79b4f741d 48 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
beaglescout007 0:b5f79b4f741d 49 if ((dev = host->getDevice(i)) != NULL) {
beaglescout007 0:b5f79b4f741d 50
beaglescout007 0:b5f79b4f741d 51 if(host->enumerate(dev, this))
beaglescout007 0:b5f79b4f741d 52 break;
beaglescout007 0:b5f79b4f741d 53
beaglescout007 0:b5f79b4f741d 54 if (gamepad_device_found) {
beaglescout007 0:b5f79b4f741d 55
beaglescout007 0:b5f79b4f741d 56 int_in = dev->getEndpoint(gamepad_intf, INTERRUPT_ENDPOINT, IN);
beaglescout007 0:b5f79b4f741d 57 if (!int_in)
beaglescout007 0:b5f79b4f741d 58 break;
beaglescout007 0:b5f79b4f741d 59
beaglescout007 0:b5f79b4f741d 60 USB_INFO("New Gamepad/Joystick device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, gamepad_intf);
beaglescout007 0:b5f79b4f741d 61 #if DEBUG > 3
beaglescout007 0:b5f79b4f741d 62 //Parse HID Report Descriptor
beaglescout007 0:b5f79b4f741d 63 parseHidDescr();
beaglescout007 0:b5f79b4f741d 64 #endif
beaglescout007 0:b5f79b4f741d 65 dev->setName("Gamepad", gamepad_intf);
beaglescout007 0:b5f79b4f741d 66 host->registerDriver(dev, gamepad_intf, this, &USBHostGamepad::init);
beaglescout007 0:b5f79b4f741d 67
beaglescout007 0:b5f79b4f741d 68 int_in->attach(this, &USBHostGamepad::rxHandler);
beaglescout007 0:b5f79b4f741d 69 rxSize = int_in->getSize();
beaglescout007 0:b5f79b4f741d 70 if (rxSize > sizeof(report))
beaglescout007 0:b5f79b4f741d 71 rxSize = sizeof(report);
beaglescout007 0:b5f79b4f741d 72 host->interruptRead(dev, int_in, report, rxSize, false);
beaglescout007 0:b5f79b4f741d 73
beaglescout007 0:b5f79b4f741d 74 dev_connected = true;
beaglescout007 0:b5f79b4f741d 75 return true;
beaglescout007 0:b5f79b4f741d 76 }
beaglescout007 0:b5f79b4f741d 77 }
beaglescout007 0:b5f79b4f741d 78 }
beaglescout007 0:b5f79b4f741d 79 init();
beaglescout007 0:b5f79b4f741d 80 return false;
beaglescout007 0:b5f79b4f741d 81 }
beaglescout007 0:b5f79b4f741d 82
beaglescout007 0:b5f79b4f741d 83 void USBHostGamepad::rxHandler() {
beaglescout007 0:b5f79b4f741d 84 if (dev)
beaglescout007 0:b5f79b4f741d 85 host->interruptRead(dev, int_in, report, rxSize, false);
beaglescout007 0:b5f79b4f741d 86
beaglescout007 0:b5f79b4f741d 87 /* modified 2016 Racoon */
beaglescout007 0:b5f79b4f741d 88
beaglescout007 0:b5f79b4f741d 89 }
beaglescout007 0:b5f79b4f741d 90
beaglescout007 0:b5f79b4f741d 91 /*virtual*/ void USBHostGamepad::setVidPid(uint16_t vid, uint16_t pid)
beaglescout007 0:b5f79b4f741d 92 {
beaglescout007 0:b5f79b4f741d 93 // we don't check VID/PID for gamepad driver
beaglescout007 0:b5f79b4f741d 94 }
beaglescout007 0:b5f79b4f741d 95
beaglescout007 0:b5f79b4f741d 96 /*virtual*/ bool USBHostGamepad::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
beaglescout007 0:b5f79b4f741d 97 {
beaglescout007 0:b5f79b4f741d 98 if ((gamepad_intf == -1) &&
beaglescout007 0:b5f79b4f741d 99 (intf_class == HID_CLASS) &&
beaglescout007 0:b5f79b4f741d 100 (intf_subclass == 0x00) &&
beaglescout007 0:b5f79b4f741d 101 (intf_protocol == 0x00)) {
beaglescout007 0:b5f79b4f741d 102 gamepad_intf = intf_nb;
beaglescout007 0:b5f79b4f741d 103 return true;
beaglescout007 0:b5f79b4f741d 104 }
beaglescout007 0:b5f79b4f741d 105 return false;
beaglescout007 0:b5f79b4f741d 106 }
beaglescout007 0:b5f79b4f741d 107
beaglescout007 0:b5f79b4f741d 108 /*virtual*/ bool USBHostGamepad::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
beaglescout007 0:b5f79b4f741d 109 {
beaglescout007 0:b5f79b4f741d 110 if (intf_nb == gamepad_intf) {
beaglescout007 0:b5f79b4f741d 111 if (type == INTERRUPT_ENDPOINT && dir == IN) {
beaglescout007 0:b5f79b4f741d 112 gamepad_device_found = true;
beaglescout007 0:b5f79b4f741d 113 return true;
beaglescout007 0:b5f79b4f741d 114 }
beaglescout007 0:b5f79b4f741d 115 }
beaglescout007 0:b5f79b4f741d 116 return false;
beaglescout007 0:b5f79b4f741d 117 }
beaglescout007 0:b5f79b4f741d 118
beaglescout007 0:b5f79b4f741d 119