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(__USBHUB_H__)
kotakku 0:b1ce54272580 18 #define __USBHUB_H__
kotakku 0:b1ce54272580 19
kotakku 0:b1ce54272580 20 #include "Usb.h"
kotakku 0:b1ce54272580 21
kotakku 0:b1ce54272580 22 #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type
kotakku 0:b1ce54272580 23
kotakku 0:b1ce54272580 24 // Hub Requests
kotakku 0:b1ce54272580 25 #define bmREQ_CLEAR_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
kotakku 0:b1ce54272580 26 #define bmREQ_CLEAR_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 27 #define bmREQ_CLEAR_TT_BUFFER USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 28 #define bmREQ_GET_HUB_DESCRIPTOR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
kotakku 0:b1ce54272580 29 #define bmREQ_GET_HUB_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
kotakku 0:b1ce54272580 30 #define bmREQ_GET_PORT_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 31 #define bmREQ_RESET_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 32 #define bmREQ_SET_HUB_DESCRIPTOR USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
kotakku 0:b1ce54272580 33 #define bmREQ_SET_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
kotakku 0:b1ce54272580 34 #define bmREQ_SET_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 35 #define bmREQ_GET_TT_STATE USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 36 #define bmREQ_STOP_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
kotakku 0:b1ce54272580 37
kotakku 0:b1ce54272580 38 // Hub Class Requests
kotakku 0:b1ce54272580 39 #define HUB_REQUEST_CLEAR_TT_BUFFER 8
kotakku 0:b1ce54272580 40 #define HUB_REQUEST_RESET_TT 9
kotakku 0:b1ce54272580 41 #define HUB_REQUEST_GET_TT_STATE 10
kotakku 0:b1ce54272580 42 #define HUB_REQUEST_STOP_TT 11
kotakku 0:b1ce54272580 43
kotakku 0:b1ce54272580 44 // Hub Features
kotakku 0:b1ce54272580 45 #define HUB_FEATURE_C_HUB_LOCAL_POWER 0
kotakku 0:b1ce54272580 46 #define HUB_FEATURE_C_HUB_OVER_CURRENT 1
kotakku 0:b1ce54272580 47 #define HUB_FEATURE_PORT_CONNECTION 0
kotakku 0:b1ce54272580 48 #define HUB_FEATURE_PORT_ENABLE 1
kotakku 0:b1ce54272580 49 #define HUB_FEATURE_PORT_SUSPEND 2
kotakku 0:b1ce54272580 50 #define HUB_FEATURE_PORT_OVER_CURRENT 3
kotakku 0:b1ce54272580 51 #define HUB_FEATURE_PORT_RESET 4
kotakku 0:b1ce54272580 52 #define HUB_FEATURE_PORT_POWER 8
kotakku 0:b1ce54272580 53 #define HUB_FEATURE_PORT_LOW_SPEED 9
kotakku 0:b1ce54272580 54 #define HUB_FEATURE_C_PORT_CONNECTION 16
kotakku 0:b1ce54272580 55 #define HUB_FEATURE_C_PORT_ENABLE 17
kotakku 0:b1ce54272580 56 #define HUB_FEATURE_C_PORT_SUSPEND 18
kotakku 0:b1ce54272580 57 #define HUB_FEATURE_C_PORT_OVER_CURRENT 19
kotakku 0:b1ce54272580 58 #define HUB_FEATURE_C_PORT_RESET 20
kotakku 0:b1ce54272580 59 #define HUB_FEATURE_PORT_TEST 21
kotakku 0:b1ce54272580 60 #define HUB_FEATURE_PORT_INDICATOR 22
kotakku 0:b1ce54272580 61
kotakku 0:b1ce54272580 62 // Hub Port Test Modes
kotakku 0:b1ce54272580 63 #define HUB_PORT_TEST_MODE_J 1
kotakku 0:b1ce54272580 64 #define HUB_PORT_TEST_MODE_K 2
kotakku 0:b1ce54272580 65 #define HUB_PORT_TEST_MODE_SE0_NAK 3
kotakku 0:b1ce54272580 66 #define HUB_PORT_TEST_MODE_PACKET 4
kotakku 0:b1ce54272580 67 #define HUB_PORT_TEST_MODE_FORCE_ENABLE 5
kotakku 0:b1ce54272580 68
kotakku 0:b1ce54272580 69 // Hub Port Indicator Color
kotakku 0:b1ce54272580 70 #define HUB_PORT_INDICATOR_AUTO 0
kotakku 0:b1ce54272580 71 #define HUB_PORT_INDICATOR_AMBER 1
kotakku 0:b1ce54272580 72 #define HUB_PORT_INDICATOR_GREEN 2
kotakku 0:b1ce54272580 73 #define HUB_PORT_INDICATOR_OFF 3
kotakku 0:b1ce54272580 74
kotakku 0:b1ce54272580 75 // Hub Port Status Bitmasks
kotakku 0:b1ce54272580 76 #define bmHUB_PORT_STATUS_PORT_CONNECTION 0x0001
kotakku 0:b1ce54272580 77 #define bmHUB_PORT_STATUS_PORT_ENABLE 0x0002
kotakku 0:b1ce54272580 78 #define bmHUB_PORT_STATUS_PORT_SUSPEND 0x0004
kotakku 0:b1ce54272580 79 #define bmHUB_PORT_STATUS_PORT_OVER_CURRENT 0x0008
kotakku 0:b1ce54272580 80 #define bmHUB_PORT_STATUS_PORT_RESET 0x0010
kotakku 0:b1ce54272580 81 #define bmHUB_PORT_STATUS_PORT_POWER 0x0100
kotakku 0:b1ce54272580 82 #define bmHUB_PORT_STATUS_PORT_LOW_SPEED 0x0200
kotakku 0:b1ce54272580 83 #define bmHUB_PORT_STATUS_PORT_HIGH_SPEED 0x0400
kotakku 0:b1ce54272580 84 #define bmHUB_PORT_STATUS_PORT_TEST 0x0800
kotakku 0:b1ce54272580 85 #define bmHUB_PORT_STATUS_PORT_INDICATOR 0x1000
kotakku 0:b1ce54272580 86
kotakku 0:b1ce54272580 87 // Hub Port Status Change Bitmasks (used one byte instead of two)
kotakku 0:b1ce54272580 88 #define bmHUB_PORT_STATUS_C_PORT_CONNECTION 0x0001
kotakku 0:b1ce54272580 89 #define bmHUB_PORT_STATUS_C_PORT_ENABLE 0x0002
kotakku 0:b1ce54272580 90 #define bmHUB_PORT_STATUS_C_PORT_SUSPEND 0x0004
kotakku 0:b1ce54272580 91 #define bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT 0x0008
kotakku 0:b1ce54272580 92 #define bmHUB_PORT_STATUS_C_PORT_RESET 0x0010
kotakku 0:b1ce54272580 93
kotakku 0:b1ce54272580 94 // Hub Status Bitmasks (used one byte instead of two)
kotakku 0:b1ce54272580 95 #define bmHUB_STATUS_LOCAL_POWER_SOURCE 0x01
kotakku 0:b1ce54272580 96 #define bmHUB_STATUS_OVER_CURRENT 0x12
kotakku 0:b1ce54272580 97
kotakku 0:b1ce54272580 98 // Hub Status Change Bitmasks (used one byte instead of two)
kotakku 0:b1ce54272580 99 #define bmHUB_STATUS_C_LOCAL_POWER_SOURCE 0x01
kotakku 0:b1ce54272580 100 #define bmHUB_STATUS_C_OVER_CURRENT 0x12
kotakku 0:b1ce54272580 101
kotakku 0:b1ce54272580 102
kotakku 0:b1ce54272580 103 // Hub Port Configuring Substates
kotakku 0:b1ce54272580 104 #define USB_STATE_HUB_PORT_CONFIGURING 0xb0
kotakku 0:b1ce54272580 105 #define USB_STATE_HUB_PORT_POWERED_OFF 0xb1
kotakku 0:b1ce54272580 106 #define USB_STATE_HUB_PORT_WAIT_FOR_POWER_GOOD 0xb2
kotakku 0:b1ce54272580 107 #define USB_STATE_HUB_PORT_DISCONNECTED 0xb3
kotakku 0:b1ce54272580 108 #define USB_STATE_HUB_PORT_DISABLED 0xb4
kotakku 0:b1ce54272580 109 #define USB_STATE_HUB_PORT_RESETTING 0xb5
kotakku 0:b1ce54272580 110 #define USB_STATE_HUB_PORT_ENABLED 0xb6
kotakku 0:b1ce54272580 111
kotakku 0:b1ce54272580 112 // Additional Error Codes
kotakku 0:b1ce54272580 113 #define HUB_ERROR_PORT_HAS_BEEN_RESET 0xb1
kotakku 0:b1ce54272580 114
kotakku 0:b1ce54272580 115 // The bit mask to check for all necessary state bits
kotakku 0:b1ce54272580 116 #define bmHUB_PORT_STATUS_ALL_MAIN ((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION | bmHUB_PORT_STATUS_C_PORT_ENABLE | bmHUB_PORT_STATUS_C_PORT_SUSPEND | bmHUB_PORT_STATUS_C_PORT_RESET) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_SUSPEND)
kotakku 0:b1ce54272580 117
kotakku 0:b1ce54272580 118 // Bit mask to check for DISABLED state in HubEvent::bmStatus field
kotakku 0:b1ce54272580 119 #define bmHUB_PORT_STATE_CHECK_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_SUSPEND)
kotakku 0:b1ce54272580 120
kotakku 0:b1ce54272580 121 // Hub Port States
kotakku 0:b1ce54272580 122 #define bmHUB_PORT_STATE_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
kotakku 0:b1ce54272580 123
kotakku 0:b1ce54272580 124 // Hub Port Events
kotakku 0:b1ce54272580 125 #define bmHUB_PORT_EVENT_CONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
kotakku 0:b1ce54272580 126 #define bmHUB_PORT_EVENT_DISCONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER)
kotakku 0:b1ce54272580 127 #define bmHUB_PORT_EVENT_RESET_COMPLETE (((0UL | bmHUB_PORT_STATUS_C_PORT_RESET) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION)
kotakku 0:b1ce54272580 128
kotakku 0:b1ce54272580 129 #define bmHUB_PORT_EVENT_LS_CONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_LOW_SPEED)
kotakku 0:b1ce54272580 130 #define bmHUB_PORT_EVENT_LS_RESET_COMPLETE (((0UL | bmHUB_PORT_STATUS_C_PORT_RESET) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_LOW_SPEED)
kotakku 0:b1ce54272580 131 #define bmHUB_PORT_EVENT_LS_PORT_ENABLED (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION | bmHUB_PORT_STATUS_C_PORT_ENABLE) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_LOW_SPEED)
kotakku 0:b1ce54272580 132
kotakku 0:b1ce54272580 133 struct HubDescriptor {
kotakku 0:b1ce54272580 134 uint8_t bDescLength; // descriptor length
kotakku 0:b1ce54272580 135 uint8_t bDescriptorType; // descriptor type
kotakku 0:b1ce54272580 136 uint8_t bNbrPorts; // number of ports a hub equiped with
kotakku 0:b1ce54272580 137
kotakku 0:b1ce54272580 138 struct {
kotakku 0:b1ce54272580 139 uint16_t LogPwrSwitchMode : 2;
kotakku 0:b1ce54272580 140 uint16_t CompoundDevice : 1;
kotakku 0:b1ce54272580 141 uint16_t OverCurrentProtectMode : 2;
kotakku 0:b1ce54272580 142 uint16_t TTThinkTime : 2;
kotakku 0:b1ce54272580 143 uint16_t PortIndicatorsSupported : 1;
kotakku 0:b1ce54272580 144 uint16_t Reserved : 8;
kotakku 0:b1ce54272580 145 } __attribute__((packed));
kotakku 0:b1ce54272580 146
kotakku 0:b1ce54272580 147 uint8_t bPwrOn2PwrGood;
kotakku 0:b1ce54272580 148 uint8_t bHubContrCurrent;
kotakku 0:b1ce54272580 149 } __attribute__((packed));
kotakku 0:b1ce54272580 150
kotakku 0:b1ce54272580 151 struct HubEvent {
kotakku 0:b1ce54272580 152
kotakku 0:b1ce54272580 153 union {
kotakku 0:b1ce54272580 154
kotakku 0:b1ce54272580 155 struct {
kotakku 0:b1ce54272580 156 uint16_t bmStatus; // port status bits
kotakku 0:b1ce54272580 157 uint16_t bmChange; // port status change bits
kotakku 0:b1ce54272580 158 } __attribute__((packed));
kotakku 0:b1ce54272580 159 uint32_t bmEvent;
kotakku 0:b1ce54272580 160 uint8_t evtBuff[4];
kotakku 0:b1ce54272580 161 };
kotakku 0:b1ce54272580 162 } __attribute__((packed));
kotakku 0:b1ce54272580 163
kotakku 0:b1ce54272580 164 class USBHub : USBDeviceConfig {
kotakku 0:b1ce54272580 165 static bool bResetInitiated; // True when reset is triggered
kotakku 0:b1ce54272580 166
kotakku 0:b1ce54272580 167 USB *pUsb; // USB class instance pointer
kotakku 0:b1ce54272580 168
kotakku 0:b1ce54272580 169 EpInfo epInfo[2]; // interrupt endpoint info structure
kotakku 0:b1ce54272580 170
kotakku 0:b1ce54272580 171 uint8_t bAddress; // address
kotakku 0:b1ce54272580 172 uint8_t bNbrPorts; // number of ports
kotakku 0:b1ce54272580 173 // uint8_t bInitState; // initialization state variable
kotakku 0:b1ce54272580 174 uint32_t qNextPollTime; // next poll time
kotakku 0:b1ce54272580 175 bool bPollEnable; // poll enable flag
kotakku 0:b1ce54272580 176
kotakku 0:b1ce54272580 177 uint8_t CheckHubStatus();
kotakku 0:b1ce54272580 178 uint8_t PortStatusChange(uint8_t port, HubEvent &evt);
kotakku 0:b1ce54272580 179
kotakku 0:b1ce54272580 180 public:
kotakku 0:b1ce54272580 181 USBHub(USB *p);
kotakku 0:b1ce54272580 182
kotakku 0:b1ce54272580 183 uint8_t ClearHubFeature(uint8_t fid);
kotakku 0:b1ce54272580 184 uint8_t ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel = 0);
kotakku 0:b1ce54272580 185 uint8_t GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr);
kotakku 0:b1ce54272580 186 uint8_t GetHubStatus(uint16_t nbytes, uint8_t* dataptr);
kotakku 0:b1ce54272580 187 uint8_t GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t* dataptr);
kotakku 0:b1ce54272580 188 uint8_t SetHubDescriptor(uint8_t port, uint16_t nbytes, uint8_t* dataptr);
kotakku 0:b1ce54272580 189 uint8_t SetHubFeature(uint8_t fid);
kotakku 0:b1ce54272580 190 uint8_t SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel = 0);
kotakku 0:b1ce54272580 191
kotakku 0:b1ce54272580 192 void PrintHubStatus();
kotakku 0:b1ce54272580 193
kotakku 0:b1ce54272580 194 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
kotakku 0:b1ce54272580 195 uint8_t Release();
kotakku 0:b1ce54272580 196 uint8_t Poll();
kotakku 0:b1ce54272580 197 void ResetHubPort(uint8_t port);
kotakku 0:b1ce54272580 198
kotakku 0:b1ce54272580 199 virtual uint8_t GetAddress() {
kotakku 0:b1ce54272580 200 return bAddress;
kotakku 0:b1ce54272580 201 };
kotakku 0:b1ce54272580 202
kotakku 0:b1ce54272580 203 virtual bool DEVCLASSOK(uint8_t klass) {
kotakku 0:b1ce54272580 204 return (klass == 0x09);
kotakku 0:b1ce54272580 205 }
kotakku 0:b1ce54272580 206
kotakku 0:b1ce54272580 207 };
kotakku 0:b1ce54272580 208
kotakku 0:b1ce54272580 209 // Clear Hub Feature
kotakku 0:b1ce54272580 210
kotakku 0:b1ce54272580 211 inline uint8_t USBHub::ClearHubFeature(uint8_t fid) {
kotakku 0:b1ce54272580 212 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_CLEAR_HUB_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, 0, 0, 0, NULL, NULL));
kotakku 0:b1ce54272580 213 }
kotakku 0:b1ce54272580 214 // Clear Port Feature
kotakku 0:b1ce54272580 215
kotakku 0:b1ce54272580 216 inline uint8_t USBHub::ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel) {
kotakku 0:b1ce54272580 217 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_CLEAR_PORT_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, ((0x0000 | port) | (sel << 8)), 0, 0, NULL, NULL));
kotakku 0:b1ce54272580 218 }
kotakku 0:b1ce54272580 219 // Get Hub Descriptor
kotakku 0:b1ce54272580 220
kotakku 0:b1ce54272580 221 inline uint8_t USBHub::GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr) {
kotakku 0:b1ce54272580 222 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_HUB_DESCRIPTOR, USB_REQUEST_GET_DESCRIPTOR, index, 0x29, 0, nbytes, nbytes, dataptr, NULL));
kotakku 0:b1ce54272580 223 }
kotakku 0:b1ce54272580 224 // Get Hub Status
kotakku 0:b1ce54272580 225
kotakku 0:b1ce54272580 226 inline uint8_t USBHub::GetHubStatus(uint16_t nbytes, uint8_t* dataptr) {
kotakku 0:b1ce54272580 227 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_HUB_STATUS, USB_REQUEST_GET_STATUS, 0, 0, 0x0000, nbytes, nbytes, dataptr, NULL));
kotakku 0:b1ce54272580 228 }
kotakku 0:b1ce54272580 229 // Get Port Status
kotakku 0:b1ce54272580 230
kotakku 0:b1ce54272580 231 inline uint8_t USBHub::GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t* dataptr) {
kotakku 0:b1ce54272580 232 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_PORT_STATUS, USB_REQUEST_GET_STATUS, 0, 0, port, nbytes, nbytes, dataptr, NULL));
kotakku 0:b1ce54272580 233 }
kotakku 0:b1ce54272580 234 // Set Hub Descriptor
kotakku 0:b1ce54272580 235
kotakku 0:b1ce54272580 236 inline uint8_t USBHub::SetHubDescriptor(uint8_t port, uint16_t nbytes, uint8_t* dataptr) {
kotakku 0:b1ce54272580 237 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_HUB_DESCRIPTOR, USB_REQUEST_SET_DESCRIPTOR, 0, 0, port, nbytes, nbytes, dataptr, NULL));
kotakku 0:b1ce54272580 238 }
kotakku 0:b1ce54272580 239 // Set Hub Feature
kotakku 0:b1ce54272580 240
kotakku 0:b1ce54272580 241 inline uint8_t USBHub::SetHubFeature(uint8_t fid) {
kotakku 0:b1ce54272580 242 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_HUB_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, 0, 0, 0, NULL, NULL));
kotakku 0:b1ce54272580 243 }
kotakku 0:b1ce54272580 244 // Set Port Feature
kotakku 0:b1ce54272580 245
kotakku 0:b1ce54272580 246 inline uint8_t USBHub::SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel) {
kotakku 0:b1ce54272580 247 return ( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_PORT_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, (((0x0000 | sel) << 8) | port), 0, 0, NULL, NULL));
kotakku 0:b1ce54272580 248 }
kotakku 0:b1ce54272580 249
kotakku 0:b1ce54272580 250 void PrintHubPortStatus(USB *usbptr, uint8_t addr, uint8_t port, bool print_changes = false);
kotakku 0:b1ce54272580 251
kotakku 0:b1ce54272580 252 #endif // __USBHUB_H__
kotakku 0:b1ce54272580 253