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:
robo_ichinoseki_a
Date:
Sat May 02 05:56:48 2020 +0000
Revision:
1:da31140f2a1c
Parent:
0:b1ce54272580
update

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 software may be distributed and modified under the terms of the GNU
kotakku 0:b1ce54272580 4 General Public License version 2 (GPL2) as published by the Free Software
kotakku 0:b1ce54272580 5 Foundation and appearing in the file GPL2.TXT included in the packaging of
kotakku 0:b1ce54272580 6 this file. Please note that GPL2 Section 2[b] requires that all works based
kotakku 0:b1ce54272580 7 on this software must also be made publicly available under the terms of
kotakku 0:b1ce54272580 8 the GPL2 ("Copyleft").
kotakku 0:b1ce54272580 9
kotakku 0:b1ce54272580 10 Contact information
kotakku 0:b1ce54272580 11 -------------------
kotakku 0:b1ce54272580 12
kotakku 0:b1ce54272580 13 Circuits At Home, LTD
kotakku 0:b1ce54272580 14 Web : http://www.circuitsathome.com
kotakku 0:b1ce54272580 15 e-mail : support@circuitsathome.com
kotakku 0:b1ce54272580 16 */
kotakku 0:b1ce54272580 17 #if !defined(__USBHID_H__)
kotakku 0:b1ce54272580 18 #define __USBHID_H__
kotakku 0:b1ce54272580 19
kotakku 0:b1ce54272580 20 #include "Usb.h"
kotakku 0:b1ce54272580 21 #include "hidusagestr.h"
kotakku 0:b1ce54272580 22
kotakku 0:b1ce54272580 23 #define MAX_REPORT_PARSERS 2
kotakku 0:b1ce54272580 24 #define HID_MAX_HID_CLASS_DESCRIPTORS 5
kotakku 0:b1ce54272580 25
kotakku 0:b1ce54272580 26 #define DATA_SIZE_MASK 0x03
kotakku 0:b1ce54272580 27 #define TYPE_MASK 0x0C
kotakku 0:b1ce54272580 28 #define TAG_MASK 0xF0
kotakku 0:b1ce54272580 29
kotakku 0:b1ce54272580 30 #define DATA_SIZE_0 0x00
kotakku 0:b1ce54272580 31 #define DATA_SIZE_1 0x01
kotakku 0:b1ce54272580 32 #define DATA_SIZE_2 0x02
kotakku 0:b1ce54272580 33 #define DATA_SIZE_4 0x03
kotakku 0:b1ce54272580 34
kotakku 0:b1ce54272580 35 #define TYPE_MAIN 0x00
kotakku 0:b1ce54272580 36 #define TYPE_GLOBAL 0x04
kotakku 0:b1ce54272580 37 #define TYPE_LOCAL 0x08
kotakku 0:b1ce54272580 38
kotakku 0:b1ce54272580 39 #define TAG_MAIN_INPUT 0x80
kotakku 0:b1ce54272580 40 #define TAG_MAIN_OUTPUT 0x90
kotakku 0:b1ce54272580 41 #define TAG_MAIN_COLLECTION 0xA0
kotakku 0:b1ce54272580 42 #define TAG_MAIN_FEATURE 0xB0
kotakku 0:b1ce54272580 43 #define TAG_MAIN_ENDCOLLECTION 0xC0
kotakku 0:b1ce54272580 44
kotakku 0:b1ce54272580 45 #define TAG_GLOBAL_USAGEPAGE 0x00
kotakku 0:b1ce54272580 46 #define TAG_GLOBAL_LOGICALMIN 0x10
kotakku 0:b1ce54272580 47 #define TAG_GLOBAL_LOGICALMAX 0x20
kotakku 0:b1ce54272580 48 #define TAG_GLOBAL_PHYSMIN 0x30
kotakku 0:b1ce54272580 49 #define TAG_GLOBAL_PHYSMAX 0x40
kotakku 0:b1ce54272580 50 #define TAG_GLOBAL_UNITEXP 0x50
kotakku 0:b1ce54272580 51 #define TAG_GLOBAL_UNIT 0x60
kotakku 0:b1ce54272580 52 #define TAG_GLOBAL_REPORTSIZE 0x70
kotakku 0:b1ce54272580 53 #define TAG_GLOBAL_REPORTID 0x80
kotakku 0:b1ce54272580 54 #define TAG_GLOBAL_REPORTCOUNT 0x90
kotakku 0:b1ce54272580 55 #define TAG_GLOBAL_PUSH 0xA0
kotakku 0:b1ce54272580 56 #define TAG_GLOBAL_POP 0xB0
kotakku 0:b1ce54272580 57
kotakku 0:b1ce54272580 58 #define TAG_LOCAL_USAGE 0x00
kotakku 0:b1ce54272580 59 #define TAG_LOCAL_USAGEMIN 0x10
kotakku 0:b1ce54272580 60 #define TAG_LOCAL_USAGEMAX 0x20
kotakku 0:b1ce54272580 61
kotakku 0:b1ce54272580 62 /* HID requests */
kotakku 0:b1ce54272580 63 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
kotakku 0:b1ce54272580 64 #define bmREQ_HID_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
kotakku 0:b1ce54272580 65 #define bmREQ_HID_REPORT USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_INTERFACE
kotakku 0:b1ce54272580 66
kotakku 0:b1ce54272580 67 /* HID constants. Not part of chapter 9 */
kotakku 0:b1ce54272580 68 /* Class-Specific Requests */
kotakku 0:b1ce54272580 69 #define HID_REQUEST_GET_REPORT 0x01
kotakku 0:b1ce54272580 70 #define HID_REQUEST_GET_IDLE 0x02
kotakku 0:b1ce54272580 71 #define HID_REQUEST_GET_PROTOCOL 0x03
kotakku 0:b1ce54272580 72 #define HID_REQUEST_SET_REPORT 0x09
kotakku 0:b1ce54272580 73 #define HID_REQUEST_SET_IDLE 0x0A
kotakku 0:b1ce54272580 74 #define HID_REQUEST_SET_PROTOCOL 0x0B
kotakku 0:b1ce54272580 75
kotakku 0:b1ce54272580 76 /* Class Descriptor Types */
kotakku 0:b1ce54272580 77 #define HID_DESCRIPTOR_HID 0x21
kotakku 0:b1ce54272580 78 #define HID_DESCRIPTOR_REPORT 0x22
kotakku 0:b1ce54272580 79 #define HID_DESRIPTOR_PHY 0x23
kotakku 0:b1ce54272580 80
kotakku 0:b1ce54272580 81 /* Protocol Selection */
kotakku 0:b1ce54272580 82 #define USB_HID_BOOT_PROTOCOL 0x00
kotakku 0:b1ce54272580 83 #define HID_RPT_PROTOCOL 0x01
kotakku 0:b1ce54272580 84
kotakku 0:b1ce54272580 85 /* HID Interface Class Code */
kotakku 0:b1ce54272580 86 #define HID_INTF 0x03
kotakku 0:b1ce54272580 87
kotakku 0:b1ce54272580 88 /* HID Interface Class SubClass Codes */
kotakku 0:b1ce54272580 89 #define HID_BOOT_INTF_SUBCLASS 0x01
kotakku 0:b1ce54272580 90
kotakku 0:b1ce54272580 91 /* HID Interface Class Protocol Codes */
kotakku 0:b1ce54272580 92 #define USB_HID_PROTOCOL_NONE 0x00
kotakku 0:b1ce54272580 93 #define USB_HID_PROTOCOL_KEYBOARD 0x01
kotakku 0:b1ce54272580 94 #define USB_HID_PROTOCOL_MOUSE 0x02
kotakku 0:b1ce54272580 95
kotakku 0:b1ce54272580 96 #define HID_ITEM_TYPE_MAIN 0
kotakku 0:b1ce54272580 97 #define HID_ITEM_TYPE_GLOBAL 1
kotakku 0:b1ce54272580 98 #define HID_ITEM_TYPE_LOCAL 2
kotakku 0:b1ce54272580 99 #define HID_ITEM_TYPE_RESERVED 3
kotakku 0:b1ce54272580 100
kotakku 0:b1ce54272580 101 #define HID_LONG_ITEM_PREFIX 0xfe // Long item prefix value
kotakku 0:b1ce54272580 102
kotakku 0:b1ce54272580 103 #define bmHID_MAIN_ITEM_TAG 0xfc // Main item tag mask
kotakku 0:b1ce54272580 104
kotakku 0:b1ce54272580 105 #define bmHID_MAIN_ITEM_INPUT 0x80 // Main item Input tag value
kotakku 0:b1ce54272580 106 #define bmHID_MAIN_ITEM_OUTPUT 0x90 // Main item Output tag value
kotakku 0:b1ce54272580 107 #define bmHID_MAIN_ITEM_FEATURE 0xb0 // Main item Feature tag value
kotakku 0:b1ce54272580 108 #define bmHID_MAIN_ITEM_COLLECTION 0xa0 // Main item Collection tag value
kotakku 0:b1ce54272580 109 #define bmHID_MAIN_ITEM_END_COLLECTION 0xce // Main item End Collection tag value
kotakku 0:b1ce54272580 110
kotakku 0:b1ce54272580 111 #define HID_MAIN_ITEM_COLLECTION_PHYSICAL 0
kotakku 0:b1ce54272580 112 #define HID_MAIN_ITEM_COLLECTION_APPLICATION 1
kotakku 0:b1ce54272580 113 #define HID_MAIN_ITEM_COLLECTION_LOGICAL 2
kotakku 0:b1ce54272580 114 #define HID_MAIN_ITEM_COLLECTION_REPORT 3
kotakku 0:b1ce54272580 115 #define HID_MAIN_ITEM_COLLECTION_NAMED_ARRAY 4
kotakku 0:b1ce54272580 116 #define HID_MAIN_ITEM_COLLECTION_USAGE_SWITCH 5
kotakku 0:b1ce54272580 117 #define HID_MAIN_ITEM_COLLECTION_USAGE_MODIFIER 6
kotakku 0:b1ce54272580 118
kotakku 0:b1ce54272580 119 struct HidItemPrefix {
kotakku 0:b1ce54272580 120 uint8_t bSize : 2;
kotakku 0:b1ce54272580 121 uint8_t bType : 2;
kotakku 0:b1ce54272580 122 uint8_t bTag : 4;
kotakku 0:b1ce54272580 123 };
kotakku 0:b1ce54272580 124
kotakku 0:b1ce54272580 125 struct MainItemIOFeature {
kotakku 0:b1ce54272580 126 uint8_t bmIsConstantOrData : 1;
kotakku 0:b1ce54272580 127 uint8_t bmIsArrayOrVariable : 1;
kotakku 0:b1ce54272580 128 uint8_t bmIsRelativeOrAbsolute : 1;
kotakku 0:b1ce54272580 129 uint8_t bmIsWrapOrNoWrap : 1;
kotakku 0:b1ce54272580 130 uint8_t bmIsNonLonearOrLinear : 1;
kotakku 0:b1ce54272580 131 uint8_t bmIsNoPreferedOrPrefered : 1;
kotakku 0:b1ce54272580 132 uint8_t bmIsNullOrNoNull : 1;
kotakku 0:b1ce54272580 133 uint8_t bmIsVolatileOrNonVolatile : 1;
kotakku 0:b1ce54272580 134 };
kotakku 0:b1ce54272580 135
kotakku 0:b1ce54272580 136 class USBHID;
kotakku 0:b1ce54272580 137
kotakku 0:b1ce54272580 138 class HIDReportParser {
kotakku 0:b1ce54272580 139 public:
kotakku 0:b1ce54272580 140 virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) = 0;
kotakku 0:b1ce54272580 141 };
kotakku 0:b1ce54272580 142
kotakku 0:b1ce54272580 143 class USBHID : public USBDeviceConfig, public UsbConfigXtracter {
kotakku 0:b1ce54272580 144 protected:
kotakku 0:b1ce54272580 145 USB *pUsb; // USB class instance pointer
kotakku 0:b1ce54272580 146 uint8_t bAddress; // address
kotakku 0:b1ce54272580 147
kotakku 0:b1ce54272580 148 protected:
kotakku 0:b1ce54272580 149 static const uint8_t epInterruptInIndex = 1; // InterruptIN endpoint index
kotakku 0:b1ce54272580 150 static const uint8_t epInterruptOutIndex = 2; // InterruptOUT endpoint index
kotakku 0:b1ce54272580 151
kotakku 0:b1ce54272580 152 static const uint8_t maxHidInterfaces = 3;
kotakku 0:b1ce54272580 153 static const uint8_t maxEpPerInterface = 2;
kotakku 0:b1ce54272580 154 static const uint8_t totalEndpoints = (maxHidInterfaces * maxEpPerInterface + 1); // We need to make room for the control endpoint
kotakku 0:b1ce54272580 155
kotakku 0:b1ce54272580 156 void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
kotakku 0:b1ce54272580 157 void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
kotakku 0:b1ce54272580 158
kotakku 0:b1ce54272580 159 virtual HIDReportParser* GetReportParser(uint8_t id __attribute__((unused))) {
kotakku 0:b1ce54272580 160 return NULL;
kotakku 0:b1ce54272580 161 };
kotakku 0:b1ce54272580 162
kotakku 0:b1ce54272580 163 public:
kotakku 0:b1ce54272580 164
kotakku 0:b1ce54272580 165 USBHID(USB *pusb) : pUsb(pusb) {
kotakku 0:b1ce54272580 166 };
kotakku 0:b1ce54272580 167
kotakku 0:b1ce54272580 168 const USB* GetUsb() {
kotakku 0:b1ce54272580 169 return pUsb;
kotakku 0:b1ce54272580 170 };
kotakku 0:b1ce54272580 171
kotakku 0:b1ce54272580 172 virtual bool SetReportParser(uint8_t id __attribute__((unused)), HIDReportParser *prs __attribute__((unused))) {
kotakku 0:b1ce54272580 173 return false;
kotakku 0:b1ce54272580 174 };
kotakku 0:b1ce54272580 175
kotakku 0:b1ce54272580 176 uint8_t SetProtocol(uint8_t iface, uint8_t protocol);
kotakku 0:b1ce54272580 177 uint8_t GetProtocol(uint8_t iface, uint8_t* dataptr);
kotakku 0:b1ce54272580 178 uint8_t GetIdle(uint8_t iface, uint8_t reportID, uint8_t* dataptr);
kotakku 0:b1ce54272580 179 uint8_t SetIdle(uint8_t iface, uint8_t reportID, uint8_t duration);
kotakku 0:b1ce54272580 180
kotakku 0:b1ce54272580 181 uint8_t GetReportDescr(uint16_t wIndex, USBReadParser *parser = NULL);
kotakku 0:b1ce54272580 182
kotakku 0:b1ce54272580 183 uint8_t GetHidDescr(uint8_t ep, uint16_t nbytes, uint8_t* dataptr);
kotakku 0:b1ce54272580 184 uint8_t GetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
kotakku 0:b1ce54272580 185 uint8_t SetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
kotakku 0:b1ce54272580 186 };
kotakku 0:b1ce54272580 187
kotakku 0:b1ce54272580 188 #endif // __USBHID_H__
kotakku 0:b1ce54272580 189