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 #include "usbhub.h"
kotakku 0:b1ce54272580 18
kotakku 0:b1ce54272580 19 bool USBHub::bResetInitiated = false;
kotakku 0:b1ce54272580 20
kotakku 0:b1ce54272580 21 USBHub::USBHub(USB *p) :
kotakku 0:b1ce54272580 22 pUsb(p),
kotakku 0:b1ce54272580 23 bAddress(0),
kotakku 0:b1ce54272580 24 bNbrPorts(0),
kotakku 0:b1ce54272580 25 //bInitState(0),
kotakku 0:b1ce54272580 26 qNextPollTime(0),
kotakku 0:b1ce54272580 27 bPollEnable(false) {
kotakku 0:b1ce54272580 28 epInfo[0].epAddr = 0;
kotakku 0:b1ce54272580 29 epInfo[0].maxPktSize = 8;
kotakku 0:b1ce54272580 30 epInfo[0].bmSndToggle = 0;
kotakku 0:b1ce54272580 31 epInfo[0].bmRcvToggle = 0;
kotakku 0:b1ce54272580 32 epInfo[0].bmNakPower = USB_NAK_MAX_POWER;
kotakku 0:b1ce54272580 33
kotakku 0:b1ce54272580 34 epInfo[1].epAddr = 1;
kotakku 0:b1ce54272580 35 epInfo[1].maxPktSize = 8; //kludge
kotakku 0:b1ce54272580 36 epInfo[1].bmSndToggle = 0;
kotakku 0:b1ce54272580 37 epInfo[1].bmRcvToggle = 0;
kotakku 0:b1ce54272580 38 epInfo[1].bmNakPower = USB_NAK_NOWAIT;
kotakku 0:b1ce54272580 39
kotakku 0:b1ce54272580 40 if(pUsb)
kotakku 0:b1ce54272580 41 pUsb->RegisterDeviceClass(this);
kotakku 0:b1ce54272580 42 }
kotakku 0:b1ce54272580 43
kotakku 0:b1ce54272580 44 uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed) {
kotakku 0:b1ce54272580 45 uint8_t buf[32];
kotakku 0:b1ce54272580 46 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
kotakku 0:b1ce54272580 47 HubDescriptor* hd = reinterpret_cast<HubDescriptor*>(buf);
kotakku 0:b1ce54272580 48 USB_CONFIGURATION_DESCRIPTOR * ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(buf);
kotakku 0:b1ce54272580 49 uint8_t rcode;
kotakku 0:b1ce54272580 50 UsbDevice *p = NULL;
kotakku 0:b1ce54272580 51 EpInfo *oldep_ptr = NULL;
kotakku 0:b1ce54272580 52 uint8_t len = 0;
kotakku 0:b1ce54272580 53 uint16_t cd_len = 0;
kotakku 0:b1ce54272580 54
kotakku 0:b1ce54272580 55 //USBTRACE("\r\nHub Init Start ");
kotakku 0:b1ce54272580 56 //D_PrintHex<uint8_t > (bInitState, 0x80);
kotakku 0:b1ce54272580 57
kotakku 0:b1ce54272580 58 AddressPool &addrPool = pUsb->GetAddressPool();
kotakku 0:b1ce54272580 59
kotakku 0:b1ce54272580 60 //switch (bInitState) {
kotakku 0:b1ce54272580 61 // case 0:
kotakku 0:b1ce54272580 62 if(bAddress)
kotakku 0:b1ce54272580 63 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
kotakku 0:b1ce54272580 64
kotakku 0:b1ce54272580 65 // Get pointer to pseudo device with address 0 assigned
kotakku 0:b1ce54272580 66 p = addrPool.GetUsbDevicePtr(0);
kotakku 0:b1ce54272580 67
kotakku 0:b1ce54272580 68 if(!p)
kotakku 0:b1ce54272580 69 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
kotakku 0:b1ce54272580 70
kotakku 0:b1ce54272580 71 if(!p->epinfo)
kotakku 0:b1ce54272580 72 return USB_ERROR_EPINFO_IS_NULL;
kotakku 0:b1ce54272580 73
kotakku 0:b1ce54272580 74 // Save old pointer to EP_RECORD of address 0
kotakku 0:b1ce54272580 75 oldep_ptr = p->epinfo;
kotakku 0:b1ce54272580 76
kotakku 0:b1ce54272580 77 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
kotakku 0:b1ce54272580 78 p->epinfo = epInfo;
kotakku 0:b1ce54272580 79
kotakku 0:b1ce54272580 80 p->lowspeed = lowspeed;
kotakku 0:b1ce54272580 81
kotakku 0:b1ce54272580 82 // Get device descriptor
kotakku 0:b1ce54272580 83 rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
kotakku 0:b1ce54272580 84
kotakku 0:b1ce54272580 85 p->lowspeed = false;
kotakku 0:b1ce54272580 86
kotakku 0:b1ce54272580 87 if(!rcode)
kotakku 0:b1ce54272580 88 len = (buf[0] > 32) ? 32 : buf[0];
kotakku 0:b1ce54272580 89
kotakku 0:b1ce54272580 90 if(rcode) {
kotakku 0:b1ce54272580 91 // Restore p->epinfo
kotakku 0:b1ce54272580 92 p->epinfo = oldep_ptr;
kotakku 0:b1ce54272580 93 return rcode;
kotakku 0:b1ce54272580 94 }
kotakku 0:b1ce54272580 95
kotakku 0:b1ce54272580 96 // Extract device class from device descriptor
kotakku 0:b1ce54272580 97 // If device class is not a hub return
kotakku 0:b1ce54272580 98 if(udd->bDeviceClass != 0x09)
kotakku 0:b1ce54272580 99 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
kotakku 0:b1ce54272580 100
kotakku 0:b1ce54272580 101 // Allocate new address according to device class
kotakku 0:b1ce54272580 102 bAddress = addrPool.AllocAddress(parent, (udd->bDeviceClass == 0x09) ? true : false, port);
kotakku 0:b1ce54272580 103
kotakku 0:b1ce54272580 104 if(!bAddress)
kotakku 0:b1ce54272580 105 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
kotakku 0:b1ce54272580 106
kotakku 0:b1ce54272580 107 // Extract Max Packet Size from the device descriptor
kotakku 0:b1ce54272580 108 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
kotakku 0:b1ce54272580 109
kotakku 0:b1ce54272580 110 // Assign new address to the device
kotakku 0:b1ce54272580 111 rcode = pUsb->setAddr(0, 0, bAddress);
kotakku 0:b1ce54272580 112
kotakku 0:b1ce54272580 113 if(rcode) {
kotakku 0:b1ce54272580 114 // Restore p->epinfo
kotakku 0:b1ce54272580 115 p->epinfo = oldep_ptr;
kotakku 0:b1ce54272580 116 addrPool.FreeAddress(bAddress);
kotakku 0:b1ce54272580 117 bAddress = 0;
kotakku 0:b1ce54272580 118 return rcode;
kotakku 0:b1ce54272580 119 }
kotakku 0:b1ce54272580 120
kotakku 0:b1ce54272580 121 //USBTRACE2("\r\nHub address: ", bAddress );
kotakku 0:b1ce54272580 122
kotakku 0:b1ce54272580 123 // Restore p->epinfo
kotakku 0:b1ce54272580 124 p->epinfo = oldep_ptr;
kotakku 0:b1ce54272580 125
kotakku 0:b1ce54272580 126 if(len)
kotakku 0:b1ce54272580 127 rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
kotakku 0:b1ce54272580 128
kotakku 0:b1ce54272580 129 if(rcode)
kotakku 0:b1ce54272580 130 goto FailGetDevDescr;
kotakku 0:b1ce54272580 131
kotakku 0:b1ce54272580 132 // Assign epInfo to epinfo pointer
kotakku 0:b1ce54272580 133 rcode = pUsb->setEpInfoEntry(bAddress, 2, epInfo);
kotakku 0:b1ce54272580 134
kotakku 0:b1ce54272580 135 if(rcode)
kotakku 0:b1ce54272580 136 goto FailSetDevTblEntry;
kotakku 0:b1ce54272580 137
kotakku 0:b1ce54272580 138 // bInitState = 1;
kotakku 0:b1ce54272580 139
kotakku 0:b1ce54272580 140 // case 1:
kotakku 0:b1ce54272580 141 // Get hub descriptor
kotakku 0:b1ce54272580 142 rcode = GetHubDescriptor(0, 8, buf);
kotakku 0:b1ce54272580 143
kotakku 0:b1ce54272580 144 if(rcode)
kotakku 0:b1ce54272580 145 goto FailGetHubDescr;
kotakku 0:b1ce54272580 146
kotakku 0:b1ce54272580 147 // Save number of ports for future use
kotakku 0:b1ce54272580 148 bNbrPorts = hd->bNbrPorts;
kotakku 0:b1ce54272580 149
kotakku 0:b1ce54272580 150 // bInitState = 2;
kotakku 0:b1ce54272580 151
kotakku 0:b1ce54272580 152 // case 2:
kotakku 0:b1ce54272580 153 // Read configuration Descriptor in Order To Obtain Proper Configuration Value
kotakku 0:b1ce54272580 154 rcode = pUsb->getConfDescr(bAddress, 0, 8, 0, buf);
kotakku 0:b1ce54272580 155
kotakku 0:b1ce54272580 156 if(!rcode) {
kotakku 0:b1ce54272580 157 cd_len = ucd->wTotalLength;
kotakku 0:b1ce54272580 158 rcode = pUsb->getConfDescr(bAddress, 0, cd_len, 0, buf);
kotakku 0:b1ce54272580 159 }
kotakku 0:b1ce54272580 160 if(rcode)
kotakku 0:b1ce54272580 161 goto FailGetConfDescr;
kotakku 0:b1ce54272580 162
kotakku 0:b1ce54272580 163 // The following code is of no practical use in real life applications.
kotakku 0:b1ce54272580 164 // It only intended for the usb protocol sniffer to properly parse hub-class requests.
kotakku 0:b1ce54272580 165 {
kotakku 0:b1ce54272580 166 uint8_t buf2[24];
kotakku 0:b1ce54272580 167
kotakku 0:b1ce54272580 168 rcode = pUsb->getConfDescr(bAddress, 0, buf[0], 0, buf2);
kotakku 0:b1ce54272580 169
kotakku 0:b1ce54272580 170 if(rcode)
kotakku 0:b1ce54272580 171 goto FailGetConfDescr;
kotakku 0:b1ce54272580 172 }
kotakku 0:b1ce54272580 173
kotakku 0:b1ce54272580 174 // Set Configuration Value
kotakku 0:b1ce54272580 175 rcode = pUsb->setConf(bAddress, 0, buf[5]);
kotakku 0:b1ce54272580 176
kotakku 0:b1ce54272580 177 if(rcode)
kotakku 0:b1ce54272580 178 goto FailSetConfDescr;
kotakku 0:b1ce54272580 179
kotakku 0:b1ce54272580 180 // bInitState = 3;
kotakku 0:b1ce54272580 181
kotakku 0:b1ce54272580 182 // case 3:
kotakku 0:b1ce54272580 183 // Power on all ports
kotakku 0:b1ce54272580 184 for(uint8_t j = 1; j <= bNbrPorts; j++)
kotakku 0:b1ce54272580 185 SetPortFeature(HUB_FEATURE_PORT_POWER, j, 0); //HubPortPowerOn(j);
kotakku 0:b1ce54272580 186
kotakku 0:b1ce54272580 187 pUsb->SetHubPreMask();
kotakku 0:b1ce54272580 188 bPollEnable = true;
kotakku 0:b1ce54272580 189 // bInitState = 0;
kotakku 0:b1ce54272580 190 //}
kotakku 0:b1ce54272580 191 //bInitState = 0;
kotakku 0:b1ce54272580 192 //USBTRACE("...OK\r\n");
kotakku 0:b1ce54272580 193 return 0;
kotakku 0:b1ce54272580 194
kotakku 0:b1ce54272580 195 // Oleg, No debugging?? -- xxxajk
kotakku 0:b1ce54272580 196 FailGetDevDescr:
kotakku 0:b1ce54272580 197 goto Fail;
kotakku 0:b1ce54272580 198
kotakku 0:b1ce54272580 199 FailSetDevTblEntry:
kotakku 0:b1ce54272580 200 goto Fail;
kotakku 0:b1ce54272580 201
kotakku 0:b1ce54272580 202 FailGetHubDescr:
kotakku 0:b1ce54272580 203 goto Fail;
kotakku 0:b1ce54272580 204
kotakku 0:b1ce54272580 205 FailGetConfDescr:
kotakku 0:b1ce54272580 206 goto Fail;
kotakku 0:b1ce54272580 207
kotakku 0:b1ce54272580 208 FailSetConfDescr:
kotakku 0:b1ce54272580 209 goto Fail;
kotakku 0:b1ce54272580 210
kotakku 0:b1ce54272580 211 Fail:
kotakku 0:b1ce54272580 212 USBTRACE("...FAIL\r\n");
kotakku 0:b1ce54272580 213 return rcode;
kotakku 0:b1ce54272580 214 }
kotakku 0:b1ce54272580 215
kotakku 0:b1ce54272580 216 uint8_t USBHub::Release() {
kotakku 0:b1ce54272580 217 pUsb->GetAddressPool().FreeAddress(bAddress);
kotakku 0:b1ce54272580 218
kotakku 0:b1ce54272580 219 if(bAddress == 0x41)
kotakku 0:b1ce54272580 220 pUsb->SetHubPreMask();
kotakku 0:b1ce54272580 221
kotakku 0:b1ce54272580 222 bAddress = 0;
kotakku 0:b1ce54272580 223 bNbrPorts = 0;
kotakku 0:b1ce54272580 224 qNextPollTime = 0;
kotakku 0:b1ce54272580 225 bPollEnable = false;
kotakku 0:b1ce54272580 226 return 0;
kotakku 0:b1ce54272580 227 }
kotakku 0:b1ce54272580 228
kotakku 0:b1ce54272580 229 uint8_t USBHub::Poll() {
kotakku 0:b1ce54272580 230 uint8_t rcode = 0;
kotakku 0:b1ce54272580 231
kotakku 0:b1ce54272580 232 if(!bPollEnable)
kotakku 0:b1ce54272580 233 return 0;
kotakku 0:b1ce54272580 234
kotakku 0:b1ce54272580 235 if(((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L)) {
kotakku 0:b1ce54272580 236 rcode = CheckHubStatus();
kotakku 0:b1ce54272580 237 qNextPollTime = (uint32_t)millis() + 100;
kotakku 0:b1ce54272580 238 }
kotakku 0:b1ce54272580 239 return rcode;
kotakku 0:b1ce54272580 240 }
kotakku 0:b1ce54272580 241
kotakku 0:b1ce54272580 242 uint8_t USBHub::CheckHubStatus() {
kotakku 0:b1ce54272580 243 uint8_t rcode;
kotakku 0:b1ce54272580 244 uint8_t buf[8];
kotakku 0:b1ce54272580 245 uint16_t read = 1;
kotakku 0:b1ce54272580 246
kotakku 0:b1ce54272580 247 rcode = pUsb->inTransfer(bAddress, 1, &read, buf);
kotakku 0:b1ce54272580 248
kotakku 0:b1ce54272580 249 if(rcode)
kotakku 0:b1ce54272580 250 return rcode;
kotakku 0:b1ce54272580 251
kotakku 0:b1ce54272580 252 //if (buf[0] & 0x01) // Hub Status Change
kotakku 0:b1ce54272580 253 //{
kotakku 0:b1ce54272580 254 // pUsb->PrintHubStatus(addr);
kotakku 0:b1ce54272580 255 // rcode = GetHubStatus(1, 0, 1, 4, buf);
kotakku 0:b1ce54272580 256 // if (rcode)
kotakku 0:b1ce54272580 257 // {
kotakku 0:b1ce54272580 258 // ////USB_HOST_SERIAL.print("GetHubStatus Error");
kotakku 0:b1ce54272580 259 // ////USB_HOST_SERIAL.println(rcode, HEX);
kotakku 0:b1ce54272580 260 // return rcode;
kotakku 0:b1ce54272580 261 // }
kotakku 0:b1ce54272580 262 //}
kotakku 0:b1ce54272580 263 for(uint8_t port = 1, mask = 0x02; port < 8; mask <<= 1, port++) {
kotakku 0:b1ce54272580 264 if(buf[0] & mask) {
kotakku 0:b1ce54272580 265 HubEvent evt;
kotakku 0:b1ce54272580 266 evt.bmEvent = 0;
kotakku 0:b1ce54272580 267
kotakku 0:b1ce54272580 268 rcode = GetPortStatus(port, 4, evt.evtBuff);
kotakku 0:b1ce54272580 269
kotakku 0:b1ce54272580 270 if(rcode)
kotakku 0:b1ce54272580 271 continue;
kotakku 0:b1ce54272580 272
kotakku 0:b1ce54272580 273 rcode = PortStatusChange(port, evt);
kotakku 0:b1ce54272580 274
kotakku 0:b1ce54272580 275 if(rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
kotakku 0:b1ce54272580 276 return 0;
kotakku 0:b1ce54272580 277
kotakku 0:b1ce54272580 278 if(rcode)
kotakku 0:b1ce54272580 279 return rcode;
kotakku 0:b1ce54272580 280 }
kotakku 0:b1ce54272580 281 } // for
kotakku 0:b1ce54272580 282
kotakku 0:b1ce54272580 283 for(uint8_t port = 1; port <= bNbrPorts; port++) {
kotakku 0:b1ce54272580 284 HubEvent evt;
kotakku 0:b1ce54272580 285 evt.bmEvent = 0;
kotakku 0:b1ce54272580 286
kotakku 0:b1ce54272580 287 rcode = GetPortStatus(port, 4, evt.evtBuff);
kotakku 0:b1ce54272580 288
kotakku 0:b1ce54272580 289 if(rcode)
kotakku 0:b1ce54272580 290 continue;
kotakku 0:b1ce54272580 291
kotakku 0:b1ce54272580 292 if((evt.bmStatus & bmHUB_PORT_STATE_CHECK_DISABLED) != bmHUB_PORT_STATE_DISABLED)
kotakku 0:b1ce54272580 293 continue;
kotakku 0:b1ce54272580 294
kotakku 0:b1ce54272580 295 // Emulate connection event for the port
kotakku 0:b1ce54272580 296 evt.bmChange |= bmHUB_PORT_STATUS_C_PORT_CONNECTION;
kotakku 0:b1ce54272580 297
kotakku 0:b1ce54272580 298 rcode = PortStatusChange(port, evt);
kotakku 0:b1ce54272580 299
kotakku 0:b1ce54272580 300 if(rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
kotakku 0:b1ce54272580 301 return 0;
kotakku 0:b1ce54272580 302
kotakku 0:b1ce54272580 303 if(rcode)
kotakku 0:b1ce54272580 304 return rcode;
kotakku 0:b1ce54272580 305 } // for
kotakku 0:b1ce54272580 306 return 0;
kotakku 0:b1ce54272580 307 }
kotakku 0:b1ce54272580 308
kotakku 0:b1ce54272580 309 void USBHub::ResetHubPort(uint8_t port) {
kotakku 0:b1ce54272580 310 HubEvent evt;
kotakku 0:b1ce54272580 311 evt.bmEvent = 0;
kotakku 0:b1ce54272580 312 uint8_t rcode;
kotakku 0:b1ce54272580 313
kotakku 0:b1ce54272580 314 ClearPortFeature(HUB_FEATURE_C_PORT_ENABLE, port, 0);
kotakku 0:b1ce54272580 315 ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
kotakku 0:b1ce54272580 316 SetPortFeature(HUB_FEATURE_PORT_RESET, port, 0);
kotakku 0:b1ce54272580 317
kotakku 0:b1ce54272580 318
kotakku 0:b1ce54272580 319 for(int i = 0; i < 3; i++) {
kotakku 0:b1ce54272580 320 rcode = GetPortStatus(port, 4, evt.evtBuff);
kotakku 0:b1ce54272580 321 if(rcode) break; // Some kind of error, bail.
kotakku 0:b1ce54272580 322 if(evt.bmEvent == bmHUB_PORT_EVENT_RESET_COMPLETE || evt.bmEvent == bmHUB_PORT_EVENT_LS_RESET_COMPLETE) {
kotakku 0:b1ce54272580 323 break;
kotakku 0:b1ce54272580 324 }
kotakku 0:b1ce54272580 325 delay(100); // simulate polling.
kotakku 0:b1ce54272580 326 }
kotakku 0:b1ce54272580 327 ClearPortFeature(HUB_FEATURE_C_PORT_RESET, port, 0);
kotakku 0:b1ce54272580 328 ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
kotakku 0:b1ce54272580 329 delay(20);
kotakku 0:b1ce54272580 330 }
kotakku 0:b1ce54272580 331
kotakku 0:b1ce54272580 332 uint8_t USBHub::PortStatusChange(uint8_t port, HubEvent &evt) {
kotakku 0:b1ce54272580 333 switch(evt.bmEvent) {
kotakku 0:b1ce54272580 334 // Device connected event
kotakku 0:b1ce54272580 335 case bmHUB_PORT_EVENT_CONNECT:
kotakku 0:b1ce54272580 336 case bmHUB_PORT_EVENT_LS_CONNECT:
kotakku 0:b1ce54272580 337 if(bResetInitiated)
kotakku 0:b1ce54272580 338 return 0;
kotakku 0:b1ce54272580 339
kotakku 0:b1ce54272580 340 ClearPortFeature(HUB_FEATURE_C_PORT_ENABLE, port, 0);
kotakku 0:b1ce54272580 341 ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
kotakku 0:b1ce54272580 342 SetPortFeature(HUB_FEATURE_PORT_RESET, port, 0);
kotakku 0:b1ce54272580 343 bResetInitiated = true;
kotakku 0:b1ce54272580 344 return HUB_ERROR_PORT_HAS_BEEN_RESET;
kotakku 0:b1ce54272580 345
kotakku 0:b1ce54272580 346 // Device disconnected event
kotakku 0:b1ce54272580 347 case bmHUB_PORT_EVENT_DISCONNECT:
kotakku 0:b1ce54272580 348 ClearPortFeature(HUB_FEATURE_C_PORT_ENABLE, port, 0);
kotakku 0:b1ce54272580 349 ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
kotakku 0:b1ce54272580 350 bResetInitiated = false;
kotakku 0:b1ce54272580 351
kotakku 0:b1ce54272580 352 UsbDeviceAddress a;
kotakku 0:b1ce54272580 353 a.devAddress = 0;
kotakku 0:b1ce54272580 354 a.bmHub = 0;
kotakku 0:b1ce54272580 355 a.bmParent = bAddress;
kotakku 0:b1ce54272580 356 a.bmAddress = port;
kotakku 0:b1ce54272580 357 pUsb->ReleaseDevice(a.devAddress);
kotakku 0:b1ce54272580 358 return 0;
kotakku 0:b1ce54272580 359
kotakku 0:b1ce54272580 360 // Reset complete event
kotakku 0:b1ce54272580 361 case bmHUB_PORT_EVENT_RESET_COMPLETE:
kotakku 0:b1ce54272580 362 case bmHUB_PORT_EVENT_LS_RESET_COMPLETE:
kotakku 0:b1ce54272580 363 ClearPortFeature(HUB_FEATURE_C_PORT_RESET, port, 0);
kotakku 0:b1ce54272580 364 ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
kotakku 0:b1ce54272580 365
kotakku 0:b1ce54272580 366 delay(20);
kotakku 0:b1ce54272580 367
kotakku 0:b1ce54272580 368 a.devAddress = bAddress;
kotakku 0:b1ce54272580 369
kotakku 0:b1ce54272580 370 pUsb->Configuring(a.bmAddress, port, (evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED));
kotakku 0:b1ce54272580 371 bResetInitiated = false;
kotakku 0:b1ce54272580 372 break;
kotakku 0:b1ce54272580 373
kotakku 0:b1ce54272580 374 } // switch (evt.bmEvent)
kotakku 0:b1ce54272580 375 return 0;
kotakku 0:b1ce54272580 376 }
kotakku 0:b1ce54272580 377
kotakku 0:b1ce54272580 378 void PrintHubPortStatus(USBHub *hubptr, uint8_t addr __attribute__((unused)), uint8_t port, bool print_changes) {
kotakku 0:b1ce54272580 379 uint8_t rcode = 0;
kotakku 0:b1ce54272580 380 HubEvent evt;
kotakku 0:b1ce54272580 381
kotakku 0:b1ce54272580 382 rcode = hubptr->GetPortStatus(port, 4, evt.evtBuff);
kotakku 0:b1ce54272580 383
kotakku 0:b1ce54272580 384 if(rcode) {
kotakku 0:b1ce54272580 385 ////////USB_HOST_SERIAL.println("ERROR!");
kotakku 0:b1ce54272580 386 return;
kotakku 0:b1ce54272580 387 }
kotakku 0:b1ce54272580 388 //USB_HOST_SERIAL.print("\r\nPort ");
kotakku 0:b1ce54272580 389 //USB_HOST_SERIAL.println(port, DEC);
kotakku 0:b1ce54272580 390
kotakku 0:b1ce54272580 391 //USB_HOST_SERIAL.println("Status");
kotakku 0:b1ce54272580 392 //USB_HOST_SERIAL.print("CONNECTION:\t");
kotakku 0:b1ce54272580 393 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_CONNECTION) > 0, DEC);
kotakku 0:b1ce54272580 394 //USB_HOST_SERIAL.print("ENABLE:\t\t");
kotakku 0:b1ce54272580 395 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_ENABLE) > 0, DEC);
kotakku 0:b1ce54272580 396 //USB_HOST_SERIAL.print("SUSPEND:\t");
kotakku 0:b1ce54272580 397 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_SUSPEND) > 0, DEC);
kotakku 0:b1ce54272580 398 //USB_HOST_SERIAL.print("OVER_CURRENT:\t");
kotakku 0:b1ce54272580 399 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_OVER_CURRENT) > 0, DEC);
kotakku 0:b1ce54272580 400 //USB_HOST_SERIAL.print("RESET:\t\t");
kotakku 0:b1ce54272580 401 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_RESET) > 0, DEC);
kotakku 0:b1ce54272580 402 //USB_HOST_SERIAL.print("POWER:\t\t");
kotakku 0:b1ce54272580 403 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_POWER) > 0, DEC);
kotakku 0:b1ce54272580 404 //USB_HOST_SERIAL.print("LOW_SPEED:\t");
kotakku 0:b1ce54272580 405 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED) > 0, DEC);
kotakku 0:b1ce54272580 406 //USB_HOST_SERIAL.print("HIGH_SPEED:\t");
kotakku 0:b1ce54272580 407 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_HIGH_SPEED) > 0, DEC);
kotakku 0:b1ce54272580 408 //USB_HOST_SERIAL.print("TEST:\t\t");
kotakku 0:b1ce54272580 409 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_TEST) > 0, DEC);
kotakku 0:b1ce54272580 410 //USB_HOST_SERIAL.print("INDICATOR:\t");
kotakku 0:b1ce54272580 411 //USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_INDICATOR) > 0, DEC);
kotakku 0:b1ce54272580 412
kotakku 0:b1ce54272580 413 if(!print_changes)
kotakku 0:b1ce54272580 414 return;
kotakku 0:b1ce54272580 415
kotakku 0:b1ce54272580 416 //USB_HOST_SERIAL.println("\r\nChange");
kotakku 0:b1ce54272580 417 //USB_HOST_SERIAL.print("CONNECTION:\t");
kotakku 0:b1ce54272580 418 //USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_CONNECTION) > 0, DEC);
kotakku 0:b1ce54272580 419 //USB_HOST_SERIAL.print("ENABLE:\t\t");
kotakku 0:b1ce54272580 420 //USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_ENABLE) > 0, DEC);
kotakku 0:b1ce54272580 421 //USB_HOST_SERIAL.print("SUSPEND:\t");
kotakku 0:b1ce54272580 422 //USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_SUSPEND) > 0, DEC);
kotakku 0:b1ce54272580 423 //USB_HOST_SERIAL.print("OVER_CURRENT:\t");
kotakku 0:b1ce54272580 424 //USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT) > 0, DEC);
kotakku 0:b1ce54272580 425 //USB_HOST_SERIAL.print("RESET:\t\t");
kotakku 0:b1ce54272580 426 //USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_RESET) > 0, DEC);
kotakku 0:b1ce54272580 427 }
kotakku 0:b1ce54272580 428