Library to use Arduino USB host shield on mbed

Dependents:   USBHOST_PS5

ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。

Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です https://os.mbed.com/media/uploads/kotakku/dffgfddswa.png

シールドについて

3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください

接続例

https://os.mbed.com/media/uploads/kotakku/esgsvfvhjrekldkcjxvb.png

使い方

Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。

仕様

  • Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。

main.cpp

#include "mbed.h"
#include <PS3BT.h>
#include <usbhub.h>

Serial pc(USBTX, USBRX, 115200);

//Nucleo f303k8用
USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int main()
{
    bool printAngle = false;

    if (Usb.Init() == -1)
    {
        pc.printf("\r\nOSC did not start");
        while (1); // Halt
    }
    pc.printf("\r\nPS3 USB Library Started");

    while (1)
    {
        Usb.Task();
        
        if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
            if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117)
            {
                pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX));
                pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY));
                if (PS3.PS3Connected)
                { // The Navigation controller only have one joystick
                    pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX));
                    pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY));
                }
            }
            // Analog button values can be read from almost all buttons
            if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2))
            {
                pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2));
                if (!PS3.PS3NavigationConnected)
                {
                    pc.printf("\tR2: %d", PS3.getAnalogButton(R2));
                }
            }
            if (PS3.getButtonClick(PS))
            {
                PS3.disconnect();
                pc.printf("\r\nPS");
            }
    
            if (PS3.getButtonClick(TRIANGLE))
                pc.printf("\r\nTriangle");
            if (PS3.getButtonClick(CIRCLE))
                pc.printf("\r\nCircle");
            if (PS3.getButtonClick(CROSS))
                pc.printf("\r\nCross");
            if (PS3.getButtonClick(SQUARE))
                pc.printf("\r\nSquare");
    
            if (PS3.getButtonClick(UP))
            {
                pc.printf("\r\nUp");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED4);
            }
            if (PS3.getButtonClick(RIGHT))
            {
                pc.printf("\r\nRight");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED1);
            }
            if (PS3.getButtonClick(DOWN))
            {
                pc.printf("\r\nDown");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED2);
            }
            if (PS3.getButtonClick(LEFT))
            {
                pc.printf("\r\nLeft");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED3);
            }
    
            if (PS3.getButtonClick(L1))
                pc.printf("\r\nL1");
            if (PS3.getButtonClick(L3))
                pc.printf("\r\nL3");
            if (PS3.getButtonClick(R1))
                pc.printf("\r\nR1");
            if (PS3.getButtonClick(R3))
                pc.printf("\r\nR3");
    
            if (PS3.getButtonClick(SELECT))
            {
                pc.printf("\r\nSelect - ");
                PS3.printStatusString();
            }
            if (PS3.getButtonClick(START))
            {
                pc.printf("\r\nStart");
                printAngle = !printAngle;
            }
            if (printAngle)
            {
                pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch));
                pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll));
            }
        }
        else
        {
            pc.printf("not connect\n");
        }
    }
}
Committer:
kotakku
Date:
Sat Jan 18 15:06:35 2020 +0000
Revision:
0:b1ce54272580
1.0.0 first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kotakku 0:b1ce54272580 1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
kotakku 0:b1ce54272580 2
kotakku 0:b1ce54272580 3 This program is free software; you can redistribute it and/or modify
kotakku 0:b1ce54272580 4 it under the terms of the GNU General Public License as published by
kotakku 0:b1ce54272580 5 the Free Software Foundation; either version 2 of the License, or
kotakku 0:b1ce54272580 6 (at your option) any later version.
kotakku 0:b1ce54272580 7
kotakku 0:b1ce54272580 8 This program is distributed in the hope that it will be useful,
kotakku 0:b1ce54272580 9 but WITHOUT ANY WARRANTY; without even the implied warranty of
kotakku 0:b1ce54272580 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kotakku 0:b1ce54272580 11 GNU General Public License for more details.
kotakku 0:b1ce54272580 12
kotakku 0:b1ce54272580 13 You should have received a copy of the GNU General Public License
kotakku 0:b1ce54272580 14 along with this program; if not, write to the Free Software
kotakku 0:b1ce54272580 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
kotakku 0:b1ce54272580 16
kotakku 0:b1ce54272580 17 Contact information
kotakku 0:b1ce54272580 18 -------------------
kotakku 0:b1ce54272580 19
kotakku 0:b1ce54272580 20 Circuits At Home, LTD
kotakku 0:b1ce54272580 21 Web : http://www.circuitsathome.com
kotakku 0:b1ce54272580 22 e-mail : support@circuitsathome.com
kotakku 0:b1ce54272580 23 */
kotakku 0:b1ce54272580 24
kotakku 0:b1ce54272580 25 #if !defined(_usb_h_) || defined(_ch9_h_)
kotakku 0:b1ce54272580 26 #error "Never include usb_ch9.h directly; include Usb.h instead"
kotakku 0:b1ce54272580 27 #else
kotakku 0:b1ce54272580 28
kotakku 0:b1ce54272580 29 /* USB chapter 9 structures */
kotakku 0:b1ce54272580 30 #define _ch9_h_
kotakku 0:b1ce54272580 31
kotakku 0:b1ce54272580 32 /* Misc.USB constants */
kotakku 0:b1ce54272580 33 #define DEV_DESCR_LEN 18 //device descriptor length
kotakku 0:b1ce54272580 34 #define CONF_DESCR_LEN 9 //configuration descriptor length
kotakku 0:b1ce54272580 35 #define INTR_DESCR_LEN 9 //interface descriptor length
kotakku 0:b1ce54272580 36 #define EP_DESCR_LEN 7 //endpoint descriptor length
kotakku 0:b1ce54272580 37
kotakku 0:b1ce54272580 38 /* Standard Device Requests */
kotakku 0:b1ce54272580 39
kotakku 0:b1ce54272580 40 #define USB_REQUEST_GET_STATUS 0 // Standard Device Request - GET STATUS
kotakku 0:b1ce54272580 41 #define USB_REQUEST_CLEAR_FEATURE 1 // Standard Device Request - CLEAR FEATURE
kotakku 0:b1ce54272580 42 #define USB_REQUEST_SET_FEATURE 3 // Standard Device Request - SET FEATURE
kotakku 0:b1ce54272580 43 #define USB_REQUEST_SET_ADDRESS 5 // Standard Device Request - SET ADDRESS
kotakku 0:b1ce54272580 44 #define USB_REQUEST_GET_DESCRIPTOR 6 // Standard Device Request - GET DESCRIPTOR
kotakku 0:b1ce54272580 45 #define USB_REQUEST_SET_DESCRIPTOR 7 // Standard Device Request - SET DESCRIPTOR
kotakku 0:b1ce54272580 46 #define USB_REQUEST_GET_CONFIGURATION 8 // Standard Device Request - GET CONFIGURATION
kotakku 0:b1ce54272580 47 #define USB_REQUEST_SET_CONFIGURATION 9 // Standard Device Request - SET CONFIGURATION
kotakku 0:b1ce54272580 48 #define USB_REQUEST_GET_INTERFACE 10 // Standard Device Request - GET INTERFACE
kotakku 0:b1ce54272580 49 #define USB_REQUEST_SET_INTERFACE 11 // Standard Device Request - SET INTERFACE
kotakku 0:b1ce54272580 50 #define USB_REQUEST_SYNCH_FRAME 12 // Standard Device Request - SYNCH FRAME
kotakku 0:b1ce54272580 51
kotakku 0:b1ce54272580 52 #define USB_FEATURE_ENDPOINT_HALT 0 // CLEAR/SET FEATURE - Endpoint Halt
kotakku 0:b1ce54272580 53 #define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1 // CLEAR/SET FEATURE - Device remote wake-up
kotakku 0:b1ce54272580 54 #define USB_FEATURE_TEST_MODE 2 // CLEAR/SET FEATURE - Test mode
kotakku 0:b1ce54272580 55
kotakku 0:b1ce54272580 56 /* Setup Data Constants */
kotakku 0:b1ce54272580 57
kotakku 0:b1ce54272580 58 #define USB_SETUP_HOST_TO_DEVICE 0x00 // Device Request bmRequestType transfer direction - host to device transfer
kotakku 0:b1ce54272580 59 #define USB_SETUP_DEVICE_TO_HOST 0x80 // Device Request bmRequestType transfer direction - device to host transfer
kotakku 0:b1ce54272580 60 #define USB_SETUP_TYPE_STANDARD 0x00 // Device Request bmRequestType type - standard
kotakku 0:b1ce54272580 61 #define USB_SETUP_TYPE_CLASS 0x20 // Device Request bmRequestType type - class
kotakku 0:b1ce54272580 62 #define USB_SETUP_TYPE_VENDOR 0x40 // Device Request bmRequestType type - vendor
kotakku 0:b1ce54272580 63 #define USB_SETUP_RECIPIENT_DEVICE 0x00 // Device Request bmRequestType recipient - device
kotakku 0:b1ce54272580 64 #define USB_SETUP_RECIPIENT_INTERFACE 0x01 // Device Request bmRequestType recipient - interface
kotakku 0:b1ce54272580 65 #define USB_SETUP_RECIPIENT_ENDPOINT 0x02 // Device Request bmRequestType recipient - endpoint
kotakku 0:b1ce54272580 66 #define USB_SETUP_RECIPIENT_OTHER 0x03 // Device Request bmRequestType recipient - other
kotakku 0:b1ce54272580 67
kotakku 0:b1ce54272580 68 /* USB descriptors */
kotakku 0:b1ce54272580 69
kotakku 0:b1ce54272580 70 #define USB_DESCRIPTOR_DEVICE 0x01 // bDescriptorType for a Device Descriptor.
kotakku 0:b1ce54272580 71 #define USB_DESCRIPTOR_CONFIGURATION 0x02 // bDescriptorType for a Configuration Descriptor.
kotakku 0:b1ce54272580 72 #define USB_DESCRIPTOR_STRING 0x03 // bDescriptorType for a String Descriptor.
kotakku 0:b1ce54272580 73 #define USB_DESCRIPTOR_INTERFACE 0x04 // bDescriptorType for an Interface Descriptor.
kotakku 0:b1ce54272580 74 #define USB_DESCRIPTOR_ENDPOINT 0x05 // bDescriptorType for an Endpoint Descriptor.
kotakku 0:b1ce54272580 75 #define USB_DESCRIPTOR_DEVICE_QUALIFIER 0x06 // bDescriptorType for a Device Qualifier.
kotakku 0:b1ce54272580 76 #define USB_DESCRIPTOR_OTHER_SPEED 0x07 // bDescriptorType for a Other Speed Configuration.
kotakku 0:b1ce54272580 77 #define USB_DESCRIPTOR_INTERFACE_POWER 0x08 // bDescriptorType for Interface Power.
kotakku 0:b1ce54272580 78 #define USB_DESCRIPTOR_OTG 0x09 // bDescriptorType for an OTG Descriptor.
kotakku 0:b1ce54272580 79
kotakku 0:b1ce54272580 80 #define HID_DESCRIPTOR_HID 0x21
kotakku 0:b1ce54272580 81
kotakku 0:b1ce54272580 82
kotakku 0:b1ce54272580 83
kotakku 0:b1ce54272580 84 /* OTG SET FEATURE Constants */
kotakku 0:b1ce54272580 85 #define OTG_FEATURE_B_HNP_ENABLE 3 // SET FEATURE OTG - Enable B device to perform HNP
kotakku 0:b1ce54272580 86 #define OTG_FEATURE_A_HNP_SUPPORT 4 // SET FEATURE OTG - A device supports HNP
kotakku 0:b1ce54272580 87 #define OTG_FEATURE_A_ALT_HNP_SUPPORT 5 // SET FEATURE OTG - Another port on the A device supports HNP
kotakku 0:b1ce54272580 88
kotakku 0:b1ce54272580 89 /* USB Endpoint Transfer Types */
kotakku 0:b1ce54272580 90 #define USB_TRANSFER_TYPE_CONTROL 0x00 // Endpoint is a control endpoint.
kotakku 0:b1ce54272580 91 #define USB_TRANSFER_TYPE_ISOCHRONOUS 0x01 // Endpoint is an isochronous endpoint.
kotakku 0:b1ce54272580 92 #define USB_TRANSFER_TYPE_BULK 0x02 // Endpoint is a bulk endpoint.
kotakku 0:b1ce54272580 93 #define USB_TRANSFER_TYPE_INTERRUPT 0x03 // Endpoint is an interrupt endpoint.
kotakku 0:b1ce54272580 94 #define bmUSB_TRANSFER_TYPE 0x03 // bit mask to separate transfer type from ISO attributes
kotakku 0:b1ce54272580 95
kotakku 0:b1ce54272580 96
kotakku 0:b1ce54272580 97 /* Standard Feature Selectors for CLEAR_FEATURE Requests */
kotakku 0:b1ce54272580 98 #define USB_FEATURE_ENDPOINT_STALL 0 // Endpoint recipient
kotakku 0:b1ce54272580 99 #define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1 // Device recipient
kotakku 0:b1ce54272580 100 #define USB_FEATURE_TEST_MODE 2 // Device recipient
kotakku 0:b1ce54272580 101
kotakku 0:b1ce54272580 102 /* descriptor data structures */
kotakku 0:b1ce54272580 103
kotakku 0:b1ce54272580 104 /* Device descriptor structure */
kotakku 0:b1ce54272580 105 typedef struct {
kotakku 0:b1ce54272580 106 uint8_t bLength; // Length of this descriptor.
kotakku 0:b1ce54272580 107 uint8_t bDescriptorType; // DEVICE descriptor type (USB_DESCRIPTOR_DEVICE).
kotakku 0:b1ce54272580 108 uint16_t bcdUSB; // USB Spec Release Number (BCD).
kotakku 0:b1ce54272580 109 uint8_t bDeviceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
kotakku 0:b1ce54272580 110 uint8_t bDeviceSubClass; // Subclass code (assigned by the USB-IF).
kotakku 0:b1ce54272580 111 uint8_t bDeviceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
kotakku 0:b1ce54272580 112 uint8_t bMaxPacketSize0; // Maximum packet size for endpoint 0.
kotakku 0:b1ce54272580 113 uint16_t idVendor; // Vendor ID (assigned by the USB-IF).
kotakku 0:b1ce54272580 114 uint16_t idProduct; // Product ID (assigned by the manufacturer).
kotakku 0:b1ce54272580 115 uint16_t bcdDevice; // Device release number (BCD).
kotakku 0:b1ce54272580 116 uint8_t iManufacturer; // Index of String Descriptor describing the manufacturer.
kotakku 0:b1ce54272580 117 uint8_t iProduct; // Index of String Descriptor describing the product.
kotakku 0:b1ce54272580 118 uint8_t iSerialNumber; // Index of String Descriptor with the device's serial number.
kotakku 0:b1ce54272580 119 uint8_t bNumConfigurations; // Number of possible configurations.
kotakku 0:b1ce54272580 120 } __attribute__((packed)) USB_DEVICE_DESCRIPTOR;
kotakku 0:b1ce54272580 121
kotakku 0:b1ce54272580 122 /* Configuration descriptor structure */
kotakku 0:b1ce54272580 123 typedef struct {
kotakku 0:b1ce54272580 124 uint8_t bLength; // Length of this descriptor.
kotakku 0:b1ce54272580 125 uint8_t bDescriptorType; // CONFIGURATION descriptor type (USB_DESCRIPTOR_CONFIGURATION).
kotakku 0:b1ce54272580 126 uint16_t wTotalLength; // Total length of all descriptors for this configuration.
kotakku 0:b1ce54272580 127 uint8_t bNumInterfaces; // Number of interfaces in this configuration.
kotakku 0:b1ce54272580 128 uint8_t bConfigurationValue; // Value of this configuration (1 based).
kotakku 0:b1ce54272580 129 uint8_t iConfiguration; // Index of String Descriptor describing the configuration.
kotakku 0:b1ce54272580 130 uint8_t bmAttributes; // Configuration characteristics.
kotakku 0:b1ce54272580 131 uint8_t bMaxPower; // Maximum power consumed by this configuration.
kotakku 0:b1ce54272580 132 } __attribute__((packed)) USB_CONFIGURATION_DESCRIPTOR;
kotakku 0:b1ce54272580 133
kotakku 0:b1ce54272580 134 /* Interface descriptor structure */
kotakku 0:b1ce54272580 135 typedef struct {
kotakku 0:b1ce54272580 136 uint8_t bLength; // Length of this descriptor.
kotakku 0:b1ce54272580 137 uint8_t bDescriptorType; // INTERFACE descriptor type (USB_DESCRIPTOR_INTERFACE).
kotakku 0:b1ce54272580 138 uint8_t bInterfaceNumber; // Number of this interface (0 based).
kotakku 0:b1ce54272580 139 uint8_t bAlternateSetting; // Value of this alternate interface setting.
kotakku 0:b1ce54272580 140 uint8_t bNumEndpoints; // Number of endpoints in this interface.
kotakku 0:b1ce54272580 141 uint8_t bInterfaceClass; // Class code (assigned by the USB-IF). 0xFF-Vendor specific.
kotakku 0:b1ce54272580 142 uint8_t bInterfaceSubClass; // Subclass code (assigned by the USB-IF).
kotakku 0:b1ce54272580 143 uint8_t bInterfaceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
kotakku 0:b1ce54272580 144 uint8_t iInterface; // Index of String Descriptor describing the interface.
kotakku 0:b1ce54272580 145 } __attribute__((packed)) USB_INTERFACE_DESCRIPTOR;
kotakku 0:b1ce54272580 146
kotakku 0:b1ce54272580 147 /* Endpoint descriptor structure */
kotakku 0:b1ce54272580 148 typedef struct {
kotakku 0:b1ce54272580 149 uint8_t bLength; // Length of this descriptor.
kotakku 0:b1ce54272580 150 uint8_t bDescriptorType; // ENDPOINT descriptor type (USB_DESCRIPTOR_ENDPOINT).
kotakku 0:b1ce54272580 151 uint8_t bEndpointAddress; // Endpoint address. Bit 7 indicates direction (0=OUT, 1=IN).
kotakku 0:b1ce54272580 152 uint8_t bmAttributes; // Endpoint transfer type.
kotakku 0:b1ce54272580 153 uint16_t wMaxPacketSize; // Maximum packet size.
kotakku 0:b1ce54272580 154 uint8_t bInterval; // Polling interval in frames.
kotakku 0:b1ce54272580 155 } __attribute__((packed)) USB_ENDPOINT_DESCRIPTOR;
kotakku 0:b1ce54272580 156
kotakku 0:b1ce54272580 157 /* HID descriptor */
kotakku 0:b1ce54272580 158 typedef struct {
kotakku 0:b1ce54272580 159 uint8_t bLength;
kotakku 0:b1ce54272580 160 uint8_t bDescriptorType;
kotakku 0:b1ce54272580 161 uint16_t bcdHID; // HID class specification release
kotakku 0:b1ce54272580 162 uint8_t bCountryCode;
kotakku 0:b1ce54272580 163 uint8_t bNumDescriptors; // Number of additional class specific descriptors
kotakku 0:b1ce54272580 164 uint8_t bDescrType; // Type of class descriptor
kotakku 0:b1ce54272580 165 uint16_t wDescriptorLength; // Total size of the Report descriptor
kotakku 0:b1ce54272580 166 } __attribute__((packed)) USB_HID_DESCRIPTOR;
kotakku 0:b1ce54272580 167
kotakku 0:b1ce54272580 168 typedef struct {
kotakku 0:b1ce54272580 169 uint8_t bDescrType; // Type of class descriptor
kotakku 0:b1ce54272580 170 uint16_t wDescriptorLength; // Total size of the Report descriptor
kotakku 0:b1ce54272580 171 } __attribute__((packed)) HID_CLASS_DESCRIPTOR_LEN_AND_TYPE;
kotakku 0:b1ce54272580 172
kotakku 0:b1ce54272580 173 #endif // _ch9_h_
kotakku 0:b1ce54272580 174