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 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 #if !defined(_usb_h_) || defined(_max3421e_h_)
kotakku 0:b1ce54272580 25 #error "Never include max3421e.h directly; include Usb.h instead"
kotakku 0:b1ce54272580 26 #else
kotakku 0:b1ce54272580 27
kotakku 0:b1ce54272580 28 #define _max3421e_h_
kotakku 0:b1ce54272580 29
kotakku 0:b1ce54272580 30 /* MAX3421E register/bit names and bitmasks */
kotakku 0:b1ce54272580 31
kotakku 0:b1ce54272580 32 /* Arduino pin definitions */
kotakku 0:b1ce54272580 33 /* pin numbers to port numbers */
kotakku 0:b1ce54272580 34
kotakku 0:b1ce54272580 35 #define SE0 0
kotakku 0:b1ce54272580 36 #define SE1 1
kotakku 0:b1ce54272580 37 #define FSHOST 2
kotakku 0:b1ce54272580 38 #define LSHOST 3
kotakku 0:b1ce54272580 39
kotakku 0:b1ce54272580 40 /* MAX3421E command byte format: rrrrr0wa where 'r' is register number */
kotakku 0:b1ce54272580 41 //
kotakku 0:b1ce54272580 42 // MAX3421E Registers in HOST mode.
kotakku 0:b1ce54272580 43 //
kotakku 0:b1ce54272580 44 #define rRCVFIFO 0x08 //1<<3
kotakku 0:b1ce54272580 45 #define rSNDFIFO 0x10 //2<<3
kotakku 0:b1ce54272580 46 #define rSUDFIFO 0x20 //4<<3
kotakku 0:b1ce54272580 47 #define rRCVBC 0x30 //6<<3
kotakku 0:b1ce54272580 48 #define rSNDBC 0x38 //7<<3
kotakku 0:b1ce54272580 49
kotakku 0:b1ce54272580 50 #define rUSBIRQ 0x68 //13<<3
kotakku 0:b1ce54272580 51 /* USBIRQ Bits */
kotakku 0:b1ce54272580 52 #define bmVBUSIRQ 0x40 //b6
kotakku 0:b1ce54272580 53 #define bmNOVBUSIRQ 0x20 //b5
kotakku 0:b1ce54272580 54 #define bmOSCOKIRQ 0x01 //b0
kotakku 0:b1ce54272580 55
kotakku 0:b1ce54272580 56 #define rUSBIEN 0x70 //14<<3
kotakku 0:b1ce54272580 57 /* USBIEN Bits */
kotakku 0:b1ce54272580 58 #define bmVBUSIE 0x40 //b6
kotakku 0:b1ce54272580 59 #define bmNOVBUSIE 0x20 //b5
kotakku 0:b1ce54272580 60 #define bmOSCOKIE 0x01 //b0
kotakku 0:b1ce54272580 61
kotakku 0:b1ce54272580 62 #define rUSBCTL 0x78 //15<<3
kotakku 0:b1ce54272580 63 /* USBCTL Bits */
kotakku 0:b1ce54272580 64 #define bmCHIPRES 0x20 //b5
kotakku 0:b1ce54272580 65 #define bmPWRDOWN 0x10 //b4
kotakku 0:b1ce54272580 66
kotakku 0:b1ce54272580 67 #define rCPUCTL 0x80 //16<<3
kotakku 0:b1ce54272580 68 /* CPUCTL Bits */
kotakku 0:b1ce54272580 69 #define bmPUSLEWID1 0x80 //b7
kotakku 0:b1ce54272580 70 #define bmPULSEWID0 0x40 //b6
kotakku 0:b1ce54272580 71 #define bmIE 0x01 //b0
kotakku 0:b1ce54272580 72
kotakku 0:b1ce54272580 73 #define rPINCTL 0x88 //17<<3
kotakku 0:b1ce54272580 74 /* PINCTL Bits */
kotakku 0:b1ce54272580 75 #define bmFDUPSPI 0x10 //b4
kotakku 0:b1ce54272580 76 #define bmINTLEVEL 0x08 //b3
kotakku 0:b1ce54272580 77 #define bmPOSINT 0x04 //b2
kotakku 0:b1ce54272580 78 #define bmGPXB 0x02 //b1
kotakku 0:b1ce54272580 79 #define bmGPXA 0x01 //b0
kotakku 0:b1ce54272580 80 // GPX pin selections
kotakku 0:b1ce54272580 81 #define GPX_OPERATE 0x00
kotakku 0:b1ce54272580 82 #define GPX_VBDET 0x01
kotakku 0:b1ce54272580 83 #define GPX_BUSACT 0x02
kotakku 0:b1ce54272580 84 #define GPX_SOF 0x03
kotakku 0:b1ce54272580 85
kotakku 0:b1ce54272580 86 #define rREVISION 0x90 //18<<3
kotakku 0:b1ce54272580 87
kotakku 0:b1ce54272580 88 #define rIOPINS1 0xa0 //20<<3
kotakku 0:b1ce54272580 89
kotakku 0:b1ce54272580 90 /* IOPINS1 Bits */
kotakku 0:b1ce54272580 91 #define bmGPOUT0 0x01
kotakku 0:b1ce54272580 92 #define bmGPOUT1 0x02
kotakku 0:b1ce54272580 93 #define bmGPOUT2 0x04
kotakku 0:b1ce54272580 94 #define bmGPOUT3 0x08
kotakku 0:b1ce54272580 95 #define bmGPIN0 0x10
kotakku 0:b1ce54272580 96 #define bmGPIN1 0x20
kotakku 0:b1ce54272580 97 #define bmGPIN2 0x40
kotakku 0:b1ce54272580 98 #define bmGPIN3 0x80
kotakku 0:b1ce54272580 99
kotakku 0:b1ce54272580 100 #define rIOPINS2 0xa8 //21<<3
kotakku 0:b1ce54272580 101 /* IOPINS2 Bits */
kotakku 0:b1ce54272580 102 #define bmGPOUT4 0x01
kotakku 0:b1ce54272580 103 #define bmGPOUT5 0x02
kotakku 0:b1ce54272580 104 #define bmGPOUT6 0x04
kotakku 0:b1ce54272580 105 #define bmGPOUT7 0x08
kotakku 0:b1ce54272580 106 #define bmGPIN4 0x10
kotakku 0:b1ce54272580 107 #define bmGPIN5 0x20
kotakku 0:b1ce54272580 108 #define bmGPIN6 0x40
kotakku 0:b1ce54272580 109 #define bmGPIN7 0x80
kotakku 0:b1ce54272580 110
kotakku 0:b1ce54272580 111 #define rGPINIRQ 0xb0 //22<<3
kotakku 0:b1ce54272580 112 /* GPINIRQ Bits */
kotakku 0:b1ce54272580 113 #define bmGPINIRQ0 0x01
kotakku 0:b1ce54272580 114 #define bmGPINIRQ1 0x02
kotakku 0:b1ce54272580 115 #define bmGPINIRQ2 0x04
kotakku 0:b1ce54272580 116 #define bmGPINIRQ3 0x08
kotakku 0:b1ce54272580 117 #define bmGPINIRQ4 0x10
kotakku 0:b1ce54272580 118 #define bmGPINIRQ5 0x20
kotakku 0:b1ce54272580 119 #define bmGPINIRQ6 0x40
kotakku 0:b1ce54272580 120 #define bmGPINIRQ7 0x80
kotakku 0:b1ce54272580 121
kotakku 0:b1ce54272580 122 #define rGPINIEN 0xb8 //23<<3
kotakku 0:b1ce54272580 123 /* GPINIEN Bits */
kotakku 0:b1ce54272580 124 #define bmGPINIEN0 0x01
kotakku 0:b1ce54272580 125 #define bmGPINIEN1 0x02
kotakku 0:b1ce54272580 126 #define bmGPINIEN2 0x04
kotakku 0:b1ce54272580 127 #define bmGPINIEN3 0x08
kotakku 0:b1ce54272580 128 #define bmGPINIEN4 0x10
kotakku 0:b1ce54272580 129 #define bmGPINIEN5 0x20
kotakku 0:b1ce54272580 130 #define bmGPINIEN6 0x40
kotakku 0:b1ce54272580 131 #define bmGPINIEN7 0x80
kotakku 0:b1ce54272580 132
kotakku 0:b1ce54272580 133 #define rGPINPOL 0xc0 //24<<3
kotakku 0:b1ce54272580 134 /* GPINPOL Bits */
kotakku 0:b1ce54272580 135 #define bmGPINPOL0 0x01
kotakku 0:b1ce54272580 136 #define bmGPINPOL1 0x02
kotakku 0:b1ce54272580 137 #define bmGPINPOL2 0x04
kotakku 0:b1ce54272580 138 #define bmGPINPOL3 0x08
kotakku 0:b1ce54272580 139 #define bmGPINPOL4 0x10
kotakku 0:b1ce54272580 140 #define bmGPINPOL5 0x20
kotakku 0:b1ce54272580 141 #define bmGPINPOL6 0x40
kotakku 0:b1ce54272580 142 #define bmGPINPOL7 0x80
kotakku 0:b1ce54272580 143
kotakku 0:b1ce54272580 144 #define rHIRQ 0xc8 //25<<3
kotakku 0:b1ce54272580 145 /* HIRQ Bits */
kotakku 0:b1ce54272580 146 #define bmBUSEVENTIRQ 0x01 // indicates BUS Reset Done or BUS Resume
kotakku 0:b1ce54272580 147 #define bmRWUIRQ 0x02
kotakku 0:b1ce54272580 148 #define bmRCVDAVIRQ 0x04
kotakku 0:b1ce54272580 149 #define bmSNDBAVIRQ 0x08
kotakku 0:b1ce54272580 150 #define bmSUSDNIRQ 0x10
kotakku 0:b1ce54272580 151 #define bmCONDETIRQ 0x20
kotakku 0:b1ce54272580 152 #define bmFRAMEIRQ 0x40
kotakku 0:b1ce54272580 153 #define bmHXFRDNIRQ 0x80
kotakku 0:b1ce54272580 154
kotakku 0:b1ce54272580 155 #define rHIEN 0xd0 //26<<3
kotakku 0:b1ce54272580 156
kotakku 0:b1ce54272580 157 /* HIEN Bits */
kotakku 0:b1ce54272580 158 #define bmBUSEVENTIE 0x01
kotakku 0:b1ce54272580 159 #define bmRWUIE 0x02
kotakku 0:b1ce54272580 160 #define bmRCVDAVIE 0x04
kotakku 0:b1ce54272580 161 #define bmSNDBAVIE 0x08
kotakku 0:b1ce54272580 162 #define bmSUSDNIE 0x10
kotakku 0:b1ce54272580 163 #define bmCONDETIE 0x20
kotakku 0:b1ce54272580 164 #define bmFRAMEIE 0x40
kotakku 0:b1ce54272580 165 #define bmHXFRDNIE 0x80
kotakku 0:b1ce54272580 166
kotakku 0:b1ce54272580 167 #define rMODE 0xd8 //27<<3
kotakku 0:b1ce54272580 168
kotakku 0:b1ce54272580 169 /* MODE Bits */
kotakku 0:b1ce54272580 170 #define bmHOST 0x01
kotakku 0:b1ce54272580 171 #define bmLOWSPEED 0x02
kotakku 0:b1ce54272580 172 #define bmHUBPRE 0x04
kotakku 0:b1ce54272580 173 #define bmSOFKAENAB 0x08
kotakku 0:b1ce54272580 174 #define bmSEPIRQ 0x10
kotakku 0:b1ce54272580 175 #define bmDELAYISO 0x20
kotakku 0:b1ce54272580 176 #define bmDMPULLDN 0x40
kotakku 0:b1ce54272580 177 #define bmDPPULLDN 0x80
kotakku 0:b1ce54272580 178
kotakku 0:b1ce54272580 179 #define rPERADDR 0xe0 //28<<3
kotakku 0:b1ce54272580 180
kotakku 0:b1ce54272580 181 #define rHCTL 0xe8 //29<<3
kotakku 0:b1ce54272580 182 /* HCTL Bits */
kotakku 0:b1ce54272580 183 #define bmBUSRST 0x01
kotakku 0:b1ce54272580 184 #define bmFRMRST 0x02
kotakku 0:b1ce54272580 185 #define bmSAMPLEBUS 0x04
kotakku 0:b1ce54272580 186 #define bmSIGRSM 0x08
kotakku 0:b1ce54272580 187 #define bmRCVTOG0 0x10
kotakku 0:b1ce54272580 188 #define bmRCVTOG1 0x20
kotakku 0:b1ce54272580 189 #define bmSNDTOG0 0x40
kotakku 0:b1ce54272580 190 #define bmSNDTOG1 0x80
kotakku 0:b1ce54272580 191
kotakku 0:b1ce54272580 192 #define rHXFR 0xf0 //30<<3
kotakku 0:b1ce54272580 193 /* Host transfer token values for writing the HXFR register (R30) */
kotakku 0:b1ce54272580 194 /* OR this bit field with the endpoint number in bits 3:0 */
kotakku 0:b1ce54272580 195 #define tokSETUP 0x10 // HS=0, ISO=0, OUTNIN=0, SETUP=1
kotakku 0:b1ce54272580 196 #define tokIN 0x00 // HS=0, ISO=0, OUTNIN=0, SETUP=0
kotakku 0:b1ce54272580 197 #define tokOUT 0x20 // HS=0, ISO=0, OUTNIN=1, SETUP=0
kotakku 0:b1ce54272580 198 #define tokINHS 0x80 // HS=1, ISO=0, OUTNIN=0, SETUP=0
kotakku 0:b1ce54272580 199 #define tokOUTHS 0xA0 // HS=1, ISO=0, OUTNIN=1, SETUP=0
kotakku 0:b1ce54272580 200 #define tokISOIN 0x40 // HS=0, ISO=1, OUTNIN=0, SETUP=0
kotakku 0:b1ce54272580 201 #define tokISOOUT 0x60 // HS=0, ISO=1, OUTNIN=1, SETUP=0
kotakku 0:b1ce54272580 202
kotakku 0:b1ce54272580 203 #define rHRSL 0xf8 //31<<3
kotakku 0:b1ce54272580 204
kotakku 0:b1ce54272580 205 /* HRSL Bits */
kotakku 0:b1ce54272580 206 #define bmRCVTOGRD 0x10
kotakku 0:b1ce54272580 207 #define bmSNDTOGRD 0x20
kotakku 0:b1ce54272580 208 #define bmKSTATUS 0x40
kotakku 0:b1ce54272580 209 #define bmJSTATUS 0x80
kotakku 0:b1ce54272580 210 #define bmSE0 0x00 //SE0 - disconnect state
kotakku 0:b1ce54272580 211 #define bmSE1 0xc0 //SE1 - illegal state
kotakku 0:b1ce54272580 212
kotakku 0:b1ce54272580 213 /* Host error result codes, the 4 LSB's in the HRSL register */
kotakku 0:b1ce54272580 214 #define hrSUCCESS 0x00
kotakku 0:b1ce54272580 215 #define hrBUSY 0x01
kotakku 0:b1ce54272580 216 #define hrBADREQ 0x02
kotakku 0:b1ce54272580 217 #define hrUNDEF 0x03
kotakku 0:b1ce54272580 218 #define hrNAK 0x04
kotakku 0:b1ce54272580 219 #define hrSTALL 0x05
kotakku 0:b1ce54272580 220 #define hrTOGERR 0x06
kotakku 0:b1ce54272580 221 #define hrWRONGPID 0x07
kotakku 0:b1ce54272580 222 #define hrBADBC 0x08
kotakku 0:b1ce54272580 223 #define hrPIDERR 0x09
kotakku 0:b1ce54272580 224 #define hrPKTERR 0x0A
kotakku 0:b1ce54272580 225 #define hrCRCERR 0x0B
kotakku 0:b1ce54272580 226 #define hrKERR 0x0C
kotakku 0:b1ce54272580 227 #define hrJERR 0x0D
kotakku 0:b1ce54272580 228 #define hrTIMEOUT 0x0E
kotakku 0:b1ce54272580 229 #define hrBABBLE 0x0F
kotakku 0:b1ce54272580 230
kotakku 0:b1ce54272580 231 #define MODE_FS_HOST (bmDPPULLDN|bmDMPULLDN|bmHOST|bmSOFKAENAB)
kotakku 0:b1ce54272580 232 #define MODE_LS_HOST (bmDPPULLDN|bmDMPULLDN|bmHOST|bmLOWSPEED|bmSOFKAENAB)
kotakku 0:b1ce54272580 233
kotakku 0:b1ce54272580 234
kotakku 0:b1ce54272580 235 #endif //_max3421e_h_
kotakku 0:b1ce54272580 236