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/USBHostHub.cpp@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 | #include "USBHost.h" |
beaglescout007 | 0:b5f79b4f741d | 2 | |
beaglescout007 | 0:b5f79b4f741d | 3 | #define PORT_CONNECTION 0 |
beaglescout007 | 0:b5f79b4f741d | 4 | #define PORT_ENABLE 1 |
beaglescout007 | 0:b5f79b4f741d | 5 | #define PORT_SUSPEND 2 |
beaglescout007 | 0:b5f79b4f741d | 6 | #define PORT_OVER_CURRENT 3 |
beaglescout007 | 0:b5f79b4f741d | 7 | #define PORT_RESET 4 |
beaglescout007 | 0:b5f79b4f741d | 8 | #define PORT_POWER 8 |
beaglescout007 | 0:b5f79b4f741d | 9 | #define PORT_LOW_SPEED 9 |
beaglescout007 | 0:b5f79b4f741d | 10 | |
beaglescout007 | 0:b5f79b4f741d | 11 | #define C_PORT_CONNECTION 16 |
beaglescout007 | 0:b5f79b4f741d | 12 | #define C_PORT_ENABLE 17 |
beaglescout007 | 0:b5f79b4f741d | 13 | #define C_PORT_SUSPEND 18 |
beaglescout007 | 0:b5f79b4f741d | 14 | #define C_PORT_OVER_CURRENT 19 |
beaglescout007 | 0:b5f79b4f741d | 15 | #define C_PORT_RESET 20 |
beaglescout007 | 0:b5f79b4f741d | 16 | |
beaglescout007 | 0:b5f79b4f741d | 17 | bool USBHost::Hub(USBDeviceConnected* dev) { |
beaglescout007 | 0:b5f79b4f741d | 18 | USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p]", dev->getVid(), dev->getPid(), dev); |
beaglescout007 | 0:b5f79b4f741d | 19 | HubDescriptor hubdesc; |
beaglescout007 | 0:b5f79b4f741d | 20 | // get HUB descriptor |
beaglescout007 | 0:b5f79b4f741d | 21 | int rc = controlRead(dev, |
beaglescout007 | 0:b5f79b4f741d | 22 | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
beaglescout007 | 0:b5f79b4f741d | 23 | GET_DESCRIPTOR, |
beaglescout007 | 0:b5f79b4f741d | 24 | 0x29 << 8, 0, reinterpret_cast<uint8_t*>(&hubdesc), |
beaglescout007 | 0:b5f79b4f741d | 25 | sizeof(HubDescriptor)); |
beaglescout007 | 0:b5f79b4f741d | 26 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
beaglescout007 | 0:b5f79b4f741d | 27 | if (rc != USB_TYPE_OK) { |
beaglescout007 | 0:b5f79b4f741d | 28 | return false; |
beaglescout007 | 0:b5f79b4f741d | 29 | } |
beaglescout007 | 0:b5f79b4f741d | 30 | USB_DBG_HEX((uint8_t*)&hubdesc, sizeof(hubdesc)); |
beaglescout007 | 0:b5f79b4f741d | 31 | |
beaglescout007 | 0:b5f79b4f741d | 32 | uint32_t status; |
beaglescout007 | 0:b5f79b4f741d | 33 | rc = controlRead( dev, |
beaglescout007 | 0:b5f79b4f741d | 34 | 0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4); |
beaglescout007 | 0:b5f79b4f741d | 35 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
beaglescout007 | 0:b5f79b4f741d | 36 | if (rc != USB_TYPE_OK) { |
beaglescout007 | 0:b5f79b4f741d | 37 | return false; |
beaglescout007 | 0:b5f79b4f741d | 38 | } |
beaglescout007 | 0:b5f79b4f741d | 39 | USB_DBG("HUB STATUS: %08X\n", status); |
beaglescout007 | 0:b5f79b4f741d | 40 | |
beaglescout007 | 0:b5f79b4f741d | 41 | for(int i = 1; i <= hubdesc.bNbrPorts; i++) { |
beaglescout007 | 0:b5f79b4f741d | 42 | SetPortPower(dev, i); // power on |
beaglescout007 | 0:b5f79b4f741d | 43 | wait_ms(hubdesc.bPwrOn2PwrGood*2); |
beaglescout007 | 0:b5f79b4f741d | 44 | uint32_t status; |
beaglescout007 | 0:b5f79b4f741d | 45 | GetPortStatus(dev, i, &status); |
beaglescout007 | 0:b5f79b4f741d | 46 | USB_DBG("port: %d status: %08X\n", i, status); |
beaglescout007 | 0:b5f79b4f741d | 47 | if (status & 0x010000) { // Connect Status Change, has changed |
beaglescout007 | 0:b5f79b4f741d | 48 | USB_TEST_ASSERT(status & 0x000001); |
beaglescout007 | 0:b5f79b4f741d | 49 | ClearPortFeature(dev, C_PORT_CONNECTION, i); |
beaglescout007 | 0:b5f79b4f741d | 50 | int lowSpeed = 0; |
beaglescout007 | 0:b5f79b4f741d | 51 | if (status & 0x0200) { |
beaglescout007 | 0:b5f79b4f741d | 52 | lowSpeed = 1; |
beaglescout007 | 0:b5f79b4f741d | 53 | } |
beaglescout007 | 0:b5f79b4f741d | 54 | PortReset(dev, i); |
beaglescout007 | 0:b5f79b4f741d | 55 | if (!addDevice(dev, i, lowSpeed)) { |
beaglescout007 | 0:b5f79b4f741d | 56 | ClearPortPower(dev, i); // power off |
beaglescout007 | 0:b5f79b4f741d | 57 | } |
beaglescout007 | 0:b5f79b4f741d | 58 | } else { |
beaglescout007 | 0:b5f79b4f741d | 59 | ClearPortPower(dev, i); // power off |
beaglescout007 | 0:b5f79b4f741d | 60 | } |
beaglescout007 | 0:b5f79b4f741d | 61 | } |
beaglescout007 | 0:b5f79b4f741d | 62 | return false; |
beaglescout007 | 0:b5f79b4f741d | 63 | } |
beaglescout007 | 0:b5f79b4f741d | 64 | |
beaglescout007 | 0:b5f79b4f741d | 65 | |
beaglescout007 | 0:b5f79b4f741d | 66 | int USBHost::SetPortPower(USBDeviceConnected* dev, int port) |
beaglescout007 | 0:b5f79b4f741d | 67 | { |
beaglescout007 | 0:b5f79b4f741d | 68 | return SetPortFeature(dev, PORT_POWER, port); |
beaglescout007 | 0:b5f79b4f741d | 69 | } |
beaglescout007 | 0:b5f79b4f741d | 70 | |
beaglescout007 | 0:b5f79b4f741d | 71 | int USBHost::ClearPortPower(USBDeviceConnected* dev, int port) |
beaglescout007 | 0:b5f79b4f741d | 72 | { |
beaglescout007 | 0:b5f79b4f741d | 73 | return ClearPortFeature(dev, PORT_POWER, port); |
beaglescout007 | 0:b5f79b4f741d | 74 | } |
beaglescout007 | 0:b5f79b4f741d | 75 | |
beaglescout007 | 0:b5f79b4f741d | 76 | int USBHost::SetPortFeature(USBDeviceConnected* dev, int feature, int index) |
beaglescout007 | 0:b5f79b4f741d | 77 | { |
beaglescout007 | 0:b5f79b4f741d | 78 | return controlWrite(dev, 0x23, SET_FEATURE,feature,index,0,0); |
beaglescout007 | 0:b5f79b4f741d | 79 | } |
beaglescout007 | 0:b5f79b4f741d | 80 | |
beaglescout007 | 0:b5f79b4f741d | 81 | int USBHost::ClearPortFeature(USBDeviceConnected* dev, int feature, int index) |
beaglescout007 | 0:b5f79b4f741d | 82 | { |
beaglescout007 | 0:b5f79b4f741d | 83 | return controlWrite(dev, 0x23, CLEAR_FEATURE,feature,index,0,0); |
beaglescout007 | 0:b5f79b4f741d | 84 | } |
beaglescout007 | 0:b5f79b4f741d | 85 | |
beaglescout007 | 0:b5f79b4f741d | 86 | int USBHost::SetPortReset(USBDeviceConnected* dev, int port) |
beaglescout007 | 0:b5f79b4f741d | 87 | { |
beaglescout007 | 0:b5f79b4f741d | 88 | return SetPortFeature(dev, PORT_RESET, port); |
beaglescout007 | 0:b5f79b4f741d | 89 | } |
beaglescout007 | 0:b5f79b4f741d | 90 | |
beaglescout007 | 0:b5f79b4f741d | 91 | int USBHost::GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status) |
beaglescout007 | 0:b5f79b4f741d | 92 | { |
beaglescout007 | 0:b5f79b4f741d | 93 | return controlRead(dev, 0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4); |
beaglescout007 | 0:b5f79b4f741d | 94 | } |
beaglescout007 | 0:b5f79b4f741d | 95 | |
beaglescout007 | 0:b5f79b4f741d | 96 | int USBHost::PortReset(USBDeviceConnected* dev, int port) |
beaglescout007 | 0:b5f79b4f741d | 97 | { |
beaglescout007 | 0:b5f79b4f741d | 98 | USB_DBG("%p port=%d\n", this, port); |
beaglescout007 | 0:b5f79b4f741d | 99 | USB_TEST_ASSERT(port >= 1); |
beaglescout007 | 0:b5f79b4f741d | 100 | SetPortReset(dev, port); |
beaglescout007 | 0:b5f79b4f741d | 101 | // wait reset |
beaglescout007 | 0:b5f79b4f741d | 102 | for(int i = 0; i < 100; i++) { |
beaglescout007 | 0:b5f79b4f741d | 103 | uint32_t status; |
beaglescout007 | 0:b5f79b4f741d | 104 | GetPortStatus(dev, port, &status); |
beaglescout007 | 0:b5f79b4f741d | 105 | USB_DBG("RESET port: %d status: %08X\n", port, status); |
beaglescout007 | 0:b5f79b4f741d | 106 | if (status & 0x100000) { // Reset change , Reset complete |
beaglescout007 | 0:b5f79b4f741d | 107 | return USB_TYPE_OK; |
beaglescout007 | 0:b5f79b4f741d | 108 | } |
beaglescout007 | 0:b5f79b4f741d | 109 | wait_ms(5); |
beaglescout007 | 0:b5f79b4f741d | 110 | } |
beaglescout007 | 0:b5f79b4f741d | 111 | return USB_TYPE_ERROR; |
beaglescout007 | 0:b5f79b4f741d | 112 | } |
beaglescout007 | 0:b5f79b4f741d | 113 |